|
| 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 |
0 commit comments