Skip to content

Commit 48a6bf5

Browse files
committed
Sync PG_VERSION file in CREATE DATABASE.
An OS crash could leave PG_VERSION empty or missing. The same symptom appeared in a backup by block device snapshot, taken after the next checkpoint and before the OS flushes the PG_VERSION blocks. Device snapshots are not a documented backup method, however. Back-patch to v15, where commit 9c08aea introduced STRATEGY=WAL_LOG and made it the default. Discussion: https://postgr.es/m/20240130195003.0a.nmisch@google.com
1 parent 6d423e9 commit 48a6bf5

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

doc/src/sgml/monitoring.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,11 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
15451545
<entry><literal>TwophaseFileWrite</literal></entry>
15461546
<entry>Waiting for a write of a two phase state file.</entry>
15471547
</row>
1548+
<row>
1549+
<entry><literal>VersionFileSync</literal></entry>
1550+
<entry>Waiting for the version file to reach durable storage while
1551+
creating a database.</entry>
1552+
</row>
15481553
<row>
15491554
<entry><literal>VersionFileWrite</literal></entry>
15501555
<entry>Waiting for the version file to be written while creating a database.</entry>

src/backend/commands/dbcommands.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,14 @@ CreateDirAndVersionFile(char *dbpath, Oid dbid, Oid tsid, bool isRedo)
508508
}
509509
pgstat_report_wait_end();
510510

511+
pgstat_report_wait_start(WAIT_EVENT_VERSION_FILE_SYNC);
512+
if (pg_fsync(fd) != 0)
513+
ereport(data_sync_elevel(ERROR),
514+
(errcode_for_file_access(),
515+
errmsg("could not fsync file \"%s\": %m", versionfile)));
516+
fsync_fname(dbpath, true);
517+
pgstat_report_wait_end();
518+
511519
/* Close the version file. */
512520
CloseTransientFile(fd);
513521

src/backend/utils/activity/wait_event.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,9 @@ pgstat_get_wait_io(WaitEventIO w)
717717
case WAIT_EVENT_TWOPHASE_FILE_WRITE:
718718
event_name = "TwophaseFileWrite";
719719
break;
720+
case WAIT_EVENT_VERSION_FILE_SYNC:
721+
event_name = "VersionFileSync";
722+
break;
720723
case WAIT_EVENT_VERSION_FILE_WRITE:
721724
event_name = "VersionFileWrite";
722725
break;

src/include/utils/wait_event.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ typedef enum
234234
WAIT_EVENT_WAL_READ,
235235
WAIT_EVENT_WAL_SYNC,
236236
WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN,
237-
WAIT_EVENT_WAL_WRITE
237+
WAIT_EVENT_WAL_WRITE,
238+
WAIT_EVENT_VERSION_FILE_SYNC
238239
} WaitEventIO;
239240

240241

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy