uint16 *new_infomask2);
static TransactionId MultiXactIdGetUpdateXid(TransactionId xmax,
uint16 t_infomask);
-static void MultiXactIdWait(MultiXactId multi, MultiXactStatus status,
- int *remaining, uint16 infomask);
-static bool ConditionalMultiXactIdWait(MultiXactId multi,
- MultiXactStatus status, int *remaining,
- uint16 infomask);
+static void MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask,
+ Relation rel, ItemPointer ctid, XLTW_Oper oper,
+ int *remaining);
+static bool ConditionalMultiXactIdWait(MultiXactId multi, MultiXactStatus status,
+ uint16 infomask, Relation rel, ItemPointer ctid,
+ XLTW_Oper oper, int *remaining);
static XLogRecPtr log_heap_new_cid(Relation relation, HeapTuple tup);
static HeapTuple ExtractReplicaIdentity(Relation rel, HeapTuple tup, bool key_modified,
bool *copy);
if (infomask & HEAP_XMAX_IS_MULTI)
{
/* wait for multixact */
- MultiXactIdWait((MultiXactId) xwait, MultiXactStatusUpdate,
- NULL, infomask);
+ MultiXactIdWait((MultiXactId) xwait, MultiXactStatusUpdate, infomask,
+ relation, &tp.t_data->t_ctid, XLTW_Delete,
+ NULL);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
/*
else
{
/* wait for regular transaction to end */
- XactLockTableWait(xwait);
+ XactLockTableWait(xwait, relation, &tp.t_data->t_ctid, XLTW_Delete);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
/*
int remain;
/* wait for multixact */
- MultiXactIdWait((MultiXactId) xwait, mxact_status, &remain,
- infomask);
+ MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask,
+ relation, &oldtup.t_data->t_ctid, XLTW_Update,
+ &remain);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
/*
else
{
/* wait for regular transaction to end */
- XactLockTableWait(xwait);
+ XactLockTableWait(xwait, relation, &oldtup.t_data->t_ctid,
+ XLTW_Update);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
/*
if (nowait)
{
if (!ConditionalMultiXactIdWait((MultiXactId) xwait,
- status, NULL, infomask))
+ status, infomask, relation,
+ &tuple->t_data->t_ctid,
+ XLTW_Lock, NULL))
ereport(ERROR,
(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
errmsg("could not obtain lock on row in relation \"%s\"",
RelationGetRelationName(relation))));
}
else
- MultiXactIdWait((MultiXactId) xwait, status, NULL, infomask);
+ MultiXactIdWait((MultiXactId) xwait, status, infomask,
+ relation, &tuple->t_data->t_ctid,
+ XLTW_Lock, NULL);
/* if there are updates, follow the update chain */
if (follow_updates &&
RelationGetRelationName(relation))));
}
else
- XactLockTableWait(xwait);
+ XactLockTableWait(xwait, relation, &tuple->t_data->t_ctid,
+ XLTW_Lock);
/* if there are updates, follow the update chain */
if (follow_updates &&
if (needwait)
{
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
- XactLockTableWait(members[i].xid);
+ XactLockTableWait(members[i].xid, rel,
+ &mytup.t_data->t_ctid,
+ XLTW_LockUpdated);
pfree(members);
goto l4;
}
if (needwait)
{
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
- XactLockTableWait(rawxmax);
+ XactLockTableWait(rawxmax, rel, &mytup.t_data->t_ctid,
+ XLTW_LockUpdated);
goto l4;
}
if (res != HeapTupleMayBeUpdated)
* Do_MultiXactIdWait
* Actual implementation for the two functions below.
*
+ * 'multi', 'status' and 'infomask' indicate what to sleep on (the status is
+ * needed to ensure we only sleep on conflicting members, and the infomask is
+ * used to optimize multixact access in case it's a lock-only multi); 'nowait'
+ * indicates whether to use conditional lock acquisition, to allow callers to
+ * fail if lock is unavailable. 'rel', 'ctid' and 'oper' are used to set up
+ * context information for error messages. 'remaining', if not NULL, receives
+ * the number of members that are still running, including any (non-aborted)
+ * subtransactions of our own transaction.
+ *
* We do this by sleeping on each member using XactLockTableWait. Any
* members that belong to the current backend are *not* waited for, however;
* this would not merely be useless but would lead to Assert failure inside
*/
static bool
Do_MultiXactIdWait(MultiXactId multi, MultiXactStatus status,
- int *remaining, uint16 infomask, bool nowait)
+ uint16 infomask, bool nowait,
+ Relation rel, ItemPointer ctid, XLTW_Oper oper,
+ int *remaining)
{
bool allow_old;
bool result = true;
/*
* This member conflicts with our multi, so we have to sleep (or
* return failure, if asked to avoid waiting.)
+ *
+ * Note that we don't set up an error context callback ourselves,
+ * but instead we pass the info down to XactLockTableWait. This
+ * might seem a bit wasteful because the context is set up and
+ * tore down for each member of the multixact, but in reality it
+ * should be barely noticeable, and it avoids duplicate code.
*/
if (nowait)
{
break;
}
else
- XactLockTableWait(memxid);
+ XactLockTableWait(memxid, rel, ctid, oper);
}
pfree(members);
*
* We return (in *remaining, if not NULL) the number of members that are still
* running, including any (non-aborted) subtransactions of our own transaction.
- *
*/
static void
-MultiXactIdWait(MultiXactId multi, MultiXactStatus status,
- int *remaining, uint16 infomask)
+MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask,
+ Relation rel, ItemPointer ctid, XLTW_Oper oper,
+ int *remaining)
{
- Do_MultiXactIdWait(multi, status, remaining, infomask, false);
+ (void) Do_MultiXactIdWait(multi, status, infomask, false,
+ rel, ctid, oper, remaining);
}
/*
*/
static bool
ConditionalMultiXactIdWait(MultiXactId multi, MultiXactStatus status,
- int *remaining, uint16 infomask)
+ uint16 infomask, Relation rel, ItemPointer ctid,
+ XLTW_Oper oper, int *remaining)
{
- return Do_MultiXactIdWait(multi, status, remaining, infomask, true);
+ return Do_MultiXactIdWait(multi, status, infomask, true,
+ rel, ctid, oper, remaining);
}
/*
#include "utils/inval.h"
+/*
+ * Struct to hold context info for transaction lock waits.
+ *
+ * 'oper' is the operation that needs to wait for the other transaction; 'rel'
+ * and 'ctid' specify the address of the tuple being waited for.
+ */
+typedef struct XactLockTableWaitInfo
+{
+ XLTW_Oper oper;
+ Relation rel;
+ ItemPointer ctid;
+} XactLockTableWaitInfo;
+
+static void XactLockTableWaitErrorCb(void *arg);
+
/*
* RelationInitLockInfo
* Initializes the lock information in a relation descriptor.
/*
* XactLockTableWait
*
- * Wait for the specified transaction to commit or abort.
+ * Wait for the specified transaction to commit or abort. If an operation
+ * is specified, an error context callback is set up. If 'oper' is passed as
+ * None, no error context callback is set up.
*
* Note that this does the right thing for subtransactions: if we wait on a
* subtransaction, we will exit as soon as it aborts or its top parent commits.
* and if so wait for its parent.
*/
void
-XactLockTableWait(TransactionId xid)
+XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid,
+ XLTW_Oper oper)
{
LOCKTAG tag;
+ XactLockTableWaitInfo info;
+ ErrorContextCallback callback;
+
+ /*
+ * If an operation is specified, set up our verbose error context
+ * callback.
+ */
+ if (oper != XLTW_None)
+ {
+ Assert(RelationIsValid(rel));
+ Assert(ItemPointerIsValid(ctid));
+
+ info.rel = rel;
+ info.ctid = ctid;
+ info.oper = oper;
+
+ callback.callback = XactLockTableWaitErrorCb;
+ callback.arg = &info;
+ callback.previous = error_context_stack;
+ error_context_stack = &callback;
+ }
for (;;)
{
break;
xid = SubTransGetParent(xid);
}
+
+ if (oper != XLTW_None)
+ error_context_stack = callback.previous;
}
/*
return true;
}
+/*
+ * XactLockTableWaitErrorContextCb
+ * Error context callback for transaction lock waits.
+ */
+static void
+XactLockTableWaitErrorCb(void *arg)
+{
+ XactLockTableWaitInfo *info = (XactLockTableWaitInfo *) arg;
+
+ /*
+ * We would like to print schema name too, but that would require a
+ * syscache lookup.
+ */
+ if (info->oper != XLTW_None &&
+ ItemPointerIsValid(info->ctid) && RelationIsValid(info->rel))
+ {
+ const char *cxt;
+
+ switch (info->oper)
+ {
+ case XLTW_Update:
+ cxt = gettext_noop("while updating tuple (%u,%u) in relation \"%s\"");
+ break;
+ case XLTW_Delete:
+ cxt = gettext_noop("while deleting tuple (%u,%u) in relation \"%s\"");
+ break;
+ case XLTW_Lock:
+ cxt = gettext_noop("while locking tuple (%u,%u) in relation \"%s\"");
+ break;
+ case XLTW_LockUpdated:
+ cxt = gettext_noop("while locking updated version (%u,%u) of tuple in relation \"%s\"");
+ break;
+ case XLTW_InsertIndex:
+ cxt = gettext_noop("while inserting index tuple (%u,%u) in relation \"%s\"");
+ break;
+ case XLTW_InsertIndexUnique:
+ cxt = gettext_noop("while checking uniqueness of tuple (%u,%u) in relation \"%s\"");
+ break;
+ case XLTW_FetchUpdated:
+ cxt = gettext_noop("while rechecking updated tuple (%u,%u) in relation \"%s\"");
+ break;
+ case XLTW_RecheckExclusionConstr:
+ cxt = gettext_noop("while checking exclusion constraint on tuple (%u,%u) in relation \"%s\"");
+ break;
+
+ default:
+ return;
+ }
+
+ errcontext(cxt,
+ ItemPointerGetBlockNumber(info->ctid),
+ ItemPointerGetOffsetNumber(info->ctid),
+ RelationGetRelationName(info->rel));
+ }
+}
+
/*
* WaitForLockersMultiple
* Wait until no transaction holds locks that conflict with the given