pg_stat_get_backend_subxact() and backend IDs?

Ian Lawrence Barwick <barwick@gmail.com>

From: Ian Lawrence Barwick <barwick@gmail.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2023-08-24T01:22:49Z
Lists: pgsql-hackers

Attachments

Hi

I was playing around with "pg_stat_get_backend_subxact()" (commit 10ea0f924)
and see it emits NULL values for some backends, e.g.:

    postgres=# \pset null NULL
    Null display is "NULL".

    postgres=#  SELECT id, pg_stat_get_backend_pid(id), s.*,
                       pg_stat_get_backend_activity (id)
                  FROM pg_stat_get_backend_idset() id
               JOIN LATERAL pg_stat_get_backend_subxact(id) AS s ON TRUE;
     id  | pg_stat_get_backend_pid | subxact_count |
subxact_overflowed |                pg_stat_get_backend_activity
    -----+-------------------------+---------------+--------------------+------------------------------------------------------------
       1 |                 3175972 |             0 | f
 | <command string not enabled>
       2 |                 3175973 |             0 | f
 | <command string not enabled>
       3 |                 3177889 |             0 | f
 | SELECT id, pg_stat_get_backend_pid(id), s.*,              +
         |                         |               |
 |         pg_stat_get_backend_activity (id)                 +
         |                         |               |
 |    FROM pg_stat_get_backend_idset() id                    +
         |                         |               |
 | JOIN LATERAL pg_stat_get_backend_subxact(id) AS s ON TRUE;
       4 |                 3176027 |             5 | f
 | savepoint s4;
     256 |                 3175969 |          NULL | NULL
 | <command string not enabled>
     258 |                 3175968 |          NULL | NULL
 | <command string not enabled>
     259 |                 3175971 |          NULL | NULL
 | <command string not enabled>
    (7 rows)

Reading through the thread [1], it looks like 0/false are intended to be
returned for non-backend processes too [2], so it seems odd that NULL/NULL is
getting returned in some cases, especially as that's what's returned if a
non-existent backend ID is provided.

[1] https://www.postgresql.org/message-id/flat/CAFiTN-uvYAofNRaGF4R%2Bu6_OrABdkqNRoX7V6%2BPP3H_0HuYMwg%40mail.gmail.com
[2] https://www.postgresql.org/message-id/flat/CAFiTN-ut0uwkRJDQJeDPXpVyTWD46m3gt3JDToE02hTfONEN%3DQ%40mail.gmail.com#821f6f40e91314066390efd06d71d5ac

Looking at the code, this is happening because
"pgstat_fetch_stat_local_beentry()"
expects to be passed the backend ID as an integer representing a 1-based index
referring to "localBackendStatusTable", but "pg_stat_get_backend_subxact()"
is presumably intended to take the actual BackendId , as per other
"pg_stat_get_XXX()"
functions.

Also, the comment for "pgstat_fetch_stat_local_beentry()" says:

   Returns NULL if the argument is out of range (no current caller does that).

so the last part is currently incorrect.

Assuming I am not misunderstanding something here (always a
possibility, apologies
in advance if this is merely noise), what is actually needed is a function which
accepts a BackendId (as per "pgstat_fetch_stat_beentry()"), but returns a
LocalPgBackendStatus (as per "pgstat_fetch_stat_local_beentry()") like the
attached, clumsily named "pgstat_fetch_stat_backend_local_beentry()".

Regards

Ian Barwick

Commits

  1. Use actual backend IDs in pg_stat_get_backend_subxact().

  2. Rename some support functions for pgstat* views.

  3. Expose some information about backend subxact status.