pgsql: lwlock: Fix quadratic behavior with very long wait lists

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: lwlock: Fix quadratic behavior with very long wait lists
Date: 2022-11-20 19:57:01
Message-ID: E1owqR2-0007My-Lk@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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.

As the quadratic behavior arguably is a bug, we might want to decide to
backpatch this fix in the future.

Author: Andres Freund <andres(at)anarazel(dot)de>
Reviewed-by: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
Discussion: https://postgr.es/m/20221027165914.2hofzp4cvutj6gin@awork3.anarazel.de
Discussion: https://postgr.es/m/CALj2ACXktNbG=K8Xi7PSqbofTZozavhaxjatVc14iYaLu4Maag@mail.gmail.com

Branch
------
master

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

Modified Files
--------------
src/backend/access/transam/twophase.c | 2 +-
src/backend/storage/lmgr/lwlock.c | 53 ++++++++++++++++++++---------------
src/backend/storage/lmgr/proc.c | 4 +--
src/include/storage/lwlock.h | 8 ++++++
src/include/storage/proc.h | 2 +-
5 files changed, 42 insertions(+), 27 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Amit Kapila 2022-11-21 03:37:01 pgsql: Add additional checks while creating the initial decoding snapsh
Previous Message Andres Freund 2022-11-20 19:00:13 pgsql: pgstat: replace double lookup with IsSharedRelation()