Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 77d411d

Browse files
committed
[test] Test for global temp tables
1 parent 490cb5d commit 77d411d

File tree

2 files changed

+348
-0
lines changed

2 files changed

+348
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
--
2+
-- GLOBAL TEMP
3+
-- Test global temp relations
4+
--
5+
-- Test ON COMMIT DELETE ROWS
6+
CREATE GLOBAL TEMP TABLE temptest(col int) ON COMMIT DELETE ROWS;
7+
BEGIN;
8+
INSERT INTO temptest VALUES (1);
9+
INSERT INTO temptest VALUES (2);
10+
SELECT * FROM temptest;
11+
col
12+
-----
13+
1
14+
2
15+
(2 rows)
16+
17+
COMMIT;
18+
SELECT * FROM temptest;
19+
col
20+
-----
21+
(0 rows)
22+
23+
DROP TABLE temptest;
24+
BEGIN;
25+
CREATE GLOBAL TEMP TABLE temptest(col) ON COMMIT DELETE ROWS AS SELECT 1;
26+
SELECT * FROM temptest;
27+
col
28+
-----
29+
1
30+
(1 row)
31+
32+
COMMIT;
33+
SELECT * FROM temptest;
34+
col
35+
-----
36+
(0 rows)
37+
38+
DROP TABLE temptest;
39+
-- Test foreign keys
40+
BEGIN;
41+
CREATE GLOBAL TEMP TABLE temptest1(col int PRIMARY KEY);
42+
CREATE GLOBAL TEMP TABLE temptest2(col int REFERENCES temptest1)
43+
ON COMMIT DELETE ROWS;
44+
INSERT INTO temptest1 VALUES (1);
45+
INSERT INTO temptest2 VALUES (1);
46+
COMMIT;
47+
SELECT * FROM temptest1;
48+
col
49+
-----
50+
1
51+
(1 row)
52+
53+
SELECT * FROM temptest2;
54+
col
55+
-----
56+
(0 rows)
57+
58+
BEGIN;
59+
CREATE GLOBAL TEMP TABLE temptest3(col int PRIMARY KEY) ON COMMIT DELETE ROWS;
60+
CREATE GLOBAL TEMP TABLE temptest4(col int REFERENCES temptest3);
61+
COMMIT;
62+
ERROR: unsupported ON COMMIT and foreign key combination
63+
DETAIL: Table "temptest4" references "temptest3", but they do not have the same ON COMMIT setting.
64+
-- For partitioned temp tables, ON COMMIT actions ignore storage-less
65+
-- partitioned tables.
66+
begin;
67+
CREATE GLOBAL TEMP TABLE temp_parted_oncommit (a int)
68+
partition by list (a) on commit delete rows;
69+
CREATE GLOBAL TEMP TABLE temp_parted_oncommit_1
70+
partition of temp_parted_oncommit
71+
for values in (1) on commit delete rows;
72+
insert into temp_parted_oncommit values (1);
73+
commit;
74+
-- partitions are emptied by the previous commit
75+
select * from temp_parted_oncommit;
76+
a
77+
---
78+
(0 rows)
79+
80+
drop table temp_parted_oncommit;
81+
-- Using ON COMMIT DELETE on a partitioned table does not remove
82+
-- all rows if partitions preserve their data.
83+
begin;
84+
CREATE GLOBAL TEMP TABLE temp_parted_oncommit_test (a int)
85+
partition by list (a) on commit delete rows;
86+
CREATE GLOBAL TEMP TABLE temp_parted_oncommit_test1
87+
partition of temp_parted_oncommit_test
88+
for values in (1) on commit preserve rows;
89+
insert into temp_parted_oncommit_test values (1);
90+
commit;
91+
-- Data from the remaining partition is still here as its rows are
92+
-- preserved.
93+
select * from temp_parted_oncommit_test;
94+
a
95+
---
96+
1
97+
(1 row)
98+
99+
-- two relations remain in this case.
100+
select relname from pg_class where relname like 'temp_parted_oncommit_test%';
101+
relname
102+
----------------------------
103+
temp_parted_oncommit_test
104+
temp_parted_oncommit_test1
105+
(2 rows)
106+
107+
drop table temp_parted_oncommit_test;
108+
-- Check dependencies between ON COMMIT actions with inheritance trees.
109+
-- Data on the parent is removed, and the child goes away.
110+
begin;
111+
CREATE GLOBAL TEMP TABLE temp_inh_oncommit_test (a int) on commit delete rows;
112+
CREATE GLOBAL TEMP TABLE temp_inh_oncommit_test1 ()
113+
inherits(temp_inh_oncommit_test) on commit preserve rows;
114+
insert into temp_inh_oncommit_test1 values (1);
115+
insert into temp_inh_oncommit_test values (1);
116+
commit;
117+
select * from temp_inh_oncommit_test;
118+
a
119+
---
120+
1
121+
(1 row)
122+
123+
-- two relations remain
124+
select relname from pg_class where relname like 'temp_inh_oncommit_test%';
125+
relname
126+
-------------------------
127+
temp_inh_oncommit_test
128+
temp_inh_oncommit_test1
129+
(2 rows)
130+
131+
drop table temp_inh_oncommit_test;
132+
-- Tests with two-phase commit
133+
-- Transactions creating objects in a temporary namespace cannot be used
134+
-- with two-phase commit.
135+
-- These cases generate errors about temporary namespace.
136+
-- Function creation
137+
begin;
138+
create function pg_temp.twophase_func() returns void as
139+
$$ select '2pc_func'::text $$ language sql;
140+
prepare transaction 'twophase_func';
141+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
142+
-- Function drop
143+
create function pg_temp.twophase_func() returns void as
144+
$$ select '2pc_func'::text $$ language sql;
145+
begin;
146+
drop function pg_temp.twophase_func();
147+
prepare transaction 'twophase_func';
148+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
149+
-- Operator creation
150+
begin;
151+
create operator pg_temp.@@ (leftarg = int4, rightarg = int4, procedure = int4mi);
152+
prepare transaction 'twophase_operator';
153+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
154+
-- These generate errors about temporary tables.
155+
begin;
156+
create type pg_temp.twophase_type as (a int);
157+
prepare transaction 'twophase_type';
158+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
159+
begin;
160+
create view pg_temp.twophase_view as select 1;
161+
prepare transaction 'twophase_view';
162+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
163+
begin;
164+
create sequence pg_temp.twophase_seq;
165+
prepare transaction 'twophase_sequence';
166+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
167+
-- Temporary tables cannot be used with two-phase commit.
168+
CREATE GLOBAL TEMP TABLE twophase_tab (a int);
169+
begin;
170+
select a from twophase_tab;
171+
a
172+
---
173+
(0 rows)
174+
175+
prepare transaction 'twophase_tab';
176+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
177+
begin;
178+
insert into twophase_tab values (1);
179+
prepare transaction 'twophase_tab';
180+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
181+
begin;
182+
lock twophase_tab in access exclusive mode;
183+
prepare transaction 'twophase_tab';
184+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
185+
begin;
186+
drop table twophase_tab;
187+
prepare transaction 'twophase_tab';
188+
ERROR: cannot PREPARE a transaction that has operated on temporary objects
189+
-- Corner case: current_schema may create a temporary schema if namespace
190+
-- creation is pending, so check after that. First reset the connection
191+
-- to remove the temporary namespace.
192+
\c -
193+
SET search_path TO 'pg_temp';
194+
BEGIN;
195+
SELECT current_schema() ~ 'pg_temp' AS is_temp_schema;
196+
is_temp_schema
197+
----------------
198+
t
199+
(1 row)
200+
201+
PREPARE TRANSACTION 'twophase_search';
202+
ERROR: cannot PREPARE a transaction that has operated on temporary objects

