Refactor function pg_get_multixact_stats (src/backend/utils/adt/multixactfuncs.c)

From: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Refactor function pg_get_multixact_stats (src/backend/utils/adt/multixactfuncs.c)
Date: 2026-06-30 12:23:20
Message-ID: CAEudQAonQh7be=wOR-CJFW=bgMBz5wW_bv4t0OFxbgn-794JCQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi.

pg_get_multixact_stats: avoid computing stats with unprivileged
users

Previously, pg_get_multixact_stats() called GetMultiXactInfo() and
computed the derived "members" and "membersBytes" values
unconditionally, before checking whether the caller holds
privileges of pg_read_all_stats. When the caller lacks those
privileges, the computed values are simply discarded and the
function returns an all-NULL row instead, so the work was wasted on
every unprivileged call.

Move the GetMultiXactInfo() call and the resulting computations
into the branch where the values are actually consumed, narrowing
the scope of the related local variables to that branch. This
matches the usual pattern for privileged stats functions, which
skip the underlying work entirely for callers who can't see the
result, and avoids touching shared MultiXact state when there is no
caller around to observe it.

While here, size the memset() that NULLs the output row off
sizeof(nulls) rather than *sizeof(bool) * tupdesc->natts*.
The two are equivalent today, since nulls is a fixed 4-element array
matching the function's declared return type, but relying the
memset() with *natts* is an unnecessary dependency.
If a column were ever added to the function's SQL definition without a
matching update to this array, the old code would silently write
past the end of nulls.

best regards,
Ranier Vilela

Attachment Content-Type Size
refactor-multixact-stats-function-variables-scope.patch application/octet-stream 1.6 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Anton Ratundalov 2026-06-30 12:23:56 Using ForeignScan::fs_server to find user mapping
Previous Message Daniil Davydov 2026-06-30 12:01:20 Re: Fix bug with accessing to temporary tables of other sessions