File: 01_oncommitdelete.out

package info (click to toggle)
pgtt 4.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 536 kB
  • sloc: ansic: 1,547; sql: 569; makefile: 27; sh: 1
file content (121 lines) | stat: -rw-r--r-- 3,509 bytes parent folder | download | duplicates (2)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
----
-- Regression test to Global Temporary Table implementation
--
-- Test for GTT with ON COMMIT DELETE ROWS clause.
--
-- Test drop of GTT when in use throwing error and effective
-- when done in a separate session.
--
----
-- Create a GTT like table to test ON COMMIT DELETE ROWS
CREATE GLOBAL TEMPORARY TABLE t_glob_temptable1 (id integer, lbl text) ON COMMIT DELETE ROWS;
WARNING:  GLOBAL is deprecated in temporary table creation
LINE 1: CREATE GLOBAL TEMPORARY TABLE t_glob_temptable1 (id integer,...
               ^
-- 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 | f         | id integer, lbl text
(1 row)

-- A "template" unlogged table should exists
SELECT n.nspname, c.relname FROM pg_class c JOIN pg_namespace n ON (c.relnamespace=n.oid) WHERE relname = 't_glob_temptable1';
   nspname   |      relname      
-------------+-------------------
 pgtt_schema | t_glob_temptable1
(1 row)

BEGIN;
-- With the first insert some value in the temporary table
INSERT INTO t_glob_temptable1 VALUES (1, 'One');
-- Look if we have two tables now
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)

-- Second insert, the temporary table exists
INSERT INTO t_glob_temptable1 VALUES (2, 'Two');
-- 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 | lbl 
----+-----
(0 rows)

SET pgtt.enabled TO on;
-- Look at content of the Global Temporary Table
SELECT * FROM t_glob_temptable1;
 id | lbl 
----+-----
  1 | One
  2 | Two
(2 rows)

COMMIT;
-- No row must perstist after the commit
SELECT * FROM t_glob_temptable1;
 id | lbl 
----+-----
(0 rows)

-- With holdabe cursor the rows must remain
BEGIN;
INSERT INTO t_glob_temptable1 VALUES (1, 'One');
INSERT INTO t_glob_temptable1 VALUES (2, 'Two');
DECLARE curs1 CURSOR WITH HOLD FOR SELECT * FROM t_glob_temptable1;
FETCH curs1;
 id | lbl 
----+-----
  1 | One
(1 row)

COMMIT;
-- No row must perstist after the commit for the cursor
FETCH curs1;
 id | lbl 
----+-----
  2 | Two
(1 row)

-- But not for a new select
SELECT * FROM t_glob_temptable1;
 id | lbl 
----+-----
(0 rows)

CLOSE curs1;
-- Drop the global temporary table: fail because it is in use
DROP TABLE t_glob_temptable1;
ERROR:  can not drop a GTT that is in use.
-- Reconnect and drop it
\c - -
SHOW search_path;
         search_path          
------------------------------
 "$user", public, pgtt_schema
(1 row)

DROP TABLE t_glob_temptable1;
VACUUM pg_class;
SELECT pg_sleep(1);
 pg_sleep 
----------
 
(1 row)

-- Look at Global Temporary Table definition
SELECT nspname, relname, preserved, code FROM pgtt_schema.pg_global_temp_tables; -- should be empty
 nspname | relname | preserved | code 
---------+---------+-----------+------
(0 rows)

-- The "template" unlogged table should not exists anymore
SELECT n.nspname, c.relname FROM pg_class c JOIN pg_namespace n ON (c.relnamespace=n.oid) WHERE relname = 't_glob_temptable1';
 nspname | relname 
---------+---------
(0 rows)