Make write of pgstats file durable at shutdown
authorMichael Paquier <[email protected]>
Wed, 17 Jul 2024 02:50:36 +0000 (11:50 +0900)
committerMichael Paquier <[email protected]>
Wed, 17 Jul 2024 02:50:36 +0000 (11:50 +0900)
This switches the pgstats write code to use durable_rename() rather than
rename().  This ensures that the stats file's data is durable when the
statistics are written, which is something only happening at shutdown
now with the checkpointer doing the job.

This could cause the statistics to be lost even after PostgreSQL is shut
down, should a host failure happen, for example.

Suggested-by: Konstantin Knizhnik
Reviewed-by: Bertrand Drouvot
Discussion: https://postgr.es/m/[email protected]

src/backend/utils/activity/pgstat.c

index ed7baa6e04632a1c67f83f6fcf032dadcf75c47f..2e22bf270781ae5b1cee0f83da7de673e67a726d 100644 (file)
@@ -1478,12 +1478,9 @@ pgstat_write_statsfile(void)
                        tmpfile)));
        unlink(tmpfile);
    }
-   else if (rename(tmpfile, statfile) < 0)
+   else if (durable_rename(tmpfile, statfile, LOG) < 0)
    {
-       ereport(LOG,
-               (errcode_for_file_access(),
-                errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
-                       tmpfile, statfile)));
+       /* durable_rename already emitted log message */
        unlink(tmpfile);
    }
 }