Skip to content

Commit 559efce

Browse files
committed
Add num_done counter to the pg_stat_checkpointer view.
Checkpoints can be skipped when the server is idle. The existing num_timed and num_requested counters in pg_stat_checkpointer track both completed and skipped checkpoints, but there was no way to count only the completed ones. This commit introduces the num_done counter, which tracks only completed checkpoints, making it easier to see how many were actually performed. Bump catalog version. Author: Anton A. Melnikov Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/[email protected]
1 parent 20cfec8 commit 559efce

File tree

11 files changed

+60
-19
lines changed

11 files changed

+60
-19
lines changed

doc/src/sgml/monitoring.sgml

+10-1
Original file line numberDiff line numberDiff line change
@@ -3063,7 +3063,16 @@ description | Waiting for a newly initialized WAL file to reach durable storage
30633063
<structfield>num_requested</structfield> <type>bigint</type>
30643064
</para>
30653065
<para>
3066-
Number of requested checkpoints that have been performed
3066+
Number of backend requested checkpoints
3067+
</para></entry>
3068+
</row>
3069+
3070+
<row>
3071+
<entry role="catalog_table_entry"><para role="column_definition">
3072+
<structfield>num_done</structfield> <type>bigint</type>
3073+
</para>
3074+
<para>
3075+
Number of checkpoints that have been performed
30673076
</para></entry>
30683077
</row>
30693078

src/backend/access/transam/xlog.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -6878,8 +6878,11 @@ update_checkpoint_display(int flags, bool restartpoint, bool reset)
68786878
* In this case, we only insert an XLOG_CHECKPOINT_SHUTDOWN record, and it's
68796879
* both the record marking the completion of the checkpoint and the location
68806880
* from which WAL replay would begin if needed.
6881+
*
6882+
* Returns true if a new checkpoint was performed, or false if it was skipped
6883+
* because the system was idle.
68816884
*/
6882-
void
6885+
bool
68836886
CreateCheckPoint(int flags)
68846887
{
68856888
bool shutdown;
@@ -6971,7 +6974,7 @@ CreateCheckPoint(int flags)
69716974
END_CRIT_SECTION();
69726975
ereport(DEBUG1,
69736976
(errmsg_internal("checkpoint skipped because system is idle")));
6974-
return;
6977+
return false;
69756978
}
69766979
}
69776980

@@ -7353,6 +7356,8 @@ CreateCheckPoint(int flags)
73537356
CheckpointStats.ckpt_segs_added,
73547357
CheckpointStats.ckpt_segs_removed,
73557358
CheckpointStats.ckpt_segs_recycled);
7359+
7360+
return true;
73567361
}
73577362

73587363
/*

src/backend/catalog/system_views.sql

+1
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ CREATE VIEW pg_stat_checkpointer AS
11381138
SELECT
11391139
pg_stat_get_checkpointer_num_timed() AS num_timed,
11401140
pg_stat_get_checkpointer_num_requested() AS num_requested,
1141+
pg_stat_get_checkpointer_num_performed() AS num_done,
11411142
pg_stat_get_checkpointer_restartpoints_timed() AS restartpoints_timed,
11421143
pg_stat_get_checkpointer_restartpoints_requested() AS restartpoints_req,
11431144
pg_stat_get_checkpointer_restartpoints_performed() AS restartpoints_done,

src/backend/postmaster/checkpointer.c

+25-14
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,7 @@ CheckpointerMain(char *startup_data, size_t startup_data_len)
460460
* Do the checkpoint.
461461
*/
462462
if (!do_restartpoint)
463-
{
464-
CreateCheckPoint(flags);
465-
ckpt_performed = true;
466-
}
463+
ckpt_performed = CreateCheckPoint(flags);
467464
else
468465
ckpt_performed = CreateRestartPoint(flags);
469466

@@ -484,7 +481,7 @@ CheckpointerMain(char *startup_data, size_t startup_data_len)
484481

485482
ConditionVariableBroadcast(&CheckpointerShmem->done_cv);
486483

487-
if (ckpt_performed)
484+
if (!do_restartpoint)
488485
{
489486
/*
490487
* Note we record the checkpoint start time not end time as
@@ -493,18 +490,32 @@ CheckpointerMain(char *startup_data, size_t startup_data_len)
493490
*/
494491
last_checkpoint_time = now;
495492

496-
if (do_restartpoint)
497-
PendingCheckpointerStats.restartpoints_performed++;
493+
if (ckpt_performed)
494+
PendingCheckpointerStats.num_performed++;
498495
}
499496
else
500497
{
501-
/*
502-
* We were not able to perform the restartpoint (checkpoints
503-
* throw an ERROR in case of error). Most likely because we
504-
* have not received any new checkpoint WAL records since the
505-
* last restartpoint. Try again in 15 s.
506-
*/
507-
last_checkpoint_time = now - CheckPointTimeout + 15;
498+
if (ckpt_performed)
499+
{
500+
/*
501+
* The same as for checkpoint. Please see the
502+
* corresponding comment.
503+
*/
504+
last_checkpoint_time = now;
505+
506+
PendingCheckpointerStats.restartpoints_performed++;
507+
}
508+
else
509+
{
510+
/*
511+
* We were not able to perform the restartpoint
512+
* (checkpoints throw an ERROR in case of error). Most
513+
* likely because we have not received any new checkpoint
514+
* WAL records since the last restartpoint. Try again in
515+
* 15 s.
516+
*/
517+
last_checkpoint_time = now - CheckPointTimeout + 15;
518+
}
508519
}
509520

