Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ccfbd92

Browse files
committed
Replace existing durable_rename_excl() calls with durable_rename()
durable_rename_excl() attempts to avoid overwriting any existing files by using link() and unlink(), falling back to rename() on some platforms (e.g., Windows where link() followed by unlink() is not concurrent-safe, see 909b449). Most callers of durable_rename_excl() use it just in case there is an existing file, but it happens that for all of them we never expect a target file to exist (WAL segment recycling, creation of timeline history file and basic_archive). basic_archive used durable_rename_excl() to avoid overwriting an archive concurrently created by another server. Now, there is a stat() call to avoid overwriting an existing archive a couple of lines above, so note that this change opens a small TOCTOU window in this module between the stat() call and durable_rename(). Furthermore, as mentioned in the top comment of durable_rename_excl(), this routine can result in multiple hard links to the same file and data corruption, with two or more links to the same file in pg_wal/ if a crash happens before the unlink() call during WAL recycling. Specifically, this would produce links to the same file for the current WAL file and the next one because the half-recycled WAL file was re-recycled during crash recovery of a follow-up cluster restart. This change replaces all calls to durable_rename_excl() with durable_rename(). This removes the protection against accidentally overwriting an existing file, but some platforms are already living without it, and all those code paths never expect an existing file (a couple of assertions are added to check after that, in case). This is a bug fix, but knowing the unlikeliness of the problem involving one of more crashes at an exceptionally bad moment, no backpatch is done. This could be revisited in the future. Author: Nathan Bossart Reviewed-by: Robert Haas, Kyotaro Horiguchi, Michael Paquier Discussion: https://postgr.es/m/20220407182954.GA1231544@nathanxps13
1 parent 755df30 commit ccfbd92

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

contrib/basic_archive/basic_archive.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,10 @@ basic_archive_file_internal(const char *file, const char *path)
281281

282282
/*
283283
* Sync the temporary file to disk and move it to its final destination.
284-
* This will fail if destination already exists.
284+
* Note that this will overwrite any existing file, but this is only
285+
* possible if someone else created the file since the stat() above.
285286
*/
286-
(void) durable_rename_excl(temp, destination, ERROR);
287+
(void) durable_rename(temp, destination, ERROR);
287288

288289
ereport(DEBUG1,
289290
(errmsg("archived \"%s\" via basic_archive", file)));

src/backend/access/transam/timeline.c

+6-12
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,11 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
439439

440440
/*
441441
* Now move the completed history file into place with its final name.
442+
* The target file should not exist.
442443
*/
443444
TLHistoryFilePath(path, newTLI);
444-
445-
/*
446-
* Perform the rename using link if available, paranoidly trying to avoid
447-
* overwriting an existing file (there shouldn't be one).
448-
*/
449-
durable_rename_excl(tmppath, path, ERROR);
445+
Assert(access(path, F_OK) != 0 && errno == ENOENT);
446+
durable_rename(tmppath, path, ERROR);
450447

451448
/* The history file can be archived immediately. */
452449
if (XLogArchivingActive())
@@ -517,14 +514,11 @@ writeTimeLineHistoryFile(TimeLineID tli, char *content, int size)
517514

518515
/*
519516
* Now move the completed history file into place with its final name.
517+
* The target file should not exist.
520518
*/
521519
TLHistoryFilePath(path, tli);
522-
523-
/*
524-
* Perform the rename using link if available, paranoidly trying to avoid
525-
* overwriting an existing file (there shouldn't be one).
526-
*/
527-
durable_rename_excl(tmppath, path, ERROR);
520+
Assert(access(path, F_OK) != 0 && errno == ENOENT);
521+
durable_rename(tmppath, path, ERROR);
528522
}
529523

530524
/*

src/backend/access/transam/xlog.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -3323,14 +3323,12 @@ InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
33233323
}
33243324
}
33253325

3326-
/*
3327-
* Perform the rename using link if available, paranoidly trying to avoid
3328-
* overwriting an existing file (there shouldn't be one).
3329-
*/
3330-
if (durable_rename_excl(tmppath, path, LOG) != 0)
3326+
/* The target file should not exist */
3327+
Assert(access(path, F_OK) != 0 && errno == ENOENT);
3328+
if (durable_rename(tmppath, path, LOG) != 0)
33313329
{
33323330
LWLockRelease(ControlFileLock);
3333-
/* durable_rename_excl already emitted log message */
3331+
/* durable_rename already emitted log message */
33343332
return false;
33353333
}
33363334

0 commit comments

Comments
 (0)