postgresql/src/include/storage
Michael Paquier 71e4cc6b8e Optimize WAL insertion lock acquisition and release with some atomics
The WAL insertion lock variable insertingAt is currently being read
and written with the help of the LWLock wait list lock to avoid any read
of torn values.  This wait list lock can become a point of contention on
a highly concurrent write workloads.

This commit switches insertingAt to a 64b atomic variable that provides
torn-free reads/writes.  On platforms without 64b atomic support, the
fallback implementation uses spinlocks to provide the same guarantees
for the values read.  LWLockWaitForVar(), through
LWLockConflictsWithVar(), reads the new value to check if it still needs
to wait with a u64 atomic operation.  LWLockUpdateVar() updates the
variable before waking up the waiters with an exchange_u64 (full memory
barrier).  LWLockReleaseClearVar() now uses also an exchange_u64 to
reset the variable.  Before this commit, all these steps relied on
LWLockWaitListLock() and LWLockWaitListUnlock().

This reduces contention on LWLock wait list lock and improves
performance of highly-concurrent write workloads.  Here are some
numbers using pg_logical_emit_message() (HEAD at d6677b93) with various
arbitrary record lengths and clients up to 1k on a rather-large machine
(64 vCPUs, 512GB of RAM, 16 cores per sockets, 2 sockets), in terms of
TPS numbers coming from pgbench:
 message_size_b     |     16 |     64 |    256 |   1024
--------------------+--------+--------+--------+-------
 patch_4_clients    |  83830 |  82929 |  80478 |  73131
 patch_16_clients   | 267655 | 264973 | 250566 | 213985
 patch_64_clients   | 380423 | 378318 | 356907 | 294248
 patch_256_clients  | 360915 | 354436 | 326209 | 263664
 patch_512_clients  | 332654 | 321199 | 287521 | 240128
 patch_1024_clients | 288263 | 276614 | 258220 | 217063
 patch_2048_clients | 252280 | 243558 | 230062 | 192429
 patch_4096_clients | 212566 | 213654 | 205951 | 166955
 head_4_clients     |  83686 |  83766 |  81233 |  73749
 head_16_clients    | 266503 | 265546 | 249261 | 213645
 head_64_clients    | 366122 | 363462 | 341078 | 261707
 head_256_clients   | 132600 | 132573 | 134392 | 165799
 head_512_clients   | 118937 | 114332 | 116860 | 150672
 head_1024_clients  | 133546 | 115256 | 125236 | 151390
 head_2048_clients  | 137877 | 117802 | 120909 | 138165
 head_4096_clients  | 113440 | 115611 | 120635 | 114361

Bharath has been measuring similar improvements, where the limit of the
WAL insertion lock begins to be felt when more than 256 concurrent
clients are involved in this specific workload.

An extra patch has been discussed to introduce a fast-exit path in
LWLockUpdateVar() when there are no waiters, still this does not
influence the write-heavy workload cases discussed as there are always
waiters.  This will be considered separately.

Author: Bharath Rupireddy
Reviewed-by: Nathan Bossart, Andres Freund, Michael Paquier
Discussion: https://postgr.es/m/CALj2ACVF+6jLvqKe6xhDzCCkr=rfd6upaGc3477Pji1Ke9G7Bg@mail.gmail.com
2023-07-25 13:38:58 +09:00
..
.gitignore When trace_lwlocks is used, identify individual lwlocks by name. 2015-09-11 14:01:39 -04:00
backendid.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
barrier.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
block.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
buf.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
buf_internals.h Add writeback to pg_stat_io 2023-05-17 11:18:35 -07:00
buffile.h Add BufFileRead variants with short read and EOF detection 2023-01-16 11:01:31 +01:00
bufmgr.h Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
bufpage.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
checksum.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
checksum_impl.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
condition_variable.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
copydir.h Constify the arguments of copydir.h functions 2023-01-18 08:55:26 +09:00
dsm.h Avoid type cheats for invalid dsa_handles and dshash_table_handles. 2023-01-25 11:48:38 -05:00
dsm_impl.h Avoid type cheats for invalid dsa_handles and dshash_table_handles. 2023-01-25 11:48:38 -05:00
fd.h Add io_direct setting (developer-only). 2023-04-08 16:35:07 +12:00
fileset.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
freespace.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
fsm_internals.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
indexfsm.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
ipc.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
item.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
itemid.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
itemptr.h Consolidate ItemPointer to Datum conversion functions 2023-02-13 09:57:15 +01:00
large_object.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
latch.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
lmgr.h Perform apply of large transactions by parallel workers. 2023-01-09 07:52:45 +05:30
lock.h Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
lockdefs.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
lwlock.h Optimize WAL insertion lock acquisition and release with some atomics 2023-07-25 13:38:58 +09:00
md.h Add smgrzeroextend(), FileZero(), FileFallocate() 2023-04-05 10:06:39 -07:00
meson.build Update copyright for 2023 2023-01-02 15:00:37 -05:00
off.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_sema.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_shmem.h Add GUC parameter "huge_pages_status" 2023-07-06 14:42:36 +09:00
pmsignal.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
predicate.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
predicate_internals.h Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
proc.h Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
procarray.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
proclist.h Constify proclist.h 2023-01-19 09:45:34 +01:00
proclist_types.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
procsignal.h Handle logical slot conflicts on standby 2023-04-08 00:05:44 -07:00
reinit.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
relfilelocator.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
s_lock.h Fix some typos and some incorrectly duplicated words 2023-04-18 14:03:49 +12:00
sharedfileset.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
shm_mq.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
shm_toc.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
shmem.h Remove SHM_QUEUE 2023-01-19 18:55:51 -08:00
sinval.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
sinvaladt.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
smgr.h Remove bogus #include added by d4e71df6d7. 2023-04-26 10:43:53 +12:00
spin.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
standby.h Remove vacuum_defer_cleanup_age 2023-04-24 12:21:02 -07:00
standbydefs.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
sync.h Update copyright for 2023 2023-01-02 15:00:37 -05:00