]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix assertions with RI triggers in heap_update and heap_delete.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Tue, 28 Nov 2023 09:59:09 +0000 (11:59 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Tue, 28 Nov 2023 09:59:53 +0000 (11:59 +0200)
If the tuple being updated is not visible to the crosscheck snapshot,
we return TM_Updated but the assertions would not hold in that case.
Move them to before the cross-check.

Fixes bug #17893. Backpatch to all supported versions.

Author: Alexander Lakhin
Backpatch-through: 12
Discussion: https://www.postgresql.org/message-id/17893-35847009eec517b5%40postgresql.org

src/backend/access/heap/heapam.c
src/include/access/tableam.h

index 657af12a3990504b88fee00dfa2159cbf21a2a43..e1e54a265cfcc0608c35754d12ab68177cc084d2 100644 (file)
@@ -2677,13 +2677,7 @@ l1:
                        result = TM_Deleted;
        }
 
-       if (crosscheck != InvalidSnapshot && result == TM_Ok)
-       {
-               /* Perform additional check for transaction-snapshot mode RI updates */
-               if (!HeapTupleSatisfiesVisibility(&tp, crosscheck, buffer))
-                       result = TM_Updated;
-       }
-
+       /* sanity check the result HeapTupleSatisfiesUpdate() and the logic above */
        if (result != TM_Ok)
        {
                Assert(result == TM_SelfModified ||
@@ -2693,6 +2687,17 @@ l1:
                Assert(!(tp.t_data->t_infomask & HEAP_XMAX_INVALID));
                Assert(result != TM_Updated ||
                           !ItemPointerEquals(&tp.t_self, &tp.t_data->t_ctid));
+       }
+
+       if (crosscheck != InvalidSnapshot && result == TM_Ok)
+       {
+               /* Perform additional check for transaction-snapshot mode RI updates */
+               if (!HeapTupleSatisfiesVisibility(&tp, crosscheck, buffer))
+                       result = TM_Updated;
+       }
+
+       if (result != TM_Ok)
+       {
                tmfd->ctid = tp.t_data->t_ctid;
                tmfd->xmax = HeapTupleHeaderGetUpdateXid(tp.t_data);
                if (result == TM_SelfModified)
@@ -3320,16 +3325,7 @@ l2:
                        result = TM_Deleted;
        }
 
-       if (crosscheck != InvalidSnapshot && result == TM_Ok)
-       {
-               /* Perform additional check for transaction-snapshot mode RI updates */
-               if (!HeapTupleSatisfiesVisibility(&oldtup, crosscheck, buffer))
-               {
-                       result = TM_Updated;
-                       Assert(!ItemPointerEquals(&oldtup.t_self, &oldtup.t_data->t_ctid));
-               }
-       }
-
+       /* Sanity check the result HeapTupleSatisfiesUpdate() and the logic above */
        if (result != TM_Ok)
        {
                Assert(result == TM_SelfModified ||
@@ -3339,6 +3335,17 @@ l2:
                Assert(!(oldtup.t_data->t_infomask & HEAP_XMAX_INVALID));
                Assert(result != TM_Updated ||
                           !ItemPointerEquals(&oldtup.t_self, &oldtup.t_data->t_ctid));
+       }
+
+       if (crosscheck != InvalidSnapshot && result == TM_Ok)
+       {
+               /* Perform additional check for transaction-snapshot mode RI updates */
+               if (!HeapTupleSatisfiesVisibility(&oldtup, crosscheck, buffer))
+                       result = TM_Updated;
+       }
+
+       if (result != TM_Ok)
+       {
                tmfd->ctid = oldtup.t_data->t_ctid;
                tmfd->xmax = HeapTupleHeaderGetUpdateXid(oldtup.t_data);
                if (result == TM_SelfModified)
index 5933012d1837b86815f57a6eb7d5dd1f8f7b3eec..29f67d515350a6cfca24609462162a6c1d62bb5f 100644 (file)
@@ -1220,8 +1220,8 @@ table_multi_insert(Relation rel, TupleTableSlot **slots, int nslots,
  * TM_BeingModified (the last only possible if wait == false).
  *
  * In the failure cases, the routine fills *tmfd with the tuple's t_ctid,
- * t_xmax, and, if possible, and, if possible, t_cmax.  See comments for
- * struct TM_FailureData for additional info.
+ * t_xmax, and, if possible, t_cmax.  See comments for struct
+ * TM_FailureData for additional info.
  */
 static inline TM_Result
 table_tuple_delete(Relation rel, ItemPointer tid, CommandId cid,