Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2b97db6

Browse files
committed
Fix handling of pg_stat_statements.stat temporary file
Write the file to a temporary name and then rename() it into the permanent name, to ensure it can't end up half-written and corrupt in case of a crash during shutdown. Unlink the file after it has been read so it's removed from the data directory and not included in base backups going to replication slaves.
1 parent 532fe28 commit 2b97db6

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

+19-3
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,13 @@ pgss_shmem_startup(void)
511511

512512
pfree(buffer);
513513
FreeFile(file);
514+
515+
/*
516+
* Remove the file so it's not included in backups/replication
517+
* slaves, etc. A new file will be written on next shutdown.
518+
*/
519+
unlink(PGSS_DUMP_FILE);
520+
514521
return;
515522

516523
error:
@@ -552,7 +559,7 @@ pgss_shmem_shutdown(int code, Datum arg)
552559
if (!pgss_save)
553560
return;
554561

555-
file = AllocateFile(PGSS_DUMP_FILE, PG_BINARY_W);
562+
file = AllocateFile(PGSS_DUMP_FILE ".tmp", PG_BINARY_W);
556563
if (file == NULL)
557564
goto error;
558565

@@ -578,16 +585,25 @@ pgss_shmem_shutdown(int code, Datum arg)
578585
goto error;
579586
}
580587

588+
/*
589+
* Rename file into place, so we atomically replace the old one.
590+
*/
591+
if (rename(PGSS_DUMP_FILE ".tmp", PGSS_DUMP_FILE) != 0)
592+
ereport(LOG,
593+
(errcode_for_file_access(),
594+
errmsg("could not rename pg_stat_statement file \"%s\": %m",
595+
PGSS_DUMP_FILE ".tmp")));
596+
581597
return;
582598

583599
error:
584600
ereport(LOG,
585601
(errcode_for_file_access(),
586602
errmsg("could not write pg_stat_statement file \"%s\": %m",
587-
PGSS_DUMP_FILE)));
603+
PGSS_DUMP_FILE ".tmp")));
588604
if (file)
589605
FreeFile(file);
590-
unlink(PGSS_DUMP_FILE);
606+
unlink(PGSS_DUMP_FILE ".tmp");
591607
}
592608

593609
/*

0 commit comments

Comments
 (0)