3 -- Test temp relations and indexes
5 -- test temp table/index masking
6 CREATE TABLE temptest(col int);
7 CREATE INDEX i_temptest ON temptest(col);
8 CREATE TEMP TABLE temptest(tcol int);
9 CREATE INDEX i_temptest ON temptest(tcol);
10 SELECT * FROM temptest;
15 DROP INDEX i_temptest;
17 SELECT * FROM temptest;
22 DROP INDEX i_temptest;
24 -- test temp table selects
25 CREATE TABLE temptest(col int);
26 INSERT INTO temptest VALUES (1);
27 CREATE TEMP TABLE temptest(tcol float);
28 INSERT INTO temptest VALUES (2.1);
29 SELECT * FROM temptest;
36 SELECT * FROM temptest;
43 -- test temp table deletion
44 CREATE TEMP TABLE temptest(col int);
46 SELECT * FROM temptest;
47 ERROR: relation "temptest" does not exist
48 LINE 1: SELECT * FROM temptest;
50 -- Test ON COMMIT DELETE ROWS
51 CREATE TEMP TABLE temptest(col int) ON COMMIT DELETE ROWS;
52 -- while we're here, verify successful truncation of index with SQL function
53 CREATE INDEX ON temptest(bit_length(''));
55 INSERT INTO temptest VALUES (1);
56 INSERT INTO temptest VALUES (2);
57 SELECT * FROM temptest;
65 SELECT * FROM temptest;
72 CREATE TEMP TABLE temptest(col) ON COMMIT DELETE ROWS AS SELECT 1;
73 SELECT * FROM temptest;
80 SELECT * FROM temptest;
86 -- Test ON COMMIT DROP
88 CREATE TEMP TABLE temptest(col int) ON COMMIT DROP;
89 INSERT INTO temptest VALUES (1);
90 INSERT INTO temptest VALUES (2);
91 SELECT * FROM temptest;
99 SELECT * FROM temptest;
100 ERROR: relation "temptest" does not exist
101 LINE 1: SELECT * FROM temptest;
104 CREATE TEMP TABLE temptest(col) ON COMMIT DROP AS SELECT 1;
105 SELECT * FROM temptest;
112 SELECT * FROM temptest;
113 ERROR: relation "temptest" does not exist
114 LINE 1: SELECT * FROM temptest;
116 -- Test it with a CHECK condition that produces a toasted pg_constraint entry
121 CREATE TEMP TABLE temptest (col text CHECK (col < %L)) ON COMMIT DROP
123 (SELECT string_agg(g.i::text || ':' || random()::text, '|')
124 FROM generate_series(1, 100) g(i)));
126 SELECT * FROM temptest;
132 SELECT * FROM temptest;
133 ERROR: relation "temptest" does not exist
134 LINE 1: SELECT * FROM temptest;
136 -- ON COMMIT is only allowed for TEMP
137 CREATE TABLE temptest(col int) ON COMMIT DELETE ROWS;
138 ERROR: ON COMMIT can only be used on temporary tables
139 CREATE TABLE temptest(col) ON COMMIT DELETE ROWS AS SELECT 1;
140 ERROR: ON COMMIT can only be used on temporary tables
143 CREATE TEMP TABLE temptest1(col int PRIMARY KEY);
144 CREATE TEMP TABLE temptest2(col int REFERENCES temptest1)
145 ON COMMIT DELETE ROWS;
146 INSERT INTO temptest1 VALUES (1);
147 INSERT INTO temptest2 VALUES (1);
149 SELECT * FROM temptest1;
155 SELECT * FROM temptest2;
161 CREATE TEMP TABLE temptest3(col int PRIMARY KEY) ON COMMIT DELETE ROWS;
162 CREATE TEMP TABLE temptest4(col int REFERENCES temptest3);
164 ERROR: unsupported ON COMMIT and foreign key combination
165 DETAIL: Table "temptest4" references "temptest3", but they do not have the same ON COMMIT setting.
166 -- Test manipulation of temp schema's placement in search path
167 create table public.whereami (f1 text);
168 insert into public.whereami values ('public');
169 create temp table whereami (f1 text);
170 insert into whereami values ('temp');
171 create function public.whoami() returns text
172 as $$select 'public'::text$$ language sql;
173 create function pg_temp.whoami() returns text
174 as $$select 'temp'::text$$ language sql;
175 -- default should have pg_temp implicitly first, but only for tables
176 select * from whereami;
188 -- can list temp first explicitly, but it still doesn't affect functions
189 set search_path = pg_temp, public;
190 select * from whereami;
202 -- or put it last for security
203 set search_path = public, pg_temp;
204 select * from whereami;
216 -- you can invoke a temp function explicitly, though
217 select pg_temp.whoami();
223 drop table public.whereami;
224 -- types in temp schema
225 set search_path = pg_temp, public;
226 create domain pg_temp.nonempty as text check (value <> '');
227 -- function-syntax invocation of types matches rules for functions
229 ERROR: function nonempty(unknown) does not exist
230 LINE 1: select nonempty('');
232 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
233 select pg_temp.nonempty('');
234 ERROR: value for domain nonempty violates check constraint "nonempty_check"
235 -- other syntax matches rules for tables
237 ERROR: value for domain nonempty violates check constraint "nonempty_check"
239 -- For partitioned temp tables, ON COMMIT actions ignore storage-less
240 -- partitioned tables.
242 create temp table temp_parted_oncommit (a int)
243 partition by list (a) on commit delete rows;
244 create temp table temp_parted_oncommit_1
245 partition of temp_parted_oncommit
246 for values in (1) on commit delete rows;
247 insert into temp_parted_oncommit values (1);
249 -- partitions are emptied by the previous commit
250 select * from temp_parted_oncommit;
255 drop table temp_parted_oncommit;
256 -- Check dependencies between ON COMMIT actions with a partitioned
257 -- table and its partitions. Using ON COMMIT DROP on a parent removes
260 create temp table temp_parted_oncommit_test (a int)
261 partition by list (a) on commit drop;
262 create temp table temp_parted_oncommit_test1
263 partition of temp_parted_oncommit_test
264 for values in (1) on commit delete rows;
265 create temp table temp_parted_oncommit_test2
266 partition of temp_parted_oncommit_test
267 for values in (2) on commit drop;
268 insert into temp_parted_oncommit_test values (1), (2);
270 -- no relations remain in this case.
271 select relname from pg_class where relname ~ '^temp_parted_oncommit_test';
276 -- Using ON COMMIT DELETE on a partitioned table does not remove
277 -- all rows if partitions preserve their data.
279 create temp table temp_parted_oncommit_test (a int)
280 partition by list (a) on commit delete rows;
281 create temp table temp_parted_oncommit_test1
282 partition of temp_parted_oncommit_test
283 for values in (1) on commit preserve rows;
284 create temp table temp_parted_oncommit_test2
285 partition of temp_parted_oncommit_test
286 for values in (2) on commit drop;
287 insert into temp_parted_oncommit_test values (1), (2);
289 -- Data from the remaining partition is still here as its rows are
291 select * from temp_parted_oncommit_test;
297 -- two relations remain in this case.
298 select relname from pg_class where relname ~ '^temp_parted_oncommit_test'
301 ----------------------------
302 temp_parted_oncommit_test
303 temp_parted_oncommit_test1
306 drop table temp_parted_oncommit_test;
307 -- Check dependencies between ON COMMIT actions with inheritance trees.
308 -- Using ON COMMIT DROP on a parent removes the whole set.
310 create temp table temp_inh_oncommit_test (a int) on commit drop;
311 create temp table temp_inh_oncommit_test1 ()
312 inherits(temp_inh_oncommit_test) on commit delete rows;
313 insert into temp_inh_oncommit_test1 values (1);
315 -- no relations remain in this case
316 select relname from pg_class where relname ~ '^temp_inh_oncommit_test';
321 -- Data on the parent is removed, and the child goes away.
323 create temp table temp_inh_oncommit_test (a int) on commit delete rows;
324 create temp table temp_inh_oncommit_test1 ()
325 inherits(temp_inh_oncommit_test) on commit drop;
326 insert into temp_inh_oncommit_test1 values (1);
327 insert into temp_inh_oncommit_test values (1);
329 select * from temp_inh_oncommit_test;
334 -- one relation remains
335 select relname from pg_class where relname ~ '^temp_inh_oncommit_test';
337 ------------------------
338 temp_inh_oncommit_test
341 drop table temp_inh_oncommit_test;
342 -- Tests with two-phase commit
343 -- Transactions creating objects in a temporary namespace cannot be used
344 -- with two-phase commit.
345 -- These cases generate errors about temporary namespace.
348 create function pg_temp.twophase_func() returns void as
349 $$ select '2pc_func'::text $$ language sql;
350 prepare transaction 'twophase_func';
351 ERROR: cannot PREPARE a transaction that has operated on temporary objects
353 create function pg_temp.twophase_func() returns void as
354 $$ select '2pc_func'::text $$ language sql;
356 drop function pg_temp.twophase_func();
357 prepare transaction 'twophase_func';
358 ERROR: cannot PREPARE a transaction that has operated on temporary objects
361 create operator pg_temp.@@ (leftarg = int4, rightarg = int4, procedure = int4mi);
362 prepare transaction 'twophase_operator';
363 ERROR: cannot PREPARE a transaction that has operated on temporary objects
364 -- These generate errors about temporary tables.
366 create type pg_temp.twophase_type as (a int);
367 prepare transaction 'twophase_type';
368 ERROR: cannot PREPARE a transaction that has operated on temporary objects
370 create view pg_temp.twophase_view as select 1;
371 prepare transaction 'twophase_view';
372 ERROR: cannot PREPARE a transaction that has operated on temporary objects
374 create sequence pg_temp.twophase_seq;
375 prepare transaction 'twophase_sequence';
376 ERROR: cannot PREPARE a transaction that has operated on temporary objects
377 -- Temporary tables cannot be used with two-phase commit.
378 create temp table twophase_tab (a int);
380 select a from twophase_tab;
385 prepare transaction 'twophase_tab';
386 ERROR: cannot PREPARE a transaction that has operated on temporary objects
388 insert into twophase_tab values (1);
389 prepare transaction 'twophase_tab';
390 ERROR: cannot PREPARE a transaction that has operated on temporary objects
392 lock twophase_tab in access exclusive mode;
393 prepare transaction 'twophase_tab';
394 ERROR: cannot PREPARE a transaction that has operated on temporary objects
396 drop table twophase_tab;
397 prepare transaction 'twophase_tab';
398 ERROR: cannot PREPARE a transaction that has operated on temporary objects
399 -- Corner case: current_schema may create a temporary schema if namespace
400 -- creation is pending, so check after that. First reset the connection
401 -- to remove the temporary namespace.
403 SET search_path TO 'pg_temp';
405 SELECT current_schema() ~ 'pg_temp' AS is_temp_schema;
411 PREPARE TRANSACTION 'twophase_search';
412 ERROR: cannot PREPARE a transaction that has operated on temporary objects