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 '\''
84 #define RM_CMD "rm -f"
85 #define RMDIR_CMD "rm -rf"
86 #define SCRIPT_PREFIX "./"
87 #define SCRIPT_EXT "sh"
88 #define ECHO_QUOTE "'"
91 #define pg_mv_file pgrename
92 #define PATH_SEPARATOR '\\'
93 #define PATH_QUOTE '"'
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 * large object chunk size added to pg_controldata,
119 * commit 5f93c37805e7485488480916b4585e098d3cc883
121 #define LARGE_OBJECT_SIZE_PG_CONTROL_VER 942
124 * change in JSONB format during 9.4 beta
126 #define JSONB_FORMAT_CHANGE_CAT_VER 201409291
130 * Each relation is represented by a relinfo structure.
134 /* Can't use NAMEDATALEN; not guaranteed to be same on client */
135 char *nspname; /* namespace name */
136 char *relname; /* relation name */
137 Oid reloid; /* relation OID */
138 RelFileNumber relfilenumber; /* relation file number */
139 Oid indtable; /* if index, OID of its table, else 0 */
140 Oid toastheap; /* if toast table, OID of base table, else 0 */
141 char *tablespace; /* tablespace path; "" for cluster default */
142 bool nsp_alloc; /* should nspname be freed? */
143 bool tblsp_alloc; /* should tablespace be freed? */
153 * Structure to store logical replication slot information.
157 char *slotname; /* slot name */
158 char *plugin; /* plugin */
159 bool two_phase; /* can the slot decode 2PC? */
160 bool caught_up; /* has the slot caught up to latest changes? */
161 bool invalid; /* if true, the slot is unusable */
162 bool failover; /* is the slot designated to be synced to the
163 * physical standby? */
168 int nslots; /* number of logical slot infos */
169 LogicalSlotInfo *slots; /* array of logical slot infos */
170 } LogicalSlotInfoArr;
173 * The following structure represents a relation mapping.
177 const char *old_tablespace;
178 const char *new_tablespace;
179 const char *old_tablespace_suffix;
180 const char *new_tablespace_suffix;
182 RelFileNumber relfilenumber;
183 /* the rest are used only for logging and error reporting */
184 char *nspname; /* namespaces */
189 * Structure to store database information
193 Oid db_oid; /* oid of the database */
194 char *db_name; /* database name */
195 char db_tablespace[MAXPGPATH]; /* database default tablespace
197 RelInfoArr rel_arr; /* array of all user relinfos */
198 LogicalSlotInfoArr slot_arr; /* array of all LogicalSlotInfo */
202 * Locale information about a database.
208 char db_collprovider;
215 DbInfo *dbs; /* array of db infos */
216 int ndbs; /* number of db infos */
220 * The following structure is used to hold pg_control information.
221 * Rather than using the backend's control structure we use our own
222 * structure to avoid pg_control version issues between releases.
228 char nextxlogfile[25];
229 uint32 chkpnt_nxtxid;
230 uint32 chkpnt_nxtepoch;
231 uint32 chkpnt_nxtoid;
232 uint32 chkpnt_nxtmulti;
233 uint32 chkpnt_nxtmxoff;
234 uint32 chkpnt_oldstMulti;
235 uint32 chkpnt_oldstxid;
246 bool float8_pass_by_value;
247 uint32 data_checksum_version;
251 * Enumeration to denote transfer modes
257 TRANSFER_MODE_COPY_FILE_RANGE,
262 * Enumeration to denote pg_log modes
267 PG_STATUS, /* these messages do not get a newline added */
268 PG_REPORT_NONL, /* these too */
278 * information about each cluster
282 ControlData controldata; /* pg_control information */
283 DbLocaleInfo *template0; /* template0 locale info */
284 DbInfoArr dbarr; /* dbinfos array */
285 char *pgdata; /* pathname for cluster's $PGDATA directory */
286 char *pgconfig; /* pathname for cluster's config file
288 char *bindir; /* pathname for cluster's executable directory */
289 char *pgopts; /* options to pass to the server, like pg_ctl
291 char *sockdir; /* directory for Unix Domain socket, if any */
292 unsigned short port; /* port number where postmaster is waiting */
293 uint32 major_version; /* PG_VERSION of cluster */
294 char major_version_str[64]; /* string PG_VERSION of cluster */
295 uint32 bin_version; /* version returned from pg_ctl */
296 const char *tablespace_suffix; /* directory specification */
297 int nsubs; /* number of subscriptions */
306 FILE *internal; /* internal log FILE */
307 bool verbose; /* true -> be verbose in messages */
308 bool retain; /* retain log files on success */
309 /* Set of internal directories for output files */
310 char *rootdir; /* Root directory, aka pg_upgrade_output.d */
311 char *basedir; /* Base output directory, with timestamp */
312 char *dumpdir; /* Dumps */
313 char *logdir; /* Log files */
314 bool isatty; /* is stdout a tty */
323 bool check; /* check clusters only, don't change any data */
324 bool live_check; /* check clusters only, old server is running */
325 bool do_sync; /* flush changes to disk */
326 transferMode transfer_mode; /* copy files or link them? */
327 int jobs; /* number of processes/threads to use */
328 char *socketdir; /* directory to use for Unix sockets */
343 const char *progname; /* complete pathname for this program */
344 char *user; /* username for clusters */
345 bool user_specified; /* user specified on command-line */
346 char **old_tablespaces; /* tablespaces */
347 int num_old_tablespaces;
348 LibraryInfo *libraries; /* loadable libraries */
350 ClusterInfo *running_cluster;
354 /* Function signature for data type check version hook */
355 typedef bool (*DataTypesUsageVersionCheck) (ClusterInfo *cluster);
360 extern LogOpts log_opts;
361 extern UserOpts user_opts;
362 extern ClusterInfo old_cluster,
364 extern OSInfo os_info;
369 void output_check_banner(void);
370 void check_and_dump_old_cluster(void);
371 void check_new_cluster(void);
372 void report_clusters_compatible(void);
373 void issue_warnings_and_set_wal_level(void);
374 void output_completion_banner(char *deletion_script_file_name);
375 void check_cluster_versions(void);
376 void check_cluster_compatibility(void);
377 void create_script_for_old_cluster_deletion(char **deletion_script_file_name);
382 void get_control_data(ClusterInfo *cluster);
383 void check_control_data(ControlData *oldctrl, ControlData *newctrl);
384 void disable_old_cluster(void);
389 void generate_old_dump(void);
394 #define EXEC_PSQL_ARGS "--echo-queries --set ON_ERROR_STOP=on --no-psqlrc --dbname=template1"
396 bool exec_prog(const char *log_filename, const char *opt_log_file,
397 bool report_error, bool exit_on_error, const char *fmt,...) pg_attribute_printf(5, 6);
398 void verify_directories(void);
399 bool pid_lock_file_exists(const char *datadir);
404 void cloneFile(const char *src, const char *dst,
405 const char *schemaName, const char *relName);
406 void copyFile(const char *src, const char *dst,
407 const char *schemaName, const char *relName);
408 void copyFileByRange(const char *src, const char *dst,
409 const char *schemaName, const char *relName);
410 void linkFile(const char *src, const char *dst,
411 const char *schemaName, const char *relName);
412 void rewriteVisibilityMap(const char *fromfile, const char *tofile,
413 const char *schemaName, const char *relName);
414 void check_file_clone(void);
415 void check_copy_file_range(void);
416 void check_hard_link(void);
418 /* fopen_priv() is no longer different from fopen() */
419 #define fopen_priv(path, mode) fopen(path, mode)
423 void get_loadable_libraries(void);
424 void check_loadable_libraries(void);
428 FileNameMap *gen_db_file_maps(DbInfo *old_db,
429 DbInfo *new_db, int *nmaps, const char *old_pgdata,
430 const char *new_pgdata);
431 void get_db_rel_and_slot_infos(ClusterInfo *cluster);
432 int count_old_cluster_logical_slots(void);
433 void get_subscription_count(ClusterInfo *cluster);
437 void parseCommandLine(int argc, char *argv[]);
438 void adjust_data_dir(ClusterInfo *cluster);
439 void get_sock_dir(ClusterInfo *cluster);
441 /* relfilenumber.c */
443 void transfer_all_new_tablespaces(DbInfoArr *old_db_arr,
444 DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata);
445 void transfer_all_new_dbs(DbInfoArr *old_db_arr,
446 DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata,
447 char *old_tablespace);
451 void init_tablespaces(void);
456 PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
457 PGresult *executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2, 3);
459 char *cluster_conn_opts(ClusterInfo *cluster);
461 bool start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error);
462 void stop_postmaster(bool in_atexit);
463 uint32 get_major_server_version(ClusterInfo *cluster);
464 void check_pghost_envvar(void);
469 char *quote_identifier(const char *s);
470 int get_user_info(char **user_name_p);
472 void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
473 void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
474 void pg_fatal(const char *fmt,...) pg_attribute_printf(1, 2) pg_attribute_noreturn();
475 void end_progress_output(void);
476 void cleanup_output_dirs(void);
477 void prep_status(const char *fmt,...) pg_attribute_printf(1, 2);
478 void prep_status_progress(const char *fmt,...) pg_attribute_printf(1, 2);
479 unsigned int str2uint(const char *str);
484 bool jsonb_9_4_check_applicable(ClusterInfo *cluster);
485 void old_9_6_invalidate_hash_indexes(ClusterInfo *cluster,
488 void report_extension_updates(ClusterInfo *cluster);
491 void parallel_exec_prog(const char *log_file, const char *opt_log_file,
492 const char *fmt,...) pg_attribute_printf(3, 4);
493 void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
494 char *old_pgdata, char *new_pgdata,
495 char *old_tablespace);
496 bool reap_child(bool wait_for_child);
500 typedef void (*UpgradeTaskProcessCB) (DbInfo *dbinfo, PGresult *res, void *arg);
502 /* struct definition is private to task.c */
503 typedef struct UpgradeTask UpgradeTask;
505 UpgradeTask *upgrade_task_create(void);
506 void upgrade_task_add_step(UpgradeTask *task, const char *query,
507 UpgradeTaskProcessCB process_cb, bool free_result,
509 void upgrade_task_run(const UpgradeTask *task, const ClusterInfo *cluster);
510 void upgrade_task_free(UpgradeTask *task);
512 /* convenient type for common private data needed by several tasks */
516 char path[MAXPGPATH];