PostgreSQL Source Code git master
pg_upgrade.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include "catalog/pg_class_d.h"
#include "common/file_perm.h"
#include "common/logging.h"
#include "common/restricted_token.h"
#include "fe_utils/string_utils.h"
#include "pg_upgrade.h"
Include dependency graph for pg_upgrade.c:

Go to the source code of this file.

Macros

#define RESTORE_TRANSACTION_SIZE   1000
 

Functions

static void set_new_cluster_char_signedness (void)
 
static void set_locale_and_encoding (void)
 
static void prepare_new_cluster (void)
 
static void prepare_new_globals (void)
 
static void create_new_objects (void)
 
static void copy_xact_xlog_xid (void)
 
static void set_frozenxids (bool minmxid_only)
 
static void make_outputdirs (char *pgdata)
 
static void setup (char *argv0)
 
static void create_logical_replication_slots (void)
 
int main (int argc, char **argv)
 
static void remove_new_subdir (const char *subdir, bool rmtopdir)
 
static void copy_subdir_files (const char *old_subdir, const char *new_subdir)
 

Variables

ClusterInfo old_cluster
 
ClusterInfo new_cluster
 
OSInfo os_info
 
char * output_files []
 

Macro Definition Documentation

◆ RESTORE_TRANSACTION_SIZE

#define RESTORE_TRANSACTION_SIZE   1000

Definition at line 55 of file pg_upgrade.c.

Function Documentation

◆ copy_subdir_files()

static void copy_subdir_files ( const char *  old_subdir,
const char *  new_subdir 
)
static

Definition at line 715 of file pg_upgrade.c.

716{
717 char old_path[MAXPGPATH];
718 char new_path[MAXPGPATH];
719
720 remove_new_subdir(new_subdir, true);
721
722 snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
723 snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
724
725 prep_status("Copying old %s to new server", old_subdir);
726
727 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
728#ifndef WIN32
729 "cp -Rf \"%s\" \"%s\"",
730#else
731 /* flags: everything, no confirm, quiet, overwrite read-only */
732 "xcopy /e /y /q /r \"%s\" \"%s\\\"",
733#endif
734 old_path, new_path);
735
736 check_ok();
737}
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:85
static void check_ok(void)
Definition: initdb.c:2121
#define MAXPGPATH
ClusterInfo new_cluster
Definition: pg_upgrade.c:69
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:698
ClusterInfo old_cluster
Definition: pg_upgrade.c:68
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:45
void prep_status(const char *fmt,...) pg_attribute_printf(1
#define snprintf
Definition: port.h:239
char * pgdata
Definition: pg_upgrade.h:291

References check_ok(), exec_prog(), MAXPGPATH, new_cluster, old_cluster, ClusterInfo::pgdata, prep_status(), remove_new_subdir(), snprintf, and UTILITY_LOG_FILE.

Referenced by copy_xact_xlog_xid().

◆ copy_xact_xlog_xid()

static void copy_xact_xlog_xid ( void  )
static

Definition at line 740 of file pg_upgrade.c.

741{
742 /*
743 * Copy old commit logs to new data dir. pg_clog has been renamed to
744 * pg_xact in post-10 clusters.
745 */
747 "pg_clog" : "pg_xact",
749 "pg_clog" : "pg_xact");
750
751 prep_status("Setting oldest XID for new cluster");
752 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
753 "\"%s/pg_resetwal\" -f -u %u \"%s\"",
756 check_ok();
757
758 /* set the next transaction id and epoch of the new cluster */
759 prep_status("Setting next transaction ID and epoch for new cluster");
760 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
761 "\"%s/pg_resetwal\" -f -x %u \"%s\"",
764 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
765 "\"%s/pg_resetwal\" -f -e %u \"%s\"",
768 /* must reset commit timestamp limits also */
769 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
770 "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
775 check_ok();
776
777 /*
778 * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
779 * (see pg_upgrade.h) and the new server is after, then we don't copy
780 * pg_multixact files, but we need to reset pg_control so that the new
781 * server doesn't attempt to read multis older than the cutoff value.
782 */
785 {
786 copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
787 copy_subdir_files("pg_multixact/members", "pg_multixact/members");
788
789 prep_status("Setting next multixact ID and offset for new cluster");
790
791 /*
792 * we preserve all files and contents, so we must preserve both "next"
793 * counters here and the oldest multi present on system.
794 */
795 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
796 "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
802 check_ok();
803 }
805 {
806 /*
807 * Remove offsets/0000 file created by initdb that no longer matches
808 * the new multi-xid value. "members" starts at zero so no need to
809 * remove it.
810 */
811 remove_new_subdir("pg_multixact/offsets", false);
812
813 prep_status("Setting oldest multixact ID in new cluster");
814
815 /*
816 * We don't preserve files in this case, but it's important that the
817 * oldest multi is set to the latest value used by the old system, so
818 * that multixact.c returns the empty set for multis that might be
819 * present on disk. We set next multi to the value following that; it
820 * might end up wrapped around (i.e. 0) if the old cluster had
821 * next=MaxMultiXactId, but multixact.c can cope with that just fine.
822 */
823 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
824 "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
829 check_ok();
830 }
831
832 /* now reset the wal archives in the new cluster */
833 prep_status("Resetting WAL archives");
834 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
835 /* use timeline 1 to match controldata and no WAL history file */
836 "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
839 check_ok();
840}
static void copy_subdir_files(const char *old_subdir, const char *new_subdir)
Definition: pg_upgrade.c:715
#define MULTIXACT_FORMATCHANGE_CAT_VER
Definition: pg_upgrade.h:115
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:27
ControlData controldata
Definition: pg_upgrade.h:288
char * bindir
Definition: pg_upgrade.h:294
uint32 major_version
Definition: pg_upgrade.h:299
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:234
char nextxlogfile[25]
Definition: pg_upgrade.h:233
uint32 chkpnt_nxtmxoff
Definition: pg_upgrade.h:238
uint32 cat_ver
Definition: pg_upgrade.h:232
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:237
uint32 chkpnt_oldstxid
Definition: pg_upgrade.h:240
uint32 chkpnt_nxtepoch
Definition: pg_upgrade.h:235
uint32 chkpnt_oldstMulti
Definition: pg_upgrade.h:239

