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?

-- 
  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 0fec73e..e7574a2
*** a/contrib/pg_upgrade/check.c
--- b/contrib/pg_upgrade/check.c
*************** output_check_banner(bool *live_check)
*** 29,40 ****
  	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");
  	}
--- 29,34 ----
*************** check_cluster_compatibility(bool live_ch
*** 291,296 ****
--- 285,300 ----
  		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 (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 (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 94bce50..e511839
*** a/contrib/pg_upgrade/option.c
--- b/contrib/pg_upgrade/option.c
*************** adjust_data_dir(ClusterInfo *cluster)
*** 376,378 ****
--- 376,439 ----
  
  	check_ok();
  }
+ 
+ 
+ /*
+  * get_sockdir_and_live_port
+  *
+  * Set the socket directory to either the configured location (live check)
+  * or the current directory, and live port number.
+  */
+ void
+ get_sockdir_and_live_port(bool live_check)
+ {
+ 	if (!live_check)
+ 	{
+ #ifdef HAVE_UNIX_SOCKETS
+ 		/* Use the current directory for the socket */
+ 		if (!getcwd(os_info.sockdir, MAXPGPATH))
+ 			pg_log(PG_FATAL, "cannot find current directory\n");
+ #endif
+ 	}
+ 	/* port/sockdir added to the 4th/5th line of postmaster.pid in PG 9.1 */
+ 	else if (GET_MAJOR_VERSION(old_cluster.major_version) >= 901)
+ 	{
+ 		/* get live port number and sockdir */
+ 
+ 		char		filename[MAXPGPATH], line[MAXPGPATH];
+ 		FILE		*fp;
+ 		int			i;
+ 		unsigned short orig_old_port = old_cluster.port;
+ 
+ 		snprintf(filename, sizeof(filename), "%s/postmaster.pid", old_cluster.pgdata);
+ 		if ((fp = fopen(filename, "r")) == NULL)
+ 			pg_log(PG_FATAL, "Cannot open file %s: %m\n", filename);
+ 	
+ 		for (i = 0; i < 5; i++)
+ 		{
+ 			if (fgets(line, sizeof(line), fp) == NULL)
+ 				pg_log(PG_FATAL, "Cannot read line %d from %s: %m\n", i + 1, filename);
+ 			/* potentially overwrite user-supplied value */
+ 			if (i == 3)
+ 				sscanf(line, "%hu", &old_cluster.port);
+ #ifdef HAVE_UNIX_SOCKETS
+ 			if (i == 4)
+ 				/* strip off newline */
+ 				sscanf(line, "%s\n", os_info.sockdir);
+ #endif
+ 		}
+ 
+ 		/* warn of port number correction */
+ 		if (old_cluster.port != orig_old_port && orig_old_port != DEF_PGUPORT)
+ 			pg_log(PG_WARNING, "User-supplied old port number %hu corrected to %hu\n",
+ 			orig_old_port, old_cluster.port);
+ 			
+ 		fclose(fp);
+ 	}
+ 
+ #ifdef HAVE_UNIX_SOCKETS
+ 	if (strlen(os_info.sockdir) != 0)
+ 		/* For client communication */
+ 		pg_putenv("PGHOST", os_info.sockdir);
+ #endif
+ }
diff --git a/contrib/pg_upgrade/pg_upgrade.c b/contrib/pg_upgrade/pg_upgrade.c
new file mode 100644
index c47c8bb..6b41559
*** 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);
  
  	check_old_cluster(live_check, &sequence_script_file_name);
  
- 
  	/* -- NEW -- */
  	start_postmaster(&new_cluster);
  
--- 86,97 ----
  	setup(argv[0], live_check);
  
  	check_cluster_versions();
+ 	get_sockdir_and_live_port(live_check);
+ 
  	check_cluster_compatibility(live_check);
  
  	check_old_cluster(live_check, &sequence_script_file_name);
  
  	/* -- NEW -- */
  	start_postmaster(&new_cluster);
  
-- 
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