Skip to content

Commit

Permalink
pgstat: set timestamps of fixed-numbered stats after a crash.
Browse files Browse the repository at this point in the history
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" <[email protected]>
Discussion: https://postgr.es/m/CAKFQuwamFuaQHKdhcMt4Gbw5+Hca2UE741B8gOOXoA=TtAd2Yw@mail.gmail.com
  • Loading branch information
anarazel committed Apr 15, 2022
1 parent 3f19e17 commit 5cd1c40
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/backend/utils/activity/pgstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ typedef struct PgStat_SnapshotEntry
static void pgstat_write_statsfile(void);
static void pgstat_read_statsfile(void);

static void pgstat_reset_after_failure(TimestampTz ts);
static void pgstat_reset_after_failure(void);

static bool pgstat_flush_pending_entries(bool nowait);

Expand Down Expand Up @@ -427,6 +427,12 @@ pgstat_discard_stats(void)
errmsg("unlinked permanent statistics file \"%s\"",
PGSTAT_STAT_PERMANENT_FILENAME)));
}

/*
* Reset stats contents. This will set reset timestamps of fixed-numbered
* stats to the current time (no variable stats exist).
*/
pgstat_reset_after_failure();
}

/*
Expand Down Expand Up @@ -1422,7 +1428,6 @@ pgstat_read_statsfile(void)
bool found;
const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
PgStat_ShmemControl *shmem = pgStatLocal.shmem;
TimestampTz ts = GetCurrentTimestamp();

/* shouldn't be called from postmaster */
Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
Expand All @@ -1445,7 +1450,7 @@ pgstat_read_statsfile(void)
(errcode_for_file_access(),
errmsg("could not open statistics file \"%s\": %m",
statfile)));
pgstat_reset_after_failure(ts);
pgstat_reset_after_failure();
return;
}

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

/* Set the current timestamp as reset timestamp */
pgstat_reset_after_failure(ts);
pgstat_reset_after_failure();

goto done;
}

/*
* Helper to reset / drop stats after restoring stats from disk failed,
* potentially after already loading parts.
* Helper to reset / drop stats after a crash or after restoring stats from
* disk failed, potentially after already loading parts.
*/
static void
pgstat_reset_after_failure(TimestampTz ts)
pgstat_reset_after_failure(void)
{
TimestampTz ts = GetCurrentTimestamp();

/* reset fixed-numbered stats */
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
{
Expand Down
10 changes: 10 additions & 0 deletions src/test/recovery/t/029_stats_restart.pl
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@
$wal_restart2->{reset},
"$sect: newer stats_reset");

$node->stop('immediate');
$node->start;

$sect = "post immediate restart";
my $wal_restart_immediate = wal_stats();

cmp_ok(
$wal_reset_restart->{reset}, 'lt',
$wal_restart_immediate->{reset},
"$sect: reset timestamp is new");

$node->stop;
done_testing();
Expand Down

0 comments on commit 5cd1c40

Please sign in to comment.