diff -durpN postgresql.1/doc/src/sgml/keywords.sgml postgresql.2/doc/src/sgml/keywords.sgml
--- postgresql.1/doc/src/sgml/keywords.sgml	2013-01-18 19:37:12.088576202 +0100
+++ postgresql.2/doc/src/sgml/keywords.sgml	2013-01-18 21:01:48.879276633 +0100
@@ -3388,7 +3388,6 @@
     <entry></entry>
    </row>
    <row>
-   <row>
     <entry><token>PERSISTENT</token></entry>
     <entry>non-reserved</entry>
     <entry></entry>
diff -durpN postgresql.1/doc/src/sgml/ref/set.sgml postgresql.2/doc/src/sgml/ref/set.sgml
--- postgresql.1/doc/src/sgml/ref/set.sgml	2013-01-18 19:37:12.089576209 +0100
+++ postgresql.2/doc/src/sgml/ref/set.sgml	2013-01-18 18:35:17.758223569 +0100
@@ -50,12 +50,13 @@ SET [ SESSION | LOCAL | PERSISTENT ] TIM
   </para>
 
   <para>
-   The <command>SET PERSISTENT</command> write the configuration parameter values to
-   <filename>postgresql.auto.conf</filename> file. with <literal>DEFAULT</literal> write the 
-   configuration parameter values to default value in <filename>postgresql.auto.conf</filename> file.
-   For deleting configuration entry in <filename>postgresql.auto.conf</filename> file use the 
-   <command>RESET PERSISTENT</command>. The values will be effective after reload of 
-   server configuration (SIGHUP) or server startup.  
+   <command>SET PERSISTENT</command> writes the configuration parameter
+   values to the <filename>postgresql.auto.conf</filename> file. With
+   <literal>DEFAULT</literal>, it writes the default configuration parameter
+   values to the <filename>postgresql.auto.conf</filename> file. For removing
+   a configuration entry from <filename>postgresql.auto.conf</filename> file,
+   use <command>RESET PERSISTENT</command>. The values will be effective
+   after reload of server configuration (SIGHUP) or server startup.
   </para>
   
   <para>
@@ -131,9 +132,10 @@ SET [ SESSION | LOCAL | PERSISTENT ] TIM
     <term><literal>PERSISTENT</></term>
     <listitem>
      <para>
-      Writes updated configuration paramters in <filename>postgresql.auto.conf</filename>.
-      These configuration parameters takes effect after reload of configuration
-      file (SIGHUP) or in next server start based on type of configuration parameter modified.
+      Writes updated configuration parameters to <filename>postgresql.auto.conf</filename>.
+      These configuration parameters takes effect after reload of
+      configuration file (SIGHUP) or in next server start based on
+      the type of configuration parameter modified.
      </para>
     </listitem>
    </varlistentry>
diff -durpN postgresql.1/src/backend/utils/misc/guc.c postgresql.2/src/backend/utils/misc/guc.c
--- postgresql.1/src/backend/utils/misc/guc.c	2013-01-18 19:37:12.097576264 +0100
+++ postgresql.2/src/backend/utils/misc/guc.c	2013-01-18 20:38:56.238781146 +0100
@@ -3350,7 +3350,8 @@ static void ShowAllGUCConfig(DestReceive
 static char *_ShowOption(struct config_generic * record, bool use_units);
 static bool validate_option_array_item(const char *name, const char *value,
 						   bool skipIfNoPermissions);
-static void write_auto_conf_file(int fd, ConfigVariable **head_p);
+static void write_auto_conf_file(int fd, const char *filename,
+						   ConfigVariable **head_p);
 static void replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p,
 				 char *config_file, char *name, char *value, bool is_quoted);
 static void set_config_file(VariableSetStmt *setstmt);
@@ -5267,11 +5268,11 @@ validate_conf_option(const char *name, c
 
 						if (hintmsg)
 							pfree(hintmsg);
-						return 0;
+						return NULL;
 					}
 					if (!call_enum_check_hook(conf, &newval, &newextra,
 											  PGC_S_FILE, LOG))
-						return 0;
+						return NULL;
 				}
 			}
 	}
