Subject: pgsql: Fix OldestMemberMXactId and OldestVisibleMXactId array usage

From: Heikki Linnakangas <[email-masked]>
Date: Mon, 02 Mar 2026 17:23:55 +0000

Fix OldestMemberMXactId and OldestVisibleMXactId array usage

Commit ab355e3a88 changed how the OldestMemberMXactId array is
indexed. It's no longer indexed by synthetic dummyBackendId, but with
ProcNumber. The PGPROC entries for prepared xacts come after auxiliary
processes in the allProcs array, which rendered the calculation for
MaxOldestSlot and the indexes into the array incorrect.  (The
OldestVisibleMXactId array is not used for prepared xacts, and thus
never accessed with ProcNumber's greater than MaxBackends, so this
only affects the OldestMemberMXactId array.)

As a result, a prepared xact would store its value past the end of the
OldestMemberMXactId array, overflowing into the OldestVisibleMXactId
array. That could cause a transaction's row lock to appear invisible
to other backends, or other such visibility issues. With a very small
max_connections setting, the store could even go beyond the
OldestVisibleMXactId array, stomping over the first element in the
BufferDescriptor array.

To fix, calculate the array sizes more precisely, and introduce helper
functions to calculate the array indexes correctly.

Author: Yura Sokolov <[email-masked]>
Reviewed-by: Sami Imseih <[email-masked]>
Reviewed-by: Chao Li <[email-masked]>
Discussion: https://www.postgresql.org/message-id/[email-masked]
Backpatch-through: 17

Branch
------
master

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

Modified Files
--------------
src/backend/access/transam/multixact.c | 131 +++++++++++++++++++++++----------
src/backend/access/transam/twophase.c  |   7 +-
src/backend/storage/lmgr/proc.c        |   4 +-
src/include/storage/proc.h             |   1 +
4 files changed, 99 insertions(+), 44 deletions(-)