From: Tom Lane Date: Thu, 12 Jan 2006 21:49:41 +0000 (+0000) Subject: Repair "Halloween problem" in EvalPlanQual: a tuple that's been inserted by X-Git-Tag: REL7_3_14~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a6cc7db9a67663d59ceb6328d2b0ccea7b384ef6;p=thirdparty%2Fpostgresql.git Repair "Halloween problem" in EvalPlanQual: a tuple that's been inserted by 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. --- diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index 125a9d2063e..3a4818eeeb9 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.136.2.3 2005/08/26 20:07:15 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.136.2.4 2006/01/12 21:49:40 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1385,7 +1385,8 @@ ltrmark:; epqslot = EvalPlanQual(estate, relinfo->ri_RangeTableIndex, &update_ctid, - update_xmax); + update_xmax, + cid); if (!TupIsNull(epqslot)) { *tid = update_ctid; diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index b6684579f1b..400afb81d28 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -27,7 +27,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.180.2.3 2005/08/26 20:07:17 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.180.2.4 2006/01/12 21:49:40 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1056,7 +1056,8 @@ lnext: ; newSlot = EvalPlanQual(estate, erm->rti, &update_ctid, - update_xmax); + update_xmax, + estate->es_snapshot->curcid); if (!TupIsNull(newSlot)) { slot = newSlot; @@ -1336,7 +1337,8 @@ ldelete:; epqslot = EvalPlanQual(estate, resultRelInfo->ri_RangeTableIndex, &update_ctid, - update_xmax); + update_xmax, + estate->es_snapshot->curcid); if (!TupIsNull(epqslot)) { *tupleid = update_ctid; @@ -1476,7 +1478,8 @@ lreplace:; epqslot = EvalPlanQual(estate, resultRelInfo->ri_RangeTableIndex, &update_ctid, - update_xmax); + update_xmax, + estate->es_snapshot->curcid); if (!TupIsNull(epqslot)) { *tupleid = update_ctid; @@ -1626,6 +1629,7 @@ ExecConstraints(const char *caller, 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) @@ -1635,7 +1639,7 @@ ExecConstraints(const char *caller, ResultRelInfo *resultRelInfo, */ TupleTableSlot * EvalPlanQual(EState *estate, Index rti, - ItemPointer tid, TransactionId priorXmax) + ItemPointer tid, TransactionId priorXmax, CommandId curCid) { evalPlanQual *epq; EState *epqstate; @@ -1712,6 +1716,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. */ diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h index 734edc1ceda..5a98923627b 100644 --- a/src/include/executor/executor.h +++ b/src/include/executor/executor.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: executor.h,v 1.78.2.1 2005/08/26 20:07:17 tgl Exp $ + * $Id: executor.h,v 1.78.2.2 2006/01/12 21:49:41 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -55,7 +55,7 @@ extern void ExecutorEnd(QueryDesc *queryDesc, EState *estate); extern void ExecConstraints(const char *caller, 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