PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_upgrade.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include "access/multixact.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)
 
static void create_conflict_detection_slot (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
 
charoutput_files []
 

Macro Definition Documentation

◆ RESTORE_TRANSACTION_SIZE

#define RESTORE_TRANSACTION_SIZE   1000

Definition at line 59 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 749 of file pg_upgrade.c.

750{
751 char old_path[MAXPGPATH];
752 char new_path[MAXPGPATH];
753
755
758
759 prep_status("Copying old %s to new server", old_subdir);
760
761 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
763 "cp -Rf \"%s\" \"%s\"",
764#else
765 /* flags: everything, no confirm, quiet, overwrite read-only */
766 "xcopy /e /y /q /r \"%s\" \"%s\\\"",
767#endif
769
770 check_ok();
771}
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
static void check_ok(void)
Definition initdb.c:2108
#define MAXPGPATH
ClusterInfo new_cluster
Definition pg_upgrade.c:74
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition pg_upgrade.c:732
ClusterInfo old_cluster
Definition pg_upgrade.c:73
#define UTILITY_LOG_FILE
Definition pg_upgrade.h:45
void prep_status(const char *fmt,...) pg_attribute_printf(1
#define snprintf
Definition port.h:260
static int fb(int x)
char * pgdata
Definition pg_upgrade.h:299

References check_ok(), exec_prog(), fb(), 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 774 of file pg_upgrade.c.

775{
776 /*
777 * Copy old commit logs to new data dir. pg_clog has been renamed to
778 * pg_xact in post-10 clusters.
779 */
781 "pg_clog" : "pg_xact",
782 GET_MAJOR_VERSION(new_cluster.major_version) <= 906 ?
783 "pg_clog" : "pg_xact");
784
785 prep_status("Setting oldest XID for new cluster");
786 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
787 "\"%s/pg_resetwal\" -f -u %u \"%s\"",
790 check_ok();
791
792 /* set the next transaction id and epoch of the new cluster */
793 prep_status("Setting next transaction ID and epoch for new cluster");
794 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
795 "\"%s/pg_resetwal\" -f -x %u \"%s\"",
798 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
799 "\"%s/pg_resetwal\" -f -e %u \"%s\"",
802 /* must reset commit timestamp limits also */
803 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
804 "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
809 check_ok();
810
811 /* Copy or convert pg_multixact files */
815 {
816 /* No change in multixact format, just copy the files */
819
820 copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
821 copy_subdir_files("pg_multixact/members", "pg_multixact/members");
822
823 prep_status("Setting next multixact ID and offset for new cluster");
824
825 /*
826 * we preserve all files and contents, so we must preserve both "next"
827 * counters here and the oldest multi present on system.
828 */
829 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
830 "\"%s/pg_resetwal\" -O %" PRIu64 " -m %u,%u \"%s\"",
834 check_ok();
835 }
836 else
837 {
838 /* Conversion is needed */
842
843 /*
844 * Determine the range of multixacts to convert.
845 */
848 {
849 /* Versions 9.3 - 18: convert all multixids */
851 }
852 else
853 {
854 /*
855 * In PostgreSQL 9.2 and below, multitransactions were only used
856 * for row locking, and as such don't need to be preserved during
857 * upgrade. In that case, we utilize rewrite_multixacts() just to
858 * initialize new, empty files in the new format.
859 *
860 * It's important that the oldest multi is set to the latest value
861 * used by the old system, so that multixact.c returns the empty
862 * set for multis that might be present on disk.
863 */
865 }
866 /* handle wraparound */
871
872 /*
873 * Remove the files created by initdb in the new cluster.
874 * rewrite_multixacts() will create new ones.
875 */
876 remove_new_subdir("pg_multixact/members", false);
877 remove_new_subdir("pg_multixact/offsets", false);
878
879 /*
880 * Create new pg_multixact files, converting old ones if needed.
881 */
882 prep_status("Converting pg_multixact files");
884 check_ok();
885
886 prep_status("Setting next multixact ID and offset for new cluster");
887 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
888 "\"%s/pg_resetwal\" -O %" PRIu64 " -m %u,%u \"%s\"",
892 check_ok();
893 }
894
895 /* now reset the wal archives in the new cluster */
896 prep_status("Resetting WAL archives");
897 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
898 /* use timeline 1 to match controldata and no WAL history file */
899 "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
902 check_ok();
903}
#define Assert(condition)
Definition c.h:873
TransactionId MultiXactId
Definition c.h:676
uint64 MultiXactOffset
Definition c.h:678
#define FirstMultiXactId
Definition multixact.h:26
MultiXactOffset rewrite_multixacts(MultiXactId from_multi, MultiXactId to_multi)
static void copy_subdir_files(const char *old_subdir, const char *new_subdir)
Definition pg_upgrade.c:749
#define MULTIXACT_FORMATCHANGE_CAT_VER
Definition pg_upgrade.h:115
#define MULTIXACTOFFSET_FORMATCHANGE_CAT_VER
Definition pg_upgrade.h:122
#define GET_MAJOR_VERSION(v)
Definition pg_upgrade.h:27
ControlData controldata
Definition pg_upgrade.h:296
char * bindir
Definition pg_upgrade.h:302
uint32 major_version
Definition pg_upgrade.h:307
uint32 chkpnt_nxtxid
Definition pg_upgrade.h:241
char nextxlogfile[25]
Definition pg_upgrade.h:240
uint32 cat_ver
Definition pg_upgrade.h:239
uint32 chkpnt_nxtmulti
Definition pg_upgrade.h:244
uint64 chkpnt_nxtmxoff
Definition pg_upgrade.h:245
uint32 chkpnt_oldstxid
Definition pg_upgrade.h:247
uint32 chkpnt_nxtepoch
Definition pg_upgrade.h:242
uint32 chkpnt_oldstMulti
Definition pg_upgrade.h:246

References Assert, 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(), fb(), FirstMultiXactId, GET_MAJOR_VERSION, ClusterInfo::major_version, MULTIXACT_FORMATCHANGE_CAT_VER, MULTIXACTOFFSET_FORMATCHANGE_CAT_VER, new_cluster, ControlData::nextxlogfile, old_cluster, ClusterInfo::pgdata, prep_status(), remove_new_subdir(), rewrite_multixacts(), and UTILITY_LOG_FILE.

Referenced by main().

◆ create_conflict_detection_slot()

static void create_conflict_detection_slot ( void  )
static

Definition at line 1090 of file pg_upgrade.c.

1091{
1093
1094 prep_status("Creating the replication conflict detection slot");
1095
1097 PQclear(executeQueryOrDie(conn_new_template1, "SELECT pg_catalog.binary_upgrade_create_conflict_detection_slot()"));
1099
1100 check_ok();
1101}
void PQfinish(PGconn *conn)
#define PQclear
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition server.c:28
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2

References check_ok(), connectToServer(), executeQueryOrDie(), fb(), new_cluster, PQclear, PQfinish(), and prep_status().

Referenced by main().

◆ create_logical_replication_slots()

static void create_logical_replication_slots ( void  )
static

Definition at line 1030 of file pg_upgrade.c.

1031{
1032 prep_status_progress("Restoring logical replication slots in the new cluster");
1033
1034 for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
1035 {
1036 DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
1037 LogicalSlotInfoArr *slot_arr = &old_db->slot_arr;
1038 PGconn *conn;
1039 PQExpBuffer query;
1040
1041 /* Skip this database if there are no slots */
1042 if (slot_arr->nslots == 0)
1043 continue;
1044
1045 conn = connectToServer(&new_cluster, old_db->db_name);
1046 query = createPQExpBuffer();
1047
1048 pg_log(PG_STATUS, "%s", old_db->db_name);
1049
1050 for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
1051 {
1052 LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
1053
1054 /* Constructs a query for creating logical replication slots */
1056 "SELECT * FROM "
1057 "pg_catalog.pg_create_logical_replication_slot(");
1058 appendStringLiteralConn(query, slot_info->slotname, conn);
1059 appendPQExpBufferStr(query, ", ");
1060 appendStringLiteralConn(query, slot_info->plugin, conn);
1061
1062 appendPQExpBuffer(query, ", false, %s, %s);",
1063 slot_info->two_phase ? "true" : "false",
1064 slot_info->failover ? "true" : "false");
1065
1066 PQclear(executeQueryOrDie(conn, "%s", query->data));
1067
1068 resetPQExpBuffer(query);
1069 }
1070
1071 PQfinish(conn);
1072
1073 destroyPQExpBuffer(query);
1074 }
1075
1077 check_ok();
1078
1079 return;
1080}
static bool failover
void void pg_log(eLogType type, 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:281
PQExpBuffer createPQExpBuffer(void)
Definition pqexpbuffer.c:72
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
PGconn * conn
Definition streamutil.c:52
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
DbInfoArr dbarr
Definition pg_upgrade.h:298
DbInfo * dbs
Definition pg_upgrade.h:227
LogicalSlotInfo * slots
Definition pg_upgrade.h:181

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralConn(), check_ok(), conn, connectToServer(), createPQExpBuffer(), PQExpBufferData::data, ClusterInfo::dbarr, DbInfoArr::dbs, destroyPQExpBuffer(), end_progress_output(), executeQueryOrDie(), fb(), DbInfoArr::ndbs, new_cluster, LogicalSlotInfoArr::nslots, old_cluster, pg_log(), PG_STATUS, PQclear, PQfinish(), prep_status_progress(), resetPQExpBuffer(), and LogicalSlotInfoArr::slots.

Referenced by main().

◆ create_new_objects()

static void create_new_objects ( void  )
static

Definition at line 596 of file pg_upgrade.c.

597{
598 int dbnum;
600
601 prep_status_progress("Restoring database schemas in the new cluster");
602
603 /*
604 * Ensure that any changes to template0 are fully written out to disk
605 * prior to restoring the databases. This is necessary because we use the
606 * FILE_COPY strategy to create the databases (which testing has shown to
607 * be faster), and when the server is in binary upgrade mode, it skips the
608 * checkpoints this strategy ordinarily performs.
609 */
613
614 /*
615 * We cannot process the template1 database concurrently with others,
616 * because when it's transiently dropped, connection attempts would fail.
617 * So handle it in a separate non-parallelized pass.
618 */
619 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
620 {
623 DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
624 const char *create_opts;
625
626 /* Process only template1 in this pass */
627 if (strcmp(old_db->db_name, "template1") != 0)
628 continue;
629
630 pg_log(PG_STATUS, "%s", old_db->db_name);
633
634 /*
635 * template1 database will already exist in the target installation,
636 * so tell pg_restore to drop and recreate it; otherwise we would fail
637 * to propagate its database-level properties.
638 */
639 create_opts = "--clean --create";
640
642 NULL,
643 true,
644 true,
645 "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
646 "--transaction-size=%d "
647 "--dbname postgres \"%s/%s\"",
654
655 break; /* done once we've processed template1 */
656 }
657
658 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
659 {
662 DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
663 const char *create_opts;
664 int txn_size;
665
666 /* Skip template1 in this pass */
667 if (strcmp(old_db->db_name, "template1") == 0)
668 continue;
669
670 pg_log(PG_STATUS, "%s", old_db->db_name);
673
674 /*
675 * postgres database will already exist in the target installation, so
676 * tell pg_restore to drop and recreate it; otherwise we would fail to
677 * propagate its database-level properties.
678 */
679 if (strcmp(old_db->db_name, "postgres") == 0)
680 create_opts = "--clean --create";
681 else
682 create_opts = "--create";
683
684 /*
685 * In parallel mode, reduce the --transaction-size of each restore job
686 * so that the total number of locks that could be held across all the
687 * jobs stays in bounds.
688 */
689 txn_size = RESTORE_TRANSACTION_SIZE;
690 if (user_opts.jobs > 1)
691 {
692 txn_size /= user_opts.jobs;
693 /* Keep some sanity if -j is huge */
694 txn_size = Max(txn_size, 10);
695 }
696
698 NULL,
699 "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
700 "--transaction-size=%d "
701 "--dbname template1 \"%s/%s\"",
705 txn_size,
708 }
709
710 /* reap all children */
711 while (reap_child(true) == true)
712 ;
713
715 check_ok();
716
717 /*
718 * We don't have minmxids for databases or relations in pre-9.3 clusters,
719 * so set those after we have restored the schema.
720 */
722 set_frozenxids(true);
723
724 /* update new_cluster info now that we have objects in the databases */
726}
bool reap_child(bool wait_for_child)
Definition parallel.c:281
void parallel_exec_prog(const char *log_file, const char *opt_log_file, const char *fmt,...)
Definition parallel.c:63
#define Max(x, y)
Definition c.h:991
void get_db_rel_and_slot_infos(ClusterInfo *cluster)
Definition info.c:280
static void set_frozenxids(bool minmxid_only)
Definition pg_upgrade.c:928
#define RESTORE_TRANSACTION_SIZE
Definition pg_upgrade.c:59
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
char * dumpdir
Definition pg_upgrade.h:330

References ClusterInfo::bindir, check_ok(), cluster_conn_opts(), connectToServer(), DB_DUMP_FILE_MASK, DB_DUMP_LOG_FILE_MASK, ClusterInfo::dbarr, DbInfoArr::dbs, LogOpts::dumpdir, end_progress_output(), exec_prog(), executeQueryOrDie(), fb(), 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 90 of file pg_upgrade.c.

91{
94
95 /*
96 * pg_upgrade doesn't currently use common/logging.c, but initialize it
97 * anyway because we might call common code that does.
98 */
99 pg_logging_init(argv[0]);
100 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
101
102 /* Set default restrictive mask until new cluster permissions are read */
104
105 parseCommandLine(argc, argv);
106
108
111
112 /*
113 * Set mask based on PGDATA permissions, needed for the creation of the
114 * output directories with correct permissions.
115 */
117 pg_fatal("could not read permissions of directory \"%s\": %m",
119
121
122 /*
123 * This needs to happen after adjusting the data directory of the new
124 * cluster in adjust_data_dir().
125 */
127
128 setup(argv[0]);
129
131
133
136
138
140
141
142 /* -- NEW -- */
144
147
149 "\n"
150 "Performing Upgrade\n"
151 "------------------");
152
154
156
157 stop_postmaster(false);
158
159 /*
160 * Destructive Changes to New Cluster
161 */
162
165
166 /* New now using xids of the old system */
167
168 /* -- NEW -- */
170
172
174
175 stop_postmaster(false);
176
177 /*
178 * Most failures happen in create_new_objects(), which has completed at
179 * this point. We do this here because it is just before file transfer,
180 * which for --link will make it unsafe to start the old cluster once the
181 * new cluster is started, and for --swap will make it unsafe to start the
182 * old cluster at all.
183 */
187
190
191 /*
192 * Assuming OIDs are only used in system tables, there is no need to
193 * restore the OID counter because we have not transferred any OIDs from
194 * the old system, but we do it anyway just in case. We do it late here
195 * because there is no need to have the schema load use new oids.
196 */
197 prep_status("Setting next OID for new cluster");
198 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
199 "\"%s/pg_resetwal\" -o %u \"%s\"",
202 check_ok();
203
205
206 /*
207 * Migrate replication slots to the new cluster.
208 *
209 * Note that we must migrate logical slots after resetting WAL because
210 * otherwise the required WAL would be removed and slots would become
211 * unusable. There is a possibility that background processes might
212 * generate some WAL before we could create the slots in the new cluster
213 * but we can ignore that WAL as that won't be required downstream.
214 *
215 * The conflict detection slot is not affected by concerns related to WALs
216 * as it only retains the dead tuples. It is created here for consistency.
217 * Note that the new conflict detection slot uses the latest transaction
218 * ID as xmin, so it cannot protect dead tuples that existed before the
219 * upgrade. Additionally, commit timestamps and origin data are not
220 * preserved during the upgrade. So, even after creating the slot, the
221 * upgraded subscriber may be unable to detect conflicts or log relevant
222 * commit timestamps and origins when applying changes from the publisher
223 * occurred before the upgrade especially if those changes were not
224 * replicated. It can only protect tuples that might be deleted after the
225 * new cluster starts.
226 */
228 {
230
233
236
237 stop_postmaster(false);
238 }
239
240 if (user_opts.do_sync)
241 {
242 prep_status("Sync data directory to disk");
243 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
244 "\"%s/initdb\" --sync-only %s \"%s\" --sync-method %s",
247 "--no-sync-data-files" : "",
248 new_cluster.pgdata,
250 check_ok();
251 }
252
254
256
258 "\n"
259 "Upgrade Complete\n"
260 "----------------");
261
263
265
267
268 return 0;
269}
#define PG_TEXTDOMAIN(domain)
Definition c.h:1209
void check_cluster_versions(void)
Definition check.c:866
void issue_warnings_and_set_wal_level(void)
Definition check.c:808
void check_cluster_compatibility(void)
Definition check.c:921
void check_new_cluster(void)
Definition check.c:726
void report_clusters_compatible(void)
Definition check.c:789
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition check.c:996
void check_and_dump_old_cluster(void)
Definition check.c:590
void output_completion_banner(char *deletion_script_file_name)
Definition check.c:829
void output_check_banner(void)
Definition check.c:572
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition exec.c:430
void disable_old_cluster(transferMode transfer_mode)
void pg_free(void *ptr)
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:774
static DataDirSyncMethod sync_method
Definition initdb.c:170
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:441
static void stop_postmaster(void)
Definition pg_regress.c:433
static void create_logical_replication_slots(void)
static void make_outputdirs(char *pgdata)
Definition pg_upgrade.c:277
static void prepare_new_cluster(void)
Definition pg_upgrade.c:544
static void create_conflict_detection_slot(void)
static void set_new_cluster_char_signedness(void)
Definition pg_upgrade.c:429
static void create_new_objects(void)
Definition pg_upgrade.c:596
static void copy_xact_xlog_xid(void)
Definition pg_upgrade.c:774
static void set_locale_and_encoding(void)
Definition pg_upgrade.c:465
static void setup(char *argv0)
Definition pg_upgrade.c:362
static void prepare_new_globals(void)
Definition pg_upgrade.c:574
void transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
void cleanup_output_dirs(void)
Definition util.c:63
@ TRANSFER_MODE_LINK
Definition pg_upgrade.h:271
@ TRANSFER_MODE_SWAP
Definition pg_upgrade.h:272
@ PG_REPORT
Definition pg_upgrade.h:283
void get_restricted_token(void)
void parseCommandLine(int argc, char *argv[])
Definition option.c:39
void get_sock_dir(ClusterInfo *cluster)
Definition option.c:499
bool sub_retain_dead_tuples
Definition pg_upgrade.h:314
uint32 chkpnt_nxtoid
Definition pg_upgrade.h:243
bool do_sync
Definition pg_upgrade.h:343
transferMode transfer_mode
Definition pg_upgrade.h:344

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_conflict_detection_slot(), create_logical_replication_slots(), create_new_objects(), create_script_for_old_cluster_deletion(), ClusterInfo::dbarr, disable_old_cluster(), UserOpts::do_sync, exec_prog(), fb(), 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(), ClusterInfo::sub_retain_dead_tuples, UserOpts::sync_method, transfer_all_new_tablespaces(), UserOpts::transfer_mode, TRANSFER_MODE_LINK, TRANSFER_MODE_SWAP, user_opts, and UTILITY_LOG_FILE.

◆ make_outputdirs()

static void make_outputdirs ( char pgdata)
static

Definition at line 277 of file pg_upgrade.c.

278{
279 FILE *fp;
280 char **filename;
281 time_t run_time = time(NULL);
283 char timebuf[128];
284 struct timeval time;
285 time_t tt;
286 int len;
287
289 len = snprintf(log_opts.rootdir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
290 if (len >= MAXPGPATH)
291 pg_fatal("directory path for new cluster is too long");
292
293 /* BASE_OUTPUTDIR/$timestamp/ */
294 gettimeofday(&time, NULL);
295 tt = (time_t) time.tv_sec;
296 strftime(timebuf, sizeof(timebuf), "%Y%m%dT%H%M%S", localtime(&tt));
297 /* append milliseconds */
299 ".%03d", (int) (time.tv_usec / 1000));
302 timebuf);
303 if (len >= MAXPGPATH)
304 pg_fatal("directory path for new cluster is too long");
305
306 /* BASE_OUTPUTDIR/$timestamp/dump/ */
310 if (len >= MAXPGPATH)
311 pg_fatal("directory path for new cluster is too long");
312
313 /* BASE_OUTPUTDIR/$timestamp/log/ */
317 if (len >= MAXPGPATH)
318 pg_fatal("directory path for new cluster is too long");
319
320 /*
321 * Ignore the error case where the root path exists, as it is kept the
322 * same across runs.
323 */
325 pg_fatal("could not create directory \"%s\": %m", log_opts.rootdir);
327 pg_fatal("could not create directory \"%s\": %m", log_opts.basedir);
329 pg_fatal("could not create directory \"%s\": %m", log_opts.dumpdir);
331 pg_fatal("could not create directory \"%s\": %m", log_opts.logdir);
332
333 len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
335 if (len >= sizeof(filename_path))
336 pg_fatal("directory path for new cluster is too long");
337
339 pg_fatal("could not open log file \"%s\": %m", filename_path);
340
341 /* label start of upgrade in logfiles */
343 {
344 len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
346 if (len >= sizeof(filename_path))
347 pg_fatal("directory path for new cluster is too long");
348 if ((fp = fopen_priv(filename_path, "a")) == NULL)
349 pg_fatal("could not write to log file \"%s\": %m", filename_path);
350
351 fprintf(fp,
352 "-----------------------------------------------------------------\n"
353 " pg_upgrade run on %s"
354 "-----------------------------------------------------------------\n\n",
355 ctime(&run_time));
356 fclose(fp);
357 }
358}
#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:120
char * output_files[]
Definition pg_upgrade.c:77
#define LOG_OUTPUTDIR
Definition pg_upgrade.h:40
#define fopen_priv(path, mode)
Definition pg_upgrade.h:439
#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:328
FILE * internal
Definition pg_upgrade.h:324
char * basedir
Definition pg_upgrade.h:329
char * logdir
Definition pg_upgrade.h:331
#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, fb(), 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 544 of file pg_upgrade.c.

545{
546 /*
547 * It would make more sense to freeze after loading the schema, but that
548 * would cause us to lose the frozenxids restored by the load. We use
549 * --analyze so autovacuum doesn't update statistics later
550 */
551 prep_status("Analyzing all rows in the new cluster");
552 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
553 "\"%s/vacuumdb\" %s --all --analyze %s",
555 log_opts.verbose ? "--verbose" : "");
556 check_ok();
557
558 /*
559 * We do freeze after analyze so pg_statistic is also frozen. template0 is
560 * not frozen here, but data rows were frozen by initdb, and we set its
561 * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
562 * counter later.
563 */
564 prep_status("Freezing all rows in the new cluster");
565 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
566 "\"%s/vacuumdb\" %s --all --freeze %s",
568 log_opts.verbose ? "--verbose" : "");
569 check_ok();
570}
bool verbose
Definition pg_upgrade.h:325

References ClusterInfo::bindir, check_ok(), cluster_conn_opts(), exec_prog(), fb(), 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 574 of file pg_upgrade.c.

575{
576 /*
577 * Before we restore anything, set frozenxids of initdb-created tables.
578 */
579 set_frozenxids(false);
580
581 /*
582 * Now restore global objects (roles and tablespaces).
583 */
584 prep_status("Restoring global objects in the new cluster");
585
586 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
587 "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s/%s\"",
591 check_ok();
592}
#define GLOBALS_DUMP_FILE
Definition pg_upgrade.h:30
#define EXEC_PSQL_ARGS
Definition pg_upgrade.h:414

References ClusterInfo::bindir, check_ok(), cluster_conn_opts(), LogOpts::dumpdir, exec_prog(), EXEC_PSQL_ARGS, fb(), 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 732 of file pg_upgrade.c.

733{
734 char new_path[MAXPGPATH];
735
736 prep_status("Deleting files from new %s", subdir);
737
738 snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
739 if (!rmtree(new_path, rmtopdir))
740 pg_fatal("could not delete directory \"%s\"", new_path);
741
742 check_ok();
743}
bool rmtree(const char *path, bool rmtopdir)
Definition rmtree.c:50

References check_ok(), fb(), 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 928 of file pg_upgrade.c.

929{
930 int dbnum;
931 PGconn *conn,
934 int ntups;
935 int i_datname;
936 int i_datallowconn;
937
938 if (!minmxid_only)
939 prep_status("Setting frozenxid and minmxid counters in new cluster");
940 else
941 prep_status("Setting minmxid counter in new cluster");
942
944
945 if (!minmxid_only)
946 /* set pg_database.datfrozenxid */
948 "UPDATE pg_catalog.pg_database "
949 "SET datfrozenxid = '%u'",
951
952 /* set pg_database.datminmxid */
954 "UPDATE pg_catalog.pg_database "
955 "SET datminmxid = '%u'",
957
958 /* get database names */
960 "SELECT datname, datallowconn "
961 "FROM pg_catalog.pg_database");
962
963 i_datname = PQfnumber(dbres, "datname");
964 i_datallowconn = PQfnumber(dbres, "datallowconn");
965
966 ntups = PQntuples(dbres);
967 for (dbnum = 0; dbnum < ntups; dbnum++)
968 {
969 char *datname = PQgetvalue(dbres, dbnum, i_datname);
971
972 /*
973 * We must update databases where datallowconn = false, e.g.
974 * template0, because autovacuum increments their datfrozenxids,
975 * relfrozenxids, and relminmxid even if autovacuum is turned off, and
976 * even though all the data rows are already frozen. To enable this,
977 * we temporarily change datallowconn.
978 */
979 if (strcmp(datallowconn, "f") == 0)
981 "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
983
985
986 if (!minmxid_only)
987 /* set pg_class.relfrozenxid */
989 "UPDATE pg_catalog.pg_class "
990 "SET relfrozenxid = '%u' "
991 /* only heap, materialized view, and TOAST are vacuumed */
992 "WHERE relkind IN ("
997
998 /* set pg_class.relminmxid */
1000 "UPDATE pg_catalog.pg_class "
1001 "SET relminmxid = '%u' "
1002 /* only heap, materialized view, and TOAST are vacuumed */
1003 "WHERE relkind IN ("
1008 PQfinish(conn);
1009
1010 /* Reset datallowconn flag */
1011 if (strcmp(datallowconn, "f") == 0)
1013 "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
1015 }
1016
1017 PQclear(dbres);
1018
1020
1021 check_ok();
1022}
#define CppAsString2(x)
Definition c.h:428
int PQfnumber(const PGresult *res, const char *field_name)
Definition fe-exec.c:3606
#define PQgetvalue
#define PQntuples
NameData datname
Definition pg_database.h:35
bool datallowconn
Definition pg_database.h:50
const char * quote_identifier(const char *ident)

References check_ok(), ControlData::chkpnt_nxtmulti, ControlData::chkpnt_nxtxid, conn, connectToServer(), ClusterInfo::controldata, CppAsString2, datallowconn, datname, executeQueryOrDie(), fb(), 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 465 of file pg_upgrade.c.

466{
468 char *datcollate_literal;
469 char *datctype_literal;
470 char *datlocale_literal = NULL;
472
473 prep_status("Setting locale and encoding for new cluster");
474
475 /* escape literals with respect to new cluster */
477
479 locale->db_collate,
480 strlen(locale->db_collate));
482 locale->db_ctype,
483 strlen(locale->db_ctype));
484
485 if (locale->db_locale)
487 locale->db_locale,
488 strlen(locale->db_locale));
489 else
490 datlocale_literal = "NULL";
491
492 /* update template0 in new cluster */
495 "UPDATE pg_catalog.pg_database "
496 " SET encoding = %d, "
497 " datlocprovider = '%c', "
498 " datcollate = %s, "
499 " datctype = %s, "
500 " datlocale = %s "
501 " WHERE datname = 'template0' ",
502 locale->db_encoding,
503 locale->db_collprovider,
509 "UPDATE pg_catalog.pg_database "
510 " SET encoding = %d, "
511 " datlocprovider = '%c', "
512 " datcollate = %s, "
513 " datctype = %s, "
514 " daticulocale = %s "
515 " WHERE datname = 'template0' ",
516 locale->db_encoding,
517 locale->db_collprovider,
521 else
523 "UPDATE pg_catalog.pg_database "
524 " SET encoding = %d, "
525 " datcollate = %s, "
526 " datctype = %s "
527 " WHERE datname = 'template0' ",
528 locale->db_encoding,
531
534 if (locale->db_locale)
536
538
539 check_ok();
540}
void PQfreemem(void *ptr)
Definition fe-exec.c:4049
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition fe-exec.c:4399
DbLocaleInfo * template0
Definition pg_upgrade.h:297
char db_collprovider
Definition pg_upgrade.h:220
char * db_locale
Definition pg_upgrade.h:221
char * db_collate
Definition pg_upgrade.h:218
char * db_ctype
Definition pg_upgrade.h:219

