From 2a41fe74a819cc7c43d02f0af3718f20fc16d830 Mon Sep 17 00:00:00 2001 From: Sami Imseih Date: Tue, 14 Jul 2026 08:07:23 -0500 Subject: [PATCH v2 2/3] pgstat: pass STATS_DISCARD to finish() on stats file I/O failure pgstat_write_statsfile() unconditionally passed STATS_WRITE to finish() callbacks regardless of write failure, and pgstat_read_statsfile() skipped finish() entirely when the main stats file could not be opened. Both cases can leave orphaned auxiliary files on disk. Fix by tracking success/failure in both paths and passing STATS_DISCARD to finish() callbacks when the core stats file operation did not succeed. This ensures that extensions receive the proper status to cleanup auxiliary files in cases of such failures. --- src/backend/utils/activity/pgstat.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c index debcc87af50..b89c21da6ee 100644 --- a/src/backend/utils/activity/pgstat.c +++ b/src/backend/utils/activity/pgstat.c @@ -1647,6 +1647,7 @@ pgstat_write_statsfile(void) const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME; dshash_seq_status hstat; PgStatShared_HashEntry *ps; + PgStat_StatsFileOp status = STATS_WRITE; pgstat_assert_is_up(); @@ -1795,6 +1796,7 @@ pgstat_write_statsfile(void) tmpfile))); FreeFile(fpout); unlink(tmpfile); + status = STATS_DISCARD; } else if (FreeFile(fpout) < 0) { @@ -1803,11 +1805,13 @@ pgstat_write_statsfile(void) errmsg("could not close temporary statistics file \"%s\": %m", tmpfile))); unlink(tmpfile); + status = STATS_DISCARD; } else if (durable_rename(tmpfile, statfile, LOG) < 0) { /* durable_rename already emitted log message */ unlink(tmpfile); + status = STATS_DISCARD; } /* Finish callbacks, if required */ @@ -1816,7 +1820,7 @@ pgstat_write_statsfile(void) const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind); if (kind_info && kind_info->finish) - kind_info->finish(STATS_WRITE); + kind_info->finish(status); } } @@ -1839,6 +1843,7 @@ pgstat_read_statsfile(void) FILE *fpin; int32 format_id; bool found; + PgStat_StatsFileOp status = STATS_READ; const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME; PgStat_ShmemControl *shmem = pgStatLocal.shmem; @@ -1864,7 +1869,8 @@ pgstat_read_statsfile(void) errmsg("could not open statistics file \"%s\": %m", statfile))); pgstat_reset_after_failure(); - return; + status = STATS_DISCARD; + goto finish; } /* @@ -2122,13 +2128,14 @@ done: elog(DEBUG2, "removing permanent stats file \"%s\"", statfile); unlink(statfile); +finish: /* Finish callbacks, if required */ for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++) { const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind); if (kind_info && kind_info->finish) - kind_info->finish(STATS_READ); + kind_info->finish(status); } return; @@ -2138,6 +2145,7 @@ error: (errmsg("corrupted statistics file \"%s\"", statfile))); pgstat_reset_after_failure(); + status = STATS_DISCARD; goto done; } -- 2.50.1 (Apple Git-155)