pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix concurrent update issue with MERGE.
5+ messages / 1 participants
[nested] [flat]

* pgsql: Fix concurrent update issue with MERGE.
@ 2025-09-05 07:29  Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Dean Rasheed @ 2025-09-05 07:29 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix concurrent update issue with MERGE.

When executing a MERGE UPDATE action, if there is more than one
concurrent update of the target row, the lock-and-retry code would
sometimes incorrectly identify the latest version of the target tuple,
leading to incorrect results.

This was caused by using the ctid field from the TM_FailureData
returned by table_tuple_lock() in a case where the result was TM_Ok,
which is unsafe because the TM_FailureData struct is not guaranteed to
be fully populated in that case. Instead, it should use the tupleid
passed to (and updated by) table_tuple_lock().

To reduce the chances of similar errors in the future, improve the
commentary for table_tuple_lock() and TM_FailureData to make it
clearer that table_tuple_lock() updates the tid passed to it, and most
fields of TM_FailureData should not be relied on in non-failure cases.
An exception to this is the "traversed" field, which is set in both
success and failure cases.

Reported-by: Dmitry <dsy.075@yandex.ru>
Author: Yugo Nagata <nagata@sraoss.co.jp>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/1570d30e-2b95-4239-b9c3-f7bf2f2f8556@yandex.ru
Backpatch-through: 15

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/c70b6db34ffeab48beef1fb4ce61bcad3772b8dd

Modified Files
--------------
src/backend/executor/nodeModifyTable.c             |   9 +-
src/include/access/tableam.h                       |  15 ++-
.../isolation/expected/merge-match-recheck.out     | 145 +++++++++++++++++++++
src/test/isolation/specs/merge-match-recheck.spec  |  18 +++
4 files changed, 179 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix concurrent update issue with MERGE.
@ 2025-09-05 07:29  Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Dean Rasheed @ 2025-09-05 07:29 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix concurrent update issue with MERGE.

When executing a MERGE UPDATE action, if there is more than one
concurrent update of the target row, the lock-and-retry code would
sometimes incorrectly identify the latest version of the target tuple,
leading to incorrect results.

This was caused by using the ctid field from the TM_FailureData
returned by table_tuple_lock() in a case where the result was TM_Ok,
which is unsafe because the TM_FailureData struct is not guaranteed to
be fully populated in that case. Instead, it should use the tupleid
passed to (and updated by) table_tuple_lock().

To reduce the chances of similar errors in the future, improve the
commentary for table_tuple_lock() and TM_FailureData to make it
clearer that table_tuple_lock() updates the tid passed to it, and most
fields of TM_FailureData should not be relied on in non-failure cases.
An exception to this is the "traversed" field, which is set in both
success and failure cases.

Reported-by: Dmitry <dsy.075@yandex.ru>
Author: Yugo Nagata <nagata@sraoss.co.jp>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/1570d30e-2b95-4239-b9c3-f7bf2f2f8556@yandex.ru
Backpatch-through: 15

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/f871fbae9d000938a194034eb58342cf88dbcee4

Modified Files
--------------
src/backend/executor/nodeModifyTable.c             |   8 +-
src/include/access/tableam.h                       |  15 ++-
.../isolation/expected/merge-match-recheck.out     | 136 +++++++++++++++++++++
src/test/isolation/specs/merge-match-recheck.spec  |  18 +++
4 files changed, 169 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix concurrent update issue with MERGE.
@ 2025-09-05 07:29  Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Dean Rasheed @ 2025-09-05 07:29 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix concurrent update issue with MERGE.

When executing a MERGE UPDATE action, if there is more than one
concurrent update of the target row, the lock-and-retry code would
sometimes incorrectly identify the latest version of the target tuple,
leading to incorrect results.

This was caused by using the ctid field from the TM_FailureData
returned by table_tuple_lock() in a case where the result was TM_Ok,
which is unsafe because the TM_FailureData struct is not guaranteed to
be fully populated in that case. Instead, it should use the tupleid
passed to (and updated by) table_tuple_lock().

To reduce the chances of similar errors in the future, improve the
commentary for table_tuple_lock() and TM_FailureData to make it
clearer that table_tuple_lock() updates the tid passed to it, and most
fields of TM_FailureData should not be relied on in non-failure cases.
An exception to this is the "traversed" field, which is set in both
success and failure cases.

Reported-by: Dmitry <dsy.075@yandex.ru>
Author: Yugo Nagata <nagata@sraoss.co.jp>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/1570d30e-2b95-4239-b9c3-f7bf2f2f8556@yandex.ru
Backpatch-through: 15

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/6195afbe536afd7b25aa78001d7510ed111f0404

