@@ -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_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_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_fatal ("The source cluster was not shut down cleanly.\n" );
155
+ else
156
+ 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_fatal ("The source cluster lacks cluster state information:\n" );
168
+ else
169
+ pg_fatal ("The target cluster lacks cluster state information:\n" );
170
+ }
171
+ }
172
+
114
173
snprintf (cmd , sizeof (cmd ), "\"%s/%s \"%s\"" ,
115
174
cluster -> bindir ,
116
175
live_check ? "pg_controldata\"" : "pg_resetxlog\" -n" ,
@@ -452,64 +511,6 @@ get_control_data(ClusterInfo *cluster, bool live_check)
452
511
if (output )
453
512
pclose (output );
454
513
455
- /*
456
- * Check for clean shutdown
457
- */
458
-
459
- /* only pg_controldata outputs the cluster state */
460
- snprintf (cmd , sizeof (cmd ), "\"%s/pg_controldata\" \"%s\"" ,
461
- cluster -> bindir , cluster -> pgdata );
462
- fflush (stdout );
463
- fflush (stderr );
464
-
465
- if ((output = popen (cmd , "r" )) == NULL )
466
- pg_fatal ("could not get control data using %s: %s\n" ,
467
- cmd , strerror (errno ));
468
-
469
- /* we have the result of cmd in "output". so parse it line by line now */
470
- while (fgets (bufin , sizeof (bufin ), output ))
471
- {
472
- if ((!live_check || cluster == & new_cluster ) &&
473
- (p = strstr (bufin , "Database cluster state:" )) != NULL )
474
- {
475
- p = strchr (p , ':' );
476
-
477
- if (p == NULL || strlen (p ) <= 1 )
478
- pg_fatal ("%d: database cluster state problem\n" , __LINE__ );
479
-
480
- p ++ ; /* remove ':' char */
481
-
482
- /*
483
- * We checked earlier for a postmaster lock file, and if we found
484
- * one, we tried to start/stop the server to replay the WAL. However,
485
- * pg_ctl -m immediate doesn't leave a lock file, but does require
486
- * WAL replay, so we check here that the server was shut down cleanly,
487
- * from the controldata perspective.
488
- */
489
- /* remove leading spaces */
490
- while (* p == ' ' )
491
- p ++ ;
492
- if (strcmp (p , "shut down\n" ) != 0 )
493
- {
494
- if (cluster == & old_cluster )
495
- pg_fatal ("The source cluster was not shut down cleanly.\n" );
496
- else
497
- pg_fatal ("The target cluster was not shut down cleanly.\n" );
498
- }
499
- got_cluster_state = true;
500
- }
501
- }
502
-
503
- pclose (output );
504
-
505
- if (!got_cluster_state )
506
- {
507
- if (cluster == & old_cluster )
508
- pg_fatal ("The source cluster lacks cluster state information:\n" );
509
- else
510
- pg_fatal ("The target cluster lacks cluster state information:\n" );
511
- }
512
-
513
514
/*
514
515
* Restore environment variables
515
516
*/
0 commit comments