mirror of
https://github.com/postgres/postgres.git
synced 2025-12-19 17:02:53 +03:00
lwlock: Fix quadratic behavior with very long wait lists
Until now LWLockDequeueSelf() sequentially searched the list of waiters to see
if the current proc is still is on the list of waiters, or has already been
removed. In extreme workloads, where the wait lists are very long, this leads
to a quadratic behavior. #backends iterating over a list #backends
long. Additionally, the likelihood of needing to call LWLockDequeueSelf() in
the first place also increases with the increased length of the wait queue, as
it becomes more likely that a lock is released while waiting for the wait list
lock, which is held for longer during lock release.
Due to the exponential back-off in perform_spin_delay() this is surprisingly
hard to detect. We should make that easier, e.g. by adding a wait event around
the pg_usleep() - but that's a separate patch.
The fix is simple - track whether a proc is currently waiting in the wait list
or already removed but waiting to be woken up in PGPROC->lwWaiting.
In some workloads with a lot of clients contending for a small number of
lwlocks (e.g. WALWriteLock), the fix can substantially increase throughput.
This has been originally fixed for 16~ with a4adc31f69 without a
backpatch, and we have heard complaints from users impacted by this
quadratic behavior in older versions as well.
Author: Andres Freund <andres@anarazel.de>
Reviewed-by: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Discussion: https://postgr.es/m/20221027165914.2hofzp4cvutj6gin@awork3.anarazel.de
Discussion: https://postgr.es/m/CALj2ACXktNbG=K8Xi7PSqbofTZozavhaxjatVc14iYaLu4Maag@mail.gmail.com
Backpatch-through: 12
This commit is contained in:
committed by
Michael Paquier
parent
90ccc9bf90
commit
f374fb4aab
@@ -23,6 +23,14 @@
|
||||
|
||||
struct PGPROC;
|
||||
|
||||
/* what state of the wait process is a backend in */
|
||||
typedef enum LWLockWaitState
|
||||
{
|
||||
LW_WS_NOT_WAITING, /* not currently waiting / woken up */
|
||||
LW_WS_WAITING, /* currently waiting */
|
||||
LW_WS_PENDING_WAKEUP /* removed from waitlist, but not yet signalled */
|
||||
} LWLockWaitState;
|
||||
|
||||
/*
|
||||
* Code outside of lwlock.c should not manipulate the contents of this
|
||||
* structure directly, but we have to declare it here to allow LWLocks to be
|
||||
|
||||
@@ -211,7 +211,7 @@ struct PGPROC
|
||||
bool recoveryConflictPending;
|
||||
|
||||
/* Info about LWLock the process is currently waiting for, if any. */
|
||||
bool lwWaiting; /* true if waiting for an LW lock */
|
||||
uint8 lwWaiting; /* see LWLockWaitState */
|
||||
uint8 lwWaitMode; /* lwlock mode being waited for */
|
||||
proclist_node lwWaitLink; /* position in LW lock wait list */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user