699f87
Back-port of upstream 9.3 patch to support multiple sockets
699f87
(upstream git commits c9b0cbe98bd783e24a8c4d8d8ac472a494b81292 and
699f87
d2286a98ef3fb88bafb57381b4c20b8b878827f1, plus updates of derived
699f87
documentation files).
699f87
699f87
Note the patch also touches html-stamp and man-stamp in doc/src/sgml/;
699f87
this is to keep the makefiles from trying to rebuild the derived doc
699f87
files.  We don't want that to happen because the BuildRequires for the
699f87
package don't include the necessary documentation tools.  Those diff
699f87
hunks *must be at the end* so that those files are newer than the
699f87
master doc files.
699f87
699f87
diff --git a/contrib/pg_upgrade/server.c b/contrib/pg_upgrade/server.c
699f87
index 0940e00..c5ecb84 100644
699f87
--- a/contrib/pg_upgrade/server.c
699f87
+++ b/contrib/pg_upgrade/server.c
699f87
@@ -197,7 +197,8 @@ start_postmaster(ClusterInfo *cluster)
699f87
 		snprintf(socket_string + strlen(socket_string),
699f87
 				 sizeof(socket_string) - strlen(socket_string),
699f87
 				 " -c %s='%s'",
699f87
-				 (GET_MAJOR_VERSION(cluster->major_version) < 903) ?
699f87
+	/* assume 9.1 build will not have unix_socket_directories patch */
699f87
+				 (GET_MAJOR_VERSION(cluster->major_version) < 902) ?
699f87
 				 "unix_socket_directory" : "unix_socket_directories",
699f87
 				 cluster->sockdir);
699f87
 #endif
699f87
diff --git a/doc/src/sgml/client-auth.sgml b/doc/src/sgml/client-auth.sgml
699f87
index b9664cb..94e166d 100644
699f87
--- a/doc/src/sgml/client-auth.sgml
699f87
+++ b/doc/src/sgml/client-auth.sgml
699f87
@@ -838,7 +838,7 @@ omicron         bryanh                  guest1
699f87
     <varname>unix_socket_permissions</varname> (and possibly
699f87
     <varname>unix_socket_group</varname>) configuration parameters as
699f87
     described in <xref linkend="runtime-config-connection">.  Or you
699f87
-    could set the <varname>unix_socket_directory</varname>
699f87
+    could set the <varname>unix_socket_directories</varname>
699f87
     configuration parameter to place the socket file in a suitably
699f87
     restricted directory.
699f87
    </para>
699f87
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
699f87
index 51d7da9..cb5c5d2 100644
699f87
--- a/doc/src/sgml/config.sgml
699f87
+++ b/doc/src/sgml/config.sgml
699f87
@@ -453,17 +453,24 @@ SET ENABLE_SEQSCAN TO OFF;
699f87
       </listitem>
699f87
      </varlistentry>
699f87
 
699f87
-     <varlistentry id="guc-unix-socket-directory" xreflabel="unix_socket_directory">
699f87
-      <term><varname>unix_socket_directory</varname> (<type>string</type>)</term>
699f87
+     <varlistentry id="guc-unix-socket-directories" xreflabel="unix_socket_directories">
699f87
+      <term><varname>unix_socket_directories</varname> (<type>string</type>)</term>
699f87
       <indexterm>
699f87
-       <primary><varname>unix_socket_directory configuration parameter</primary>
699f87
+       <primary><varname>unix_socket_directories configuration parameter</primary>
699f87
       </indexterm>
699f87
       <listitem>
699f87
        <para>
699f87
-        Specifies the directory of the Unix-domain socket on which the
699f87
-        server is to listen for
699f87
-        connections from client applications.  The default is normally
699f87
-        <filename>/tmp</filename>, but can be changed at build time.
699f87
+        Specifies the directory of the Unix-domain socket(s) on which the
699f87
+        server is to listen for connections from client applications.
699f87
+        Multiple sockets can be created by listing multiple directories
699f87
+        separated by commas.  Whitespace between entries is
699f87
+        ignored; surround a directory name with double quotes if you need
699f87
+        to include whitespace or commas in the name.
699f87
+        An empty value
699f87
+        specifies not listening on any Unix-domain sockets, in which case
699f87
+        only TCP/IP sockets can be used to connect to the server.
699f87
+        The default value is normally
699f87
+        <filename>/tmp</filename>, but that can be changed at build time.
699f87
         This parameter can only be set at server start.
699f87
        </para>
699f87
 
699f87
@@ -472,8 +479,8 @@ SET ENABLE_SEQSCAN TO OFF;
699f87
         <literal>.s.PGSQL.<replaceable>nnnn</literal> where
699f87
         <replaceable>nnnn is the server's port number, an ordinary file
699f87
         named <literal>.s.PGSQL.<replaceable>nnnn.lock</literal> will be
699f87
-        created in the <varname>unix_socket_directory directory.  Neither
699f87
-        file should ever be removed manually.
699f87
+        created in each of the <varname>unix_socket_directories directories.
699f87
+        Neither file should ever be removed manually.
699f87
        </para>
699f87
 
699f87
        <para>
699f87
@@ -490,8 +497,8 @@ SET ENABLE_SEQSCAN TO OFF;
699f87
       </indexterm>
699f87
       <listitem>
699f87
        <para>
