1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-15 05:46:52 +03:00

Expand the use of get_dirent_type(), shaving a few calls to stat()/lstat()

Several backend-side loops scanning one or more directories with
ReadDir() (WAL segment recycle/removal in xlog.c, backend-side directory
copy, temporary file removal, configuration file parsing, some logical
decoding logic and some pgtz stuff) already know the type of the entry
being scanned thanks to the dirent structure associated to the entry, on
platforms where we know about DT_REG, DT_DIR and DT_LNK to make the
difference between a regular file, a directory and a symbolic link.

Relying on the direct structure of an entry saves a few system calls to
stat() and lstat() in the loops updated here, shaving some code while on
it.  The logic of the code remains the same, calling stat() or lstat()
depending on if it is necessary to look through symlinks.

Authors: Nathan Bossart, Bharath Rupireddy
Reviewed-by: Andres Freund, Thomas Munro, Michael Paquier
Discussion: https://postgr.es/m/CALj2ACV8n-J-f=yiLUOx2=HrQGPSOZM3nWzyQQvLPcccPXxEdg@mail.gmail.com
This commit is contained in:
Michael Paquier
2022-09-02 16:58:06 +09:00
parent 11e5f99d39
commit bfb9dfd937
8 changed files with 53 additions and 75 deletions

View File

@@ -657,8 +657,9 @@ static void PreallocXlogFiles(XLogRecPtr endptr, TimeLineID tli);
static void RemoveTempXlogFiles(void);
static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr,
XLogRecPtr endptr, TimeLineID insertTLI);
static void RemoveXlogFile(const char *segname, XLogSegNo recycleSegNo,
XLogSegNo *endlogSegNo, TimeLineID insertTLI);
static void RemoveXlogFile(const struct dirent *segment_de,
XLogSegNo recycleSegNo, XLogSegNo *endlogSegNo,
TimeLineID insertTLI);
static void UpdateLastRemovedPtr(char *filename);
static void ValidateXLOGDirectoryStructure(void);
static void CleanupBackupHistory(void);
@@ -3596,8 +3597,7 @@ RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr,
/* Update the last removed location in shared memory first */
UpdateLastRemovedPtr(xlde->d_name);
RemoveXlogFile(xlde->d_name, recycleSegNo, &endlogSegNo,
insertTLI);
RemoveXlogFile(xlde, recycleSegNo, &endlogSegNo, insertTLI);
}
}
}
@@ -3669,8 +3669,7 @@ RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
* - but seems safer to let them be archived and removed later.
*/
if (!XLogArchiveIsReady(xlde->d_name))
RemoveXlogFile(xlde->d_name, recycleSegNo, &endLogSegNo,
newTLI);
RemoveXlogFile(xlde, recycleSegNo, &endLogSegNo, newTLI);
}
}
@@ -3680,9 +3679,9 @@ RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
/*
* Recycle or remove a log file that's no longer needed.
*
* segname is the name of the segment to recycle or remove. recycleSegNo
* is the segment number to recycle up to. endlogSegNo is the segment
* number of the current (or recent) end of WAL.
* segment_de is the dirent structure of the segment to recycle or remove.
* recycleSegNo is the segment number to recycle up to. endlogSegNo is
* the segment number of the current (or recent) end of WAL.
*
* endlogSegNo gets incremented if the segment is recycled so as it is not
* checked again with future callers of this function.
@@ -3691,14 +3690,15 @@ RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
* should be used for this timeline.
*/
static void
RemoveXlogFile(const char *segname, XLogSegNo recycleSegNo,
XLogSegNo *endlogSegNo, TimeLineID insertTLI)
RemoveXlogFile(const struct dirent *segment_de,
XLogSegNo recycleSegNo, XLogSegNo *endlogSegNo,
TimeLineID insertTLI)
{
char path[MAXPGPATH];
#ifdef WIN32
char newpath[MAXPGPATH];
#endif
struct stat statbuf;
const char *segname = segment_de->d_name;
snprintf(path, MAXPGPATH, XLOGDIR "/%s", segname);
@@ -3710,7 +3710,7 @@ RemoveXlogFile(const char *segname, XLogSegNo recycleSegNo,
if (wal_recycle &&
*endlogSegNo <= recycleSegNo &&
XLogCtl->InstallXLogFileSegmentActive && /* callee rechecks this */
lstat(path, &statbuf) == 0 && S_ISREG(statbuf.st_mode) &&
get_dirent_type(path, segment_de, false, DEBUG2) == PGFILETYPE_REG &&
InstallXLogFileSegment(endlogSegNo, path,
true, recycleSegNo, insertTLI))
{