summaryrefslogtreecommitdiffstats
path: root/src/test/regress/expected/prepared_xacts.out
blob: ba8e3ccc6c9bf35d192dff5b8f49dd9be8055ea1 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
--
-- PREPARED TRANSACTIONS (two-phase commit)
--
-- We can't readily test persistence of prepared xacts within the
-- regression script framework, unfortunately.  Note that a crash
-- isn't really needed ... stopping and starting the postmaster would
-- be enough, but we can't even do that here.
-- create a simple table that we'll use in the tests
CREATE TABLE pxtest1 (foobar VARCHAR(10));
INSERT INTO pxtest1 VALUES ('aaa');
-- Test PREPARE TRANSACTION
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
UPDATE pxtest1 SET foobar = 'bbb' WHERE foobar = 'aaa';
SELECT * FROM pxtest1;
 foobar 
--------
 bbb
(1 row)

PREPARE TRANSACTION 'foo1';
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
(1 row)

-- Test pg_prepared_xacts system view
SELECT gid FROM pg_prepared_xacts;
 gid  
------
 foo1
(1 row)

-- Test ROLLBACK PREPARED
ROLLBACK PREPARED 'foo1';
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
(1 row)

SELECT gid FROM pg_prepared_xacts;
 gid 
-----
(0 rows)

-- Test COMMIT PREPARED
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
INSERT INTO pxtest1 VALUES ('ddd');
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
 ddd
(2 rows)

PREPARE TRANSACTION 'foo2';
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
(1 row)

COMMIT PREPARED 'foo2';
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
 ddd
(2 rows)

-- Test duplicate gids
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
UPDATE pxtest1 SET foobar = 'eee' WHERE foobar = 'ddd';
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
 eee
(2 rows)

PREPARE TRANSACTION 'foo3';
SELECT gid FROM pg_prepared_xacts;
 gid  
------
 foo3
(1 row)

BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
INSERT INTO pxtest1 VALUES ('fff');
-- This should fail, because the gid foo3 is already in use
PREPARE TRANSACTION 'foo3';
ERROR:  transaction identifier "foo3" is already in use
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
 ddd
(2 rows)

ROLLBACK PREPARED 'foo3';
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
 ddd
(2 rows)

-- Test serialization failure (SSI)
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
UPDATE pxtest1 SET foobar = 'eee' WHERE foobar = 'ddd';
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
 eee
(2 rows)

PREPARE TRANSACTION 'foo4';
SELECT gid FROM pg_prepared_xacts;
 gid  
------
 foo4
(1 row)

BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT * FROM pxtest1;
 foobar 
--------
 aaa
 ddd
(2 rows)

-- This should fail, because the two transactions have a write-skew anomaly
INSERT INTO pxtest1 VALUES ('fff');
ERROR:  could not serialize access due to read/write dependencies among transactions
DETAIL:  Reason code: Canceled on identification as a pivot, during write.
HINT:  The transaction might succeed if retried.
PREPARE TRANSACTION 'foo5';
SELECT gid FROM pg_prepared_xacts;
 gid  
------
 foo4
(1 row)

ROLLBACK PREPARED 'foo4';
SELECT gid FROM pg_prepared_xacts;
 gid 
-----
(0 rows)

-- Clean up
DROP TABLE pxtest1;
-- Test detection of session-level and xact-level locks on same object
BEGIN;
SELECT pg_advisory_lock(1);
 pg_advisory_lock 
------------------
 
(1 row)

SELECT pg_advisory_xact_lock_shared(1);
 pg_advisory_xact_lock_shared 
------------------------------
 
(1 row)

PREPARE TRANSACTION 'foo6';  -- fails
ERROR:  cannot PREPARE while holding both session-level and transaction-level locks on the same object
-- Test subtransactions
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
  CREATE TABLE pxtest2 (a int);
  INSERT INTO pxtest2 VALUES (1);
  SAVEPOINT a;
    INSERT INTO pxtest2 VALUES (2);
  ROLLBACK TO a;
  SAVEPOINT b;
  INSERT INTO pxtest2 VALUES (3);
PREPARE TRANSACTION 'regress-one';
CREATE TABLE pxtest3(fff int);
-- Test shared invalidation
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
  DROP TABLE pxtest3;
  CREATE TABLE pxtest4 (a int);
  INSERT INTO pxtest4 VALUES (1);
  INSERT INTO pxtest4 VALUES (2);
  DECLARE foo CURSOR FOR SELECT * FROM pxtest4;
  -- Fetch 1 tuple, keeping the cursor open
  FETCH 1 FROM foo;
 a 
---
 1
(1 row)

PREPARE TRANSACTION 'regress-two';
-- No such cursor
FETCH 1 FROM foo;
ERROR:  cursor "foo" does not exist
-- Table doesn't exist, the creation hasn't been committed yet
SELECT * FROM pxtest2;
ERROR:  relation "pxtest2" does not exist
LINE 1: SELECT * FROM pxtest2;
                      ^
-- There should be two prepared transactions
SELECT gid FROM pg_prepared_xacts;
     gid     
-------------
 regress-one
 regress-two
(2 rows)

-- pxtest3 should be locked because of the pending DROP
begin;
lock table pxtest3 in access share mode nowait;
ERROR:  could not obtain lock on relation "pxtest3"
rollback;
-- Disconnect, we will continue testing in a different backend
\c -
-- There should still be two prepared transactions
SELECT gid FROM pg_prepared_xacts;
     gid     
-------------
 regress-one
 regress-two
(2 rows)

-- pxtest3 should still be locked because of the pending DROP
begin;
lock table pxtest3 in access share mode nowait;
ERROR:  could not obtain lock on relation "pxtest3"
rollback;
-- Commit table creation
COMMIT PREPARED 'regress-one';
\d pxtest2
              Table "public.pxtest2"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           |          | 

SELECT * FROM pxtest2;
 a 
---
 1
 3
(2 rows)

-- There should be one prepared transaction
SELECT gid FROM pg_prepared_xacts;
     gid     
-------------
 regress-two
(1 row)

-- Commit table drop
COMMIT PREPARED 'regress-two';
SELECT * FROM pxtest3;
ERROR:  relation "pxtest3" does not exist
LINE 1: SELECT * FROM pxtest3;
                      ^
-- There should be no prepared transactions
SELECT gid FROM pg_prepared_xacts;
 gid 
-----
(0 rows)

-- Clean up
DROP TABLE pxtest2;
DROP TABLE pxtest3;  -- will still be there if prepared xacts are disabled
ERROR:  table "pxtest3" does not exist
DROP TABLE pxtest4;