References ClusterInfo::bindir, ControlData::cat_ver, check_ok(), ControlData::chkpnt_nxtepoch, ControlData::chkpnt_nxtmulti, ControlData::chkpnt_nxtmxoff, ControlData::chkpnt_nxtxid, ControlData::chkpnt_oldstMulti, ControlData::chkpnt_oldstxid, ClusterInfo::controldata, copy_subdir_files(), exec_prog(), GET_MAJOR_VERSION, ClusterInfo::major_version, MULTIXACT_FORMATCHANGE_CAT_VER, new_cluster, ControlData::nextxlogfile, old_cluster, ClusterInfo::pgdata, prep_status(), remove_new_subdir(), and UTILITY_LOG_FILE.

Referenced by main().

◆ create_logical_replication_slots()

static void create_logical_replication_slots ( void  )
static

Definition at line 967 of file pg_upgrade.c.

968{
969 prep_status_progress("Restoring logical replication slots in the new cluster");
970
971 for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
972 {
973 DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
974 LogicalSlotInfoArr *slot_arr = &old_db->slot_arr;
975 PGconn *conn;
976 PQExpBuffer query;
977
978 /* Skip this database if there are no slots */
979 if (slot_arr->nslots == 0)
980 continue;
981
983 query = createPQExpBuffer();
984
985 pg_log(PG_STATUS, "%s", old_db->db_name);
986
987 for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
988 {
989 LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
990
991 /* Constructs a query for creating logical replication slots */
992 appendPQExpBuffer(query,
993 "SELECT * FROM "
994 "pg_catalog.pg_create_logical_replication_slot(");
995 appendStringLiteralConn(query, slot_info->slotname, conn);
996 appendPQExpBuffer(query, ", ");
997 appendStringLiteralConn(query, slot_info->plugin, conn);
998
999 appendPQExpBuffer(query, ", false, %s, %s);",
1000 slot_info->two_phase ? "true" : "false",
1001 slot_info->failover ? "true" : "false");
1002
1003 PQclear(executeQueryOrDie(conn, "%s", query->data));
1004
1005 resetPQExpBuffer(query);
1006 }
1007
1008 PQfinish(conn);
1009
1010 destroyPQExpBuffer(query);
1011 }
1012
1014 check_ok();
1015
1016 return;
1017}
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5224
void PQclear(PGresult *res)
Definition: fe-exec.c:721
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:28
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
void void prep_status_progress(const char *fmt,...) pg_attribute_printf(1
void void pg_noreturn void void end_progress_output(void)
Definition: util.c:43
@ PG_STATUS
Definition: pg_upgrade.h:273
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
PGconn * conn
Definition: streamutil.c:52
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:446
DbInfoArr dbarr
Definition: pg_upgrade.h:290
DbInfo * dbs
Definition: pg_upgrade.h:220
LogicalSlotInfoArr slot_arr
Definition: pg_upgrade.h:203
char * db_name
Definition: pg_upgrade.h:199
LogicalSlotInfo * slots
Definition: pg_upgrade.h:174

References appendPQExpBuffer(), appendStringLiteralConn(), check_ok(), conn, connectToServer(), createPQExpBuffer(), PQExpBufferData::data, DbInfo::db_name, ClusterInfo::dbarr, DbInfoArr::dbs, destroyPQExpBuffer(), end_progress_output(), executeQueryOrDie(), LogicalSlotInfo::failover, DbInfoArr::ndbs, new_cluster, LogicalSlotInfoArr::nslots, old_cluster, pg_log(), PG_STATUS, LogicalSlotInfo::plugin, PQclear(), PQfinish(), prep_status_progress(), resetPQExpBuffer(), DbInfo::slot_arr, LogicalSlotInfo::slotname, LogicalSlotInfoArr::slots, and LogicalSlotInfo::two_phase.

Referenced by main().

◆ create_new_objects()

static void create_new_objects ( void  )
static

Definition at line 562 of file pg_upgrade.c.

563{
564 int dbnum;
565 PGconn *conn_new_template1;
566
567 prep_status_progress("Restoring database schemas in the new cluster");
568
569 /*
570 * Ensure that any changes to template0 are fully written out to disk
571 * prior to restoring the databases. This is necessary because we use the
572 * FILE_COPY strategy to create the databases (which testing has shown to
573 * be faster), and when the server is in binary upgrade mode, it skips the
574 * checkpoints this strategy ordinarily performs.
575 */
576 conn_new_template1 = connectToServer(&new_cluster, "template1");
577 PQclear(executeQueryOrDie(conn_new_template1, "CHECKPOINT"));
578 PQfinish(conn_new_template1);
579
580 /*
581 * We cannot process the template1 database concurrently with others,
582 * because when it's transiently dropped, connection attempts would fail.
583 * So handle it in a separate non-parallelized pass.
584 */
585 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
586 {
587 char sql_file_name[MAXPGPATH],
588 log_file_name[MAXPGPATH];
589 DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
590 const char *create_opts;
591
592 /* Process only template1 in this pass */
593 if (strcmp(old_db->db_name, "template1") != 0)
594 continue;
595
596 pg_log(PG_STATUS, "%s", old_db->db_name);
597 snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
598 snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
599
600 /*
601 * template1 database will already exist in the target installation,
602 * so tell pg_restore to drop and recreate it; otherwise we would fail
603 * to propagate its database-level properties.
604 */
605 create_opts = "--clean --create";
606
607 exec_prog(log_file_name,
608 NULL,
609 true,
610 true,
611 "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
612 "--transaction-size=%d "
613 "--dbname postgres \"%s/%s\"",
616 create_opts,
619 sql_file_name);
620
621 break; /* done once we've processed template1 */
622 }
623
624 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
625 {
626 char sql_file_name[MAXPGPATH],
627 log_file_name[MAXPGPATH];
628 DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
629 const char *create_opts;
630 int txn_size;
631
632 /* Skip template1 in this pass */
633 if (strcmp(old_db->db_name, "template1") == 0)
634 continue;
635
636 pg_log(PG_STATUS, "%s", old_db->db_name);
637 snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
638 snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
639
640 /*
641 * postgres database will already exist in the target installation, so
642 * tell pg_restore to drop and recreate it; otherwise we would fail to
643 * propagate its database-level properties.
644 */
645 if (strcmp(old_db->db_name, "postgres") == 0)
646 create_opts = "--clean --create";
647 else
648 create_opts = "--create";
649
650 /*
651 * In parallel mode, reduce the --transaction-size of each restore job
652 * so that the total number of locks that could be held across all the
653 * jobs stays in bounds.
654 */
655 txn_size = RESTORE_TRANSACTION_SIZE;
656 if (user_opts.jobs > 1)
657 {
658 txn_size /= user_opts.jobs;
659 /* Keep some sanity if -j is huge */
660 txn_size = Max(txn_size, 10);
661 }
662
663 parallel_exec_prog(log_file_name,
664 NULL,
665 "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
666 "--transaction-size=%d "
667 "--dbname template1 \"%s/%s\"",
670 create_opts,
671 txn_size,
673 sql_file_name);
674 }
675
676 /* reap all children */
677 while (reap_child(true) == true)
678 ;
679
681 check_ok();
682
683 /*
684 * We don't have minmxids for databases or relations in pre-9.3 clusters,
685 * so set those after we have restored the schema.
686 */
688 set_frozenxids(true);
689
690 /* update new_cluster info now that we have objects in the databases */
692}
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 Max(x, y)
Definition: c.h:969
void get_db_rel_and_slot_infos(ClusterInfo *cluster)
Definition: info.c:280
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:865
#define RESTORE_TRANSACTION_SIZE
Definition: pg_upgrade.c:55
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:92
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:43
LogOpts log_opts
Definition: util.c:17
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:31
UserOpts user_opts
Definition: option.c:30
Oid db_oid
Definition: pg_upgrade.h:198
char * dumpdir
Definition: pg_upgrade.h:318
int jobs
Definition: pg_upgrade.h:333

References ClusterInfo::bindir, check_ok(), cluster_conn_opts(), connectToServer(), DB_DUMP_FILE_MASK, DB_DUMP_LOG_FILE_MASK, DbInfo::db_name, DbInfo::db_oid, ClusterInfo::dbarr, DbInfoArr::dbs, LogOpts::dumpdir, end_progress_output(), exec_prog(), executeQueryOrDie(), get_db_rel_and_slot_infos(), GET_MAJOR_VERSION, UserOpts::jobs, log_opts, ClusterInfo::major_version, Max, MAXPGPATH, DbInfoArr::ndbs, new_cluster, old_cluster, parallel_exec_prog(), pg_log(), PG_STATUS, PQclear(), PQfinish(), prep_status_progress(), reap_child(), RESTORE_TRANSACTION_SIZE, set_frozenxids(), snprintf, and user_opts.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 85 of file pg_upgrade.c.

86{
87 char *deletion_script_file_name = NULL;
88
89 /*
90 * pg_upgrade doesn't currently use common/logging.c, but initialize it
91 * anyway because we might call common code that does.
92 */
93 pg_logging_init(argv[0]);
94 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
95
96 /* Set default restrictive mask until new cluster permissions are read */
97 umask(PG_MODE_MASK_OWNER);
98
99 parseCommandLine(argc, argv);
100
102
105
106 /*
107 * Set mask based on PGDATA permissions, needed for the creation of the
108 * output directories with correct permissions.
109 */
111 pg_fatal("could not read permissions of directory \"%s\": %m",
113
114 umask(pg_mode_mask);
115
116 /*
117 * This needs to happen after adjusting the data directory of the new
118 * cluster in adjust_data_dir().
119 */
121
122 setup(argv[0]);
123
125
127
130
132
134
135
136 /* -- NEW -- */
138
141
143 "\n"
144 "Performing Upgrade\n"
145 "------------------");
146
148
150
151 stop_postmaster(false);
152
153 /*
154 * Destructive Changes to New Cluster
155 */
156
159
160 /* New now using xids of the old system */
161
162 /* -- NEW -- */
164
166
168
169 stop_postmaster(false);
170
171 /*
172 * Most failures happen in create_new_objects(), which has completed at
173 * this point. We do this here because it is just before linking, which
174 * will link the old and new cluster data files, preventing the old
175 * cluster from being safely started once the new cluster is started.
176 */
179
182
183 /*
184 * Assuming OIDs are only used in system tables, there is no need to
185 * restore the OID counter because we have not transferred any OIDs from
186 * the old system, but we do it anyway just in case. We do it late here
187 * because there is no need to have the schema load use new oids.
188 */
189 prep_status("Setting next OID for new cluster");
190 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
191 "\"%s/pg_resetwal\" -o %u \"%s\"",
194 check_ok();
195
196 /*
197 * Migrate the logical slots to the new cluster. Note that we need to do
198 * this after resetting WAL because otherwise the required WAL would be
199 * removed and slots would become unusable. There is a possibility that
200 * background processes might generate some WAL before we could create the
201 * slots in the new cluster but we can ignore that WAL as that won't be
202 * required downstream.
203 */
205 {
208 stop_postmaster(false);
209 }
210
211 if (user_opts.do_sync)
212 {
213 prep_status("Sync data directory to disk");
214 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
215 "\"%s/initdb\" --sync-only \"%s\" --sync-method %s",
219 check_ok();
220 }
221
222 create_script_for_old_cluster_deletion(&deletion_script_file_name);
223
225
227 "\n"
228 "Upgrade Complete\n"
229 "----------------");
230
231 output_completion_banner(deletion_script_file_name);
232
233 pg_free(deletion_script_file_name);
234
236
237 return 0;
238}
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1185
void check_cluster_versions(void)
Definition: check.c:803
void issue_warnings_and_set_wal_level(void)
Definition: check.c:748
void check_cluster_compatibility(void)
Definition: check.c:858
void check_new_cluster(void)
Definition: check.c:693
void report_clusters_compatible(void)
Definition: check.c:729
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition: check.c:933
void check_and_dump_old_cluster(void)
Definition: check.c:586
void output_completion_banner(char *deletion_script_file_name)
Definition: check.c:769
void output_check_banner(void)
Definition: check.c:568
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
void disable_old_cluster(void)
Definition: controldata.c:754
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int pg_mode_mask
Definition: file_perm.c:25
bool GetDataDirectoryCreatePerm(const char *dataDir)
#define PG_MODE_MASK_OWNER
Definition: file_perm.h:24
int count_old_cluster_logical_slots(void)
Definition: info.c:742
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_fatal(...)
static void adjust_data_dir(void)
Definition: pg_ctl.c:2125
static pid_t start_postmaster(void)
Definition: pg_ctl.c:440
static void create_logical_replication_slots(void)
Definition: pg_upgrade.c:967
static void make_outputdirs(char *pgdata)
Definition: pg_upgrade.c:246
static void prepare_new_cluster(void)
Definition: pg_upgrade.c:510
static void set_new_cluster_char_signedness(void)
Definition: pg_upgrade.c:398
static void create_new_objects(void)
Definition: pg_upgrade.c:562
static void copy_xact_xlog_xid(void)
Definition: pg_upgrade.c:740
static void set_locale_and_encoding(void)
Definition: pg_upgrade.c:434
static void setup(char *argv0)
Definition: pg_upgrade.c:331
static void prepare_new_globals(void)
Definition: pg_upgrade.c:540
void transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
Definition: relfilenumber.c:27
void cleanup_output_dirs(void)
Definition: util.c:63
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:264
@ PG_REPORT
Definition: pg_upgrade.h:275
void stop_postmaster(bool in_atexit)
Definition: server.c:349
void get_restricted_token(void)
void parseCommandLine(int argc, char *argv[])
Definition: option.c:39
void get_sock_dir(ClusterInfo *cluster)
Definition: option.c:492
uint32 chkpnt_nxtoid
Definition: pg_upgrade.h:236
char * sync_method
Definition: pg_upgrade.h:335
bool do_sync
Definition: pg_upgrade.h:331
transferMode transfer_mode
Definition: pg_upgrade.h:332

