PostgreSQL Source Code  git master
pg_upgrade.c
Go to the documentation of this file.
1 /*
2  * pg_upgrade.c
3  *
4  * main source file
5  *
6  * Copyright (c) 2010-2023, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/pg_upgrade.c
8  */
9 
10 /*
11  * To simplify the upgrade process, we force certain system values to be
12  * identical between old and new clusters:
13  *
14  * We control all assignments of pg_class.oid (and relfilenode) so toast
15  * oids are the same between old and new clusters. This is important
16  * because toast oids are stored as toast pointers in user tables.
17  *
18  * While pg_class.oid and pg_class.relfilenode are initially the same in a
19  * cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
20  * control assignments of pg_class.relfilenode because we want the filenames
21  * to match between the old and new cluster.
22  *
23  * We control assignment of pg_tablespace.oid because we want the oid to match
24  * between the old and new cluster.
25  *
26  * We control all assignments of pg_type.oid because these oids are stored
27  * in user composite type values.
28  *
29  * We control all assignments of pg_enum.oid because these oids are stored
30  * in user tables as enum values.
31  *
32  * We control all assignments of pg_authid.oid for historical reasons (the
33  * oids used to be stored in pg_largeobject_metadata, which is now copied via
34  * SQL commands), that might change at some point in the future.
35  */
36 
37 
38 
39 #include "postgres_fe.h"
40 
41 #include <time.h>
42 
43 #ifdef HAVE_LANGINFO_H
44 #include <langinfo.h>
45 #endif
46 
47 #include "catalog/pg_class_d.h"
48 #include "common/file_perm.h"
49 #include "common/logging.h"
51 #include "fe_utils/string_utils.h"
52 #include "pg_upgrade.h"
53 
54 static void set_locale_and_encoding(void);
55 static void prepare_new_cluster(void);
56 static void prepare_new_globals(void);
57 static void create_new_objects(void);
58 static void copy_xact_xlog_xid(void);
59 static void set_frozenxids(bool minmxid_only);
60 static void make_outputdirs(char *pgdata);
61 static void setup(char *argv0, bool *live_check);
62 
66 
67 char *output_files[] = {
69 #ifdef WIN32
70  /* unique file for pg_ctl start */
72 #endif
75  NULL
76 };
77 
78 
79 int
80 main(int argc, char **argv)
81 {
82  char *deletion_script_file_name = NULL;
83  bool live_check = false;
84 
85  /*
86  * pg_upgrade doesn't currently use common/logging.c, but initialize it
87  * anyway because we might call common code that does.
88  */
89  pg_logging_init(argv[0]);
90  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
91 
92  /* Set default restrictive mask until new cluster permissions are read */
93  umask(PG_MODE_MASK_OWNER);
94 
95  parseCommandLine(argc, argv);
96 
98 
101 
102  /*
103  * Set mask based on PGDATA permissions, needed for the creation of the
104  * output directories with correct permissions.
105  */
107  pg_fatal("could not read permissions of directory \"%s\": %s",
108  new_cluster.pgdata, strerror(errno));
109 
110  umask(pg_mode_mask);
111 
112  /*
113  * This needs to happen after adjusting the data directory of the new
114  * cluster in adjust_data_dir().
115  */
117 
118  setup(argv[0], &live_check);
119 
120  output_check_banner(live_check);
121 
123 
124  get_sock_dir(&old_cluster, live_check);
125  get_sock_dir(&new_cluster, false);
126 
127  check_cluster_compatibility(live_check);
128 
129  check_and_dump_old_cluster(live_check);
130 
131 
132  /* -- NEW -- */
134 
137 
139  "\n"
140  "Performing Upgrade\n"
141  "------------------");
142 
144 
146 
147  stop_postmaster(false);
148 
149  /*
150  * Destructive Changes to New Cluster
151  */
152 
154 
155  /* New now using xids of the old system */
156 
157  /* -- NEW -- */
159 
161 
163 
164  stop_postmaster(false);
165 
166  /*
167  * Most failures happen in create_new_objects(), which has completed at
168  * this point. We do this here because it is just before linking, which
169  * will link the old and new cluster data files, preventing the old
170  * cluster from being safely started once the new cluster is started.
171  */
174 
177 
178  /*
179  * Assuming OIDs are only used in system tables, there is no need to
180  * restore the OID counter because we have not transferred any OIDs from
181  * the old system, but we do it anyway just in case. We do it late here
182  * because there is no need to have the schema load use new oids.
183  */
184  prep_status("Setting next OID for new cluster");
185  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
186  "\"%s/pg_resetwal\" -o %u \"%s\"",
189  check_ok();
190 
191  if (user_opts.do_sync)
192  {
193  prep_status("Sync data directory to disk");
194  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
195  "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
197  check_ok();
198  }
199 
200  create_script_for_old_cluster_deletion(&deletion_script_file_name);
201 
203 
205  "\n"
206  "Upgrade Complete\n"
207  "----------------");
208 
209  output_completion_banner(deletion_script_file_name);
210 
211  pg_free(deletion_script_file_name);
212 
214 
215  return 0;
216 }
217 
218 /*
219  * Create and assign proper permissions to the set of output directories
220  * used to store any data generated internally, filling in log_opts in
221  * the process.
222  */
223 static void
224 make_outputdirs(char *pgdata)
225 {
226  FILE *fp;
227  char **filename;
228  time_t run_time = time(NULL);
229  char filename_path[MAXPGPATH];
230  char timebuf[128];
231  struct timeval time;
232  time_t tt;
233  int len;
234 
235  log_opts.rootdir = (char *) pg_malloc0(MAXPGPATH);
236  len = snprintf(log_opts.rootdir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
237  if (len >= MAXPGPATH)
238  pg_fatal("directory path for new cluster is too long");
239 
240  /* BASE_OUTPUTDIR/$timestamp/ */
241  gettimeofday(&time, NULL);
242  tt = (time_t) time.tv_sec;
243  strftime(timebuf, sizeof(timebuf), "%Y%m%dT%H%M%S", localtime(&tt));
244  /* append milliseconds */
245  snprintf(timebuf + strlen(timebuf), sizeof(timebuf) - strlen(timebuf),
246  ".%03d", (int) (time.tv_usec / 1000));
247  log_opts.basedir = (char *) pg_malloc0(MAXPGPATH);
249  timebuf);
250  if (len >= MAXPGPATH)
251  pg_fatal("directory path for new cluster is too long");
252 
253  /* BASE_OUTPUTDIR/$timestamp/dump/ */
254  log_opts.dumpdir = (char *) pg_malloc0(MAXPGPATH);
256  timebuf, DUMP_OUTPUTDIR);
257  if (len >= MAXPGPATH)
258  pg_fatal("directory path for new cluster is too long");
259 
260  /* BASE_OUTPUTDIR/$timestamp/log/ */
261  log_opts.logdir = (char *) pg_malloc0(MAXPGPATH);
263  timebuf, LOG_OUTPUTDIR);
264  if (len >= MAXPGPATH)
265  pg_fatal("directory path for new cluster is too long");
266 
267  /*
268  * Ignore the error case where the root path exists, as it is kept the
269  * same across runs.
270  */
271  if (mkdir(log_opts.rootdir, pg_dir_create_mode) < 0 && errno != EEXIST)
272  pg_fatal("could not create directory \"%s\": %m", log_opts.rootdir);
274  pg_fatal("could not create directory \"%s\": %m", log_opts.basedir);
276  pg_fatal("could not create directory \"%s\": %m", log_opts.dumpdir);
278  pg_fatal("could not create directory \"%s\": %m", log_opts.logdir);
279 
280  len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
282  if (len >= sizeof(filename_path))
283  pg_fatal("directory path for new cluster is too long");
284 
285  if ((log_opts.internal = fopen_priv(filename_path, "a")) == NULL)
286  pg_fatal("could not open log file \"%s\": %m", filename_path);
287 
288  /* label start of upgrade in logfiles */
289  for (filename = output_files; *filename != NULL; filename++)
290  {
291  len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
293  if (len >= sizeof(filename_path))
294  pg_fatal("directory path for new cluster is too long");
295  if ((fp = fopen_priv(filename_path, "a")) == NULL)
296  pg_fatal("could not write to log file \"%s\": %m", filename_path);
297 
298  fprintf(fp,
299  "-----------------------------------------------------------------\n"
300  " pg_upgrade run on %s"
301  "-----------------------------------------------------------------\n\n",
302  ctime(&run_time));
303  fclose(fp);
304  }
305 }
306 
307 
308 static void
309 setup(char *argv0, bool *live_check)
310 {
311  /*
312  * make sure the user has a clean environment, otherwise, we may confuse
313  * libpq when we connect to one (or both) of the servers.
314  */
316 
317  /*
318  * In case the user hasn't specified the directory for the new binaries
319  * with -B, default to using the path of the currently executed pg_upgrade
320  * binary.
321  */
322  if (!new_cluster.bindir)
323  {
324  char exec_path[MAXPGPATH];
325 
326  if (find_my_exec(argv0, exec_path) < 0)
327  pg_fatal("%s: could not find own program executable", argv0);
328  /* Trim off program name and keep just path */
329  *last_dir_separator(exec_path) = '\0';
332  }
333 
335 
336  /* no postmasters should be running, except for a live check */
338  {
339  /*
340  * If we have a postmaster.pid file, try to start the server. If it
341  * starts, the pid file was stale, so stop the server. If it doesn't
342  * start, assume the server is running. If the pid file is left over
343  * from a server crash, this also allows any committed transactions
344  * stored in the WAL to be replayed so they are not lost, because WAL
345  * files are not transferred from old to new servers. We later check
346  * for a clean shutdown.
347  */
348  if (start_postmaster(&old_cluster, false))
349  stop_postmaster(false);
350  else
351  {
352  if (!user_opts.check)
353  pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
354  "Please shutdown that postmaster and try again.");
355  else
356  *live_check = true;
357  }
358  }
359 
360  /* same goes for the new postmaster */
362  {
363  if (start_postmaster(&new_cluster, false))
364  stop_postmaster(false);
365  else
366  pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
367  "Please shutdown that postmaster and try again.");
368  }
369 }
370 
371 
372 /*
373  * Copy locale and encoding information into the new cluster's template0.
374  *
375  * We need to copy the encoding, datlocprovider, datcollate, datctype, and
376  * daticulocale. We don't need datcollversion because that's never set for
377  * template0.
378  */
379 static void
381 {
382  PGconn *conn_new_template1;
383  char *datcollate_literal;
384  char *datctype_literal;
385  char *daticulocale_literal = NULL;
387 
388  prep_status("Setting locale and encoding for new cluster");
389 
390  /* escape literals with respect to new cluster */
391  conn_new_template1 = connectToServer(&new_cluster, "template1");
392 
393  datcollate_literal = PQescapeLiteral(conn_new_template1,
394  locale->db_collate,
395  strlen(locale->db_collate));
396  datctype_literal = PQescapeLiteral(conn_new_template1,
397  locale->db_ctype,
398  strlen(locale->db_ctype));
399  if (locale->db_iculocale)
400  daticulocale_literal = PQescapeLiteral(conn_new_template1,
401  locale->db_iculocale,
402  strlen(locale->db_iculocale));
403  else
404  daticulocale_literal = pg_strdup("NULL");
405 
406  /* update template0 in new cluster */
408  PQclear(executeQueryOrDie(conn_new_template1,
409  "UPDATE pg_catalog.pg_database "
410  " SET encoding = %d, "
411  " datlocprovider = '%c', "
412  " datcollate = %s, "
413  " datctype = %s, "
414  " daticulocale = %s "
415  " WHERE datname = 'template0' ",
416  locale->db_encoding,
417  locale->db_collprovider,
418  datcollate_literal,
419  datctype_literal,
420  daticulocale_literal));
421  else
422  PQclear(executeQueryOrDie(conn_new_template1,
423  "UPDATE pg_catalog.pg_database "
424  " SET encoding = %d, "
425  " datcollate = %s, "
426  " datctype = %s "
427  " WHERE datname = 'template0' ",
428  locale->db_encoding,
429  datcollate_literal,
430  datctype_literal));
431 
432  PQfreemem(datcollate_literal);
433  PQfreemem(datctype_literal);
434  PQfreemem(daticulocale_literal);
435 
436  PQfinish(conn_new_template1);
437 
438  check_ok();
439 }
440 
441 
442 static void
444 {
445  /*
446  * It would make more sense to freeze after loading the schema, but that
447  * would cause us to lose the frozenxids restored by the load. We use
448  * --analyze so autovacuum doesn't update statistics later
449  */
450  prep_status("Analyzing all rows in the new cluster");
451  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
452  "\"%s/vacuumdb\" %s --all --analyze %s",
454  log_opts.verbose ? "--verbose" : "");
455  check_ok();
456 
457  /*
458  * We do freeze after analyze so pg_statistic is also frozen. template0 is
459  * not frozen here, but data rows were frozen by initdb, and we set its
460  * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
461  * counter later.
462  */
463  prep_status("Freezing all rows in the new cluster");
464  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
465  "\"%s/vacuumdb\" %s --all --freeze %s",
467  log_opts.verbose ? "--verbose" : "");
468  check_ok();
469 }
470 
471 
472 static void
474 {
475  /*
476  * Before we restore anything, set frozenxids of initdb-created tables.
477  */
478  set_frozenxids(false);
479 
480  /*
481  * Now restore global objects (roles and tablespaces).
482  */
483  prep_status("Restoring global objects in the new cluster");
484 
485  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
486  "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s/%s\"",
490  check_ok();
491 }
492 
493 
494 static void
496 {
497  int dbnum;
498 
499  prep_status_progress("Restoring database schemas in the new cluster");
500 
501  /*
502  * We cannot process the template1 database concurrently with others,
503  * because when it's transiently dropped, connection attempts would fail.
504  * So handle it in a separate non-parallelized pass.
505  */
506  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
507  {
508  char sql_file_name[MAXPGPATH],
509  log_file_name[MAXPGPATH];
510  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
511  const char *create_opts;
512 
513  /* Process only template1 in this pass */
514  if (strcmp(old_db->db_name, "template1") != 0)
515  continue;
516 
517  pg_log(PG_STATUS, "%s", old_db->db_name);
518  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
519  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
520 
521  /*
522  * template1 database will already exist in the target installation,
523  * so tell pg_restore to drop and recreate it; otherwise we would fail
524  * to propagate its database-level properties.
525  */
526  create_opts = "--clean --create";
527 
528  exec_prog(log_file_name,
529  NULL,
530  true,
531  true,
532  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
533  "--dbname postgres \"%s/%s\"",
536  create_opts,
538  sql_file_name);
539 
540  break; /* done once we've processed template1 */
541  }
542 
543  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
544  {
545  char sql_file_name[MAXPGPATH],
546  log_file_name[MAXPGPATH];
547  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
548  const char *create_opts;
549 
550  /* Skip template1 in this pass */
551  if (strcmp(old_db->db_name, "template1") == 0)
552  continue;
553 
554  pg_log(PG_STATUS, "%s", old_db->db_name);
555  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
556  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
557 
558  /*
559  * postgres database will already exist in the target installation, so
560  * tell pg_restore to drop and recreate it; otherwise we would fail to
561  * propagate its database-level properties.
562  */
563  if (strcmp(old_db->db_name, "postgres") == 0)
564  create_opts = "--clean --create";
565  else
566  create_opts = "--create";
567 
568  parallel_exec_prog(log_file_name,
569  NULL,
570  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
571  "--dbname template1 \"%s/%s\"",
574  create_opts,
576  sql_file_name);
577  }
578 
579  /* reap all children */
580  while (reap_child(true) == true)
581  ;
582 
584  check_ok();
585 
586  /*
587  * We don't have minmxids for databases or relations in pre-9.3 clusters,
588  * so set those after we have restored the schema.
589  */
591  set_frozenxids(true);
592 
593  /* update new_cluster info now that we have objects in the databases */
595 }
596 
597 /*
598  * Delete the given subdirectory contents from the new cluster
599  */
600 static void
601 remove_new_subdir(const char *subdir, bool rmtopdir)
602 {
603  char new_path[MAXPGPATH];
604 
605  prep_status("Deleting files from new %s", subdir);
606 
607  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
608  if (!rmtree(new_path, rmtopdir))
609  pg_fatal("could not delete directory \"%s\"", new_path);
610 
611  check_ok();
612 }
613 
614 /*
615  * Copy the files from the old cluster into it
616  */
617 static void
618 copy_subdir_files(const char *old_subdir, const char *new_subdir)
619 {
620  char old_path[MAXPGPATH];
621  char new_path[MAXPGPATH];
622 
623  remove_new_subdir(new_subdir, true);
624 
625  snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
626  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
627 
628  prep_status("Copying old %s to new server", old_subdir);
629 
630  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
631 #ifndef WIN32
632  "cp -Rf \"%s\" \"%s\"",
633 #else
634  /* flags: everything, no confirm, quiet, overwrite read-only */
635  "xcopy /e /y /q /r \"%s\" \"%s\\\"",
636 #endif
637  old_path, new_path);
638 
639  check_ok();
640 }
641 
642 static void
644 {
645  /*
646  * Copy old commit logs to new data dir. pg_clog has been renamed to
647  * pg_xact in post-10 clusters.
648  */
650  "pg_clog" : "pg_xact",
652  "pg_clog" : "pg_xact");
653 
654  prep_status("Setting oldest XID for new cluster");
655  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
656  "\"%s/pg_resetwal\" -f -u %u \"%s\"",
659  check_ok();
660 
661  /* set the next transaction id and epoch of the new cluster */
662  prep_status("Setting next transaction ID and epoch for new cluster");
663  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
664  "\"%s/pg_resetwal\" -f -x %u \"%s\"",
667  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
668  "\"%s/pg_resetwal\" -f -e %u \"%s\"",
671  /* must reset commit timestamp limits also */
672  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
673  "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
678  check_ok();
679 
680  /*
681  * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
682  * (see pg_upgrade.h) and the new server is after, then we don't copy
683  * pg_multixact files, but we need to reset pg_control so that the new
684  * server doesn't attempt to read multis older than the cutoff value.
685  */
688  {
689  copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
690  copy_subdir_files("pg_multixact/members", "pg_multixact/members");
691 
692  prep_status("Setting next multixact ID and offset for new cluster");
693 
694  /*
695  * we preserve all files and contents, so we must preserve both "next"
696  * counters here and the oldest multi present on system.
697  */
698  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
699  "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
705  check_ok();
706  }
708  {
709  /*
710  * Remove offsets/0000 file created by initdb that no longer matches
711  * the new multi-xid value. "members" starts at zero so no need to
712  * remove it.
713  */
714  remove_new_subdir("pg_multixact/offsets", false);
715 
716  prep_status("Setting oldest multixact ID in new cluster");
717 
718  /*
719  * We don't preserve files in this case, but it's important that the
720  * oldest multi is set to the latest value used by the old system, so
721  * that multixact.c returns the empty set for multis that might be
722  * present on disk. We set next multi to the value following that; it
723  * might end up wrapped around (i.e. 0) if the old cluster had
724  * next=MaxMultiXactId, but multixact.c can cope with that just fine.
725  */
726  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
727  "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
732  check_ok();
733  }
734 
735  /* now reset the wal archives in the new cluster */
736  prep_status("Resetting WAL archives");
737  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
738  /* use timeline 1 to match controldata and no WAL history file */
739  "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
742  check_ok();
743 }
744 
745 
746 /*
747  * set_frozenxids()
748  *
749  * This is called on the new cluster before we restore anything, with
750  * minmxid_only = false. Its purpose is to ensure that all initdb-created
751  * vacuumable tables have relfrozenxid/relminmxid matching the old cluster's
752  * xid/mxid counters. We also initialize the datfrozenxid/datminmxid of the
753  * built-in databases to match.
754  *
755  * As we create user tables later, their relfrozenxid/relminmxid fields will
756  * be restored properly by the binary-upgrade restore script. Likewise for
757  * user-database datfrozenxid/datminmxid. However, if we're upgrading from a
758  * pre-9.3 database, which does not store per-table or per-DB minmxid, then
759  * the relminmxid/datminmxid values filled in by the restore script will just
760  * be zeroes.
761  *
762  * Hence, with a pre-9.3 source database, a second call occurs after
763  * everything is restored, with minmxid_only = true. This pass will
764  * initialize all tables and databases, both those made by initdb and user
765  * objects, with the desired minmxid value. frozenxid values are left alone.
766  */
767 static void
768 set_frozenxids(bool minmxid_only)
769 {
770  int dbnum;
771  PGconn *conn,
772  *conn_template1;
773  PGresult *dbres;
774  int ntups;
775  int i_datname;
776  int i_datallowconn;
777 
778  if (!minmxid_only)
779  prep_status("Setting frozenxid and minmxid counters in new cluster");
780  else
781  prep_status("Setting minmxid counter in new cluster");
782 
783  conn_template1 = connectToServer(&new_cluster, "template1");
784 
785  if (!minmxid_only)
786  /* set pg_database.datfrozenxid */
787  PQclear(executeQueryOrDie(conn_template1,
788  "UPDATE pg_catalog.pg_database "
789  "SET datfrozenxid = '%u'",
791 
792  /* set pg_database.datminmxid */
793  PQclear(executeQueryOrDie(conn_template1,
794  "UPDATE pg_catalog.pg_database "
795  "SET datminmxid = '%u'",
797 
798  /* get database names */
799  dbres = executeQueryOrDie(conn_template1,
800  "SELECT datname, datallowconn "
801  "FROM pg_catalog.pg_database");
802 
803  i_datname = PQfnumber(dbres, "datname");
804  i_datallowconn = PQfnumber(dbres, "datallowconn");
805 
806  ntups = PQntuples(dbres);
807  for (dbnum = 0; dbnum < ntups; dbnum++)
808  {
809  char *datname = PQgetvalue(dbres, dbnum, i_datname);
810  char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
811 
812  /*
813  * We must update databases where datallowconn = false, e.g.
814  * template0, because autovacuum increments their datfrozenxids,
815  * relfrozenxids, and relminmxid even if autovacuum is turned off, and
816  * even though all the data rows are already frozen. To enable this,
817  * we temporarily change datallowconn.
818  */
819  if (strcmp(datallowconn, "f") == 0)
820  PQclear(executeQueryOrDie(conn_template1,
821  "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
823 
825 
826  if (!minmxid_only)
827  /* set pg_class.relfrozenxid */
829  "UPDATE pg_catalog.pg_class "
830  "SET relfrozenxid = '%u' "
831  /* only heap, materialized view, and TOAST are vacuumed */
832  "WHERE relkind IN ("
833  CppAsString2(RELKIND_RELATION) ", "
834  CppAsString2(RELKIND_MATVIEW) ", "
835  CppAsString2(RELKIND_TOASTVALUE) ")",
837 
838  /* set pg_class.relminmxid */
840  "UPDATE pg_catalog.pg_class "
841  "SET relminmxid = '%u' "
842  /* only heap, materialized view, and TOAST are vacuumed */
843  "WHERE relkind IN ("
844  CppAsString2(RELKIND_RELATION) ", "
845  CppAsString2(RELKIND_MATVIEW) ", "
846  CppAsString2(RELKIND_TOASTVALUE) ")",
848  PQfinish(conn);
849 
850  /* Reset datallowconn flag */
851  if (strcmp(datallowconn, "f") == 0)
852  PQclear(executeQueryOrDie(conn_template1,
853  "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
855  }
856 
857  PQclear(dbres);
858 
859  PQfinish(conn_template1);
860 
861  check_ok();
862 }
bool exec_prog(const char *log_filename, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:86
bool pid_lock_file_exists(const char *datadir)
Definition: exec.c:234
void verify_directories(void)
Definition: exec.c:265
bool reap_child(bool wait_for_child)
Definition: parallel.c:278
void parallel_exec_prog(const char *log_file, const char *opt_log_file, const char *fmt,...)
Definition: parallel.c:62
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1204
#define CppAsString2(x)
Definition: c.h:311
void output_check_banner(bool live_check)
Definition: check.c:64
void check_cluster_versions(void)
Definition: check.c:291
void check_cluster_compatibility(bool live_check)
Definition: check.c:334
void check_and_dump_old_cluster(bool live_check)
Definition: check.c:82
void issue_warnings_and_set_wal_level(void)
Definition: check.c:236
void check_new_cluster(void)
Definition: check.c:188
void report_clusters_compatible(void)
Definition: check.c:217
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition: check.c:409
void output_completion_banner(char *deletion_script_file_name)
Definition: check.c:257
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:158
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:436
void disable_old_cluster(void)
Definition: controldata.c:712
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4405
void PQfreemem(void *ptr)
Definition: fe-exec.c:3865
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3310
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4137
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3705
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3418
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int pg_mode_mask
Definition: file_perm.c:25
int pg_dir_create_mode
Definition: file_perm.c:18
bool GetDataDirectoryCreatePerm(const char *dataDir)
#define PG_MODE_MASK_OWNER
Definition: file_perm.h:24
void get_db_and_rel_infos(ClusterInfo *cluster)
Definition: info.c:275
static char * locale
Definition: initdb.c:139
static void check_ok(void)
Definition: initdb.c:2041
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_fatal(...)
#define MAXPGPATH
const void size_t len
static void adjust_data_dir(void)
Definition: pg_ctl.c:2116
static char * argv0
Definition: pg_ctl.c:92
static pid_t start_postmaster(void)
Definition: pg_ctl.c:440
static char * exec_path
Definition: pg_ctl.c:87
NameData datname
Definition: pg_database.h:35
bool datallowconn
Definition: pg_database.h:50
static char * filename
Definition: pg_dumpall.c:119
char * output_files[]
Definition: pg_upgrade.c:67
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:768
static void make_outputdirs(char *pgdata)
Definition: pg_upgrade.c:224
static void prepare_new_cluster(void)
Definition: pg_upgrade.c:443
int main(int argc, char **argv)
Definition: pg_upgrade.c:80
static void copy_subdir_files(const char *old_subdir, const char *new_subdir)
Definition: pg_upgrade.c:618
OSInfo os_info
Definition: pg_upgrade.c:65
ClusterInfo new_cluster
Definition: pg_upgrade.c:64
static void create_new_objects(void)
Definition: pg_upgrade.c:495
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:601
static void copy_xact_xlog_xid(void)
Definition: pg_upgrade.c:643
ClusterInfo old_cluster
Definition: pg_upgrade.c:63
static void set_locale_and_encoding(void)
Definition: pg_upgrade.c:380
static void setup(char *argv0, bool *live_check)
Definition: pg_upgrade.c:309
static void prepare_new_globals(void)
Definition: pg_upgrade.c:473
void transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
Definition: relfilenumber.c:29
#define SERVER_START_LOG_FILE
Definition: pg_upgrade.h:67
void check_pghost_envvar(void)
Definition: server.c:350
#define MULTIXACT_FORMATCHANGE_CAT_VER
Definition: pg_upgrade.h:116
#define LOG_OUTPUTDIR
Definition: pg_upgrade.h:40
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:30
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:43
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:45
void cleanup_output_dirs(void)
Definition: util.c:63
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:237
#define SERVER_LOG_FILE
Definition: pg_upgrade.h:44
#define EXEC_PSQL_ARGS
Definition: pg_upgrade.h:368
LogOpts log_opts
Definition: util.c:17
void void prep_status_progress(const char *fmt,...) pg_attribute_printf(1
#define fopen_priv(path, mode)
Definition: pg_upgrade.h:390
#define DUMP_OUTPUTDIR
Definition: pg_upgrade.h:41
@ PG_STATUS
Definition: pg_upgrade.h:246
@ PG_REPORT
Definition: pg_upgrade.h:248
#define BASE_OUTPUTDIR
Definition: pg_upgrade.h:39
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:28
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:27
void stop_postmaster(bool in_atexit)
Definition: server.c:323
void prep_status(const char *fmt,...) pg_attribute_printf(1
#define INTERNAL_LOG_FILE
Definition: pg_upgrade.h:46
void end_progress_output(void)
Definition: util.c:43
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:92
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:31
char * last_dir_separator(const char *filename)
Definition: path.c:139
void canonicalize_path(char *path)
Definition: path.c:264
#define strerror
Definition: port.h:251
#define snprintf
Definition: port.h:238
#define fprintf
Definition: port.h:242
void get_restricted_token(void)
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:50
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11551
void get_sock_dir(ClusterInfo *cluster, bool live_check)
Definition: option.c:456
UserOpts user_opts
Definition: option.c:29
void parseCommandLine(int argc, char *argv[])
Definition: option.c:38
PGconn * conn
Definition: streamutil.c:54
char * pgdata
Definition: pg_upgrade.h:264
ControlData controldata
Definition: pg_upgrade.h:261
char * bindir
Definition: pg_upgrade.h:267
DbInfoArr dbarr
Definition: pg_upgrade.h:263
uint32 major_version
Definition: pg_upgrade.h:272
DbLocaleInfo * template0
Definition: pg_upgrade.h:262
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:209
uint32 chkpnt_nxtoid
Definition: pg_upgrade.h:211
char nextxlogfile[25]
Definition: pg_upgrade.h:208
uint32 chkpnt_nxtmxoff
Definition: pg_upgrade.h:213
uint32 cat_ver
Definition: pg_upgrade.h:207
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:212
uint32 chkpnt_oldstxid
Definition: pg_upgrade.h:215
uint32 chkpnt_nxtepoch
Definition: pg_upgrade.h:210
uint32 chkpnt_oldstMulti
Definition: pg_upgrade.h:214
DbInfo * dbs
Definition: pg_upgrade.h:195
char * db_name
Definition: pg_upgrade.h:175
Oid db_oid
Definition: pg_upgrade.h:174
char * dumpdir
Definition: pg_upgrade.h:290
char * rootdir
Definition: pg_upgrade.h:288
FILE * internal
Definition: pg_upgrade.h:284
char * basedir
Definition: pg_upgrade.h:289
char * logdir
Definition: pg_upgrade.h:291
bool verbose
Definition: pg_upgrade.h:285
bool do_sync
Definition: pg_upgrade.h:303
transferMode transfer_mode
Definition: pg_upgrade.h:304
bool check
Definition: pg_upgrade.h:301
#define mkdir(a, b)
Definition: win32_port.h:80
int gettimeofday(struct timeval *tp, void *tzp)