1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-27 23:21:58 +03:00

Revert: Allow locking updated tuples in tuple_update() and tuple_delete()

This commit reverts 87985cc925 and 818861eb57 per review by Andres Freund.

Discussion: https://postgr.es/m/20240410165236.rwyrny7ihi4ddxw4%40awork3.anarazel.de
This commit is contained in:
Alexander Korotkov
2024-04-11 15:51:35 +03:00
parent da841aa4dc
commit 193e6d18e5
9 changed files with 346 additions and 501 deletions

View File

@ -2665,11 +2665,10 @@ xmax_infomask_changed(uint16 new_infomask, uint16 old_infomask)
} }
/* /*
* heap_delete - delete a tuple, optionally fetching it into a slot * heap_delete - delete a tuple
* *
* See table_tuple_delete() for an explanation of the parameters, except that * See table_tuple_delete() for an explanation of the parameters, except that
* this routine directly takes a tuple rather than a slot. Also, we don't * this routine directly takes a tuple rather than a slot.
* place a lock on the tuple in this function, just fetch the existing version.
* *
* In the failure cases, the routine fills *tmfd with the tuple's t_ctid, * In the failure cases, the routine fills *tmfd with the tuple's t_ctid,
* t_xmax (resolving a possible MultiXact, if necessary), and t_cmax (the last * t_xmax (resolving a possible MultiXact, if necessary), and t_cmax (the last
@ -2678,9 +2677,8 @@ xmax_infomask_changed(uint16 new_infomask, uint16 old_infomask)
*/ */
TM_Result TM_Result
heap_delete(Relation relation, ItemPointer tid, heap_delete(Relation relation, ItemPointer tid,
CommandId cid, Snapshot crosscheck, int options, CommandId cid, Snapshot crosscheck, bool wait,
TM_FailureData *tmfd, bool changingPart, TM_FailureData *tmfd, bool changingPart)
TupleTableSlot *oldSlot)
{ {
TM_Result result; TM_Result result;
TransactionId xid = GetCurrentTransactionId(); TransactionId xid = GetCurrentTransactionId();
@ -2758,7 +2756,7 @@ l1:
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("attempted to delete invisible tuple"))); errmsg("attempted to delete invisible tuple")));
} }
else if (result == TM_BeingModified && (options & TABLE_MODIFY_WAIT)) else if (result == TM_BeingModified && wait)
{ {
TransactionId xwait; TransactionId xwait;
uint16 infomask; uint16 infomask;
@ -2899,30 +2897,7 @@ l1:
tmfd->cmax = HeapTupleHeaderGetCmax(tp.t_data); tmfd->cmax = HeapTupleHeaderGetCmax(tp.t_data);
else else
tmfd->cmax = InvalidCommandId; tmfd->cmax = InvalidCommandId;
/*
* If we're asked to lock the updated tuple, we just fetch the
* existing tuple. That let's the caller save some resources on
* placing the lock.
*/
if (result == TM_Updated &&
(options & TABLE_MODIFY_LOCK_UPDATED))
{
BufferHeapTupleTableSlot *bslot;
Assert(TTS_IS_BUFFERTUPLE(oldSlot));
bslot = (BufferHeapTupleTableSlot *) oldSlot;
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
bslot->base.tupdata = tp;
ExecStorePinnedBufferHeapTuple(&bslot->base.tupdata,
oldSlot,
buffer);
}
else
{
UnlockReleaseBuffer(buffer); UnlockReleaseBuffer(buffer);
}
if (have_tuple_lock) if (have_tuple_lock)
UnlockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive); UnlockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive);
if (vmbuffer != InvalidBuffer) if (vmbuffer != InvalidBuffer)
@ -3096,24 +3071,8 @@ l1:
*/ */
CacheInvalidateHeapTuple(relation, &tp, NULL); CacheInvalidateHeapTuple(relation, &tp, NULL);
/* Fetch the old tuple version if we're asked for that. */
if (options & TABLE_MODIFY_FETCH_OLD_TUPLE)
{
BufferHeapTupleTableSlot *bslot;
Assert(TTS_IS_BUFFERTUPLE(oldSlot));
bslot = (BufferHeapTupleTableSlot *) oldSlot;
bslot->base.tupdata = tp;
ExecStorePinnedBufferHeapTuple(&bslot->base.tupdata,
oldSlot,
buffer);
}
else
{
/* Now we can release the buffer */ /* Now we can release the buffer */
ReleaseBuffer(buffer); ReleaseBuffer(buffer);
}
/* /*
* Release the lmgr tuple lock, if we had it. * Release the lmgr tuple lock, if we had it.
@ -3145,8 +3104,8 @@ simple_heap_delete(Relation relation, ItemPointer tid)
result = heap_delete(relation, tid, result = heap_delete(relation, tid,
GetCurrentCommandId(true), InvalidSnapshot, GetCurrentCommandId(true), InvalidSnapshot,
TABLE_MODIFY_WAIT /* wait for commit */ , true /* wait for commit */ ,
&tmfd, false /* changingPart */ , NULL); &tmfd, false /* changingPart */ );
switch (result) switch (result)
{ {
case TM_SelfModified: case TM_SelfModified:
@ -3173,11 +3132,10 @@ simple_heap_delete(Relation relation, ItemPointer tid)
} }
/* /*
* heap_update - replace a tuple, optionally fetching it into a slot * heap_update - replace a tuple
* *
* See table_tuple_update() for an explanation of the parameters, except that * See table_tuple_update() for an explanation of the parameters, except that
* this routine directly takes a tuple rather than a slot. Also, we don't * this routine directly takes a tuple rather than a slot.
* place a lock on the tuple in this function, just fetch the existing version.
* *
* In the failure cases, the routine fills *tmfd with the tuple's t_ctid, * In the failure cases, the routine fills *tmfd with the tuple's t_ctid,
* t_xmax (resolving a possible MultiXact, if necessary), and t_cmax (the last * t_xmax (resolving a possible MultiXact, if necessary), and t_cmax (the last
@ -3186,9 +3144,9 @@ simple_heap_delete(Relation relation, ItemPointer tid)
*/ */
TM_Result TM_Result
heap_update(Relation relation, ItemPointer otid, HeapTuple newtup, heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
CommandId cid, Snapshot crosscheck, int options, CommandId cid, Snapshot crosscheck, bool wait,
TM_FailureData *tmfd, LockTupleMode *lockmode, TM_FailureData *tmfd, LockTupleMode *lockmode,
TU_UpdateIndexes *update_indexes, TupleTableSlot *oldSlot) TU_UpdateIndexes *update_indexes)
{ {
TM_Result result; TM_Result result;
TransactionId xid = GetCurrentTransactionId(); TransactionId xid = GetCurrentTransactionId();
@ -3365,7 +3323,7 @@ l2:
result = HeapTupleSatisfiesUpdate(&oldtup, cid, buffer); result = HeapTupleSatisfiesUpdate(&oldtup, cid, buffer);
/* see below about the "no wait" case */ /* see below about the "no wait" case */
Assert(result != TM_BeingModified || (options & TABLE_MODIFY_WAIT)); Assert(result != TM_BeingModified || wait);
if (result == TM_Invisible) if (result == TM_Invisible)
{ {
@ -3374,7 +3332,7 @@ l2:
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("attempted to update invisible tuple"))); errmsg("attempted to update invisible tuple")));
} }
else if (result == TM_BeingModified && (options & TABLE_MODIFY_WAIT)) else if (result == TM_BeingModified && wait)
{ {
TransactionId xwait; TransactionId xwait;
uint16 infomask; uint16 infomask;
@ -3578,30 +3536,7 @@ l2:
tmfd->cmax = HeapTupleHeaderGetCmax(oldtup.t_data); tmfd->cmax = HeapTupleHeaderGetCmax(oldtup.t_data);
else else
tmfd->cmax = InvalidCommandId; tmfd->cmax = InvalidCommandId;
/*
* If we're asked to lock the updated tuple, we just fetch the
* existing tuple. That lets the caller save some resources on
* placing the lock.
*/
if (result == TM_Updated &&
(options & TABLE_MODIFY_LOCK_UPDATED))
{
BufferHeapTupleTableSlot *bslot;
Assert(TTS_IS_BUFFERTUPLE(oldSlot));
bslot = (BufferHeapTupleTableSlot *) oldSlot;
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
bslot->base.tupdata = oldtup;
ExecStorePinnedBufferHeapTuple(&bslot->base.tupdata,
oldSlot,
buffer);
}
else
{
UnlockReleaseBuffer(buffer); UnlockReleaseBuffer(buffer);
}
if (have_tuple_lock) if (have_tuple_lock)
UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode); UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
if (vmbuffer != InvalidBuffer) if (vmbuffer != InvalidBuffer)
@ -4080,26 +4015,7 @@ l2:
/* Now we can release the buffer(s) */ /* Now we can release the buffer(s) */
if (newbuf != buffer) if (newbuf != buffer)
ReleaseBuffer(newbuf); ReleaseBuffer(newbuf);
/* Fetch the old tuple version if we're asked for that. */
if (options & TABLE_MODIFY_FETCH_OLD_TUPLE)
{
BufferHeapTupleTableSlot *bslot;
Assert(TTS_IS_BUFFERTUPLE(oldSlot));
bslot = (BufferHeapTupleTableSlot *) oldSlot;
bslot->base.tupdata = oldtup;
ExecStorePinnedBufferHeapTuple(&bslot->base.tupdata,
oldSlot,
buffer);
}
else
{
/* Now we can release the buffer */
ReleaseBuffer(buffer); ReleaseBuffer(buffer);
}
if (BufferIsValid(vmbuffer_new)) if (BufferIsValid(vmbuffer_new))
ReleaseBuffer(vmbuffer_new); ReleaseBuffer(vmbuffer_new);
if (BufferIsValid(vmbuffer)) if (BufferIsValid(vmbuffer))
@ -4307,8 +4223,8 @@ simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup,
result = heap_update(relation, otid, tup, result = heap_update(relation, otid, tup,
GetCurrentCommandId(true), InvalidSnapshot, GetCurrentCommandId(true), InvalidSnapshot,
TABLE_MODIFY_WAIT /* wait for commit */ , true /* wait for commit */ ,
&tmfd, &lockmode, update_indexes, NULL); &tmfd, &lockmode, update_indexes);
switch (result) switch (result)
{ {
case TM_SelfModified: case TM_SelfModified:
@ -4371,14 +4287,12 @@ get_mxact_status_for_lock(LockTupleMode mode, bool is_update)
* tuples. * tuples.
* *
* Output parameters: * Output parameters:
* *slot: BufferHeapTupleTableSlot filled with tuple * *tuple: all fields filled in
* *buffer: set to buffer holding tuple (pinned but not locked at exit)
* *tmfd: filled in failure cases (see below) * *tmfd: filled in failure cases (see below)
* *
* Function results are the same as the ones for table_tuple_lock(). * Function results are the same as the ones for table_tuple_lock().
* *
* If *slot already contains the target tuple, it takes advantage on that by
* skipping the ReadBuffer() call.
*
* In the failure cases other than TM_Invisible, the routine fills * In the failure cases other than TM_Invisible, the routine fills
* *tmfd with the tuple's t_ctid, t_xmax (resolving a possible MultiXact, * *tmfd with the tuple's t_ctid, t_xmax (resolving a possible MultiXact,
* if necessary), and t_cmax (the last only for TM_SelfModified, * if necessary), and t_cmax (the last only for TM_SelfModified,
@ -4389,14 +4303,15 @@ get_mxact_status_for_lock(LockTupleMode mode, bool is_update)
* See README.tuplock for a thorough explanation of this mechanism. * See README.tuplock for a thorough explanation of this mechanism.
*/ */
TM_Result TM_Result
heap_lock_tuple(Relation relation, ItemPointer tid, TupleTableSlot *slot, heap_lock_tuple(Relation relation, HeapTuple tuple,
CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy,
bool follow_updates, TM_FailureData *tmfd) bool follow_updates,
Buffer *buffer, TM_FailureData *tmfd)
{ {
TM_Result result; TM_Result result;
ItemPointer tid = &(tuple->t_self);
ItemId lp; ItemId lp;
Page page; Page page;
Buffer buffer;
Buffer vmbuffer = InvalidBuffer; Buffer vmbuffer = InvalidBuffer;
BlockNumber block; BlockNumber block;
TransactionId xid, TransactionId xid,
@ -4408,24 +4323,8 @@ heap_lock_tuple(Relation relation, ItemPointer tid, TupleTableSlot *slot,
bool skip_tuple_lock = false; bool skip_tuple_lock = false;
bool have_tuple_lock = false; bool have_tuple_lock = false;
bool cleared_all_frozen = false; bool cleared_all_frozen = false;
BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
HeapTuple tuple = &bslot->base.tupdata;
Assert(TTS_IS_BUFFERTUPLE(slot)); *buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
/* Take advantage if slot already contains the relevant tuple */
if (!TTS_EMPTY(slot) &&
slot->tts_tableOid == relation->rd_id &&
ItemPointerCompare(&slot->tts_tid, tid) == 0 &&
BufferIsValid(bslot->buffer))
{
buffer = bslot->buffer;
IncrBufferRefCount(buffer);
}
else
{
buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
}
block = ItemPointerGetBlockNumber(tid); block = ItemPointerGetBlockNumber(tid);
/* /*
@ -4434,22 +4333,21 @@ heap_lock_tuple(Relation relation, ItemPointer tid, TupleTableSlot *slot,
* in the middle of changing this, so we'll need to recheck after we have * in the middle of changing this, so we'll need to recheck after we have
* the lock. * the lock.
*/ */
if (PageIsAllVisible(BufferGetPage(buffer))) if (PageIsAllVisible(BufferGetPage(*buffer)))
visibilitymap_pin(relation, block, &vmbuffer); visibilitymap_pin(relation, block, &vmbuffer);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
page = BufferGetPage(buffer); page = BufferGetPage(*buffer);
lp = PageGetItemId(page, ItemPointerGetOffsetNumber(tid)); lp = PageGetItemId(page, ItemPointerGetOffsetNumber(tid));
Assert(ItemIdIsNormal(lp)); Assert(ItemIdIsNormal(lp));
tuple->t_self = *tid;
tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp); tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
tuple->t_len = ItemIdGetLength(lp); tuple->t_len = ItemIdGetLength(lp);
tuple->t_tableOid = RelationGetRelid(relation); tuple->t_tableOid = RelationGetRelid(relation);
l3: l3:
result = HeapTupleSatisfiesUpdate(tuple, cid, buffer); result = HeapTupleSatisfiesUpdate(tuple, cid, *buffer);
if (result == TM_Invisible) if (result == TM_Invisible)
{ {
@ -4478,7 +4376,7 @@ l3:
infomask2 = tuple->t_data->t_infomask2; infomask2 = tuple->t_data->t_infomask2;
ItemPointerCopy(&tuple->t_data->t_ctid, &t_ctid); ItemPointerCopy(&tuple->t_data->t_ctid, &t_ctid);
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
/* /*
* If any subtransaction of the current top transaction already holds * If any subtransaction of the current top transaction already holds
@ -4630,12 +4528,12 @@ l3:
{ {
result = res; result = res;
/* recovery code expects to have buffer lock held */ /* recovery code expects to have buffer lock held */
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
goto failed; goto failed;
} }
} }
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
/* /*
* Make sure it's still an appropriate lock, else start over. * Make sure it's still an appropriate lock, else start over.
@ -4670,7 +4568,7 @@ l3:
if (HEAP_XMAX_IS_LOCKED_ONLY(infomask) && if (HEAP_XMAX_IS_LOCKED_ONLY(infomask) &&
!HEAP_XMAX_IS_EXCL_LOCKED(infomask)) !HEAP_XMAX_IS_EXCL_LOCKED(infomask))
{ {
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
/* /*
* Make sure it's still an appropriate lock, else start over. * Make sure it's still an appropriate lock, else start over.
@ -4698,7 +4596,7 @@ l3:
* No conflict, but if the xmax changed under us in the * No conflict, but if the xmax changed under us in the
* meantime, start over. * meantime, start over.
*/ */
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) ||
!TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
xwait)) xwait))
@ -4710,7 +4608,7 @@ l3:
} }
else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask)) else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
{ {
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
/* if the xmax changed in the meantime, start over */ /* if the xmax changed in the meantime, start over */
if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) ||
@ -4738,7 +4636,7 @@ l3:
TransactionIdIsCurrentTransactionId(xwait)) TransactionIdIsCurrentTransactionId(xwait))
{ {
/* ... but if the xmax changed in the meantime, start over */ /* ... but if the xmax changed in the meantime, start over */
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) ||
!TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
xwait)) xwait))
@ -4760,7 +4658,7 @@ l3:
*/ */
if (require_sleep && (result == TM_Updated || result == TM_Deleted)) if (require_sleep && (result == TM_Updated || result == TM_Deleted))
{ {
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
goto failed; goto failed;
} }
else if (require_sleep) else if (require_sleep)
@ -4785,7 +4683,7 @@ l3:
*/ */
result = TM_WouldBlock; result = TM_WouldBlock;
/* recovery code expects to have buffer lock held */ /* recovery code expects to have buffer lock held */
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
goto failed; goto failed;
} }
@ -4811,7 +4709,7 @@ l3:
{ {
result = TM_WouldBlock; result = TM_WouldBlock;
/* recovery code expects to have buffer lock held */ /* recovery code expects to have buffer lock held */
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
goto failed; goto failed;
} }
break; break;
@ -4851,7 +4749,7 @@ l3:
{ {
result = TM_WouldBlock; result = TM_WouldBlock;
/* recovery code expects to have buffer lock held */ /* recovery code expects to have buffer lock held */
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
goto failed; goto failed;
} }
break; break;
@ -4877,12 +4775,12 @@ l3:
{ {
result = res; result = res;
/* recovery code expects to have buffer lock held */ /* recovery code expects to have buffer lock held */
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
goto failed; goto failed;
} }
} }
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
/* /*
* xwait is done, but if xwait had just locked the tuple then some * xwait is done, but if xwait had just locked the tuple then some
@ -4904,7 +4802,7 @@ l3:
* don't check for this in the multixact case, because some * don't check for this in the multixact case, because some
* locker transactions might still be running. * locker transactions might still be running.
*/ */
UpdateXmaxHintBits(tuple->t_data, buffer, xwait); UpdateXmaxHintBits(tuple->t_data, *buffer, xwait);
} }
} }
@ -4963,9 +4861,9 @@ failed:
*/ */
if (vmbuffer == InvalidBuffer && PageIsAllVisible(page)) if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
{ {
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
visibilitymap_pin(relation, block, &vmbuffer); visibilitymap_pin(relation, block, &vmbuffer);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
goto l3; goto l3;
} }
@ -5028,7 +4926,7 @@ failed:
cleared_all_frozen = true; cleared_all_frozen = true;
MarkBufferDirty(buffer); MarkBufferDirty(*buffer);
/* /*
* XLOG stuff. You might think that we don't need an XLOG record because * XLOG stuff. You might think that we don't need an XLOG record because
@ -5048,7 +4946,7 @@ failed:
XLogRecPtr recptr; XLogRecPtr recptr;
XLogBeginInsert(); XLogBeginInsert();
XLogRegisterBuffer(0, buffer, REGBUF_STANDARD); XLogRegisterBuffer(0, *buffer, REGBUF_STANDARD);
xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self); xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self);
xlrec.xmax = xid; xlrec.xmax = xid;
@ -5069,7 +4967,7 @@ failed:
result = TM_Ok; result = TM_Ok;
out_locked: out_locked:
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
out_unlocked: out_unlocked:
if (BufferIsValid(vmbuffer)) if (BufferIsValid(vmbuffer))
@ -5087,9 +4985,6 @@ out_unlocked:
if (have_tuple_lock) if (have_tuple_lock)
UnlockTupleTuplock(relation, tid, mode); UnlockTupleTuplock(relation, tid, mode);
/* Put the target tuple to the slot */
ExecStorePinnedBufferHeapTuple(tuple, slot, buffer);
return result; return result;
} }

