@@ -112,6 +112,65 @@ get_control_data(ClusterInfo *cluster, bool live_check)
112112 pg_putenv ("LC_ALL" , NULL );
113113 pg_putenv ("LC_MESSAGES" , "C" );
114114
115+ /*
116+ * Check for clean shutdown
117+ */
118+ if (!live_check || cluster == & new_cluster )
119+ {
120+ /* only pg_controldata outputs the cluster state */
121+ snprintf (cmd , sizeof (cmd ), "\"%s/pg_controldata\" \"%s\"" ,
122+ cluster -> bindir , cluster -> pgdata );
123+ fflush (stdout );
124+ fflush (stderr );
125+
126+ if ((output = popen (cmd , "r" )) == NULL )
127+ pg_fatal ("could not get control data using %s: %s\n" ,
128+ cmd , strerror (errno ));
129+
130+ /* we have the result of cmd in "output". so parse it line by line now */
131+ while (fgets (bufin , sizeof (bufin ), output ))
132+ {
133+ if ((p = strstr (bufin , "Database cluster state:" )) != NULL )
134+ {
135+ p = strchr (p , ':' );
136+
137+ if (p == NULL || strlen (p ) <= 1 )
138+ pg_fatal ("%d: database cluster state problem\n" , __LINE__ );
139+
140+ p ++ ; /* remove ':' char */
141+
142+ /*
143+ * We checked earlier for a postmaster lock file, and if we found
144+ * one, we tried to start/stop the server to replay the WAL. However,
145+ * pg_ctl -m immediate doesn't leave a lock file, but does require
146+ * WAL replay, so we check here that the server was shut down cleanly,
147+ * from the controldata perspective.
148+ */
149+ /* remove leading spaces */
150+ while (* p == ' ' )
151+ p ++ ;
152+ if (strcmp (p , "shut down\n" ) != 0 )
153+ {
154+ if (cluster == & old_cluster )
155+ pg_fatal ("The source cluster was not shut down cleanly.\n" );
156+ else
157+ pg_fatal ("The target cluster was not shut down cleanly.\n" );
158+ }
159+ got_cluster_state = true;
160+ }
161+ }
162+
163+ pclose (output );
164+
165+ if (!got_cluster_state )
166+ {
167+ if (cluster == & old_cluster )
168+ pg_fatal ("The source cluster lacks cluster state information:\n" );
169+ else
170+ pg_fatal ("The target cluster lacks cluster state information:\n" );
171+ }
172+ }
173+
115174 snprintf (cmd , sizeof (cmd ), "\"%s/%s \"%s\"" ,
116175 cluster -> bindir ,
117176 live_check ? "pg_controldata\"" : "pg_resetxlog\" -n" ,
@@ -417,64 +476,6 @@ get_control_data(ClusterInfo *cluster, bool live_check)
417476
418477 pclose (output );
419478
420- /*
421- * Check for clean shutdown
422- */
423-
424- /* only pg_controldata outputs the cluster state */
425- snprintf (cmd , sizeof (cmd ), "\"%s/pg_controldata\" \"%s\"" ,
426- cluster -> bindir , cluster -> pgdata );
427- fflush (stdout );
428- fflush (stderr );
429-
430- if ((output = popen (cmd , "r" )) == NULL )
431- pg_fatal ("could not get control data using %s: %s\n" ,
432- cmd , strerror (errno ));
433-
434- /* we have the result of cmd in "output". so parse it line by line now */
435- while (fgets (bufin , sizeof (bufin ), output ))
436- {
437- if ((!live_check || cluster == & new_cluster ) &&
438- (p = strstr (bufin , "Database cluster state:" )) != NULL )
439- {
440- p = strchr (p , ':' );
441-
442- if (p == NULL || strlen (p ) <= 1 )
443- pg_fatal ("%d: database cluster state problem\n" , __LINE__ );
444-
445- p ++ ; /* remove ':' char */
446-
447- /*
448- * We checked earlier for a postmaster lock file, and if we found
449- * one, we tried to start/stop the server to replay the WAL. However,
450- * pg_ctl -m immediate doesn't leave a lock file, but does require
451- * WAL replay, so we check here that the server was shut down cleanly,
452- * from the controldata perspective.
453- */
454- /* remove leading spaces */
455- while (* p == ' ' )
456- p ++ ;
457- if (strcmp (p , "shut down\n" ) != 0 )
458- {
459- if (cluster == & old_cluster )
460- pg_fatal ("The source cluster was not shut down cleanly.\n" );
461- else
462- pg_fatal ("The target cluster was not shut down cleanly.\n" );
463- }
464- got_cluster_state = true;
465- }
466- }
467-
468- pclose (output );
469-
470- if (!got_cluster_state )
471- {
472- if (cluster == & old_cluster )
473- pg_fatal ("The source cluster lacks cluster state information:\n" );
474- else
475- pg_fatal ("The target cluster lacks cluster state information:\n" );
476- }
477-
478479 /*
479480 * Restore environment variables
480481 */
0 commit comments