699f87
-        Sets the owning group of the Unix-domain socket.  (The owning
699f87
-        user of the socket is always the user that starts the
699f87
+        Sets the owning group of the Unix-domain socket(s).  (The owning
699f87
+        user of the sockets is always the user that starts the
699f87
         server.)  In combination with the parameter
699f87
         <varname>unix_socket_permissions</varname> this can be used as
699f87
         an additional access control mechanism for Unix-domain connections.
699f87
@@ -514,7 +521,7 @@ SET ENABLE_SEQSCAN TO OFF;
699f87
       </indexterm>
699f87
       <listitem>
699f87
        <para>
699f87
-        Sets the access permissions of the Unix-domain socket.  Unix-domain
699f87
+        Sets the access permissions of the Unix-domain socket(s).  Unix-domain
699f87
         sockets use the usual Unix file system permission set.
699f87
         The parameter value is expected to be a numeric mode
699f87
         specified in the format accepted by the
699f87
@@ -6624,7 +6631,7 @@ LOG:  CleanUpLock: deleting: lock(0xb7acd844) id(24688,24696,0,0,0,1)
699f87
        </row>
699f87
        <row>
699f87
         <entry><option>-k <replaceable>x</replaceable></option></entry>
699f87
-        <entry><literal>unix_socket_directory = <replaceable>x</replaceable></entry>
699f87
+        <entry><literal>unix_socket_directories = <replaceable>x</replaceable></entry>
699f87
        </row>
699f87
        <row>
699f87
         <entry><option>-l</option></entry>
699f87
diff --git a/doc/src/sgml/html/app-postgres.html b/doc/src/sgml/html/app-postgres.html
699f87
index b1f4e31..b6359a8 100644
699f87
--- a/doc/src/sgml/html/app-postgres.html
699f87
+++ b/doc/src/sgml/html/app-postgres.html
699f87
@@ -574,11 +574,19 @@ CLASS="REPLACEABLE"
699f87
 CLASS="COMMAND"
699f87
 >postgres
699f87
 > is to listen for
699f87
-        connections from client applications.  The default is normally
699f87
+        connections from client applications.  The value can also be a
699f87
+        comma-separated list of directories.  An empty value
699f87
+        specifies not listening on any Unix-domain sockets, in which case
699f87
+        only TCP/IP sockets can be used to connect to the server.
699f87
+        The default value is normally
699f87
         
699f87
 CLASS="FILENAME"
699f87
 >/tmp
699f87
->, but can be changed at build time.
699f87
+>, but that can be changed at build time.
699f87
+        Specifying this option is equivalent to setting the 
699f87
+HREF="runtime-config-connection.html#GUC-UNIX-SOCKET-DIRECTORIES"
699f87
+>unix_socket_directories
699f87
+> configuration parameter.
699f87
        
699f87
 >
699f87
 >
699f87
diff --git a/doc/src/sgml/html/auth-methods.html b/doc/src/sgml/html/auth-methods.html
699f87
index ab544a9..e1225d1 100644
699f87
--- a/doc/src/sgml/html/auth-methods.html
699f87
+++ b/doc/src/sgml/html/auth-methods.html
699f87
@@ -161,7 +161,7 @@ HREF="runtime-config-connection.html"
699f87
 >.  Or you
699f87
     could set the 
699f87
 CLASS="VARNAME"
699f87
->unix_socket_directory
699f87
+>unix_socket_directories
699f87
 >
699f87
     configuration parameter to place the socket file in a suitably
699f87
     restricted directory.
699f87
diff --git a/doc/src/sgml/html/bookindex.html b/doc/src/sgml/html/bookindex.html
699f87
index 5e25f95..1562ffe 100644
699f87
--- a/doc/src/sgml/html/bookindex.html
699f87
+++ b/doc/src/sgml/html/bookindex.html
699f87
@@ -17250,7 +17250,7 @@ HREF="xfunc-c.html#DFUNC"
699f87
 >
699f87
 >
699f87
 >
699f87
->unix_socket_directory configuration parameter,
699f87
+>unix_socket_directories configuration parameter,
699f87
     
699f87
 HREF="runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-SETTINGS"
699f87
 >Connection Settings
699f87
diff --git a/doc/src/sgml/html/preventing-server-spoofing.html b/doc/src/sgml/html/preventing-server-spoofing.html
699f87
index 77d11ab..6abc511 100644
699f87
--- a/doc/src/sgml/html/preventing-server-spoofing.html
699f87
+++ b/doc/src/sgml/html/preventing-server-spoofing.html
699f87
@@ -115,8 +115,8 @@ CLASS="LITERAL"
699f87
 >local
699f87
 >
699f87
    connections is to use a Unix domain socket directory (
699f87
-HREF="runtime-config-connection.html#GUC-UNIX-SOCKET-DIRECTORY"
699f87
->unix_socket_directory
699f87
+HREF="runtime-config-connection.html#GUC-UNIX-SOCKET-DIRECTORIES"
699f87
+>unix_socket_directories
699f87
 >) that has write permission only
699f87
    for a trusted local user.  This prevents a malicious user from creating
699f87
    their own socket file in that directory.  If you are concerned that
699f87
diff --git a/doc/src/sgml/html/runtime-config-connection.html b/doc/src/sgml/html/runtime-config-connection.html
699f87
index 74f096e..0ba6987 100644
699f87
--- a/doc/src/sgml/html/runtime-config-connection.html
699f87
+++ b/doc/src/sgml/html/runtime-config-connection.html
699f87
@@ -274,24 +274,31 @@ CLASS="VARNAME"
699f87
 >
699f87
 >
699f87
 >
699f87
-NAME="GUC-UNIX-SOCKET-DIRECTORY"
699f87
+NAME="GUC-UNIX-SOCKET-DIRECTORIES"
699f87
 >
699f87
 >
699f87
 CLASS="VARNAME"
699f87
->unix_socket_directory
699f87
+>unix_socket_directories
699f87
 > (
699f87
 CLASS="TYPE"
699f87
 >string
699f87
 >)
699f87
 >
699f87
 >
699f87
->        Specifies the directory of the Unix-domain socket on which the
699f87
-        server is to listen for
699f87
-        connections from client applications.  The default is normally
699f87
+>        Specifies the directory of the Unix-domain socket(s) on which the
699f87
+        server is to listen for connections from client applications.
699f87
+        Multiple sockets can be created by listing multiple directories
699f87
+        separated by commas.  Whitespace between entries is
699f87
+        ignored; surround a directory name with double quotes if you need
699f87
+        to include whitespace or commas in the name.
699f87
+        An empty value
699f87
+        specifies not listening on any Unix-domain sockets, in which case
699f87
+        only TCP/IP sockets can be used to connect to the server.
699f87
+        The default value is normally
699f87
         
699f87
 CLASS="FILENAME"
699f87
 >/tmp
699f87
->, but can be changed at build time.
699f87
+>, but that can be changed at build time.
699f87
         This parameter can only be set at server start.
699f87
        
699f87
 >
699f87
@@ -320,11 +327,11 @@ CLASS="REPLACEABLE"
699f87
 >
699f87
 >.lock
699f87
 > will be
699f87
-        created in the 
699f87
+        created in each of the 
699f87
 CLASS="VARNAME"
699f87
->unix_socket_directory
699f87
-> directory.  Neither
699f87
-        file should ever be removed manually.
699f87
+>unix_socket_directories
699f87
+> directories.
699f87
+        Neither file should ever be removed manually.
699f87
        
699f87
 >
699f87
 >        This parameter is irrelevant on Windows, which does not have
699f87
@@ -344,8 +351,8 @@ CLASS="TYPE"
699f87
 >)
699f87
 >
699f87
 >
699f87
->        Sets the owning group of the Unix-domain socket.  (The owning
699f87
-        user of the socket is always the user that starts the
699f87
+>        Sets the owning group of the Unix-domain socket(s).  (The owning
699f87
+        user of the sockets is always the user that starts the
699f87
         server.)  In combination with the parameter
699f87
         
699f87
 CLASS="VARNAME"
699f87
@@ -374,7 +381,7 @@ CLASS="TYPE"
699f87
 >)
699f87
 >
699f87
 >
699f87
->        Sets the access permissions of the Unix-domain socket.  Unix-domain
699f87
+>        Sets the access permissions of the Unix-domain socket(s).  Unix-domain
699f87
         sockets use the usual Unix file system permission set.
699f87
         The parameter value is expected to be a numeric mode
699f87
         specified in the format accepted by the
699f87
diff --git a/doc/src/sgml/html/runtime-config-short.html b/doc/src/sgml/html/runtime-config-short.html
699f87
index 8c39c2f..f0d7080 100644
699f87
--- a/doc/src/sgml/html/runtime-config-short.html
699f87
+++ b/doc/src/sgml/html/runtime-config-short.html
699f87
@@ -330,7 +330,7 @@ CLASS="REPLACEABLE"
699f87
 >
699f87
 >
699f87
 CLASS="LITERAL"
699f87
->unix_socket_directory = 
699f87
+>unix_socket_directories = 
699f87
 CLASS="REPLACEABLE"
699f87
 >
699f87
 >x
699f87
diff --git a/doc/src/sgml/man1/postgres.1 b/doc/src/sgml/man1/postgres.1
699f87
index 6ec18ee..3378dde 100644
699f87
--- a/doc/src/sgml/man1/postgres.1
699f87
+++ b/doc/src/sgml/man1/postgres.1
699f87
@@ -195,8 +195,10 @@ directly\&.
699f87
 .RS 4
699f87
 Specifies the directory of the Unix\-domain socket on which
699f87
 \fBpostgres\fR
699f87
-is to listen for connections from client applications\&. The default is normally
699f87
-/tmp, but can be changed at build time\&.
699f87
+is to listen for connections from client applications\&. The value can also be a comma\-separated list of directories\&. An empty value specifies not listening on any Unix\-domain sockets, in which case only TCP/IP sockets can be used to connect to the server\&. The default value is normally
699f87
+/tmp, but that can be changed at build time\&. Specifying this option is equivalent to setting the
699f87
+unix_socket_directories
699f87
+configuration parameter\&.
699f87
 .RE
