diff options
author | Heikki Linnakangas | 2024-03-03 17:37:28 +0000 |
---|---|---|
committer | Heikki Linnakangas | 2024-03-03 17:37:28 +0000 |
commit | ab355e3a88de745607f6dd4c21f0119b5c68f2ad (patch) | |
tree | 7808e71e8fbee29095708f47f80bb2bea099e52e /src/backend/utils/activity/backend_status.c | |
parent | 30b8d6e4ce1112168ddfe8cdbba76fbefd304b34 (diff) |
Redefine backend ID to be an index into the proc array
Previously, backend ID was an index into the ProcState array, in the
shared cache invalidation manager (sinvaladt.c). The entry in the
ProcState array was reserved at backend startup by scanning the array
for a free entry, and that was also when the backend got its backend
ID. Things become slightly simpler if we redefine backend ID to be the
index into the PGPROC array, and directly use it also as an index to
the ProcState array. This uses a little more memory, as we reserve a
few extra slots in the ProcState array for aux processes that don't
need them, but the simplicity is worth it.
Aux processes now also have a backend ID. This simplifies the
reservation of BackendStatusArray and ProcSignal slots.
You can now convert a backend ID into an index into the PGPROC array
simply by subtracting 1. We still use 0-based "pgprocnos" in various
places, for indexes into the PGPROC array, but the only difference now
is that backend IDs start at 1 while pgprocnos start at 0. (The next
commmit will get rid of the term "backend ID" altogether and make
everything 0-based.)
There is still a 'backendId' field in PGPROC, now part of 'vxid' which
encapsulates the backend ID and local transaction ID together. It's
needed for prepared xacts. For regular backends, the backendId is
always equal to pgprocno + 1, but for prepared xact PGPROC entries,
it's the ID of the original backend that processed the transaction.
Reviewed-by: Andres Freund, Reid Thompson
Discussion: https://www.postgresql.org/message-id/[email protected]
Diffstat (limited to 'src/backend/utils/activity/backend_status.c')
-rw-r--r-- | src/backend/utils/activity/backend_status.c | 52 |
1 files changed, 17 insertions, 35 deletions
diff --git a/src/backend/utils/activity/backend_status.c b/src/backend/utils/activity/backend_status.c index 1a1050c8da1..3d3f7b06723 100644 --- a/src/backend/utils/activity/backend_status.c +++ b/src/backend/utils/activity/backend_status.c @@ -19,6 +19,7 @@ #include "port/atomics.h" /* for memory barriers */ #include "storage/ipc.h" #include "storage/proc.h" /* for MyProc */ +#include "storage/procarray.h" #include "storage/sinvaladt.h" #include "utils/ascii.h" #include "utils/backend_status.h" @@ -29,13 +30,12 @@ /* ---------- * Total number of backends including auxiliary * - * We reserve a slot for each possible BackendId, plus one for each - * possible auxiliary process type. (This scheme assumes there is not - * more than one of any auxiliary process type at a time.) MaxBackends - * includes autovacuum workers and background workers as well. + * We reserve a slot for each possible PGPROC entry, including aux processes. + * (But not including PGPROC entries reserved for prepared xacts; they are not + * real processes.) * ---------- */ -#define NumBackendStatSlots (MaxBackends + NUM_AUXPROCTYPES) +#define NumBackendStatSlots (MaxBackends + NUM_AUXILIARY_PROCS) /* ---------- @@ -238,10 +238,9 @@ CreateSharedBackendStatus(void) /* * Initialize pgstats backend activity state, and set up our on-proc-exit - * hook. Called from InitPostgres and AuxiliaryProcessMain. For auxiliary - * process, MyBackendId is invalid. Otherwise, MyBackendId must be set, but we - * must not have started any transaction yet (since the exit hook must run - * after the last transaction exit). + * hook. Called from InitPostgres and AuxiliaryProcessMain. MyBackendId must + * be set, but we must not have started any transaction yet (since the exit + * hook must run after the last transaction exit). * * NOTE: MyDatabaseId isn't set yet; so the shutdown hook has to be careful. */ @@ -249,26 +248,9 @@ void pgstat_beinit(void) { /* Initialize MyBEEntry */ - if (MyBackendId != InvalidBackendId) - { - Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends); - MyBEEntry = &BackendStatusArray[MyBackendId - 1]; - } - else - { - /* Must be an auxiliary process */ - Assert(MyAuxProcType != NotAnAuxProcess); - - /* - * Assign the MyBEEntry for an auxiliary process. Since it doesn't - * have a BackendId, the slot is statically allocated based on the - * auxiliary process type (MyAuxProcType). Backends use slots indexed - * in the range from 0 to MaxBackends (exclusive), so we use - * MaxBackends + AuxProcType as the index of the slot for an auxiliary - * process. - */ - MyBEEntry = &BackendStatusArray[MaxBackends + MyAuxProcType]; - } + Assert(MyBackendId != InvalidBackendId); + Assert(MyBackendId >= 1 && MyBackendId <= NumBackendStatSlots); + MyBEEntry = &BackendStatusArray[MyBackendId - 1]; /* Set up a process-exit hook to clean up */ on_shmem_exit(pgstat_beshutdown_hook, 0); @@ -281,12 +263,12 @@ pgstat_beinit(void) * Initialize this backend's entry in the PgBackendStatus array. * Called from InitPostgres. * - * Apart from auxiliary processes, MyBackendId, MyDatabaseId, - * session userid, and application_name must be set for a - * backend (hence, this cannot be combined with pgstat_beinit). - * Note also that we must be inside a transaction if this isn't an aux - * process, as we may need to do encoding conversion on some strings. - * ---------- + * Apart from auxiliary processes, MyDatabaseId, session userid, and + * application_name must already be set (hence, this cannot be combined + * with pgstat_beinit). Note also that we must be inside a transaction + * if this isn't an aux process, as we may need to do encoding conversion + * on some strings. + *---------- */ void pgstat_bestart(void) |