src/test/regress/sql/global_temp.sql

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
--
2+
-- GLOBAL TEMP
3+
-- Test global temp relations
4+
--
5+
6+
-- Test ON COMMIT DELETE ROWS
7+
8+
CREATE GLOBAL TEMP TABLE temptest(col int) ON COMMIT DELETE ROWS;
9+
10+
BEGIN;
11+
INSERT INTO temptest VALUES (1);
12+
INSERT INTO temptest VALUES (2);
13+
14+
SELECT * FROM temptest;
15+
COMMIT;
16+
17+
SELECT * FROM temptest;
18+
19+
DROP TABLE temptest;
20+
21+
BEGIN;
22+
CREATE GLOBAL TEMP TABLE temptest(col) ON COMMIT DELETE ROWS AS SELECT 1;
23+
24+
SELECT * FROM temptest;
25+
COMMIT;
26+
27+
SELECT * FROM temptest;
28+
29+
DROP TABLE temptest;
30+
31+
-- Test foreign keys
32+
BEGIN;
33+
CREATE GLOBAL TEMP TABLE temptest1(col int PRIMARY KEY);
34+
CREATE GLOBAL TEMP TABLE temptest2(col int REFERENCES temptest1)
35+
ON COMMIT DELETE ROWS;
36+
INSERT INTO temptest1 VALUES (1);
37+
INSERT INTO temptest2 VALUES (1);
38+
COMMIT;
39+
SELECT * FROM temptest1;
40+
SELECT * FROM temptest2;
41+
42+
BEGIN;
43+
CREATE GLOBAL TEMP TABLE temptest3(col int PRIMARY KEY) ON COMMIT DELETE ROWS;
44+
CREATE GLOBAL TEMP TABLE temptest4(col int REFERENCES temptest3);
45+
COMMIT;
46+
47+
-- For partitioned temp tables, ON COMMIT actions ignore storage-less
48+
-- partitioned tables.
49+
begin;
50+
CREATE GLOBAL TEMP TABLE temp_parted_oncommit (a int)
51+
partition by list (a) on commit delete rows;
52+
CREATE GLOBAL TEMP TABLE temp_parted_oncommit_1
53+
partition of temp_parted_oncommit
54+
for values in (1) on commit delete rows;
55+
insert into temp_parted_oncommit values (1);
56+
commit;
57+
-- partitions are emptied by the previous commit
58+
select * from temp_parted_oncommit;
59+
drop table temp_parted_oncommit;
60+
61+
-- Using ON COMMIT DELETE on a partitioned table does not remove
62+
-- all rows if partitions preserve their data.
63+
begin;
64+
CREATE GLOBAL TEMP TABLE temp_parted_oncommit_test (a int)
65+
partition by list (a) on commit delete rows;
66+
CREATE GLOBAL TEMP TABLE temp_parted_oncommit_test1
67+
partition of temp_parted_oncommit_test
68+
for values in (1) on commit preserve rows;
69+
insert into temp_parted_oncommit_test values (1);
70+
commit;
71+
-- Data from the remaining partition is still here as its rows are
72+
-- preserved.
73+
select * from temp_parted_oncommit_test;
74+
-- two relations remain in this case.
75+
select relname from pg_class where relname like 'temp_parted_oncommit_test%';
76+
drop table temp_parted_oncommit_test;
77+
78+
-- Check dependencies between ON COMMIT actions with inheritance trees.
79+
-- Data on the parent is removed, and the child goes away.
80+
begin;
81+
CREATE GLOBAL TEMP TABLE temp_inh_oncommit_test (a int) on commit delete rows;
82+
CREATE GLOBAL TEMP TABLE temp_inh_oncommit_test1 ()
83+
inherits(temp_inh_oncommit_test) on commit preserve rows;
84+
insert into temp_inh_oncommit_test1 values (1);
85+
insert into temp_inh_oncommit_test values (1);
86+
commit;
87+
select * from temp_inh_oncommit_test;
88+
-- two relations remain
89+
select relname from pg_class where relname like 'temp_inh_oncommit_test%';
90+
drop table temp_inh_oncommit_test;
91+
92+
-- Tests with two-phase commit
93+
-- Transactions creating objects in a temporary namespace cannot be used
94+
-- with two-phase commit.
95+
96+
-- These cases generate errors about temporary namespace.
97+
-- Function creation
98+
begin;
99+
create function pg_temp.twophase_func() returns void as
100+
$$ select '2pc_func'::text $$ language sql;
101+
prepare transaction 'twophase_func';
102+
-- Function drop
103+
create function pg_temp.twophase_func() returns void as
104+
$$ select '2pc_func'::text $$ language sql;
105+
begin;
106+
drop function pg_temp.twophase_func();
107+
prepare transaction 'twophase_func';
108+
-- Operator creation
109+
begin;
110+
create operator pg_temp.@@ (leftarg = int4, rightarg = int4, procedure = int4mi);
111+
prepare transaction 'twophase_operator';
112+
113+
-- These generate errors about temporary tables.
114+
begin;
115+
create type pg_temp.twophase_type as (a int);
116+
prepare transaction 'twophase_type';
117+
begin;
118+
create view pg_temp.twophase_view as select 1;
119+
prepare transaction 'twophase_view';
120+
begin;
121+
create sequence pg_temp.twophase_seq;
122+
prepare transaction 'twophase_sequence';
123+
124+
-- Temporary tables cannot be used with two-phase commit.
125+
CREATE GLOBAL TEMP TABLE twophase_tab (a int);
126+
begin;
127+
select a from twophase_tab;
128+
prepare transaction 'twophase_tab';
129+
begin;
130+
insert into twophase_tab values (1);
131+
prepare transaction 'twophase_tab';
132+
begin;
133+
lock twophase_tab in access exclusive mode;
134+
prepare transaction 'twophase_tab';
135+
begin;
136+
drop table twophase_tab;
137+
prepare transaction 'twophase_tab';
138+
139+
-- Corner case: current_schema may create a temporary schema if namespace
140+
-- creation is pending, so check after that. First reset the connection
141+
-- to remove the temporary namespace.
142+
\c -
143+
SET search_path TO 'pg_temp';
144+
BEGIN;
145+
SELECT current_schema() ~ 'pg_temp' AS is_temp_schema;
146+
PREPARE TRANSACTION 'twophase_search';

0 commit comments

Comments
 (0)