OSDN Git Service

Fix plancache so that any required replanning is done with the same
[pg-rex/syncrep.git] / src / test / regress / sql / plancache.sql
1 --
2 -- Tests to exercise the plan caching/invalidation mechanism
3 --
4
5 CREATE TEMP TABLE pcachetest AS SELECT * FROM int8_tbl;
6
7 -- create and use a cached plan
8 PREPARE prepstmt AS SELECT * FROM pcachetest;
9
10 EXECUTE prepstmt;
11
12 -- and one with parameters
13 PREPARE prepstmt2(bigint) AS SELECT * FROM pcachetest WHERE q1 = $1;
14
15 EXECUTE prepstmt2(123);
16
17 -- invalidate the plans and see what happens
18 DROP TABLE pcachetest;
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 pcachetest 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 pcachetest 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 pcachetest 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 pcacheview AS
46   SELECT * FROM pcachetest;
47
48 PREPARE vprep AS SELECT * FROM pcacheview;
49
50 EXECUTE vprep;
51
52 CREATE OR REPLACE TEMP VIEW pcacheview AS
53   SELECT q1, q2/2 AS q2 FROM pcachetest;
54
55 EXECUTE vprep;
56
57 -- Check basic SPI plan invalidation
58
59 create function cache_test(int) returns int as $$
60 declare total int;
61 begin
62         create temp table t1(f1 int);
63         insert into t1 values($1);
64         insert into t1 values(11);
65         insert into t1 values(12);
66         insert into t1 values(13);
67         select sum(f1) into total from t1;
68         drop table t1;
69         return total;
70 end
71 $$ language plpgsql;
72
73 select cache_test(1);
74 select cache_test(2);
75 select cache_test(3);
76
77 -- Check invalidation of plpgsql "simple expression"
78
79 create temp view v1 as
80   select 2+2 as f1;
81
82 create function cache_test_2() returns int as $$
83 begin
84         return f1 from v1;
85 end$$ language plpgsql;
86
87 select cache_test_2();
88
89 create or replace temp view v1 as
90   select 2+2+4 as f1;
91 select cache_test_2();
92
93 create or replace temp view v1 as
94   select 2+2+4+(select max(unique1) from tenk1) as f1;
95 select cache_test_2();
96
97 --- Check that change of search_path is ignored by replans
98
99 create schema s1
100   create table abc (f1 int);
101
102 create schema s2
103   create table abc (f1 int);
104
105 insert into s1.abc values(123);
106 insert into s2.abc values(456);
107
108 set search_path = s1;
109
110 prepare p1 as select f1 from abc;
111
112 execute p1;
113
114 set search_path = s2;
115
116 select f1 from abc;
117
118 execute p1;
119
120 alter table s1.abc add column f2 float8;   -- force replan
121
122 execute p1;
123
124 drop schema s1 cascade;
125 drop schema s2 cascade;