1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
----
-- Regression test to Global Temporary Table implementation
--
-- Test for GTT with TABLE ... AS clause.
--
----
-- Create a GTT like table to test ON COMMIT PRESERVE ROWS
CREATE GLOBAL TEMPORARY TABLE t_glob_temptable1 AS SELECT * FROM source WITH DATA;
WARNING: GLOBAL is deprecated in temporary table creation
LINE 1: CREATE GLOBAL TEMPORARY TABLE t_glob_temptable1 AS SELECT * ...
^
-- Look at Global Temporary Table definition
SELECT nspname, relname, preserved, code FROM pgtt_schema.pg_global_temp_tables;
nspname | relname | preserved | code
-------------+-------------------+-----------+---------------------------
pgtt_schema | t_glob_temptable1 | t | AS SELECT * FROM source
(1 row)
-- A "template" unlogged table should exists as well as
-- the temporary table as we have used WITH DATA
SELECT regexp_replace(n.nspname, '\d+', 'x', 'g'), c.relname FROM pg_class c JOIN pg_namespace n ON (c.relnamespace=n.oid) WHERE relname = 't_glob_temptable1';
regexp_replace | relname
----------------+-------------------
pgtt_schema | t_glob_temptable1
pg_temp_x | t_glob_temptable1
(2 rows)
-- Look at content of the template for Global Temporary Table, must be empty
SET pgtt.enabled TO off;
SELECT * FROM pgtt_schema.t_glob_temptable1;
id | c2 | lbl
----+-------+-----
1 | one | -
2 | two | -
3 | three | -
(3 rows)
SET pgtt.enabled TO on;
-- Look at the temporary table itself, it must have the rows
SELECT * FROM t_glob_temptable1 ORDER BY id;
id | c2 | lbl
----+-------+-----
1 | one | -
2 | two | -
3 | three | -
(3 rows)
BEGIN;
-- With a new row
INSERT INTO t_glob_temptable1 VALUES (4, 'four');
-- Look at content of the template for Global Temporary Table, must be empty
SET pgtt.enabled TO off;
SELECT * FROM pgtt_schema.t_glob_temptable1;
id | c2 | lbl
----+-------+-----
1 | one | -
2 | two | -
3 | three | -
(3 rows)
SET pgtt.enabled TO on;
-- Look at content of the Global Temporary Table
SELECT * FROM t_glob_temptable1 ORDER BY id;
id | c2 | lbl
----+-------+-----
1 | one | -
2 | two | -
3 | three | -
4 | four |
(4 rows)
COMMIT;
SELECT * FROM t_glob_temptable1 ORDER BY id;
id | c2 | lbl
----+-------+-----
1 | one | -
2 | two | -
3 | three | -
4 | four |
(4 rows)
-- Reconnect and drop it
\c - -
-- Cleanup
DROP TABLE t_glob_temptable1;
|