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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
--
-- Test cumulative statistics with relation rewrites
--
-- Two-phase commit.
-- Table-level stats with VACUUM and rewrite after 2PC commit.
CREATE TABLE test_2pc_timestamp (a int) WITH (autovacuum_enabled = false);
VACUUM ANALYZE test_2pc_timestamp;
SELECT last_analyze AS last_vacuum_analyze
FROM pg_stat_all_tables WHERE relname = 'test_2pc_timestamp' \gset
BEGIN;
ALTER TABLE test_2pc_timestamp ALTER COLUMN a TYPE int;
PREPARE TRANSACTION 'test';
COMMIT PREPARED 'test';
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT last_analyze = :'last_vacuum_analyze'::timestamptz AS same_vacuum_ts
FROM pg_stat_all_tables WHERE relname = 'test_2pc_timestamp';
same_vacuum_ts
----------------
t
(1 row)
DROP TABLE test_2pc_timestamp;
-- Table-level stats with single rewrite after 2PC commit.
CREATE TABLE test_2pc_rewrite_alone (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_2pc_rewrite_alone VALUES (1);
BEGIN;
ALTER TABLE test_2pc_rewrite_alone ALTER COLUMN a TYPE bigint;
PREPARE TRANSACTION 'test';
COMMIT PREPARED 'test';
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_2pc_rewrite_alone';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
1 | 1 | 0
(1 row)
DROP TABLE test_2pc_rewrite_alone;
-- Table-level stats with rewrite and DMLs after 2PC commit.
CREATE TABLE test_2pc (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_2pc VALUES (1);
BEGIN;
INSERT INTO test_2pc VALUES (1);
INSERT INTO test_2pc VALUES (2);
INSERT INTO test_2pc VALUES (3);
ALTER TABLE test_2pc ALTER COLUMN a TYPE bigint;
PREPARE TRANSACTION 'test';
COMMIT PREPARED 'test';
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_2pc';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
4 | 4 | 0
(1 row)
DROP TABLE test_2pc;
-- Table-level stats with multiple rewrites after 2PC commit.
CREATE TABLE test_2pc_multi (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_2pc_multi VALUES (1);
BEGIN;
INSERT INTO test_2pc_multi VALUES (1);
INSERT INTO test_2pc_multi VALUES (2);
ALTER TABLE test_2pc_multi ALTER COLUMN a TYPE bigint;
INSERT INTO test_2pc_multi VALUES (3);
INSERT INTO test_2pc_multi VALUES (4);
ALTER TABLE test_2pc_multi ALTER COLUMN a TYPE int;
INSERT INTO test_2pc_multi VALUES (5);
PREPARE TRANSACTION 'test';
COMMIT PREPARED 'test';
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_2pc_multi';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
6 | 6 | 0
(1 row)
DROP TABLE test_2pc_multi;
-- Table-level stats with single rewrite after 2PC abort.
CREATE TABLE test_2pc_rewrite_alone_abort (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_2pc_rewrite_alone_abort VALUES (1);
BEGIN;
ALTER TABLE test_2pc_rewrite_alone_abort ALTER COLUMN a TYPE bigint;
PREPARE TRANSACTION 'test';
ROLLBACK PREPARED 'test';
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_2pc_rewrite_alone_abort';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
1 | 1 | 0
(1 row)
DROP TABLE test_2pc_rewrite_alone_abort;
-- Table-level stats with rewrite and DMLs after 2PC abort.
CREATE TABLE test_2pc_abort (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_2pc_abort VALUES (1);
BEGIN;
INSERT INTO test_2pc_abort VALUES (1);
INSERT INTO test_2pc_abort VALUES (2);
ALTER TABLE test_2pc_abort ALTER COLUMN a TYPE bigint;
INSERT INTO test_2pc_abort VALUES (3);
PREPARE TRANSACTION 'test';
ROLLBACK PREPARED 'test';
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_2pc_abort';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
4 | 1 | 3
(1 row)
DROP TABLE test_2pc_abort;
-- Table-level stats with rewrites and subtransactions after 2PC commit.
CREATE TABLE test_2pc_savepoint (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_2pc_savepoint VALUES (1);
BEGIN;
SAVEPOINT a;
INSERT INTO test_2pc_savepoint VALUES (1);
INSERT INTO test_2pc_savepoint VALUES (2);
ALTER TABLE test_2pc_savepoint ALTER COLUMN a TYPE bigint;
SAVEPOINT b;
INSERT INTO test_2pc_savepoint VALUES (3);
ALTER TABLE test_2pc_savepoint ALTER COLUMN a TYPE int;
SAVEPOINT c;
INSERT INTO test_2pc_savepoint VALUES (4);
INSERT INTO test_2pc_savepoint VALUES (5);
ROLLBACK TO SAVEPOINT b;
PREPARE TRANSACTION 'test';
COMMIT PREPARED 'test';
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_2pc_savepoint';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
6 | 3 | 3
(1 row)
DROP TABLE test_2pc_savepoint;
-- Table-level stats with single rewrite and VACUUM
CREATE TABLE test_timestamp (a int) WITH (autovacuum_enabled = false);
VACUUM ANALYZE test_timestamp;
SELECT last_analyze AS last_vacuum_analyze
FROM pg_stat_all_tables WHERE relname = 'test_timestamp' \gset
ALTER TABLE test_timestamp ALTER COLUMN a TYPE bigint;
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT last_analyze = :'last_vacuum_analyze'::timestamptz AS same_vacuum_ts
FROM pg_stat_all_tables WHERE relname = 'test_timestamp';
same_vacuum_ts
----------------
t
(1 row)
DROP TABLE test_timestamp;
-- Table-level stats with single rewrite.
CREATE TABLE test_alone (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_alone VALUES (1);
BEGIN;
ALTER TABLE test_alone ALTER COLUMN a TYPE bigint;
COMMIT;
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_alone';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
1 | 1 | 0
(1 row)
DROP TABLE test_alone;
-- Table-level stats with rewrite and DMLs.
CREATE TABLE test (a int) WITH (autovacuum_enabled = false);
INSERT INTO test VALUES (1);
BEGIN;
INSERT INTO test VALUES (1);
INSERT INTO test VALUES (2);
INSERT INTO test VALUES (3);
ALTER TABLE test ALTER COLUMN a TYPE bigint;
COMMIT;
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
4 | 4 | 0
(1 row)
DROP TABLE test;
-- Table-level stats with multiple rewrites and DMLs.
CREATE TABLE test_multi (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_multi VALUES (1);
BEGIN;
INSERT INTO test_multi VALUES (1);
INSERT INTO test_multi VALUES (2);
ALTER TABLE test_multi ALTER COLUMN a TYPE bigint;
INSERT INTO test_multi VALUES (3);
INSERT INTO test_multi VALUES (4);
ALTER TABLE test_multi ALTER COLUMN a TYPE int;
INSERT INTO test_multi VALUES (5);
COMMIT;
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_multi';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
6 | 6 | 0
(1 row)
DROP TABLE test_multi;
-- Table-level stats with rewrite and rollback.
CREATE TABLE test_rewrite_alone_abort (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_rewrite_alone_abort VALUES (1);
BEGIN;
ALTER TABLE test_rewrite_alone_abort ALTER COLUMN a TYPE bigint;
ROLLBACK;
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_rewrite_alone_abort';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
1 | 1 | 0
(1 row)
DROP TABLE test_rewrite_alone_abort;
-- Table-level stats with rewrite, DMLs and rollback.
CREATE TABLE test_abort (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_abort VALUES (1);
BEGIN;
INSERT INTO test_abort VALUES (1);
INSERT INTO test_abort VALUES (2);
ALTER TABLE test_abort ALTER COLUMN a TYPE bigint;
INSERT INTO test_abort VALUES (3);
ROLLBACK;
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_abort';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
4 | 1 | 3
(1 row)
DROP TABLE test_abort;
-- Table-level stats with rewrites and subtransactions.
CREATE TABLE test_savepoint (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_savepoint VALUES (1);
BEGIN;
SAVEPOINT a;
INSERT INTO test_savepoint VALUES (1);
INSERT INTO test_savepoint VALUES (2);
ALTER TABLE test_savepoint ALTER COLUMN a TYPE bigint;
SAVEPOINT b;
INSERT INTO test_savepoint VALUES (3);
ALTER TABLE test_savepoint ALTER COLUMN a TYPE int;
SAVEPOINT c;
INSERT INTO test_savepoint VALUES (4);
INSERT INTO test_savepoint VALUES (5);
ROLLBACK TO SAVEPOINT b;
COMMIT;
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_savepoint';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
6 | 3 | 3
(1 row)
DROP TABLE test_savepoint;
-- Table-level stats with tablespace rewrite.
CREATE TABLE test_tbs (a int) WITH (autovacuum_enabled = false);
INSERT INTO test_tbs VALUES (1);
ALTER TABLE test_tbs SET TABLESPACE pg_default;
SELECT pg_stat_force_next_flush();
pg_stat_force_next_flush
--------------------------
(1 row)
SELECT n_tup_ins, n_live_tup, n_dead_tup
FROM pg_stat_all_tables WHERE relname = 'test_tbs';
n_tup_ins | n_live_tup | n_dead_tup
-----------+------------+------------
1 | 1 | 0
(1 row)
DROP TABLE test_tbs;
|