Skip to content

Commit 1b373ae

Browse files
committed
Add callback for backend initialization in pgstats
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
1 parent 341e9a0 commit 1b373ae

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/backend/utils/activity/pgstat.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
441441
.shared_data_off = offsetof(PgStatShared_Wal, stats),
442442
.shared_data_len = sizeof(((PgStatShared_Wal *) 0)->stats),
443443

444+
.init_backend_cb = pgstat_wal_init_backend_cb,
444445
.init_shmem_cb = pgstat_wal_init_shmem_cb,
445446
.reset_all_cb = pgstat_wal_reset_all_cb,
446447
.snapshot_cb = pgstat_wal_snapshot_cb,
@@ -604,10 +605,19 @@ pgstat_initialize(void)
604605

605606
pgstat_attach_shmem();
606607

607-
pgstat_init_wal();
608-
609608
pgstat_init_snapshot_fixed();
610609

610+
/* Backend initialization callbacks */
611+
for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
612+
{
613+
const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
614+
615+
if (kind_info == NULL || kind_info->init_backend_cb == NULL)
616+
continue;
617+
618+
kind_info->init_backend_cb();
619+
}
620+
611621
/* Set up a process-exit hook to clean up */
612622
before_shmem_exit(pgstat_shutdown_hook, 0);
613623

src/backend/utils/activity/pgstat_wal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pgstat_flush_wal(bool nowait)
138138
}
139139

140140
void
141-
pgstat_init_wal(void)
141+
pgstat_wal_init_backend_cb(void)
142142
{
143143
/*
144144
* Initialize prevWalUsage with pgWalUsage so that pgstat_flush_wal() can

src/include/utils/pgstat_internal.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ typedef struct PgStat_KindInfo
229229
*/
230230
uint32 pending_size;
231231

232+
/*
233+
* Perform custom actions when initializing a backend (standalone or under
234+
* postmaster). Optional.
235+
*/
236+
void (*init_backend_cb) (void);
237+
232238
/*
233239
* For variable-numbered stats: flush pending stats. Required if pending
234240
* data is used.
@@ -673,9 +679,9 @@ extern void pgstat_slru_snapshot_cb(void);
673679
*/
674680

675681
extern bool pgstat_flush_wal(bool nowait);
676-
extern void pgstat_init_wal(void);
677682
extern bool pgstat_have_pending_wal(void);
678683

684+
extern void pgstat_wal_init_backend_cb(void);
679685
extern void pgstat_wal_init_shmem_cb(void *stats);
680686
extern void pgstat_wal_reset_all_cb(TimestampTz ts);
681687
extern void pgstat_wal_snapshot_cb(void);

0 commit comments

Comments
 (0)