Skip to content

Instantly share code, notes, and snippets.

@portnov
Created April 2, 2023 09:59
Show Gist options
  • Save portnov/3f640d92a3d89a6d1ca17e6961db6f7d to your computer and use it in GitHub Desktop.
Save portnov/3f640d92a3d89a6d1ca17e6961db6f7d to your computer and use it in GitHub Desktop.
cursor plans invalidation
drop table if exists tst_stats;
create table tst_stats (
id bigserial primary key,
value text
);
insert into tst_stats (value)
select 'value ' || (i*i)
from generate_series(1, 100) g (i);
analyze tst_stats;
prepare q_stats as
select value from tst_stats where id < 90;
explain (analyze,buffers)
execute q_stats;
/*
QUERY PLAN |
----------------------------------------------------------------------------------------------------+
Seq Scan on tst_stats (cost=0.00..2.25 rows=89 width=10) (actual time=0.009..0.017 rows=89 loops=1)|
Filter: (id < 90) |
Rows Removed by Filter: 11 |
Buffers: shared hit=1 |
Planning Time: 0.002 ms |
Execution Time: 0.038 ms |
*/
insert into tst_stats (value)
select 'x value ' || i
from generate_series(1, 100*1000) g (i);
analyze tst_stats;
explain (analyze,buffers)
execute q_stats;
/*
QUERY PLAN |
---------------------------------------------------------------------------------------------------------------------------+
Index Scan using tst_stats_pkey on tst_stats (cost=0.29..3.89 rows=80 width=13) (actual time=0.004..0.015 rows=89 loops=1)|
Index Cond: (id < 90) |
Buffers: shared hit=3 |
Planning: |
Buffers: shared hit=11 |
Planning Time: 0.111 ms |
Execution Time: 0.029 ms |
*/
deallocate q_stats;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment