pgsql: Prevent stale pg_locks.waitstart values

From: Fujii Masao <fujii(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Prevent stale pg_locks.waitstart values
Date: 2026-09-28 05:24:01
Message-ID: E1xB3qL-00000001YhK-0yj2@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Prevent stale pg_locks.waitstart values

Previously, pg_locks.waitstart could report the start time of a previous
lock wait for a new wait. For a regular backend, this could happen briefly
before the new start time was stored. On a standby, the startup process could
show the old time throughout the next wait, making it appear to have started
earlier than it did.

This happened because PGPROC->waitStart could remain set after a previous
wait in two cases. First, RemoveFromWaitQueue() did not clear it when a wait
was canceled due to a lock timeout, cancellation, or deadlock. Second, even
after a successful lock grant, ProcWakeup() could clear it before the waiter
stored its new start time.

Fix this by clearing waitStart in RemoveFromWaitQueue() when a failed wait
is removed, at the end of ProcSleep() to handle writes made after
ProcWakeup() cleared it, and in LockErrorCleanup() when an error bypasses
the ProcSleep() reset. This prevents pg_locks.waitstart from showing the
previous wait's time for a new wait.

Backpatch to all supported versions.

Author: Shihao Zhong <zhong950419(at)gmail(dot)com>
Reported-by: Alex Shapalov <shapalov(at)gmail(dot)com>
Reviewed-by: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Reviewed-by: Michael Paquier <michael(at)paquier(dot)xyz>
Reviewed-by: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Reviewed-by: Andrew Krylosov <krylosov(dot)andrew(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAPrb+Q+XN=sNusXiUeWmMo2H7Qgq3Y4uPekSSLkHcnCyf7GhXg@mail.gmail.com
Discussion: https://postgr.es/m/CAGRkXqQLxZBr-ouVrtaX2utMggi4+TiVMbgH04b_0JLgKryxbA@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_18_STABLE

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

Modified Files
--------------
src/backend/storage/lmgr/lock.c | 1 +
src/backend/storage/lmgr/proc.c | 9 +++++++++
2 files changed, 10 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2026-09-28 05:24:12 pgsql: Prevent stale pg_locks.waitstart values
Previous Message Fujii Masao 2026-09-28 05:23:50 pgsql: Prevent stale pg_locks.waitstart values