Skip to content

Commit 5cd1c40

Browse files
committed
pgstat: set timestamps of fixed-numbered stats after a crash.
When not loading stats at startup (i.e. pgstat_discard_stats() getting called), reset timestamps of fixed numbered stats would be left at 0. Oversight in 5891c7a. Instead use pgstat_reset_after_failure() and add tests verifying that fixed-numbered reset timestamps are set appropriately. Reported-By: "David G. Johnston" <david.g.johnston@gmail.com> Discussion: https://postgr.es/m/CAKFQuwamFuaQHKdhcMt4Gbw5+Hca2UE741B8gOOXoA=TtAd2Yw@mail.gmail.com
1 parent 3f19e17 commit 5cd1c40

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/backend/utils/activity/pgstat.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ typedef struct PgStat_SnapshotEntry
166166
static void pgstat_write_statsfile(void);
167167
static void pgstat_read_statsfile(void);
168168

169-
static void pgstat_reset_after_failure(TimestampTz ts);
169+
static void pgstat_reset_after_failure(void);
170170

171171
static bool pgstat_flush_pending_entries(bool nowait);
172172

@@ -427,6 +427,12 @@ pgstat_discard_stats(void)
427427
errmsg("unlinked permanent statistics file \"%s\"",
428428
PGSTAT_STAT_PERMANENT_FILENAME)));
429429
}
430+
431+
/*
432+
* Reset stats contents. This will set reset timestamps of fixed-numbered
433+
* stats to the current time (no variable stats exist).
434+
*/
435+
pgstat_reset_after_failure();
430436
}
431437

432438
/*
@@ -1422,7 +1428,6 @@ pgstat_read_statsfile(void)
14221428
bool found;
14231429
const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
14241430
PgStat_ShmemControl *shmem = pgStatLocal.shmem;
1425-
TimestampTz ts = GetCurrentTimestamp();
14261431

14271432
/* shouldn't be called from postmaster */
14281433
Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
@@ -1445,7 +1450,7 @@ pgstat_read_statsfile(void)
14451450
(errcode_for_file_access(),
14461451
errmsg("could not open statistics file \"%s\": %m",
14471452
statfile)));
1448-
pgstat_reset_after_failure(ts);
1453+
pgstat_reset_after_failure();
14491454
return;
14501455
}
14511456

@@ -1597,19 +1602,20 @@ pgstat_read_statsfile(void)
15971602
ereport(LOG,
15981603
(errmsg("corrupted statistics file \"%s\"", statfile)));
15991604

1600-
/* Set the current timestamp as reset timestamp */
1601-
pgstat_reset_after_failure(ts);
1605+
pgstat_reset_after_failure();
16021606

16031607
goto done;
16041608
}
16051609

16061610
/*
1607-
* Helper to reset / drop stats after restoring stats from disk failed,
1608-
* potentially after already loading parts.
1611+
* Helper to reset / drop stats after a crash or after restoring stats from
1612+
* disk failed, potentially after already loading parts.
16091613
*/
16101614
static void
1611-
pgstat_reset_after_failure(TimestampTz ts)
1615+
pgstat_reset_after_failure(void)
16121616
{
1617+
TimestampTz ts = GetCurrentTimestamp();
1618+
16131619
/* reset fixed-numbered stats */
16141620
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
16151621
{

src/test/recovery/t/029_stats_restart.pl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@
250250
$wal_restart2->{reset},
251251
"$sect: newer stats_reset");
252252

253+
$node->stop('immediate');
254+
$node->start;
255+
256+
$sect = "post immediate restart";
257+
my $wal_restart_immediate = wal_stats();
258+
259+
cmp_ok(
260+
$wal_reset_restart->{reset}, 'lt',
261+
$wal_restart_immediate->{reset},
262+
"$sect: reset timestamp is new");
253263

254264
$node->stop;
255265
done_testing();

0 commit comments

Comments
 (0)