References adjust_data_dir(), ClusterInfo::bindir, check_and_dump_old_cluster(), check_cluster_compatibility(), check_cluster_versions(), check_new_cluster(), check_ok(), ControlData::chkpnt_nxtoid, cleanup_output_dirs(), ClusterInfo::controldata, copy_xact_xlog_xid(), count_old_cluster_logical_slots(), create_logical_replication_slots(), create_new_objects(), create_script_for_old_cluster_deletion(), ClusterInfo::dbarr, disable_old_cluster(), UserOpts::do_sync, exec_prog(), get_restricted_token(), get_sock_dir(), GetDataDirectoryCreatePerm(), issue_warnings_and_set_wal_level(), make_outputdirs(), new_cluster, old_cluster, output_check_banner(), output_completion_banner(), parseCommandLine(), pg_fatal, pg_free(), pg_log(), pg_logging_init(), pg_mode_mask, PG_MODE_MASK_OWNER, PG_REPORT, PG_TEXTDOMAIN, ClusterInfo::pgdata, prep_status(), prepare_new_cluster(), prepare_new_globals(), report_clusters_compatible(), set_locale_and_encoding(), set_new_cluster_char_signedness(), set_pglocale_pgservice(), setup(), start_postmaster(), stop_postmaster(), UserOpts::sync_method, transfer_all_new_tablespaces(), UserOpts::transfer_mode, TRANSFER_MODE_LINK, user_opts, and UTILITY_LOG_FILE.

◆ make_outputdirs()

static void make_outputdirs ( char *  pgdata)
static

Definition at line 246 of file pg_upgrade.c.

247{
248 FILE *fp;
249 char **filename;
250 time_t run_time = time(NULL);
251 char filename_path[MAXPGPATH];
252 char timebuf[128];
253 struct timeval time;
254 time_t tt;
255 int len;
256
258 len = snprintf(log_opts.rootdir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
259 if (len >= MAXPGPATH)
260 pg_fatal("directory path for new cluster is too long");
261
262 /* BASE_OUTPUTDIR/$timestamp/ */
263 gettimeofday(&time, NULL);
264 tt = (time_t) time.tv_sec;
265 strftime(timebuf, sizeof(timebuf), "%Y%m%dT%H%M%S", localtime(&tt));
266 /* append milliseconds */
267 snprintf(timebuf + strlen(timebuf), sizeof(timebuf) - strlen(timebuf),
268 ".%03d", (int) (time.tv_usec / 1000));
271 timebuf);
272 if (len >= MAXPGPATH)
273 pg_fatal("directory path for new cluster is too long");
274
275 /* BASE_OUTPUTDIR/$timestamp/dump/ */
278 timebuf, DUMP_OUTPUTDIR);
279 if (len >= MAXPGPATH)
280 pg_fatal("directory path for new cluster is too long");
281
282 /* BASE_OUTPUTDIR/$timestamp/log/ */
285 timebuf, LOG_OUTPUTDIR);
286 if (len >= MAXPGPATH)
287 pg_fatal("directory path for new cluster is too long");
288
289 /*
290 * Ignore the error case where the root path exists, as it is kept the
291 * same across runs.
292 */
293 if (mkdir(log_opts.rootdir, pg_dir_create_mode) < 0 && errno != EEXIST)
294 pg_fatal("could not create directory \"%s\": %m", log_opts.rootdir);
296 pg_fatal("could not create directory \"%s\": %m", log_opts.basedir);
298 pg_fatal("could not create directory \"%s\": %m", log_opts.dumpdir);
300 pg_fatal("could not create directory \"%s\": %m", log_opts.logdir);
301
302 len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
304 if (len >= sizeof(filename_path))
305 pg_fatal("directory path for new cluster is too long");
306
307 if ((log_opts.internal = fopen_priv(filename_path, "a")) == NULL)
308 pg_fatal("could not open log file \"%s\": %m", filename_path);
309
310 /* label start of upgrade in logfiles */
311 for (filename = output_files; *filename != NULL; filename++)
312 {
313 len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
315 if (len >= sizeof(filename_path))
316 pg_fatal("directory path for new cluster is too long");
317 if ((fp = fopen_priv(filename_path, "a")) == NULL)
318 pg_fatal("could not write to log file \"%s\": %m", filename_path);
319
320 fprintf(fp,
321 "-----------------------------------------------------------------\n"
322 " pg_upgrade run on %s"
323 "-----------------------------------------------------------------\n\n",
324 ctime(&run_time));
325 fclose(fp);
326 }
327}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
int pg_dir_create_mode
Definition: file_perm.c:18
const void size_t len
static char * filename
Definition: pg_dumpall.c:124
char * output_files[]
Definition: pg_upgrade.c:72
#define LOG_OUTPUTDIR
Definition: pg_upgrade.h:40
#define fopen_priv(path, mode)
Definition: pg_upgrade.h:429
#define DUMP_OUTPUTDIR
Definition: pg_upgrade.h:41
#define BASE_OUTPUTDIR
Definition: pg_upgrade.h:39
#define INTERNAL_LOG_FILE
Definition: pg_upgrade.h:46
char * rootdir
Definition: pg_upgrade.h:316
FILE * internal
Definition: pg_upgrade.h:312
char * basedir
Definition: pg_upgrade.h:317
char * logdir
Definition: pg_upgrade.h:319
#define mkdir(a, b)
Definition: win32_port.h:80
int gettimeofday(struct timeval *tp, void *tzp)

