Skip to content

Commit c06e71d

Browse files
committed
Add write_to_file to PgStat_KindInfo for pgstats kinds
This new field controls if entries of a stats kind should be written or not to the on-disk pgstats file when shutting down an instance. This affects both fixed and variable-numbered kinds. This is useful for custom statistics by itself, and a patch is under discussion to add a new builtin stats kind where the write of the stats is not necessary. All the built-in stats kinds, as well as the two custom stats kinds in the test module injection_points, set this flag to "true" for now, so as stats entries are written to the on-disk pgstats file. Author: Bertrand Drouvot Reviewed-by: Nazir Bilal Yavuz Discussion: https://postgr.es/m/Zz7T47nHwYgeYwOe@ip-10-97-1-34.eu-west-3.compute.internal
1 parent 4c4aaa1 commit c06e71d

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

src/backend/utils/activity/pgstat.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
* Statistics are loaded from the filesystem during startup (by the startup
1313
* process), unless preceded by a crash, in which case all stats are
1414
* discarded. They are written out by the checkpointer process just before
15-
* shutting down, except when shutting down in immediate mode.
15+
* shutting down (if the stats kind allows it), except when shutting down in
16+
* immediate mode.
1617
*
1718
* Fixed-numbered stats are stored in plain (non-dynamic) shared memory.
1819
*
@@ -281,6 +282,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
281282
.name = "database",
282283

283284
.fixed_amount = false,
285+
.write_to_file = true,
284286
/* so pg_stat_database entries can be seen in all databases */
285287
.accessed_across_databases = true,
286288

@@ -297,6 +299,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
297299
.name = "relation",
298300

299301
.fixed_amount = false,
302+
.write_to_file = true,
300303

301304
.shared_size = sizeof(PgStatShared_Relation),
302305
.shared_data_off = offsetof(PgStatShared_Relation, stats),
@@ -311,6 +314,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
311314
.name = "function",
312315

313316
.fixed_amount = false,
317+
.write_to_file = true,
314318

315319
.shared_size = sizeof(PgStatShared_Function),
316320
.shared_data_off = offsetof(PgStatShared_Function, stats),
@@ -324,6 +328,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
324328
.name = "replslot",
325329

326330
.fixed_amount = false,
331+
.write_to_file = true,
327332

328333
.accessed_across_databases = true,
329334

@@ -340,6 +345,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
340345
.name = "subscription",
341346

342347
.fixed_amount = false,
348+
.write_to_file = true,
343349
/* so pg_stat_subscription_stats entries can be seen in all databases */
344350
.accessed_across_databases = true,
345351

@@ -359,6 +365,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
359365
.name = "archiver",
360366

361367
.fixed_amount = true,
368+
.write_to_file = true,
362369

363370
.snapshot_ctl_off = offsetof(PgStat_Snapshot, archiver),
364371
.shared_ctl_off = offsetof(PgStat_ShmemControl, archiver),
@@ -374,6 +381,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
374381
.name = "bgwriter",
375382

376383
.fixed_amount = true,
384+
.write_to_file = true,
377385

378386
.snapshot_ctl_off = offsetof(PgStat_Snapshot, bgwriter),
379387
.shared_ctl_off = offsetof(PgStat_ShmemControl, bgwriter),
@@ -389,6 +397,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
389397
.name = "checkpointer",
390398

391399
.fixed_amount = true,
400+
.write_to_file = true,
392401

393402
.snapshot_ctl_off = offsetof(PgStat_Snapshot, checkpointer),
394403
.shared_ctl_off = offsetof(PgStat_ShmemControl, checkpointer),
@@ -404,6 +413,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
404413
.name = "io",
405414

406415
.fixed_amount = true,
416+
.write_to_file = true,
407417

408418
.snapshot_ctl_off = offsetof(PgStat_Snapshot, io),
409419
.shared_ctl_off = offsetof(PgStat_ShmemControl, io),
@@ -421,6 +431,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
421431
.name = "slru",
422432

423433
.fixed_amount = true,
434+
.write_to_file = true,
424435

425436
.snapshot_ctl_off = offsetof(PgStat_Snapshot, slru),
426437
.shared_ctl_off = offsetof(PgStat_ShmemControl, slru),
@@ -438,6 +449,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
438449
.name = "wal",
439450

440451
.fixed_amount = true,
452+
.write_to_file = true,
441453

442454
.snapshot_ctl_off = offsetof(PgStat_Snapshot, wal),
443455
.shared_ctl_off = offsetof(PgStat_ShmemControl, wal),
@@ -1617,6 +1629,10 @@ pgstat_write_statsfile(XLogRecPtr redo)
16171629
if (pgstat_is_kind_builtin(kind))
16181630
Assert(info->snapshot_ctl_off != 0);
16191631

1632+
/* skip if no need to write to file */
1633+
if (!info->write_to_file)
1634+
continue;
1635+
16201636
pgstat_build_snapshot_fixed(kind);
16211637
if (pgstat_is_kind_builtin(kind))
16221638
ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
@@ -1663,6 +1679,10 @@ pgstat_write_statsfile(XLogRecPtr redo)
16631679
/* if not dropped the valid-entry refcount should exist */
16641680
Assert(pg_atomic_read_u32(&ps->refcount) > 0);
16651681

1682+
/* skip if no need to write to file */
1683+
if (!kind_info->write_to_file)
1684+
continue;
1685+
16661686
if (!kind_info->to_serialized_name)
16671687
{
16681688
/* normal stats entry, identified by PgStat_HashKey */

src/include/utils/pgstat_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ typedef struct PgStat_KindInfo
213213
*/
214214
bool accessed_across_databases:1;
215215

216+
/* Should stats be written to the on-disk stats file? */
217+
bool write_to_file:1;
218+
216219
/*
217220
* The size of an entry in the shared stats hash table (pointed to by
218221
* PgStatShared_HashEntry->body). For fixed-numbered statistics, this is

src/test/modules/injection_points/injection_stats.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static bool injection_stats_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
3939
static const PgStat_KindInfo injection_stats = {
4040
.name = "injection_points",
4141
.fixed_amount = false, /* Bounded by the number of points */
42+
.write_to_file = true,
4243

4344
/* Injection points are system-wide */
4445
.accessed_across_databases = true,

src/test/modules/injection_points/injection_stats_fixed.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static void injection_stats_fixed_snapshot_cb(void);
5050
static const PgStat_KindInfo injection_stats_fixed = {
5151
.name = "injection_points_fixed",
5252
.fixed_amount = true,
53+
.write_to_file = true,
5354

5455
.shared_size = sizeof(PgStat_StatInjFixedEntry),
5556
.shared_data_off = offsetof(PgStatShared_InjectionPointFixed, stats),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy