@@ -48,7 +48,7 @@ static void prepare_new_databases(void);
4848static void create_new_objects (void );
4949static void copy_clog_xlog_xid (void );
5050static void set_frozenxids (void );
51- static void setup (char * argv0 , bool live_check );
51+ static void setup (char * argv0 , bool * live_check );
5252static void cleanup (void );
5353
5454ClusterInfo old_cluster ,
@@ -80,9 +80,9 @@ main(int argc, char **argv)
8080 adjust_data_dir (& old_cluster );
8181 adjust_data_dir (& new_cluster );
8282
83- output_check_banner ( & live_check );
83+ setup ( argv [ 0 ], & live_check );
8484
85- setup ( argv [ 0 ], live_check );
85+ output_check_banner ( live_check );
8686
8787 check_cluster_versions ();
8888
@@ -95,7 +95,7 @@ main(int argc, char **argv)
9595
9696
9797 /* -- NEW -- */
98- start_postmaster (& new_cluster );
98+ start_postmaster (& new_cluster , true );
9999
100100 check_new_cluster ();
101101 report_clusters_compatible ();
@@ -116,7 +116,7 @@ main(int argc, char **argv)
116116 /* New now using xids of the old system */
117117
118118 /* -- NEW -- */
119- start_postmaster (& new_cluster );
119+ start_postmaster (& new_cluster , true );
120120
121121 prepare_new_databases ();
122122
@@ -177,7 +177,7 @@ main(int argc, char **argv)
177177
178178
179179static void
180- setup (char * argv0 , bool live_check )
180+ setup (char * argv0 , bool * live_check )
181181{
182182 char exec_path [MAXPGPATH ]; /* full path to my executable */
183183
@@ -189,15 +189,39 @@ setup(char *argv0, bool live_check)
189189
190190 verify_directories ();
191191
192- /* no postmasters should be running */
193- if (!live_check && is_server_running (old_cluster .pgdata ))
194- pg_log (PG_FATAL , "There seems to be a postmaster servicing the old cluster.\n"
195- "Please shutdown that postmaster and try again.\n" );
192+ /* no postmasters should be running, except for a live check */
193+ if (pid_lock_file_exists (old_cluster .pgdata ))
194+ {
195+ /*
196+ * If we have a postmaster.pid file, try to start the server. If
197+ * it starts, the pid file was stale, so stop the server. If it
198+ * doesn't start, assume the server is running. If the pid file
199+ * is left over from a server crash, this also allows any committed
200+ * transactions stored in the WAL to be replayed so they are not
201+ * lost, because WAL files are not transfered from old to new
202+ * servers.
203+ */
204+ if (start_postmaster (& old_cluster , false))
205+ stop_postmaster (false);
206+ else
207+ {
208+ if (!user_opts .check )
209+ pg_log (PG_FATAL , "There seems to be a postmaster servicing the old cluster.\n"
210+ "Please shutdown that postmaster and try again.\n" );
211+ else
212+ * live_check = true;
213+ }
214+ }
196215
197216 /* same goes for the new postmaster */
198- if (is_server_running (new_cluster .pgdata ))
199- pg_log (PG_FATAL , "There seems to be a postmaster servicing the new cluster.\n"
217+ if (pid_lock_file_exists (new_cluster .pgdata ))
218+ {
219+ if (start_postmaster (& new_cluster , false))
220+ stop_postmaster (false);
221+ else
222+ pg_log (PG_FATAL , "There seems to be a postmaster servicing the new cluster.\n"
200223 "Please shutdown that postmaster and try again.\n" );
224+ }
201225
202226 /* get path to pg_upgrade executable */
203227 if (find_my_exec (argv0 , exec_path ) < 0 )
0 commit comments