pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander Korotkov <akorotkov@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Allow locking updated tuples in tuple_update() and tuple_delete(
Date: Wed, 22 Mar 2023 21:27:25 +0000
Message-ID: <E1pf5zQ-004tAk-TI@gemulon.postgresql.org> (raw)

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 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 patch eliminates step 2 by taking the lock during first
tuple_update()/tuple_delete() call.  Heap table access method saves some
efforts 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.

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/11470f544e3729c60fab890145b2e839cbc8905e

Modified Files
--------------
src/backend/access/heap/heapam_handler.c | 109 +++++++++++-
src/backend/access/table/tableam.c       |   6 +-
src/backend/executor/nodeModifyTable.c   | 288 +++++++++++++------------------
src/include/access/tableam.h             |  28 ++-
src/include/executor/tuptable.h          |  38 ++++
src/tools/pgindent/typedefs.list         |   2 +
6 files changed, 285 insertions(+), 186 deletions(-)



view thread (3+ messages)  latest in thread

Message-ID: <E1pf5zQ-004tAk-TI@gemulon.postgresql.org>
Permalink:  ../E1pf5zQ-004tAk-TI@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1pf5zQ-004tAk-TI@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: akorotkov@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Allow locking updated tuples in tuple_update() and tuple_delete(
  In-Reply-To: <E1pf5zQ-004tAk-TI@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox