Relax overly strict assertion
authorAlvaro Herrera <[email protected]>
Tue, 12 Feb 2019 21:42:37 +0000 (18:42 -0300)
committerAlvaro Herrera <[email protected]>
Tue, 12 Feb 2019 21:42:37 +0000 (18:42 -0300)
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

contrib/test_decoding/expected/ddl.out
contrib/test_decoding/sql/ddl.sql
src/backend/replication/logical/reorderbuffer.c

index 1ead37a72916ea69f8e9a57a256a4ed4583d4e82..a04038555efff4981fa730de1fe365e092bcbaef 100644 (file)
@@ -338,6 +338,24 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc
  COMMIT
 (6 rows)
 
+-- check that DDL in aborted subtransactions handled correctly
+CREATE TABLE tr_sub_ddl(data int);
+BEGIN;
+SAVEPOINT a;
+ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE text;
+INSERT INTO tr_sub_ddl VALUES ('blah-blah');
+ROLLBACK TO SAVEPOINT a;
+ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE bigint;
+INSERT INTO tr_sub_ddl VALUES(43);
+COMMIT;
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+                       data                       
+--------------------------------------------------
+ BEGIN
+ table public.tr_sub_ddl: INSERT: data[bigint]:43
+ COMMIT
+(3 rows)
+
 /*
  * Check whether treating a table as a catalog table works somewhat
  */
index 7f3446e70d48a8c03bec1e530d3d08b000bfb851..ae9835fe8693ea296a1e34440d1613ac06e549df 100644 (file)
@@ -215,6 +215,19 @@ INSERT INTO tr_sub(path) VALUES ('5-top-1-#1');
 COMMIT;
 
 
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+
+-- check that DDL in aborted subtransactions handled correctly
+CREATE TABLE tr_sub_ddl(data int);
+BEGIN;
+SAVEPOINT a;
+ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE text;
+INSERT INTO tr_sub_ddl VALUES ('blah-blah');
+ROLLBACK TO SAVEPOINT a;
+ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE bigint;
+INSERT INTO tr_sub_ddl VALUES(43);
+COMMIT;
+
 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
 
 
index 42d448162418a3464dba32bebfe1bcac28fc848c..fbc2b6fe1eee2ffe6bb758ef108c304d2d4917f2 100644 (file)
@@ -1326,15 +1326,19 @@ ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
        }
        else
        {
+           /*
+            * Maybe we already saw this tuple before in this transaction,
+            * but if so it must have the same cmin.
+            */
            Assert(ent->cmin == change->data.tuplecid.cmin);
-           Assert(ent->cmax == InvalidCommandId ||
-                  ent->cmax == change->data.tuplecid.cmax);
 
            /*
-            * if the tuple got valid in this transaction and now got deleted
-            * we already have a valid cmin stored. The cmax will be
-            * InvalidCommandId though.
+            * cmax may be initially invalid, but once set it can only grow,
+            * and never become invalid again.
             */
+           Assert((ent->cmax == InvalidCommandId) ||
+                  ((change->data.tuplecid.cmax != InvalidCommandId) &&
+                   (change->data.tuplecid.cmax > ent->cmax)));
            ent->cmax = change->data.tuplecid.cmax;
        }
    }