This is an automated email from the ASF dual-hosted git repository.

leborchuk pushed a commit to branch REL_2_STABLE
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit bb85059b12cb1fe3e2c458fc00102e741cf88254
Author: Heikki Linnakangas <[email protected]>
AuthorDate: Tue Nov 28 11:59:09 2023 +0200

    Fix assertions with RI triggers in heap_update and heap_delete.
    
    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
    (cherry picked from commit 59c62a21f2439e425abae5a2e9dd1d53371226ea)
---
 src/backend/access/heap/heapam.c | 41 +++++++++++++++++++++++-----------------
 src/include/access/tableam.h     |  4 ++--
 2 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 70574f6db41..9018c755465 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -3029,13 +3029,7 @@ l1:
                        result = TM_Deleted;
        }
 
-       if (crosscheck != InvalidSnapshot && result == TM_Ok)
-       {
-               /* Perform additional check for transaction-snapshot mode RI 
updates */
-               if (!HeapTupleSatisfiesVisibility(relation, &tp, crosscheck, 
buffer))
-                       result = TM_Updated;
-       }
-
+       /* sanity check the result HeapTupleSatisfiesUpdate() and the logic 
above */
        if (result != TM_Ok)
        {
                Assert(result == TM_SelfModified ||
@@ -3045,6 +3039,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(relation, &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)
@@ -3686,16 +3691,7 @@ l2:
                        result = TM_Deleted;
        }
 
-       if (crosscheck != InvalidSnapshot && result == TM_Ok)
-       {
-               /* Perform additional check for transaction-snapshot mode RI 
updates */
-               if (!HeapTupleSatisfiesVisibility(relation, &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 ||
@@ -3705,6 +3701,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(relation, &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)
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index 2253e400efb..572033ad9bb 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -1685,8 +1685,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,


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to