20#define DEF_PGUPORT 50432
22#define MAX_STRING 1024
23#define QUERY_ALLOC 8192
25#define MESSAGE_WIDTH 62
27#define GET_MAJOR_VERSION(v) ((v) / 100)
30#define GLOBALS_DUMP_FILE "pg_upgrade_dump_globals.sql"
31#define DB_DUMP_FILE_MASK "pg_upgrade_dump_%u.custom"
39#define BASE_OUTPUTDIR "pg_upgrade_output.d"
40#define LOG_OUTPUTDIR "log"
41#define DUMP_OUTPUTDIR "dump"
43#define DB_DUMP_LOG_FILE_MASK "pg_upgrade_dump_%u.log"
44#define SERVER_LOG_FILE "pg_upgrade_server.log"
45#define UTILITY_LOG_FILE "pg_upgrade_utility.log"
46#define INTERNAL_LOG_FILE "pg_upgrade_internal.log"
67#define SERVER_START_LOG_FILE SERVER_LOG_FILE
68#define SERVER_STOP_LOG_FILE SERVER_LOG_FILE
70#define SERVER_START_LOG_FILE "pg_upgrade_server_start.log"
76#define SERVER_STOP_LOG_FILE UTILITY_LOG_FILE
81#define pg_mv_file rename
82#define PATH_SEPARATOR '/'
83#define PATH_QUOTE '\''
85#define RMDIR_CMD "rm -rf"
86#define SCRIPT_PREFIX "./"
87#define SCRIPT_EXT "sh"
91#define pg_mv_file pgrename
92#define PATH_SEPARATOR '\\'
94/* @ prefix disables command echo in .bat files */
95#define RM_CMD "@DEL /q"
96#define RMDIR_CMD "@RMDIR /s/q"
97#define SCRIPT_PREFIX ""
98#define SCRIPT_EXT "bat"
100#define ECHO_BLANK "."
105 * The format of visibility map was changed with this 9.6 commit.
107#define VISIBILITY_MAP_FROZEN_BIT_CAT_VER 201603011
110 * pg_multixact format changed in 9.3 commit 0ac5ad5134f2769ccbaefec73844f85,
111 * ("Improve concurrency of foreign key locking") which also updated catalog
112 * version to this value. pg_upgrade behavior depends on whether old and new
113 * server versions are both newer than this, or only the new one is.
115#define MULTIXACT_FORMATCHANGE_CAT_VER 201301231
118 * MultiXactOffset was changed from 32-bit to 64-bit in version 19, at this
119 * catalog version. pg_multixact files need to be converted when upgrading
120 * across this version.
122#define MULTIXACTOFFSET_FORMATCHANGE_CAT_VER 202512091
125 * large object chunk size added to pg_controldata,
126 * commit 5f93c37805e7485488480916b4585e098d3cc883
128#define LARGE_OBJECT_SIZE_PG_CONTROL_VER 942
131 * change in JSONB format during 9.4 beta
133#define JSONB_FORMAT_CHANGE_CAT_VER 201409291
136 * The control file was changed to have the default char signedness,
137 * commit 44fe30fdab6746a287163e7cc093fd36cda8eb92
139#define DEFAULT_CHAR_SIGNEDNESS_CAT_VER 202502212
142 * Each relation is represented by a relinfo structure.
146 /* Can't use NAMEDATALEN; not guaranteed to be same on client */
147 char *nspname; /* namespace name */
148 char *relname; /* relation name */
149 Oid reloid; /* relation OID */
150 RelFileNumber relfilenumber; /* relation file number */
151 Oid indtable; /* if index, OID of its table, else 0 */
152 Oid toastheap; /* if toast table, OID of base table, else 0 */
153 char *tablespace; /* tablespace path; "" for cluster default */
154 bool nsp_alloc; /* should nspname be freed? */
155 bool tblsp_alloc; /* should tablespace be freed? */
165 * Structure to store logical replication slot information.
169 char *slotname; /* slot name */
170 char *plugin; /* plugin */
171 bool two_phase; /* can the slot decode 2PC? */
172 bool caught_up; /* has the slot caught up to latest changes? */
173 bool invalid; /* if true, the slot is unusable */
174 bool failover; /* is the slot designated to be synced to the
175 * physical standby? */
180 int nslots; /* number of logical slot infos */
181 LogicalSlotInfo *slots; /* array of logical slot infos */
185 * The following structure represents a relation mapping.
189 const char *old_tablespace;
190 const char *new_tablespace;
191 const char *old_tablespace_suffix;
192 const char *new_tablespace_suffix;
194 RelFileNumber relfilenumber;
195 /* the rest are used only for logging and error reporting */
196 char *nspname; /* namespaces */
201 * Structure to store database information
205 Oid db_oid; /* oid of the database */
206 char *db_name; /* database name */
207 char db_tablespace[MAXPGPATH]; /* database default tablespace
209 RelInfoArr rel_arr; /* array of all user relinfos */
210 LogicalSlotInfoArr slot_arr; /* array of all LogicalSlotInfo */
214 * Locale information about a database.
220 char db_collprovider;
227 DbInfo *dbs; /* array of db infos */
228 int ndbs; /* number of db infos */
232 * The following structure is used to hold pg_control information.
233 * Rather than using the backend's control structure we use our own
234 * structure to avoid pg_control version issues between releases.
240 char nextxlogfile[25];
241 uint32 chkpnt_nxtxid;
242 uint32 chkpnt_nxtepoch;
243 uint32 chkpnt_nxtoid;
244 uint32 chkpnt_nxtmulti;
245 uint64 chkpnt_nxtmxoff;
246 uint32 chkpnt_oldstMulti;
247 uint32 chkpnt_oldstxid;
258 bool float8_pass_by_value;
259 uint32 data_checksum_version;
260 bool default_char_signedness;
264 * Enumeration to denote transfer modes
270 TRANSFER_MODE_COPY_FILE_RANGE,
276 * Enumeration to denote pg_log modes
281 PG_STATUS, /* these messages do not get a newline added */
282 PG_REPORT_NONL, /* these too */
292 * information about each cluster
296 ControlData controldata; /* pg_control information */
297 DbLocaleInfo *template0; /* template0 locale info */
298 DbInfoArr dbarr; /* dbinfos array */
299 char *pgdata; /* pathname for cluster's $PGDATA directory */
300 char *pgconfig; /* pathname for cluster's config file
302 char *bindir; /* pathname for cluster's executable directory */
303 char *pgopts; /* options to pass to the server, like pg_ctl
305 char *sockdir; /* directory for Unix Domain socket, if any */
306 unsigned short port; /* port number where postmaster is waiting */
307 uint32 major_version; /* PG_VERSION of cluster */
308 char *major_version_str; /* string PG_VERSION of cluster */
309 uint32 bin_version; /* version returned from pg_ctl */
310 char **tablespaces; /* tablespace directories */
312 const char *tablespace_suffix; /* directory specification */
313 int nsubs; /* number of subscriptions */
314 bool sub_retain_dead_tuples; /* whether a subscription enables
315 * retain_dead_tuples. */
324 FILE *internal; /* internal log FILE */
325 bool verbose; /* true -> be verbose in messages */
326 bool retain; /* retain log files on success */
327 /* Set of internal directories for output files */
328 char *rootdir; /* Root directory, aka pg_upgrade_output.d */
329 char *basedir; /* Base output directory, with timestamp */
330 char *dumpdir; /* Dumps */
331 char *logdir; /* Log files */
332 bool isatty; /* is stdout a tty */
341 bool check; /* check clusters only, don't change any data */
342 bool live_check; /* check clusters only, old server is running */
343 bool do_sync; /* flush changes to disk */
344 transferMode transfer_mode; /* copy files or link them? */
345 int jobs; /* number of processes/threads to use */
346 char *socketdir; /* directory to use for Unix sockets */
348 bool do_statistics; /* carry over statistics from old cluster */
349 int char_signedness; /* default char signedness: -1 for initial
350 * value, 1 for "signed" and 0 for
365 const char *progname; /* complete pathname for this program */
366 char *user; /* username for clusters */
367 bool user_specified; /* user specified on command-line */
368 LibraryInfo *libraries; /* loadable libraries */
370 ClusterInfo *running_cluster;
374/* Function signature for data type check version hook */
375typedef bool (*DataTypesUsageVersionCheck) (ClusterInfo *cluster);
380extern LogOpts log_opts;
381extern UserOpts user_opts;
382extern ClusterInfo old_cluster,
384extern OSInfo os_info;
389void output_check_banner(void);
390void check_and_dump_old_cluster(void);
391void check_new_cluster(void);
392void report_clusters_compatible(void);
393void issue_warnings_and_set_wal_level(void);
394void output_completion_banner(char *deletion_script_file_name);
395void check_cluster_versions(void);
396void check_cluster_compatibility(void);
397void create_script_for_old_cluster_deletion(char **deletion_script_file_name);
402void get_control_data(ClusterInfo *cluster);
403void check_control_data(ControlData *oldctrl, ControlData *newctrl);
404void disable_old_cluster(transferMode transfer_mode);
409void generate_old_dump(void);
414#define EXEC_PSQL_ARGS "--echo-queries --set ON_ERROR_STOP=on --no-psqlrc --dbname=template1"
416bool exec_prog(const char *log_filename, const char *opt_log_file,
417 bool report_error, bool exit_on_error, const char *fmt,...) pg_attribute_printf(5, 6);
418void verify_directories(void);
419bool pid_lock_file_exists(const char *datadir);
424void cloneFile(const char *src, const char *dst,
425 const char *schemaName, const char *relName);
426void copyFile(const char *src, const char *dst,
427 const char *schemaName, const char *relName);
428void copyFileByRange(const char *src, const char *dst,
429 const char *schemaName, const char *relName);
430void linkFile(const char *src, const char *dst,
431 const char *schemaName, const char *relName);
432void rewriteVisibilityMap(const char *fromfile, const char *tofile,
433 const char *schemaName, const char *relName);
434void check_file_clone(void);
435void check_copy_file_range(void);
436void check_hard_link(transferMode transfer_mode);
438/* fopen_priv() is no longer different from fopen() */
439#define fopen_priv(path, mode) fopen(path, mode)
443void get_loadable_libraries(void);
444void check_loadable_libraries(void);
448FileNameMap *gen_db_file_maps(DbInfo *old_db,
449 DbInfo *new_db, int *nmaps, const char *old_pgdata,
450 const char *new_pgdata);
451void get_db_rel_and_slot_infos(ClusterInfo *cluster);
452int count_old_cluster_logical_slots(void);
453void get_subscription_info(ClusterInfo *cluster);
457void parseCommandLine(int argc, char *argv[]);
458void adjust_data_dir(ClusterInfo *cluster);
459void get_sock_dir(ClusterInfo *cluster);
463void transfer_all_new_tablespaces(DbInfoArr *old_db_arr,
464 DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata);
465void transfer_all_new_dbs(DbInfoArr *old_db_arr,
466 DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata,
467 char *old_tablespace, char *new_tablespace);
471void init_tablespaces(void);
476PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
477PGresult *executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2, 3);
479char *cluster_conn_opts(ClusterInfo *cluster);
481bool start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error);
482void stop_postmaster(bool in_atexit);
483void check_pghost_envvar(void);
488char *quote_identifier(const char *s);
489int get_user_info(char **user_name_p);
491void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
492void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
493pg_noreturn void pg_fatal(const char *fmt,...) pg_attribute_printf(1, 2);
494void end_progress_output(void);
495void cleanup_output_dirs(void);
496void prep_status(const char *fmt,...) pg_attribute_printf(1, 2);
497void prep_status_progress(const char *fmt,...) pg_attribute_printf(1, 2);
498unsigned int str2uint(const char *str);
503bool jsonb_9_4_check_applicable(ClusterInfo *cluster);
504void old_9_6_invalidate_hash_indexes(ClusterInfo *cluster,
507void report_extension_updates(ClusterInfo *cluster);
509/* multixact_rewrite.c */
510MultiXactOffset rewrite_multixacts(MultiXactId from_multi, MultiXactId to_multi);
513void parallel_exec_prog(const char *log_file, const char *opt_log_file,
514 const char *fmt,...) pg_attribute_printf(3, 4);
515void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
516 char *old_pgdata, char *new_pgdata,
517 char *old_tablespace, char *new_tablespace);
518bool reap_child(bool wait_for_child);
522typedef void (*UpgradeTaskProcessCB) (DbInfo *dbinfo, PGresult *res, void *arg);
524/* struct definition is private to task.c */
525typedef struct UpgradeTask UpgradeTask;
527UpgradeTask *upgrade_task_create(void);
528void upgrade_task_add_step(UpgradeTask *task, const char *query,
529 UpgradeTaskProcessCB process_cb, bool free_result,
531void upgrade_task_run(const UpgradeTask *task, const ClusterInfo *cluster);
532void upgrade_task_free(UpgradeTask *task);
534/* convenient type for common private data needed by several tasks */
538 char path[MAXPGPATH];