699f87
 .PP
699f87
 \fB\-l\fR
699f87
diff --git a/doc/src/sgml/ref/postgres-ref.sgml b/doc/src/sgml/ref/postgres-ref.sgml
699f87
index 4e5cd02..a1f36e1 100644
699f87
--- a/doc/src/sgml/ref/postgres-ref.sgml
699f87
+++ b/doc/src/sgml/ref/postgres-ref.sgml
699f87
@@ -254,8 +254,14 @@ PostgreSQL documentation
699f87
        <para>
699f87
         Specifies the directory of the Unix-domain socket on which
699f87
         <command>postgres</command> is to listen for
699f87
-        connections from client applications.  The default is normally
699f87
-        <filename>/tmp</filename>, but can be changed at build time.
699f87
+        connections from client applications.  The value can also be a
699f87
+        comma-separated list of directories.  An empty value
699f87
+        specifies not listening on any Unix-domain sockets, in which case
699f87
+        only TCP/IP sockets can be used to connect to the server.
699f87
+        The default value is normally
699f87
+        <filename>/tmp</filename>, but that can be changed at build time.
699f87
+        Specifying this option is equivalent to setting the 
699f87
+        linkend="guc-unix-socket-directories"> configuration parameter.
699f87
        </para>
699f87
       </listitem>
699f87
      </varlistentry>
699f87
diff --git a/doc/src/sgml/runtime.sgml b/doc/src/sgml/runtime.sgml
699f87
index cfd3532..e6c9eaa 100644
699f87
--- a/doc/src/sgml/runtime.sgml
699f87
+++ b/doc/src/sgml/runtime.sgml
699f87
@@ -1800,7 +1800,7 @@ pg_dumpall -p 5432 | psql -d postgres -p 5433
699f87
   <para>
699f87
    The simplest way to prevent spoofing for <literal>local
699f87
    connections is to use a Unix domain socket directory (
699f87
-   linkend="guc-unix-socket-directory">) that has write permission only
699f87
+   linkend="guc-unix-socket-directories">) that has write permission only
699f87
    for a trusted local user.  This prevents a malicious user from creating
699f87
    their own socket file in that directory.  If you are concerned that
699f87
    some applications might still reference <filename>/tmp for the
699f87
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
699f87
index aeeffd7..b7e0d09 100644
699f87
--- a/src/backend/libpq/pqcomm.c
699f87
+++ b/src/backend/libpq/pqcomm.c
699f87
@@ -42,7 +42,7 @@
699f87
  *		StreamServerPort	- Open postmaster's server port
699f87
  *		StreamConnection	- Create new connection with client
699f87
  *		StreamClose			- Close a client/backend connection
699f87
- *		TouchSocketFile		- Protect socket file against /tmp cleaners
699f87
+ *		TouchSocketFiles	- Protect socket files against /tmp cleaners
699f87
  *		pq_init			- initialize libpq at backend startup
699f87
  *		pq_comm_reset	- reset libpq during error recovery
699f87
  *		pq_close		- shutdown libpq at backend exit
699f87
@@ -103,8 +103,8 @@ int			Unix_socket_permissions;
699f87
 char	   *Unix_socket_group;
699f87
 
699f87
 
699f87
-/* Where the Unix socket file is */
699f87
-static char sock_path[MAXPGPATH];
699f87
+/* Where the Unix socket files are (list of palloc'd strings) */
699f87
+static List *sock_paths = NIL;
699f87
 
699f87
 
699f87
 /*
699f87
@@ -141,8 +141,8 @@ static int	internal_flush(void);
699f87
 static void pq_set_nonblocking(bool nonblocking);
699f87
 
699f87
 #ifdef HAVE_UNIX_SOCKETS
699f87
-static int	Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName);
699f87
-static int	Setup_AF_UNIX(void);
699f87
+static int	Lock_AF_UNIX(char *unixSocketDir, char *unixSocketPath);
699f87
+static int	Setup_AF_UNIX(char *sock_path);
699f87
 #endif   /* HAVE_UNIX_SOCKETS */
699f87
 
699f87
 
699f87
@@ -236,29 +236,43 @@ pq_close(int code, Datum arg)
699f87
 
699f87
 /* StreamDoUnlink()
699f87
  * Shutdown routine for backend connection
699f87
- * If a Unix socket is used for communication, explicitly close it.
699f87
+ * If any Unix sockets are used for communication, explicitly close them.
699f87
  */
699f87
 #ifdef HAVE_UNIX_SOCKETS
699f87
 static void
699f87
 StreamDoUnlink(int code, Datum arg)
699f87
 {
699f87
-	Assert(sock_path[0]);
699f87
-	unlink(sock_path);
699f87
+	ListCell   *l;
699f87
+
699f87
+	/* Loop through all created sockets... */
699f87
+	foreach(l, sock_paths)
699f87
+	{
699f87
+		char	   *sock_path = (char *) lfirst(l);
699f87
+
699f87
+		unlink(sock_path);
699f87
+	}
699f87
+	/* Since we're about to exit, no need to reclaim storage */
699f87
+	sock_paths = NIL;
699f87
 }
699f87
 #endif   /* HAVE_UNIX_SOCKETS */
699f87
 
699f87
 /*
699f87
  * StreamServerPort -- open a "listening" port to accept connections.
699f87
  *
699f87
- * Successfully opened sockets are added to the ListenSocket[] array,
699f87
- * at the first position that isn't PGINVALID_SOCKET.
699f87
+ * family should be AF_UNIX or AF_UNSPEC; portNumber is the port number.
699f87
+ * For AF_UNIX ports, hostName should be NULL and unixSocketDir must be
699f87
+ * specified.  For TCP ports, hostName is either NULL for all interfaces or
699f87
+ * the interface to listen on, and unixSocketDir is ignored (can be NULL).
699f87
+ *
699f87
+ * Successfully opened sockets are added to the ListenSocket[] array (of
699f87
+ * length MaxListen), at the first position that isn't PGINVALID_SOCKET.
699f87
  *
699f87
  * RETURNS: STATUS_OK or STATUS_ERROR
699f87
  */
699f87
 
699f87
 int
699f87
 StreamServerPort(int family, char *hostName, unsigned short portNumber,
699f87
-				 char *unixSocketName,
699f87
+				 char *unixSocketDir,
699f87
 				 pgsocket ListenSocket[], int MaxListen)
699f87
 {
699f87
 	pgsocket	fd;
699f87
@@ -275,6 +289,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
699f87
 	int			listen_index = 0;
699f87
 	int			added = 0;
699f87
 
699f87
+#ifdef HAVE_UNIX_SOCKETS
699f87
+	char		unixSocketPath[MAXPGPATH];
699f87
+#endif
699f87
 #if !defined(WIN32) || defined(IPV6_V6ONLY)
699f87
 	int			one = 1;
699f87
 #endif
699f87
@@ -288,10 +305,22 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
699f87
 #ifdef HAVE_UNIX_SOCKETS
699f87
 	if (family == AF_UNIX)
699f87
 	{
699f87
-		/* Lock_AF_UNIX will also fill in sock_path. */
699f87
-		if (Lock_AF_UNIX(portNumber, unixSocketName) != STATUS_OK)
699f87
+		/*
699f87
+		 * Create unixSocketPath from portNumber and unixSocketDir and lock
699f87
+		 * that file path
699f87
+		 */
699f87
+		UNIXSOCK_PATH(unixSocketPath, portNumber, unixSocketDir);
699f87
+		if (strlen(unixSocketPath) >= UNIXSOCK_PATH_BUFLEN)
699f87
+		{
699f87
+			ereport(LOG,
699f87
+					(errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
699f87
+							unixSocketPath,
699f87
+							(int) (UNIXSOCK_PATH_BUFLEN - 1))));
699f87
 			return STATUS_ERROR;
699f87
-		service = sock_path;
699f87
+		}
699f87
+		if (Lock_AF_UNIX(unixSocketDir, unixSocketPath) != STATUS_OK)
699f87
+			return STATUS_ERROR;
699f87
+		service = unixSocketPath;
699f87
 	}
