From 9b4683c6d3ad798f02ba0d084d26ddc0283d1ef1 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Sun, 5 Dec 2021 19:38:20 -0800 Subject: [PATCH v1 2/5] Also remove pgsql_tmp directories during startup. Presently, the server only removes the contents of the temporary directories during startup, not the directory itself. This changes that to prepare for future commits that will move temporary file cleanup to a separate auxiliary process. --- src/backend/postmaster/postmaster.c | 2 +- src/backend/storage/file/fd.c | 20 ++++++++++---------- src/include/storage/fd.h | 4 ++-- src/test/recovery/t/022_crash_temp_files.pl | 6 ++++-- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 635313cdb7..51613aaa2a 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -1117,7 +1117,7 @@ PostmasterMain(int argc, char *argv[]) * safe to do so now, because we verified earlier that there are no * conflicting Postgres processes in this data directory. */ - RemovePgTempFilesInDir(PG_TEMP_FILES_DIR, true, false); + RemovePgTempDir(PG_TEMP_FILES_DIR, true, false); #endif /* diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 263057841d..545e91978c 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -3160,7 +3160,7 @@ RemovePgTempFiles(void) * First process temp files in pg_default ($PGDATA/base) */ snprintf(temp_path, sizeof(temp_path), "base/%s", PG_TEMP_FILES_DIR); - RemovePgTempFilesInDir(temp_path, true, false); + RemovePgTempDir(temp_path, true, false); RemovePgTempRelationFiles("base"); /* @@ -3176,7 +3176,7 @@ RemovePgTempFiles(void) snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s/%s", spc_de->d_name, TABLESPACE_VERSION_DIRECTORY, PG_TEMP_FILES_DIR); - RemovePgTempFilesInDir(temp_path, true, false); + RemovePgTempDir(temp_path, true, false); snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s", spc_de->d_name, TABLESPACE_VERSION_DIRECTORY); @@ -3209,7 +3209,7 @@ RemovePgTempFiles(void) * them separate.) */ void -RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all) +RemovePgTempDir(const char *tmpdirname, bool missing_ok, bool unlink_all) { DIR *temp_dir; struct dirent *temp_de; @@ -3247,13 +3247,7 @@ RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all) if (S_ISDIR(statbuf.st_mode)) { /* recursively remove contents, then directory itself */ - RemovePgTempFilesInDir(rm_path, false, true); - - if (rmdir(rm_path) < 0) - ereport(LOG, - (errcode_for_file_access(), - errmsg("could not remove directory \"%s\": %m", - rm_path))); + RemovePgTempDir(rm_path, false, true); } else { @@ -3271,6 +3265,12 @@ RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all) } FreeDir(temp_dir); + + if (rmdir(tmpdirname) < 0) + ereport(LOG, + (errcode_for_file_access(), + errmsg("could not remove directory \"%s\": %m", + tmpdirname))); } /* Process one tablespace directory, look for per-DB subdirectories */ diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h index 34602ae006..762f6b46c1 100644 --- a/src/include/storage/fd.h +++ b/src/include/storage/fd.h @@ -169,8 +169,8 @@ extern void AtEOXact_Files(bool isCommit); extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid); extern void RemovePgTempFiles(void); -extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, - bool unlink_all); +extern void RemovePgTempDir(const char *tmpdirname, bool missing_ok, + bool unlink_all); extern bool looks_like_temp_rel_name(const char *name); extern int pg_fsync(int fd); diff --git a/src/test/recovery/t/022_crash_temp_files.pl b/src/test/recovery/t/022_crash_temp_files.pl index bf95a30761..481f1f23a2 100644 --- a/src/test/recovery/t/022_crash_temp_files.pl +++ b/src/test/recovery/t/022_crash_temp_files.pl @@ -143,7 +143,8 @@ $node->poll_query_until('postgres', undef, ''); # Check for temporary files is( $node->safe_psql( - 'postgres', 'SELECT COUNT(1) FROM pg_ls_dir($$base/pgsql_tmp$$)'), + 'postgres', + 'SELECT COUNT(1) FROM pg_ls_dir($$base$$) WHERE pg_ls_dir = \'pgsql_tmp\''), qq(0), 'no temporary files'); @@ -241,7 +242,8 @@ $node->restart(); # Check the temporary files -- should be gone is( $node->safe_psql( - 'postgres', 'SELECT COUNT(1) FROM pg_ls_dir($$base/pgsql_tmp$$)'), + 'postgres', + 'SELECT COUNT(1) FROM pg_ls_dir($$base$$) WHERE pg_ls_dir = \'pgsql_tmp\''), qq(0), 'temporary file was removed'); -- 2.16.6