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