@@ -6085,17 +6086,16 @@ set_config_sourcefile(const char *name,
  * Remove auto conf temporary files left over from a prior postmaster session
  */
 void
-RemoveAutoConfTmpFiles()
+RemoveAutoConfTmpFiles(void)
 {
 	char		AutoConfPath[MAXPGPATH];
 	DIR		   *config_dir;
 	struct dirent *temp_de;
 	char		rm_path[MAXPGPATH];
 
-	StrNCpy(AutoConfPath, ConfigFileName, sizeof(AutoConfPath));
-	get_parent_directory(AutoConfPath);
-	join_path_components(AutoConfPath, AutoConfPath, "config_dir");
-	canonicalize_path(AutoConfPath);
+	snprintf(AutoConfPath, sizeof(AutoConfPath), "%s/%s",
+							data_directory,
+							PG_AUTOCONF_DIR);
 
 	config_dir = AllocateDir(AutoConfPath);
 	if (config_dir == NULL)
@@ -6103,7 +6103,7 @@ RemoveAutoConfTmpFiles()
 		/* anything except ENOENT is fishy */
 		if (errno != ENOENT)
 			elog(LOG,
-				 "could not open config_dir directory \"%s\": %m",
+				 "could not open automatic configuration directory \"%s\": %m",
 				 AutoConfPath);
 		return;
 	}
@@ -6116,7 +6116,7 @@ RemoveAutoConfTmpFiles()
 			continue;
 
 		snprintf(rm_path, sizeof(rm_path), "%s/%s",
-				 AutoConfPath, temp_de->d_name);
+					AutoConfPath, temp_de->d_name);
 
 		if (strncmp(temp_de->d_name,
 					PG_AUTOCONF_TMP_FILE,
@@ -6124,7 +6124,7 @@ RemoveAutoConfTmpFiles()
 			unlink(rm_path);	/* note we ignore any error */
 		else
 			elog(LOG,
-				 "unexpected file found in config_dir directory: \"%s\"",
+				 "unexpected file found in automatic configuration directory: \"%s\"",
 				 rm_path);
 	}
 
@@ -6137,7 +6137,7 @@ RemoveAutoConfTmpFiles()
  * postgresql.auto.conf.XXXXXX file
  */
 static void
-write_auto_conf_file(int fd, ConfigVariable **head_p)
+write_auto_conf_file(int fd, const char *filename, ConfigVariable **head_p)
 {
 	ConfigVariable *item;
 	StringInfoData buf;
@@ -6165,7 +6165,8 @@ write_auto_conf_file(int fd, ConfigVaria
 		if (write(fd, buf.data, buf.len) < 0)
 		{
 			ereport(ERROR,
-					(errmsg("failed to write into to \"postgresql.auto.conf.XXXXXX\" file")));
+					(errmsg("failed to write to \"%s\" file",
+							filename)));
 		}
 
 		resetStringInfo(&buf);
@@ -6174,22 +6175,23 @@ write_auto_conf_file(int fd, ConfigVaria
 	if (pg_fsync(fd) != 0)
 		ereport(ERROR,
 				(errcode_for_file_access(),
-		  errmsg("could not fsync file \"postgresql.auto.conf.XXXXXX\": %m")));
-
+				 errmsg("could not fsync file \"%s\": %m",
+				 			filename)));
 
 	pfree(buf.data);
-	return;
 }
 
 
 /*
- * Replace the updated configuration parameter value in postgresql.auto.conf
- * file lines. If it is new then append extra line at the end of lines.
+ * Replace the updated configuration parameter value in
+ * postgresql.auto.conf. If the parameter is new then
+ * it's appended to the list of parameters.
  */
 static void
 replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p,
