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;
53 INSERT INTO temptest VALUES (1);
54 INSERT INTO temptest VALUES (2);
55 SELECT * FROM temptest;
63 SELECT * FROM temptest;
70 CREATE TEMP TABLE temptest(col) ON COMMIT DELETE ROWS AS SELECT 1;
71 SELECT * FROM temptest;
78 SELECT * FROM temptest;
84 -- Test ON COMMIT DROP
86 CREATE TEMP TABLE temptest(col int) ON COMMIT DROP;
87 INSERT INTO temptest VALUES (1);
88 INSERT INTO temptest VALUES (2);
89 SELECT * FROM temptest;
97 SELECT * FROM temptest;
98 ERROR: relation "temptest" does not exist
99 LINE 1: SELECT * FROM temptest;
102 CREATE TEMP TABLE temptest(col) ON COMMIT DROP AS SELECT 1;
103 SELECT * FROM temptest;
110 SELECT * FROM temptest;
111 ERROR: relation "temptest" does not exist
112 LINE 1: SELECT * FROM temptest;
114 -- ON COMMIT is only allowed for TEMP
115 CREATE TABLE temptest(col int) ON COMMIT DELETE ROWS;
116 ERROR: ON COMMIT can only be used on temporary tables
117 CREATE TABLE temptest(col) ON COMMIT DELETE ROWS AS SELECT 1;
118 ERROR: ON COMMIT can only be used on temporary tables
121 CREATE TEMP TABLE temptest1(col int PRIMARY KEY);
122 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "temptest1_pkey" for table "temptest1"
123 CREATE TEMP TABLE temptest2(col int REFERENCES temptest1)
124 ON COMMIT DELETE ROWS;
125 INSERT INTO temptest1 VALUES (1);
126 INSERT INTO temptest2 VALUES (1);
128 SELECT * FROM temptest1;
134 SELECT * FROM temptest2;
140 CREATE TEMP TABLE temptest3(col int PRIMARY KEY) ON COMMIT DELETE ROWS;
141 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "temptest3_pkey" for table "temptest3"
142 CREATE TEMP TABLE temptest4(col int REFERENCES temptest3);
144 ERROR: unsupported ON COMMIT and foreign key combination
145 DETAIL: Table "temptest4" references "temptest3", but they do not have the same ON COMMIT setting.
146 -- Test manipulation of temp schema's placement in search path
147 create table public.whereami (f1 text);
148 insert into public.whereami values ('public');
149 create temp table whereami (f1 text);
150 insert into whereami values ('temp');
151 create function public.whoami() returns text
152 as $$select 'public'::text$$ language sql;
153 create function pg_temp.whoami() returns text
154 as $$select 'temp'::text$$ language sql;
155 -- default should have pg_temp implicitly first, but only for tables
156 select * from whereami;
168 -- can list temp first explicitly, but it still doesn't affect functions
169 set search_path = pg_temp, public;
170 select * from whereami;
182 -- or put it last for security
183 set search_path = public, pg_temp;
184 select * from whereami;
196 -- you can invoke a temp function explicitly, though
197 select pg_temp.whoami();
203 drop table public.whereami;