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

Commit1f95181

Browse files
committed
Replace durable_rename_excl() by durable_rename(), take two
durable_rename_excl() attempts to avoid overwriting any existing filesby using link() and unlink(), and it falls back to rename() on someplatforms (aka WIN32), which offers no such overwrite protection. Mostcallers use durable_rename_excl() just in case there is an existingfile, but in practice there shouldn't be one (see below for moredetails).Furthermore, failures during durable_rename_excl() can result inmultiple hard links to the same file. As per Nathan's tests, it ispossible to end up with two links to the same file in pg_wal after acrash just before unlink() during WAL recycling. Specifically, the testproduced links to the same file for the current WAL file and the nextone because the half-recycled WAL file was re-recycled upon restarting,leading to WAL corruption.This change replaces all the calls of durable_rename_excl() todurable_rename(). This removes the protection against accidentallyoverwriting an existing file, but some platforms are already livingwithout it and ordinarily there shouldn't be one. The function itselfis left around in case any extensions are using it. It will be removedon HEAD via a follow-up commit.Here is a summary of the existing callers of durable_rename_excl() (seesecond discussion link at the bottom), replaced by this commit. First,basic_archive used it to avoid overwriting an archive concurrentlycreated by another server, but as mentioned above, it will stilloverwrite files on some platforms. Second, xlog.c uses it to recyclepast WAL segments, where an overwrite should not happen (origin of thechange atf0e37a8) because there are protections about the WAL segmentto select when recycling an entry. The third and last area is relatedto the write of timeline history files. writeTimeLineHistory() willwrite a new timeline history file at the end of recovery on promotion,so there should be no such files for the same timeline.What remains is writeTimeLineHistoryFile(), that can be used in parallelby a WAL receiver and the startup process, and some digging of thebuildfarm shows that EEXIST from a WAL receiver can happen with an errorof "could not link file \"pg_wal/xlogtemp.NN\" to \"pg_wal/MM.history\",which would cause an automatic restart of the WAL receiver as it ispromoted to FATAL, hence this should improve the stability of the WALreceiver as rename() would overwrite an existing TLI history filealready fetched by the startup process at recovery.This is the second time this change is attempted,ccfbd92 being thefirst one, but this time no assertions are added for the case of a TLIhistory file written concurrently by the WAL receiver or the startupprocess because we can expect one to exist (some of the TAP tests areable to trigger with a proper timing).This commit has been originally applied on v16~ as ofdac1ff3, andwe have received more reports of this issue, where clusters can becomecorrupted at replay in older stable branches with multiple linkspointing to the same physical WAL segment file. This backpatchaddresses the problem for the v13~v15 range.Author: Nathan BossartReviewed-by: Robert Haas, Kyotaro Horiguchi, Michael PaquierDiscussion:https://postgr.es/m/20220407182954.GA1231544@nathanxps13Discussion:https://postgr.es/m/Ym6GZbqQdlalSKSG@paquier.xyzDiscussion:https://postgr.es/m/CAJhEC04tBkYPF4q2uS_rCytauvNEVqdBAzasBEokfceFhF=KDQ@mail.gmail.com
1 parentbdb07d2 commit1f95181

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

‎src/backend/access/transam/timeline.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,8 @@ writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
441441
* Now move the completed history file into place with its final name.
442442
*/
443443
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);
444+
Assert(access(path,F_OK)!=0&&errno==ENOENT);
445+
durable_rename(tmppath,path,ERROR);
450446

451447
/* The history file can be archived immediately. */
452448
if (XLogArchivingActive())
@@ -516,15 +512,11 @@ writeTimeLineHistoryFile(TimeLineID tli, char *content, int size)
516512
errmsg("could not close file \"%s\": %m",tmppath)));
517513

518514
/*
519-
* Now move the completed history file into place with its final name.
515+
* Now move the completed history file into place with its final name,
516+
* replacing any existing file with the same name.
520517
*/
521518
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);
519+
durable_rename(tmppath,path,ERROR);
528520
}
529521

530522
/*

‎src/backend/access/transam/xlog.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3693,15 +3693,12 @@ InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
36933693
}
36943694
}
36953695

3696-
/*
3697-
* Perform the rename using link if available, paranoidly trying to avoid
3698-
* overwriting an existing file (there shouldn't be one).
3699-
*/
3700-
if (durable_rename_excl(tmppath,path,LOG)!=0)
3696+
Assert(access(path,F_OK)!=0&&errno==ENOENT);
3697+
if (durable_rename(tmppath,path,LOG)!=0)
37013698
{
37023699
if (use_lock)
37033700
LWLockRelease(ControlFileLock);
3704-
/*durable_rename_excl already emitted log message */
3701+
/*durable_rename already emitted log message */
37053702
return false;
37063703
}
37073704

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp