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
12 BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
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
50 BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
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
77 BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
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;
93 BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
94 INSERT INTO pxtest1 VALUES ('fff');
95 -- This should fail, because the gid foo3 is already in use
96 PREPARE TRANSACTION 'foo3';
97 ERROR: prepared transactions are disabled
98 HINT: Set max_prepared_transactions to a nonzero value.
99 SELECT * FROM pxtest1;
105 ROLLBACK PREPARED 'foo3';
106 ERROR: prepared transaction with identifier "foo3" does not exist
107 SELECT * FROM pxtest1;
113 -- Test serialization failure (SSI)
114 BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
115 UPDATE pxtest1 SET foobar = 'eee' WHERE foobar = 'ddd';
116 SELECT * FROM pxtest1;
122 PREPARE TRANSACTION 'foo4';
123 ERROR: prepared transactions are disabled
124 HINT: Set max_prepared_transactions to a nonzero value.
125 SELECT gid FROM pg_prepared_xacts;
130 BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
131 SELECT * FROM pxtest1;
137 -- This should fail, because the two transactions have a write-skew anomaly
138 INSERT INTO pxtest1 VALUES ('fff');
139 PREPARE TRANSACTION 'foo5';
140 ERROR: prepared transactions are disabled
141 HINT: Set max_prepared_transactions to a nonzero value.
142 SELECT gid FROM pg_prepared_xacts;
147 ROLLBACK PREPARED 'foo4';
148 ERROR: prepared transaction with identifier "foo4" does not exist
149 SELECT gid FROM pg_prepared_xacts;
156 -- Test detection of session-level and xact-level locks on same object
158 SELECT pg_advisory_lock(1);
164 SELECT pg_advisory_xact_lock_shared(1);
165 pg_advisory_xact_lock_shared
166 ------------------------------
170 PREPARE TRANSACTION 'foo6'; -- fails
171 ERROR: prepared transactions are disabled
172 HINT: Set max_prepared_transactions to a nonzero value.
173 -- Test subtransactions
174 BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
175 CREATE TABLE pxtest2 (a int);
176 INSERT INTO pxtest2 VALUES (1);
178 INSERT INTO pxtest2 VALUES (2);
181 INSERT INTO pxtest2 VALUES (3);
182 PREPARE TRANSACTION 'regress-one';
183 ERROR: prepared transactions are disabled
184 HINT: Set max_prepared_transactions to a nonzero value.
185 CREATE TABLE pxtest3(fff int);
186 -- Test shared invalidation
187 BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
189 CREATE TABLE pxtest4 (a int);
190 INSERT INTO pxtest4 VALUES (1);
191 INSERT INTO pxtest4 VALUES (2);
192 DECLARE foo CURSOR FOR SELECT * FROM pxtest4;
193 -- Fetch 1 tuple, keeping the cursor open
200 PREPARE TRANSACTION 'regress-two';
201 ERROR: prepared transactions are disabled
202 HINT: Set max_prepared_transactions to a nonzero value.
205 ERROR: cursor "foo" does not exist
206 -- Table doesn't exist, the creation hasn't been committed yet
207 SELECT * FROM pxtest2;
208 ERROR: relation "pxtest2" does not exist
209 LINE 1: SELECT * FROM pxtest2;
211 -- There should be two prepared transactions
212 SELECT gid FROM pg_prepared_xacts;
217 -- pxtest3 should be locked because of the pending DROP
219 lock table pxtest3 in access share mode nowait;
221 -- Disconnect, we will continue testing in a different backend
223 -- There should still be two prepared transactions
224 SELECT gid FROM pg_prepared_xacts;
229 -- pxtest3 should still be locked because of the pending DROP
231 lock table pxtest3 in access share mode nowait;
233 -- Commit table creation
234 COMMIT PREPARED 'regress-one';
235 ERROR: prepared transaction with identifier "regress-one" does not exist
237 SELECT * FROM pxtest2;
238 ERROR: relation "pxtest2" does not exist
239 LINE 1: SELECT * FROM pxtest2;
241 -- There should be one prepared transaction
242 SELECT gid FROM pg_prepared_xacts;
248 COMMIT PREPARED 'regress-two';
249 ERROR: prepared transaction with identifier "regress-two" does not exist
250 SELECT * FROM pxtest3;
255 -- There should be no prepared transactions
256 SELECT gid FROM pg_prepared_xacts;
263 ERROR: table "pxtest2" does not exist
264 DROP TABLE pxtest3; -- will still be there if prepared xacts are disabled
266 ERROR: table "pxtest4" does not exist