diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index 7d1e62abfce..18e8a8051dd 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -588,11 +588,7 @@ pgss_shmem_shutdown(int code, Datum arg) /* * Rename file into place, so we atomically replace the old one. */ - if (rename(PGSS_DUMP_FILE ".tmp", PGSS_DUMP_FILE) != 0) - ereport(LOG, - (errcode_for_file_access(), - errmsg("could not rename pg_stat_statement file \"%s\": %m", - PGSS_DUMP_FILE ".tmp"))); + (void) durable_rename(PGSS_DUMP_FILE ".tmp", PGSS_DUMP_FILE, LOG); return; diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 0397686f5a4..8a3c2817c92 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -1380,12 +1380,7 @@ XLogArchiveForceDone(const char *xlog) StatusFilePath(archiveReady, xlog, ".ready"); if (stat(archiveReady, &stat_buf) == 0) { - if (rename(archiveReady, archiveDone) < 0) - ereport(WARNING, - (errcode_for_file_access(), - errmsg("could not rename file \"%s\" to \"%s\": %m", - archiveReady, archiveDone))); - + (void) durable_rename(archiveReady, archiveDone, WARNING); return; } @@ -2773,34 +2768,16 @@ InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath, } /* - * Prefer link() to rename() here just to be really sure that we don't - * overwrite an existing logfile. However, there shouldn't be one, so - * rename() is an acceptable substitute except for the truly paranoid. + * Perform the rename using link if available, paranoidly trying to avoid + * overwriting an existing file (there shouldn't be one). */ -#if HAVE_WORKING_LINK - if (link(tmppath, path) < 0) + if (durable_link_or_rename(tmppath, path, LOG) != 0) { if (use_lock) LWLockRelease(ControlFileLock); - ereport(LOG, - (errcode_for_file_access(), - errmsg("could not link file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m", - tmppath, path, *log, *seg))); + /* durable_link_or_rename already emitted log message */ return false; } - unlink(tmppath); -#else - if (rename(tmppath, path) < 0) - { - if (use_lock) - LWLockRelease(ControlFileLock); - ereport(LOG, - (errcode_for_file_access(), - errmsg("could not rename file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m", - tmppath, path, *log, *seg))); - return false; - } -#endif if (use_lock) LWLockRelease(ControlFileLock); @@ -3048,11 +3025,7 @@ KeepFileRestoredFromArchive(char *path, char *xlogfname) reload = true; } - if (rename(path, xlogfpath) < 0) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not rename file \"%s\" to \"%s\": %m", - path, xlogfpath))); + durable_rename(path, xlogfpath, ERROR); /* * Create .done file forcibly to prevent the restored segment from @@ -4882,24 +4855,10 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI, TLHistoryFilePath(path, newTLI); /* - * Prefer link() to rename() here just to be really sure that we don't - * overwrite an existing logfile. However, there shouldn't be one, so - * rename() is an acceptable substitute except for the truly paranoid. + * Perform the rename using link if available, paranoidly trying to avoid + * overwriting an existing file (there shouldn't be one). */ -#if HAVE_WORKING_LINK - if (link(tmppath, path) < 0) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not link file \"%s\" to \"%s\": %m", - tmppath, path))); - unlink(tmppath); -#else - if (rename(tmppath, path) < 0) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not rename file \"%s\" to \"%s\": %m", - tmppath, path))); -#endif + durable_link_or_rename(tmppath, path, ERROR); /* The history file can be archived immediately. */ if (XLogArchivingActive()) @@ -5882,11 +5841,7 @@ exitArchiveRecovery(TimeLineID endTLI, uint32 endLogId, uint32 endLogSeg) * re-enter archive recovery mode in a subsequent crash. */ unlink(RECOVERY_COMMAND_DONE); - if (rename(RECOVERY_COMMAND_FILE, RECOVERY_COMMAND_DONE) != 0) - ereport(FATAL, - (errcode_for_file_access(), - errmsg("could not rename file \"%s\" to \"%s\": %m", - RECOVERY_COMMAND_FILE, RECOVERY_COMMAND_DONE))); + durable_rename(RECOVERY_COMMAND_FILE, RECOVERY_COMMAND_DONE, FATAL); ereport(LOG, (errmsg("archive recovery complete"))); @@ -6795,11 +6750,7 @@ StartupXLOG(void) if (haveBackupLabel) { unlink(BACKUP_LABEL_OLD); - if (rename(BACKUP_LABEL_FILE, BACKUP_LABEL_OLD) != 0) - ereport(FATAL, - (errcode_for_file_access(), - errmsg("could not rename file \"%s\" to \"%s\": %m", - BACKUP_LABEL_FILE, BACKUP_LABEL_OLD))); + durable_rename(BACKUP_LABEL_FILE, BACKUP_LABEL_OLD, FATAL); } /* Check that the GUCs used to generate the WAL allow recovery */ @@ -10486,7 +10437,7 @@ CancelBackup(void) /* remove leftover file from previously canceled backup if it exists */ unlink(BACKUP_LABEL_OLD); - if (rename(BACKUP_LABEL_FILE, BACKUP_LABEL_OLD) == 0) + if (durable_rename(BACKUP_LABEL_FILE, BACKUP_LABEL_OLD, DEBUG1) == 0) { ereport(LOG, (errmsg("online backup mode canceled"), diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c index 9729c02a827..5b2a071c78f 100644 --- a/src/backend/postmaster/pgarch.c +++ b/src/backend/postmaster/pgarch.c @@ -749,9 +749,5 @@ pgarch_archiveDone(char *xlog) StatusFilePath(rlogready, xlog, ".ready"); StatusFilePath(rlogdone, xlog, ".done"); - if (rename(rlogready, rlogdone) < 0) - ereport(WARNING, - (errcode_for_file_access(), - errmsg("could not rename file \"%s\" to \"%s\": %m", - rlogready, rlogdone))); + (void) durable_rename(rlogready, rlogdone, WARNING); }