On Wed, Feb 22, 2012 at 03:37:29PM -0500, Bruce Momjian wrote:
> On Wed, Feb 22, 2012 at 05:22:29PM -0300, Alvaro Herrera wrote:
> > Not sure about this.  If the upgrades completes successfully and the
> > file is not renamed at the last minute due to some error, that would be
> > a problem as well, because now the old cluster would happily run and
> > perhaps corrupt the data files from under the new cluster.
> 
> Well, the basic problem is that the user, before pg_upgrade started,
> installed a new cluster that works.  If we rename the old control, but
> rename it back on failure, there are cases we will miss, kill like -9 or
> a server crash, and it will not be obvious to them that the control file
> was renamed.
> 
> Of course, if we only rename on success, and there is kill -9 or server
> crash, the old cluster is still start-able, like the new one.
> 
> One good argument for the rename early is that on a server crash, the
> system is probably going to restart the database automatically, and that
> means the old server.
> 
> Right now we have a clear message that they need to rename the control
> file to start the old server.  Not sure what the new wording would look
> like --- let me try.

I have thought about this, and feel that it would be odd to lock the old
cluster at the start of the upgrade, and then unlock it on a failure,
particularly because we can't always unlock it, e.g. operating system
crash.

A cleaner solution would be to lock it when we complete the upgrade,
which I have done in the attached patch.  I have also added a warning
about restarting the old server when link mode is used, and updated the
documentation to match the new behavior.

Patch attached.  I would like to apply this to 9.2/HEAD.

---------------------------------------------------------------------------

Performing Consistency Checks
-----------------------------
Checking current, bin, and data directories                 ok
Checking cluster versions                                   ok
Checking database user is a superuser                       ok
Checking for prepared transactions                          ok
Checking for reg* system OID user data types                ok
Checking for contrib/isn with bigint-passing mismatch       ok
Creating catalog dump                                       ok
Checking for prepared transactions                          ok
Checking for presence of required libraries                 ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows on the new cluster                        ok
Deleting new commit clogs                                   ok
Copying old commit clogs to new server                      ok
Setting next transaction ID for new cluster                 ok
Resetting WAL archives                                      ok
Setting frozenxid counters in new cluster                   ok
Creating databases in the new cluster                       ok
Adding support functions to new cluster                     ok
Restoring database schema to new cluster                    ok
Removing support functions from new cluster                 ok
Linking user relation files
                                                            ok
Setting next OID for new cluster                            ok
Creating script to delete old cluster                       ok
Adding ".old" suffix to old global/pg_control               ok

If you want to start the old cluster, you will need to remove
the ".old" suffix from /u/pgsql.old/data/global/pg_control.old.
Because "link" mode was used, the old cluster cannot be safely
started once the new cluster has been started.

Upgrade complete
----------------
Optimizer statistics are not transferred by pg_upgrade so
consider running:
    vacuumdb --all --analyze-only
on the newly-upgraded cluster.

Running this script will delete the old cluster's data files:
    /usr/local/pgdev/pg_upgrade/delete_old_cluster.sh

-- 
  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 891eb9a..a5f63eb
*** a/contrib/pg_upgrade/check.c
--- b/contrib/pg_upgrade/check.c
*************** report_clusters_compatible(void)
*** 148,156 ****
  	}
  
  	pg_log(PG_REPORT, "\n"
! 		   "If pg_upgrade fails after this point, you must re-initdb the new cluster\n"
! 		   "before continuing.  You will also need to remove the \".old\" suffix from\n"
! 		   "%s/global/pg_control.old.\n", old_cluster.pgdata);
  }
  
  
--- 148,155 ----
  	}
  
  	pg_log(PG_REPORT, "\n"
! 		   "If pg_upgrade fails after this point, you must re-initdb the\n"
! 		   "new cluster before continuing.\n");
  }
  
  
*************** output_completion_banner(char *deletion_
*** 198,205 ****
  	/* Did we copy the free space files? */
  	if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
  		pg_log(PG_REPORT,
! 			   "Optimizer statistics are not transferred by pg_upgrade so consider\n"
! 			   "running:\n"
  			   "    vacuumdb --all --analyze-only\n"
  			   "on the newly-upgraded cluster.\n\n");
  	else
--- 197,204 ----
  	/* Did we copy the free space files? */
  	if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
  		pg_log(PG_REPORT,
! 			   "Optimizer statistics are not transferred by pg_upgrade so\n"
! 			   "consider running:\n"
  			   "    vacuumdb --all --analyze-only\n"
  			   "on the newly-upgraded cluster.\n\n");
  	else
diff --git a/contrib/pg_upgrade/controldata.c b/contrib/pg_upgrade/controldata.c
new file mode 100644
index 8560d88..4c77ac9
*** a/contrib/pg_upgrade/controldata.c
--- b/contrib/pg_upgrade/controldata.c
*************** check_control_data(ControlData *oldctrl,
*** 516,526 ****
  
  
  void
! rename_old_pg_control(void)
  {
  	char		old_path[MAXPGPATH],
  				new_path[MAXPGPATH];
  
  	prep_status("Adding \".old\" suffix to old global/pg_control");
  
  	snprintf(old_path, sizeof(old_path), "%s/global/pg_control", old_cluster.pgdata);
--- 516,527 ----
  
  
  void
! disable_old_cluster(void)
  {
  	char		old_path[MAXPGPATH],
  				new_path[MAXPGPATH];
  
+ 	/* rename pg_control so old server cannot be accidentally started */
  	prep_status("Adding \".old\" suffix to old global/pg_control");
  
  	snprintf(old_path, sizeof(old_path), "%s/global/pg_control", old_cluster.pgdata);
*************** rename_old_pg_control(void)
*** 528,531 ****
--- 529,540 ----
  	if (pg_mv_file(old_path, new_path) != 0)
  		pg_log(PG_FATAL, "Unable to rename %s to %s.\n", old_path, new_path);
  	check_ok();
+ 
+ 	pg_log(PG_REPORT, "\n"
+ 		   "If you want to start the old cluster, you will need to remove\n"
+ 		   "the \".old\" suffix from %s/global/pg_control.old.\n", old_cluster.pgdata);
+ 	if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
+ 		pg_log(PG_REPORT,
+ 				"Because \"link\" mode was used, the old cluster cannot be safely\n"
+ 				"started once the new cluster has been started.\n");
  }
diff --git a/contrib/pg_upgrade/pg_upgrade.c b/contrib/pg_upgrade/pg_upgrade.c
new file mode 100644
index 15b30fc..47cc683
*** a/contrib/pg_upgrade/pg_upgrade.c
--- b/contrib/pg_upgrade/pg_upgrade.c
***************
*** 43,49 ****
  #include <langinfo.h>
  #endif
  
- static void disable_old_cluster(void);
  static void prepare_new_cluster(void);
  static void prepare_new_databases(void);
  static void create_new_objects(void);
--- 43,48 ----
*************** main(int argc, char **argv)
*** 87,93 ****
  	pg_log(PG_REPORT, "\nPerforming Upgrade\n");
  	pg_log(PG_REPORT, "------------------\n");
  
- 	disable_old_cluster();
  	prepare_new_cluster();
  
  	stop_postmaster(false);
--- 86,91 ----
*************** main(int argc, char **argv)
*** 128,133 ****
--- 126,133 ----
  
  	issue_warnings(sequence_script_file_name);
  
+ 	disable_old_cluster();
+ 
  	pg_log(PG_REPORT, "\nUpgrade complete\n");
  	pg_log(PG_REPORT, "----------------\n");
  
*************** setup(char *argv0, bool live_check)
*** 176,189 ****
  }
  
  
- static void
- disable_old_cluster(void)
- {
- 	/* rename pg_control so old server cannot be accidentally started */
- 	rename_old_pg_control();
- }
- 
- 
  static void
  prepare_new_cluster(void)
  {
--- 176,181 ----
diff --git a/contrib/pg_upgrade/pg_upgrade.h b/contrib/pg_upgrade/pg_upgrade.h
new file mode 100644
index 58d5201..a954815
*** a/contrib/pg_upgrade/pg_upgrade.h
--- b/contrib/pg_upgrade/pg_upgrade.h
*************** void		create_script_for_old_cluster_dele
*** 282,289 ****
  /* controldata.c */
  
  void		get_control_data(ClusterInfo *cluster, bool live_check);
! void check_control_data(ControlData *oldctrl,
! 				   ControlData *newctrl);
  
  
  /* dump.c */
--- 282,289 ----
  /* controldata.c */
  
  void		get_control_data(ClusterInfo *cluster, bool live_check);
! void		check_control_data(ControlData *oldctrl, ControlData *newctrl);
! void		disable_old_cluster(void);
  
  
  /* dump.c */
*************** int exec_prog(bool throw_error, const ch
*** 298,304 ****
  	__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
  void		verify_directories(void);
  bool		is_server_running(const char *datadir);
- void		rename_old_pg_control(void);
  
  
  /* file.c */
--- 298,303 ----
diff --git a/doc/src/sgml/pgupgrade.sgml b/doc/src/sgml/pgupgrade.sgml
new file mode 100644
index 1373069..a27f041
*** a/doc/src/sgml/pgupgrade.sgml
--- b/doc/src/sgml/pgupgrade.sgml
***************
*** 182,188 ****
  
      <para>
       If you are using a version-specific installation directory, e.g.
!      <filename>/opt/PostgreSQL/8.4</>, you do not need to move the old cluster. The
       one-click installers all use version-specific installation directories.
      </para>
  
--- 182,188 ----
  
      <para>
       If you are using a version-specific installation directory, e.g.
!      <filename>/opt/PostgreSQL/9.1</>, you do not need to move the old cluster. The
       one-click installers all use version-specific installation directories.
      </para>
  
*************** gmake prefix=/usr/local/pgsql.new instal
*** 254,260 ****
  
      <para>
       Install any custom shared object files (or DLLs) used by the old cluster
!      into the new cluster, e.g. <filename>pgcrypto.so</filename>, whether they are from <filename>contrib</filename>
       or some other source. Do not install the schema definitions, e.g.
       <filename>pgcrypto.sql</>, because these will be upgraded from the old cluster.
      </para>
--- 254,261 ----
  
      <para>
       Install any custom shared object files (or DLLs) used by the old cluster
!      into the new cluster, e.g. <filename>pgcrypto.so</filename>, 
!      whether they are from <filename>contrib</filename>
       or some other source. Do not install the schema definitions, e.g.
       <filename>pgcrypto.sql</>, because these will be upgraded from the old cluster.
      </para>
*************** psql --username postgres --file script.s
*** 457,471 ****
          If you
          ran <command>pg_upgrade</command> <emphasis>without</> <option>--link</>
          or did not start the new server, the old cluster was not
!         modified except that an <literal>.old</> suffix was appended
!         to <filename>$PGDATA/global/pg_control</> and perhaps
!         tablespace directories. To reuse the old cluster, remove
!         the <filename>.old</> suffix
!         from <filename>$PGDATA/global/pg_control</>. and, if upgrading
!         to 8.4 or earlier, remove the tablespace directories created
!         by the upgrade and remove the <filename>.old</> suffix from
!         the tablespace directory names; then you can restart the old
!         cluster.
         </para>
        </listitem>
       </itemizedlist>
--- 458,469 ----
          If you
          ran <command>pg_upgrade</command> <emphasis>without</> <option>--link</>
          or did not start the new server, the old cluster was not
!         modified except that, if <command>pg_upgrade</command> succeeded,
!         a <literal>.old</> suffix was appended to
!         <filename>$PGDATA/global/pg_control</>.  To reuse the old
!         cluster, possibly remove the <filename>.old</> suffix from
!         <filename>$PGDATA/global/pg_control</>; you can then restart the
!         old cluster.
         </para>
        </listitem>
       </itemizedlist>
*************** psql --username postgres --file script.s
*** 582,590 ****
    </para>
  
    <para>
!    If you want to use link mode and you don't want your old cluster
     to be modified when the new cluster is started, make a copy of the
!    old cluster and upgrade that with link mode. To make a valid copy
     of the old cluster, use <command>rsync</> to create a dirty
     copy of the old cluster while the server is running, then shut down
     the old server and run <command>rsync</> again to update the copy with any
--- 580,588 ----
    </para>
  
    <para>
!    If you want to use link mode and you do not want your old cluster
     to be modified when the new cluster is started, make a copy of the
!    old cluster and upgrade that in link mode. To make a valid copy
     of the old cluster, use <command>rsync</> to create a dirty
     copy of the old cluster while the server is running, then shut down
     the old server and run <command>rsync</> again to update the copy with any
-- 
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