4 * controldata functions
6 * Copyright (c) 2010-2019, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/controldata.c
10 #include "postgres_fe.h"
12 #include "pg_upgrade.h"
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.
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.
34 get_control_data(ClusterInfo *cluster, bool live_check)
37 char bufin[MAX_STRING];
41 bool got_log_id = false;
42 bool got_log_seg = false;
45 bool got_multi = false;
46 bool got_oldestmulti = false;
47 bool got_mxoff = false;
48 bool got_nextxlogfile = false;
49 bool got_float8_pass_by_value = false;
50 bool got_align = false;
51 bool got_blocksz = false;
52 bool got_largesz = false;
53 bool got_walsz = false;
54 bool got_walseg = false;
55 bool got_ident = false;
56 bool got_index = false;
57 bool got_toast = false;
58 bool got_large_object = false;
59 bool got_date_is_int = false;
60 bool got_data_checksum_version = false;
61 bool got_cluster_state = false;
62 char *lc_collate = NULL;
63 char *lc_ctype = NULL;
64 char *lc_monetary = NULL;
65 char *lc_numeric = NULL;
68 char *language = NULL;
70 char *lc_messages = NULL;
78 * Because we test the pg_resetwal output as strings, it has to be in
79 * English. Copied from pg_regress.c.
81 if (getenv("LC_COLLATE"))
82 lc_collate = pg_strdup(getenv("LC_COLLATE"));
83 if (getenv("LC_CTYPE"))
84 lc_ctype = pg_strdup(getenv("LC_CTYPE"));
85 if (getenv("LC_MONETARY"))
86 lc_monetary = pg_strdup(getenv("LC_MONETARY"));
87 if (getenv("LC_NUMERIC"))
88 lc_numeric = pg_strdup(getenv("LC_NUMERIC"));
89 if (getenv("LC_TIME"))
90 lc_time = pg_strdup(getenv("LC_TIME"));
92 lang = pg_strdup(getenv("LANG"));
93 if (getenv("LANGUAGE"))
94 language = pg_strdup(getenv("LANGUAGE"));
96 lc_all = pg_strdup(getenv("LC_ALL"));
97 if (getenv("LC_MESSAGES"))
98 lc_messages = pg_strdup(getenv("LC_MESSAGES"));
100 pg_putenv("LC_COLLATE", NULL);
101 pg_putenv("LC_CTYPE", NULL);
102 pg_putenv("LC_MONETARY", NULL);
103 pg_putenv("LC_NUMERIC", NULL);
104 pg_putenv("LC_TIME", NULL);
106 pg_putenv("LANG", NULL);
108 /* On Windows the default locale cannot be English, so force it */
109 pg_putenv("LANG", "en");
111 pg_putenv("LANGUAGE", NULL);
112 pg_putenv("LC_ALL", NULL);
113 pg_putenv("LC_MESSAGES", "C");
116 * Check for clean shutdown
118 if (!live_check || cluster == &new_cluster)
120 /* only pg_controldata outputs the cluster state */
121 snprintf(cmd, sizeof(cmd), "\"%s/pg_controldata\" \"%s\"",
122 cluster->bindir, cluster->pgdata);
126 if ((output = popen(cmd, "r")) == NULL)
127 pg_fatal("could not get control data using %s: %s\n",
128 cmd, strerror(errno));
130 /* we have the result of cmd in "output". so parse it line by line now */
131 while (fgets(bufin, sizeof(bufin), output))
133 if ((p = strstr(bufin, "Database cluster state:")) != NULL)
137 if (p == NULL || strlen(p) <= 1)
138 pg_fatal("%d: database cluster state problem\n", __LINE__);
140 p++; /* remove ':' char */
143 * We checked earlier for a postmaster lock file, and if we
144 * found one, we tried to start/stop the server to replay the
145 * WAL. However, pg_ctl -m immediate doesn't leave a lock
146 * file, but does require WAL replay, so we check here that
147 * the server was shut down cleanly, from the controldata
150 /* remove leading spaces */
153 if (strcmp(p, "shut down in recovery\n") == 0)
155 if (cluster == &old_cluster)
156 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 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 else if (strcmp(p, "shut down\n") != 0)
162 if (cluster == &old_cluster)
163 pg_fatal("The source cluster was not shut down cleanly.\n");
165 pg_fatal("The target cluster was not shut down cleanly.\n");
167 got_cluster_state = true;
173 if (!got_cluster_state)
175 if (cluster == &old_cluster)
176 pg_fatal("The source cluster lacks cluster state information:\n");
178 pg_fatal("The target cluster lacks cluster state information:\n");
182 /* pg_resetxlog has been renamed to pg_resetwal in version 10 */
183 if (GET_MAJOR_VERSION(cluster->bin_version) < 1000)
184 resetwal_bin = "pg_resetxlog\" -n";
186 resetwal_bin = "pg_resetwal\" -n";
187 snprintf(cmd, sizeof(cmd), "\"%s/%s \"%s\"",
189 live_check ? "pg_controldata\"" : resetwal_bin,
194 if ((output = popen(cmd, "r")) == NULL)
195 pg_fatal("could not get control data using %s: %s\n",
196 cmd, strerror(errno));
199 if (GET_MAJOR_VERSION(cluster->major_version) <= 902)
201 cluster->controldata.data_checksum_version = 0;
202 got_data_checksum_version = true;
205 /* we have the result of cmd in "output". so parse it line by line now */
206 while (fgets(bufin, sizeof(bufin), output))
208 pg_log(PG_VERBOSE, "%s", bufin);
210 if ((p = strstr(bufin, "pg_control version number:")) != NULL)
214 if (p == NULL || strlen(p) <= 1)
215 pg_fatal("%d: pg_resetwal problem\n", __LINE__);
217 p++; /* remove ':' char */
218 cluster->controldata.ctrl_ver = str2uint(p);
220 else if ((p = strstr(bufin, "Catalog version number:")) != NULL)
224 if (p == NULL || strlen(p) <= 1)
225 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
227 p++; /* remove ':' char */
228 cluster->controldata.cat_ver = str2uint(p);
230 else if ((p = strstr(bufin, "Latest checkpoint's TimeLineID:")) != NULL)
234 if (p == NULL || strlen(p) <= 1)
235 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
237 p++; /* remove ':' char */
241 else if ((p = strstr(bufin, "First log file ID after reset:")) != NULL)
245 if (p == NULL || strlen(p) <= 1)
246 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
248 p++; /* remove ':' char */
252 else if ((p = strstr(bufin, "First log file segment after reset:")) != NULL)
256 if (p == NULL || strlen(p) <= 1)
257 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
259 p++; /* remove ':' char */
263 else if ((p = strstr(bufin, "Latest checkpoint's NextXID:")) != NULL)
267 if (p == NULL || strlen(p) <= 1)
268 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
270 p++; /* remove ':' char */
271 cluster->controldata.chkpnt_nxtepoch = str2uint(p);
274 * Delimiter changed from '/' to ':' in 9.6. We don't test for
275 * the catalog version of the change because the catalog version
276 * is pulled from pg_controldata too, and it isn't worth adding an
277 * order dependency for this --- we just check the string.
279 if (strchr(p, '/') != NULL)
281 else if (GET_MAJOR_VERSION(cluster->major_version) >= 906)
286 if (p == NULL || strlen(p) <= 1)
287 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
289 p++; /* remove '/' or ':' char */
290 cluster->controldata.chkpnt_nxtxid = str2uint(p);
293 else if ((p = strstr(bufin, "Latest checkpoint's NextOID:")) != NULL)
297 if (p == NULL || strlen(p) <= 1)
298 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
300 p++; /* remove ':' char */
301 cluster->controldata.chkpnt_nxtoid = str2uint(p);
304 else if ((p = strstr(bufin, "Latest checkpoint's NextMultiXactId:")) != NULL)
308 if (p == NULL || strlen(p) <= 1)
309 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
311 p++; /* remove ':' char */
312 cluster->controldata.chkpnt_nxtmulti = str2uint(p);
315 else if ((p = strstr(bufin, "Latest checkpoint's oldestMultiXid:")) != NULL)
319 if (p == NULL || strlen(p) <= 1)
320 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
322 p++; /* remove ':' char */
323 cluster->controldata.chkpnt_oldstMulti = str2uint(p);
324 got_oldestmulti = true;
326 else if ((p = strstr(bufin, "Latest checkpoint's NextMultiOffset:")) != NULL)
330 if (p == NULL || strlen(p) <= 1)
331 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
333 p++; /* remove ':' char */
334 cluster->controldata.chkpnt_nxtmxoff = str2uint(p);
337 else if ((p = strstr(bufin, "First log segment after reset:")) != NULL)
339 /* Skip the colon and any whitespace after it */
341 if (p == NULL || strlen(p) <= 1)
342 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
343 p = strpbrk(p, "01234567890ABCDEF");
344 if (p == NULL || strlen(p) <= 1)
345 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
347 /* Make sure it looks like a valid WAL file name */
348 if (strspn(p, "0123456789ABCDEF") != 24)
349 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
351 strlcpy(cluster->controldata.nextxlogfile, p, 25);
352 got_nextxlogfile = true;
354 else if ((p = strstr(bufin, "Float8 argument passing:")) != NULL)
358 if (p == NULL || strlen(p) <= 1)
359 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
361 p++; /* remove ':' char */
362 /* used later for contrib check */
363 cluster->controldata.float8_pass_by_value = strstr(p, "by value") != NULL;
364 got_float8_pass_by_value = true;
366 else if ((p = strstr(bufin, "Maximum data alignment:")) != NULL)
370 if (p == NULL || strlen(p) <= 1)
371 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
373 p++; /* remove ':' char */
374 cluster->controldata.align = str2uint(p);
377 else if ((p = strstr(bufin, "Database block size:")) != NULL)
381 if (p == NULL || strlen(p) <= 1)
382 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
384 p++; /* remove ':' char */
385 cluster->controldata.blocksz = str2uint(p);
388 else if ((p = strstr(bufin, "Blocks per segment of large relation:")) != NULL)
392 if (p == NULL || strlen(p) <= 1)
393 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
395 p++; /* remove ':' char */
396 cluster->controldata.largesz = str2uint(p);
399 else if ((p = strstr(bufin, "WAL block size:")) != NULL)
403 if (p == NULL || strlen(p) <= 1)
404 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
406 p++; /* remove ':' char */
407 cluster->controldata.walsz = str2uint(p);
410 else if ((p = strstr(bufin, "Bytes per WAL segment:")) != NULL)
414 if (p == NULL || strlen(p) <= 1)
415 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
417 p++; /* remove ':' char */
418 cluster->controldata.walseg = str2uint(p);
421 else if ((p = strstr(bufin, "Maximum length of identifiers:")) != NULL)
425 if (p == NULL || strlen(p) <= 1)
426 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
428 p++; /* remove ':' char */
429 cluster->controldata.ident = str2uint(p);
432 else if ((p = strstr(bufin, "Maximum columns in an index:")) != NULL)
436 if (p == NULL || strlen(p) <= 1)
437 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
439 p++; /* remove ':' char */
440 cluster->controldata.index = str2uint(p);
443 else if ((p = strstr(bufin, "Maximum size of a TOAST chunk:")) != NULL)
447 if (p == NULL || strlen(p) <= 1)
448 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
450 p++; /* remove ':' char */
451 cluster->controldata.toast = str2uint(p);
454 else if ((p = strstr(bufin, "Size of a large-object chunk:")) != NULL)
458 if (p == NULL || strlen(p) <= 1)
459 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
461 p++; /* remove ':' char */
462 cluster->controldata.large_object = str2uint(p);
463 got_large_object = true;
465 else if ((p = strstr(bufin, "Date/time type storage:")) != NULL)
469 if (p == NULL || strlen(p) <= 1)
470 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
472 p++; /* remove ':' char */
473 cluster->controldata.date_is_int = strstr(p, "64-bit integers") != NULL;
474 got_date_is_int = true;
476 else if ((p = strstr(bufin, "checksum")) != NULL)
480 if (p == NULL || strlen(p) <= 1)
481 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
483 p++; /* remove ':' char */
484 /* used later for contrib check */
485 cluster->controldata.data_checksum_version = str2uint(p);
486 got_data_checksum_version = true;
493 * Restore environment variables
495 pg_putenv("LC_COLLATE", lc_collate);
496 pg_putenv("LC_CTYPE", lc_ctype);
497 pg_putenv("LC_MONETARY", lc_monetary);
498 pg_putenv("LC_NUMERIC", lc_numeric);
499 pg_putenv("LC_TIME", lc_time);
500 pg_putenv("LANG", lang);
501 pg_putenv("LANGUAGE", language);
502 pg_putenv("LC_ALL", lc_all);
503 pg_putenv("LC_MESSAGES", lc_messages);
507 pg_free(lc_monetary);
513 pg_free(lc_messages);
516 * Before 9.3, pg_resetwal reported the xlogid and segno of the first log
517 * file after reset as separate lines. Starting with 9.3, it reports the
518 * WAL file name. If the old cluster is older than 9.3, we construct the
519 * WAL file name from the xlogid and segno.
521 if (GET_MAJOR_VERSION(cluster->major_version) <= 902)
523 if (got_tli && got_log_id && got_log_seg)
525 snprintf(cluster->controldata.nextxlogfile, 25, "%08X%08X%08X",
527 got_nextxlogfile = true;
531 /* verify that we got all the mandatory pg_control data */
532 if (!got_xid || !got_oid ||
535 cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER) ||
536 !got_mxoff || (!live_check && !got_nextxlogfile) ||
537 !got_float8_pass_by_value || !got_align || !got_blocksz ||
538 !got_largesz || !got_walsz || !got_walseg || !got_ident ||
539 !got_index || !got_toast ||
540 (!got_large_object &&
541 cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER) ||
542 !got_date_is_int || !got_data_checksum_version)
544 if (cluster == &old_cluster)
546 "The source cluster lacks some required control information:\n");
549 "The target cluster lacks some required control information:\n");
552 pg_log(PG_REPORT, " checkpoint next XID\n");
555 pg_log(PG_REPORT, " latest checkpoint next OID\n");
558 pg_log(PG_REPORT, " latest checkpoint next MultiXactId\n");
560 if (!got_oldestmulti &&
561 cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
562 pg_log(PG_REPORT, " latest checkpoint oldest MultiXactId\n");
565 pg_log(PG_REPORT, " latest checkpoint next MultiXactOffset\n");
567 if (!live_check && !got_nextxlogfile)
568 pg_log(PG_REPORT, " first WAL segment after reset\n");
570 if (!got_float8_pass_by_value)
571 pg_log(PG_REPORT, " float8 argument passing method\n");
574 pg_log(PG_REPORT, " maximum alignment\n");
577 pg_log(PG_REPORT, " block size\n");
580 pg_log(PG_REPORT, " large relation segment size\n");
583 pg_log(PG_REPORT, " WAL block size\n");
586 pg_log(PG_REPORT, " WAL segment size\n");
589 pg_log(PG_REPORT, " maximum identifier length\n");
592 pg_log(PG_REPORT, " maximum number of indexed columns\n");
595 pg_log(PG_REPORT, " maximum TOAST chunk size\n");
597 if (!got_large_object &&
598 cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER)
599 pg_log(PG_REPORT, " large-object chunk size\n");
601 if (!got_date_is_int)
602 pg_log(PG_REPORT, " dates/times are integers?\n");
604 /* value added in Postgres 9.3 */
605 if (!got_data_checksum_version)
606 pg_log(PG_REPORT, " data checksum version\n");
608 pg_fatal("Cannot continue without required control information, terminating\n");
614 * check_control_data()
616 * check to make sure the control data settings are compatible
619 check_control_data(ControlData *oldctrl,
620 ControlData *newctrl)
622 if (oldctrl->align == 0 || oldctrl->align != newctrl->align)
623 pg_fatal("old and new pg_controldata alignments are invalid or do not match\n"
624 "Likely one cluster is a 32-bit install, the other 64-bit\n");
626 if (oldctrl->blocksz == 0 || oldctrl->blocksz != newctrl->blocksz)
627 pg_fatal("old and new pg_controldata block sizes are invalid or do not match\n");
629 if (oldctrl->largesz == 0 || oldctrl->largesz != newctrl->largesz)
630 pg_fatal("old and new pg_controldata maximum relation segment sizes are invalid or do not match\n");
632 if (oldctrl->walsz == 0 || oldctrl->walsz != newctrl->walsz)
633 pg_fatal("old and new pg_controldata WAL block sizes are invalid or do not match\n");
635 if (oldctrl->walseg == 0 || oldctrl->walseg != newctrl->walseg)
636 pg_fatal("old and new pg_controldata WAL segment sizes are invalid or do not match\n");
638 if (oldctrl->ident == 0 || oldctrl->ident != newctrl->ident)
639 pg_fatal("old and new pg_controldata maximum identifier lengths are invalid or do not match\n");
641 if (oldctrl->index == 0 || oldctrl->index != newctrl->index)
642 pg_fatal("old and new pg_controldata maximum indexed columns are invalid or do not match\n");
644 if (oldctrl->toast == 0 || oldctrl->toast != newctrl->toast)
645 pg_fatal("old and new pg_controldata maximum TOAST chunk sizes are invalid or do not match\n");
647 /* large_object added in 9.5, so it might not exist in the old cluster */
648 if (oldctrl->large_object != 0 &&
649 oldctrl->large_object != newctrl->large_object)
650 pg_fatal("old and new pg_controldata large-object chunk sizes are invalid or do not match\n");
652 if (oldctrl->date_is_int != newctrl->date_is_int)
653 pg_fatal("old and new pg_controldata date/time storage types do not match\n");
656 * float8_pass_by_value does not need to match, but is used in
657 * check_for_isn_and_int8_passing_mismatch().
661 * We might eventually allow upgrades from checksum to no-checksum
664 if (oldctrl->data_checksum_version == 0 &&
665 newctrl->data_checksum_version != 0)
666 pg_fatal("old cluster does not use data checksums but the new one does\n");
667 else if (oldctrl->data_checksum_version != 0 &&
668 newctrl->data_checksum_version == 0)
669 pg_fatal("old cluster uses data checksums but the new one does not\n");
670 else if (oldctrl->data_checksum_version != newctrl->data_checksum_version)
671 pg_fatal("old and new cluster pg_controldata checksum versions do not match\n");
676 disable_old_cluster(void)
678 char old_path[MAXPGPATH],
681 /* rename pg_control so old server cannot be accidentally started */
682 prep_status("Adding \".old\" suffix to old global/pg_control");
684 snprintf(old_path, sizeof(old_path), "%s/global/pg_control", old_cluster.pgdata);
685 snprintf(new_path, sizeof(new_path), "%s/global/pg_control.old", old_cluster.pgdata);
686 if (pg_mv_file(old_path, new_path) != 0)
687 pg_fatal("Unable to rename %s to %s.\n", old_path, new_path);
690 pg_log(PG_REPORT, "\n"
691 "If you want to start the old cluster, you will need to remove\n"
692 "the \".old\" suffix from %s/global/pg_control.old.\n"
693 "Because \"link\" mode was used, the old cluster cannot be safely\n"
694 "started once the new cluster has been started.\n\n", old_cluster.pgdata);