1 /*
2  *	controldata.c
3  *
4  *	controldata functions
5  *
6  *	Copyright (c) 2010-2021, PostgreSQL Global Development Group
7  *	src/bin/pg_upgrade/controldata.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include <ctype.h>
13 
14 #include "pg_upgrade.h"
15 
16 /*
17  * get_control_data()
18  *
19  * gets pg_control information in "ctrl". Assumes that bindir and
20  * datadir are valid absolute paths to postgresql bin and pgdata
21  * directories respectively *and* pg_resetwal is version compatible
22  * with datadir. The main purpose of this function is to get pg_control
23  * data in a version independent manner.
24  *
25  * The approach taken here is to invoke pg_resetwal with -n option
26  * and then pipe its output. With little string parsing we get the
27  * pg_control data.  pg_resetwal cannot be run while the server is running
28  * so we use pg_controldata;  pg_controldata doesn't provide all the fields
29  * we need to actually perform the upgrade, but it provides enough for
30  * check mode.  We do not implement pg_resetwal -n because it is hard to
31  * return valid xid data for a running server.
32  */
33 void
get_control_data(ClusterInfo * cluster,bool live_check)34 get_control_data(ClusterInfo *cluster, bool live_check)
35 {
36 	char		cmd[MAXPGPATH];
37 	char		bufin[MAX_STRING];
38 	FILE	   *output;
39 	char	   *p;
40 	bool		got_tli = false;
41 	bool		got_log_id = false;
42 	bool		got_log_seg = false;
43 	bool		got_xid = false;
44 	bool		got_oid = false;
45 	bool		got_multi = false;
46 	bool		got_oldestmulti = false;
47 	bool		got_oldestxid = false;
48 	bool		got_mxoff = false;
49 	bool		got_nextxlogfile = false;
50 	bool		got_float8_pass_by_value = false;
51 	bool		got_align = false;
52 	bool		got_blocksz = false;
53 	bool		got_largesz = false;
54 	bool		got_walsz = false;
55 	bool		got_walseg = false;
56 	bool		got_ident = false;
57 	bool		got_index = false;
58 	bool		got_toast = false;
59 	bool		got_large_object = false;
60 	bool		got_date_is_int = false;
61 	bool		got_data_checksum_version = false;
62 	bool		got_cluster_state = false;
63 	char	   *lc_collate = NULL;
64 	char	   *lc_ctype = NULL;
65 	char	   *lc_monetary = NULL;
66 	char	   *lc_numeric = NULL;
67 	char	   *lc_time = NULL;
68 	char	   *lang = NULL;
69 	char	   *language = NULL;
70 	char	   *lc_all = NULL;
71 	char	   *lc_messages = NULL;
72 	uint32		tli = 0;
73 	uint32		logid = 0;
74 	uint32		segno = 0;
75 	char	   *resetwal_bin;
76 
77 
78 	/*
79 	 * Because we test the pg_resetwal output as strings, it has to be in
80 	 * English.  Copied from pg_regress.c.
81 	 */
82 	if (getenv("LC_COLLATE"))
83 		lc_collate = pg_strdup(getenv("LC_COLLATE"));
84 	if (getenv("LC_CTYPE"))
85 		lc_ctype = pg_strdup(getenv("LC_CTYPE"));
86 	if (getenv("LC_MONETARY"))
87 		lc_monetary = pg_strdup(getenv("LC_MONETARY"));
88 	if (getenv("LC_NUMERIC"))
89 		lc_numeric = pg_strdup(getenv("LC_NUMERIC"));
90 	if (getenv("LC_TIME"))
91 		lc_time = pg_strdup(getenv("LC_TIME"));
92 	if (getenv("LANG"))
93 		lang = pg_strdup(getenv("LANG"));
94 	if (getenv("LANGUAGE"))
95 		language = pg_strdup(getenv("LANGUAGE"));
96 	if (getenv("LC_ALL"))
97 		lc_all = pg_strdup(getenv("LC_ALL"));
98 	if (getenv("LC_MESSAGES"))
99 		lc_messages = pg_strdup(getenv("LC_MESSAGES"));
100 
101 	unsetenv("LC_COLLATE");
102 	unsetenv("LC_CTYPE");
103 	unsetenv("LC_MONETARY");
104 	unsetenv("LC_NUMERIC");
105 	unsetenv("LC_TIME");
106 #ifndef WIN32
107 	unsetenv("LANG");
108 #else
109 	/* On Windows the default locale may not be English, so force it */
110 	setenv("LANG", "en", 1);
111 #endif
112 	unsetenv("LANGUAGE");
113 	unsetenv("LC_ALL");
114 	setenv("LC_MESSAGES", "C", 1);
115 
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
145 				 * found one, we tried to start/stop the server to replay the
146 				 * WAL.  However, pg_ctl -m immediate doesn't leave a lock
147 				 * file, but does require WAL replay, so we check here that
148 				 * the server was shut down cleanly, from the controldata
149 				 * perspective.
150 				 */
151 				/* remove leading spaces */
152 				while (*p == ' ')
153 					p++;
154 				if (strcmp(p, "shut down in recovery\n") == 0)
155 				{
156 					if (cluster == &old_cluster)
157 						pg_fatal("The source cluster was shut down while in recovery mode.  To upgrade, use \"rsync\" as documented or shut it down as a primary.\n");
158 					else
159 						pg_fatal("The target cluster was shut down while in recovery mode.  To upgrade, use \"rsync\" as documented or shut it down as a primary.\n");
160 				}
161 				else if (strcmp(p, "shut down\n") != 0)
162 				{
163 					if (cluster == &old_cluster)
164 						pg_fatal("The source cluster was not shut down cleanly.\n");
165 					else
166 						pg_fatal("The target cluster was not shut down cleanly.\n");
167 				}
168 				got_cluster_state = true;
169 			}
170 		}
171 
172 		pclose(output);
173 
174 		if (!got_cluster_state)
175 		{
176 			if (cluster == &old_cluster)
177 				pg_fatal("The source cluster lacks cluster state information:\n");
178 			else
179 				pg_fatal("The target cluster lacks cluster state information:\n");
180 		}
181 	}
182 
183 	/* pg_resetxlog has been renamed to pg_resetwal in version 10 */
184 	if (GET_MAJOR_VERSION(cluster->bin_version) <= 906)
185 		resetwal_bin = "pg_resetxlog\" -n";
186 	else
187 		resetwal_bin = "pg_resetwal\" -n";
188 	snprintf(cmd, sizeof(cmd), "\"%s/%s \"%s\"",
189 			 cluster->bindir,
190 			 live_check ? "pg_controldata\"" : resetwal_bin,
191 			 cluster->pgdata);
192 	fflush(stdout);
193 	fflush(stderr);
194 
195 	if ((output = popen(cmd, "r")) == NULL)
196 		pg_fatal("could not get control data using %s: %s\n",
197 				 cmd, strerror(errno));
198 
199 	/* Only in <= 9.2 */
200 	if (GET_MAJOR_VERSION(cluster->major_version) <= 902)
201 	{
202 		cluster->controldata.data_checksum_version = 0;
203 		got_data_checksum_version = true;
204 	}
205 
206 	/* we have the result of cmd in "output". so parse it line by line now */
207 	while (fgets(bufin, sizeof(bufin), output))
208 	{
209 		pg_log(PG_VERBOSE, "%s", bufin);
210 
211 		if ((p = strstr(bufin, "pg_control version number:")) != NULL)
212 		{
213 			p = strchr(p, ':');
214 
215 			if (p == NULL || strlen(p) <= 1)
216 				pg_fatal("%d: pg_resetwal problem\n", __LINE__);
217 
218 			p++;				/* remove ':' char */
219 			cluster->controldata.ctrl_ver = str2uint(p);
220 		}
221 		else if ((p = strstr(bufin, "Catalog version number:")) != NULL)
222 		{
223 			p = strchr(p, ':');
224 
225 			if (p == NULL || strlen(p) <= 1)
226 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
227 
228 			p++;				/* remove ':' char */
229 			cluster->controldata.cat_ver = str2uint(p);
230 		}
231 		else if ((p = strstr(bufin, "Latest checkpoint's TimeLineID:")) != NULL)
232 		{
233 			p = strchr(p, ':');
234 
235 			if (p == NULL || strlen(p) <= 1)
236 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
237 
238 			p++;				/* remove ':' char */
239 			tli = str2uint(p);
240 			got_tli = true;
241 		}
242 		else if ((p = strstr(bufin, "First log file ID after reset:")) != NULL)
243 		{
244 			p = strchr(p, ':');
245 
246 			if (p == NULL || strlen(p) <= 1)
247 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
248 
249 			p++;				/* remove ':' char */
250 			logid = str2uint(p);
251 			got_log_id = true;
252 		}
253 		else if ((p = strstr(bufin, "First log file segment after reset:")) != NULL)
254 		{
255 			p = strchr(p, ':');
256 
257 			if (p == NULL || strlen(p) <= 1)
258 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
259 
260 			p++;				/* remove ':' char */
261 			segno = str2uint(p);
262 			got_log_seg = true;
263 		}
264 		else if ((p = strstr(bufin, "Latest checkpoint's NextXID:")) != NULL)
265 		{
266 			p = strchr(p, ':');
267 
268 			if (p == NULL || strlen(p) <= 1)
269 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
270 
271 			p++;				/* remove ':' char */
272 			cluster->controldata.chkpnt_nxtepoch = str2uint(p);
273 
274 			/*
275 			 * Delimiter changed from '/' to ':' in 9.6.  We don't test for
276 			 * the catalog version of the change because the catalog version
277 			 * is pulled from pg_controldata too, and it isn't worth adding an
278 			 * order dependency for this --- we just check the string.
279 			 */
280 			if (strchr(p, '/') != NULL)
281 				p = strchr(p, '/');
282 			else if (GET_MAJOR_VERSION(cluster->major_version) >= 906)
283 				p = strchr(p, ':');
284 			else
285 				p = NULL;
286 
287 			if (p == NULL || strlen(p) <= 1)
288 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
289 
290 			p++;				/* remove '/' or ':' char */
291 			cluster->controldata.chkpnt_nxtxid = str2uint(p);
292 			got_xid = true;
293 		}
294 		else if ((p = strstr(bufin, "Latest checkpoint's NextOID:")) != NULL)
295 		{
296 			p = strchr(p, ':');
297 
298 			if (p == NULL || strlen(p) <= 1)
299 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
300 
301 			p++;				/* remove ':' char */
302 			cluster->controldata.chkpnt_nxtoid = str2uint(p);
303 			got_oid = true;
304 		}
305 		else if ((p = strstr(bufin, "Latest checkpoint's NextMultiXactId:")) != NULL)
306 		{
307 			p = strchr(p, ':');
308 
309 			if (p == NULL || strlen(p) <= 1)
310 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
311 
312 			p++;				/* remove ':' char */
313 			cluster->controldata.chkpnt_nxtmulti = str2uint(p);
314 			got_multi = true;
315 		}
316 		else if ((p = strstr(bufin, "Latest checkpoint's oldestXID:")) != NULL)
317 		{
318 			p = strchr(p, ':');
319 
320 			if (p == NULL || strlen(p) <= 1)
321 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
322 
323 			p++;				/* remove ':' char */
324 			cluster->controldata.chkpnt_oldstxid = str2uint(p);
325 			got_oldestxid = true;
326 		}
327 		else if ((p = strstr(bufin, "Latest checkpoint's oldestMultiXid:")) != NULL)
328 		{
329 			p = strchr(p, ':');
330 
331 			if (p == NULL || strlen(p) <= 1)
332 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
333 
334 			p++;				/* remove ':' char */
335 			cluster->controldata.chkpnt_oldstMulti = str2uint(p);
336 			got_oldestmulti = true;
337 		}
338 		else if ((p = strstr(bufin, "Latest checkpoint's NextMultiOffset:")) != NULL)
339 		{
340 			p = strchr(p, ':');
341 
342 			if (p == NULL || strlen(p) <= 1)
343 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
344 
345 			p++;				/* remove ':' char */
346 			cluster->controldata.chkpnt_nxtmxoff = str2uint(p);
347 			got_mxoff = true;
348 		}
349 		else if ((p = strstr(bufin, "First log segment after reset:")) != NULL)
350 		{
351 			/* Skip the colon and any whitespace after it */
352 			p = strchr(p, ':');
353 			if (p == NULL || strlen(p) <= 1)
354 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
355 			p = strpbrk(p, "01234567890ABCDEF");
356 			if (p == NULL || strlen(p) <= 1)
357 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
358 
359 			/* Make sure it looks like a valid WAL file name */
360 			if (strspn(p, "0123456789ABCDEF") != 24)
361 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
362 
363 			strlcpy(cluster->controldata.nextxlogfile, p, 25);
364 			got_nextxlogfile = true;
365 		}
366 		else if ((p = strstr(bufin, "Float8 argument passing:")) != NULL)
367 		{
368 			p = strchr(p, ':');
369 
370 			if (p == NULL || strlen(p) <= 1)
371 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
372 
373 			p++;				/* remove ':' char */
374 			/* used later for contrib check */
375 			cluster->controldata.float8_pass_by_value = strstr(p, "by value") != NULL;
376 			got_float8_pass_by_value = true;
377 		}
378 		else if ((p = strstr(bufin, "Maximum data alignment:")) != NULL)
379 		{
380 			p = strchr(p, ':');
381 
382 			if (p == NULL || strlen(p) <= 1)
383 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
384 
385 			p++;				/* remove ':' char */
386 			cluster->controldata.align = str2uint(p);
387 			got_align = true;
388 		}
389 		else if ((p = strstr(bufin, "Database block size:")) != NULL)
390 		{
391 			p = strchr(p, ':');
392 
393 			if (p == NULL || strlen(p) <= 1)
394 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
395 
396 			p++;				/* remove ':' char */
397 			cluster->controldata.blocksz = str2uint(p);
398 			got_blocksz = true;
399 		}
400 		else if ((p = strstr(bufin, "Blocks per segment of large relation:")) != NULL)
401 		{
402 			p = strchr(p, ':');
403 
404 			if (p == NULL || strlen(p) <= 1)
405 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
406 
407 			p++;				/* remove ':' char */
408 			cluster->controldata.largesz = str2uint(p);
409 			got_largesz = true;
410 		}
411 		else if ((p = strstr(bufin, "WAL block size:")) != NULL)
412 		{
413 			p = strchr(p, ':');
414 
415 			if (p == NULL || strlen(p) <= 1)
416 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
417 
418 			p++;				/* remove ':' char */
419 			cluster->controldata.walsz = str2uint(p);
420 			got_walsz = true;
421 		}
422 		else if ((p = strstr(bufin, "Bytes per WAL segment:")) != NULL)
423 		{
424 			p = strchr(p, ':');
425 
426 			if (p == NULL || strlen(p) <= 1)
427 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
428 
429 			p++;				/* remove ':' char */
430 			cluster->controldata.walseg = str2uint(p);
431 			got_walseg = true;
432 		}
433 		else if ((p = strstr(bufin, "Maximum length of identifiers:")) != NULL)
434 		{
435 			p = strchr(p, ':');
436 
437 			if (p == NULL || strlen(p) <= 1)
438 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
439 
440 			p++;				/* remove ':' char */
441 			cluster->controldata.ident = str2uint(p);
442 			got_ident = true;
443 		}
444 		else if ((p = strstr(bufin, "Maximum columns in an index:")) != NULL)
445 		{
446 			p = strchr(p, ':');
447 
448 			if (p == NULL || strlen(p) <= 1)
449 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
450 
451 			p++;				/* remove ':' char */
452 			cluster->controldata.index = str2uint(p);
453 			got_index = true;
454 		}
455 		else if ((p = strstr(bufin, "Maximum size of a TOAST chunk:")) != NULL)
456 		{
457 			p = strchr(p, ':');
458 
459 			if (p == NULL || strlen(p) <= 1)
460 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
461 
462 			p++;				/* remove ':' char */
463 			cluster->controldata.toast = str2uint(p);
464 			got_toast = true;
465 		}
466 		else if ((p = strstr(bufin, "Size of a large-object chunk:")) != NULL)
467 		{
468 			p = strchr(p, ':');
469 
470 			if (p == NULL || strlen(p) <= 1)
471 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
472 
473 			p++;				/* remove ':' char */
474 			cluster->controldata.large_object = str2uint(p);
475 			got_large_object = true;
476 		}
477 		else if ((p = strstr(bufin, "Date/time type storage:")) != NULL)
478 		{
479 			p = strchr(p, ':');
480 
481 			if (p == NULL || strlen(p) <= 1)
482 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
483 
484 			p++;				/* remove ':' char */
485 			cluster->controldata.date_is_int = strstr(p, "64-bit integers") != NULL;
486 			got_date_is_int = true;
487 		}
488 		else if ((p = strstr(bufin, "checksum")) != NULL)
489 		{
490 			p = strchr(p, ':');
491 
492 			if (p == NULL || strlen(p) <= 1)
493 				pg_fatal("%d: controldata retrieval problem\n", __LINE__);
494 
495 			p++;				/* remove ':' char */
496 			/* used later for contrib check */
497 			cluster->controldata.data_checksum_version = str2uint(p);
498 			got_data_checksum_version = true;
499 		}
500 	}
501 
502 	pclose(output);
503 
504 	/*
505 	 * Restore environment variables.  Note all but LANG and LC_MESSAGES were
506 	 * unset above.
507 	 */
508 	if (lc_collate)
509 		setenv("LC_COLLATE", lc_collate, 1);
510 	if (lc_ctype)
511 		setenv("LC_CTYPE", lc_ctype, 1);
512 	if (lc_monetary)
513 		setenv("LC_MONETARY", lc_monetary, 1);
514 	if (lc_numeric)
515 		setenv("LC_NUMERIC", lc_numeric, 1);
516 	if (lc_time)
517 		setenv("LC_TIME", lc_time, 1);
518 	if (lang)
519 		setenv("LANG", lang, 1);
520 	else
521 		unsetenv("LANG");
522 	if (language)
523 		setenv("LANGUAGE", language, 1);
524 	if (lc_all)
525 		setenv("LC_ALL", lc_all, 1);
526 	if (lc_messages)
527 		setenv("LC_MESSAGES", lc_messages, 1);
528 	else
529 		unsetenv("LC_MESSAGES");
530 
531 	pg_free(lc_collate);
532 	pg_free(lc_ctype);
533 	pg_free(lc_monetary);
534 	pg_free(lc_numeric);
535 	pg_free(lc_time);
536 	pg_free(lang);
537 	pg_free(language);
538 	pg_free(lc_all);
539 	pg_free(lc_messages);
540 
541 	/*
542 	 * Before 9.3, pg_resetwal reported the xlogid and segno of the first log
543 	 * file after reset as separate lines. Starting with 9.3, it reports the
544 	 * WAL file name. If the old cluster is older than 9.3, we construct the
545 	 * WAL file name from the xlogid and segno.
546 	 */
547 	if (GET_MAJOR_VERSION(cluster->major_version) <= 902)
548 	{
549 		if (got_tli && got_log_id && got_log_seg)
550 		{
551 			snprintf(cluster->controldata.nextxlogfile, 25, "%08X%08X%08X",
552 					 tli, logid, segno);
553 			got_nextxlogfile = true;
554 		}
555 	}
556 
557 	/* verify that we got all the mandatory pg_control data */
558 	if (!got_xid || !got_oid ||
559 		!got_multi || !got_oldestxid ||
560 		(!got_oldestmulti &&
561 		 cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER) ||
562 		!got_mxoff || (!live_check && !got_nextxlogfile) ||
563 		!got_float8_pass_by_value || !got_align || !got_blocksz ||
564 		!got_largesz || !got_walsz || !got_walseg || !got_ident ||
565 		!got_index || !got_toast ||
566 		(!got_large_object &&
567 		 cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER) ||
568 		!got_date_is_int || !got_data_checksum_version)
569 	{
570 		if (cluster == &old_cluster)
571 			pg_log(PG_REPORT,
572 				   "The source cluster lacks some required control information:\n");
573 		else
574 			pg_log(PG_REPORT,
575 				   "The target cluster lacks some required control information:\n");
576 
577 		if (!got_xid)
578 			pg_log(PG_REPORT, "  checkpoint next XID\n");
579 
580 		if (!got_oid)
581 			pg_log(PG_REPORT, "  latest checkpoint next OID\n");
582 
583 		if (!got_multi)
584 			pg_log(PG_REPORT, "  latest checkpoint next MultiXactId\n");
585 
586 		if (!got_oldestmulti &&
587 			cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
588 			pg_log(PG_REPORT, "  latest checkpoint oldest MultiXactId\n");
589 
590 		if (!got_oldestxid)
591 			pg_log(PG_REPORT, "  latest checkpoint oldestXID\n");
592 
593 		if (!got_mxoff)
594 			pg_log(PG_REPORT, "  latest checkpoint next MultiXactOffset\n");
595 
596 		if (!live_check && !got_nextxlogfile)
597 			pg_log(PG_REPORT, "  first WAL segment after reset\n");
598 
599 		if (!got_float8_pass_by_value)
600 			pg_log(PG_REPORT, "  float8 argument passing method\n");
601 
602 		if (!got_align)
603 			pg_log(PG_REPORT, "  maximum alignment\n");
604 
605 		if (!got_blocksz)
606 			pg_log(PG_REPORT, "  block size\n");
607 
608 		if (!got_largesz)
609 			pg_log(PG_REPORT, "  large relation segment size\n");
610 
611 		if (!got_walsz)
612 			pg_log(PG_REPORT, "  WAL block size\n");
613 
614 		if (!got_walseg)
615 			pg_log(PG_REPORT, "  WAL segment size\n");
616 
617 		if (!got_ident)
618 			pg_log(PG_REPORT, "  maximum identifier length\n");
619 
620 		if (!got_index)
621 			pg_log(PG_REPORT, "  maximum number of indexed columns\n");
622 
623 		if (!got_toast)
624 			pg_log(PG_REPORT, "  maximum TOAST chunk size\n");
625 
626 		if (!got_large_object &&
627 			cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER)
628 			pg_log(PG_REPORT, "  large-object chunk size\n");
629 
630 		if (!got_date_is_int)
631 			pg_log(PG_REPORT, "  dates/times are integers?\n");
632 
633 		/* value added in Postgres 9.3 */
634 		if (!got_data_checksum_version)
635 			pg_log(PG_REPORT, "  data checksum version\n");
636 
637 		pg_fatal("Cannot continue without required control information, terminating\n");
638 	}
639 }
640 
641 
642 /*
643  * check_control_data()
644  *
645  * check to make sure the control data settings are compatible
646  */
647 void
check_control_data(ControlData * oldctrl,ControlData * newctrl)648 check_control_data(ControlData *oldctrl,
649 				   ControlData *newctrl)
650 {
651 	if (oldctrl->align == 0 || oldctrl->align != newctrl->align)
652 		pg_fatal("old and new pg_controldata alignments are invalid or do not match\n"
653 				 "Likely one cluster is a 32-bit install, the other 64-bit\n");
654 
655 	if (oldctrl->blocksz == 0 || oldctrl->blocksz != newctrl->blocksz)
656 		pg_fatal("old and new pg_controldata block sizes are invalid or do not match\n");
657 
658 	if (oldctrl->largesz == 0 || oldctrl->largesz != newctrl->largesz)
659 		pg_fatal("old and new pg_controldata maximum relation segment sizes are invalid or do not match\n");
660 
661 	if (oldctrl->walsz == 0 || oldctrl->walsz != newctrl->walsz)
662 		pg_fatal("old and new pg_controldata WAL block sizes are invalid or do not match\n");
663 
664 	if (oldctrl->walseg == 0 || oldctrl->walseg != newctrl->walseg)
665 		pg_fatal("old and new pg_controldata WAL segment sizes are invalid or do not match\n");
666 
667 	if (oldctrl->ident == 0 || oldctrl->ident != newctrl->ident)
668 		pg_fatal("old and new pg_controldata maximum identifier lengths are invalid or do not match\n");
669 
670 	if (oldctrl->index == 0 || oldctrl->index != newctrl->index)
671 		pg_fatal("old and new pg_controldata maximum indexed columns are invalid or do not match\n");
672 
673 	if (oldctrl->toast == 0 || oldctrl->toast != newctrl->toast)
674 		pg_fatal("old and new pg_controldata maximum TOAST chunk sizes are invalid or do not match\n");
675 
676 	/* large_object added in 9.5, so it might not exist in the old cluster */
677 	if (oldctrl->large_object != 0 &&
678 		oldctrl->large_object != newctrl->large_object)
679 		pg_fatal("old and new pg_controldata large-object chunk sizes are invalid or do not match\n");
680 
681 	if (oldctrl->date_is_int != newctrl->date_is_int)
682 		pg_fatal("old and new pg_controldata date/time storage types do not match\n");
683 
684 	/*
685 	 * float8_pass_by_value does not need to match, but is used in
686 	 * check_for_isn_and_int8_passing_mismatch().
687 	 */
688 
689 	/*
690 	 * We might eventually allow upgrades from checksum to no-checksum
691 	 * clusters.
692 	 */
693 	if (oldctrl->data_checksum_version == 0 &&
694 		newctrl->data_checksum_version != 0)
695 		pg_fatal("old cluster does not use data checksums but the new one does\n");
696 	else if (oldctrl->data_checksum_version != 0 &&
697 			 newctrl->data_checksum_version == 0)
698 		pg_fatal("old cluster uses data checksums but the new one does not\n");
699 	else if (oldctrl->data_checksum_version != newctrl->data_checksum_version)
700 		pg_fatal("old and new cluster pg_controldata checksum versions do not match\n");
701 }
702 
703 
704 void
disable_old_cluster(void)705 disable_old_cluster(void)
706 {
707 	char		old_path[MAXPGPATH],
708 				new_path[MAXPGPATH];
709 
710 	/* rename pg_control so old server cannot be accidentally started */
711 	prep_status("Adding \".old\" suffix to old global/pg_control");
712 
713 	snprintf(old_path, sizeof(old_path), "%s/global/pg_control", old_cluster.pgdata);
714 	snprintf(new_path, sizeof(new_path), "%s/global/pg_control.old", old_cluster.pgdata);
715 	if (pg_mv_file(old_path, new_path) != 0)
716 		pg_fatal("Unable to rename %s to %s.\n", old_path, new_path);
717 	check_ok();
718 
719 	pg_log(PG_REPORT, "\n"
720 		   "If you want to start the old cluster, you will need to remove\n"
721 		   "the \".old\" suffix from %s/global/pg_control.old.\n"
722 		   "Because \"link\" mode was used, the old cluster cannot be safely\n"
723 		   "started once the new cluster has been started.\n\n", old_cluster.pgdata);
724 }
725