Skip to content

Commit f5f9a76

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 2a0edae commit f5f9a76

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
@@ -405,6 +405,24 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc
405405
COMMIT
406406
(6 rows)
407407

408+
-- check that DDL in aborted subtransactions handled correctly
409+
CREATE TABLE tr_sub_ddl(data int);
410+
BEGIN;
411+
SAVEPOINT a;
412+
ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE text;
413+
INSERT INTO tr_sub_ddl VALUES ('blah-blah');
414+
ROLLBACK TO SAVEPOINT a;
415+
ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE bigint;
416+
INSERT INTO tr_sub_ddl VALUES(43);
417+
COMMIT;
418+
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
419+
data
420+
--------------------------------------------------
421+
BEGIN
422+
table public.tr_sub_ddl: INSERT: data[bigint]:43
423+
COMMIT
424+
(3 rows)
425+
408426
/*
409427
* Check whether treating a table as a catalog table works somewhat
410428
*/

contrib/test_decoding/sql/ddl.sql

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

237237

238+
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
239+
240+
-- check that DDL in aborted subtransactions handled correctly
241+
CREATE TABLE tr_sub_ddl(data int);
242+
BEGIN;
243+
SAVEPOINT a;
244+
ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE text;
245+
INSERT INTO tr_sub_ddl VALUES ('blah-blah');
246+
ROLLBACK TO SAVEPOINT a;
247+
ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE bigint;
248+
INSERT INTO tr_sub_ddl VALUES(43);
249+
COMMIT;
250+
238251
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
239252

240253

src/backend/replication/logical/reorderbuffer.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,15 +1334,19 @@ ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
13341334
}
13351335
else
13361336
{
1337+
/*
1338+
* Maybe we already saw this tuple before in this transaction,
1339+
* but if so it must have the same cmin.
1340+
*/
13371341
Assert(ent->cmin == change->data.tuplecid.cmin);
1338-
Assert(ent->cmax == InvalidCommandId ||
1339-
ent->cmax == change->data.tuplecid.cmax);
13401342

13411343
/*
1342-
* if the tuple got valid in this transaction and now got deleted
1343-
* we already have a valid cmin stored. The cmax will be
1344-
* InvalidCommandId though.
1344+
* cmax may be initially invalid, but once set it can only grow,
1345+
* and never become invalid again.
13451346
*/
1347+
Assert((ent->cmax == InvalidCommandId) ||
1348+
((change->data.tuplecid.cmax != InvalidCommandId) &&
1349+
(change->data.tuplecid.cmax > ent->cmax)));
13461350
ent->cmax = change->data.tuplecid.cmax;
13471351
}
13481352
}

0 commit comments

Comments
 (0)