Repair "Halloween problem" in EvalPlanQual: a tuple that's been inserted by
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 12 Jan 2006 21:49:07 +0000 (21:49 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 12 Jan 2006 21:49:07 +0000 (21:49 +0000)
our own command (or more generally, xmin = our xact and cmin >= current
command ID) should not be seen as good.  Else we may try to update rows
we already updated.  This error was inserted last August while fixing the
even bigger problem that the old coding wouldn't see *any* tuples inserted
by our own transaction as good.  Per report from Euler Taveira de Oliveira.

src/backend/commands/trigger.c
src/backend/executor/execMain.c
src/include/executor/executor.h

index 4a91527eac9a705dfaa949157323945f6feacb9f..e31a555a92029de7824bb8a95448bf507cca93df 100644 (file)
@@ -1736,7 +1736,8 @@ ltrmark:;
                                        epqslot = EvalPlanQual(estate,
                                                                                   relinfo->ri_RangeTableIndex,
                                                                                   &update_ctid,
-                                                                                  update_xmax);
+                                                                                  update_xmax,
+                                                                                  cid);
                                        if (!TupIsNull(epqslot))
                                        {
                                                *tid = update_ctid;
index 929dcc12e824449f8dc8606316d0627a37c30fb8..a63da79e9cdb76f01f7e2dbc7a57dce834fd912b 100644 (file)
@@ -1219,7 +1219,8 @@ lnext:    ;
                                                                newSlot = EvalPlanQual(estate,
                                                                                                           erm->rti,
                                                                                                           &update_ctid,
-                                                                                                          update_xmax);
+                                                                                                          update_xmax,
+                                                                                                          estate->es_snapshot->curcid);
                                                                if (!TupIsNull(newSlot))
                                                                {
                                                                        slot = newSlot;
@@ -1527,7 +1528,8 @@ ldelete:;
                                epqslot = EvalPlanQual(estate,
                                                                           resultRelInfo->ri_RangeTableIndex,
                                                                           &update_ctid,
-                                                                          update_xmax);
+                                                                          update_xmax,
+                                                                          estate->es_snapshot->curcid);
                                if (!TupIsNull(epqslot))
                                {
                                        *tupleid = update_ctid;
@@ -1679,7 +1681,8 @@ lreplace:;
                                epqslot = EvalPlanQual(estate,
                                                                           resultRelInfo->ri_RangeTableIndex,
                                                                           &update_ctid,
-                                                                          update_xmax);
+                                                                          update_xmax,
+                                                                          estate->es_snapshot->curcid);
                                if (!TupIsNull(epqslot))
                                {
                                        *tupleid = update_ctid;
@@ -1826,6 +1829,7 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
  *     rti - rangetable index of table containing tuple
  *     *tid - t_ctid from the outdated tuple (ie, next updated version)
  *     priorXmax - t_xmax from the outdated tuple
+ *     curCid - command ID of current command of my transaction
  *
  * *tid is also an output parameter: it's modified to hold the TID of the
  * latest version of the tuple (note this may be changed even on failure)
@@ -1835,7 +1839,7 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
  */
 TupleTableSlot *
 EvalPlanQual(EState *estate, Index rti,
-                        ItemPointer tid, TransactionId priorXmax)
+                        ItemPointer tid, TransactionId priorXmax, CommandId curCid)
 {
        evalPlanQual *epq;
        EState     *epqstate;
@@ -1911,6 +1915,24 @@ EvalPlanQual(EState *estate, Index rti,
                                continue;               /* loop back to repeat heap_fetch */
                        }
 
+                       /*
+                        * If tuple was inserted by our own transaction, we have to check
+                        * cmin against curCid: cmin >= curCid means our command cannot
+                        * see the tuple, so we should ignore it.  Without this we are
+                        * open to the "Halloween problem" of indefinitely re-updating
+                        * the same tuple.  (We need not check cmax because
+                        * HeapTupleSatisfiesDirty will consider a tuple deleted by
+                        * our transaction dead, regardless of cmax.)  We just checked
+                        * that priorXmax == xmin, so we can test that variable instead
+                        * of doing HeapTupleHeaderGetXmin again.
+                        */
+                       if (TransactionIdIsCurrentTransactionId(priorXmax) &&
+                               HeapTupleHeaderGetCmin(tuple.t_data) >= curCid)
+                       {
+                               ReleaseBuffer(buffer);
+                               return NULL;
+                       }
+
                        /*
                         * We got tuple - now copy it for use by recheck query.
                         */
index 75fefa4a4fe63836c572ba1867beb8a09a1725f9..976634b966908648a5471c45d8a58b5e4bf7b8a6 100644 (file)
@@ -98,7 +98,7 @@ extern bool ExecContextForcesOids(PlanState *planstate, bool *hasoids);
 extern void ExecConstraints(ResultRelInfo *resultRelInfo,
                                TupleTableSlot *slot, EState *estate);
 extern TupleTableSlot *EvalPlanQual(EState *estate, Index rti,
-                        ItemPointer tid, TransactionId priorXmax);
+                        ItemPointer tid, TransactionId priorXmax, CommandId curCid);
 
 /*
  * prototypes from functions in execProcnode.c