Skip to content

Commit 92daeca

Browse files
committed
Add wait event for pg_usleep() in perform_spin_delay()
The lwlock wait queue scalability issue fixed in a4adc31 was quite hard to find because of the exponential backoff and because we adjust spins_per_delay over time within a backend. To make it easier to find similar issues in the future, add a wait event for the pg_usleep() in perform_spin_delay(). Showing a wait event while spinning without sleeping would increase the overhead of spinlocks, which we do not want. We may at some later point want to have more granular wait events, but that'd be a substantial amount of work. This provides at least some insights into something currently hard to observe. Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Robert Haas <robertmhaas@gmail.com> Reviewed-by: Alexander Korotkov <aekorotkov@gmail.com> https://postgr.es/m/20221120204310.xywrhyxyytsajuuq@awork3.anarazel.de
1 parent bd82928 commit 92daeca

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

doc/src/sgml/monitoring.sgml

+4
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,10 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
22892289
<entry>Waiting while sending synchronization requests to the
22902290
checkpointer, because the request queue is full.</entry>
22912291
</row>
2292+
<row>
2293+
<entry><literal>SpinDelay</literal></entry>
2294+
<entry>Waiting while acquiring a contended spinlock.</entry>
2295+
</row>
22922296
<row>
22932297
<entry><literal>VacuumDelay</literal></entry>
22942298
<entry>Waiting in a cost-based vacuum delay point.</entry>

src/backend/storage/lmgr/s_lock.c

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "common/pg_prng.h"
5454
#include "port/atomics.h"
5555
#include "storage/s_lock.h"
56+
#include "utils/wait_event.h"
5657

5758
#define MIN_SPINS_PER_DELAY 10
5859
#define MAX_SPINS_PER_DELAY 1000
@@ -136,7 +137,17 @@ perform_spin_delay(SpinDelayStatus *status)
136137
if (status->cur_delay == 0) /* first time to delay? */
137138
status->cur_delay = MIN_DELAY_USEC;
138139

140+
/*
141+
* Once we start sleeping, the overhead of reporting a wait event is
142+
* justified. Actively spinning easily stands out in profilers, but
143+
* sleeping with an exponential backoff is harder to spot...
144+
*
145+
* We might want to report something more granular at some point, but
146+
* this is better than nothing.
147+
*/
148+
pgstat_report_wait_start(WAIT_EVENT_SPIN_DELAY);
139149
pg_usleep(status->cur_delay);
150+
pgstat_report_wait_end();
140151

141152
#if defined(S_LOCK_TEST)
142153
fprintf(stdout, "*");

src/backend/utils/activity/wait_event.c

+3
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ pgstat_get_wait_timeout(WaitEventTimeout w)
497497
case WAIT_EVENT_REGISTER_SYNC_REQUEST:
498498
event_name = "RegisterSyncRequest";
499499
break;
500+
case WAIT_EVENT_SPIN_DELAY:
501+
event_name = "SpinDelay";
502+
break;
500503
case WAIT_EVENT_VACUUM_DELAY:
501504
event_name = "VacuumDelay";
502505
break;

src/include/utils/wait_event.h

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ typedef enum
145145
WAIT_EVENT_RECOVERY_APPLY_DELAY,
146146
WAIT_EVENT_RECOVERY_RETRIEVE_RETRY_INTERVAL,
147147
WAIT_EVENT_REGISTER_SYNC_REQUEST,
148+
WAIT_EVENT_SPIN_DELAY,
148149
WAIT_EVENT_VACUUM_DELAY,
149150
WAIT_EVENT_VACUUM_TRUNCATE
150151
} WaitEventTimeout;

0 commit comments

Comments
 (0)