References BASE_OUTPUTDIR, LogOpts::basedir, DUMP_OUTPUTDIR, LogOpts::dumpdir, filename, fopen_priv, fprintf, gettimeofday(), LogOpts::internal, INTERNAL_LOG_FILE, len, log_opts, LOG_OUTPUTDIR, LogOpts::logdir, MAXPGPATH, mkdir, output_files, pg_dir_create_mode, pg_fatal, pg_malloc0(), LogOpts::rootdir, and snprintf.

Referenced by main().

◆ prepare_new_cluster()

static void prepare_new_cluster ( void  )
static

Definition at line 510 of file pg_upgrade.c.

511{
512 /*
513 * It would make more sense to freeze after loading the schema, but that
514 * would cause us to lose the frozenxids restored by the load. We use
515 * --analyze so autovacuum doesn't update statistics later
516 */
517 prep_status("Analyzing all rows in the new cluster");
518 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
519 "\"%s/vacuumdb\" %s --all --analyze %s",
521 log_opts.verbose ? "--verbose" : "");
522 check_ok();
523
524 /*
525 * We do freeze after analyze so pg_statistic is also frozen. template0 is
526 * not frozen here, but data rows were frozen by initdb, and we set its
527 * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
528 * counter later.
529 */
530 prep_status("Freezing all rows in the new cluster");
531 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
532 "\"%s/vacuumdb\" %s --all --freeze %s",
534 log_opts.verbose ? "--verbose" : "");
535 check_ok();
536}
bool verbose
Definition: pg_upgrade.h:313

