'pg_ctl -w' times out when unix_socket_directory is set

Started by Jeff Davisover 19 years ago4 messages
#1Jeff Davis
pgsql@j-davis.com

When I have "unix_socket_directory" set to an alternate value, "pg_ctl -
D data -w start" times out. If I set it to default, it works fine.

I'm using postgresql 8.1.4 on FreeBSD.

Perhaps pg_ctl is waiting to see the socket file in /tmp/ before
reporting that postgresql successfully started?

Regards,
Jeff Davis

#2Jeff Davis
pgsql@j-davis.com
In reply to: Jeff Davis (#1)
1 attachment(s)
Re: 'pg_ctl -w' times out when unix_socket_directory is

On Wed, 2006-09-27 at 14:26 -0700, Jeff Davis wrote:

When I have "unix_socket_directory" set to an alternate value, "pg_ctl -
D data -w start" times out. If I set it to default, it works fine.

I'm using postgresql 8.1.4 on FreeBSD.

Perhaps pg_ctl is waiting to see the socket file in /tmp/ before
reporting that postgresql successfully started?

I took a look at the source quickly (as usual, the postgres source is so
easy to read I should have looked before I posted) and I found that the
problem seems to be in test_postmaster_connection() in pg_ctl.c.

The function checks for a non-default port, including scanning the
configuration file, but does not look for a non-default socket
directory.

It seems reasonable to add a check to find the real socket directory
before trying the connection.

I have attached a patch. I wrote it very quickly, but it seems to work
as I expect.

Regards,
Jeff Davis

Attachments:

pg_ctl.c.patchtext/x-patch; charset=utf-8; name=pg_ctl.c.patchDownload
diff -ru pgsql.orig/src/bin/pg_ctl/pg_ctl.c pgsql/src/bin/pg_ctl/pg_ctl.c
--- pgsql.orig/src/bin/pg_ctl/pg_ctl.c	2006-09-27 14:57:20.000000000 -0700
+++ pgsql/src/bin/pg_ctl/pg_ctl.c	2006-09-27 14:58:02.000000000 -0700
@@ -387,9 +387,13 @@
 	bool		success = false;
 	int			i;
 	char		portstr[32];
+	char		sockdirstr[256];
+	char		*loginhost;
 	char	   *p;
 
 	*portstr = '\0';
+	*sockdirstr = '\0';
+	loginhost = NULL;
 
 	/* post_opts */
 	for (p = post_opts; *p;)
@@ -443,6 +447,37 @@
 			}
 		}
 	}
+	if (!*sockdirstr)
+	{
+		char	  **optlines;
+
+		optlines = readfile(conf_file);
+		if (optlines != NULL)
+		{
+			for (; *optlines != NULL; optlines++)
+			{
+				p = *optlines;
+
+				while (isspace((unsigned char) *p))
+					p++;
+				if (strncmp(p, "unix_socket_directory", strlen("unix_socket_directory")) != 0)
+					continue;
+				p += strlen("unix_socket_directory");
+				while (isspace((unsigned char) *p))
+					p++;
+				if (*p != '=')
+					continue;
+				p++;
+				while (isspace((unsigned char) *p))
+					p++;
+				p++; /* skip first single quote */
+				StrNCpy(sockdirstr, p, Min(strcspn(p, "'") + 1,
+										sizeof(sockdirstr)));
+				/* keep looking, maybe there is another */
+			}
+		}
+		loginhost = sockdirstr;
+	}
 
 	/* environment */
 	if (!*portstr && getenv("PGPORT") != NULL)
@@ -454,7 +489,7 @@
 
 	for (i = 0; i < wait_seconds; i++)
 	{
-		if ((conn = PQsetdbLogin(NULL, portstr, NULL, NULL,
+		if ((conn = PQsetdbLogin(loginhost, portstr, NULL, NULL,
 								 "postgres", NULL, NULL)) != NULL &&
 			(PQstatus(conn) == CONNECTION_OK ||
 			 (strcmp(PQerrorMessage(conn),
Only in pgsql/src/bin/pg_ctl: pg_ctl.c.orig
#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jeff Davis (#2)
Re: 'pg_ctl -w' times out when unix_socket_directory is

Jeff Davis <pgsql@j-davis.com> writes:

I have attached a patch. I wrote it very quickly, but it seems to work
as I expect.

I don't think this is very workable as a postgresql.conf parser ...
at minimum it needs to handle quoted strings correctly, and really it
ought to deal with file inclusions.

There are other reasons why pg_ctl needs to look at
postgresql.conf --- it currently fails to cope with
config-file-outside-the-datadir cases, and that can't be fixed except by
extracting the data_directory setting from the config file. What we
probably need to do is port guc-file.l into the pg_ctl environment.
(Not sure if it's feasible to use a single source file for both cases,
though that would be nice if not too messy.)

regards, tom lane

#4Jeff Davis
pgsql@j-davis.com
In reply to: Tom Lane (#3)
Re: [GENERAL] 'pg_ctl -w' times out when unix_socket_directory is

On Thu, 2006-09-28 at 10:05 -0400, Tom Lane wrote:

Jeff Davis <pgsql@j-davis.com> writes:

I have attached a patch. I wrote it very quickly, but it seems to work
as I expect.

I don't think this is very workable as a postgresql.conf parser ...
at minimum it needs to handle quoted strings correctly, and really it
ought to deal with file inclusions.

There are other reasons why pg_ctl needs to look at
postgresql.conf --- it currently fails to cope with
config-file-outside-the-datadir cases, and that can't be fixed except by
extracting the data_directory setting from the config file. What we
probably need to do is port guc-file.l into the pg_ctl environment.
(Not sure if it's feasible to use a single source file for both cases,
though that would be nice if not too messy.)

And same for reading the port out of postgresql.conf, right?

I was a little skeptical that the function would always work. However,
to the extent that it looked for the correct port, it makes sense it
should look at the correct unix_socket_directory also. I think my patch
handles the simplest cases correctly (a simple single-quoted string),
and I don't think it breaks anything.

I can understand that we want a more correct solution to search for both
parameters.

I could make an attempt to port guc-file.l. However, if you were
planning on fixing this for 8.2, I can't make any guarantees. It does
seem like a bug to me, especially since the FreeBSD port of postgres
uses pg_ctl -w in the startup script (and probably a lot of other
platforms).

Regards,
Jeff Davis