From 0ab41bd5030caf33c82692c7a3a6618a3771166f Mon Sep 17 00:00:00 2001 From: Melanie Plageman Date: Wed, 27 Dec 2023 16:32:40 -0500 Subject: [PATCH v6 4/6] Bgwriter maintains global LSNTimeStream Insert new LSN, time pairs to the global LSNTimeStream stored in PgStat_WalStats in the background writer's main loop. This ensures that new values are added to the stream in a regular manner. --- src/backend/postmaster/bgwriter.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c index 0f75548759a..99c2e6eecc3 100644 --- a/src/backend/postmaster/bgwriter.c +++ b/src/backend/postmaster/bgwriter.c @@ -273,6 +273,7 @@ BackgroundWriterMain(char *startup_data, size_t startup_data_len) { TimestampTz timeout = 0; TimestampTz now = GetCurrentTimestamp(); + XLogRecPtr current_lsn; timeout = TimestampTzPlusMilliseconds(last_snapshot_ts, LOG_SNAPSHOT_INTERVAL_MS); @@ -284,11 +285,23 @@ BackgroundWriterMain(char *startup_data, size_t startup_data_len) * start of a record, whereas last_snapshot_lsn points just past * the end of the record. */ - if (now >= timeout && - last_snapshot_lsn <= GetLastImportantRecPtr()) + if (now >= timeout) { - last_snapshot_lsn = LogStandbySnapshot(); - last_snapshot_ts = now; + current_lsn = GetLastImportantRecPtr(); + if (last_snapshot_lsn <= current_lsn) + { + last_snapshot_lsn = LogStandbySnapshot(); + last_snapshot_ts = now; + + /* + * After a restart GetXLogInsertRecPtr() may return 0. We + * don't want the timeline to move backwards, though, so + * get the insert LSN instead. + */ + if (current_lsn == 0) + current_lsn = GetXLogInsertRecPtr(); + pgstat_wal_update_lsntime_stream(now, current_lsn); + } } } -- 2.34.1