2 -- PREPARED TRANSACTIONS (two-phase commit)
4 -- We can't readily test persistence of prepared xacts within the
5 -- regression script framework, unfortunately. Note that a crash
6 -- isn't really needed ... stopping and starting the postmaster would
7 -- be enough, but we can't even do that here.
8 -- create a simple table that we'll use in the tests
9 CREATE TABLE pxtest1 (foobar VARCHAR(10));
10 INSERT INTO pxtest1 VALUES ('aaa');
11 -- Test PREPARE TRANSACTION
13 UPDATE pxtest1 SET foobar = 'bbb' WHERE foobar = 'aaa';
14 SELECT * FROM pxtest1;
20 PREPARE TRANSACTION 'foo1';
21 SELECT * FROM pxtest1;
27 -- Test pg_prepared_xacts system view
28 SELECT gid FROM pg_prepared_xacts;
34 -- Test ROLLBACK PREPARED
35 ROLLBACK PREPARED 'foo1';
36 SELECT * FROM pxtest1;
42 SELECT gid FROM pg_prepared_xacts;
47 -- Test COMMIT PREPARED
49 INSERT INTO pxtest1 VALUES ('ddd');
50 SELECT * FROM pxtest1;
57 PREPARE TRANSACTION 'foo2';
58 SELECT * FROM pxtest1;
64 COMMIT PREPARED 'foo2';
65 SELECT * FROM pxtest1;
72 -- Test duplicate gids
74 UPDATE pxtest1 SET foobar = 'eee' WHERE foobar = 'ddd';
75 SELECT * FROM pxtest1;
82 PREPARE TRANSACTION 'foo3';
83 SELECT gid FROM pg_prepared_xacts;
90 INSERT INTO pxtest1 VALUES ('fff');
91 SELECT * FROM pxtest1;
99 -- This should fail, because the gid foo3 is already in use
100 PREPARE TRANSACTION 'foo3';
101 ERROR: transaction identifier "foo3" is already in use
102 SELECT * FROM pxtest1;
109 ROLLBACK PREPARED 'foo3';
110 SELECT * FROM pxtest1;
119 -- Test subtransactions
121 CREATE TABLE pxtest2 (a int);
122 INSERT INTO pxtest2 VALUES (1);
124 INSERT INTO pxtest2 VALUES (2);
127 INSERT INTO pxtest2 VALUES (3);
128 PREPARE TRANSACTION 'regress-one';
129 CREATE TABLE pxtest3(fff int);
130 -- Test shared invalidation
133 CREATE TABLE pxtest4 (a int);
134 INSERT INTO pxtest4 VALUES (1);
135 INSERT INTO pxtest4 VALUES (2);
136 DECLARE foo CURSOR FOR SELECT * FROM pxtest4;
137 -- Fetch 1 tuple, keeping the cursor open
144 PREPARE TRANSACTION 'regress-two';
147 ERROR: cursor "foo" does not exist
148 -- Table doesn't exist, the creation hasn't been committed yet
149 SELECT * FROM pxtest2;
150 ERROR: relation "pxtest2" does not exist
151 LINE 1: SELECT * FROM pxtest2;
153 -- There should be two prepared transactions
154 SELECT gid FROM pg_prepared_xacts;
161 -- pxtest3 should be locked because of the pending DROP
162 set statement_timeout to 2000;
163 SELECT * FROM pxtest3;
164 ERROR: canceling statement due to statement timeout
165 reset statement_timeout;
166 -- Disconnect, we will continue testing in a different backend
168 -- There should still be two prepared transactions
169 SELECT gid FROM pg_prepared_xacts;
176 -- pxtest3 should still be locked because of the pending DROP
177 set statement_timeout to 2000;
178 SELECT * FROM pxtest3;
179 ERROR: canceling statement due to statement timeout
180 reset statement_timeout;
181 -- Commit table creation
182 COMMIT PREPARED 'regress-one';
184 Table "public.pxtest2"
185 Column | Type | Modifiers
186 --------+---------+-----------
189 SELECT * FROM pxtest2;
196 -- There should be one prepared transaction
197 SELECT gid FROM pg_prepared_xacts;
204 COMMIT PREPARED 'regress-two';
205 SELECT * FROM pxtest3;
206 ERROR: relation "pxtest3" does not exist
207 LINE 1: SELECT * FROM pxtest3;
209 -- There should be no prepared transactions
210 SELECT gid FROM pg_prepared_xacts;
217 DROP TABLE pxtest3; -- will still be there if prepared xacts are disabled
218 ERROR: table "pxtest3" does not exist