]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Add callback for backend initialization in pgstats
authorMichael Paquier <michael@paquier.xyz>
Thu, 5 Sep 2024 07:05:21 +0000 (16:05 +0900)
committerMichael Paquier <michael@paquier.xyz>
Thu, 5 Sep 2024 07:05:21 +0000 (16:05 +0900)
pgstat_initialize() is currently used by the WAL stats as a code path to
take some custom actions when a backend starts.  A callback is added to
generalize the concept so as all stats kinds can do the same, for
builtin and custom kinds, if set.

Reviewed-by: Bertrand Drouvot, Kyotaro Horiguchi
Discussion: https://postgr.es/m/ZtZr1K4PLdeWclXY@paquier.xyz

src/backend/utils/activity/pgstat.c
src/backend/utils/activity/pgstat_wal.c
src/include/utils/pgstat_internal.h

index 612158f2b9641a6374386e972fbcf6872834aa02..178b5ef65aab4939aabd4cc7115c8c2d3640c964 100644 (file)
@@ -441,6 +441,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
                .shared_data_off = offsetof(PgStatShared_Wal, stats),
                .shared_data_len = sizeof(((PgStatShared_Wal *) 0)->stats),
 
+               .init_backend_cb = pgstat_wal_init_backend_cb,
                .init_shmem_cb = pgstat_wal_init_shmem_cb,
                .reset_all_cb = pgstat_wal_reset_all_cb,
                .snapshot_cb = pgstat_wal_snapshot_cb,
@@ -604,10 +605,19 @@ pgstat_initialize(void)
 
        pgstat_attach_shmem();
 
-       pgstat_init_wal();
-
        pgstat_init_snapshot_fixed();
 
+       /* Backend initialization callbacks */
+       for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
+       {
+               const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
+
+               if (kind_info == NULL || kind_info->init_backend_cb == NULL)
+                       continue;
+
+               kind_info->init_backend_cb();
+       }
+
        /* Set up a process-exit hook to clean up */
        before_shmem_exit(pgstat_shutdown_hook, 0);
 
index e2a3f6b865c212e2571a5bca5eda57f308bb6c31..8c19c3f2fd52d1d80bbc7423148ad3e55f9347ce 100644 (file)
@@ -138,7 +138,7 @@ pgstat_flush_wal(bool nowait)
 }
 
 void
-pgstat_init_wal(void)
+pgstat_wal_init_backend_cb(void)
 {
        /*
         * Initialize prevWalUsage with pgWalUsage so that pgstat_flush_wal() can
index fb132e439dc19a2acc1c96dedbf72b485a2e602a..25820cbf0a6b41e920c19af620dfee8a5cc512e2 100644 (file)
@@ -229,6 +229,12 @@ typedef struct PgStat_KindInfo
         */
        uint32          pending_size;
 
+       /*
+        * Perform custom actions when initializing a backend (standalone or under
+        * postmaster). Optional.
+        */
+       void            (*init_backend_cb) (void);
+
        /*
         * For variable-numbered stats: flush pending stats. Required if pending
         * data is used.
@@ -673,9 +679,9 @@ extern void pgstat_slru_snapshot_cb(void);
  */
 
 extern bool pgstat_flush_wal(bool nowait);
-extern void pgstat_init_wal(void);
 extern bool pgstat_have_pending_wal(void);
 
+extern void pgstat_wal_init_backend_cb(void);
 extern void pgstat_wal_init_shmem_cb(void *stats);
 extern void pgstat_wal_reset_all_cb(TimestampTz ts);
 extern void pgstat_wal_snapshot_cb(void);