View File

@ -46,11 +46,6 @@
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/rel.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, static void reform_and_rewrite_tuple(HeapTuple tuple,
Relation OldHeap, Relation NewHeap, Relation OldHeap, Relation NewHeap,
Datum *values, bool *isnull, RewriteState rwstate); Datum *values, bool *isnull, RewriteState rwstate);
@ -306,55 +301,23 @@ heapam_tuple_complete_speculative(Relation relation, TupleTableSlot *slot,
static TM_Result static TM_Result
heapam_tuple_delete(Relation relation, ItemPointer tid, CommandId cid, heapam_tuple_delete(Relation relation, ItemPointer tid, CommandId cid,
Snapshot snapshot, Snapshot crosscheck, int options, Snapshot snapshot, Snapshot crosscheck, bool wait,
TM_FailureData *tmfd, bool changingPart, TM_FailureData *tmfd, bool changingPart)
TupleTableSlot *oldSlot)
{ {
TM_Result result;
/* /*
* Currently Deleting of index tuples are handled at vacuum, in case if * 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 * the storage itself is cleaning the dead tuples by itself, it is the
* time to call the index tuple deletion also. * time to call the index tuple deletion also.
*/ */
result = heap_delete(relation, tid, cid, crosscheck, options, return heap_delete(relation, tid, cid, crosscheck, wait, tmfd, changingPart);
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 static TM_Result
heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot, heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
CommandId cid, Snapshot snapshot, Snapshot crosscheck, CommandId cid, Snapshot snapshot, Snapshot crosscheck,
int options, TM_FailureData *tmfd, bool wait, TM_FailureData *tmfd,
LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes, LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes)
TupleTableSlot *oldSlot)
{ {
bool shouldFree = true; bool shouldFree = true;
HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree); HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
@ -364,8 +327,8 @@ heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
slot->tts_tableOid = RelationGetRelid(relation); slot->tts_tableOid = RelationGetRelid(relation);
tuple->t_tableOid = slot->tts_tableOid; tuple->t_tableOid = slot->tts_tableOid;
result = heap_update(relation, otid, tuple, cid, crosscheck, options, result = heap_update(relation, otid, tuple, cid, crosscheck, wait,
tmfd, lockmode, update_indexes, oldSlot); tmfd, lockmode, update_indexes);
ItemPointerCopy(&tuple->t_self, &slot->tts_tid); ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
/* /*
@ -392,31 +355,6 @@ heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
if (shouldFree) if (shouldFree)
pfree(tuple); 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; return result;
} }
@ -428,6 +366,7 @@ heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
{ {
BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot; BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
TM_Result result; TM_Result result;
Buffer buffer;
HeapTuple tuple = &bslot->base.tupdata; HeapTuple tuple = &bslot->base.tupdata;
bool follow_updates; bool follow_updates;
@ -437,8 +376,9 @@ heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
Assert(TTS_IS_BUFFERTUPLE(slot)); Assert(TTS_IS_BUFFERTUPLE(slot));
tuple_lock_retry: tuple_lock_retry:
result = heap_lock_tuple(relation, tid, slot, cid, mode, wait_policy, tuple->t_self = *tid;
follow_updates, tmfd); result = heap_lock_tuple(relation, tuple, cid, mode, wait_policy,
follow_updates, &buffer, tmfd);
if (result == TM_Updated && if (result == TM_Updated &&
(flags & TUPLE_LOCK_FLAG_FIND_LAST_VERSION)) (flags & TUPLE_LOCK_FLAG_FIND_LAST_VERSION))
@ -446,6 +386,8 @@ tuple_lock_retry:
/* Should not encounter speculative tuple on recheck */ /* Should not encounter speculative tuple on recheck */
Assert(!HeapTupleHeaderIsSpeculative(tuple->t_data)); Assert(!HeapTupleHeaderIsSpeculative(tuple->t_data));
ReleaseBuffer(buffer);
if (!ItemPointerEquals(&tmfd->ctid, &tuple->t_self)) if (!ItemPointerEquals(&tmfd->ctid, &tuple->t_self))
{ {
SnapshotData SnapshotDirty; SnapshotData SnapshotDirty;
@ -467,8 +409,6 @@ tuple_lock_retry:
InitDirtySnapshot(SnapshotDirty); InitDirtySnapshot(SnapshotDirty);
for (;;) for (;;)
{ {
Buffer buffer = InvalidBuffer;
if (ItemPointerIndicatesMovedPartitions(tid)) if (ItemPointerIndicatesMovedPartitions(tid))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE), (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
@ -563,7 +503,7 @@ tuple_lock_retry:
/* /*
* This is a live tuple, so try to lock it again. * This is a live tuple, so try to lock it again.
*/ */
ExecStorePinnedBufferHeapTuple(tuple, slot, buffer); ReleaseBuffer(buffer);
goto tuple_lock_retry; goto tuple_lock_retry;
} }
@ -574,7 +514,7 @@ tuple_lock_retry:
*/ */
if (tuple->t_data == NULL) if (tuple->t_data == NULL)
{ {
ReleaseBuffer(buffer); Assert(!BufferIsValid(buffer));
return TM_Deleted; return TM_Deleted;
} }
@ -627,6 +567,9 @@ tuple_lock_retry:
slot->tts_tableOid = RelationGetRelid(relation); slot->tts_tableOid = RelationGetRelid(relation);
tuple->t_tableOid = slot->tts_tableOid; tuple->t_tableOid = slot->tts_tableOid;
/* store in slot, transferring existing pin */
ExecStorePinnedBufferHeapTuple(tuple, slot, buffer);
return result; return result;
} }

View File

@ -287,23 +287,16 @@ simple_table_tuple_insert(Relation rel, TupleTableSlot *slot)
* via ereport(). * via ereport().
*/ */
void void
simple_table_tuple_delete(Relation rel, ItemPointer tid, Snapshot snapshot, simple_table_tuple_delete(Relation rel, ItemPointer tid, Snapshot snapshot)
TupleTableSlot *oldSlot)
{ {
TM_Result result; TM_Result result;
TM_FailureData tmfd; TM_FailureData tmfd;
int options = TABLE_MODIFY_WAIT; /* wait for commit */
/* Fetch old tuple if the relevant slot is provided */
if (oldSlot)
options |= TABLE_MODIFY_FETCH_OLD_TUPLE;
result = table_tuple_delete(rel, tid, result = table_tuple_delete(rel, tid,
GetCurrentCommandId(true), GetCurrentCommandId(true),
snapshot, InvalidSnapshot, snapshot, InvalidSnapshot,
options, true /* wait for commit */ ,
&tmfd, false /* changingPart */ , &tmfd, false /* changingPart */ );
oldSlot);
switch (result) switch (result)
{ {
@ -342,24 +335,17 @@ void
simple_table_tuple_update(Relation rel, ItemPointer otid, simple_table_tuple_update(Relation rel, ItemPointer otid,
TupleTableSlot *slot, TupleTableSlot *slot,
Snapshot snapshot, Snapshot snapshot,
TU_UpdateIndexes *update_indexes, TU_UpdateIndexes *update_indexes)
TupleTableSlot *oldSlot)
{ {
TM_Result result; TM_Result result;
TM_FailureData tmfd; TM_FailureData tmfd;
LockTupleMode lockmode; LockTupleMode lockmode;
int options = TABLE_MODIFY_WAIT; /* wait for commit */
/* Fetch old tuple if the relevant slot is provided */
if (oldSlot)
options |= TABLE_MODIFY_FETCH_OLD_TUPLE;
result = table_tuple_update(rel, otid, slot, result = table_tuple_update(rel, otid, slot,
GetCurrentCommandId(true), GetCurrentCommandId(true),
snapshot, InvalidSnapshot, snapshot, InvalidSnapshot,
options, true /* wait for commit */ ,
&tmfd, &lockmode, update_indexes, &tmfd, &lockmode, update_indexes);
oldSlot);
switch (result) switch (result)
{ {

View File

@ -2773,8 +2773,8 @@ ExecBRDeleteTriggers(EState *estate, EPQState *epqstate,
void void
ExecARDeleteTriggers(EState *estate, ExecARDeleteTriggers(EState *estate,
ResultRelInfo *relinfo, ResultRelInfo *relinfo,
ItemPointer tupleid,
HeapTuple fdw_trigtuple, HeapTuple fdw_trigtuple,
TupleTableSlot *slot,
TransitionCaptureState *transition_capture, TransitionCaptureState *transition_capture,
bool is_crosspart_update) bool is_crosspart_update)
{ {
@ -2783,11 +2783,20 @@ ExecARDeleteTriggers(EState *estate,
if ((trigdesc && trigdesc->trig_delete_after_row) || if ((trigdesc && trigdesc->trig_delete_after_row) ||
(transition_capture && transition_capture->tcs_delete_old_table)) (transition_capture && transition_capture->tcs_delete_old_table))
{ {
/* TupleTableSlot *slot = ExecGetTriggerOldSlot(estate, relinfo);
* Put the FDW old tuple to the slot. Otherwise, the caller is
* expected to have an old tuple already fetched to the slot. Assert(HeapTupleIsValid(fdw_trigtuple) ^ ItemPointerIsValid(tupleid));
*/ if (fdw_trigtuple == NULL)
if (fdw_trigtuple != NULL) GetTupleForTrigger(estate,
NULL,
relinfo,
tupleid,
LockTupleExclusive,
slot,
NULL,
NULL,
NULL);
else
ExecForceStoreHeapTuple(fdw_trigtuple, slot, false); ExecForceStoreHeapTuple(fdw_trigtuple, slot, false);
AfterTriggerSaveEvent(estate, relinfo, NULL, NULL, AfterTriggerSaveEvent(estate, relinfo, NULL, NULL,
@ -3078,17 +3087,18 @@ ExecBRUpdateTriggers(EState *estate, EPQState *epqstate,
* Note: 'src_partinfo' and 'dst_partinfo', when non-NULL, refer to the source * Note: 'src_partinfo' and 'dst_partinfo', when non-NULL, refer to the source
* and destination partitions, respectively, of a cross-partition update of * and destination partitions, respectively, of a cross-partition update of
* the root partitioned table mentioned in the query, given by 'relinfo'. * the root partitioned table mentioned in the query, given by 'relinfo'.
* 'oldslot' contains the "old" tuple in the source partition, and 'newslot' * 'tupleid' in that case refers to the ctid of the "old" tuple in the source
* contains the "new" tuple in the destination partition. This interface * partition, and 'newslot' contains the "new" tuple in the destination
* allows to support the requirements of ExecCrossPartitionUpdateForeignKey(); * partition. This interface allows to support the requirements of
* is_crosspart_update must be true in that case. * ExecCrossPartitionUpdateForeignKey(); is_crosspart_update must be true in
* that case.
*/ */
void void
ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
ResultRelInfo *src_partinfo, ResultRelInfo *src_partinfo,
ResultRelInfo *dst_partinfo, ResultRelInfo *dst_partinfo,
ItemPointer tupleid,
HeapTuple fdw_trigtuple, HeapTuple fdw_trigtuple,
TupleTableSlot *oldslot,
TupleTableSlot *newslot, TupleTableSlot *newslot,
List *recheckIndexes, List *recheckIndexes,
TransitionCaptureState *transition_capture, TransitionCaptureState *transition_capture,
@ -3107,14 +3117,29 @@ ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
* separately for DELETE and INSERT to capture transition table rows. * separately for DELETE and INSERT to capture transition table rows.
* In such case, either old tuple or new tuple can be NULL. * In such case, either old tuple or new tuple can be NULL.
*/ */
TupleTableSlot *oldslot;
ResultRelInfo *tupsrc;
Assert((src_partinfo != NULL && dst_partinfo != NULL) || Assert((src_partinfo != NULL && dst_partinfo != NULL) ||
!is_crosspart_update); !is_crosspart_update);
if (fdw_trigtuple != NULL) tupsrc = src_partinfo ? src_partinfo : relinfo;
{ oldslot = ExecGetTriggerOldSlot(estate, tupsrc);
Assert(oldslot);
if (fdw_trigtuple == NULL && ItemPointerIsValid(tupleid))
GetTupleForTrigger(estate,
NULL,
tupsrc,
tupleid,
LockTupleExclusive,
oldslot,
NULL,
NULL,
NULL);
else if (fdw_trigtuple != NULL)
ExecForceStoreHeapTuple(fdw_trigtuple, oldslot, false); ExecForceStoreHeapTuple(fdw_trigtuple, oldslot, false);
} else
ExecClearTuple(oldslot);
AfterTriggerSaveEvent(estate, relinfo, AfterTriggerSaveEvent(estate, relinfo,
src_partinfo, dst_partinfo, src_partinfo, dst_partinfo,

View File

@ -577,7 +577,6 @@ ExecSimpleRelationUpdate(ResultRelInfo *resultRelInfo,
{ {
List *recheckIndexes = NIL; List *recheckIndexes = NIL;
TU_UpdateIndexes update_indexes; TU_UpdateIndexes update_indexes;
TupleTableSlot *oldSlot = NULL;
/* Compute stored generated columns */ /* Compute stored generated columns */
if (rel->rd_att->constr && if (rel->rd_att->constr &&
@ -591,12 +590,8 @@ ExecSimpleRelationUpdate(ResultRelInfo *resultRelInfo,
if (rel->rd_rel->relispartition) if (rel->rd_rel->relispartition)
ExecPartitionCheck(resultRelInfo, slot, estate, true); ExecPartitionCheck(resultRelInfo, slot, estate, true);
if (resultRelInfo->ri_TrigDesc &&
resultRelInfo->ri_TrigDesc->trig_update_after_row)
oldSlot = ExecGetTriggerOldSlot(estate, resultRelInfo);
simple_table_tuple_update(rel, tid, slot, estate->es_snapshot, simple_table_tuple_update(rel, tid, slot, estate->es_snapshot,
&update_indexes, oldSlot); &update_indexes);
if (resultRelInfo->ri_NumIndices > 0 && (update_indexes != TU_None)) if (resultRelInfo->ri_NumIndices > 0 && (update_indexes != TU_None))
recheckIndexes = ExecInsertIndexTuples(resultRelInfo, recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
@ -607,7 +602,7 @@ ExecSimpleRelationUpdate(ResultRelInfo *resultRelInfo,
/* AFTER ROW UPDATE Triggers */ /* AFTER ROW UPDATE Triggers */
ExecARUpdateTriggers(estate, resultRelInfo, ExecARUpdateTriggers(estate, resultRelInfo,
NULL, NULL, NULL, NULL,
NULL, oldSlot, slot, tid, NULL, slot,
recheckIndexes, NULL, false); recheckIndexes, NULL, false);
list_free(recheckIndexes); list_free(recheckIndexes);
@ -641,18 +636,12 @@ ExecSimpleRelationDelete(ResultRelInfo *resultRelInfo,
if (!skip_tuple) if (!skip_tuple)
{ {
TupleTableSlot *oldSlot = NULL;
if (resultRelInfo->ri_TrigDesc &&
resultRelInfo->ri_TrigDesc->trig_delete_after_row)
oldSlot = ExecGetTriggerOldSlot(estate, resultRelInfo);
/* OK, delete the tuple */ /* OK, delete the tuple */
simple_table_tuple_delete(rel, tid, estate->es_snapshot, oldSlot); simple_table_tuple_delete(rel, tid, estate->es_snapshot);
/* AFTER ROW DELETE Triggers */ /* AFTER ROW DELETE Triggers */
ExecARDeleteTriggers(estate, resultRelInfo, ExecARDeleteTriggers(estate, resultRelInfo,
NULL, oldSlot, NULL, false); tid, NULL, NULL, false);
} }
} }

View File

@ -568,15 +568,6 @@ ExecInitInsertProjection(ModifyTableState *mtstate,
table_slot_create(resultRelInfo->ri_RelationDesc, table_slot_create(resultRelInfo->ri_RelationDesc,
&estate->es_tupleTable); &estate->es_tupleTable);
/*
* In the ON CONFLICT UPDATE case, we will also need a slot for the old
* tuple to calculate the updated tuple on its base.
*/
if (node->onConflictAction == ONCONFLICT_UPDATE)
resultRelInfo->ri_oldTupleSlot =
table_slot_create(resultRelInfo->ri_RelationDesc,
&estate->es_tupleTable);
/* Build ProjectionInfo if needed (it probably isn't). */ /* Build ProjectionInfo if needed (it probably isn't). */
if (need_projection) if (need_projection)
{ {
@ -1165,7 +1156,7 @@ ExecInsert(ModifyTableContext *context,
ExecARUpdateTriggers(estate, resultRelInfo, ExecARUpdateTriggers(estate, resultRelInfo,
NULL, NULL, NULL, NULL,
NULL, NULL,
resultRelInfo->ri_oldTupleSlot, NULL,
slot, slot,
NULL, NULL,
mtstate->mt_transition_capture, mtstate->mt_transition_capture,
@ -1345,8 +1336,7 @@ ExecDeletePrologue(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
*/ */
static TM_Result static TM_Result
ExecDeleteAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ExecDeleteAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
ItemPointer tupleid, bool changingPart, int options, ItemPointer tupleid, bool changingPart)
TupleTableSlot *oldSlot)
{ {
EState *estate = context->estate; EState *estate = context->estate;
@ -1354,10 +1344,9 @@ ExecDeleteAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
estate->es_output_cid, estate->es_output_cid,
estate->es_snapshot, estate->es_snapshot,
estate->es_crosscheck_snapshot, estate->es_crosscheck_snapshot,
options, true /* wait for commit */ ,
&context->tmfd, &context->tmfd,
changingPart, changingPart);
oldSlot);
} }
/* /*
@ -1366,15 +1355,10 @@ ExecDeleteAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
* Closing steps of tuple deletion; this invokes AFTER FOR EACH ROW triggers, * Closing steps of tuple deletion; this invokes AFTER FOR EACH ROW triggers,
* including the UPDATE triggers if the deletion is being done as part of a * including the UPDATE triggers if the deletion is being done as part of a
* cross-partition tuple move. * cross-partition tuple move.
*
* The old tuple is already fetched into slot for regular tables. For FDW,
* the old tuple is given as 'oldtuple' and is to be stored in 'slot' when
* needed.
*/ */
static void static void
ExecDeleteEpilogue(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ExecDeleteEpilogue(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
ItemPointer tupleid, HeapTuple oldtuple, ItemPointer tupleid, HeapTuple oldtuple, bool changingPart)
TupleTableSlot *slot, bool changingPart)
{ {
ModifyTableState *mtstate = context->mtstate; ModifyTableState *mtstate = context->mtstate;
EState *estate = context->estate; EState *estate = context->estate;
@ -1392,8 +1376,8 @@ ExecDeleteEpilogue(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
{ {
ExecARUpdateTriggers(estate, resultRelInfo, ExecARUpdateTriggers(estate, resultRelInfo,
NULL, NULL, NULL, NULL,
oldtuple, tupleid, oldtuple,
slot, NULL, NULL, mtstate->mt_transition_capture, NULL, NULL, mtstate->mt_transition_capture,
false); false);
/* /*
@ -1404,30 +1388,10 @@ ExecDeleteEpilogue(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
} }
/* AFTER ROW DELETE Triggers */ /* AFTER ROW DELETE Triggers */
ExecARDeleteTriggers(estate, resultRelInfo, oldtuple, slot, ExecARDeleteTriggers(estate, resultRelInfo, tupleid, oldtuple,
ar_delete_trig_tcs, changingPart); ar_delete_trig_tcs, changingPart);
} }
/*
* Initializes the tuple slot in a ResultRelInfo for DELETE action.
*
* We mark 'projectNewInfoValid' even though the projections themselves
* are not initialized here.
*/
static void
ExecInitDeleteTupleSlot(ModifyTableState *mtstate,
ResultRelInfo *resultRelInfo)
{
EState *estate = mtstate->ps.state;
Assert(!resultRelInfo->ri_projectNewInfoValid);
resultRelInfo->ri_oldTupleSlot =
table_slot_create(resultRelInfo->ri_RelationDesc,
&estate->es_tupleTable);
resultRelInfo->ri_projectNewInfoValid = true;
}
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
* ExecDelete * ExecDelete
* *
@ -1447,8 +1411,7 @@ ExecInitDeleteTupleSlot(ModifyTableState *mtstate,
* part of an UPDATE of partition-key, then the slot returned by * part of an UPDATE of partition-key, then the slot returned by
* EvalPlanQual() is passed back using output parameter epqreturnslot. * EvalPlanQual() is passed back using output parameter epqreturnslot.
* *
* Returns RETURNING result if any, otherwise NULL. The deleted tuple * Returns RETURNING result if any, otherwise NULL.
* to be stored into oldslot independently that.
* ---------------------------------------------------------------- * ----------------------------------------------------------------
*/ */
static TupleTableSlot * static TupleTableSlot *
@ -1456,7 +1419,6 @@ ExecDelete(ModifyTableContext *context,
ResultRelInfo *resultRelInfo, ResultRelInfo *resultRelInfo,
ItemPointer tupleid, ItemPointer tupleid,
HeapTuple oldtuple, HeapTuple oldtuple,
TupleTableSlot *oldslot,
bool processReturning, bool processReturning,
bool changingPart, bool changingPart,
bool canSetTag, bool canSetTag,
@ -1520,15 +1482,6 @@ ExecDelete(ModifyTableContext *context,
} }
else else
{ {
int options = TABLE_MODIFY_WAIT | TABLE_MODIFY_FETCH_OLD_TUPLE;
/*
* Specify that we need to lock and fetch the last tuple version for
* EPQ on appropriate transaction isolation levels.
*/
if (!IsolationUsesXactSnapshot())
options |= TABLE_MODIFY_LOCK_UPDATED;
/* /*
* delete the tuple * delete the tuple
* *
@ -1539,8 +1492,7 @@ ExecDelete(ModifyTableContext *context,
* transaction-snapshot mode transactions. * transaction-snapshot mode transactions.
*/ */
ldelete: ldelete:
result = ExecDeleteAct(context, resultRelInfo, tupleid, changingPart, result = ExecDeleteAct(context, resultRelInfo, tupleid, changingPart);
options, oldslot);
if (tmresult) if (tmresult)
*tmresult = result; *tmresult = result;
@ -1587,6 +1539,7 @@ ldelete:
case TM_Updated: case TM_Updated:
{ {
TupleTableSlot *inputslot;
TupleTableSlot *epqslot; TupleTableSlot *epqslot;
if (IsolationUsesXactSnapshot()) if (IsolationUsesXactSnapshot())
@ -1595,21 +1548,35 @@ ldelete:
errmsg("could not serialize access due to concurrent update"))); errmsg("could not serialize access due to concurrent update")));
/* /*
* We need to do EPQ. The latest tuple is already found * Already know that we're going to need to do EPQ, so
* and locked as a result of TABLE_MODIFY_LOCK_UPDATED. * fetch tuple directly into the right slot.
*/ */
EvalPlanQualBegin(context->epqstate);
inputslot = EvalPlanQualSlot(context->epqstate, resultRelationDesc,
resultRelInfo->ri_RangeTableIndex);
result = table_tuple_lock(resultRelationDesc, tupleid,
estate->es_snapshot,
inputslot, estate->es_output_cid,
LockTupleExclusive, LockWaitBlock,
TUPLE_LOCK_FLAG_FIND_LAST_VERSION,
&context->tmfd);
switch (result)
{
case TM_Ok:
Assert(context->tmfd.traversed); Assert(context->tmfd.traversed);
epqslot = EvalPlanQual(context->epqstate, epqslot = EvalPlanQual(context->epqstate,
resultRelationDesc, resultRelationDesc,
resultRelInfo->ri_RangeTableIndex, resultRelInfo->ri_RangeTableIndex,
oldslot); inputslot);
if (TupIsNull(epqslot)) if (TupIsNull(epqslot))
/* Tuple not passing quals anymore, exiting... */ /* Tuple not passing quals anymore, exiting... */
return NULL; return NULL;
/* /*
* If requested, skip delete and pass back the updated * If requested, skip delete and pass back the
* row. * updated row.
*/ */
if (epqreturnslot) if (epqreturnslot)
{ {
@ -1618,6 +1585,50 @@ ldelete:
} }
else else
goto ldelete; goto ldelete;
case TM_SelfModified:
/*
* This can be reached when following an update
* chain from a tuple updated by another session,
* reaching a tuple that was already updated in
* this transaction. If previously updated by this
* command, ignore the delete, otherwise error
* out.
*
* See also TM_SelfModified response to
* table_tuple_delete() above.
*/
if (context->tmfd.cmax != estate->es_output_cid)
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
errmsg("tuple to be deleted was already modified by an operation triggered by the current command"),
errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
return NULL;
case TM_Deleted:
/* tuple already deleted; nothing to do */
return NULL;
default:
/*
* TM_Invisible should be impossible because we're
* waiting for updated row versions, and would
* already have errored out if the first version
* is invisible.
*
* TM_Updated should be impossible, because we're
* locking the latest version via
* TUPLE_LOCK_FLAG_FIND_LAST_VERSION.
*/
elog(ERROR, "unexpected table_tuple_lock status: %u",
result);
return NULL;
}
Assert(false);
break;
} }
case TM_Deleted: case TM_Deleted:
@ -1651,8 +1662,7 @@ ldelete:
if (tupleDeleted) if (tupleDeleted)
*tupleDeleted = true; *tupleDeleted = true;
ExecDeleteEpilogue(context, resultRelInfo, tupleid, oldtuple, ExecDeleteEpilogue(context, resultRelInfo, tupleid, oldtuple, changingPart);
oldslot, changingPart);
/* Process RETURNING if present and if requested */ /* Process RETURNING if present and if requested */
if (processReturning && resultRelInfo->ri_projectReturning) if (processReturning && resultRelInfo->ri_projectReturning)
@ -1670,13 +1680,17 @@ ldelete:
} }
else else
{ {
/* Copy old tuple to the returning slot */
slot = ExecGetReturningSlot(estate, resultRelInfo); slot = ExecGetReturningSlot(estate, resultRelInfo);
if (oldtuple != NULL) if (oldtuple != NULL)
{
ExecForceStoreHeapTuple(oldtuple, slot, false); ExecForceStoreHeapTuple(oldtuple, slot, false);
}
else else
ExecCopySlot(slot, oldslot); {
Assert(!TupIsNull(slot)); if (!table_tuple_fetch_row_version(resultRelationDesc, tupleid,
SnapshotAny, slot))
elog(ERROR, "failed to fetch deleted tuple for DELETE RETURNING");
}
} }
rslot = ExecProcessReturning(resultRelInfo, slot, context->planSlot); rslot = ExecProcessReturning(resultRelInfo, slot, context->planSlot);
@ -1776,19 +1790,12 @@ ExecCrossPartitionUpdate(ModifyTableContext *context,
MemoryContextSwitchTo(oldcxt); MemoryContextSwitchTo(oldcxt);
} }
/*
* Make sure ri_oldTupleSlot is initialized. The old tuple is to be saved
* there by ExecDelete() to save effort on further re-fetching.
*/
if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
ExecInitUpdateProjection(mtstate, resultRelInfo);
/* /*
* Row movement, part 1. Delete the tuple, but skip RETURNING processing. * Row movement, part 1. Delete the tuple, but skip RETURNING processing.
* We want to return rows from INSERT. * We want to return rows from INSERT.
*/ */
ExecDelete(context, resultRelInfo, ExecDelete(context, resultRelInfo,
tupleid, oldtuple, resultRelInfo->ri_oldTupleSlot, tupleid, oldtuple,
false, /* processReturning */ false, /* processReturning */
true, /* changingPart */ true, /* changingPart */
false, /* canSetTag */ false, /* canSetTag */
@ -1829,13 +1836,21 @@ ExecCrossPartitionUpdate(ModifyTableContext *context,
return true; return true;
else else
{ {
/* /* Fetch the most recent version of old tuple. */
* ExecDelete already fetches the most recent version of old tuple TupleTableSlot *oldSlot;
* to resultRelInfo->ri_oldTupleSlot. So, just project the new
* tuple to retry the UPDATE with. /* ... but first, make sure ri_oldTupleSlot is initialized. */
*/ if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
ExecInitUpdateProjection(mtstate, resultRelInfo);
oldSlot = resultRelInfo->ri_oldTupleSlot;
if (!table_tuple_fetch_row_version(resultRelInfo->ri_RelationDesc,
tupleid,
SnapshotAny,
oldSlot))
elog(ERROR, "failed to fetch tuple being updated");
/* and project the new tuple to retry the UPDATE with */
*retry_slot = ExecGetUpdateNewTuple(resultRelInfo, epqslot, *retry_slot = ExecGetUpdateNewTuple(resultRelInfo, epqslot,
resultRelInfo->ri_oldTupleSlot); oldSlot);
return false; return false;
} }
} }
@ -1954,8 +1969,7 @@ ExecUpdatePrepareSlot(ResultRelInfo *resultRelInfo,
static TM_Result static TM_Result
ExecUpdateAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ExecUpdateAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot,
bool canSetTag, int options, TupleTableSlot *oldSlot, bool canSetTag, UpdateContext *updateCxt)
UpdateContext *updateCxt)
{ {
EState *estate = context->estate; EState *estate = context->estate;
Relation resultRelationDesc = resultRelInfo->ri_RelationDesc; Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
@ -2047,8 +2061,7 @@ lreplace:
ExecCrossPartitionUpdateForeignKey(context, ExecCrossPartitionUpdateForeignKey(context,
resultRelInfo, resultRelInfo,
insert_destrel, insert_destrel,
tupleid, tupleid, slot,
resultRelInfo->ri_oldTupleSlot,
inserted_tuple); inserted_tuple);
return TM_Ok; return TM_Ok;
@ -2091,10 +2104,9 @@ lreplace:
estate->es_output_cid, estate->es_output_cid,
estate->es_snapshot, estate->es_snapshot,
estate->es_crosscheck_snapshot, estate->es_crosscheck_snapshot,
options /* wait for commit */ , true /* wait for commit */ ,
&context->tmfd, &updateCxt->lockmode, &context->tmfd, &updateCxt->lockmode,
&updateCxt->updateIndexes, &updateCxt->updateIndexes);
oldSlot);
return result; return result;
} }
@ -2108,8 +2120,7 @@ lreplace:
static void static void
ExecUpdateEpilogue(ModifyTableContext *context, UpdateContext *updateCxt, ExecUpdateEpilogue(ModifyTableContext *context, UpdateContext *updateCxt,
ResultRelInfo *resultRelInfo, ItemPointer tupleid, ResultRelInfo *resultRelInfo, ItemPointer tupleid,
HeapTuple oldtuple, TupleTableSlot *slot, HeapTuple oldtuple, TupleTableSlot *slot)
TupleTableSlot *oldslot)
{ {
ModifyTableState *mtstate = context->mtstate; ModifyTableState *mtstate = context->mtstate;
List *recheckIndexes = NIL; List *recheckIndexes = NIL;
@ -2125,7 +2136,7 @@ ExecUpdateEpilogue(ModifyTableContext *context, UpdateContext *updateCxt,
/* AFTER ROW UPDATE Triggers */ /* AFTER ROW UPDATE Triggers */
ExecARUpdateTriggers(context->estate, resultRelInfo, ExecARUpdateTriggers(context->estate, resultRelInfo,
NULL, NULL, NULL, NULL,
oldtuple, oldslot, slot, tupleid, oldtuple, slot,
recheckIndexes, recheckIndexes,
mtstate->operation == CMD_INSERT ? mtstate->operation == CMD_INSERT ?
mtstate->mt_oc_transition_capture : mtstate->mt_oc_transition_capture :
@ -2214,7 +2225,7 @@ ExecCrossPartitionUpdateForeignKey(ModifyTableContext *context,
/* Perform the root table's triggers. */ /* Perform the root table's triggers. */
ExecARUpdateTriggers(context->estate, ExecARUpdateTriggers(context->estate,
rootRelInfo, sourcePartInfo, destPartInfo, rootRelInfo, sourcePartInfo, destPartInfo,
NULL, oldslot, newslot, NIL, NULL, true); tupleid, NULL, newslot, NIL, NULL, true);
} }
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
@ -2237,7 +2248,6 @@ ExecCrossPartitionUpdateForeignKey(ModifyTableContext *context,
* no relevant triggers. * no relevant triggers.
* *
* slot contains the new tuple value to be stored. * slot contains the new tuple value to be stored.
* oldslot is the slot to store the old tuple.
* planSlot is the output of the ModifyTable's subplan; we use it * planSlot is the output of the ModifyTable's subplan; we use it
* to access values from other input tables (for RETURNING), * to access values from other input tables (for RETURNING),
* row-ID junk columns, etc. * row-ID junk columns, etc.
@ -2248,7 +2258,7 @@ ExecCrossPartitionUpdateForeignKey(ModifyTableContext *context,
static TupleTableSlot * static TupleTableSlot *
ExecUpdate(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ExecUpdate(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot,
TupleTableSlot *oldslot, bool canSetTag, bool locked) bool canSetTag)
{ {
EState *estate = context->estate; EState *estate = context->estate;
Relation resultRelationDesc = resultRelInfo->ri_RelationDesc; Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
@ -2301,16 +2311,6 @@ ExecUpdate(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
} }
else else
{ {
int options = TABLE_MODIFY_WAIT | TABLE_MODIFY_FETCH_OLD_TUPLE;
/*
* Specify that we need to lock and fetch the last tuple version for
* EPQ on appropriate transaction isolation levels if the tuple isn't
* locked already.
*/
if (!locked && !IsolationUsesXactSnapshot())
options |= TABLE_MODIFY_LOCK_UPDATED;
/* /*
* If we generate a new candidate tuple after EvalPlanQual testing, we * If we generate a new candidate tuple after EvalPlanQual testing, we
* must loop back here to try again. (We don't need to redo triggers, * must loop back here to try again. (We don't need to redo triggers,
@ -2320,7 +2320,7 @@ ExecUpdate(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
*/ */
redo_act: redo_act:
result = ExecUpdateAct(context, resultRelInfo, tupleid, oldtuple, slot, result = ExecUpdateAct(context, resultRelInfo, tupleid, oldtuple, slot,
canSetTag, options, oldslot, &updateCxt); canSetTag, &updateCxt);
/* /*
* If ExecUpdateAct reports that a cross-partition update was done, * If ExecUpdateAct reports that a cross-partition update was done,
@ -2371,32 +2371,88 @@ redo_act:
case TM_Updated: case TM_Updated:
{ {
TupleTableSlot *inputslot;
TupleTableSlot *epqslot; TupleTableSlot *epqslot;
TupleTableSlot *oldSlot;
if (IsolationUsesXactSnapshot()) if (IsolationUsesXactSnapshot())
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE), (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
errmsg("could not serialize access due to concurrent update"))); errmsg("could not serialize access due to concurrent update")));
/* Shouldn't get there if the tuple was previously locked */
Assert(!locked);
/* /*
* We need to do EPQ. The latest tuple is already found * Already know that we're going to need to do EPQ, so
* and locked as a result of TABLE_MODIFY_LOCK_UPDATED. * fetch tuple directly into the right slot.
*/ */
inputslot = EvalPlanQualSlot(context->epqstate, resultRelationDesc,
resultRelInfo->ri_RangeTableIndex);
result = table_tuple_lock(resultRelationDesc, tupleid,
estate->es_snapshot,
inputslot, estate->es_output_cid,
updateCxt.lockmode, LockWaitBlock,
TUPLE_LOCK_FLAG_FIND_LAST_VERSION,
&context->tmfd);
switch (result)
{
case TM_Ok:
Assert(context->tmfd.traversed); Assert(context->tmfd.traversed);
epqslot = EvalPlanQual(context->epqstate, epqslot = EvalPlanQual(context->epqstate,
resultRelationDesc, resultRelationDesc,
resultRelInfo->ri_RangeTableIndex, resultRelInfo->ri_RangeTableIndex,
oldslot); inputslot);
if (TupIsNull(epqslot)) if (TupIsNull(epqslot))
/* Tuple not passing quals anymore, exiting... */ /* Tuple not passing quals anymore, exiting... */
return NULL; return NULL;
/* Make sure ri_oldTupleSlot is initialized. */
if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
ExecInitUpdateProjection(context->mtstate,
resultRelInfo);
/* Fetch the most recent version of old tuple. */
oldSlot = resultRelInfo->ri_oldTupleSlot;
if (!table_tuple_fetch_row_version(resultRelationDesc,
tupleid,
SnapshotAny,
oldSlot))
elog(ERROR, "failed to fetch tuple being updated");
slot = ExecGetUpdateNewTuple(resultRelInfo, slot = ExecGetUpdateNewTuple(resultRelInfo,
epqslot, epqslot, oldSlot);
oldslot);
goto redo_act; goto redo_act;
case TM_Deleted:
/* tuple already deleted; nothing to do */
return NULL;
case TM_SelfModified:
/*
* This can be reached when following an update
* chain from a tuple updated by another session,
* reaching a tuple that was already updated in
* this transaction. If previously modified by
* this command, ignore the redundant update,
* otherwise error out.
*
* See also TM_SelfModified response to
* table_tuple_update() above.
*/
if (context->tmfd.cmax != estate->es_output_cid)
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
return NULL;
default:
/* see table_tuple_lock call in ExecDelete() */
elog(ERROR, "unexpected table_tuple_lock status: %u",
result);
return NULL;
}
} }
break; break;
@ -2420,7 +2476,7 @@ redo_act:
(estate->es_processed)++; (estate->es_processed)++;
ExecUpdateEpilogue(context, &updateCxt, resultRelInfo, tupleid, oldtuple, ExecUpdateEpilogue(context, &updateCxt, resultRelInfo, tupleid, oldtuple,
slot, oldslot); slot);
/* Process RETURNING if present */ /* Process RETURNING if present */
if (resultRelInfo->ri_projectReturning) if (resultRelInfo->ri_projectReturning)
@ -2638,8 +2694,7 @@ ExecOnConflictUpdate(ModifyTableContext *context,
*returning = ExecUpdate(context, resultRelInfo, *returning = ExecUpdate(context, resultRelInfo,
conflictTid, NULL, conflictTid, NULL,
resultRelInfo->ri_onConflict->oc_ProjSlot, resultRelInfo->ri_onConflict->oc_ProjSlot,
existing, canSetTag);
canSetTag, true);
/* /*
* Clear out existing tuple, as there might not be another conflict among * Clear out existing tuple, as there might not be another conflict among
@ -2918,7 +2973,6 @@ lmerge_matched:
{ {
result = ExecUpdateAct(context, resultRelInfo, tupleid, result = ExecUpdateAct(context, resultRelInfo, tupleid,
NULL, newslot, canSetTag, NULL, newslot, canSetTag,
TABLE_MODIFY_WAIT, NULL,
&updateCxt); &updateCxt);
/* /*
@ -2940,8 +2994,7 @@ lmerge_matched:
if (result == TM_Ok) if (result == TM_Ok)
{ {
ExecUpdateEpilogue(context, &updateCxt, resultRelInfo, ExecUpdateEpilogue(context, &updateCxt, resultRelInfo,
tupleid, NULL, newslot, tupleid, NULL, newslot);
resultRelInfo->ri_oldTupleSlot);
mtstate->mt_merge_updated += 1; mtstate->mt_merge_updated += 1;
} }
break; break;
@ -2967,12 +3020,12 @@ lmerge_matched:
} }
else else
result = ExecDeleteAct(context, resultRelInfo, tupleid, result = ExecDeleteAct(context, resultRelInfo, tupleid,
false, TABLE_MODIFY_WAIT, NULL); false);
if (result == TM_Ok) if (result == TM_Ok)
{ {
ExecDeleteEpilogue(context, resultRelInfo, tupleid, NULL, ExecDeleteEpilogue(context, resultRelInfo, tupleid, NULL,
resultRelInfo->ri_oldTupleSlot, false); false);
mtstate->mt_merge_deleted += 1; mtstate->mt_merge_deleted += 1;
} }
break; break;
@ -4037,18 +4090,12 @@ ExecModifyTable(PlanState *pstate)
/* Now apply the update. */ /* Now apply the update. */
slot = ExecUpdate(&context, resultRelInfo, tupleid, oldtuple, slot = ExecUpdate(&context, resultRelInfo, tupleid, oldtuple,
slot, resultRelInfo->ri_oldTupleSlot, slot, node->canSetTag);
node->canSetTag, false);
break; break;
case CMD_DELETE: case CMD_DELETE:
/* Initialize slot for DELETE to fetch the old tuple */
if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
ExecInitDeleteTupleSlot(node, resultRelInfo);
slot = ExecDelete(&context, resultRelInfo, tupleid, oldtuple, slot = ExecDelete(&context, resultRelInfo, tupleid, oldtuple,
resultRelInfo->ri_oldTupleSlot, true, false, true, false, node->canSetTag, NULL, NULL, NULL);
node->canSetTag, NULL, NULL, NULL);
break; break;
case CMD_MERGE: case CMD_MERGE:

View File

@ -322,22 +322,19 @@ extern void heap_multi_insert(Relation relation, struct TupleTableSlot **slots,
int ntuples, CommandId cid, int options, int ntuples, CommandId cid, int options,
BulkInsertState bistate); BulkInsertState bistate);
extern TM_Result heap_delete(Relation relation, ItemPointer tid, extern TM_Result heap_delete(Relation relation, ItemPointer tid,
CommandId cid, Snapshot crosscheck, int options, CommandId cid, Snapshot crosscheck, bool wait,
struct TM_FailureData *tmfd, bool changingPart, struct TM_FailureData *tmfd, bool changingPart);
TupleTableSlot *oldSlot);
extern void heap_finish_speculative(Relation relation, ItemPointer tid); extern void heap_finish_speculative(Relation relation, ItemPointer tid);
extern void heap_abort_speculative(Relation relation, ItemPointer tid); extern void heap_abort_speculative(Relation relation, ItemPointer tid);
extern TM_Result heap_update(Relation relation, ItemPointer otid, extern TM_Result heap_update(Relation relation, ItemPointer otid,
HeapTuple newtup, HeapTuple newtup,
CommandId cid, Snapshot crosscheck, int options, CommandId cid, Snapshot crosscheck, bool wait,
struct TM_FailureData *tmfd, LockTupleMode *lockmode, struct TM_FailureData *tmfd, LockTupleMode *lockmode,
TU_UpdateIndexes *update_indexes, TU_UpdateIndexes *update_indexes);
TupleTableSlot *oldSlot); extern TM_Result heap_lock_tuple(Relation relation, HeapTuple tuple,
extern TM_Result heap_lock_tuple(Relation relation, ItemPointer tid, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy,
TupleTableSlot *slot, bool follow_updates,
CommandId cid, LockTupleMode mode, Buffer *buffer, struct TM_FailureData *tmfd);
LockWaitPolicy wait_policy, bool follow_updates,
struct TM_FailureData *tmfd);
extern void heap_inplace_update(Relation relation, HeapTuple tuple); extern void heap_inplace_update(Relation relation, HeapTuple tuple);
extern bool heap_prepare_freeze_tuple(HeapTupleHeader tuple, extern bool heap_prepare_freeze_tuple(HeapTupleHeader tuple,

View File

@ -267,15 +267,6 @@ typedef struct TM_IndexDeleteOp
/* Follow update chain and lock latest version of tuple */ /* Follow update chain and lock latest version of tuple */
#define TUPLE_LOCK_FLAG_FIND_LAST_VERSION (1 << 1) #define TUPLE_LOCK_FLAG_FIND_LAST_VERSION (1 << 1)
/*
* "options" flag bits for table_tuple_update and table_tuple_delete,
* Wait for any conflicting update to commit/abort */
#define TABLE_MODIFY_WAIT 0x0001
/* Fetch the existing tuple into a dedicated slot */
#define TABLE_MODIFY_FETCH_OLD_TUPLE 0x0002
/* On concurrent update, follow the update chain and lock latest version of tuple */
#define TABLE_MODIFY_LOCK_UPDATED 0x0004
/* Typedef for callback function for table_index_build_scan */ /* Typedef for callback function for table_index_build_scan */
typedef void (*IndexBuildCallback) (Relation index, typedef void (*IndexBuildCallback) (Relation index,
@ -545,10 +536,9 @@ typedef struct TableAmRoutine
CommandId cid, CommandId cid,
Snapshot snapshot, Snapshot snapshot,
Snapshot crosscheck, Snapshot crosscheck,
int options, bool wait,
TM_FailureData *tmfd, TM_FailureData *tmfd,
bool changingPart, bool changingPart);
TupleTableSlot *oldSlot);
/* see table_tuple_update() for reference about parameters */ /* see table_tuple_update() for reference about parameters */
TM_Result (*tuple_update) (Relation rel, TM_Result (*tuple_update) (Relation rel,
@ -557,11 +547,10 @@ typedef struct TableAmRoutine
CommandId cid, CommandId cid,
Snapshot snapshot, Snapshot snapshot,
Snapshot crosscheck, Snapshot crosscheck,
int options, bool wait,
TM_FailureData *tmfd, TM_FailureData *tmfd,
LockTupleMode *lockmode, LockTupleMode *lockmode,
TU_UpdateIndexes *update_indexes, TU_UpdateIndexes *update_indexes);
TupleTableSlot *oldSlot);
/* see table_tuple_lock() for reference about parameters */ /* see table_tuple_lock() for reference about parameters */
TM_Result (*tuple_lock) (Relation rel, TM_Result (*tuple_lock) (Relation rel,
@ -1456,7 +1445,7 @@ table_multi_insert(Relation rel, TupleTableSlot **slots, int nslots,
} }
/* /*
* Delete a tuple (and optionally lock the last tuple version). * Delete a tuple.
* *
* NB: do not call this directly unless prepared to deal with * NB: do not call this directly unless prepared to deal with
* concurrent-update conditions. Use simple_table_tuple_delete instead. * concurrent-update conditions. Use simple_table_tuple_delete instead.
@ -1467,21 +1456,11 @@ table_multi_insert(Relation rel, TupleTableSlot **slots, int nslots,
* cid - delete command ID (used for visibility test, and stored into * cid - delete command ID (used for visibility test, and stored into
* cmax if successful) * cmax if successful)
* crosscheck - if not InvalidSnapshot, also check tuple against this * crosscheck - if not InvalidSnapshot, also check tuple against this
* options: * wait - true if should wait for any conflicting update to commit/abort
* If TABLE_MODIFY_WAIT, wait for any conflicting update to commit/abort.
* If TABLE_MODIFY_FETCH_OLD_TUPLE option is given, the existing tuple is
* fetched into oldSlot when the update is successful.
* If TABLE_MODIFY_LOCK_UPDATED option is given and the tuple is
* concurrently updated, then the last tuple version is locked and fetched
* into oldSlot.
*
* Output parameters: * Output parameters:
* tmfd - filled in failure cases (see below) * tmfd - filled in failure cases (see below)
* changingPart - true iff the tuple is being moved to another partition * changingPart - true iff the tuple is being moved to another partition
* table due to an update of the partition key. Otherwise, false. * table due to an update of the partition key. Otherwise, false.
* oldSlot - slot to save the deleted or locked tuple. Can be NULL if none of
* TABLE_MODIFY_FETCH_OLD_TUPLE or TABLE_MODIFY_LOCK_UPDATED options
* is specified.
* *
* Normal, successful return value is TM_Ok, which means we did actually * Normal, successful return value is TM_Ok, which means we did actually
* delete it. Failure return codes are TM_SelfModified, TM_Updated, and * delete it. Failure return codes are TM_SelfModified, TM_Updated, and
@ -1493,18 +1472,16 @@ table_multi_insert(Relation rel, TupleTableSlot **slots, int nslots,
*/ */
static inline TM_Result static inline TM_Result
table_tuple_delete(Relation rel, ItemPointer tid, CommandId cid, table_tuple_delete(Relation rel, ItemPointer tid, CommandId cid,
Snapshot snapshot, Snapshot crosscheck, int options, Snapshot snapshot, Snapshot crosscheck, bool wait,
TM_FailureData *tmfd, bool changingPart, TM_FailureData *tmfd, bool changingPart)
TupleTableSlot *oldSlot)
{ {
return rel->rd_tableam->tuple_delete(rel, tid, cid, return rel->rd_tableam->tuple_delete(rel, tid, cid,
snapshot, crosscheck, snapshot, crosscheck,
options, tmfd, changingPart, wait, tmfd, changingPart);
oldSlot);
} }
/* /*
* Update a tuple (and optionally lock the last tuple version). * Update a tuple.
* *
* NB: do not call this directly unless you are prepared to deal with * NB: do not call this directly unless you are prepared to deal with
* concurrent-update conditions. Use simple_table_tuple_update instead. * concurrent-update conditions. Use simple_table_tuple_update instead.
@ -1516,23 +1493,13 @@ table_tuple_delete(Relation rel, ItemPointer tid, CommandId cid,
* cid - update command ID (used for visibility test, and stored into * cid - update command ID (used for visibility test, and stored into
* cmax/cmin if successful) * cmax/cmin if successful)
* crosscheck - if not InvalidSnapshot, also check old tuple against this * crosscheck - if not InvalidSnapshot, also check old tuple against this
* options: * wait - true if should wait for any conflicting update to commit/abort
* If TABLE_MODIFY_WAIT, wait for any conflicting update to commit/abort.
* If TABLE_MODIFY_FETCH_OLD_TUPLE option is given, the existing tuple is
* fetched into oldSlot when the update is successful.
* If TABLE_MODIFY_LOCK_UPDATED option is given and the tuple is
* concurrently updated, then the last tuple version is locked and fetched
* into oldSlot.
*
* Output parameters: * Output parameters:
* tmfd - filled in failure cases (see below) * tmfd - filled in failure cases (see below)
* lockmode - filled with lock mode acquired on tuple * lockmode - filled with lock mode acquired on tuple
* update_indexes - in success cases this is set to true if new index entries * update_indexes - in success cases this is set to true if new index entries
* are required for this tuple * are required for this tuple
* oldSlot - slot to save the deleted or locked tuple. Can be NULL if none of *
* TABLE_MODIFY_FETCH_OLD_TUPLE or TABLE_MODIFY_LOCK_UPDATED options
* is specified.
* Normal, successful return value is TM_Ok, which means we did actually * Normal, successful return value is TM_Ok, which means we did actually
* update it. Failure return codes are TM_SelfModified, TM_Updated, and * update it. Failure return codes are TM_SelfModified, TM_Updated, and
* TM_BeingModified (the last only possible if wait == false). * TM_BeingModified (the last only possible if wait == false).
@ -1550,15 +1517,13 @@ table_tuple_delete(Relation rel, ItemPointer tid, CommandId cid,
static inline TM_Result static inline TM_Result
table_tuple_update(Relation rel, ItemPointer otid, TupleTableSlot *slot, table_tuple_update(Relation rel, ItemPointer otid, TupleTableSlot *slot,
CommandId cid, Snapshot snapshot, Snapshot crosscheck, CommandId cid, Snapshot snapshot, Snapshot crosscheck,
int options, TM_FailureData *tmfd, LockTupleMode *lockmode, bool wait, TM_FailureData *tmfd, LockTupleMode *lockmode,
TU_UpdateIndexes *update_indexes, TU_UpdateIndexes *update_indexes)
TupleTableSlot *oldSlot)
{ {
return rel->rd_tableam->tuple_update(rel, otid, slot, return rel->rd_tableam->tuple_update(rel, otid, slot,
cid, snapshot, crosscheck, cid, snapshot, crosscheck,
options, tmfd, wait, tmfd,
lockmode, update_indexes, lockmode, update_indexes);
oldSlot);
} }
/* /*
@ -2079,12 +2044,10 @@ table_scan_sample_next_tuple(TableScanDesc scan,
extern void simple_table_tuple_insert(Relation rel, TupleTableSlot *slot); extern void simple_table_tuple_insert(Relation rel, TupleTableSlot *slot);
extern void simple_table_tuple_delete(Relation rel, ItemPointer tid, extern void simple_table_tuple_delete(Relation rel, ItemPointer tid,
Snapshot snapshot, Snapshot snapshot);
TupleTableSlot *oldSlot);
extern void simple_table_tuple_update(Relation rel, ItemPointer otid, extern void simple_table_tuple_update(Relation rel, ItemPointer otid,
TupleTableSlot *slot, Snapshot snapshot, TupleTableSlot *slot, Snapshot snapshot,
TU_UpdateIndexes *update_indexes, TU_UpdateIndexes *update_indexes);
TupleTableSlot *oldSlot);
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------

View File

@ -216,8 +216,8 @@ extern bool ExecBRDeleteTriggers(EState *estate,
TM_FailureData *tmfd); TM_FailureData *tmfd);
extern void ExecARDeleteTriggers(EState *estate, extern void ExecARDeleteTriggers(EState *estate,
ResultRelInfo *relinfo, ResultRelInfo *relinfo,
ItemPointer tupleid,
HeapTuple fdw_trigtuple, HeapTuple fdw_trigtuple,
TupleTableSlot *slot,
TransitionCaptureState *transition_capture, TransitionCaptureState *transition_capture,
bool is_crosspart_update); bool is_crosspart_update);
extern bool ExecIRDeleteTriggers(EState *estate, extern bool ExecIRDeleteTriggers(EState *estate,
@ -240,8 +240,8 @@ extern void ExecARUpdateTriggers(EState *estate,
ResultRelInfo *relinfo, ResultRelInfo *relinfo,
ResultRelInfo *src_partinfo, ResultRelInfo *src_partinfo,
ResultRelInfo *dst_partinfo, ResultRelInfo *dst_partinfo,
ItemPointer tupleid,
HeapTuple fdw_trigtuple, HeapTuple fdw_trigtuple,
TupleTableSlot *oldslot,
TupleTableSlot *newslot, TupleTableSlot *newslot,
List *recheckIndexes, List *recheckIndexes,
TransitionCaptureState *transition_capture, TransitionCaptureState *transition_capture,