699f87
 	else
699f87
 #endif   /* HAVE_UNIX_SOCKETS */
699f87
@@ -434,7 +463,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
699f87
 					 (IS_AF_UNIX(addr->ai_family)) ?
699f87
 				  errhint("Is another postmaster already running on port %d?"
699f87
 						  " If not, remove socket file \"%s\" and retry.",
699f87
-						  (int) portNumber, sock_path) :
699f87
+						  (int) portNumber, service) :
699f87
 				  errhint("Is another postmaster already running on port %d?"
699f87
 						  " If not, wait a few seconds and retry.",
699f87
 						  (int) portNumber)));
699f87
@@ -445,7 +474,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
699f87
 #ifdef HAVE_UNIX_SOCKETS
699f87
 		if (addr->ai_family == AF_UNIX)
699f87
 		{
699f87
-			if (Setup_AF_UNIX() != STATUS_OK)
699f87
+			if (Setup_AF_UNIX(service) != STATUS_OK)
699f87
 			{
699f87
 				closesocket(fd);
699f87
 				break;
699f87
@@ -492,18 +521,8 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
699f87
  * Lock_AF_UNIX -- configure unix socket file path
699f87
  */
699f87
 static int
699f87
-Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName)
699f87
+Lock_AF_UNIX(char *unixSocketDir, char *unixSocketPath)
699f87
 {
699f87
-	UNIXSOCK_PATH(sock_path, portNumber, unixSocketName);
699f87
-	if (strlen(sock_path) >= UNIXSOCK_PATH_BUFLEN)
699f87
-	{
699f87
-		ereport(LOG,
699f87
-				(errmsg("Unix-domain socket path \"%s\" is too long (maximum %d bytes)",
699f87
-						sock_path,
699f87
-						(int) (UNIXSOCK_PATH_BUFLEN - 1))));
699f87
-		return STATUS_ERROR;
699f87
-	}
699f87
-
699f87
 	/*
699f87
 	 * Grab an interlock file associated with the socket file.
699f87
 	 *
699f87
@@ -512,13 +531,23 @@ Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName)
699f87
 	 * more portable, and second, it lets us remove any pre-existing socket
699f87
 	 * file without race conditions.
699f87
 	 */
699f87
-	CreateSocketLockFile(sock_path, true);
699f87
+	CreateSocketLockFile(unixSocketPath, true, unixSocketDir);
699f87
 
699f87
 	/*
699f87
 	 * Once we have the interlock, we can safely delete any pre-existing
699f87
 	 * socket file to avoid failure at bind() time.
699f87
 	 */
699f87
-	unlink(sock_path);
699f87
+	unlink(unixSocketPath);
699f87
+
699f87
+	/*
699f87
+	 * Arrange to unlink the socket file(s) at proc_exit.  If this is the
699f87
+	 * first one, set up the on_proc_exit function to do it; then add this
699f87
+	 * socket file to the list of files to unlink.
699f87
+	 */
699f87
+	if (sock_paths == NIL)
699f87
+		on_proc_exit(StreamDoUnlink, 0);
699f87
+
699f87
+	sock_paths = lappend(sock_paths, pstrdup(unixSocketPath));
699f87
 
699f87
 	return STATUS_OK;
699f87
 }
699f87
@@ -528,11 +557,8 @@ Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName)
699f87
  * Setup_AF_UNIX -- configure unix socket permissions
699f87
  */
699f87
 static int
699f87
-Setup_AF_UNIX(void)
699f87
+Setup_AF_UNIX(char *sock_path)
699f87
 {
699f87
-	/* Arrange to unlink the socket file at exit */
699f87
-	on_proc_exit(StreamDoUnlink, 0);
699f87
-
699f87
 	/*
699f87
 	 * Fix socket ownership/permission if requested.  Note we must do this
699f87
 	 * before we listen() to avoid a window where unwanted connections could
699f87
@@ -714,20 +740,24 @@ StreamClose(pgsocket sock)
699f87
 }
699f87
 
699f87
 /*
699f87
- * TouchSocketFile -- mark socket file as recently accessed
699f87
+ * TouchSocketFiles -- mark socket files as recently accessed
699f87
  *
699f87
  * This routine should be called every so often to ensure that the socket
699f87
- * file has a recent mod date (ordinary operations on sockets usually won't
699f87
- * change the mod date).  That saves it from being removed by
699f87
+ * files have a recent mod date (ordinary operations on sockets usually won't
699f87
+ * change the mod date).  That saves them from being removed by
699f87
  * overenthusiastic /tmp-directory-cleaner daemons.  (Another reason we should
699f87
  * never have put the socket file in /tmp...)
699f87
  */
699f87
 void
699f87
-TouchSocketFile(void)
699f87
+TouchSocketFiles(void)
699f87
 {
699f87
-	/* Do nothing if we did not create a socket... */
699f87
-	if (sock_path[0] != '\0')
699f87
+	ListCell   *l;
699f87
+
699f87
+	/* Loop through all created sockets... */
699f87
+	foreach(l, sock_paths)
699f87
 	{
699f87
+		char	   *sock_path = (char *) lfirst(l);
699f87
+
699f87
 		/*
699f87
 		 * utime() is POSIX standard, utimes() is a common alternative. If we
699f87
 		 * have neither, there's no way to affect the mod or access time of
699f87
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
699f87
index 07aca31..e84a18c 100644
699f87
--- a/src/backend/postmaster/postmaster.c
699f87
+++ b/src/backend/postmaster/postmaster.c
699f87
@@ -160,7 +160,9 @@ static Backend *ShmemBackendArray;
699f87
 
699f87
 /* The socket number we are listening for connections on */
699f87
 int			PostPortNumber;
699f87
-char	   *UnixSocketDir;
699f87
+/* The directory names for Unix socket(s) */
699f87
+char	   *Unix_socket_directories;
699f87
+/* The TCP listen address(es) */
699f87
 char	   *ListenAddresses;
699f87
 
699f87
 /*
699f87
@@ -636,7 +638,7 @@ PostmasterMain(int argc, char *argv[])
699f87
 				break;
699f87
 
699f87
 			case 'k':
699f87
-				SetConfigOption("unix_socket_directory", optarg, PGC_POSTMASTER, PGC_S_ARGV);
699f87
+				SetConfigOption("unix_socket_directories", optarg, PGC_POSTMASTER, PGC_S_ARGV);
699f87
 				break;
699f87
 
699f87
 			case 'l':
699f87
@@ -880,7 +882,7 @@ PostmasterMain(int argc, char *argv[])
699f87
 		/* Need a modifiable copy of ListenAddresses */
699f87
 		rawstring = pstrdup(ListenAddresses);
699f87
 
699f87
-		/* Parse string into list of identifiers */
699f87
+		/* Parse string into list of hostnames */
699f87
 		if (!SplitIdentifierString(rawstring, ',', &elemlist))
699f87
 		{
699f87
 			/* syntax error in list */
699f87
@@ -896,12 +898,12 @@ PostmasterMain(int argc, char *argv[])
699f87
 			if (strcmp(curhost, "*") == 0)
699f87
 				status = StreamServerPort(AF_UNSPEC, NULL,
699f87
 										  (unsigned short) PostPortNumber,
699f87
-										  UnixSocketDir,
699f87
+										  NULL,
699f87
 										  ListenSocket, MAXLISTEN);
699f87
 			else
699f87
 				status = StreamServerPort(AF_UNSPEC, curhost,
699f87
 										  (unsigned short) PostPortNumber,
699f87
-										  UnixSocketDir,
699f87
+										  NULL,
699f87
 										  ListenSocket, MAXLISTEN);
699f87
 
699f87
 			if (status == STATUS_OK)
699f87
@@ -920,7 +922,7 @@ PostmasterMain(int argc, char *argv[])
699f87
 								curhost)));
699f87
 		}
699f87
 
699f87
-		if (!success && list_length(elemlist))
699f87
+		if (!success && elemlist != NIL)
699f87
 			ereport(FATAL,
699f87
 					(errmsg("could not create any TCP/IP sockets")));
699f87
 
699f87
@@ -967,13 +969,54 @@ PostmasterMain(int argc, char *argv[])
699f87
 #endif
699f87
 
699f87
 #ifdef HAVE_UNIX_SOCKETS
699f87
-	status = StreamServerPort(AF_UNIX, NULL,
699f87
-							  (unsigned short) PostPortNumber,
699f87
-							  UnixSocketDir,
699f87
-							  ListenSocket, MAXLISTEN);
699f87
-	if (status != STATUS_OK)
699f87
-		ereport(WARNING,
699f87
-				(errmsg("could not create Unix-domain socket")));
699f87
+	if (Unix_socket_directories)
699f87
+	{
699f87
+		char	   *rawstring;
699f87
+		List	   *elemlist;
699f87
+		ListCell   *l;
699f87
+		int			success = 0;
699f87
+
699f87
+		/* Need a modifiable copy of Unix_socket_directories */
699f87
+		rawstring = pstrdup(Unix_socket_directories);
699f87
+
699f87
+		/* Parse string into list of directories */
699f87
+		if (!SplitDirectoriesString(rawstring, ',', &elemlist))
699f87
+		{
699f87
+			/* syntax error in list */
699f87
+			ereport(FATAL,
699f87
+					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
699f87
+					 errmsg("invalid list syntax for \"unix_socket_directories\"")));
699f87
+		}
699f87
+
699f87
+		foreach(l, elemlist)
699f87
+		{
699f87
+			char	   *socketdir = (char *) lfirst(l);
699f87
+
699f87
+			status = StreamServerPort(AF_UNIX, NULL,
699f87
+									  (unsigned short) PostPortNumber,
699f87
+									  socketdir,
699f87
+									  ListenSocket, MAXLISTEN);
699f87
+
699f87
+			if (status == STATUS_OK)
699f87
+			{
699f87
+				success++;
699f87
+				/* record the first successful Unix socket in lockfile */
699f87
+				if (success == 1)
699f87
+					AddToDataDirLockFile(LOCK_FILE_LINE_SOCKET_DIR, socketdir);
699f87
+			}
699f87
+			else
699f87
+				ereport(WARNING,
699f87
+						(errmsg("could not create Unix-domain socket in directory \"%s\"",
699f87
+								socketdir)));
699f87
+		}
699f87
+
699f87
+		if (!success && elemlist != NIL)
699f87
+			ereport(FATAL,
699f87
+					(errmsg("could not create any Unix-domain sockets")));
699f87
+
699f87
+		list_free_deep(elemlist);
699f87
+		pfree(rawstring);
699f87
+	}
699f87
 #endif
699f87
 
699f87
 	/*
699f87
@@ -1561,14 +1604,14 @@ ServerLoop(void)
699f87
 		}
699f87
 
699f87
 		/*
699f87
-		 * Touch Unix socket and lock file every 58 minutes, to ensure that
699f87
+		 * Touch Unix socket and lock files every 58 minutes, to ensure that
699f87
 		 * they are not removed by overzealous /tmp-cleaning tasks.  We assume
699f87
 		 * no one runs cleaners with cutoff times of less than an hour ...
699f87
 		 */
699f87
 		if (now - last_touch_time >= 58 * SECS_PER_MINUTE)
699f87
 		{
699f87
-			TouchSocketFile();
699f87
-			TouchSocketLockFile();
699f87
+			TouchSocketFiles();
699f87
+			TouchSocketLockFiles();
699f87
 			last_touch_time = now;
699f87
 		}
699f87
 	}
699f87
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
699f87
index 39017ff..318ac38 100644
699f87
--- a/src/backend/tcop/postgres.c
699f87
+++ b/src/backend/tcop/postgres.c
699f87
@@ -3393,7 +3393,7 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx,
699f87
 				break;
699f87
 
699f87
 			case 'k':
699f87
-				SetConfigOption("unix_socket_directory", optarg, ctx, gucsource);
699f87
+				SetConfigOption("unix_socket_directories", optarg, ctx, gucsource);
699f87
 				break;
699f87
 
699f87
 			case 'l':
699f87
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
699f87
index d4abfe3..e8217ac 100644
699f87
--- a/src/backend/utils/adt/varlena.c
699f87
+++ b/src/backend/utils/adt/varlena.c
699f87
@@ -2446,6 +2446,119 @@ SplitIdentifierString(char *rawstring, char separator,
699f87
 }
699f87
 
699f87
 
699f87
+/*
699f87
+ * SplitDirectoriesString --- parse a string containing directory names
699f87
+ *
699f87
+ * This is similar to SplitIdentifierString, except that the parsing
699f87
+ * rules are meant to handle pathnames instead of identifiers: there is
699f87
+ * no downcasing, embedded spaces are allowed, the max length is MAXPGPATH-1,
699f87
+ * and we apply canonicalize_path() to each extracted string.  Because of the
699f87
+ * last, the returned strings are separately palloc'd rather than being
699f87
+ * pointers into rawstring --- but we still scribble on rawstring.
699f87
+ *
699f87
+ * Inputs:
699f87
+ *	rawstring: the input string; must be modifiable!
699f87
+ *	separator: the separator punctuation expected between directories
699f87
+ *			   (typically ',' or ';').	Whitespace may also appear around
699f87
+ *			   directories.
699f87
+ * Outputs:
699f87
+ *	namelist: filled with a palloc'd list of directory names.
699f87
+ *			  Caller should list_free_deep() this even on error return.
699f87
+ *
699f87
+ * Returns TRUE if okay, FALSE if there is a syntax error in the string.
699f87
+ *
699f87
+ * Note that an empty string is considered okay here.
699f87
+ */
699f87
+bool
699f87
+SplitDirectoriesString(char *rawstring, char separator,
699f87
+					   List **namelist)
699f87
+{
699f87
+	char	   *nextp = rawstring;
699f87
+	bool		done = false;
699f87
+
699f87
+	*namelist = NIL;
699f87
+
699f87
+	while (isspace((unsigned char) *nextp))
699f87
+		nextp++;				/* skip leading whitespace */
699f87
+
699f87
+	if (*nextp == '\0')
699f87
+		return true;			/* allow empty string */
699f87
+
699f87
+	/* At the top of the loop, we are at start of a new directory. */
699f87
+	do
699f87
+	{
699f87
+		char	   *curname;
699f87
+		char	   *endp;
699f87
+
699f87
+		if (*nextp == '\"')
699f87
+		{
699f87
+			/* Quoted name --- collapse quote-quote pairs */
699f87
+			curname = nextp + 1;
699f87
+			for (;;)
699f87
+			{
699f87
+				endp = strchr(nextp + 1, '\"');
699f87
+				if (endp == NULL)
699f87
+					return false;		/* mismatched quotes */
699f87
+				if (endp[1] != '\"')
699f87
+					break;		/* found end of quoted name */
699f87
+				/* Collapse adjacent quotes into one quote, and look again */
699f87
+				memmove(endp, endp + 1, strlen(endp));
699f87
+				nextp = endp;
699f87
+			}
699f87
+			/* endp now points at the terminating quote */
699f87
+			nextp = endp + 1;
699f87
+		}
699f87
+		else
699f87
+		{
699f87
+			/* Unquoted name --- extends to separator or end of string */
699f87
+			curname = endp = nextp;
699f87
+			while (*nextp && *nextp != separator)
699f87
+			{
699f87
+				/* trailing whitespace should not be included in name */
699f87
+				if (!isspace((unsigned char) *nextp))
699f87
+					endp = nextp + 1;
699f87
+				nextp++;
699f87
+			}
699f87
+			if (curname == endp)
699f87
+				return false;	/* empty unquoted name not allowed */
699f87
+		}
699f87
+
699f87
+		while (isspace((unsigned char) *nextp))
699f87
+			nextp++;			/* skip trailing whitespace */
699f87
+
699f87
+		if (*nextp == separator)
699f87
+		{
699f87
+			nextp++;
699f87
+			while (isspace((unsigned char) *nextp))
699f87
+				nextp++;		/* skip leading whitespace for next */
699f87
+			/* we expect another name, so done remains false */
699f87
+		}
699f87
+		else if (*nextp == '\0')
699f87
+			done = true;
699f87
+		else
699f87
+			return false;		/* invalid syntax */
699f87
+
699f87
+		/* Now safe to overwrite separator with a null */
699f87
+		*endp = '\0';
699f87
+
699f87
+		/* Truncate path if it's overlength */
699f87
+		if (strlen(curname) >= MAXPGPATH)
699f87
+			curname[MAXPGPATH - 1] = '\0';
699f87
+
699f87
+		/*
699f87
+		 * Finished isolating current name --- add it to list
699f87
+		 */
699f87
+		curname = pstrdup(curname);
699f87
+		canonicalize_path(curname);
699f87
+		*namelist = lappend(*namelist, curname);
699f87
+
699f87
+		/* Loop back if we didn't reach end of string */
699f87
+	} while (!done);
699f87
+
699f87
+	return true;
699f87
+}
699f87
+
699f87
+
699f87
 /*****************************************************************************
699f87
  *	Comparison Functions used for bytea
699f87
  *
699f87
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
699f87
index f994af6..db5303c 100644
699f87
--- a/src/backend/utils/init/miscinit.c
699f87
+++ b/src/backend/utils/init/miscinit.c
699f87
@@ -49,8 +49,8 @@
699f87
 
699f87
 ProcessingMode Mode = InitProcessing;
699f87
 
699f87
-/* Note: we rely on this to initialize as zeroes */
699f87
-static char socketLockFile[MAXPGPATH];
699f87
+/* List of lock files to be removed at proc exit */
699f87
+static List *lock_files = NIL;
699f87
 
699f87
 
699f87
 /* ----------------------------------------------------------------
699f87
@@ -640,32 +640,35 @@ GetUserNameFromId(Oid roleid)
699f87
  */
699f87
 
699f87
 /*
699f87
- * proc_exit callback to remove a lockfile.
699f87
+ * proc_exit callback to remove lockfiles.
699f87
  */
699f87
 static void
699f87
-UnlinkLockFile(int status, Datum filename)
699f87
+UnlinkLockFiles(int status, Datum arg)
699f87
 {
699f87
-	char	   *fname = (char *) DatumGetPointer(filename);
699f87
+	ListCell   *l;
699f87
 
699f87
-	if (fname != NULL)
699f87
+	foreach(l, lock_files)
699f87
 	{
699f87
-		if (unlink(fname) != 0)
699f87
-		{
699f87
-			/* Should we complain if the unlink fails? */
699f87
-		}
699f87
-		free(fname);
699f87
+		char	   *curfile = (char *) lfirst(l);
699f87
+
699f87
+		unlink(curfile);
699f87
+		/* Should we complain if the unlink fails? */
699f87
 	}
699f87
+	/* Since we're about to exit, no need to reclaim storage */
699f87
+	lock_files = NIL;
699f87
 }
699f87
 
699f87
 /*
699f87
  * Create a lockfile.
699f87
  *
699f87
- * filename is the name of the lockfile to create.
699f87
+ * filename is the path name of the lockfile to create.
699f87
  * amPostmaster is used to determine how to encode the output PID.
699f87
+ * socketDir is the Unix socket directory path to include (possibly empty).
699f87
  * isDDLock and refName are used to determine what error message to produce.
699f87
  */
699f87
 static void
699f87
 CreateLockFile(const char *filename, bool amPostmaster,
699f87
+			   const char *socketDir,
699f87
 			   bool isDDLock, const char *refName)
699f87
 {
699f87
 	int			fd;
699f87
@@ -891,12 +894,7 @@ CreateLockFile(const char *filename, bool amPostmaster,
699f87
 			 DataDir,
699f87
 			 (long) MyStartTime,
699f87
 			 PostPortNumber,
699f87
-#ifdef HAVE_UNIX_SOCKETS
699f87
-			 (*UnixSocketDir != '\0') ? UnixSocketDir : DEFAULT_PGSOCKET_DIR
699f87
-#else
699f87
-			 ""
699f87
-#endif
699f87
-		);
699f87
+			 socketDir);
699f87
 
699f87
 	/*
699f87
 	 * In a standalone backend, the next line (LOCK_FILE_LINE_LISTEN_ADDR)
699f87
@@ -941,9 +939,14 @@ CreateLockFile(const char *filename, bool amPostmaster,
699f87
 	}
699f87
 
699f87
 	/*
699f87
-	 * Arrange for automatic removal of lockfile at proc_exit.
699f87
+	 * Arrange to unlink the lock file(s) at proc_exit.  If this is the
699f87
+	 * first one, set up the on_proc_exit function to do it; then add this
699f87
+	 * lock file to the list of files to unlink.
699f87
 	 */
699f87
-	on_proc_exit(UnlinkLockFile, PointerGetDatum(strdup(filename)));
699f87
+	if (lock_files == NIL)
699f87
+		on_proc_exit(UnlinkLockFiles, 0);
699f87
+
699f87
+	lock_files = lappend(lock_files, pstrdup(filename));
699f87
 }
699f87
 
699f87
 /*
699f87
@@ -952,41 +955,50 @@ CreateLockFile(const char *filename, bool amPostmaster,
699f87
  * When this is called, we must have already switched the working
699f87
  * directory to DataDir, so we can just use a relative path.  This
699f87
  * helps ensure that we are locking the directory we should be.
699f87
+ *
699f87
+ * Note that the socket directory path line is initially written as empty.
699f87
+ * postmaster.c will rewrite it upon creating the first Unix socket.
699f87
  */
699f87
 void
699f87
 CreateDataDirLockFile(bool amPostmaster)
699f87
 {
699f87
-	CreateLockFile(DIRECTORY_LOCK_FILE, amPostmaster, true, DataDir);
699f87
+	CreateLockFile(DIRECTORY_LOCK_FILE, amPostmaster, "", true, DataDir);
699f87
 }
699f87
 
699f87
 /*
699f87
  * Create a lockfile for the specified Unix socket file.
699f87
  */
699f87
 void
699f87
-CreateSocketLockFile(const char *socketfile, bool amPostmaster)
699f87
+CreateSocketLockFile(const char *socketfile, bool amPostmaster,
699f87
+					 const char *socketDir)
699f87
 {
699f87
 	char		lockfile[MAXPGPATH];
699f87
 
699f87
 	snprintf(lockfile, sizeof(lockfile), "%s.lock", socketfile);
699f87
-	CreateLockFile(lockfile, amPostmaster, false, socketfile);
699f87
-	/* Save name of lockfile for TouchSocketLockFile */
699f87
-	strcpy(socketLockFile, lockfile);
699f87
+	CreateLockFile(lockfile, amPostmaster, socketDir, false, socketfile);
699f87
 }
