From bc755b58a982956e2a494fab91a49c0142f84030 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Sun, 5 Dec 2021 21:42:52 -0800 Subject: [PATCH v1 4/5] Move pgsql_tmp file removal to custodian process. With this change, startup (and restart after a crash) simply renames the pgsql_tmp directories, and the custodian process actually removes all the files in the staged directories as well as the staged directories themselves. This should help avoid long startup delays due to many leftover temporary files. --- src/backend/postmaster/custodian.c | 13 ++++++++++++- src/backend/postmaster/postmaster.c | 14 +++++++++----- src/backend/storage/file/fd.c | 22 ++++++++++++++++------ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/backend/postmaster/custodian.c b/src/backend/postmaster/custodian.c index 0ba59949bb..a5443f9a21 100644 --- a/src/backend/postmaster/custodian.c +++ b/src/backend/postmaster/custodian.c @@ -191,7 +191,18 @@ CustodianMain(void) start_time = (pg_time_t) time(NULL); - /* TODO: offloaded tasks go here */ + /* + * Remove any pgsql_tmp directories that have been staged for deletion. + * Since pgsql_tmp directories can accumulate many files, removing all + * of the files during startup (which we used to do) can take a very + * long time. To avoid delaying startup, we simply have startup rename + * the temporary directories, and we clean them up here. + * + * pgsql_tmp directories are not staged or cleaned in single-user mode, + * so we don't need any extra handling outside of the custodian process + * for this. + */ + RemovePgTempFiles(false, false); /* Calculate how long to sleep */ end_time = (pg_time_t) time(NULL); diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 1ae2dc179e..b098482496 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -1391,9 +1391,11 @@ PostmasterMain(int argc, char *argv[]) /* * Remove old temporary files. At this point there can be no other * Postgres processes running in this directory, so this should be safe. + * + * Note that this just stages the pgsql_tmp directories for deletion. The + * custodian process is responsible for actually removing the files. */ RemovePgTempFiles(true, true); - RemovePgTempFiles(false, false); /* * Initialize stats collection subsystem (this does NOT start the @@ -4139,12 +4141,14 @@ PostmasterStateMachine(void) ereport(LOG, (errmsg("all server processes terminated; reinitializing"))); - /* remove leftover temporary files after a crash */ + /* + * Remove leftover temporary files after a crash. + * + * Note that this just stages the pgsql_tmp directories for deletion. + * The custodian process is responsible for actually removing the files. + */ if (remove_temp_files_after_crash) - { RemovePgTempFiles(true, true); - RemovePgTempFiles(false, false); - } /* allow background workers to immediately restart */ ResetBackgroundWorkerCrashTimes(); diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 633c6eee18..0807f9c590 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -100,6 +100,8 @@ #include "postmaster/startup.h" #include "storage/fd.h" #include "storage/ipc.h" +#include "storage/latch.h" +#include "storage/proc.h" #include "utils/guc.h" #include "utils/resowner_private.h" @@ -1640,9 +1642,9 @@ PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode) * * Directories created within the top-level temporary directory should begin * with PG_TEMP_FILE_PREFIX, so that they can be identified as temporary and - * deleted at startup by RemovePgTempFiles(). Further subdirectories below - * that do not need any particular prefix. -*/ + * deleted by RemovePgTempFiles(). Further subdirectories below that do not + * need any particular prefix. + */ void PathNameCreateTemporaryDir(const char *basedir, const char *directory) { @@ -1840,9 +1842,9 @@ OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError) * * If the file is inside the top-level temporary directory, its name should * begin with PG_TEMP_FILE_PREFIX so that it can be identified as temporary - * and deleted at startup by RemovePgTempFiles(). Alternatively, it can be - * inside a directory created with PathNameCreateTemporaryDir(), in which case - * the prefix isn't needed. + * and deleted by RemovePgTempFiles(). Alternatively, it can be inside a + * directory created with PathNameCreateTemporaryDir(), in which case the prefix + * isn't needed. */ File PathNameCreateTemporaryFile(const char *path, bool error_on_failure) @@ -3211,6 +3213,14 @@ RemovePgTempFiles(bool stage, bool remove_relation_files) * would create a race condition. It's done separately, earlier in * postmaster startup. */ + + /* + * If we just staged some pgsql_tmp directories for removal, wake up the + * custodian process so that it deletes all the files in the staged + * directories as well as the directories themselves. + */ + if (stage && ProcGlobal->custodianLatch) + SetLatch(ProcGlobal->custodianLatch); } /* -- 2.16.6