Commit 5817d861 authored by Alvaro Herrera's avatar Alvaro Herrera

Optimize CleanupTempFiles by having a boolean flag that keeps track of whether

there are FD_XACT_TEMPORARY files to clean up at transaction end.

Per performance profiling results on AWeber's huge systems.

Patch by me after an idea suggested by Simon Riggs.
parent 9fe79c39
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.144 2008/03/10 20:06:27 tgl Exp $ * $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.145 2008/09/19 04:57:10 alvherre Exp $
* *
* NOTES: * NOTES:
* *
...@@ -121,6 +121,12 @@ static int max_safe_fds = 32; /* default if not changed */ ...@@ -121,6 +121,12 @@ static int max_safe_fds = 32; /* default if not changed */
#define FD_TEMPORARY (1 << 0) /* T = delete when closed */ #define FD_TEMPORARY (1 << 0) /* T = delete when closed */
#define FD_XACT_TEMPORARY (1 << 1) /* T = delete at eoXact */ #define FD_XACT_TEMPORARY (1 << 1) /* T = delete at eoXact */
/*
* Flag to tell whether it's worth scanning VfdCache looking for temp files to
* close
*/
static bool have_xact_temporary_files = false;
typedef struct vfd typedef struct vfd
{ {
int fd; /* current FD, or VFD_CLOSED if none */ int fd; /* current FD, or VFD_CLOSED if none */
...@@ -889,6 +895,9 @@ OpenTemporaryFile(bool interXact) ...@@ -889,6 +895,9 @@ OpenTemporaryFile(bool interXact)
{ {
VfdCache[file].fdstate |= FD_XACT_TEMPORARY; VfdCache[file].fdstate |= FD_XACT_TEMPORARY;
VfdCache[file].create_subid = GetCurrentSubTransactionId(); VfdCache[file].create_subid = GetCurrentSubTransactionId();
/* ensure cleanup happens at eoxact */
have_xact_temporary_files = true;
} }
return file; return file;
...@@ -1608,7 +1617,7 @@ AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, ...@@ -1608,7 +1617,7 @@ AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
{ {
Index i; Index i;
if (SizeVfdCache > 0) if (have_xact_temporary_files)
{ {
Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */ Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
for (i = 1; i < SizeVfdCache; i++) for (i = 1; i < SizeVfdCache; i++)
...@@ -1684,7 +1693,11 @@ CleanupTempFiles(bool isProcExit) ...@@ -1684,7 +1693,11 @@ CleanupTempFiles(bool isProcExit)
{ {
Index i; Index i;
if (SizeVfdCache > 0) /*
* Careful here: at proc_exit we need extra cleanup, not just
* xact_temporary files.
*/
if (isProcExit || have_xact_temporary_files)
{ {
Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */ Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
for (i = 1; i < SizeVfdCache; i++) for (i = 1; i < SizeVfdCache; i++)
...@@ -1702,6 +1715,8 @@ CleanupTempFiles(bool isProcExit) ...@@ -1702,6 +1715,8 @@ CleanupTempFiles(bool isProcExit)
FileClose(i); FileClose(i);
} }
} }
have_xact_temporary_files = false;
} }
while (numAllocatedDescs > 0) while (numAllocatedDescs > 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment