Skip to content

Commit dfd0919

Browse files
committed
Fix concurrent locking of tuple update chain
If several sessions are concurrently locking a tuple update chain with nonconflicting lock modes using an old snapshot, and they all succeed, it may happen that some of them fail because of restarting the loop (due to a concurrent Xmax change) and getting an error in the subsequent pass while trying to obtain a tuple lock that they already have in some tuple version. This can only happen with very high concurrency (where a row is being both updated and FK-checked by multiple transactions concurrently), but it's been observed in the field and can have unpleasant consequences such as an FK check failing to see a tuple that definitely exists: ERROR: insert or update on table "child_table" violates foreign key constraint "fk_constraint_name" DETAIL: Key (keyid)=(123456) is not present in table "parent_table". (where the key is observably present in the table). Discussion: https://postgr.es/m/20170714210011.r25mrff4nxjhmf3g@alvherre.pgsql
1 parent 8ad35c7 commit dfd0919

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/backend/access/heap/heapam.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5101,8 +5101,10 @@ compute_new_xmax_infomask(TransactionId xmax, uint16 old_infomask,
51015101
* with the given xid, does the current transaction need to wait, fail, or can
51025102
* it continue if it wanted to acquire a lock of the given mode? "needwait"
51035103
* is set to true if waiting is necessary; if it can continue, then
5104-
* HeapTupleMayBeUpdated is returned. In case of a conflict, a different
5105-
* HeapTupleSatisfiesUpdate return code is returned.
5104+
* HeapTupleMayBeUpdated is returned. If the lock is already held by the
5105+
* current transaction, return HeapTupleSelfUpdated. In case of a conflict
5106+
* with another transaction, a different HeapTupleSatisfiesUpdate return code
5107+
* is returned.
51065108
*
51075109
* The held status is said to be hypothetical because it might correspond to a
51085110
* lock held by a single Xid, i.e. not a real MultiXactId; we express it this
@@ -5125,8 +5127,9 @@ test_lockmode_for_conflict(MultiXactStatus status, TransactionId xid,
51255127
if (TransactionIdIsCurrentTransactionId(xid))
51265128
{
51275129
/*
5128-
* Updated by our own transaction? Just return failure. This
5129-
* shouldn't normally happen.
5130+
* The tuple has already been locked by our own transaction. This is
5131+
* very rare but can happen if multiple transactions are trying to
5132+
* lock an ancient version of the same tuple.
51305133
*/
51315134
return HeapTupleSelfUpdated;
51325135
}
@@ -5295,6 +5298,22 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
52955298
members[i].xid,
52965299
mode, &needwait);
52975300

5301+
/*
5302+
* If the tuple was already locked by ourselves in a
5303+
* previous iteration of this (say heap_lock_tuple was
5304+
* forced to restart the locking loop because of a change
5305+
* in xmax), then we hold the lock already on this tuple
5306+
* version and we don't need to do anything; and this is
5307+
* not an error condition either. We just need to skip
5308+
* this tuple and continue locking the next version in the
5309+
* update chain.
5310+
*/
5311+
if (res == HeapTupleSelfUpdated)
5312+
{
5313+
pfree(members);
5314+
goto next;
5315+
}
5316+
52985317
if (needwait)
52995318
{
53005319
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
@@ -5357,6 +5376,19 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
53575376

53585377
res = test_lockmode_for_conflict(status, rawxmax, mode,
53595378
&needwait);
5379+
5380+
/*
5381+
* If the tuple was already locked by ourselves in a previous
5382+
* iteration of this (say heap_lock_tuple was forced to
5383+
* restart the locking loop because of a change in xmax), then
5384+
* we hold the lock already on this tuple version and we don't
5385+
* need to do anything; and this is not an error condition
5386+
* either. We just need to skip this tuple and continue
5387+
* locking the next version in the update chain.
5388+
*/
5389+
if (res == HeapTupleSelfUpdated)
5390+
goto next;
5391+
53605392
if (needwait)
53615393
{
53625394
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
@@ -5419,6 +5451,7 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
54195451

54205452
END_CRIT_SECTION();
54215453

5454+
next:
54225455
/* if we find the end of update chain, we're done. */
54235456
if (mytup.t_data->t_infomask & HEAP_XMAX_INVALID ||
54245457
ItemPointerEquals(&mytup.t_self, &mytup.t_data->t_ctid) ||

0 commit comments

Comments
 (0)