Skip to content

Commit 533e9c6

Browse files
committed
Avoid serializability errors when locking a tuple with a committed update
When key-share locking a tuple that has been not-key-updated, and the update is a committed transaction, in some cases we raised serializability errors: ERROR: could not serialize access due to concurrent update Because the key-share doesn't conflict with the update, the error is unnecessary and inconsistent with the case that the update hasn't committed yet. This causes problems for some usage patterns, even if it can be claimed that it's sufficient to retry the aborted transaction: given a steady stream of updating transactions and a long locking transaction, the long transaction can be starved indefinitely despite multiple retries. To fix, we recognize that HeapTupleSatisfiesUpdate can return HeapTupleUpdated when an updating transaction has committed, and that we need to deal with that case exactly as if it were a non-committed update: verify whether the two operations conflict, and if not, carry on normally. If they do conflict, however, there is a difference: in the HeapTupleBeingUpdated case we can just sleep until the concurrent transaction is gone, while in the HeapTupleUpdated case this is not possible and we must raise an error instead. Per trouble report from Olivier Dony. In addition to a couple of test cases that verify the changed behavior, I added a test case to verify the behavior that remains unchanged, namely that errors are raised when a update that modifies the key is used. That must still generate serializability errors. One pre-existing test case changes behavior; per discussion, the new behavior is actually the desired one. Discussion: https://www.postgresql.org/message-id/[email protected] https://www.postgresql.org/message-id/[email protected] Backpatch to 9.3, where the problem appeared.
1 parent 00f304c commit 533e9c6

9 files changed

+1401
-4
lines changed

src/backend/access/heap/heapam.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -4540,7 +4540,7 @@ heap_lock_tuple(Relation relation, HeapTuple tuple,
45404540
*/
45414541
return HeapTupleInvisible;
45424542
}
4543-
else if (result == HeapTupleBeingUpdated)
4543+
else if (result == HeapTupleBeingUpdated || result == HeapTupleUpdated)
45444544
{
45454545
TransactionId xwait;
45464546
uint16 infomask;
@@ -4800,12 +4800,22 @@ heap_lock_tuple(Relation relation, HeapTuple tuple,
48004800
}
48014801

48024802
/*
4803+
* Time to sleep on the other transaction/multixact, if necessary.
4804+
*
4805+
* If the other transaction is an update that's already committed,
4806+
* then sleeping cannot possibly do any good: if we're required to
4807+
* sleep, get out to raise an error instead.
4808+
*
48034809
* By here, we either have already acquired the buffer exclusive lock,
48044810
* or we must wait for the locking transaction or multixact; so below
48054811
* we ensure that we grab buffer lock after the sleep.
48064812
*/
4807-
4808-
if (require_sleep)
4813+
if (require_sleep && result == HeapTupleUpdated)
4814+
{
4815+
LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
4816+
goto failed;
4817+
}
4818+
else if (require_sleep)
48094819
{
48104820
/*
48114821
* Acquire tuple lock to establish our priority for the tuple, or

0 commit comments

Comments
 (0)