postgres.git / summary / log / commit / refs

commit    bb356f869fa595db8c2bee4ae4096a64897d7aa7
Author:   Fujii Masao <fujii@postgresql.org>
Date:     Mon Sep 28 05:20:27 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    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@gmail.com>
    Reported-by: Alex Shapalov <shapalov@gmail.com>
    Reviewed-by: Chao Li <li.evan.chao@gmail.com>
    Reviewed-by: Michael Paquier <michael@paquier.xyz>
    Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
    Reviewed-by: Andrew Krylosov <krylosov.andrew@gmail.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


src/backend/storage/lmgr/lock.c | 1 + src/backend/storage/lmgr/proc.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c index ce1f8cdef1c..71778b37d33 100644 --- a/src/backend/storage/lmgr/lock.c +++ b/src/backend/storage/lmgr/lock.c @@ -2114,6 +2114,7 @@ RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode) proc->waitLock = NULL; proc->waitProcLock = NULL; proc->waitStatus = PROC_WAIT_STATUS_ERROR; + pg_atomic_write_u64(&proc->waitStart, 0); /* * Delete the proclock immediately if it represents no already-held locks. diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index cfa60154278..7840a620912 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -867,6 +867,9 @@ LockErrorCleanup(void) GrantAwaitedLock(); } + /* An error can bypass ProcSleep()'s reset, so clear waitStart here */ + pg_atomic_write_u64(&MyProc->waitStart, 0); + ResetAwaitedLock(); LWLockRelease(partitionLock); @@ -1720,6 +1723,12 @@ ProcSleep(LOCALLOCK *locallock) } } while (myWaitStatus == PROC_WAIT_STATUS_WAITING); + /* + * Clear waitStart after the wait, as we may have set it after + * ProcWakeup() cleared it. + */ + pg_atomic_write_u64(&MyProc->waitStart, 0); + /* * Disable the timers, if they are still running. As in LockErrorCleanup, * we must preserve the LOCK_TIMEOUT indicator flag: if a lock timeout has [parent: 3004ac4220f3]