[PATCH] Extend MXactCache lifetime from per-transaction to per-session
[PATCH] Extend MXactCache lifetime from per-transaction to per-session
The MultiXact member cache (MXactCache) is allocated under
TopTransactionContext and destroyed at every transaction end, so
cross-transaction lookups of the same MultiXactId always fall through
to SLRU. There is a FIXME comment in multixact.c (added in commit
0ac5ad5134f2) noting this is "plain wrong now that multixact's may
contain update Xids."
Since MultiXact member lists are immutable once created, cached entries
stay valid regardless of transaction boundaries. The attached patch
moves the cache to CacheMemoryContext so it persists for the backend
session, and adds an oldestMultiXactId staleness check at lookup time
to evict truncated entries.
The oldestMultiXactId read is lockless; a stale read just keeps an
entry slightly longer, which is harmless since the value only advances
monotonically. This is consistent with the existing lockless
MultiXactId read precedent in AtEOXact_MultiXact.
For PostPrepare_MultiXact, explicit MemoryContextDelete is added
since the context is no longer auto-destroyed with the transaction.
I confirmed the improvement via pg_stat_slru: repeated
GetMultiXactIdMembers calls across transactions go from SLRU hits
every time to zero additional SLRU accesses after the first lookup.
Patch attached.
TopTransactionContext and destroyed at every transaction end, so
cross-transaction lookups of the same MultiXactId always fall through
to SLRU. There is a FIXME comment in multixact.c (added in commit
0ac5ad5134f2) noting this is "plain wrong now that multixact's may
contain update Xids."
Since MultiXact member lists are immutable once created, cached entries
stay valid regardless of transaction boundaries. The attached patch
moves the cache to CacheMemoryContext so it persists for the backend
session, and adds an oldestMultiXactId staleness check at lookup time
to evict truncated entries.
The oldestMultiXactId read is lockless; a stale read just keeps an
entry slightly longer, which is harmless since the value only advances
monotonically. This is consistent with the existing lockless
MultiXactId read precedent in AtEOXact_MultiXact.
For PostPrepare_MultiXact, explicit MemoryContextDelete is added
since the context is no longer auto-destroyed with the transaction.
I confirmed the improvement via pg_stat_slru: repeated
GetMultiXactIdMembers calls across transactions go from SLRU hits
every time to zero additional SLRU accesses after the first lookup.
Patch attached.
Re: [PATCH] Extend MXactCache lifetime from per-transaction to per-session
От:
JoongHyuk Shin <sjh910805@gmail.com>
Дата:
On Mon, Apr 20, 2026 at 6:38 PM JoongHyuk Shin <sjh910805@gmail.com> wrote:
> Since MultiXact member lists are immutable once created, cached entries
> stay valid regardless of transaction boundaries.
MultiXactIds are assigned again after wraparound, so a backend that
stays connected across a wraparound can still hold a cache entry for an
old MultiXactId. If that backend then looks up a new MultiXact that
received the same MultiXactId, it may get the old member list back.
I think the patch needs additional design work to handle this case, and
after fixing that it would also need measurements showing a real
performance gain. So for now I have marked the CF entry as Withdrawn.
--
JoongHyuk Shin
> Since MultiXact member lists are immutable once created, cached entries
> stay valid regardless of transaction boundaries.
MultiXactIds are assigned again after wraparound, so a backend that
stays connected across a wraparound can still hold a cache entry for an
old MultiXactId. If that backend then looks up a new MultiXact that
received the same MultiXactId, it may get the old member list back.
I think the patch needs additional design work to handle this case, and
after fixing that it would also need measurements showing a real
performance gain. So for now I have marked the CF entry as Withdrawn.
--
JoongHyuk Shin