1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-31 05:54:36 +03:00

Fix partial two-phase files left by crash during checkpoint

CheckPointTwoPhase() wrote each 2PC file in place, with O_TRUNC.  A
crash mid-write, or a base backup copying the file at that point left an
empty or partial file that would make startup fail, most likely on an
"incorrect size of file" ERROR.

The 2PC files are now written through a temporary file that gets renamed
at the end, relying on the atomicity of the rename to make startup safe,
be it after a crash or a base backup.

Author: Shihao Zhong <zhong950419@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: David Steele <david@pgbackrest.org>
Discussion: https://postgr.es/m/CAGRkXqS_On-pLgcv6xjH8mvGhdcPmfWtythW3r8pYKUG+hsiuQ@mail.gmail.com
Backpatch-through: 14
This commit is contained in:
Michael Paquier
2026-10-26 17:14:07 +09:00
parent 022a80fe6c
commit a5ccf20801

View File

@@ -90,6 +90,7 @@
#include "access/xlogutils.h"
#include "catalog/pg_type.h"
#include "catalog/storage.h"
#include "common/string.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pg_trace.h"
@@ -1750,6 +1751,7 @@ static void
RecreateTwoPhaseFile(FullTransactionId fxid, const void *content, size_t len)
{
char path[MAXPGPATH];
char tmppath[MAXPGPATH];
pg_crc32c statefile_crc;
int fd;
@@ -1760,12 +1762,15 @@ RecreateTwoPhaseFile(FullTransactionId fxid, const void *content, size_t len)
TwoPhaseFilePath(path, fxid);
fd = OpenTransientFile(path,
/* Write to a temp file and rename it into place */
snprintf(tmppath, sizeof(tmppath), "%s.tmp", path);
fd = OpenTransientFile(tmppath,
O_CREAT | O_TRUNC | O_WRONLY | PG_BINARY);
if (fd < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not recreate file \"%s\": %m", path)));
errmsg("could not recreate file \"%s\": %m", tmppath)));
/* Write content and CRC */
errno = 0;
@@ -1777,7 +1782,7 @@ RecreateTwoPhaseFile(FullTransactionId fxid, const void *content, size_t len)
errno = ENOSPC;
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write file \"%s\": %m", path)));
errmsg("could not write file \"%s\": %m", tmppath)));
}
if (write(fd, &statefile_crc, sizeof(pg_crc32c)) != sizeof(pg_crc32c))
{
@@ -1786,7 +1791,7 @@ RecreateTwoPhaseFile(FullTransactionId fxid, const void *content, size_t len)
errno = ENOSPC;
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write file \"%s\": %m", path)));
errmsg("could not write file \"%s\": %m", tmppath)));
}
pgstat_report_wait_end();
@@ -1798,13 +1803,23 @@ RecreateTwoPhaseFile(FullTransactionId fxid, const void *content, size_t len)
if (pg_fsync(fd) != 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m", path)));
errmsg("could not fsync file \"%s\": %m", tmppath)));
pgstat_report_wait_end();
if (CloseTransientFile(fd) != 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not close file \"%s\": %m", path)));
errmsg("could not close file \"%s\": %m", tmppath)));
/*
* No durable_rename(), as CheckPointTwoPhase() fsyncs the parent
* directory.
*/
if (rename(tmppath, path) != 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not rename file \"%s\" to \"%s\": %m",
tmppath, path)));
}
/*
@@ -1918,6 +1933,19 @@ restoreTwoPhaseData(void)
cldir = AllocateDir(TWOPHASE_DIR);
while ((clde = ReadDir(cldir, TWOPHASE_DIR)) != NULL)
{
/* Ignore temp files created by RecreateTwoPhaseFile(). */
if (pg_str_endswith(clde->d_name, ".tmp"))
{
char path[MAXPGPATH];
snprintf(path, MAXPGPATH, TWOPHASE_DIR "/%s", clde->d_name);
if (unlink(path) < 0)
ereport(WARNING,
(errcode_for_file_access(),
errmsg("could not remove file \"%s\": %m", path)));
continue;
}
if (strlen(clde->d_name) == 16 &&
strspn(clde->d_name, "0123456789ABCDEF") == 16)
{