-- -- Tests to exercise the plan caching/invalidation mechanism --
CREATE TEMP TABLE pcachetest ASSELECT * FROM int8_tbl;
-- create and use a cached plan
PREPARE prepstmt ASSELECT * FROM pcachetest;
EXECUTE prepstmt;
-- and one with parameters
PREPARE prepstmt2(bigint) ASSELECT * FROM pcachetest WHERE q1 = $1;
EXECUTE prepstmt2(123);
-- invalidate the plans and see what happens DROPTABLE pcachetest;
EXECUTE prepstmt;
EXECUTE prepstmt2(123);
-- recreate the temp table (this demonstrates that the raw plan is -- purely textual and doesn't depend on OIDs, for instance) CREATE TEMP TABLE pcachetest ASSELECT * FROM int8_tbl ORDERBY2;
EXECUTE prepstmt;
EXECUTE prepstmt2(123);
-- prepared statements should prevent change in output tupdesc, -- since clients probably aren't expecting that to change on the fly ALTERTABLE pcachetest ADDCOLUMN q3 bigint;
EXECUTE prepstmt;
EXECUTE prepstmt2(123);
-- but we're nice guys and will let you undo your mistake ALTERTABLE pcachetest DROPCOLUMN q3;
EXECUTE prepstmt;
EXECUTE prepstmt2(123);
-- Try it with a view, which isn't directly used in the resulting plan -- but should trigger invalidation anyway CREATE TEMP VIEW pcacheview AS SELECT * FROM pcachetest;
PREPARE vprep ASSELECT * FROM pcacheview;
EXECUTE vprep;
CREATEORREPLACE TEMP VIEW pcacheview AS SELECT q1, q2/2AS q2 FROM pcachetest;
EXECUTE vprep;
-- Check basic SPI plan invalidation
create function cache_test(int) returns intas $$ declare total int;
begin create temp table t1(f1 int); insertinto t1 values($1); insertinto t1 values(11); insertinto t1 values(12); insertinto t1 values(13); select sum(f1) into total from t1; droptable t1; return total;
end
$$ language plpgsql;
altertable s1.abc addcolumn f2 float8; -- force replan
execute p1;
dropschema s1 cascade; dropschema s2 cascade;
reset search_path;
-- Check that invalidation deals with regclass constants
create temp sequence seq;
prepare p2 asselect nextval('seq');
execute p2;
drop sequence seq;
create temp sequence seq;
execute p2;
-- Check DDL via SPI, immediately followed by SPI plan re-use -- (bug in original coding)
create function cachebug() returns void as $$ declare r int;
begin droptableifexists temptable cascade; create temp table temptable asselect * from generate_series(1,3) as f1; create temp view vv asselect * from temptable; for r inselect * from vv loop
raise notice '%', r;
end loop;
end$$ language plpgsql;
select cachebug(); select cachebug();
-- Check that addition or removal of any partition is correctly dealt with by -- default partition table when it is being used in prepared statement. createtable pc_list_parted (a int) partition by list(a); createtable pc_list_part_null partition of pc_list_parted forvaluesin (null); createtable pc_list_part_1 partition of pc_list_parted forvaluesin (1); createtable pc_list_part_def partition of pc_list_parted default;
prepare pstmt_def_insert (int) asinsertinto pc_list_part_def values($1); -- should fail
execute pstmt_def_insert(null);
execute pstmt_def_insert(1); createtable pc_list_part_2 partition of pc_list_parted forvaluesin (2);
execute pstmt_def_insert(2); altertable pc_list_parted detach partition pc_list_part_null; -- should be ok
execute pstmt_def_insert(null); droptable pc_list_part_1; -- should be ok
execute pstmt_def_insert(1); droptable pc_list_parted, pc_list_part_null;
deallocate pstmt_def_insert;
prepare test_mode_pp (int) asselect count(*) from test_mode where a = $1; select name, generic_plans, custom_plans from pg_prepared_statements where name = 'test_mode_pp';
-- up to 5 executions, custom plan is used set plan_cache_mode to auto; explain (costs off) execute test_mode_pp(2); select name, generic_plans, custom_plans from pg_prepared_statements where name = 'test_mode_pp';
-- force generic plan set plan_cache_mode to force_generic_plan; explain (costs off) execute test_mode_pp(2); select name, generic_plans, custom_plans from pg_prepared_statements where name = 'test_mode_pp';
-- 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 select name, generic_plans, custom_plans from pg_prepared_statements where name = 'test_mode_pp';
execute test_mode_pp(1); -- 5x select name, generic_plans, custom_plans from pg_prepared_statements where name = 'test_mode_pp';
-- 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); select name, generic_plans, custom_plans from pg_prepared_statements where name = 'test_mode_pp';
droptable test_mode;
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.18Bemerkung:
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.