Remove sort files - Mailing list pgsql-patches

From Bruce Momjian
Subject Remove sort files
Date
Msg-id 200105230036.f4N0aE816155@candle.pha.pa.us
Whole thread Raw
Responses Re: Remove sort files  (Peter Eisentraut <peter_e@gmx.net>)
List pgsql-patches
We have the TODO item:

    * Remove unused files during database vacuum or postmaster startup

The following patch places sort files in a separate directory (created
when first needed), and removes old sort files on postmaster startup.

The only unusual part is the use of system("find...rm...") to remove the
files in pg_sorttemp.  Seemed cleaner than doing fancy directory
walking code in C.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
Index: src/backend/postmaster/postmaster.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/postmaster/postmaster.c,v
retrieving revision 1.212
diff -c -r1.212 postmaster.c
*** src/backend/postmaster/postmaster.c    2001/04/19 19:09:23    1.212
--- src/backend/postmaster/postmaster.c    2001/05/23 00:31:42
***************
*** 243,248 ****
--- 243,249 ----
  static void SignalChildren(int signal);
  static int    CountChildren(void);
  static bool CreateOptsFile(int argc, char *argv[]);
+ static void RemovePgSorttemp(void);

  static pid_t SSDataBase(int xlop);

***************
*** 595,600 ****
--- 596,604 ----
      if (!CreateDataDirLockFile(DataDir, true))
          ExitPostmaster(1);

+     /* Remove old sort files */
+     RemovePgSorttemp();
+
      /*
       * Establish input sockets.
       */
***************
*** 2449,2452 ****
--- 2453,2474 ----

      fclose(fp);
      return true;
+ }
+
+
+ /*
+  * Remove old sort files
+  */
+ static void
+ RemovePgSorttemp(void)
+ {
+     char clear_pg_sorttemp[1024];
+
+     /* Don't remove directory in case it is a symlink */
+     snprintf(clear_pg_sorttemp, sizeof(clear_pg_sorttemp),
+             "find \"%s\"/base -name pg_sorttemp -type d -exec sh -c \"rm -f {}/*\" \\;",
+             DataDir);
+     /* Make sure we have the full 'find' command */
+     if (strlen(clear_pg_sorttemp)+1 < 1024)
+         system(clear_pg_sorttemp);
  }
Index: src/backend/storage/file/fd.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/storage/file/fd.c,v
retrieving revision 1.76
diff -c -r1.76 fd.c
*** src/backend/storage/file/fd.c    2001/04/03 04:07:02    1.76
--- src/backend/storage/file/fd.c    2001/05/23 00:31:42
***************
*** 90,95 ****
--- 90,97 ----

  #define VFD_CLOSED (-1)

+ #define TEMP_SORT_DIR "pg_sorttemp"
+
  #define FileIsValid(file) \
      ((file) > 0 && (file) < (int) SizeVfdCache && VfdCache[file].fileName != NULL)

***************
*** 742,762 ****
  File
  OpenTemporaryFile(void)
  {
!     char        tempfilename[64];
      File        file;

      /*
       * Generate a tempfile name that's unique within the current
       * transaction
       */
!     snprintf(tempfilename, sizeof(tempfilename),
!              "pg_sorttemp%d.%ld", MyProcPid, tempFileCounter++);

      /* Open the file */
!     file = FileNameOpenFile(tempfilename,
                              O_RDWR | O_CREAT | O_TRUNC | PG_BINARY, 0600);
      if (file <= 0)
!         elog(ERROR, "Failed to create temporary file %s", tempfilename);

      /* Mark it for deletion at close or EOXact */
      VfdCache[file].fdstate |= FD_TEMPORARY;
--- 744,772 ----
  File
  OpenTemporaryFile(void)
  {
!     char        tempfilepath[128];
      File        file;

      /*
       * Generate a tempfile name that's unique within the current
       * transaction
       */
!     snprintf(tempfilepath, sizeof(tempfilepath),
!              "%s%c%d.%ld", TEMP_SORT_DIR, SEP_CHAR, MyProcPid,
!              tempFileCounter++);

      /* Open the file */
!     file = FileNameOpenFile(tempfilepath,
                              O_RDWR | O_CREAT | O_TRUNC | PG_BINARY, 0600);
      if (file <= 0)
!     {
!         /* mkdir could fail if some one else already created it */
!         mkdir(TEMP_SORT_DIR, S_IRWXU);
!         file = FileNameOpenFile(tempfilepath,
!                             O_RDWR | O_CREAT | O_TRUNC | PG_BINARY, 0600);
!         if (file <= 0)
!             elog(ERROR, "Failed to create temporary file %s", tempfilepath);
!     }

      /* Mark it for deletion at close or EOXact */
      VfdCache[file].fdstate |= FD_TEMPORARY;

pgsql-patches by date:

Previous
From: "Christopher Kings-Lynne"
Date:
Subject: A patch that adds primary key and unique key support to psql's \d
Next
From: "Michael Richards"
Date:
Subject: Re: Remove sort files