diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2018-04-28 17:45:02 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2018-04-28 17:45:02 -0400 |
commit | 9cb7db3f0c1f554cdcbbd97f78a119a19756e6ef (patch) | |
tree | 0e8c68a72d14e3218264284990de9c3bf768e36f /src/backend/storage/file/fd.c | |
parent | cfffe83ba82021a1819a656e7ec5c28fb3a99152 (diff) | |
download | postgresql-9cb7db3f0c1f554cdcbbd97f78a119a19756e6ef.tar.gz postgresql-9cb7db3f0c1f554cdcbbd97f78a119a19756e6ef.zip |
In AtEOXact_Files, complain if any files remain unclosed at commit.
This change makes this module act more like most of our other low-level
resource management modules. It's a caller error if something is not
explicitly closed by the end of a successful transaction, so issue
a WARNING about it. This would not actually have caught the file leak
bug fixed in commit 231bcd080, because that was in a transaction-abort
path; but it still seems like a good, and pretty cheap, cross-check.
Discussion: https://postgr.es/m/152056616579.4966.583293218357089052@wrigleys.postgresql.org
Diffstat (limited to 'src/backend/storage/file/fd.c')
-rw-r--r-- | src/backend/storage/file/fd.c | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index f772dfe93f7..afce5dadc09 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -314,7 +314,7 @@ static bool reserveAllocatedDesc(void); static int FreeDesc(AllocateDesc *desc); static void AtProcExit_Files(int code, Datum arg); -static void CleanupTempFiles(bool isProcExit); +static void CleanupTempFiles(bool isCommit, bool isProcExit); static void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all); static void RemovePgTempRelationFiles(const char *tsdirname); @@ -2902,17 +2902,19 @@ AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, /* * AtEOXact_Files * - * This routine is called during transaction commit or abort (it doesn't - * particularly care which). All still-open per-transaction temporary file - * VFDs are closed, which also causes the underlying files to be deleted - * (although they should've been closed already by the ResourceOwner - * cleanup). Furthermore, all "allocated" stdio files are closed. We also - * forget any transaction-local temp tablespace list. + * This routine is called during transaction commit or abort. All still-open + * per-transaction temporary file VFDs are closed, which also causes the + * underlying files to be deleted (although they should've been closed already + * by the ResourceOwner cleanup). Furthermore, all "allocated" stdio files are + * closed. We also forget any transaction-local temp tablespace list. + * + * The isCommit flag is used only to decide whether to emit warnings about + * unclosed files. */ void -AtEOXact_Files(void) +AtEOXact_Files(bool isCommit) { - CleanupTempFiles(false); + CleanupTempFiles(isCommit, false); tempTableSpaces = NULL; numTempTableSpaces = -1; } @@ -2926,12 +2928,15 @@ AtEOXact_Files(void) static void AtProcExit_Files(int code, Datum arg) { - CleanupTempFiles(true); + CleanupTempFiles(false, true); } /* * Close temporary files and delete their underlying files. * + * isCommit: if true, this is normal transaction commit, and we don't + * expect any remaining files; warn if there are some. + * * isProcExit: if true, this is being called as the backend process is * exiting. If that's the case, we should remove all temporary files; if * that's not the case, we are being called for transaction commit/abort @@ -2939,7 +2944,7 @@ AtProcExit_Files(int code, Datum arg) * also clean up "allocated" stdio files, dirs and fds. */ static void -CleanupTempFiles(bool isProcExit) +CleanupTempFiles(bool isCommit, bool isProcExit) { Index i; @@ -2979,6 +2984,11 @@ CleanupTempFiles(bool isProcExit) have_xact_temporary_files = false; } + /* Complain if any allocated files remain open at commit. */ + if (isCommit && numAllocatedDescs > 0) + elog(WARNING, "%d temporary files and directories not closed at end-of-transaction", + numAllocatedDescs); + /* Clean up "allocated" stdio files, dirs and fds. */ while (numAllocatedDescs > 0) FreeDesc(&allocatedDescs[0]); |