699f87
 
699f87
 /*
699f87
- * TouchSocketLockFile -- mark socket lock file as recently accessed
699f87
+ * TouchSocketLockFiles -- mark socket lock files as recently accessed
699f87
  *
699f87
- * This routine should be called every so often to ensure that the lock file
699f87
- * has a recent mod or access date.  That saves it
699f87
+ * This routine should be called every so often to ensure that the socket
699f87
+ * lock files have a recent mod or access date.  That saves them
699f87
  * from being removed by overenthusiastic /tmp-directory-cleaner daemons.
699f87
  * (Another reason we should never have put the socket file in /tmp...)
699f87
  */
699f87
 void
699f87
-TouchSocketLockFile(void)
699f87
+TouchSocketLockFiles(void)
699f87
 {
699f87
-	/* Do nothing if we did not create a socket... */
699f87
-	if (socketLockFile[0] != '\0')
699f87
+	ListCell   *l;
699f87
+
699f87
+	foreach(l, lock_files)
699f87
 	{
699f87
+		char	   *socketLockFile = (char *) lfirst(l);
699f87
+
699f87
+		/* No need to touch the data directory lock file, we trust */
699f87
+		if (strcmp(socketLockFile, DIRECTORY_LOCK_FILE) == 0)
699f87
+			continue;
699f87
+
699f87
 		/*
699f87
 		 * utime() is POSIX standard, utimes() is a common alternative; if we
699f87
 		 * have neither, fall back to actually reading the file (which only
699f87
@@ -1018,8 +1030,10 @@ TouchSocketLockFile(void)
699f87
  * Add (or replace) a line in the data directory lock file.
699f87
  * The given string should not include a trailing newline.
699f87
  *
699f87
- * Caution: this erases all following lines.  In current usage that is OK
699f87
- * because lines are added in order.  We could improve it if needed.
699f87
+ * Note: because we don't truncate the file, if we were to rewrite a line
699f87
+ * with less data than it had before, there would be garbage after the last
699f87
+ * line.  We don't ever actually do that, so not worth adding another kernel
699f87
+ * call to cover the possibility.
699f87
  */
699f87
 void
699f87
 AddToDataDirLockFile(int target_line, const char *str)
699f87
@@ -1027,8 +1041,10 @@ AddToDataDirLockFile(int target_line, const char *str)
699f87
 	int			fd;
699f87
 	int			len;
699f87
 	int			lineno;
699f87
-	char	   *ptr;
699f87
-	char		buffer[BLCKSZ];
699f87
+	char	   *srcptr;
699f87
+	char	   *destptr;
699f87
+	char		srcbuffer[BLCKSZ];
699f87
+	char		destbuffer[BLCKSZ];
699f87
 
699f87
 	fd = open(DIRECTORY_LOCK_FILE, O_RDWR | PG_BINARY, 0);
699f87
 	if (fd < 0)
699f87
@@ -1039,7 +1055,7 @@ AddToDataDirLockFile(int target_line, const char *str)
699f87
 						DIRECTORY_LOCK_FILE)));
699f87
 		return;
699f87
 	}
699f87
-	len = read(fd, buffer, sizeof(buffer) - 1);
699f87
+	len = read(fd, srcbuffer, sizeof(srcbuffer) - 1);
699f87
 	if (len < 0)
699f87
 	{
699f87
 		ereport(LOG,
699f87
@@ -1049,37 +1065,51 @@ AddToDataDirLockFile(int target_line, const char *str)
699f87
 		close(fd);
699f87
 		return;
699f87
 	}
699f87
-	buffer[len] = '\0';
699f87
+	srcbuffer[len] = '\0';
699f87
 
699f87
 	/*
699f87
-	 * Skip over lines we are not supposed to rewrite.
699f87
+	 * Advance over lines we are not supposed to rewrite, then copy them
699f87
+	 * to destbuffer.
699f87
 	 */
699f87
-	ptr = buffer;
699f87
+	srcptr = srcbuffer;
699f87
 	for (lineno = 1; lineno < target_line; lineno++)
699f87
 	{
699f87
-		if ((ptr = strchr(ptr, '\n')) == NULL)
699f87
+		if ((srcptr = strchr(srcptr, '\n')) == NULL)
699f87
 		{
699f87
 			elog(LOG, "incomplete data in \"%s\": found only %d newlines while trying to add line %d",
699f87
 				 DIRECTORY_LOCK_FILE, lineno - 1, target_line);
699f87
 			close(fd);
699f87
 			return;
699f87
 		}
699f87
-		ptr++;
699f87
+		srcptr++;
699f87
 	}
699f87
+	memcpy(destbuffer, srcbuffer, srcptr - srcbuffer);
699f87
+	destptr = destbuffer + (srcptr - srcbuffer);
699f87
 
699f87
 	/*
699f87
 	 * Write or rewrite the target line.
699f87
 	 */
699f87
-	snprintf(ptr, buffer + sizeof(buffer) - ptr, "%s\n", str);
699f87
+	snprintf(destptr, destbuffer + sizeof(destbuffer) - destptr, "%s\n", str);
699f87
+	destptr += strlen(destptr);
699f87
+
699f87
+	/*
699f87
+	 * If there are more lines in the old file, append them to destbuffer.
699f87
+	 */
699f87
+	if ((srcptr = strchr(srcptr, '\n')) != NULL)
699f87
+	{
699f87
+		srcptr++;
699f87
+		snprintf(destptr, destbuffer + sizeof(destbuffer) - destptr, "%s",
699f87
+				 srcptr);
699f87
+	}
699f87
 
699f87
 	/*
699f87
 	 * And rewrite the data.  Since we write in a single kernel call, this
699f87
 	 * update should appear atomic to onlookers.
699f87
 	 */
699f87
-	len = strlen(buffer);
699f87
+	len = strlen(destbuffer);
699f87
 	errno = 0;
699f87
 	if (lseek(fd, (off_t) 0, SEEK_SET) != 0 ||
699f87
-		(int) write(fd, buffer, len) != len)
699f87
+		(int) write(fd, destbuffer, len) != len)
699f87
 	{
699f87
 		/* if write didn't set errno, assume problem is no disk space */
699f87
 		if (errno == 0)
699f87
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
699f87
index e5ee0f8..1428cb6 100644
699f87
--- a/src/backend/utils/misc/guc.c
699f87
+++ b/src/backend/utils/misc/guc.c
699f87
@@ -2894,14 +2894,18 @@ static struct config_string ConfigureNamesString[] =
699f87
 	},
699f87
 
