]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Avoid useless calls in pg_get_multixact_stats()
authorMichael Paquier <michael@paquier.xyz>
Wed, 1 Jul 2026 03:17:17 +0000 (12:17 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 1 Jul 2026 03:17:17 +0000 (12:17 +0900)
MultiXactOffsetStorageSize() and GetMultiXactInfo() are called to gather
the information reported by the function, but were wasteful for the case
where a role does not have the privileges of pg_read_all_stats, where we
return a set of NULLs.  These calls are moved to the code path where
their results are used.

Author: Ranier Vilela <ranier.vf@gmail.com>
Discussion: https://postgr.es/m/CAEudQAonQh7be=wOR-CJFW=bgMBz5wW_bv4t0OFxbgn-794JCQ@mail.gmail.com
Backpatch-through: 19

src/backend/utils/adt/multixactfuncs.c

index 9fe2ebafa73f6decd0b5e946786b2fe484f47715..d9aa58e821d9725acafd975924792c70598815b4 100644 (file)
@@ -102,23 +102,12 @@ pg_get_multixact_stats(PG_FUNCTION_ARGS)
        TupleDesc       tupdesc;
        Datum           values[4];
        bool            nulls[4];
-       uint64          members;
-       MultiXactId oldestMultiXactId;
-       uint32          multixacts;
-       MultiXactOffset oldestOffset;
-       MultiXactOffset nextOffset;
-       uint64          membersBytes;
 
        if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                 errmsg("return type must be a row type")));
 
-       GetMultiXactInfo(&multixacts, &nextOffset, &oldestMultiXactId, &oldestOffset);
-       members = nextOffset - oldestOffset;
-
-       membersBytes = MultiXactOffsetStorageSize(nextOffset, oldestOffset);
-
        if (!has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS))
        {
                /*
@@ -129,6 +118,17 @@ pg_get_multixact_stats(PG_FUNCTION_ARGS)
        }
        else
        {
+               uint64          members;
+               MultiXactId oldestMultiXactId;
+               uint32          multixacts;
+               MultiXactOffset oldestOffset;
+               MultiXactOffset nextOffset;
+               uint64          membersBytes;
+
+               GetMultiXactInfo(&multixacts, &nextOffset, &oldestMultiXactId, &oldestOffset);
+               members = nextOffset - oldestOffset;
+               membersBytes = MultiXactOffsetStorageSize(nextOffset, oldestOffset);
+
                values[0] = UInt32GetDatum(multixacts);
                values[1] = Int64GetDatum(members);
                values[2] = Int64GetDatum(membersBytes);