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

Commit5fc1008

Browse files
committed
Clean up temporary WAL segments after an instance crash
Temporary WAL segments are created in pg_wal and named as xlogtemp.pidbefore being renamed to the real deal when creating a new segment. Ifan instance crashes after the temporary segment is created and beforethe rename is done, then the server would finish with unremovable data.After an instance crash, scan pg_wal and remove any such segments. Withrepetitive unlucky crashes this would contribute to disk bloat andpresents risks of ENOSPC especially with max_wal_size close to themaximum allowed.Author: Michael PaquierReviewed-by: Yugo Nagata, Heikki LinnakangasDiscussion:https://postgr.es/m/20180514054955.GF1528@paquier.xyz
1 parent5e6e2c8 commit5fc1008

File tree

1 file changed

+45
-7
lines changed
  • src/backend/access/transam

1 file changed

+45
-7
lines changed

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ static bool WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
887887
staticintemode_for_corrupt_record(intemode,XLogRecPtrRecPtr);
888888
staticvoidXLogFileClose(void);
889889
staticvoidPreallocXlogFiles(XLogRecPtrendptr);
890+
staticvoidRemoveTempXlogFiles(void);
890891
staticvoidRemoveOldXlogFiles(XLogSegNosegno,XLogRecPtrPriorRedoPtr,XLogRecPtrendptr);
891892
staticvoidRemoveXlogFile(constchar*segname,XLogRecPtrPriorRedoPtr,XLogRecPtrendptr);
892893
staticvoidUpdateLastRemovedPtr(char*filename);
@@ -3863,6 +3864,35 @@ UpdateLastRemovedPtr(char *filename)
38633864
SpinLockRelease(&XLogCtl->info_lck);
38643865
}
38653866

3867+
/*
3868+
* Remove all temporary log files in pg_wal
3869+
*
3870+
* This is called at the beginning of recovery after a previous crash,
3871+
* at a point where no other processes write fresh WAL data.
3872+
*/
3873+
staticvoid
3874+
RemoveTempXlogFiles(void)
3875+
{
3876+
DIR*xldir;
3877+
structdirent*xlde;
3878+
3879+
elog(DEBUG2,"removing all temporary WAL segments");
3880+
3881+
xldir=AllocateDir(XLOGDIR);
3882+
while ((xlde=ReadDir(xldir,XLOGDIR))!=NULL)
3883+
{
3884+
charpath[MAXPGPATH];
3885+
3886+
if (strncmp(xlde->d_name,"xlogtemp.",9)!=0)
3887+
continue;
3888+
3889+
snprintf(path,MAXPGPATH,XLOGDIR"/%s",xlde->d_name);
3890+
unlink(path);
3891+
elog(DEBUG2,"removed temporary WAL segment \"%s\"",path);
3892+
}
3893+
FreeDir(xldir);
3894+
}
3895+
38663896
/*
38673897
* Recycle or remove all log files older or equal to passed segno.
38683898
*
@@ -6379,17 +6409,25 @@ StartupXLOG(void)
63796409
*/
63806410
ValidateXLOGDirectoryStructure();
63816411

6382-
/*
6383-
* If we previously crashed, there might be data which we had written,
6384-
* intending to fsync it, but which we had not actually fsync'd yet.
6385-
* Therefore, a power failure in the near future might cause earlier
6386-
* unflushed writes to be lost, even though more recent data written to
6387-
* disk from here on would be persisted. To avoid that, fsync the entire
6388-
* data directory.
6412+
/*----------
6413+
* If we previously crashed, perform a couple of actions:
6414+
*- The pg_wal directory may still include some temporary WAL segments
6415+
* used when creating a new segment, so perform some clean up to not
6416+
* bloat this path. This is done first as there is no point to sync this
6417+
* temporary data.
6418+
*- There might be data which we had written, intending to fsync it,
6419+
* but which we had not actually fsync'd yet. Therefore, a power failure
6420+
* in the near future might cause earlier unflushed writes to be lost,
6421+
* even though more recent data written to disk from here on would be
6422+
* persisted. To avoid that, fsync the entire data directory.
6423+
*---------
63896424
*/
63906425
if (ControlFile->state!=DB_SHUTDOWNED&&
63916426
ControlFile->state!=DB_SHUTDOWNED_IN_RECOVERY)
6427+
{
6428+
RemoveTempXlogFiles();
63926429
SyncDataDirectory();
6430+
}
63936431

63946432
/*
63956433
* Initialize on the assumption we want to recover to the latest timeline

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp