On Mon, Sep  3, 2012 at 09:52:22AM -0400, Bruce Momjian wrote:
> On Sun, Sep  2, 2012 at 11:47:06PM -0400, Tom Lane wrote:
> > Bruce Momjian <br...@momjian.us> writes:
> > > Updated patch attached.
> > 
> > [ looks at that for a bit... ]  Now I see why you were on about that:
> > the method you used here requires both clusters to have the same socket
> > directory.  Which is silly and unnecessary.  Revised patch attached.
> 
> I was trying to avoid setting the host for every client database
> application, so I set PGHOST, but your use of get_db_conn() has
> simplified that, which I had not considered.  Also, you will need to
> update the comment above cluster_conn_opts() to match your new function
> name.
> 
> I am working on an additional enhancement that also pulls the live
> cluster's port number from the postmaster.pid file.  I am attaching the
> part of my patch that was modified to add that feature.  This allows
> live checks without requiring any port numbers to be specified.  Let me
> know if you would like me to email you that merged into your patch, if
> you want it for 9.2.
> 
> Also, I don't see my doc addition on your patch;  was that intentional?

I have applied a modified version of the above patch, attached, to head
and 9.2.  One additional change in this patch is that the current
directory is not used for pre-9.1 servers, in live check and non-live
check mode, because pre-9.1 pg_ctl -w can't handle sockets in
non-default locations;  the info isn't in postmaster.pid for it to use. 
I found this in testing the patch and using a script that tests all
possible major version combinations.

I am concerned this will hamper Tom's attempt to allow for changes to
the compiled-in socket location for pre-9.1 servers, but I don't see a
solution.  FYI, all the binaries will be from the new server, and will
have the new compiled-in socket location.  I suggest using -o to pass in
the socket location.  The new server will use the current directory just
fine.  Tom's patch allowing the old and new servers to use different
socket directories helps in this regard.

-- 
  Bruce Momjian  <br...@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +
diff --git a/contrib/pg_upgrade/check.c b/contrib/pg_upgrade/check.c
new file mode 100644
index d965fa8..c89b2df
*** a/contrib/pg_upgrade/check.c
--- b/contrib/pg_upgrade/check.c
*************** output_check_banner(bool *live_check)
*** 58,69 ****
  	if (user_opts.check && is_server_running(old_cluster.pgdata))
  	{
  		*live_check = true;
- 		if (old_cluster.port == DEF_PGUPORT)
- 			pg_log(PG_FATAL, "When checking a live old server, "
- 				   "you must specify the old server's port number.\n");
- 		if (old_cluster.port == new_cluster.port)
- 			pg_log(PG_FATAL, "When checking a live server, "
- 				   "the old and new port numbers must be different.\n");
  		pg_log(PG_REPORT, "Performing Consistency Checks on Old Live Server\n");
  		pg_log(PG_REPORT, "------------------------------------------------\n");
  	}
--- 58,63 ----
*************** check_cluster_compatibility(bool live_ch
*** 320,325 ****
--- 314,329 ----
  		new_cluster.controldata.cat_ver < TABLE_SPACE_SUBDIRS_CAT_VER)
  		pg_log(PG_FATAL, "This utility can only upgrade to PostgreSQL version 9.0 after 2010-01-11\n"
  			   "because of backend API changes made during development.\n");
+ 
+ 	/* We read the real port number for PG >= 9.1 */
+ 	if (live_check && GET_MAJOR_VERSION(old_cluster.major_version) < 901 &&
+ 		old_cluster.port == DEF_PGUPORT)
+ 			pg_log(PG_FATAL, "When checking a pre-PG 9.1 live old server, "
+ 				   "you must specify the old server's port number.\n");
+ 
+ 	if (live_check && old_cluster.port == new_cluster.port)
+ 		pg_log(PG_FATAL, "When checking a live server, "
+ 			   "the old and new port numbers must be different.\n");
  }
  
  
diff --git a/contrib/pg_upgrade/option.c b/contrib/pg_upgrade/option.c
new file mode 100644
index 6d5a93a..19053fa
*** a/contrib/pg_upgrade/option.c
--- b/contrib/pg_upgrade/option.c
*************** void
*** 392,440 ****
  get_sock_dir(ClusterInfo *cluster, bool live_check)
  {
  #ifdef HAVE_UNIX_SOCKETS
! 	if (!live_check)
! 	{
! 		/* Use the current directory for the socket */
! 		cluster->sockdir = pg_malloc(MAXPGPATH);
! 		if (!getcwd(cluster->sockdir, MAXPGPATH))
! 			pg_log(PG_FATAL, "cannot find current directory\n");
! 	}
! 	else
  	{
! 		/*
! 		 *	If we are doing a live check, we will use the old cluster's Unix
! 		 *	domain socket directory so we can connect to the live server.
! 		 */
! 
! 		/* sockdir was added to postmaster.pid in PG 9.1 */
! 		if (GET_MAJOR_VERSION(cluster->major_version) >= 901)
  		{
! 			char		filename[MAXPGPATH];
  			FILE		*fp;
! 			int			i;
! 
  			snprintf(filename, sizeof(filename), "%s/postmaster.pid",
  					 cluster->pgdata);
  			if ((fp = fopen(filename, "r")) == NULL)
! 				pg_log(PG_FATAL, "Could not get socket directory of the old server\n");
! 
! 			cluster->sockdir = pg_malloc(MAXPGPATH);
! 			for (i = 0; i < LOCK_FILE_LINE_SOCKET_DIR; i++)
! 				if (fgets(cluster->sockdir, MAXPGPATH, fp) == NULL)
! 					pg_log(PG_FATAL, "Could not get socket directory of the old server\n");
! 
  			fclose(fp);
! 
! 			/* Remove trailing newline */
! 			if (strchr(cluster->sockdir, '\n') != NULL)
! 				*strchr(cluster->sockdir, '\n') = '\0';
! 		}
! 		else
! 		{
! 			/* Can't get live sockdir, so assume the default is OK. */
! 			cluster->sockdir = NULL;
  		}
  	}
  #else /* !HAVE_UNIX_SOCKETS */
  	cluster->sockdir = NULL;
  #endif
--- 392,456 ----
  get_sock_dir(ClusterInfo *cluster, bool live_check)
  {
  #ifdef HAVE_UNIX_SOCKETS
! 	/*
! 	 *	sockdir and port were added to postmaster.pid in PG 9.1.
! 	 *	Pre-9.1 cannot process pg_ctl -w for sockets in non-default
! 	 *	locations.
! 	 */
! 	if (GET_MAJOR_VERSION(cluster->major_version) >= 901)
  	{
! 		if (!live_check)
  		{
! 			/* Use the current directory for the socket */
! 			cluster->sockdir = pg_malloc(MAXPGPATH);
! 			if (!getcwd(cluster->sockdir, MAXPGPATH))
! 				pg_log(PG_FATAL, "cannot find current directory\n");
! 		}
! 		else
! 		{
! 			/*
! 			 *	If we are doing a live check, we will use the old cluster's Unix
! 			 *	domain socket directory so we can connect to the live server.
! 			 */
! 			unsigned short orig_port = cluster->port;
! 			char		filename[MAXPGPATH], line[MAXPGPATH];
  			FILE		*fp;
! 			int			lineno;
! 	
  			snprintf(filename, sizeof(filename), "%s/postmaster.pid",
  					 cluster->pgdata);
  			if ((fp = fopen(filename, "r")) == NULL)
! 				pg_log(PG_FATAL, "Cannot open file %s: %m\n", filename);
! 	
! 			for (lineno = 1;
! 				 lineno <= Max(LOCK_FILE_LINE_PORT, LOCK_FILE_LINE_SOCKET_DIR);
! 				 lineno++)
! 			{
! 				if (fgets(line, sizeof(line), fp) == NULL)
! 					pg_log(PG_FATAL, "Cannot read line %d from %s: %m\n", lineno, filename);
! 	
! 				/* potentially overwrite user-supplied value */
! 				if (lineno == LOCK_FILE_LINE_PORT)
! 					sscanf(line, "%hu", &old_cluster.port);
! 				if (lineno == LOCK_FILE_LINE_SOCKET_DIR)
! 				{
! 					cluster->sockdir = pg_malloc(MAXPGPATH);
! 					/* strip off newline */
! 					sscanf(line, "%s\n", cluster->sockdir);
! 				}
! 			}
  			fclose(fp);
! 	
! 			/* warn of port number correction */
! 			if (orig_port != DEF_PGUPORT && old_cluster.port != orig_port)
! 				pg_log(PG_WARNING, "User-supplied old port number %hu corrected to %hu\n",
! 				orig_port, cluster->port);
  		}
  	}
+ 	else
+ 		/* Can't get sockdir and pg_ctl -w can't use a non-default, use default */
+ 		cluster->sockdir = NULL;
+ 
  #else /* !HAVE_UNIX_SOCKETS */
  	cluster->sockdir = NULL;
  #endif
diff --git a/contrib/pg_upgrade/pg_upgrade.c b/contrib/pg_upgrade/pg_upgrade.c
new file mode 100644
index ee3a151..4d2e79c
*** a/contrib/pg_upgrade/pg_upgrade.c
--- b/contrib/pg_upgrade/pg_upgrade.c
*************** main(int argc, char **argv)
*** 86,96 ****
  	setup(argv[0], live_check);
  
  	check_cluster_versions();
- 	check_cluster_compatibility(live_check);
  
  	get_sock_dir(&old_cluster, live_check);
  	get_sock_dir(&new_cluster, false);
  
  	check_old_cluster(live_check, &sequence_script_file_name);
  
  
--- 86,97 ----
  	setup(argv[0], live_check);
  
  	check_cluster_versions();
  
  	get_sock_dir(&old_cluster, live_check);
  	get_sock_dir(&new_cluster, false);
  
+ 	check_cluster_compatibility(live_check);
+ 
  	check_old_cluster(live_check, &sequence_script_file_name);
  
  
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to