References check_ok(), connectToServer(), DbLocaleInfo::db_collate, DbLocaleInfo::db_collprovider, DbLocaleInfo::db_ctype, DbLocaleInfo::db_encoding, DbLocaleInfo::db_locale, executeQueryOrDie(), fb(), GET_MAJOR_VERSION, 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 429 of file pg_upgrade.c.

430{
432
433 /*
434 * Use the specified char signedness if specified. Otherwise we inherit
435 * the source database's signedness.
436 */
437 if (user_opts.char_signedness != -1)
439 else
441
442 /* Change the char signedness of the new cluster, if necessary */
444 {
445 prep_status("Setting the default char signedness for new cluster");
446
447 exec_prog(UTILITY_LOG_FILE, NULL, true, true,
448 "\"%s/pg_resetwal\" --char-signedness %s \"%s\"",
450 new_char_signedness ? "signed" : "unsigned",
451 new_cluster.pgdata);
452
453 check_ok();
454 }
455}
bool default_char_signedness
Definition pg_upgrade.h:260
int char_signedness
Definition pg_upgrade.h:349

References ClusterInfo::bindir, UserOpts::char_signedness, check_ok(), ClusterInfo::controldata, ControlData::default_char_signedness, exec_prog(), fb(), 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 362 of file pg_upgrade.c.

363{
364 /*
365 * make sure the user has a clean environment, otherwise, we may confuse
366 * libpq when we connect to one (or both) of the servers.
367 */
369
370 /*
371 * In case the user hasn't specified the directory for the new binaries
372 * with -B, default to using the path of the currently executed pg_upgrade
373 * binary.
374 */
375 if (!new_cluster.bindir)
376 {
377 char exec_path[MAXPGPATH];
378
379 if (find_my_exec(argv0, exec_path) < 0)
380 pg_fatal("%s: could not find own program executable", argv0);
381 /* Trim off program name and keep just path */
385 }
386
388
389 /* no postmasters should be running, except for a live check */
391 {
392 /*
393 * If we have a postmaster.pid file, try to start the server. If it
394 * starts, the pid file was stale, so stop the server. If it doesn't
395 * start, assume the server is running. If the pid file is left over
396 * from a server crash, this also allows any committed transactions
397 * stored in the WAL to be replayed so they are not lost, because WAL
398 * files are not transferred from old to new servers. We later check
399 * for a clean shutdown.
400 */
401 if (start_postmaster(&old_cluster, false))
402 stop_postmaster(false);
403 else
404 {
405 if (!user_opts.check)
406 pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
407 "Please shutdown that postmaster and try again.");
408 else
409 user_opts.live_check = true;
410 }
411 }
412
413 /* same goes for the new postmaster */
415 {
416 if (start_postmaster(&new_cluster, false))
417 stop_postmaster(false);
418 else
419 pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
420 "Please shutdown that postmaster and try again.");
421 }
422}
bool pid_lock_file_exists(const char *datadir)
Definition exec.c:234
void verify_directories(void)
Definition exec.c:264
int find_my_exec(const char *argv0, char *retpath)
Definition exec.c:161
char * pg_strdup(const char *in)
Definition fe_memutils.c:85
static char * argv0
Definition pg_ctl.c:94
static char * exec_path
Definition pg_ctl.c:89
void check_pghost_envvar(void)
Definition server.c:319
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:342
bool check
Definition pg_upgrade.h:341

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:

Definition at line 77 of file pg_upgrade.c.

77 {
79#ifdef WIN32
80 /* unique file for pg_ctl start */
82#endif
85 NULL
86};
#define SERVER_START_LOG_FILE
Definition pg_upgrade.h:67

Referenced by make_outputdirs().