]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/plancache.sql
Make use of plancache module for SPI plans. In particular, since plpgsql
[postgresql] / src / test / regress / sql / plancache.sql
1 --
2 -- Tests to exercise the plan caching/invalidation mechanism
3 --
4
5 CREATE TEMP TABLE foo AS SELECT * FROM int8_tbl;
6
7 -- create and use a cached plan
8 PREPARE prepstmt AS SELECT * FROM foo;
9
10 EXECUTE prepstmt;
11
12 -- and one with parameters
13 PREPARE prepstmt2(bigint) AS SELECT * FROM foo WHERE q1 = $1;
14
15 EXECUTE prepstmt2(123);
16
17 -- invalidate the plans and see what happens
18 DROP TABLE foo;
19
20 EXECUTE prepstmt;
21 EXECUTE prepstmt2(123);
22
23 -- recreate the temp table (this demonstrates that the raw plan is
24 -- purely textual and doesn't depend on OIDs, for instance)
25 CREATE TEMP TABLE foo AS SELECT * FROM int8_tbl ORDER BY 2;
26
27 EXECUTE prepstmt;
28 EXECUTE prepstmt2(123);
29
30 -- prepared statements should prevent change in output tupdesc,
31 -- since clients probably aren't expecting that to change on the fly
32 ALTER TABLE foo ADD COLUMN q3 bigint;
33
34 EXECUTE prepstmt;
35 EXECUTE prepstmt2(123);
36
37 -- but we're nice guys and will let you undo your mistake
38 ALTER TABLE foo DROP COLUMN q3;
39
40 EXECUTE prepstmt;
41 EXECUTE prepstmt2(123);
42
43 -- Try it with a view, which isn't directly used in the resulting plan
44 -- but should trigger invalidation anyway
45 CREATE TEMP VIEW voo AS SELECT * FROM foo;
46
47 PREPARE vprep AS SELECT * FROM voo;
48
49 EXECUTE vprep;
50
51 CREATE OR REPLACE TEMP VIEW voo AS SELECT q1, q2/2 AS q2 FROM foo;
52
53 EXECUTE vprep;
54
55 -- Check basic SPI plan invalidation
56
57 create function cache_test(int) returns int as $$
58 declare total int;
59 begin
60         create temp table t1(f1 int);
61         insert into t1 values($1);
62         insert into t1 values(11);
63         insert into t1 values(12);
64         insert into t1 values(13);
65         select sum(f1) into total from t1;
66         drop table t1;
67         return total;
68 end
69 $$ language plpgsql;
70
71 select cache_test(1);
72 select cache_test(2);
73 select cache_test(3);
74
75 -- Check invalidation of plpgsql "simple expression"
76
77 create temp view v1 as
78   select 2+2 as f1;
79
80 create function cache_test_2() returns int as $$
81 begin
82         return f1 from v1;
83 end$$ language plpgsql;
84
85 select cache_test_2();
86
87 create or replace temp view v1 as
88   select 2+2+4 as f1;
89 select cache_test_2();
90
91 create or replace temp view v1 as
92   select 2+2+4+(select max(unique1) from tenk1) as f1;
93 select cache_test_2();