@@ -126,8 +126,6 @@ int max_safe_fds = 32; /* default if not changed */
126126/* these are the assigned bits in fdstate below: */
127127#define FD_TEMPORARY (1 << 0) /* T = delete when closed */
128128#define FD_XACT_TEMPORARY (1 << 1) /* T = delete at eoXact */
129- #define FD_XACT_TRANSIENT (1 << 2) /* T = close (not delete) at aoXact,
130- * but keep VFD */
131129
132130typedef struct vfd
133131{
@@ -158,8 +156,11 @@ static Size SizeVfdCache = 0;
158156 */
159157static int nfile = 0 ;
160158
161- /* True if there are files to close/delete at end of transaction */
162- static bool have_pending_fd_cleanup = false;
159+ /*
160+ * Flag to tell whether it's worth scanning VfdCache looking for temp files
161+ * to close
162+ */
163+ static bool have_xact_temporary_files = false;
163164
164165/*
165166 * Tracks the total size of all temporary files. Note: when temp_file_limit
@@ -607,7 +608,6 @@ LruDelete(File file)
607608 Vfd * vfdP ;
608609
609610 Assert (file != 0 );
610- Assert (!FileIsNotOpen (file ));
611611
612612 DO_DB (elog (LOG , "LruDelete %d (%s)" ,
613613 file , VfdCache [file ].fileName ));
@@ -971,7 +971,7 @@ OpenTemporaryFile(bool interXact)
971971 VfdCache [file ].resowner = CurrentResourceOwner ;
972972
973973 /* ensure cleanup happens at eoxact */
974- have_pending_fd_cleanup = true;
974+ have_xact_temporary_files = true;
975975 }
976976
977977 return file ;
@@ -1044,25 +1044,6 @@ OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError)
10441044 return file ;
10451045}
10461046
1047- /*
1048- * Set the transient flag on a file
1049- *
1050- * This flag tells CleanupTempFiles to close the kernel-level file descriptor
1051- * (but not the VFD itself) at end of transaction.
1052- */
1053- void
1054- FileSetTransient (File file )
1055- {
1056- Vfd * vfdP ;
1057-
1058- Assert (FileIsValid (file ));
1059-
1060- vfdP = & VfdCache [file ];
1061- vfdP -> fdstate |= FD_XACT_TRANSIENT ;
1062-
1063- have_pending_fd_cleanup = true;
1064- }
1065-
10661047/*
10671048 * close a file when done with it
10681049 */
@@ -1863,9 +1844,8 @@ AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
18631844 * particularly care which). All still-open per-transaction temporary file
18641845 * VFDs are closed, which also causes the underlying files to be deleted
18651846 * (although they should've been closed already by the ResourceOwner
1866- * cleanup). Transient files have their kernel file descriptors closed.
1867- * Furthermore, all "allocated" stdio files are closed. We also forget any
1868- * transaction-local temp tablespace list.
1847+ * cleanup). Furthermore, all "allocated" stdio files are closed. We also
1848+ * forget any transaction-local temp tablespace list.
18691849 */
18701850void
18711851AtEOXact_Files (void )
@@ -1888,10 +1868,7 @@ AtProcExit_Files(int code, Datum arg)
18881868}
18891869
18901870/*
1891- * General cleanup routine for fd.c.
1892- *
1893- * Temporary files are closed, and their underlying files deleted.
1894- * Transient files are closed.
1871+ * Close temporary files and delete their underlying files.
18951872 *
18961873 * isProcExit: if true, this is being called as the backend process is
18971874 * exiting. If that's the case, we should remove all temporary files; if
@@ -1908,51 +1885,35 @@ CleanupTempFiles(bool isProcExit)
19081885 * Careful here: at proc_exit we need extra cleanup, not just
19091886 * xact_temporary files.
19101887 */
1911- if (isProcExit || have_pending_fd_cleanup )
1888+ if (isProcExit || have_xact_temporary_files )
19121889 {
19131890 Assert (FileIsNotOpen (0 )); /* Make sure ring not corrupted */
19141891 for (i = 1 ; i < SizeVfdCache ; i ++ )
19151892 {
19161893 unsigned short fdstate = VfdCache [i ].fdstate ;
19171894
1918- if (VfdCache [i ].fileName != NULL )
1895+ if (( fdstate & FD_TEMPORARY ) && VfdCache [i ].fileName != NULL )
19191896 {
1920- if (fdstate & FD_TEMPORARY )
1921- {
1922- /*
1923- * If we're in the process of exiting a backend process,
1924- * close all temporary files. Otherwise, only close
1925- * temporary files local to the current transaction. They
1926- * should be closed by the ResourceOwner mechanism
1927- * already, so this is just a debugging cross-check.
1928- */
1929- if (isProcExit )
1930- FileClose (i );
1931- else if (fdstate & FD_XACT_TEMPORARY )
1932- {
1933- elog (WARNING ,
1934- "temporary file %s not closed at end-of-transaction" ,
1935- VfdCache [i ].fileName );
1936- FileClose (i );
1937- }
1938- }
1939- else if (fdstate & FD_XACT_TRANSIENT )
1897+ /*
1898+ * If we're in the process of exiting a backend process, close
1899+ * all temporary files. Otherwise, only close temporary files
1900+ * local to the current transaction. They should be closed by
1901+ * the ResourceOwner mechanism already, so this is just a
1902+ * debugging cross-check.
1903+ */
1904+ if (isProcExit )
1905+ FileClose (i );
1906+ else if (fdstate & FD_XACT_TEMPORARY )
19401907 {
1941- /*
1942- * Close the FD, and remove the entry from the LRU ring,
1943- * but also remove the flag from the VFD. This is to
1944- * ensure that if the VFD is reused in the future for
1945- * non-transient access, we don't close it inappropriately
1946- * then.
1947- */
1948- if (!FileIsNotOpen (i ))
1949- LruDelete (i );
1950- VfdCache [i ].fdstate &= ~FD_XACT_TRANSIENT ;
1908+ elog (WARNING ,
1909+ "temporary file %s not closed at end-of-transaction" ,
1910+ VfdCache [i ].fileName );
1911+ FileClose (i );
19511912 }
19521913 }
19531914 }
19541915
1955- have_pending_fd_cleanup = false;
1916+ have_xact_temporary_files = false;
19561917 }
19571918
19581919 /* Clean up "allocated" stdio files and dirs. */
0 commit comments