* Portions Copyright (c) 1994, Regents of the University of California
  *
  * 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:
  *
 #define FD_TEMPORARY       (1 << 0)    /* T = delete when closed */
 #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
 {
    int         fd;             /* current FD, or VFD_CLOSED if none */
    {
        VfdCache[file].fdstate |= FD_XACT_TEMPORARY;
        VfdCache[file].create_subid = GetCurrentSubTransactionId();
+
+       /* ensure cleanup happens at eoxact */
+       have_xact_temporary_files = true;
    }
 
    return file;
 {
    Index       i;
 
-   if (SizeVfdCache > 0)
+   if (have_xact_temporary_files)
    {
        Assert(FileIsNotOpen(0));       /* Make sure ring not corrupted */
        for (i = 1; i < SizeVfdCache; 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 */
        for (i = 1; i < SizeVfdCache; i++)
                    FileClose(i);
            }
        }
+
+       have_xact_temporary_files = false;
    }
 
    while (numAllocatedDescs > 0)