Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2b97db6

Browse files
committed
Fix handling of pg_stat_statements.stat temporary file
Write the file to a temporary name and then rename() it into thepermanent name, to ensure it can't end up half-written and corruptin case of a crash during shutdown.Unlink the file after it has been read so it's removed from the datadirectory and not included in base backups going to replication slaves.
1 parent532fe28 commit2b97db6

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

‎contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 19 additions & 3 deletions
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
gotoerror;
558565

@@ -578,16 +585,25 @@ pgss_shmem_shutdown(int code, Datum arg)
578585
gotoerror;
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)

[8]ページ先頭

©2009-2025 Movatter.jp