From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 2 Jun 2000 04:04:54 +0000 (+0000)
Subject: If create/drop database are going to call closeAllVfds(), they ought
X-Git-Tag: REL7_1_BETA~1205
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=664dd614d900962b621ee069c8480262d443f66a;p=postgresql

If create/drop database are going to call closeAllVfds(), they ought
to do it at the last moment before calling system() ... not at some
randomly-chosen earlier point in the routine ...
---

diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 39819fe14d..72cfd297aa 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.56 2000/05/30 00:49:43 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.57 2000/06/02 04:04:54 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -55,6 +55,7 @@ createdb(const char *dbname, const char *dbpath, int encoding)
 	char	   *loc;
 	char		locbuf[512];
 	int4		user_id;
+	int			ret;
 	bool		use_super,
 				use_createdb;
 	Relation	pg_database_rel;
@@ -90,12 +91,6 @@ createdb(const char *dbname, const char *dbpath, int encoding)
 			 "This may be due to a character that is not allowed or because the chosen "
 			 "path isn't permitted for databases", dbpath);
 
-	/*
-	 * close virtual file descriptors so the kernel has more available for
-	 * the system() calls
-	 */
-	closeAllVfds();
-
 	/*
 	 * Insert a new tuple into pg_database
 	 */
@@ -133,6 +128,12 @@ createdb(const char *dbname, const char *dbpath, int encoding)
 
 	heap_close(pg_database_rel, NoLock);
 
+	/*
+	 * Close virtual file descriptors so the kernel has more available for
+	 * the mkdir() and system() calls below.
+	 */
+	closeAllVfds();
+
 	/* Copy the template database to the new location */
 
 	if (mkdir(loc, S_IRWXU) != 0)
@@ -140,14 +141,15 @@ createdb(const char *dbname, const char *dbpath, int encoding)
 
 	snprintf(buf, sizeof(buf), "cp %s%cbase%ctemplate1%c* '%s'",
 			 DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR, loc);
+	ret = system(buf);
+	/* Some versions of SunOS seem to return ECHILD after a system() call */
 #if defined(sun)
-	if (system(buf) != 0 && errno != ECHILD)
+	if (ret != 0 && errno != ECHILD)
 #else
-	if (system(buf) != 0)
+	if (ret != 0)
 #endif
 	{
-		int			ret;
-
+		/* Failed, so try to clean up the created directory ... */
 		snprintf(buf, sizeof(buf), "rm -rf '%s'", loc);
 		ret = system(buf);
 #if defined(sun)
@@ -209,12 +211,6 @@ dropdb(const char *dbname)
 			 "This may be due to a character that is not allowed or because the chosen "
 			 "path isn't permitted for databases", path);
 
-	/*
-	 * close virtual file descriptors so the kernel has more available for
-	 * the system() calls
-	 */
-	closeAllVfds();
-
 	/*
 	 * Obtain exclusive lock on pg_database.  We need this to ensure that
 	 * no new backend starts up in the target database while we are
@@ -277,6 +273,12 @@ dropdb(const char *dbname)
 	 */
 	DropBuffers(db_id);
 
+	/*
+	 * Close virtual file descriptors so the kernel has more available for
+	 * the system() call below.
+	 */
+	closeAllVfds();
+
 	/*
 	 * Remove the database's subdirectory and everything in it.
 	 */