@@ -111,6 +111,65 @@ get_control_data(ClusterInfo *cluster, bool live_check)
111
111
pg_putenv ("LC_ALL" , NULL );
112
112
pg_putenv ("LC_MESSAGES" , "C" );
113
113
114
+ /*
115
+ * Check for clean shutdown
116
+ */
117
+ if (!live_check || cluster == & new_cluster )
118
+ {
119
+ /* only pg_controldata outputs the cluster state */
120
+ snprintf (cmd , sizeof (cmd ), "\"%s/pg_controldata\" \"%s\"" ,
121
+ cluster -> bindir , cluster -> pgdata );
122
+ fflush (stdout );
123
+ fflush (stderr );
124
+
125
+ if ((output = popen (cmd , "r" )) == NULL )
126
+ pg_log (PG_FATAL , "could not get control data using %s: %s\n" ,
127
+ cmd , strerror (errno ));
128
+
129
+ /* we have the result of cmd in "output". so parse it line by line now */
130
+ while (fgets (bufin , sizeof (bufin ), output ))
131
+ {
132
+ if ((p = strstr (bufin , "Database cluster state:" )) != NULL )
133
+ {
134
+ p = strchr (p , ':' );
135
+
136
+ if (p == NULL || strlen (p ) <= 1 )
137
+ pg_log (PG_FATAL , "%d: database cluster state problem\n" , __LINE__ );
138
+
139
+ p ++ ; /* remove ':' char */
140
+
141
+ /*
142
+ * We checked earlier for a postmaster lock file, and if we found
143
+ * one, we tried to start/stop the server to replay the WAL. However,
144
+ * pg_ctl -m immediate doesn't leave a lock file, but does require
145
+ * WAL replay, so we check here that the server was shut down cleanly,
146
+ * from the controldata perspective.
147
+ */
148
+ /* remove leading spaces */
149
+ while (* p == ' ' )
150
+ p ++ ;
151
+ if (strcmp (p , "shut down\n" ) != 0 )
152
+ {
153
+ if (cluster == & old_cluster )
154
+ pg_log (PG_FATAL , "The source cluster was not shut down cleanly.\n" );
155
+ else
156
+ pg_log (PG_FATAL , "The target cluster was not shut down cleanly.\n" );
157
+ }
158
+ got_cluster_state = true;
159
+ }
160
+ }
161
+
162
+ pclose (output );
163
+
164
+ if (!got_cluster_state )
165
+ {
166
+ if (cluster == & old_cluster )
167
+ pg_log (PG_FATAL , "The source cluster lacks cluster state information:\n" );
168
+ else
169
+ pg_log (PG_FATAL , "The target cluster lacks cluster state information:\n" );
170
+ }
171
+ }
172
+
114
173
snprintf (cmd , sizeof (cmd ), SYSTEMQUOTE "\"%s/%s \"%s\"" SYSTEMQUOTE ,
115
174
cluster -> bindir ,
116
175
live_check ? "pg_controldata\"" : "pg_resetxlog\" -n" ,
@@ -453,64 +512,6 @@ get_control_data(ClusterInfo *cluster, bool live_check)
453
512
if (output )
454
513
pclose (output );
455
514
456
- /*
457
- * Check for clean shutdown
458
- */
459
-
460
- /* only pg_controldata outputs the cluster state */
461
- snprintf (cmd , sizeof (cmd ), "\"%s/pg_controldata\" \"%s\"" ,
462
- cluster -> bindir , cluster -> pgdata );
463
- fflush (stdout );
464
- fflush (stderr );
465
-
466
- if ((output = popen (cmd , "r" )) == NULL )
467
- pg_log (PG_FATAL , "could not get control data using %s: %s\n" ,
468
- cmd , strerror (errno ));
469
-
470
- /* we have the result of cmd in "output". so parse it line by line now */
471
- while (fgets (bufin , sizeof (bufin ), output ))
472
- {
473
- if ((!live_check || cluster == & new_cluster ) &&
474
- (p = strstr (bufin , "Database cluster state:" )) != NULL )
475
- {
476
- p = strchr (p , ':' );
477
-
478
- if (p == NULL || strlen (p ) <= 1 )
479
- pg_log (PG_FATAL , "%d: database cluster state problem\n" , __LINE__ );
480
-
481
- p ++ ; /* remove ':' char */
482
-
483
- /*
484
- * We checked earlier for a postmaster lock file, and if we found
485
- * one, we tried to start/stop the server to replay the WAL. However,
486
- * pg_ctl -m immediate doesn't leave a lock file, but does require
487
- * WAL replay, so we check here that the server was shut down cleanly,
488
- * from the controldata perspective.
489
- */
490
- /* remove leading spaces */
491
- while (* p == ' ' )
492
- p ++ ;
493
- if (strcmp (p , "shut down\n" ) != 0 )
494
- {
495
- if (cluster == & old_cluster )
496
- pg_log (PG_FATAL , "The source cluster was not shut down cleanly.\n" );
497
- else
498
- pg_log (PG_FATAL , "The target cluster was not shut down cleanly.\n" );
499
- }
500
- got_cluster_state = true;
501
- }
502
- }
503
-
504
- pclose (output );
505
-
506
- if (!got_cluster_state )
507
- {
508
- if (cluster == & old_cluster )
509
- pg_log (PG_FATAL , "The source cluster lacks cluster state information:\n" );
510
- else
511
- pg_log (PG_FATAL , "The target cluster lacks cluster state information:\n" );
512
- }
513
-
514
515
/*
515
516
* Restore environment variables
516
517
*/
0 commit comments