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