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

Commit2791f21

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 parent6c1bf45 commit2791f21

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
@@ -427,6 +427,13 @@ pgss_shmem_startup(void)
427427

428428
pfree(buffer);
429429
FreeFile(file);
430+
431+
/*
432+
* Remove the file so it's not included in backups/replication
433+
* slaves, etc. A new file will be written on next shutdown.
434+
*/
435+
unlink(PGSS_DUMP_FILE);
436+
430437
return;
431438

432439
error:
@@ -468,7 +475,7 @@ pgss_shmem_shutdown(int code, Datum arg)
468475
if (!pgss_save)
469476
return;
470477

471-
file=AllocateFile(PGSS_DUMP_FILE,PG_BINARY_W);
478+
file=AllocateFile(PGSS_DUMP_FILE".tmp",PG_BINARY_W);
472479
if (file==NULL)
473480
gotoerror;
474481

@@ -494,16 +501,25 @@ pgss_shmem_shutdown(int code, Datum arg)
494501
gotoerror;
495502
}
496503

504+
/*
505+
* Rename file into place, so we atomically replace the old one.
506+
*/
507+
if (rename(PGSS_DUMP_FILE".tmp",PGSS_DUMP_FILE)!=0)
508+
ereport(LOG,
509+
(errcode_for_file_access(),
510+
errmsg("could not rename pg_stat_statement file \"%s\": %m",
511+
PGSS_DUMP_FILE".tmp")));
512+
497513
return;
498514

499515
error:
500516
ereport(LOG,
501517
(errcode_for_file_access(),
502518
errmsg("could not write pg_stat_statement file \"%s\": %m",
503-
PGSS_DUMP_FILE)));
519+
PGSS_DUMP_FILE".tmp")));
504520
if (file)
505521
FreeFile(file);
506-
unlink(PGSS_DUMP_FILE);
522+
unlink(PGSS_DUMP_FILE".tmp");
507523
}
508524

509525
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp