4 * controldata functions
6 * Copyright (c) 2010-2018, 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 char *lc_collate = NULL;
62 char *lc_ctype = NULL;
63 char *lc_monetary = NULL;
64 char *lc_numeric = NULL;
67 char *language = NULL;
69 char *lc_messages = NULL;
77 * Because we test the pg_resetwal output as strings, it has to be in
78 * English. Copied from pg_regress.c.
80 if (getenv("LC_COLLATE"))
81 lc_collate = pg_strdup(getenv("LC_COLLATE"));
82 if (getenv("LC_CTYPE"))
83 lc_ctype = pg_strdup(getenv("LC_CTYPE"));
84 if (getenv("LC_MONETARY"))
85 lc_monetary = pg_strdup(getenv("LC_MONETARY"));
86 if (getenv("LC_NUMERIC"))
87 lc_numeric = pg_strdup(getenv("LC_NUMERIC"));
88 if (getenv("LC_TIME"))
89 lc_time = pg_strdup(getenv("LC_TIME"));
91 lang = pg_strdup(getenv("LANG"));
92 if (getenv("LANGUAGE"))
93 language = pg_strdup(getenv("LANGUAGE"));
95 lc_all = pg_strdup(getenv("LC_ALL"));
96 if (getenv("LC_MESSAGES"))
97 lc_messages = pg_strdup(getenv("LC_MESSAGES"));
99 pg_putenv("LC_COLLATE", NULL);
100 pg_putenv("LC_CTYPE", NULL);
101 pg_putenv("LC_MONETARY", NULL);
102 pg_putenv("LC_NUMERIC", NULL);
103 pg_putenv("LC_TIME", NULL);
108 /* On Windows the default locale cannot be English, so force it */
111 pg_putenv("LANGUAGE", NULL);
112 pg_putenv("LC_ALL", NULL);
113 pg_putenv("LC_MESSAGES", "C");
115 /* pg_resetxlog has been renamed to pg_resetwal in version 10 */
116 if (GET_MAJOR_VERSION(cluster->bin_version) < 1000)
117 resetwal_bin = "pg_resetxlog\" -n";
119 resetwal_bin = "pg_resetwal\" -n";
120 snprintf(cmd, sizeof(cmd), "\"%s/%s \"%s\"",
122 live_check ? "pg_controldata\"" : resetwal_bin,
127 if ((output = popen(cmd, "r")) == NULL)
128 pg_fatal("could not get control data using %s: %s\n",
129 cmd, strerror(errno));
132 if (GET_MAJOR_VERSION(cluster->major_version) <= 902)
134 cluster->controldata.data_checksum_version = 0;
135 got_data_checksum_version = true;
138 /* we have the result of cmd in "output". so parse it line by line now */
139 while (fgets(bufin, sizeof(bufin), output))
141 pg_log(PG_VERBOSE, "%s", bufin);
143 if ((p = strstr(bufin, "pg_control version number:")) != NULL)
147 if (p == NULL || strlen(p) <= 1)
148 pg_fatal("%d: pg_resetwal problem\n", __LINE__);
150 p++; /* remove ':' char */
151 cluster->controldata.ctrl_ver = str2uint(p);
153 else if ((p = strstr(bufin, "Catalog version number:")) != NULL)
157 if (p == NULL || strlen(p) <= 1)
158 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
160 p++; /* remove ':' char */
161 cluster->controldata.cat_ver = str2uint(p);
163 else if ((p = strstr(bufin, "Latest checkpoint's TimeLineID:")) != NULL)
167 if (p == NULL || strlen(p) <= 1)
168 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
170 p++; /* remove ':' char */
174 else if ((p = strstr(bufin, "First log file ID after reset:")) != NULL)
178 if (p == NULL || strlen(p) <= 1)
179 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
181 p++; /* remove ':' char */
185 else if ((p = strstr(bufin, "First log file segment after reset:")) != NULL)
189 if (p == NULL || strlen(p) <= 1)
190 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
192 p++; /* remove ':' char */
196 else if ((p = strstr(bufin, "Latest checkpoint's NextXID:")) != NULL)
200 if (p == NULL || strlen(p) <= 1)
201 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
203 p++; /* remove ':' char */
204 cluster->controldata.chkpnt_nxtepoch = str2uint(p);
207 * Delimiter changed from '/' to ':' in 9.6. We don't test for
208 * the catalog version of the change because the catalog version
209 * is pulled from pg_controldata too, and it isn't worth adding an
210 * order dependency for this --- we just check the string.
212 if (strchr(p, '/') != NULL)
214 else if (GET_MAJOR_VERSION(cluster->major_version) >= 906)
219 if (p == NULL || strlen(p) <= 1)
220 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
222 p++; /* remove '/' or ':' char */
223 cluster->controldata.chkpnt_nxtxid = str2uint(p);
226 else if ((p = strstr(bufin, "Latest checkpoint's NextOID:")) != NULL)
230 if (p == NULL || strlen(p) <= 1)
231 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
233 p++; /* remove ':' char */
234 cluster->controldata.chkpnt_nxtoid = str2uint(p);
237 else if ((p = strstr(bufin, "Latest checkpoint's NextMultiXactId:")) != NULL)
241 if (p == NULL || strlen(p) <= 1)
242 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
244 p++; /* remove ':' char */
245 cluster->controldata.chkpnt_nxtmulti = str2uint(p);
248 else if ((p = strstr(bufin, "Latest checkpoint's oldestMultiXid:")) != NULL)
252 if (p == NULL || strlen(p) <= 1)
253 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
255 p++; /* remove ':' char */
256 cluster->controldata.chkpnt_oldstMulti = str2uint(p);
257 got_oldestmulti = true;
259 else if ((p = strstr(bufin, "Latest checkpoint's NextMultiOffset:")) != NULL)
263 if (p == NULL || strlen(p) <= 1)
264 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
266 p++; /* remove ':' char */
267 cluster->controldata.chkpnt_nxtmxoff = str2uint(p);
270 else if ((p = strstr(bufin, "First log segment after reset:")) != NULL)
272 /* Skip the colon and any whitespace after it */
274 if (p == NULL || strlen(p) <= 1)
275 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
276 p = strpbrk(p, "01234567890ABCDEF");
277 if (p == NULL || strlen(p) <= 1)
278 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
280 /* Make sure it looks like a valid WAL file name */
281 if (strspn(p, "0123456789ABCDEF") != 24)
282 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
284 strlcpy(cluster->controldata.nextxlogfile, p, 25);
285 got_nextxlogfile = true;
287 else if ((p = strstr(bufin, "Float8 argument passing:")) != NULL)
291 if (p == NULL || strlen(p) <= 1)
292 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
294 p++; /* remove ':' char */
295 /* used later for contrib check */
296 cluster->controldata.float8_pass_by_value = strstr(p, "by value") != NULL;
297 got_float8_pass_by_value = true;
299 else if ((p = strstr(bufin, "Maximum data alignment:")) != NULL)
303 if (p == NULL || strlen(p) <= 1)
304 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
306 p++; /* remove ':' char */
307 cluster->controldata.align = str2uint(p);
310 else if ((p = strstr(bufin, "Database block size:")) != NULL)
314 if (p == NULL || strlen(p) <= 1)
315 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
317 p++; /* remove ':' char */
318 cluster->controldata.blocksz = str2uint(p);
321 else if ((p = strstr(bufin, "Blocks per segment of large relation:")) != NULL)
325 if (p == NULL || strlen(p) <= 1)
326 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
328 p++; /* remove ':' char */
329 cluster->controldata.largesz = str2uint(p);
332 else if ((p = strstr(bufin, "WAL block size:")) != NULL)
336 if (p == NULL || strlen(p) <= 1)
337 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
339 p++; /* remove ':' char */
340 cluster->controldata.walsz = str2uint(p);
343 else if ((p = strstr(bufin, "Bytes per WAL segment:")) != NULL)
347 if (p == NULL || strlen(p) <= 1)
348 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
350 p++; /* remove ':' char */
351 cluster->controldata.walseg = str2uint(p);
354 else if ((p = strstr(bufin, "Maximum length of identifiers:")) != NULL)
358 if (p == NULL || strlen(p) <= 1)
359 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
361 p++; /* remove ':' char */
362 cluster->controldata.ident = str2uint(p);
365 else if ((p = strstr(bufin, "Maximum columns in an index:")) != NULL)
369 if (p == NULL || strlen(p) <= 1)
370 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
372 p++; /* remove ':' char */
373 cluster->controldata.index = str2uint(p);
376 else if ((p = strstr(bufin, "Maximum size of a TOAST chunk:")) != NULL)
380 if (p == NULL || strlen(p) <= 1)
381 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
383 p++; /* remove ':' char */
384 cluster->controldata.toast = str2uint(p);
387 else if ((p = strstr(bufin, "Size of a large-object chunk:")) != NULL)
391 if (p == NULL || strlen(p) <= 1)
392 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
394 p++; /* remove ':' char */
395 cluster->controldata.large_object = str2uint(p);
396 got_large_object = true;
398 else if ((p = strstr(bufin, "Date/time type storage:")) != NULL)
402 if (p == NULL || strlen(p) <= 1)
403 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
405 p++; /* remove ':' char */
406 cluster->controldata.date_is_int = strstr(p, "64-bit integers") != NULL;
407 got_date_is_int = true;
409 else if ((p = strstr(bufin, "checksum")) != NULL)
413 if (p == NULL || strlen(p) <= 1)
414 pg_fatal("%d: controldata retrieval problem\n", __LINE__);
416 p++; /* remove ':' char */
417 /* used later for contrib check */
418 cluster->controldata.data_checksum_version = str2uint(p);
419 got_data_checksum_version = true;
426 * Restore environment variables
428 pg_putenv("LC_COLLATE", lc_collate);
429 pg_putenv("LC_CTYPE", lc_ctype);
430 pg_putenv("LC_MONETARY", lc_monetary);
431 pg_putenv("LC_NUMERIC", lc_numeric);
432 pg_putenv("LC_TIME", lc_time);
433 pg_putenv("LANG", lang);
434 pg_putenv("LANGUAGE", language);
435 pg_putenv("LC_ALL", lc_all);
436 pg_putenv("LC_MESSAGES", lc_messages);
440 pg_free(lc_monetary);
446 pg_free(lc_messages);
449 * Before 9.3, pg_resetwal reported the xlogid and segno of the first log
450 * file after reset as separate lines. Starting with 9.3, it reports the
451 * WAL file name. If the old cluster is older than 9.3, we construct the
452 * WAL file name from the xlogid and segno.
454 if (GET_MAJOR_VERSION(cluster->major_version) <= 902)
456 if (got_tli && got_log_id && got_log_seg)
458 snprintf(cluster->controldata.nextxlogfile, 25, "%08X%08X%08X",
460 got_nextxlogfile = true;
464 /* verify that we got all the mandatory pg_control data */
465 if (!got_xid || !got_oid ||
468 cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER) ||
469 !got_mxoff || (!live_check && !got_nextxlogfile) ||
470 !got_float8_pass_by_value || !got_align || !got_blocksz ||
471 !got_largesz || !got_walsz || !got_walseg || !got_ident ||
472 !got_index || !got_toast ||
473 (!got_large_object &&
474 cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER) ||
475 !got_date_is_int || !got_data_checksum_version)
477 if (cluster == &old_cluster)
479 "The source cluster lacks some required control information:\n");
482 "The target cluster lacks some required control information:\n");
485 pg_log(PG_REPORT, " checkpoint next XID\n");
488 pg_log(PG_REPORT, " latest checkpoint next OID\n");
491 pg_log(PG_REPORT, " latest checkpoint next MultiXactId\n");
493 if (!got_oldestmulti &&
494 cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
495 pg_log(PG_REPORT, " latest checkpoint oldest MultiXactId\n");
498 pg_log(PG_REPORT, " latest checkpoint next MultiXactOffset\n");
500 if (!live_check && !got_nextxlogfile)
501 pg_log(PG_REPORT, " first WAL segment after reset\n");
503 if (!got_float8_pass_by_value)
504 pg_log(PG_REPORT, " float8 argument passing method\n");
507 pg_log(PG_REPORT, " maximum alignment\n");
510 pg_log(PG_REPORT, " block size\n");
513 pg_log(PG_REPORT, " large relation segment size\n");
516 pg_log(PG_REPORT, " WAL block size\n");
519 pg_log(PG_REPORT, " WAL segment size\n");
522 pg_log(PG_REPORT, " maximum identifier length\n");
525 pg_log(PG_REPORT, " maximum number of indexed columns\n");
528 pg_log(PG_REPORT, " maximum TOAST chunk size\n");
530 if (!got_large_object &&
531 cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER)
532 pg_log(PG_REPORT, " large-object chunk size\n");
534 if (!got_date_is_int)
535 pg_log(PG_REPORT, " dates/times are integers?\n");
537 /* value added in Postgres 9.3 */
538 if (!got_data_checksum_version)
539 pg_log(PG_REPORT, " data checksum version\n");
541 pg_fatal("Cannot continue without required control information, terminating\n");
547 * check_control_data()
549 * check to make sure the control data settings are compatible
552 check_control_data(ControlData *oldctrl,
553 ControlData *newctrl)
555 if (oldctrl->align == 0 || oldctrl->align != newctrl->align)
556 pg_fatal("old and new pg_controldata alignments are invalid or do not match\n"
557 "Likely one cluster is a 32-bit install, the other 64-bit\n");
559 if (oldctrl->blocksz == 0 || oldctrl->blocksz != newctrl->blocksz)
560 pg_fatal("old and new pg_controldata block sizes are invalid or do not match\n");
562 if (oldctrl->largesz == 0 || oldctrl->largesz != newctrl->largesz)
563 pg_fatal("old and new pg_controldata maximum relation segment sizes are invalid or do not match\n");
565 if (oldctrl->walsz == 0 || oldctrl->walsz != newctrl->walsz)
566 pg_fatal("old and new pg_controldata WAL block sizes are invalid or do not match\n");
568 if (oldctrl->walseg == 0 || oldctrl->walseg != newctrl->walseg)
569 pg_fatal("old and new pg_controldata WAL segment sizes are invalid or do not match\n");
571 if (oldctrl->ident == 0 || oldctrl->ident != newctrl->ident)
572 pg_fatal("old and new pg_controldata maximum identifier lengths are invalid or do not match\n");
574 if (oldctrl->index == 0 || oldctrl->index != newctrl->index)
575 pg_fatal("old and new pg_controldata maximum indexed columns are invalid or do not match\n");
577 if (oldctrl->toast == 0 || oldctrl->toast != newctrl->toast)
578 pg_fatal("old and new pg_controldata maximum TOAST chunk sizes are invalid or do not match\n");
580 /* large_object added in 9.5, so it might not exist in the old cluster */
581 if (oldctrl->large_object != 0 &&
582 oldctrl->large_object != newctrl->large_object)
583 pg_fatal("old and new pg_controldata large-object chunk sizes are invalid or do not match\n");
585 if (oldctrl->date_is_int != newctrl->date_is_int)
586 pg_fatal("old and new pg_controldata date/time storage types do not match\n");
589 * float8_pass_by_value does not need to match, but is used in
590 * check_for_isn_and_int8_passing_mismatch().
594 * We might eventually allow upgrades from checksum to no-checksum
597 if (oldctrl->data_checksum_version == 0 &&
598 newctrl->data_checksum_version != 0)
599 pg_fatal("old cluster does not use data checksums but the new one does\n");
600 else if (oldctrl->data_checksum_version != 0 &&
601 newctrl->data_checksum_version == 0)
602 pg_fatal("old cluster uses data checksums but the new one does not\n");
603 else if (oldctrl->data_checksum_version != newctrl->data_checksum_version)
604 pg_fatal("old and new cluster pg_controldata checksum versions do not match\n");
609 disable_old_cluster(void)
611 char old_path[MAXPGPATH],
614 /* rename pg_control so old server cannot be accidentally started */
615 prep_status("Adding \".old\" suffix to old global/pg_control");
617 snprintf(old_path, sizeof(old_path), "%s/global/pg_control", old_cluster.pgdata);
618 snprintf(new_path, sizeof(new_path), "%s/global/pg_control.old", old_cluster.pgdata);
619 if (pg_mv_file(old_path, new_path) != 0)
620 pg_fatal("Unable to rename %s to %s.\n", old_path, new_path);
623 pg_log(PG_REPORT, "\n"
624 "If you want to start the old cluster, you will need to remove\n"
625 "the \".old\" suffix from %s/global/pg_control.old.\n"
626 "Because \"link\" mode was used, the old cluster cannot be safely\n"
627 "started once the new cluster has been started.\n\n", old_cluster.pgdata);