-						  char *config_file, char *name, char *value,
-						  bool is_quoted)
+						 char *config_file,
+						 char *name, char *value,
+						 bool is_quoted)
 {
 	ConfigVariable *item,
 			   *prev = NULL;
@@ -6263,10 +6265,22 @@ replace_auto_config_value(ConfigVariable
 
 /*
  * Persist the configuration parameter value.
- * Writes the value of configuration parameter in postgresql.auto.conf.XXXXXX
- * file. LW lock is used to disallow changing of file by multiple backends.
- * On Success, rename postgresql.auto.conf.XXXXXX to postgresql.auto.conf
- * and On Failure, delete the postgresql.auto.conf.XXXXXX
+ *
+ * This function takes all previously set persistent
+ * configuration parameters and the currently set ones
+ * and write them all to the automatic configuration file.
+ *
+ * The configuration parameters are written to a temporary
+ * file created by mkstemp() then renamed to the final name.
+ * The template for the temporary file is
+ * postgresql.auto.conf.XXXXXX.
+ *
+ * An LWLock is used to serialize writing to the same file.
+ *
+ * In case of an error, we leave the original automatic
+ * configuration file (postgresql.auto.conf) intact.
+ * A stale temporary file may be left behind in case we crash.
+ * Such files are removed on the next server restart.
  */
 static void
 set_config_file(VariableSetStmt *setstmt)
@@ -6280,7 +6294,6 @@ set_config_file(VariableSetStmt *setstmt
 	ConfigVariable *tail_p = NULL;
 	char	    AutoConfFileName[MAXPGPATH];
 	char	    AutoConfTmpFileName[MAXPGPATH];
-	char		Filename[MAXPGPATH];
 	struct stat st;
 
 	if (!superuser())
@@ -6318,17 +6331,14 @@ set_config_file(VariableSetStmt *setstmt
 		(errmsg("invalid value for parameter \"%s\": \"%s\"", name, value)));
 
 	/* Frame auto conf name and auto conf sample temp file name */
-	snprintf(Filename,sizeof(Filename),"%s/%s", PG_AUTOCONF_DIR, PG_AUTOCONF_FILENAME);
-	StrNCpy(AutoConfFileName, ConfigFileName, sizeof(AutoConfFileName));
-	get_parent_directory(AutoConfFileName);
-	join_path_components(AutoConfFileName, AutoConfFileName, Filename);
-	canonicalize_path(AutoConfFileName);
-
-	snprintf(Filename,sizeof(Filename),"%s/%s", PG_AUTOCONF_DIR, PG_AUTOCONF_SAMPLE_TMPFILE);
-	StrNCpy(AutoConfTmpFileName, ConfigFileName, sizeof(AutoConfTmpFileName));
-	get_parent_directory(AutoConfTmpFileName);
-	join_path_components(AutoConfTmpFileName, AutoConfTmpFileName, Filename);
-	canonicalize_path(AutoConfTmpFileName);
+	snprintf(AutoConfFileName, sizeof(AutoConfFileName), "%s/%s/%s",
+						data_directory,
+						PG_AUTOCONF_DIR,
+						PG_AUTOCONF_FILENAME);
+	snprintf(AutoConfTmpFileName, sizeof(AutoConfTmpFileName),"%s/%s/%s",
+						data_directory,
+						PG_AUTOCONF_DIR,
+						PG_AUTOCONF_SAMPLE_TMPFILE);
 
 	LWLockAcquire(SetPersistentLock, LW_EXCLUSIVE);
 
@@ -6336,10 +6346,9 @@ set_config_file(VariableSetStmt *setstmt
 	if (Tmpfd < 0)
 		ereport(ERROR,
 				(errcode_for_file_access(),
-		  errmsg("could not create unique auto conf temp file \"%s\": %m ",
-					  AutoConfTmpFileName),
-				 errhint("please clean the files from config_dir left out from"
-				 		" previous postmaster session")));
+				 errmsg("could not create unique temporary file \"%s\": %m ",
+								 AutoConfTmpFileName),
+				 errhint("please remove the temporary files from the automatic configuration directory")));
 
 	PG_TRY();
 	{
@@ -6351,20 +6360,20 @@ set_config_file(VariableSetStmt *setstmt
 			if (fd < 0)
 				ereport(ERROR,
 						(errcode_for_file_access(),
-						 errmsg("creation of auto conf file failed as it was not exist\"%s\": %m",
-								AutoConfFileName)));
+						 errmsg("creating \"%s\" failed: %m",
+						 		 AutoConfFileName)));
 			close(fd);
 
 			ereport(NOTICE,
-				 (errmsg("Created auto conf file as it was not exist \"%s\"",
-						 AutoConfFileName)));
+				 (errmsg("created automatic configuration file \"%s\"",
+								 AutoConfFileName)));
 		}
 
 		/* open postgresql.auto.conf file */
 		infile = AllocateFile(AutoConfFileName, "r");
 		if (infile == NULL)
 			ereport(ERROR,
-					(errmsg("failed to open \"postgresql.auto.conf\" file")));
+					(errmsg("failed to open \"" PG_AUTOCONF_FILENAME "\" file")));
 
 		/* Parse the postgresql.auto.conf file */
 		ParseConfigFp(infile, AutoConfFileName, 0, LOG, &head_p, &tail_p);
@@ -6376,7 +6385,7 @@ set_config_file(VariableSetStmt *setstmt
 					  value, (record->vartype == PGC_STRING) ? true : false);
 
 		/* Write the New contents to postgresql.auto.conf.XXXXXX file */