510521
ckpt_active = false;

src/backend/utils/activity/pgstat_checkpointer.c

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pgstat_report_checkpointer(void)
4949
#define CHECKPOINTER_ACC(fld) stats_shmem->stats.fld += PendingCheckpointerStats.fld
5050
CHECKPOINTER_ACC(num_timed);
5151
CHECKPOINTER_ACC(num_requested);
52+
CHECKPOINTER_ACC(num_performed);
5253
CHECKPOINTER_ACC(restartpoints_timed);
5354
CHECKPOINTER_ACC(restartpoints_requested);
5455
CHECKPOINTER_ACC(restartpoints_performed);
@@ -127,6 +128,7 @@ pgstat_checkpointer_snapshot_cb(void)
127128
#define CHECKPOINTER_COMP(fld) pgStatLocal.snapshot.checkpointer.fld -= reset.fld;
128129
CHECKPOINTER_COMP(num_timed);
129130
CHECKPOINTER_COMP(num_requested);
131+
CHECKPOINTER_COMP(num_performed);
130132
CHECKPOINTER_COMP(restartpoints_timed);
131133
CHECKPOINTER_COMP(restartpoints_requested);
132134
CHECKPOINTER_COMP(restartpoints_performed);

src/backend/utils/adt/pgstatfuncs.c

+6
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,12 @@ pg_stat_get_checkpointer_num_requested(PG_FUNCTION_ARGS)
11911191
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->num_requested);
11921192
}
11931193

1194+
Datum
1195+
pg_stat_get_checkpointer_num_performed(PG_FUNCTION_ARGS)
1196+
{
1197+
PG_RETURN_INT64(pgstat_fetch_stat_checkpointer()->num_performed);
1198+
}
1199+
11941200
Datum
11951201
pg_stat_get_checkpointer_restartpoints_timed(PG_FUNCTION_ARGS)
11961202
{

src/include/access/xlog.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ extern void LocalProcessControlFile(bool reset);
239239
extern WalLevel GetActiveWalLevelOnStandby(void);
240240
extern void StartupXLOG(void);
241241
extern void ShutdownXLOG(int code, Datum arg);
242-
extern void CreateCheckPoint(int flags);
242+
extern bool CreateCheckPoint(int flags);
243243
extern bool CreateRestartPoint(int flags);
244244
extern WALAvailability GetWALAvailability(XLogRecPtr targetLSN);
245245
extern void XLogPutNextOid(Oid nextOid);

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202409271
60+
#define CATALOG_VERSION_NO 202409301
6161

6262
#endif

src/include/catalog/pg_proc.dat

+5
Original file line numberDiff line numberDiff line change
@@ -5820,6 +5820,11 @@
58205820
proname => 'pg_stat_get_checkpointer_num_requested', provolatile => 's',
58215821
proparallel => 'r', prorettype => 'int8', proargtypes => '',
58225822
prosrc => 'pg_stat_get_checkpointer_num_requested' },
5823+
{ oid => '8599',
5824+
descr => 'statistics: number of checkpoints performed by the checkpointer',
5825+
proname => 'pg_stat_get_checkpointer_num_performed', provolatile => 's',
5826+
proparallel => 'r', prorettype => 'int8', proargtypes => '',
5827+
prosrc => 'pg_stat_get_checkpointer_num_performed' },
58235828
{ oid => '6327',
58245829
descr => 'statistics: number of timed restartpoints started by the checkpointer',
58255830
proname => 'pg_stat_get_checkpointer_restartpoints_timed', provolatile => 's',

src/include/pgstat.h

+1
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ typedef struct PgStat_CheckpointerStats
294294
{
295295
PgStat_Counter num_timed;
296296
PgStat_Counter num_requested;
297+
PgStat_Counter num_performed;
297298
PgStat_Counter restartpoints_timed;
298299
PgStat_Counter restartpoints_requested;
299300
PgStat_Counter restartpoints_performed;

src/test/regress/expected/rules.out

+1
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,7 @@ pg_stat_bgwriter| SELECT pg_stat_get_bgwriter_buf_written_clean() AS buffers_cle
18241824
pg_stat_get_bgwriter_stat_reset_time() AS stats_reset;
18251825
pg_stat_checkpointer| SELECT pg_stat_get_checkpointer_num_timed() AS num_timed,
18261826
pg_stat_get_checkpointer_num_requested() AS num_requested,
1827+
pg_stat_get_checkpointer_num_performed() AS num_done,
18271828
pg_stat_get_checkpointer_restartpoints_timed() AS restartpoints_timed,
18281829
pg_stat_get_checkpointer_restartpoints_requested() AS restartpoints_req,
18291830
pg_stat_get_checkpointer_restartpoints_performed() AS restartpoints_done,

0 commit comments

Comments
 (0)