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 ERROR: prepared transactions are disabled
22 HINT: Set max_prepared_transactions to a nonzero value.
23 SELECT * FROM pxtest1;
29 -- Test pg_prepared_xacts system view
30 SELECT gid FROM pg_prepared_xacts;
35 -- Test ROLLBACK PREPARED
36 ROLLBACK PREPARED 'foo1';
37 ERROR: prepared transaction with identifier "foo1" does not exist
38 SELECT * FROM pxtest1;
44 SELECT gid FROM pg_prepared_xacts;
49 -- Test COMMIT PREPARED
51 INSERT INTO pxtest1 VALUES ('ddd');
52 SELECT * FROM pxtest1;
59 PREPARE TRANSACTION 'foo2';
60 ERROR: prepared transactions are disabled
61 HINT: Set max_prepared_transactions to a nonzero value.
62 SELECT * FROM pxtest1;
68 COMMIT PREPARED 'foo2';
69 ERROR: prepared transaction with identifier "foo2" does not exist
70 SELECT * FROM pxtest1;
76 -- Test duplicate gids
78 UPDATE pxtest1 SET foobar = 'eee' WHERE foobar = 'ddd';
79 SELECT * FROM pxtest1;
85 PREPARE TRANSACTION 'foo3';
86 ERROR: prepared transactions are disabled
87 HINT: Set max_prepared_transactions to a nonzero value.
88 SELECT gid FROM pg_prepared_xacts;
94 INSERT INTO pxtest1 VALUES ('fff');
95 SELECT * FROM pxtest1;
102 -- This should fail, because the gid foo3 is already in use
103 PREPARE TRANSACTION 'foo3';
104 ERROR: prepared transactions are disabled
105 HINT: Set max_prepared_transactions to a nonzero value.
106 SELECT * FROM pxtest1;
112 ROLLBACK PREPARED 'foo3';
113 ERROR: prepared transaction with identifier "foo3" does not exist
114 SELECT * FROM pxtest1;
122 -- Test subtransactions
124 CREATE TABLE pxtest2 (a int);
125 INSERT INTO pxtest2 VALUES (1);
127 INSERT INTO pxtest2 VALUES (2);
130 INSERT INTO pxtest2 VALUES (3);
131 PREPARE TRANSACTION 'regress-one';
132 ERROR: prepared transactions are disabled
133 HINT: Set max_prepared_transactions to a nonzero value.
134 CREATE TABLE pxtest3(fff int);
135 -- Test shared invalidation
138 CREATE TABLE pxtest4 (a int);
139 INSERT INTO pxtest4 VALUES (1);
140 INSERT INTO pxtest4 VALUES (2);
141 DECLARE foo CURSOR FOR SELECT * FROM pxtest4;
142 -- Fetch 1 tuple, keeping the cursor open
149 PREPARE TRANSACTION 'regress-two';
150 ERROR: prepared transactions are disabled
151 HINT: Set max_prepared_transactions to a nonzero value.
154 ERROR: cursor "foo" does not exist
155 -- Table doesn't exist, the creation hasn't been committed yet
156 SELECT * FROM pxtest2;
157 ERROR: relation "pxtest2" does not exist
158 LINE 1: SELECT * FROM pxtest2;
160 -- There should be two prepared transactions
161 SELECT gid FROM pg_prepared_xacts;
166 -- pxtest3 should be locked because of the pending DROP
167 set statement_timeout to 2000;
168 SELECT * FROM pxtest3;
173 reset statement_timeout;
174 -- Disconnect, we will continue testing in a different backend
176 -- There should still be two prepared transactions
177 SELECT gid FROM pg_prepared_xacts;
182 -- pxtest3 should still be locked because of the pending DROP
183 set statement_timeout to 2000;
184 SELECT * FROM pxtest3;
189 reset statement_timeout;
190 -- Commit table creation
191 COMMIT PREPARED 'regress-one';
192 ERROR: prepared transaction with identifier "regress-one" does not exist
194 SELECT * FROM pxtest2;
195 ERROR: relation "pxtest2" does not exist
196 LINE 1: SELECT * FROM pxtest2;
198 -- There should be one prepared transaction
199 SELECT gid FROM pg_prepared_xacts;
205 COMMIT PREPARED 'regress-two';
206 ERROR: prepared transaction with identifier "regress-two" does not exist
207 SELECT * FROM pxtest3;
212 -- There should be no prepared transactions
213 SELECT gid FROM pg_prepared_xacts;
220 ERROR: table "pxtest2" does not exist
221 DROP TABLE pxtest3; -- will still be there if prepared xacts are disabled
223 ERROR: table "pxtest4" does not exist