postgresql/src/backend/access
Alexander Korotkov 87985cc925 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
2024-03-26 01:27:56 +02:00
..
brin Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
common Add TIDStore, to store sets of TIDs (ItemPointerData) efficiently. 2024-03-21 10:08:42 +09:00
gin Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
gist Merge prune, freeze and vacuum WAL record formats 2024-03-25 14:59:58 +02:00
hash Merge prune, freeze and vacuum WAL record formats 2024-03-25 14:59:58 +02:00
heap Allow locking updated tuples in tuple_update() and tuple_delete() 2024-03-26 01:27:56 +02:00
index Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
nbtree Fix an oversight in refactoring in 06b10f80ba. 2024-03-22 15:25:53 +02:00
rmgrdesc Merge prune, freeze and vacuum WAL record formats 2024-03-25 14:59:58 +02:00
sequence Fix comment thinko in sequence.c 2024-02-27 08:19:39 +09:00
spgist Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
table Allow locking updated tuples in tuple_update() and tuple_delete() 2024-03-26 01:27:56 +02:00
tablesample Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
transam Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
Makefile Introduce sequence_*() access functions 2024-02-26 16:04:59 +09:00
meson.build Introduce sequence_*() access functions 2024-02-26 16:04:59 +09:00