Re: Add a permission check to pg_stat_get_backend_subxact()
Re: Add a permission check to pg_stat_get_backend_subxact()
On Sat, Sep 12, 2026 at 08:43:28AM -0400, shihao zhong wrote: > Thanks for committing that, I will not include 0001 in the following emails. Fixed the subxact_overflow -> subxact_overflowed, as that's independent. > 1. The first test block ran as superuser, so the owner branch of > HAS_PGSTAT_PERMISSIONS() was never exercised: with "userid" forced to > InvalidOid the test still passed. The block now grants the test role > membership in the session's role instead. With that, forcing userid > to InvalidOid fails the test, and removing the checks fails the > "unrelated role" block. > > 2. The doc paragraph above the per-backend table said the functions > "return NULL", but activity/wait_event return " privilege>" and the SRFs return no rows. Reworded. > > 3. Commit message: noted that processes owned by no role (autovacuum > workers, WAL writer, ...) are now visible only to superusers and > pg_read_all_stats, as in pg_stat_activity, and that no backpatch is > done. That seems globally sensible, at quick glance. I am also adding Bertrand Drouvot in CC to comment about this change, as he has worked on three of these functions. @Bertrand, what do you think? -- Michael
Re: Add a permission check to pg_stat_get_backend_subxact()
От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi, On Mon, Sep 14, 2026 at 04:06:04PM +0900, Michael Paquier wrote: > On Sat, Sep 12, 2026 at 08:43:28AM -0400, shihao zhong wrote: > > Thanks for committing that, I will not include 0001 in the following emails. > > Fixed the subxact_overflow -> subxact_overflowed, as that's > independent. > > > 1. The first test block ran as superuser, so the owner branch of > > HAS_PGSTAT_PERMISSIONS() was never exercised: with "userid" forced to > > InvalidOid the test still passed. The block now grants the test role > > membership in the session's role instead. With that, forcing userid > > to InvalidOid fails the test, and removing the checks fails the > > "unrelated role" block. > > > > 2. The doc paragraph above the per-backend table said the functions > > "return NULL", but activity/wait_event return " > privilege>" and the SRFs return no rows. Reworded. > > > > 3. Commit message: noted that processes owned by no role (autovacuum > > workers, WAL writer, ...) are now visible only to superusers and > > pg_read_all_stats, as in pg_stat_activity, and that no backpatch is > > done. > > That seems globally sensible, at quick glance. I am also adding > Bertrand Drouvot in CC to comment about this change, as he has worked > on three of these functions. > > @Bertrand, what do you think? pg_stat_io, pg_stat_wal and pg_stat_lock expose aggregate statistics without restrictions but as pg_stat_get_backend_io(), pg_stat_get_backend_wal() and pg_stat_get_backend_lock() expose the stats for a particular backend, I think the proposed patch makes sense. One thing I noticed while looking at this is that with stats_fetch_consistency = snapshot, pgstat_fetch_stat_backend_by_pid() could validate the PID and user from one backend while returning cumulative statistics cached for an older backend that used the same ProcNumber. The race is not introduced by this patch, but the new permission check makes it more relevant here. Worth to fix at the same time? Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com
Re: Add a permission check to pg_stat_get_backend_subxact()
Hi Bertrand,
> Worth to fix at the same time?
Thanks, I can reproduce it. A role with no privileges reads the IO and WAL
v5-0002 fixes it by making the PID part of the object ID of the backend
stats entries. The entry of an older backend is then never found with
the PID of a newer one, and nothing needs to be checked after the fetch.
Entries are still dropped at exit, so the number of live entries is still
bounded by the number of proc numbers.
It is a separate patch as the problem has existed since v18.
v5-0001 is v4 rebased, without the tuple descriptor fix that Michael has
applied.
Thanks,
Shihao
> Worth to fix at the same time?
Thanks, I can reproduce it. A role with no privileges reads the IO and WAL
numbers of a superuser session under the PID of its own session.
v5-0002 fixes it by making the PID part of the object ID of the backend
stats entries. The entry of an older backend is then never found with
the PID of a newer one, and nothing needs to be checked after the fetch.
Entries are still dropped at exit, so the number of live entries is still
bounded by the number of proc numbers.
It is a separate patch as the problem has existed since v18.
v5-0001 is v4 rebased, without the tuple descriptor fix that Michael has
applied.
Thanks,
Shihao
On Mon, Sep 21, 2026 at 5:58 AM Bertrand Drouvot <bertranddrouvot.pg@gmail.com> wrote:
Hi,
On Mon, Sep 14, 2026 at 04:06:04PM +0900, Michael Paquier wrote:
> On Sat, Sep 12, 2026 at 08:43:28AM -0400, shihao zhong wrote:
> > Thanks for committing that, I will not include 0001 in the following emails.
>
> Fixed the subxact_overflow -> subxact_overflowed, as that's
> independent.
>
> > 1. The first test block ran as superuser, so the owner branch of
> > HAS_PGSTAT_PERMISSIONS() was never exercised: with "userid" forced to
> > InvalidOid the test still passed. The block now grants the test role
> > membership in the session's role instead. With that, forcing userid
> > to InvalidOid fails the test, and removing the checks fails the
> > "unrelated role" block.
> >
> > 2. The doc paragraph above the per-backend table said the functions
> > "return NULL", but activity/wait_event return "<insufficient
> > privilege>" and the SRFs return no rows. Reworded.
> >
> > 3. Commit message: noted that processes owned by no role (autovacuum
> > workers, WAL writer, ...) are now visible only to superusers and
> > pg_read_all_stats, as in pg_stat_activity, and that no backpatch is
> > done.
>
> That seems globally sensible, at quick glance. I am also adding
> Bertrand Drouvot in CC to comment about this change, as he has worked
> on three of these functions.
>
> @Bertrand, what do you think?
pg_stat_io, pg_stat_wal and pg_stat_lock expose aggregate statistics without
restrictions but as pg_stat_get_backend_io(), pg_stat_get_backend_wal() and
pg_stat_get_backend_lock() expose the stats for a particular backend, I think the
proposed patch makes sense.
One thing I noticed while looking at this is that with stats_fetch_consistency = snapshot,
pgstat_fetch_stat_backend_by_pid() could validate the PID and user from one backend
while returning cumulative statistics cached for an older backend that used the
same ProcNumber.
The race is not introduced by this patch, but the new permission check makes it
more relevant here. Worth to fix at the same time?
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
Re: Add a permission check to pg_stat_get_backend_subxact()
On Mon, Sep 21, 2026 at 07:54:04PM -0400, shihao zhong wrote: > v5-0002 fixes it by making the PID part of the object ID of the backend > stats entries. The entry of an older backend is then never found with > the PID of a newer one, and nothing needs to be checked after the fetch. > Entries are still dropped at exit, so the number of live entries is still > bounded by the number of proc numbers. + * Object ID of the stats entry of a backend. The PID is part of the key, so + * that an entry cached for an older backend that used the same proc number, + * for example in a stats snapshot, is never mistaken for the entry of the + * backend currently using this proc number. + */ +#define PGSTAT_BACKEND_OBJID(pid, procnum) \ + ((((uint64) (uint32) (pid)) << 32) | (uint32) (procnum)) This breaks the fundamental law of the backend stats and makes the whole more brittle. Having *only* the procnum in the key naturally caps the maximum amount of shared memory used by this stats kind because they would be recycled when connecting a new backend. You are removing this cap, so your patch means a lot of potential bloat on a live server in the shared hash table used by pgstats, the more bloat the more connection turnover. One approach that may be saner is to store a trace of the PID in PgStat_Backend when a new backend connects, then compare it back with the existing PGPROC entry, then decide what to show based on the state of both. That should be much cheaper, and much lower in shared memory footprint than what you are suggesting. -- Michael
Re: Add a permission check to pg_stat_get_backend_subxact()
Hi Michael,
> One approach that may be saner is to store a trace of the PID in
> PgStat_Backend when a new backend connects, then compare it back with
> the existing PGPROC entry, then decide what to show based on the state
> of both.
> One approach that may be saner is to store a trace of the PID in
> PgStat_Backend when a new backend connects, then compare it back with
> the existing PGPROC entry, then decide what to show based on the state
> of both.
Thanks for pointing that out. Applied.
0001 is unchanged.
Thanks,
Shihao
Thanks,
Shihao
Re: Add a permission check to pg_stat_get_backend_subxact()
On Mon, Sep 21, 2026 at 09:58:27AM +0000, Bertrand Drouvot wrote: > One thing I noticed while looking at this is that with stats_fetch_consistency = snapshot, > pgstat_fetch_stat_backend_by_pid() could validate the PID and user from one backend > while returning cumulative statistics cached for an older backend that used the > same ProcNumber. I was wondering about the validity of this argument regarding a possible split of 0001 and 0002. And this points to the fact that 0001 is simply unsafe: we should not do a HAS_PGSTAT_PERMISSIONS() based on the user ID retrieved from a beentry as it may refer to a role different than the one associated to the stats data depending on the level of stats consistency. I don't see a way through here except by storing more information into the stats entries themselves to use in the checks. Another option than the PID would be to store the user ID in the stats entry and reuse it for the HAS_PGSTAT_PERMISSIONS() check? If we do that, a snapshot of the data would still be able to work even if the proc slot is reused due to a slot being recycled, and that could be arguably more useful than the PID (a session willing a snapshot of the data would still be able to refer to it based on a past point)? I'd slightly prefer storing a user ID, I think, because it means that the ACL check is done only based on the stats data, and there would be no cross-dependency between the data in the beentry and the stats data. Perhaps you have a different view or more ideas? -- Michael
Re: Add a permission check to pg_stat_get_backend_subxact()
От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi, On Tue, Sep 22, 2026 at 06:24:36PM +0900, Michael Paquier wrote: > On Mon, Sep 21, 2026 at 09:58:27AM +0000, Bertrand Drouvot wrote: > > One thing I noticed while looking at this is that with stats_fetch_consistency = snapshot, > > pgstat_fetch_stat_backend_by_pid() could validate the PID and user from one backend > > while returning cumulative statistics cached for an older backend that used the > > same ProcNumber. > > I'd slightly prefer storing a user ID, I think, because it means that > the ACL check is done only based on the stats data, and there would be > no cross-dependency between the data in the beentry and the stats > data. Perhaps you have a different view or more ideas? Yeah, storing the user ID in PgStat_Backend and using it for the ACL check makes sense to me. I'm not sure the user ID alone is enough though: if B reuses A's ProcNumber, pg_stat_get_backend_wal(B_pid) could still return A's cached statistics when the caller is allowed to see A's data. I'd keep the PID check from 0002 as well. A generation would be more robust against PID reuse, as done for example for AIO handles, but introducing a backend generation seems like too much for this case. So storing both seems like the simplest approach: the user ID for the ACL check and the PID for matching the statistics to the requested backend. Thoughts? Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com
Re: Add a permission check to pg_stat_get_backend_subxact()
On Tue, Sep 22, 2026 at 10:03:10AM +0000, Bertrand Drouvot wrote: > I'm not sure the user ID alone is enough though: if B reuses A's ProcNumber, > pg_stat_get_backend_wal(B_pid) could still return A's cached statistics when > the caller is allowed to see A's data. > > I'd keep the PID check from 0002 as well. A generation would be more robust > against PID reuse, as done for example for AIO handles, but introducing a > backend generation seems like too much for this case. > > So storing both seems like the simplest approach: the user ID for the ACL > check and the PID for matching the statistics to the requested backend. The PID would also act as a kind of weaker generation number, slightly weaker but simpler. So that works here. Perhaps you would like to give it a shot? -- Michael
Re: Add a permission check to pg_stat_get_backend_subxact()
От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi, On Tue, Sep 22, 2026 at 08:23:54PM +0900, Michael Paquier wrote: > On Tue, Sep 22, 2026 at 10:03:10AM +0000, Bertrand Drouvot wrote: > > I'm not sure the user ID alone is enough though: if B reuses A's ProcNumber, > > pg_stat_get_backend_wal(B_pid) could still return A's cached statistics when > > the caller is allowed to see A's data. > > > > I'd keep the PID check from 0002 as well. A generation would be more robust > > against PID reuse, as done for example for AIO handles, but introducing a > > backend generation seems like too much for this case. > > > > So storing both seems like the simplest approach: the user ID for the ACL > > check and the PID for matching the statistics to the requested backend. > > The PID would also act as a kind of weaker generation number, slightly > weaker but simpler. So that works here. Perhaps you would like to > give it a shot? Do you mean adding the user ID on top of Shihao's 0002? If so, I can have a look, unless Shihao is already planning to update the patch along those lines? Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com
Re: Add a permission check to pg_stat_get_backend_subxact()
> Do you mean adding the user ID on top of Shihao's 0002? If so, I can have a look,
> unless Shihao is already planning to update the patch along those lines?
Done in v8, attached.
0001 is the PID check, same as v7-0002. It goes first now because 0002
needs a field in PgStat_Backend.
0002 stores the user ID in PgStat_Backend next to the PID when the entry
is created, and pg_stat_get_backend_io(), wal() and lock() check the
caller against that instead of the beentry. The reset callback restores
both fields. pg_stat_get_backend_subxact() still checks the beentry, the
counters it reports come from there.
> unless Shihao is already planning to update the patch along those lines?
Done in v8, attached.
0001 is the PID check, same as v7-0002. It goes first now because 0002
needs a field in PgStat_Backend.
0002 stores the user ID in PgStat_Backend next to the PID when the entry
is created, and pg_stat_get_backend_io(), wal() and lock() check the
caller against that instead of the beentry. The reset callback restores
both fields. pg_stat_get_backend_subxact() still checks the beentry, the
counters it reports come from there.
Thanks,
Shihao
Re: Add a permission check to pg_stat_get_backend_subxact()
От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,
On Tue, Sep 22, 2026 at 11:22:40PM -0400, shihao zhong wrote:
> > Do you mean adding the user ID on top of Shihao's 0002? If so, I can have
> a look,
> > unless Shihao is already planning to update the patch along those lines?
>
> Done in v8, attached.
Thanks!
I've a few comments:
> 0001 is the PID check, same as v7-0002. It goes first now because 0002
> needs a field in PgStat_Backend.
>
> 0002 stores the user ID in PgStat_Backend next to the PID when the entry
> is created, and pg_stat_get_backend_io(), wal() and lock() check the
> caller against that instead of the beentry. The reset callback restores
> both fields. pg_stat_get_backend_subxact() still checks the beentry, the
> counters it reports come from there.
=== 1
pgstat_read_current_status() first copies the activity entry and then calls
ProcNumberGetTransactionIds() separately. If the backend exits and its
ProcNumber is reused in between, the userid can belong to the old backend
while the subxact counters belong to the new one.
This race exists before the patch, but it matters for the new permission check.
I wonder if we should pass the copied PID to ProcNumberGetTransactionIds() and
validate it under ProcArrayLock, following the same idea as
pgstat_fetch_stat_backend_by_pid()?
=== 2
typedef struct PgStat_Backend
{
+ int pid; /* PID of the backend owning these stats */
TimestampTz stat_reset_timestamp;
0002 explicitly says that it is not intended for backpatching, but what about
0001? If it is backpatched to v18, adding pid here changes the offsets of all
the existing fields.
I think it would make sense to add pid at the end in the backbranches (if we
back patch it), as suggested in [1]. That would preserve the existing field
offsets, though it would still change sizeof(PgStat_Backend). FWIW, I could not
find any use of sizeof(PgStat_Backend) in a GitHub code search and there is
no padding to add the new field into.
[1]: https://wiki.postgresql.org/wiki/Committing_checklist
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
Re: Add a permission check to pg_stat_get_backend_subxact()
Hi Bertrand,
> I wonder if we should pass the copied PID to ProcNumberGetTransactionIds() and
> validate it under ProcArrayLock, following the same idea as
> pgstat_fetch_stat_backend_by_pid()?
Yes, done in v9-0002, a new patch. pgstat_read_current_status() passes the
PID it copied, and ProcNumberGetTransactionIds() reports nothing when the
PGPROC entry holds another PID. It changes an exported function, so I
would keep it for HEAD.
> I think it would make sense to add pid at the end in the backbranches (if we
> back patch it), as suggested in [1].
0001 fixes a misreport in 18, so I think it should go there too. v9 puts
the PID at the end of PgStat_Backend, and 0003 puts the user ID after it,
so the existing offsets do not change.
> I wonder if we should pass the copied PID to ProcNumberGetTransactionIds() and
> validate it under ProcArrayLock, following the same idea as
> pgstat_fetch_stat_backend_by_pid()?
Yes, done in v9-0002, a new patch. pgstat_read_current_status() passes the
PID it copied, and ProcNumberGetTransactionIds() reports nothing when the
PGPROC entry holds another PID. It changes an exported function, so I
would keep it for HEAD.
> I think it would make sense to add pid at the end in the backbranches (if we
> back patch it), as suggested in [1].
0001 fixes a misreport in 18, so I think it should go there too. v9 puts
the PID at the end of PgStat_Backend, and 0003 puts the user ID after it,
so the existing offsets do not change.
0003 is v8-0002 with only that move.'
Thanks,
Shihao
Re: Add a permission check to pg_stat_get_backend_subxact()
On Wed, Sep 23, 2026 at 07:23:01AM +0000, Bertrand Drouvot wrote:
> typedef struct PgStat_Backend
> {
> + int pid; /* PID of the backend owning these stats */
> TimestampTz stat_reset_timestamp;
>
> 0002 explicitly says that it is not intended for backpatching, but what about
> 0001? If it is backpatched to v18, adding pid here changes the offsets of all
> the existing fields.
One thing that itches me quite a bit about adding a cross-check of the
PID in the set of checks is that it also changes the behavior of
stats_fetch_consistency when set to "cache" or "snapshot", by forcibly
discarding the stats numbers previously fetched when requesting stats
for a PID that matches with a previous procnumber slot, so it is just
switching from one behavior to another. In terms of implementation
simplicity, I see more merit with the existing logic where we don't
add a PID tracking because it's well, simpler! And the problem with
these numbers only becomes a problem if we deal with a connection
turnover that it impacts the numbers obtained. The main use case of
backend stats is for benchmarking and get numbers with longer-running
connections, so as a whole I think that we are making a big issue of
something that is not really one in practice.
Note that there is a parallel with replication slot stats, which are
indexed not by name but with an integer number. A backend could grab
in a snapshot data from slot 1, while concurrent activity has the idea
to drop and recreate a slot. The snapshot would still refer to the
data of the previous slot. If we aim at improving this kind of use
cases with stats snapshots, and I am not sure that it's really worth
bothering, this should work across all the stats kinds, not be plugged
multiple times across the board.
The role ID case is different: we want consistency to check for the
permissions.
--
Michael
Re: Add a permission check to pg_stat_get_backend_subxact()
Hi Michael,
> In terms of implementation simplicity, I see more merit with the existing
> logic where we don't add a PID tracking because it's well, simpler!
OK, v10 drops the PID from the stats entry. v10-0001 is the permission
patch, and the entry stores only the role of the backend. That is enough
for the snapshot case upthread. The old entry keeps the role of the old
backend, so u1 gets nothing from it. I checked it with the same script.
> The role ID case is different: we want consistency to check for the
> permissions.
v10-0002 is optional and follows that idea for Bertrand's point on
pg_stat_get_backend_subxact(). The role is checked from the copied status
entry, but the subxact counters are read from PGPROC in a second step.
The PID check makes sure both come from the same backend. Your call.
Thanks,
Shihao
> In terms of implementation simplicity, I see more merit with the existing
> logic where we don't add a PID tracking because it's well, simpler!
OK, v10 drops the PID from the stats entry. v10-0001 is the permission
patch, and the entry stores only the role of the backend. That is enough
for the snapshot case upthread. The old entry keeps the role of the old
backend, so u1 gets nothing from it. I checked it with the same script.
> The role ID case is different: we want consistency to check for the
> permissions.
v10-0002 is optional and follows that idea for Bertrand's point on
pg_stat_get_backend_subxact(). The role is checked from the copied status
entry, but the subxact counters are read from PGPROC in a second step.
The PID check makes sure both come from the same backend. Your call.
Thanks,
Shihao
Re: Add a permission check to pg_stat_get_backend_subxact()
От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,
On Thu, Sep 24, 2026 at 10:01:02AM +0900, Michael Paquier wrote:
> On Wed, Sep 23, 2026 at 07:23:01AM +0000, Bertrand Drouvot wrote:
> > typedef struct PgStat_Backend
> > {
> > + int pid; /* PID of the backend owning these stats */
> > TimestampTz stat_reset_timestamp;
> >
> > 0002 explicitly says that it is not intended for backpatching, but what about
> > 0001? If it is backpatched to v18, adding pid here changes the offsets of all
> > the existing fields.
>
> The main use case of
> backend stats is for benchmarking and get numbers with longer-running
> connections, so as a whole I think that we are making a big issue of
> something that is not really one in practice.
I'm not sure I agree with that assumption. Per-backend statistics can also be
used for monitoring and diagnostics, including with high connection turnover.
That said, I agree that ProcNumber reuse while statistics are held should be rare
in practice.
> Note that there is a parallel with replication slot stats, which are
> indexed not by name but with an integer number. A backend could grab
> in a snapshot data from slot 1, while concurrent activity has the idea
> to drop and recreate a slot. The snapshot would still refer to the
> data of the previous slot. If we aim at improving this kind of use
> cases with stats snapshots, and I am not sure that it's really worth
> bothering, this should work across all the stats kinds, not be plugged
> multiple times across the board.
Your replication slot point makes sense. If we want to address those, a generic
approach would probably be better than special handling for backend statistics.
Storing the user ID with the statistics is enough for the permission check, so
dropping the PID from v10-0001 makes sense to me.
Maybe worth adding a comment to pgstat_fetch_stat_backend_by_pid() mentioning that
cached statistics may belong to an older backend that used the same ProcNumber,
to avoid this being rediscovered later? (If so, I'll draft such a patch).
> The role ID case is different: we want consistency to check for the
> permissions.
Agreed. What about v10-0002? I think this is different. pgstat_read_current_status()
is constructing one activity snapshot entry from the activity entry and
PGPROC. Without the cross check, it may combine the PID and user ID of one
backend with the subxact counters of another one. Is the PID check there worth
keeping for HEAD?
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
Re: Add a permission check to pg_stat_get_backend_subxact()
Hi Bertrand,
> Maybe worth adding a comment to pgstat_fetch_stat_backend_by_pid() mentioning that
> cached statistics may belong to an older backend that used the same ProcNumber,
> to avoid this being rediscovered later? (If so, I'll draft such a patch).
Thanks, I added it to v11-0001, so no need for a separate patch. v11-0002
is the same as v10-0002.
> Agreed. What about v10-0002? I think this is different.
One more point for Michael. 0002 does not change stats_fetch_consistency
or drop anything fetched earlier. It only checks, while the activity
snapshot is built, that each entry takes its status and its PGPROC fields
from the same backend. If the PID does not match, that backend has exited,
so no xid and no subxacts is the right answer for it.
Thanks,
Shihao
> Maybe worth adding a comment to pgstat_fetch_stat_backend_by_pid() mentioning that
> cached statistics may belong to an older backend that used the same ProcNumber,
> to avoid this being rediscovered later? (If so, I'll draft such a patch).
Thanks, I added it to v11-0001, so no need for a separate patch. v11-0002
is the same as v10-0002.
> Agreed. What about v10-0002? I think this is different.
One more point for Michael. 0002 does not change stats_fetch_consistency
or drop anything fetched earlier. It only checks, while the activity
snapshot is built, that each entry takes its status and its PGPROC fields
from the same backend. If the PID does not match, that backend has exited,
so no xid and no subxacts is the right answer for it.
Thanks,
Shihao
Re: Add a permission check to pg_stat_get_backend_subxact()
On Thu, Sep 24, 2026 at 11:40:01PM -0400, shihao zhong wrote: > One more point for Michael. 0002 does not change stats_fetch_consistency > or drop anything fetched earlier. It only checks, while the activity > snapshot is built, that each entry takes its status and its PGPROC fields > from the same backend. If the PID does not match, that backend has exited, > so no xid and no subxacts is the right answer for it. Let me doubt you here. For one, it makes the API contract of ProcNumberGetTransactionIds() much harder to think about. -- Michael
Re: Add a permission check to pg_stat_get_backend_subxact()
Hi Michael,
> Let me doubt you here. For one, it makes the API contract of
> ProcNumberGetTransactionIds() much harder to think about.
Fair enough, let's drop 0002. Attached is v12, which is v11-0001 alone
and unchanged.
Thanks,
Shihao
> Let me doubt you here. For one, it makes the API contract of
> ProcNumberGetTransactionIds() much harder to think about.
Fair enough, let's drop 0002. Attached is v12, which is v11-0001 alone
and unchanged.
Thanks,
Shihao
Re: Add a permission check to pg_stat_get_backend_subxact()
От:
solai v <solai.cdac@gmail.com>
Дата:
Hi all, On Thu, Sep 24, 2026 at 12:03 PM shihao zhong wrote: > > Hi Michael, > > > In terms of implementation simplicity, I see more merit with the existing > > logic where we don't add a PID tracking because it's well, simpler! > > OK, v10 drops the PID from the stats entry. v10-0001 is the permission > patch, and the entry stores only the role of the backend. That is enough > for the snapshot case upthread. The old entry keeps the role of the old > backend, so u1 gets nothing from it. I checked it with the same script. > > > The role ID case is different: we want consistency to check for the > > permissions. > > v10-0002 is optional and follows that idea for Bertrand's point on > pg_stat_get_backend_subxact(). The role is checked from the copied status > entry, but the subxact counters are read from PGPROC in a second step. > The PID check makes sure both come from the same backend. Your call. > > Thank you for the updated patches. I tested the patch and the regression suite completed successfully with all 239 tests passing. I also performed targeted testing of the per-backend statistics permission checks. The results were as expected: 1. The role owning the backend could access the per-backend statistics. 2. An unrelated role could not access the statistics. 3. A role with pg_read_all_stats privileges could access them again. I reviewed the changes for pg_stat_get_backend_subxact(), pg_stat_get_backend_io(), pg_stat_get_backend_wal(), and pg_stat_get_backend_lock(), and verified that the permission checks are applied consistently. I also checked the PID validation in pgstat_fetch_stat_backend_by_pid(). The requested PID is compared with st_procpid before the backend statistics entry is fetched, which addresses the PID/ProcNumber consistency concern discussed in the thread. Overall, I did not find any additional issues during my testing and review. The patch looks relevant to the reported issue from my testing. Regards, Solai
Re: Add a permission check to pg_stat_get_backend_subxact()
От:
solai v <solai.cdac@gmail.com>
Дата:
Hi all, On Fri, Sep 25, 2026 at 12:04 PM shihao zhong wrote: > > Hi Michael, > > > Let me doubt you here. For one, it makes the API contract of > > ProcNumberGetTransactionIds() much harder to think about. > > Fair enough, let's drop 0002. Attached is v12, which is v11-0001 alone > and unchanged. > Thank you for the updated v12 patch. I tested the patch, and the regression suite completed successfully. I went through the permission checks for the per-backend statistics functions. The added regression tests behaved as expected for the backend owner, an unrelated role, and a role with pg_read_all_stats privileges. I reviewed the userid handling in the backend statistics entry as well, including how the owner information is preserved when the statistics are reset. I also checked and verified the added comment around ProcNumber reuse and cached statistics. I went through the documentation and verified the changes done to pg_stat_get_backend_subxact(), pg_stat_get_backend_io(), pg_stat_get_backend_wal(), and pg_stat_get_backend_lock(). All the changes look correct to me and I did not find any other issues based on my testing and review. Regards. Solai