mirror of
https://github.com/postgres/postgres.git
synced 2025-11-21 00:42:43 +03:00
Allow locking updated tuples in tuple_update() and tuple_delete()
Currently, in read committed transaction isolation mode (default), we have the following sequence of actions when tuple_update()/tuple_delete() finds the tuple updated by the concurrent transaction. 1. Attempt to update/delete tuple with tuple_update()/tuple_delete(), which returns TM_Updated. 2. Lock tuple with tuple_lock(). 3. Re-evaluate plan qual (recheck if we still need to update/delete and calculate the new tuple for update). 4. Second attempt to update/delete tuple with tuple_update()/tuple_delete(). This attempt should be successful, since the tuple was previously locked. This commit eliminates step 2 by taking the lock during the first tuple_update()/tuple_delete() call. The heap table access method saves some effort by checking the updated tuple once instead of twice. Future undo-based table access methods, which will start from the latest row version, can immediately place a lock there. Also, this commit makes tuple_update()/tuple_delete() optionally save the old tuple into the dedicated slot. That saves efforts on re-fetching tuples in certain cases. The code in nodeModifyTable.c is simplified by removing the nested switch/case. Discussion: https://postgr.es/m/CAPpHfdua-YFw3XTprfutzGp28xXLigFtzNbuFY8yPhqeq6X5kg%40mail.gmail.com Reviewed-by: Aleksander Alekseev, Pavel Borisov, Vignesh C, Mason Sharp Reviewed-by: Andres Freund, Chris Travers
This commit is contained in:
@@ -45,6 +45,12 @@
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/rel.h"
|
||||
|
||||
static TM_Result heapam_tuple_lock(Relation relation, ItemPointer tid,
|
||||
Snapshot snapshot, TupleTableSlot *slot,
|
||||
CommandId cid, LockTupleMode mode,
|
||||
LockWaitPolicy wait_policy, uint8 flags,
|
||||
TM_FailureData *tmfd);
|
||||
|
||||
static void reform_and_rewrite_tuple(HeapTuple tuple,
|
||||
Relation OldHeap, Relation NewHeap,
|
||||
Datum *values, bool *isnull, RewriteState rwstate);
|
||||
@@ -300,23 +306,55 @@ heapam_tuple_complete_speculative(Relation relation, TupleTableSlot *slot,
|
||||
|
||||
static TM_Result
|
||||
heapam_tuple_delete(Relation relation, ItemPointer tid, CommandId cid,
|
||||
Snapshot snapshot, Snapshot crosscheck, bool wait,
|
||||
TM_FailureData *tmfd, bool changingPart)
|
||||
Snapshot snapshot, Snapshot crosscheck, int options,
|
||||
TM_FailureData *tmfd, bool changingPart,
|
||||
TupleTableSlot *oldSlot)
|
||||
{
|
||||
TM_Result result;
|
||||
|
||||
/*
|
||||
* Currently Deleting of index tuples are handled at vacuum, in case if
|
||||
* the storage itself is cleaning the dead tuples by itself, it is the
|
||||
* time to call the index tuple deletion also.
|
||||
*/
|
||||
return heap_delete(relation, tid, cid, crosscheck, wait, tmfd, changingPart);
|
||||
result = heap_delete(relation, tid, cid, crosscheck, options,
|
||||
tmfd, changingPart, oldSlot);
|
||||
|
||||
/*
|
||||
* If the tuple has been concurrently updated, then get the lock on it.
|
||||
* (Do only if caller asked for this by setting the
|
||||
* TABLE_MODIFY_LOCK_UPDATED option) With the lock held retry of the
|
||||
* delete should succeed even if there are more concurrent update
|
||||
* attempts.
|
||||
*/
|
||||
if (result == TM_Updated && (options & TABLE_MODIFY_LOCK_UPDATED))
|
||||
{
|
||||
/*
|
||||
* heapam_tuple_lock() will take advantage of tuple loaded into
|
||||
* oldSlot by heap_delete().
|
||||
*/
|
||||
result = heapam_tuple_lock(relation, tid, snapshot,
|
||||
oldSlot, cid, LockTupleExclusive,
|
||||
(options & TABLE_MODIFY_WAIT) ?
|
||||
LockWaitBlock :
|
||||
LockWaitSkip,
|
||||
TUPLE_LOCK_FLAG_FIND_LAST_VERSION,
|
||||
tmfd);
|
||||
|
||||
if (result == TM_Ok)
|
||||
return TM_Updated;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static TM_Result
|
||||
heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
|
||||
CommandId cid, Snapshot snapshot, Snapshot crosscheck,
|
||||
bool wait, TM_FailureData *tmfd,
|
||||
LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes)
|
||||
int options, TM_FailureData *tmfd,
|
||||
LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes,
|
||||
TupleTableSlot *oldSlot)
|
||||
{
|
||||
bool shouldFree = true;
|
||||
HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
|
||||
@@ -326,8 +364,8 @@ heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
|
||||
slot->tts_tableOid = RelationGetRelid(relation);
|
||||
tuple->t_tableOid = slot->tts_tableOid;
|
||||
|
||||
result = heap_update(relation, otid, tuple, cid, crosscheck, wait,
|
||||
tmfd, lockmode, update_indexes);
|
||||
result = heap_update(relation, otid, tuple, cid, crosscheck, options,
|
||||
tmfd, lockmode, update_indexes, oldSlot);
|
||||
ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
|
||||
|
||||
/*
|
||||
@@ -354,6 +392,31 @@ heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
|
||||
if (shouldFree)
|
||||
pfree(tuple);
|
||||
|
||||
/*
|
||||
* If the tuple has been concurrently updated, then get the lock on it.
|
||||
* (Do only if caller asked for this by setting the
|
||||
* TABLE_MODIFY_LOCK_UPDATED option) With the lock held retry of the
|
||||
* update should succeed even if there are more concurrent update
|
||||
* attempts.
|
||||
*/
|
||||
if (result == TM_Updated && (options & TABLE_MODIFY_LOCK_UPDATED))
|
||||
{
|
||||
/*
|
||||
* heapam_tuple_lock() will take advantage of tuple loaded into
|
||||
* oldSlot by heap_update().
|
||||
*/
|
||||
result = heapam_tuple_lock(relation, otid, snapshot,
|
||||
oldSlot, cid, *lockmode,
|
||||
(options & TABLE_MODIFY_WAIT) ?
|
||||
LockWaitBlock :
|
||||
LockWaitSkip,
|
||||
TUPLE_LOCK_FLAG_FIND_LAST_VERSION,
|
||||
tmfd);
|
||||
|
||||
if (result == TM_Ok)
|
||||
return TM_Updated;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -365,7 +428,6 @@ heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
|
||||
{
|
||||
BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
|
||||
TM_Result result;
|
||||
Buffer buffer;
|
||||
HeapTuple tuple = &bslot->base.tupdata;
|
||||
bool follow_updates;
|
||||
|
||||
@@ -375,9 +437,8 @@ heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
|
||||
Assert(TTS_IS_BUFFERTUPLE(slot));
|
||||
|
||||
tuple_lock_retry:
|
||||
tuple->t_self = *tid;
|
||||
result = heap_lock_tuple(relation, tuple, cid, mode, wait_policy,
|
||||
follow_updates, &buffer, tmfd);
|
||||
result = heap_lock_tuple(relation, tid, slot, cid, mode, wait_policy,
|
||||
follow_updates, tmfd);
|
||||
|
||||
if (result == TM_Updated &&
|
||||
(flags & TUPLE_LOCK_FLAG_FIND_LAST_VERSION))
|
||||
@@ -385,8 +446,6 @@ tuple_lock_retry:
|
||||
/* Should not encounter speculative tuple on recheck */
|
||||
Assert(!HeapTupleHeaderIsSpeculative(tuple->t_data));
|
||||
|
||||
ReleaseBuffer(buffer);
|
||||
|
||||
if (!ItemPointerEquals(&tmfd->ctid, &tuple->t_self))
|
||||
{
|
||||
SnapshotData SnapshotDirty;
|
||||
@@ -408,6 +467,8 @@ tuple_lock_retry:
|
||||
InitDirtySnapshot(SnapshotDirty);
|
||||
for (;;)
|
||||
{
|
||||
Buffer buffer = InvalidBuffer;
|
||||
|
||||
if (ItemPointerIndicatesMovedPartitions(tid))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
|
||||
@@ -502,7 +563,7 @@ tuple_lock_retry:
|
||||
/*
|
||||
* This is a live tuple, so try to lock it again.
|
||||
*/
|
||||
ReleaseBuffer(buffer);
|
||||
ExecStorePinnedBufferHeapTuple(tuple, slot, buffer);
|
||||
goto tuple_lock_retry;
|
||||
}
|
||||
|
||||
@@ -513,7 +574,7 @@ tuple_lock_retry:
|
||||
*/
|
||||
if (tuple->t_data == NULL)
|
||||
{
|
||||
Assert(!BufferIsValid(buffer));
|
||||
ReleaseBuffer(buffer);
|
||||
return TM_Deleted;
|
||||
}
|
||||
|
||||
@@ -566,9 +627,6 @@ tuple_lock_retry:
|
||||
slot->tts_tableOid = RelationGetRelid(relation);
|
||||
tuple->t_tableOid = slot->tts_tableOid;
|
||||
|
||||
/* store in slot, transferring existing pin */
|
||||
ExecStorePinnedBufferHeapTuple(tuple, slot, buffer);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user