sinvaladt.c: remove msgnumLock, use atomic operations on maxMsgNum
Yura Sokolov <y.sokolov@postgrespro.ru>
From: Yura Sokolov <y.sokolov@postgrespro.ru>
To: "pgsql-hackers@lists.postgresql.org" <pgsql-hackers@lists.postgresql.org>
Date: 2025-02-03T12:05:30Z
Lists: pgsql-hackers
Attachments
- v0-0001-sinvaladt.c-use-atomic-operations-on-maxMsgNum.patch (text/x-patch) patch v0-0001
Good day, Investigating some performance issues of a client, our engineers found msgnumLock to be contended. Looking closer it is obvious it is not needed at all: it used only as memory barrier. It is even stated in comment at file start: * We deal with that by having a spinlock that readers must take for just * long enough to read maxMsgNum, while writers take it for just long enough * to write maxMsgNum. (The exact rule is that you need the spinlock to * read maxMsgNum if you are not holding SInvalWriteLock, and you need the * spinlock to write maxMsgNum unless you are holding both locks.) * * Note: since maxMsgNum is an int and hence presumably atomically readable/ * writable, the spinlock might seem unnecessary. The reason it is needed * is to provide a memory barrier: we need to be sure that messages written * to the array are actually there before maxMsgNum is increased, and that * readers will see that data after fetching maxMsgNum. So we changed maxMsgNum to be pg_atomic_uint32, and put appropriate memory barriers: - pg_write_barrier() before write to maxMsgNum (when only SInvalWriteLock is held) - pg_read_barrier() after read of maxMsgNum (when only SInvalReadLock is held) It improved situation for our client. Note: pg_(write|read)_barrier() is chosen instead of pg_atomic_(read|write)_membarrier_u32() because it certainly cheaper at least on x86_64 where it is translated to just a compiler barrier (empty asm statement). At least pg_atomic_read_membarrier_u32() is implemented currently as a write operation, that's not good for contended place. ----- regards Yura Sokolov aka funny-falcon
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Use replay LSN as target for cascading logical WAL senders
- 5231ed8262c9 18.0 cited
-
doc PG 18 relnotes: adjust COPY and REJECT_LIMIT items
- b3754dcc9ff7 18.0 cited