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

From: Alexander Korotkov <akorotkov(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Allow locking updated tuples in tuple_update() and tuple_delete(
Date: 2024-03-25 23:30:47
Message-ID: E1rotmB-005eXB-Fn@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/87985cc9252296f11db3cacc155e8d710d2e9b9c

Modified Files
--------------
src/backend/access/heap/heapam.c | 205 +++++++++++++-----
src/backend/access/heap/heapam_handler.c | 94 ++++++--
src/backend/access/table/tableam.c | 26 ++-
src/backend/commands/trigger.c | 55 ++---
src/backend/executor/execReplication.c | 19 +-
src/backend/executor/nodeModifyTable.c | 353 ++++++++++++++-----------------
src/include/access/heapam.h | 19 +-
src/include/access/tableam.h | 73 +++++--
src/include/commands/trigger.h | 4 +-
9 files changed, 502 insertions(+), 346 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Andres Freund 2024-03-25 23:40:22 pgsql: ci: macos: Choose python version
Previous Message Andrew Dunstan 2024-03-25 22:20:06 Re: pgsql: make dist uses git archive