699f87
 	{
699f87
-		{"unix_socket_directory", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
699f87
-			gettext_noop("Sets the directory where the Unix-domain socket will be created."),
699f87
+		{"unix_socket_directories", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
699f87
+			gettext_noop("Sets the directories where Unix-domain sockets will be created."),
699f87
 			NULL,
699f87
 			GUC_SUPERUSER_ONLY
699f87
 		},
699f87
-		&UnixSocketDir,
699f87
+		&Unix_socket_directories,
699f87
+#ifdef HAVE_UNIX_SOCKETS
699f87
+		DEFAULT_PGSOCKET_DIR,
699f87
+#else
699f87
 		"",
699f87
-		check_canonical_path, NULL, NULL
699f87
+#endif
699f87
+		NULL, NULL, NULL
699f87
 	},
699f87
 
699f87
 	{
699f87
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
699f87
index 013a91a..c0ab61c 100644
699f87
--- a/src/backend/utils/misc/postgresql.conf.sample
699f87
+++ b/src/backend/utils/misc/postgresql.conf.sample
699f87
@@ -67,7 +67,8 @@
699f87
 # Note:  Increasing max_connections costs ~400 bytes of shared memory per
699f87
 # connection slot, plus lock space (see max_locks_per_transaction).
699f87
 #superuser_reserved_connections = 3	# (change requires restart)
699f87
-#unix_socket_directory = ''		# (change requires restart)
699f87
+#unix_socket_directories = '/tmp'	# comma-separated list of directories
699f87
+					# (change requires restart)
699f87
 #unix_socket_group = ''			# (change requires restart)
699f87
 #unix_socket_permissions = 0777		# begin with 0 to use octal notation
699f87
 					# (change requires restart)
699f87
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
699f87
index 23fa468..452ac0b 100644
699f87
--- a/src/bin/initdb/initdb.c
699f87
+++ b/src/bin/initdb/initdb.c
699f87
@@ -991,7 +991,7 @@ static void
699f87
 setup_config(void)
699f87
 {
699f87
 	char	  **conflines;
699f87
-	char		repltok[TZ_STRLEN_MAX + 100];
699f87
+	char		repltok[MAXPGPATH];
699f87
 	char		path[MAXPGPATH];
699f87
 	const char *default_timezone;
699f87
 
699f87
@@ -1013,6 +1013,15 @@ setup_config(void)
699f87
 				 n_buffers * (BLCKSZ / 1024));
699f87
 	conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);
699f87
 
699f87
+#ifdef HAVE_UNIX_SOCKETS
699f87
+	snprintf(repltok, sizeof(repltok), "#unix_socket_directories = '%s'",
699f87
+			 DEFAULT_PGSOCKET_DIR);
699f87
+#else
699f87
+	snprintf(repltok, sizeof(repltok), "#unix_socket_directories = ''");
699f87
+#endif
699f87
+	conflines = replace_token(conflines, "#unix_socket_directories = '/tmp'",
699f87
+							  repltok);
699f87
+
699f87
 #if DEF_PGPORT != 5432
699f87
 	snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
699f87
 	conflines = replace_token(conflines, "#port = 5432", repltok);
699f87
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
699f87
index 66ef6bd..b815ef2 100644
699f87
--- a/src/bin/pg_ctl/pg_ctl.c
699f87
+++ b/src/bin/pg_ctl/pg_ctl.c
699f87
@@ -561,7 +561,7 @@ test_postmaster_connection(bool do_checkpoint)
699f87
 						hostaddr = optlines[LOCK_FILE_LINE_LISTEN_ADDR - 1];
699f87
 
699f87
 						/*
699f87
-						 * While unix_socket_directory can accept relative
699f87
+						 * While unix_socket_directories can accept relative
699f87
 						 * directories, libpq's host parameter must have a
699f87
 						 * leading slash to indicate a socket directory.  So,
699f87
 						 * ignore sockdir if it's relative, and try to use TCP
699f87
diff --git a/src/include/libpq/libpq.h b/src/include/libpq/libpq.h
699f87
index 6dd91ba..2bfec2b 100644
699f87
--- a/src/include/libpq/libpq.h
699f87
+++ b/src/include/libpq/libpq.h
699f87
@@ -44,12 +44,12 @@ typedef struct
699f87
 /*
699f87
  * prototypes for functions in pqcomm.c
699f87
  */
699f87
-extern int StreamServerPort(int family, char *hostName,
699f87
-	unsigned short portNumber, char *unixSocketName, pgsocket ListenSocket[],
699f87
-				 int MaxListen);
699f87
+extern int	StreamServerPort(int family, char *hostName,
699f87
+				 unsigned short portNumber, char *unixSocketDir,
699f87
+				 pgsocket ListenSocket[], int MaxListen);
699f87
 extern int	StreamConnection(pgsocket server_fd, Port *port);
699f87
 extern void StreamClose(pgsocket sock);
699f87
-extern void TouchSocketFile(void);
699f87
+extern void TouchSocketFiles(void);
699f87
 extern void pq_init(void);
699f87
 extern void pq_comm_reset(void);
699f87
 extern int	pq_getbytes(char *s, size_t len);
699f87
diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
699f87
index 1051ca4..e6c663c 100644
699f87
--- a/src/include/miscadmin.h
699f87
+++ b/src/include/miscadmin.h
699f87
@@ -424,7 +424,7 @@ extern char *local_preload_libraries_string;
699f87
  *		2	data directory path
699f87
  *		3	postmaster start timestamp (time_t representation)
699f87
  *		4	port number
699f87
- *		5	socket directory path (empty on Windows)
699f87
+ *		5	first Unix socket directory path (empty if none)
699f87
  *		6	first listen_address (IP address or "*"; empty if no TCP port)
699f87
  *		7	shared memory key (not present on Windows)
699f87
  *
699f87
@@ -443,8 +443,9 @@ extern char *local_preload_libraries_string;
699f87
 #define LOCK_FILE_LINE_SHMEM_KEY	7
699f87
 
699f87
 extern void CreateDataDirLockFile(bool amPostmaster);
699f87
-extern void CreateSocketLockFile(const char *socketfile, bool amPostmaster);
699f87
-extern void TouchSocketLockFile(void);
699f87
+extern void CreateSocketLockFile(const char *socketfile, bool amPostmaster,
699f87
+					 const char *socketDir);
699f87
+extern void TouchSocketLockFiles(void);
699f87
 extern void AddToDataDirLockFile(int target_line, const char *str);
699f87
 extern bool RecheckDataDirLockFile(void);
699f87
 extern void ValidatePgVersion(const char *path);
699f87
diff --git a/src/include/postmaster/postmaster.h b/src/include/postmaster/postmaster.h
699f87
index 683ce3c..080a8ca 100644
699f87
--- a/src/include/postmaster/postmaster.h
699f87
+++ b/src/include/postmaster/postmaster.h
699f87
@@ -19,7 +19,7 @@ extern int	ReservedBackends;
699f87
 extern int	PostPortNumber;
699f87
 extern int	Unix_socket_permissions;
699f87
 extern char *Unix_socket_group;
699f87
-extern char *UnixSocketDir;
699f87
+extern char *Unix_socket_directories;
699f87
 extern char *ListenAddresses;
699f87
 extern bool ClientAuthInProgress;
699f87
 extern int	PreAuthDelay;
699f87
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
699f87
index 4bf07e4..c830243 100644
699f87
--- a/src/include/utils/builtins.h
699f87
+++ b/src/include/utils/builtins.h
699f87
@@ -754,6 +754,8 @@ extern int	varstr_cmp(char *arg1, int len1, char *arg2, int len2, Oid collid);
699f87
 extern List *textToQualifiedNameList(text *textval);
699f87
 extern bool SplitIdentifierString(char *rawstring, char separator,
699f87
 					  List **namelist);
699f87
+extern bool SplitDirectoriesString(char *rawstring, char separator,
699f87
+					   List **namelist);
699f87
 extern Datum replace_text(PG_FUNCTION_ARGS);
699f87
 extern text *replace_text_regexp(text *src_text, void *regexp,
699f87
 					text *replace_text, bool glob);
699f87
diff --git a/doc/src/sgml/html-stamp b/doc/src/sgml/html-stamp
699f87
index e69de29..fcf9276 100644
699f87
--- a/doc/src/sgml/html-stamp
699f87
+++ b/doc/src/sgml/html-stamp
699f87
@@ -0,0 +1 @@
699f87
+hack
699f87
diff --git a/doc/src/sgml/man-stamp b/doc/src/sgml/man-stamp
699f87
index e69de29..fcf9276 100644
699f87
--- a/doc/src/sgml/man-stamp
699f87
+++ b/doc/src/sgml/man-stamp
699f87
@@ -0,0 +1 @@
699f87
+hack