Auto create (top level) directory for create tablespace - Mailing list pgsql-patches

From Mark Kirkwood
Subject Auto create (top level) directory for create tablespace
Date
Msg-id 476460E7.9060009@paradise.net.nz
Whole thread Raw
Responses Re: Auto create (top level) directory for create tablespace
List pgsql-patches
I thought it made sense for CREATE TABLESPACE to attempt to create the
top level location directory - and also for tablespace redo to do
likwewise during WAL replay.

Tablespace creation then behaves a bit more like intidb with respect to
directory creation, which I found quite nice.

Patch against HEAD, passes regression tests.

Cheers

Mark
Index: src/backend/commands/tablespace.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/commands/tablespace.c,v
retrieving revision 1.51
diff -c -r1.51 tablespace.c
*** src/backend/commands/tablespace.c    15 Nov 2007 21:14:34 -0000    1.51
--- src/backend/commands/tablespace.c    15 Dec 2007 19:04:45 -0000
***************
*** 199,204 ****
--- 199,205 ----
      Oid            tablespaceoid;
      char       *location;
      char       *linkloc;
+     struct stat    st;
      Oid            ownerId;

      /* Must be super user */
***************
*** 297,302 ****
--- 298,317 ----
      recordDependencyOnOwner(TableSpaceRelationId, tablespaceoid, ownerId);

      /*
+      * Try to create the target directory if it does not exist.
+      */
+     if (stat(location, &st) < 0)
+     {
+         if (mkdir(location, 0700) != 0)
+                 ereport(ERROR,
+                     (errcode_for_file_access(),
+                  errmsg("could not create location directory \"%s\": %m",
+                         location)));
+
+     }
+
+
+     /*
       * Attempt to coerce target directory to safe permissions.    If this fails,
       * it doesn't exist or has the wrong owner.
       */
***************
*** 1279,1284 ****
--- 1294,1314 ----
          xl_tblspc_create_rec *xlrec = (xl_tblspc_create_rec *) XLogRecGetData(record);
          char       *location = xlrec->ts_path;
          char       *linkloc;
+         struct stat    st;
+
+         /*
+          * Try to create the target directory if it does not exist.
+          */
+         if (stat(location, &st) < 0)
+         {
+             if (mkdir(location, 0700) != 0)
+             {
+                 ereport(ERROR,
+                         (errcode_for_file_access(),
+                      errmsg("could not create location directory \"%s\": %m",
+                             location)));
+             }
+         }

          /*
           * Attempt to coerce target directory to safe permissions.    If this
Index: src/test/regress/output/tablespace.source
===================================================================
RCS file: /projects/cvsroot/pgsql/src/test/regress/output/tablespace.source,v
retrieving revision 1.5
diff -c -r1.5 tablespace.source
*** src/test/regress/output/tablespace.source    3 Jun 2007 22:16:03 -0000    1.5
--- src/test/regress/output/tablespace.source    15 Dec 2007 19:04:46 -0000
***************
*** 57,63 ****

  -- Will fail with bad path
  CREATE TABLESPACE badspace LOCATION '/no/such/location';
! ERROR:  could not set permissions on directory "/no/such/location": No such file or directory
  -- No such tablespace
  CREATE TABLE bar (i int) TABLESPACE nosuchspace;
  ERROR:  tablespace "nosuchspace" does not exist
--- 57,63 ----

  -- Will fail with bad path
  CREATE TABLESPACE badspace LOCATION '/no/such/location';
! ERROR:  could not create location directory "/no/such/location": No such file or directory
  -- No such tablespace
  CREATE TABLE bar (i int) TABLESPACE nosuchspace;
  ERROR:  tablespace "nosuchspace" does not exist

pgsql-patches by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: [HACKERS] buildenv.pl/buildenv.bat
Next
From: Tom Lane
Date:
Subject: Re: Auto create (top level) directory for create tablespace