References ClusterInfo::bindir, check_ok(), cluster_conn_opts(), exec_prog(), log_opts, new_cluster, prep_status(), UTILITY_LOG_FILE, and LogOpts::verbose.

Referenced by main().

◆ prepare_new_globals()

static void prepare_new_globals ( void  )
static

Definition at line 540 of file pg_upgrade.c.

541{
542 /*
543 * Before we restore anything, set frozenxids of initdb-created tables.
544 */
545 set_frozenxids(false);
546
547 /*
548 * Now restore global objects (roles and tablespaces).
549 */
550 prep_status("Restoring global objects in the new cluster");
551
552 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
553 "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s/%s\"",
557 check_ok();
558}
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:30
#define EXEC_PSQL_ARGS
Definition: pg_upgrade.h:404

References ClusterInfo::bindir, check_ok(), cluster_conn_opts(), LogOpts::dumpdir, exec_prog(), EXEC_PSQL_ARGS, GLOBALS_DUMP_FILE, log_opts, new_cluster, prep_status(), set_frozenxids(), and UTILITY_LOG_FILE.

Referenced by main().

◆ remove_new_subdir()

static void remove_new_subdir ( const char *  subdir,
bool  rmtopdir 
)
static

Definition at line 698 of file pg_upgrade.c.

699{
700 char new_path[MAXPGPATH];
701
702 prep_status("Deleting files from new %s", subdir);
703
704 snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
705 if (!rmtree(new_path, rmtopdir))
706 pg_fatal("could not delete directory \"%s\"", new_path);
707
708 check_ok();
709}
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:50

References check_ok(), MAXPGPATH, new_cluster, pg_fatal, ClusterInfo::pgdata, prep_status(), rmtree(), and snprintf.

Referenced by copy_subdir_files(), and copy_xact_xlog_xid().

◆ set_frozenxids()

static void set_frozenxids ( bool  minmxid_only)
static

Definition at line 865 of file pg_upgrade.c.

866{
867 int dbnum;
868 PGconn *conn,
869 *conn_template1;
870 PGresult *dbres;
871 int ntups;
872 int i_datname;
873 int i_datallowconn;
874
875 if (!minmxid_only)
876 prep_status("Setting frozenxid and minmxid counters in new cluster");
877 else
878 prep_status("Setting minmxid counter in new cluster");
879
880 conn_template1 = connectToServer(&new_cluster, "template1");
881
882 if (!minmxid_only)
883 /* set pg_database.datfrozenxid */
884 PQclear(executeQueryOrDie(conn_template1,
885 "UPDATE pg_catalog.pg_database "
886 "SET datfrozenxid = '%u'",
888
889 /* set pg_database.datminmxid */
890 PQclear(executeQueryOrDie(conn_template1,
891 "UPDATE pg_catalog.pg_database "
892 "SET datminmxid = '%u'",
894
895 /* get database names */
896 dbres = executeQueryOrDie(conn_template1,
897 "SELECT datname, datallowconn "
898 "FROM pg_catalog.pg_database");
899
900 i_datname = PQfnumber(dbres, "datname");
901 i_datallowconn = PQfnumber(dbres, "datallowconn");
902
903 ntups = PQntuples(dbres);
904 for (dbnum = 0; dbnum < ntups; dbnum++)
905 {
906 char *datname = PQgetvalue(dbres, dbnum, i_datname);
907 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
908
909 /*
910 * We must update databases where datallowconn = false, e.g.
911 * template0, because autovacuum increments their datfrozenxids,
912 * relfrozenxids, and relminmxid even if autovacuum is turned off, and
913 * even though all the data rows are already frozen. To enable this,
914 * we temporarily change datallowconn.
915 */
916 if (strcmp(datallowconn, "f") == 0)
917 PQclear(executeQueryOrDie(conn_template1,
918 "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
920
922
923 if (!minmxid_only)
924 /* set pg_class.relfrozenxid */
926 "UPDATE pg_catalog.pg_class "
927 "SET relfrozenxid = '%u' "
928 /* only heap, materialized view, and TOAST are vacuumed */
929 "WHERE relkind IN ("
930 CppAsString2(RELKIND_RELATION) ", "
931 CppAsString2(RELKIND_MATVIEW) ", "
932 CppAsString2(RELKIND_TOASTVALUE) ")",
934
935 /* set pg_class.relminmxid */
937 "UPDATE pg_catalog.pg_class "
938 "SET relminmxid = '%u' "
939 /* only heap, materialized view, and TOAST are vacuumed */
940 "WHERE relkind IN ("
941 CppAsString2(RELKIND_RELATION) ", "
942 CppAsString2(RELKIND_MATVIEW) ", "
943 CppAsString2(RELKIND_TOASTVALUE) ")",
945 PQfinish(conn);
946
947 /* Reset datallowconn flag */
948 if (strcmp(datallowconn, "f") == 0)
949 PQclear(executeQueryOrDie(conn_template1,
950 "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
952 }
953
954 PQclear(dbres);
955
956 PQfinish(conn_template1);
957
958 check_ok();
959}
#define CppAsString2(x)
Definition: c.h:363
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3589
NameData datname
Definition: pg_database.h:35
bool datallowconn
Definition: pg_database.h:50
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:13019

References check_ok(), ControlData::chkpnt_nxtmulti, ControlData::chkpnt_nxtxid, conn, connectToServer(), ClusterInfo::controldata, CppAsString2, datallowconn, datname, executeQueryOrDie(), new_cluster, old_cluster, PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), prep_status(), and quote_identifier().

Referenced by create_new_objects(), and prepare_new_globals().

◆ set_locale_and_encoding()

static void set_locale_and_encoding ( void  )
static

Definition at line 434 of file pg_upgrade.c.

435{
436 PGconn *conn_new_template1;
437 char *datcollate_literal;
438 char *datctype_literal;
439 char *datlocale_literal = NULL;
440 char *datlocale_src;
442
443 prep_status("Setting locale and encoding for new cluster");
444
445 /* escape literals with respect to new cluster */
446 conn_new_template1 = connectToServer(&new_cluster, "template1");
447
448 datcollate_literal = PQescapeLiteral(conn_new_template1,
449 locale->db_collate,
450 strlen(locale->db_collate));
451 datctype_literal = PQescapeLiteral(conn_new_template1,
452 locale->db_ctype,
453 strlen(locale->db_ctype));
454 datlocale_src = locale->db_locale ? locale->db_locale : "NULL";
455 datlocale_literal = PQescapeLiteral(conn_new_template1,
456 datlocale_src,
457 strlen(datlocale_src));
458
459 /* update template0 in new cluster */
461 PQclear(executeQueryOrDie(conn_new_template1,
462 "UPDATE pg_catalog.pg_database "
463 " SET encoding = %d, "
464 " datlocprovider = '%c', "
465 " datcollate = %s, "
466 " datctype = %s, "
467 " datlocale = %s "
468 " WHERE datname = 'template0' ",
469 locale->db_encoding,
470 locale->db_collprovider,
471 datcollate_literal,
472 datctype_literal,
473 datlocale_literal));
475 PQclear(executeQueryOrDie(conn_new_template1,
476 "UPDATE pg_catalog.pg_database "
477 " SET encoding = %d, "
478 " datlocprovider = '%c', "
479 " datcollate = %s, "
480 " datctype = %s, "
481 " daticulocale = %s "
482 " WHERE datname = 'template0' ",
483 locale->db_encoding,
484 locale->db_collprovider,
485 datcollate_literal,
486 datctype_literal,
487 datlocale_literal));
488 else
489 PQclear(executeQueryOrDie(conn_new_template1,
490 "UPDATE pg_catalog.pg_database "
491 " SET encoding = %d, "
492 " datcollate = %s, "
493 " datctype = %s "
494 " WHERE datname = 'template0' ",
495 locale->db_encoding,
496 datcollate_literal,
497 datctype_literal));
498
499 PQfreemem(datcollate_literal);
500 PQfreemem(datctype_literal);
501 PQfreemem(datlocale_literal);
502
503 PQfinish(conn_new_template1);
504
505 check_ok();
506}
void PQfreemem(void *ptr)
Definition: fe-exec.c:4032
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4363
static char * locale
Definition: initdb.c:140
DbLocaleInfo * template0
Definition: pg_upgrade.h:289

References check_ok(), connectToServer(), executeQueryOrDie(), GET_MAJOR_VERSION, locale, ClusterInfo::major_version, new_cluster, old_cluster, PQclear(), PQescapeLiteral(), PQfinish(), PQfreemem(), prep_status(), and ClusterInfo::template0.

Referenced by main().

◆ set_new_cluster_char_signedness()

static void set_new_cluster_char_signedness ( void  )
static

Definition at line 398 of file pg_upgrade.c.

399{
400 bool new_char_signedness;
401
402 /*
403 * Use the specified char signedness if specified. Otherwise we inherit
404 * the source database's signedness.
405 */
406 if (user_opts.char_signedness != -1)
407 new_char_signedness = (user_opts.char_signedness == 1);
408 else
409 new_char_signedness = old_cluster.controldata.default_char_signedness;
410
411 /* Change the char signedness of the new cluster, if necessary */
412 if (new_cluster.controldata.default_char_signedness != new_char_signedness)
413 {
414 prep_status("Setting the default char signedness for new cluster");
415
416 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
417 "\"%s/pg_resetwal\" --char-signedness %s \"%s\"",
419 new_char_signedness ? "signed" : "unsigned",
421
422 check_ok();
423 }
424}
bool default_char_signedness
Definition: pg_upgrade.h:253
int char_signedness
Definition: pg_upgrade.h:337

References ClusterInfo::bindir, UserOpts::char_signedness, check_ok(), ClusterInfo::controldata, ControlData::default_char_signedness, exec_prog(), new_cluster, old_cluster, ClusterInfo::pgdata, prep_status(), user_opts, and UTILITY_LOG_FILE.

Referenced by main().

◆ setup()

static void setup ( char *  argv0)
static

Definition at line 331 of file pg_upgrade.c.

332{
333 /*
334 * make sure the user has a clean environment, otherwise, we may confuse
335 * libpq when we connect to one (or both) of the servers.
336 */
338
339 /*
340 * In case the user hasn't specified the directory for the new binaries
341 * with -B, default to using the path of the currently executed pg_upgrade
342 * binary.
343 */
344 if (!new_cluster.bindir)
345 {
346 char exec_path[MAXPGPATH];
347
348 if (find_my_exec(argv0, exec_path) < 0)
349 pg_fatal("%s: could not find own program executable", argv0);
350 /* Trim off program name and keep just path */
354 }
355
357
358 /* no postmasters should be running, except for a live check */
360 {
361 /*
362 * If we have a postmaster.pid file, try to start the server. If it
363 * starts, the pid file was stale, so stop the server. If it doesn't
364 * start, assume the server is running. If the pid file is left over
365 * from a server crash, this also allows any committed transactions
366 * stored in the WAL to be replayed so they are not lost, because WAL
367 * files are not transferred from old to new servers. We later check
368 * for a clean shutdown.
369 */
370 if (start_postmaster(&old_cluster, false))
371 stop_postmaster(false);
372 else
373 {
374 if (!user_opts.check)
375 pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
376 "Please shutdown that postmaster and try again.");
377 else
378 user_opts.live_check = true;
379 }
380 }
381
382 /* same goes for the new postmaster */
384 {
385 if (start_postmaster(&new_cluster, false))
386 stop_postmaster(false);
387 else
388 pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
389 "Please shutdown that postmaster and try again.");
390 }
391}
bool pid_lock_file_exists(const char *datadir)
Definition: exec.c:233
void verify_directories(void)
Definition: exec.c:263
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:160
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * argv0
Definition: pg_ctl.c:93
static char * exec_path
Definition: pg_ctl.c:88
void check_pghost_envvar(void)
Definition: server.c:376
char * last_dir_separator(const char *filename)
Definition: path.c:145
void canonicalize_path(char *path)
Definition: path.c:337
bool live_check
Definition: pg_upgrade.h:330
bool check
Definition: pg_upgrade.h:329

References argv0, ClusterInfo::bindir, canonicalize_path(), UserOpts::check, check_pghost_envvar(), exec_path, find_my_exec(), last_dir_separator(), UserOpts::live_check, MAXPGPATH, new_cluster, old_cluster, pg_fatal, pg_strdup(), ClusterInfo::pgdata, pid_lock_file_exists(), start_postmaster(), stop_postmaster(), user_opts, and verify_directories().

Referenced by main().

Variable Documentation

◆ new_cluster

◆ old_cluster

◆ os_info

◆ output_files

char* output_files[]
Initial value:
= {
NULL
}
#define SERVER_LOG_FILE
Definition: pg_upgrade.h:44

Definition at line 72 of file pg_upgrade.c.

Referenced by make_outputdirs().