Modified Files
--------------
src/backend/executor/nodeModifyTable.c             |   9 +-
src/include/access/tableam.h                       |  15 ++-
.../isolation/expected/merge-match-recheck.out     | 145 +++++++++++++++++++++
src/test/isolation/specs/merge-match-recheck.spec  |  18 +++
4 files changed, 179 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix concurrent update issue with MERGE.
@ 2025-09-05 07:29  Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Dean Rasheed @ 2025-09-05 07:29 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix concurrent update issue with MERGE.

When executing a MERGE UPDATE action, if there is more than one
concurrent update of the target row, the lock-and-retry code would
sometimes incorrectly identify the latest version of the target tuple,
leading to incorrect results.

This was caused by using the ctid field from the TM_FailureData
returned by table_tuple_lock() in a case where the result was TM_Ok,
which is unsafe because the TM_FailureData struct is not guaranteed to
be fully populated in that case. Instead, it should use the tupleid
passed to (and updated by) table_tuple_lock().

To reduce the chances of similar errors in the future, improve the
commentary for table_tuple_lock() and TM_FailureData to make it
clearer that table_tuple_lock() updates the tid passed to it, and most
fields of TM_FailureData should not be relied on in non-failure cases.
An exception to this is the "traversed" field, which is set in both
success and failure cases.

Reported-by: Dmitry <dsy.075@yandex.ru>
Author: Yugo Nagata <nagata@sraoss.co.jp>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/1570d30e-2b95-4239-b9c3-f7bf2f2f8556@yandex.ru
Backpatch-through: 15

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/21a61b87f47b6a1b359021bf9f71b14bbf805330

Modified Files
--------------
src/backend/executor/nodeModifyTable.c             |   8 +-
src/include/access/tableam.h                       |  15 ++-
.../isolation/expected/merge-match-recheck.out     | 136 +++++++++++++++++++++
src/test/isolation/specs/merge-match-recheck.spec  |  18 +++
4 files changed, 169 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix concurrent update issue with MERGE.
@ 2025-09-05 07:29  Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Dean Rasheed @ 2025-09-05 07:29 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix concurrent update issue with MERGE.

When executing a MERGE UPDATE action, if there is more than one
concurrent update of the target row, the lock-and-retry code would
sometimes incorrectly identify the latest version of the target tuple,
leading to incorrect results.

This was caused by using the ctid field from the TM_FailureData
returned by table_tuple_lock() in a case where the result was TM_Ok,
which is unsafe because the TM_FailureData struct is not guaranteed to
be fully populated in that case. Instead, it should use the tupleid
passed to (and updated by) table_tuple_lock().

To reduce the chances of similar errors in the future, improve the
commentary for table_tuple_lock() and TM_FailureData to make it
clearer that table_tuple_lock() updates the tid passed to it, and most
fields of TM_FailureData should not be relied on in non-failure cases.
An exception to this is the "traversed" field, which is set in both
success and failure cases.

Reported-by: Dmitry <dsy.075@yandex.ru>
Author: Yugo Nagata <nagata@sraoss.co.jp>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/1570d30e-2b95-4239-b9c3-f7bf2f2f8556@yandex.ru
Backpatch-through: 15

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/6ede13d1b5f515df0a199a7a830e448dab1511c0

Modified Files
--------------
src/backend/executor/nodeModifyTable.c             |   9 +-
src/include/access/tableam.h                       |  15 ++-
.../isolation/expected/merge-match-recheck.out     | 145 +++++++++++++++++++++
src/test/isolation/specs/merge-match-recheck.spec  |  18 +++
4 files changed, 179 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2025-09-05 07:29 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-09-05 07:29 pgsql: Fix concurrent update issue with MERGE. Dean Rasheed <dean.a.rasheed@gmail.com>
2025-09-05 07:29 pgsql: Fix concurrent update issue with MERGE. Dean Rasheed <dean.a.rasheed@gmail.com>
2025-09-05 07:29 pgsql: Fix concurrent update issue with MERGE. Dean Rasheed <dean.a.rasheed@gmail.com>
2025-09-05 07:29 pgsql: Fix concurrent update issue with MERGE. Dean Rasheed <dean.a.rasheed@gmail.com>
2025-09-05 07:29 pgsql: Fix concurrent update issue with MERGE. Dean Rasheed <dean.a.rasheed@gmail.com>

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