-		write_auto_conf_file(Tmpfd, &head_p);
+		write_auto_conf_file(Tmpfd, AutoConfTmpFileName, &head_p);
 
 		close(Tmpfd);
 		Tmpfd = -1;
diff -durpN postgresql.1/src/backend/utils/misc/guc-file.l postgresql.2/src/backend/utils/misc/guc-file.l
--- postgresql.1/src/backend/utils/misc/guc-file.l	2013-01-18 19:37:12.095576251 +0100
+++ postgresql.2/src/backend/utils/misc/guc-file.l	2013-01-18 20:36:22.891842079 +0100
@@ -150,8 +150,10 @@ ProcessConfigFile(GucContext context)
 	if (!is_config_dir_parsed)
 	{
 		ereport(WARNING,
-				(errmsg("Default \"config_dir\" is not present in postgresql.conf."
-						"Any configuration value changed with SET PERSISTENT command will not be effective.")));
+				(errmsg("Default \"%s\" is not present in postgresql.conf. "
+					"Configuration parameters changed with SET PERSISTENT "
+					"command will not be effective.",
+					PG_AUTOCONF_DIR)));
 	}
 
 	/*
@@ -377,7 +379,7 @@ ProcessConfigFile(GucContext context)
  * path, return an absolute one.  We consider the location to be relative to
  * the directory holding the calling file.
  */
-char *
+static char *
 AbsoluteConfigLocation(const char *location, const char *calling_file)
 {
 	char		abs_path[MAXPGPATH];
diff -durpN postgresql.1/src/bin/initdb/initdb.c postgresql.2/src/bin/initdb/initdb.c
--- postgresql.1/src/bin/initdb/initdb.c	2013-01-18 19:37:12.098576271 +0100
+++ postgresql.2/src/bin/initdb/initdb.c	2013-01-18 20:45:17.623125320 +0100
@@ -193,7 +193,7 @@ const char *subdirs[] = {
 	"base/1",
 	"pg_tblspc",
 	"pg_stat_tmp",
-	"config_dir"
+	PG_AUTOCONF_DIR
 };
 
 
@@ -1293,7 +1293,7 @@ setup_config(void)
 		conflines = replace_token(conflines, "#log_timezone = 'GMT'", repltok);
 	}
 
-	snprintf(repltok, sizeof(repltok), "include_dir = 'config_dir'");
+	snprintf(repltok, sizeof(repltok), "include_dir = '" PG_AUTOCONF_DIR "'");
 	conflines = replace_token(conflines, "#include_dir = 'auto.conf.d'", repltok);
 
 	snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
@@ -1302,7 +1302,7 @@ setup_config(void)
 	chmod(path, S_IRUSR | S_IWUSR);
 
 	/* create a .auto for conf file */
