Skip to content

Commit 350cdcd

Browse files
committed
Relax overly strict assertion
Ever since its birth, ReorderBufferBuildTupleCidHash() has contained an assertion that a catalog tuple cannot change Cmax after acquiring one. But that's wrong: if a subtransaction executes DDL that affects that catalog tuple, and later aborts and another DDL affects the same tuple, it will change Cmax. Relax the assertion to merely verify that the Cmax remains valid and monotonically increasing, instead. Add a test that tickles the relevant code. Diagnosed by, and initial patch submitted by: Arseny Sher Co-authored-by: Arseny Sher Discussion: https://postgr.es/m/874l9p8hyw.fsf@ars-thinkpad
1 parent 42baa60 commit 350cdcd

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

contrib/test_decoding/expected/ddl.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,24 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc
338338
COMMIT
339339
(6 rows)
340340

341+
-- check that DDL in aborted subtransactions handled correctly
342+
CREATE TABLE tr_sub_ddl(data int);
343+
BEGIN;
344+
SAVEPOINT a;
345+
ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE text;
346+
INSERT INTO tr_sub_ddl VALUES ('blah-blah');
347+
ROLLBACK TO SAVEPOINT a;
348+
ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE bigint;
349+
INSERT INTO tr_sub_ddl VALUES(43);
350+
COMMIT;
351+
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
352+
data
353+
--------------------------------------------------
354+
BEGIN
355+
table public.tr_sub_ddl: INSERT: data[bigint]:43
356+
COMMIT
357+
(3 rows)
358+
341359
/*
342360
* Check whether treating a table as a catalog table works somewhat
343361
*/

contrib/test_decoding/sql/ddl.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ INSERT INTO tr_sub(path) VALUES ('5-top-1-#1');
215215
COMMIT;
216216

217217

218+
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
219+
220+
-- check that DDL in aborted subtransactions handled correctly
221+
CREATE TABLE tr_sub_ddl(data int);
222+
BEGIN;
223+
SAVEPOINT a;
224+
ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE text;
225+
INSERT INTO tr_sub_ddl VALUES ('blah-blah');
226+
ROLLBACK TO SAVEPOINT a;
227+
ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE bigint;
228+
INSERT INTO tr_sub_ddl VALUES(43);
229+
COMMIT;
230+
218231
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
219232

220233

src/backend/replication/logical/reorderbuffer.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,15 +1326,19 @@ ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
13261326
}
13271327
else
13281328
{
1329+
/*
1330+
* Maybe we already saw this tuple before in this transaction,
1331+
* but if so it must have the same cmin.
1332+
*/
13291333
Assert(ent->cmin == change->data.tuplecid.cmin);
1330-
Assert(ent->cmax == InvalidCommandId ||
1331-
ent->cmax == change->data.tuplecid.cmax);
13321334

13331335
/*
1334-
* if the tuple got valid in this transaction and now got deleted
1335-
* we already have a valid cmin stored. The cmax will be
1336-
* InvalidCommandId though.
1336+
* cmax may be initially invalid, but once set it can only grow,
1337+
* and never become invalid again.
13371338
*/
1339+
Assert((ent->cmax == InvalidCommandId) ||
1340+
((change->data.tuplecid.cmax != InvalidCommandId) &&
1341+
(change->data.tuplecid.cmax > ent->cmax)));
13381342
ent->cmax = change->data.tuplecid.cmax;
13391343
}
13401344
}

0 commit comments

Comments
 (0)