mirror of
https://github.com/postgres/postgres.git
synced 2025-07-15 19:21:59 +03:00
Add plan_cache_mode setting
This allows overriding the choice of custom or generic plan. Author: Pavel Stehule <pavel.stehule@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/CAFj8pRAGLaiEm8ur5DWEBo7qHRWTk9HxkuUAz00CZZtJj-LkCA%40mail.gmail.com
This commit is contained in:
@ -278,3 +278,80 @@ drop table pc_list_part_1;
|
||||
execute pstmt_def_insert(1);
|
||||
drop table pc_list_parted, pc_list_part_null;
|
||||
deallocate pstmt_def_insert;
|
||||
-- Test plan_cache_mode
|
||||
create table test_mode (a int);
|
||||
insert into test_mode select 1 from generate_series(1,1000) union all select 2;
|
||||
create index on test_mode (a);
|
||||
analyze test_mode;
|
||||
prepare test_mode_pp (int) as select count(*) from test_mode where a = $1;
|
||||
-- up to 5 executions, custom plan is used
|
||||
explain (costs off) execute test_mode_pp(2);
|
||||
QUERY PLAN
|
||||
----------------------------------------------------------
|
||||
Aggregate
|
||||
-> Index Only Scan using test_mode_a_idx on test_mode
|
||||
Index Cond: (a = 2)
|
||||
(3 rows)
|
||||
|
||||
-- force generic plan
|
||||
set plan_cache_mode to force_generic_plan;
|
||||
explain (costs off) execute test_mode_pp(2);
|
||||
QUERY PLAN
|
||||
-----------------------------
|
||||
Aggregate
|
||||
-> Seq Scan on test_mode
|
||||
Filter: (a = $1)
|
||||
(3 rows)
|
||||
|
||||
-- get to generic plan by 5 executions
|
||||
set plan_cache_mode to auto;
|
||||
execute test_mode_pp(1); -- 1x
|
||||
count
|
||||
-------
|
||||
1000
|
||||
(1 row)
|
||||
|
||||
execute test_mode_pp(1); -- 2x
|
||||
count
|
||||
-------
|
||||
1000
|
||||
(1 row)
|
||||
|
||||
execute test_mode_pp(1); -- 3x
|
||||
count
|
||||
-------
|
||||
1000
|
||||
(1 row)
|
||||
|
||||
execute test_mode_pp(1); -- 4x
|
||||
count
|
||||
-------
|
||||
1000
|
||||
(1 row)
|
||||
|
||||
execute test_mode_pp(1); -- 5x
|
||||
count
|
||||
-------
|
||||
1000
|
||||
(1 row)
|
||||
|
||||
-- we should now get a really bad plan
|
||||
explain (costs off) execute test_mode_pp(2);
|
||||
QUERY PLAN
|
||||
-----------------------------
|
||||
Aggregate
|
||||
-> Seq Scan on test_mode
|
||||
Filter: (a = $1)
|
||||
(3 rows)
|
||||
|
||||
-- but we can force a custom plan
|
||||
set plan_cache_mode to force_custom_plan;
|
||||
explain (costs off) execute test_mode_pp(2);
|
||||
QUERY PLAN
|
||||
----------------------------------------------------------
|
||||
Aggregate
|
||||
-> Index Only Scan using test_mode_a_idx on test_mode
|
||||
Index Cond: (a = 2)
|
||||
(3 rows)
|
||||
|
||||
drop table test_mode;
|
||||
|
@ -177,3 +177,36 @@ drop table pc_list_part_1;
|
||||
execute pstmt_def_insert(1);
|
||||
drop table pc_list_parted, pc_list_part_null;
|
||||
deallocate pstmt_def_insert;
|
||||
|
||||
-- Test plan_cache_mode
|
||||
|
||||
create table test_mode (a int);
|
||||
insert into test_mode select 1 from generate_series(1,1000) union all select 2;
|
||||
create index on test_mode (a);
|
||||
analyze test_mode;
|
||||
|
||||
prepare test_mode_pp (int) as select count(*) from test_mode where a = $1;
|
||||
|
||||
-- up to 5 executions, custom plan is used
|
||||
explain (costs off) execute test_mode_pp(2);
|
||||
|
||||
-- force generic plan
|
||||
set plan_cache_mode to force_generic_plan;
|
||||
explain (costs off) execute test_mode_pp(2);
|
||||
|
||||
-- get to generic plan by 5 executions
|
||||
set plan_cache_mode to auto;
|
||||
execute test_mode_pp(1); -- 1x
|
||||
execute test_mode_pp(1); -- 2x
|
||||
execute test_mode_pp(1); -- 3x
|
||||
execute test_mode_pp(1); -- 4x
|
||||
execute test_mode_pp(1); -- 5x
|
||||
|
||||
-- we should now get a really bad plan
|
||||
explain (costs off) execute test_mode_pp(2);
|
||||
|
||||
-- but we can force a custom plan
|
||||
set plan_cache_mode to force_custom_plan;
|
||||
explain (costs off) execute test_mode_pp(2);
|
||||
|
||||
drop table test_mode;
|
||||
|
Reference in New Issue
Block a user