-	sprintf(path, "%s/config_dir/postgresql.auto.conf", pg_data);
+	sprintf(path, "%s/%s/postgresql.auto.conf", pg_data, PG_AUTOCONF_DIR);
 	autofile = fopen(path, PG_BINARY_W);
 	if (autofile == NULL)
 	{
diff -durpN postgresql.1/src/include/pg_config_manual.h postgresql.2/src/include/pg_config_manual.h
--- postgresql.1/src/include/pg_config_manual.h	2013-01-02 09:19:03.902522322 +0100
+++ postgresql.2/src/include/pg_config_manual.h	2013-01-18 20:52:35.850832958 +0100
@@ -266,3 +266,12 @@
 /* #define HEAPDEBUGALL */
 /* #define ACLDEBUG */
 /* #define RTDEBUG */
+
+/*
+ * Automatic configuration directory and file name
+ * for SET PERSISTENT.
+ */
+#define PG_AUTOCONF_DIR			"auto.conf.d"
+#define PG_AUTOCONF_FILENAME		"postgresql.auto.conf"
+#define PG_AUTOCONF_SAMPLE_TMPFILE	"postgresql.auto.conf.XXXXXX"
+#define PG_AUTOCONF_TMP_FILE		"postgresql.auto.conf."
diff -durpN postgresql.1/src/include/port.h postgresql.2/src/include/port.h
--- postgresql.1/src/include/port.h	2013-01-18 19:37:12.099576278 +0100
+++ postgresql.2/src/include/port.h	2013-01-18 18:08:54.803422911 +0100
@@ -272,7 +272,6 @@ extern int	pclose_check(FILE *stream);
  */
 extern int	pgrename(const char *from, const char *to);
 extern int	pgunlink(const char *path);
-extern int pgmkstemp (char *TmpFileName);
 
 /* Include this first so later includes don't see these defines */
 #ifdef WIN32_ONLY_COMPILER
@@ -281,7 +280,6 @@ extern int pgmkstemp (char *TmpFileName)
 
 #define rename(from, to)		pgrename(from, to)
 #define unlink(path)			pgunlink(path)
-#define mkstemp(tempfilename) 	pgmkstemp(tempfilename)
 #endif   /* defined(WIN32) || defined(__CYGWIN__) */
 
 /*
@@ -292,14 +290,19 @@ extern int pgmkstemp (char *TmpFileName)
  *	junction points don't, so use those instead.  We have no way of
  *	knowing what type of system Cygwin binaries will be run on.
  *		Note: Some CYGWIN includes might #define WIN32.
+ *
+ *	Win32 also doesn't have mkstemp(). We can emulate it using
+ *	_mktemp_s() and a subsequent open(). Cygwin has mkstemp().
  */
 #if defined(WIN32) && !defined(__CYGWIN__)
 extern int	pgsymlink(const char *oldpath, const char *newpath);
 extern int	pgreadlink(const char *path, char *buf, size_t size);
 extern bool pgwin32_is_junction(char *path);
+extern int pgmkstemp (char *TmpFileName);
 
 #define symlink(oldpath, newpath)	pgsymlink(oldpath, newpath)
 #define readlink(path, buf, size)	pgreadlink(path, buf, size)
+#define mkstemp(tempfilename)		pgmkstemp(tempfilename)
 #endif
 
 extern bool rmtree(const char *path, bool rmtopdir);
diff -durpN postgresql.1/src/include/utils/guc.h postgresql.2/src/include/utils/guc.h
--- postgresql.1/src/include/utils/guc.h	2013-01-18 19:37:12.100576285 +0100
+++ postgresql.2/src/include/utils/guc.h	2013-01-18 20:52:37.705844449 +0100
@@ -192,12 +192,6 @@ typedef enum
 
 #define GUC_NOT_WHILE_SEC_REST	0x8000	/* can't set if security restricted */
 
-
-#define PG_AUTOCONF_DIR "config_dir"
-#define PG_AUTOCONF_FILENAME "postgresql.auto.conf"
-#define PG_AUTOCONF_SAMPLE_TMPFILE "postgresql.auto.conf.XXXXXX"
-#define PG_AUTOCONF_TMP_FILE	"postgresql.auto.conf."
-
 /* GUC vars that are actually declared in guc.c, rather than elsewhere */
 extern bool log_duration;
 extern bool Debug_print_plan;
diff -durpN postgresql.1/src/port/dirmod.c postgresql.2/src/port/dirmod.c
--- postgresql.1/src/port/dirmod.c	2013-01-18 19:37:12.100576285 +0100
+++ postgresql.2/src/port/dirmod.c	2013-01-18 18:12:38.228784768 +0100
@@ -193,25 +193,9 @@ pgunlink(const char *path)
 	return 0;
 }
 
-/*
- *	pgmkstemp
- */
-int
-pgmkstemp (char *TmpFileName)
-{
-	int err;
-
-	err = _mktemp_s(TmpFileName, strlen(TmpFileName) + 1);
-	if (err != 0)
-		return -1;
-
-	return open(TmpFileName, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
-}
-
 /* We undefined these above; now redefine for possible use below */
 #define rename(from, to)		pgrename(from, to)
 #define unlink(path)			pgunlink(path)
-#define mkstemp(tempfilename) 	pgmkstemp(tempfilename)
 #endif   /* defined(WIN32) || defined(__CYGWIN__) */
 
 
@@ -439,6 +423,26 @@ pgwin32_is_junction(char *path)
 	}
 	return ((attr & FILE_ATTRIBUTE_REPARSE_POINT) == FILE_ATTRIBUTE_REPARSE_POINT);
 }
+
+/*
+ *	pgmkstemp
+ *
+ * Windows doesn't have mkstemp() but we can emulate it
+ * using _mktemp_s() and a subsequent open(). Currently,
+ * it's only used for SET PERSISTENT which is serialized
+ * using an LWLock.
+ */
+int
+pgmkstemp (char *TmpFileName)
+{
+	int err;
+
+	err = _mktemp_s(TmpFileName, strlen(TmpFileName) + 1);
+	if (err != 0)
+		return -1;
+
+	return open(TmpFileName, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
+}
 #endif   /* defined(WIN32) && !defined(__CYGWIN__) */
 
 
