19 #define DEF_PGUPORT 50432
21 #define MAX_STRING 1024
22 #define QUERY_ALLOC 8192
24 #define MESSAGE_WIDTH 60
26 #define GET_MAJOR_VERSION(v) ((v) / 100)
29 #define GLOBALS_DUMP_FILE "pg_upgrade_dump_globals.sql"
30 #define DB_DUMP_FILE_MASK "pg_upgrade_dump_%u.custom"
38 #define BASE_OUTPUTDIR "pg_upgrade_output.d"
39 #define LOG_OUTPUTDIR "log"
40 #define DUMP_OUTPUTDIR "dump"
42 #define DB_DUMP_LOG_FILE_MASK "pg_upgrade_dump_%u.log"
43 #define SERVER_LOG_FILE "pg_upgrade_server.log"
44 #define UTILITY_LOG_FILE "pg_upgrade_utility.log"
45 #define INTERNAL_LOG_FILE "pg_upgrade_internal.log"
66 #define SERVER_START_LOG_FILE SERVER_LOG_FILE
67 #define SERVER_STOP_LOG_FILE SERVER_LOG_FILE
69 #define SERVER_START_LOG_FILE "pg_upgrade_server_start.log"
75 #define SERVER_STOP_LOG_FILE UTILITY_LOG_FILE
80 #define pg_mv_file rename
81 #define PATH_SEPARATOR '/'
82 #define PATH_QUOTE '\''
83 #define RM_CMD "rm -f"
84 #define RMDIR_CMD "rm -rf"
85 #define SCRIPT_PREFIX "./"
86 #define SCRIPT_EXT "sh"
87 #define ECHO_QUOTE "'"
90 #define pg_mv_file pgrename
91 #define PATH_SEPARATOR '\\'
92 #define PATH_QUOTE '"'
93 /* @ prefix disables command echo in .bat files */
94 #define RM_CMD "@DEL /q"
95 #define RMDIR_CMD "@RMDIR /s/q"
96 #define SCRIPT_PREFIX ""
97 #define SCRIPT_EXT "bat"
98 #define EXE_EXT ".exe"
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 Oid relfilenode; /* relation file node */
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 * The following structure represents a relation mapping.
157 const char *old_tablespace;
158 const char *new_tablespace;
159 const char *old_tablespace_suffix;
160 const char *new_tablespace_suffix;
163 /* the rest are used only for logging and error reporting */
164 char *nspname; /* namespaces */
169 * Structure to store database information
173 Oid db_oid; /* oid of the database */
174 char *db_name; /* database name */
175 char db_tablespace[MAXPGPATH]; /* database default tablespace
179 char db_collprovider;
182 RelInfoArr rel_arr; /* array of all user relinfos */
187 DbInfo *dbs; /* array of db infos */
188 int ndbs; /* number of db infos */
192 * The following structure is used to hold pg_control information.
193 * Rather than using the backend's control structure we use our own
194 * structure to avoid pg_control version issues between releases.
200 char nextxlogfile[25];
201 uint32 chkpnt_nxtxid;
202 uint32 chkpnt_nxtepoch;
203 uint32 chkpnt_nxtoid;
204 uint32 chkpnt_nxtmulti;
205 uint32 chkpnt_nxtmxoff;
206 uint32 chkpnt_oldstMulti;
207 uint32 chkpnt_oldstxid;
218 bool float8_pass_by_value;
219 bool data_checksum_version;
223 * Enumeration to denote transfer modes
233 * Enumeration to denote pg_log modes
245 typedef long pgpid_t;
251 * information about each cluster
255 ControlData controldata; /* pg_control information */
256 DbInfoArr dbarr; /* dbinfos array */
257 char *pgdata; /* pathname for cluster's $PGDATA directory */
258 char *pgconfig; /* pathname for cluster's config file
260 char *bindir; /* pathname for cluster's executable directory */
261 char *pgopts; /* options to pass to the server, like pg_ctl
263 char *sockdir; /* directory for Unix Domain socket, if any */
264 unsigned short port; /* port number where postmaster is waiting */
265 uint32 major_version; /* PG_VERSION of cluster */
266 char major_version_str[64]; /* string PG_VERSION of cluster */
267 uint32 bin_version; /* version returned from pg_ctl */
268 const char *tablespace_suffix; /* directory specification */
277 FILE *internal; /* internal log FILE */
278 bool verbose; /* true -> be verbose in messages */
279 bool retain; /* retain log files on success */
280 /* Set of internal directories for output files */
281 char *rootdir; /* Root directory, aka pg_upgrade_output.d */
282 char *basedir; /* Base output directory, with timestamp */
283 char *dumpdir; /* Dumps */
284 char *logdir; /* Log files */
285 bool isatty; /* is stdout a tty */
294 bool check; /* true -> ask user for permission to make
296 bool do_sync; /* flush changes to disk */
297 transferMode transfer_mode; /* copy files or link them? */
298 int jobs; /* number of processes/threads to use */
299 char *socketdir; /* directory to use for Unix sockets */
313 const char *progname; /* complete pathname for this program */
314 char *user; /* username for clusters */
315 bool user_specified; /* user specified on command-line */
316 char **old_tablespaces; /* tablespaces */
317 int num_old_tablespaces;
318 LibraryInfo *libraries; /* loadable libraries */
320 ClusterInfo *running_cluster;
327 extern LogOpts log_opts;
328 extern UserOpts user_opts;
329 extern ClusterInfo old_cluster,
331 extern OSInfo os_info;
336 void output_check_banner(bool live_check);
337 void check_and_dump_old_cluster(bool live_check);
338 void check_new_cluster(void);
339 void report_clusters_compatible(void);
340 void issue_warnings_and_set_wal_level(void);
341 void output_completion_banner(char *deletion_script_file_name);
342 void check_cluster_versions(void);
343 void check_cluster_compatibility(bool live_check);
344 void create_script_for_old_cluster_deletion(char **deletion_script_file_name);
349 void get_control_data(ClusterInfo *cluster, bool live_check);
350 void check_control_data(ControlData *oldctrl, ControlData *newctrl);
351 void disable_old_cluster(void);
356 void generate_old_dump(void);
361 #define EXEC_PSQL_ARGS "--echo-queries --set ON_ERROR_STOP=on --no-psqlrc --dbname=template1"
363 bool exec_prog(const char *log_file, const char *opt_log_file,
364 bool report_error, bool exit_on_error, const char *fmt,...) pg_attribute_printf(5, 6);
365 void verify_directories(void);
366 bool pid_lock_file_exists(const char *datadir);
371 void cloneFile(const char *src, const char *dst,
372 const char *schemaName, const char *relName);
373 void copyFile(const char *src, const char *dst,
374 const char *schemaName, const char *relName);
375 void linkFile(const char *src, const char *dst,
376 const char *schemaName, const char *relName);
377 void rewriteVisibilityMap(const char *fromfile, const char *tofile,
378 const char *schemaName, const char *relName);
379 void check_file_clone(void);
380 void check_hard_link(void);
382 /* fopen_priv() is no longer different from fopen() */
383 #define fopen_priv(path, mode) fopen(path, mode)
387 void get_loadable_libraries(void);
388 void check_loadable_libraries(void);
392 FileNameMap *gen_db_file_maps(DbInfo *old_db,
393 DbInfo *new_db, int *nmaps, const char *old_pgdata,
394 const char *new_pgdata);
395 void get_db_and_rel_infos(ClusterInfo *cluster);
399 void parseCommandLine(int argc, char *argv[]);
400 void adjust_data_dir(ClusterInfo *cluster);
401 void get_sock_dir(ClusterInfo *cluster, bool live_check);
405 void transfer_all_new_tablespaces(DbInfoArr *old_db_arr,
406 DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata);
407 void transfer_all_new_dbs(DbInfoArr *old_db_arr,
408 DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata,
409 char *old_tablespace);
413 void init_tablespaces(void);
418 PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
419 PGresult *executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2, 3);
421 char *cluster_conn_opts(ClusterInfo *cluster);
423 bool start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error);
424 void stop_postmaster(bool in_atexit);
425 uint32 get_major_server_version(ClusterInfo *cluster);
426 void check_pghost_envvar(void);
431 char *quote_identifier(const char *s);
432 int get_user_info(char **user_name_p);
434 void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
435 void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
436 void pg_fatal(const char *fmt,...) pg_attribute_printf(1, 2) pg_attribute_noreturn();
437 void end_progress_output(void);
438 void cleanup_output_dirs(void);
439 void prep_status(const char *fmt,...) pg_attribute_printf(1, 2);
440 void prep_status_progress(const char *fmt,...) pg_attribute_printf(1, 2);
441 unsigned int str2uint(const char *str);
446 bool check_for_data_types_usage(ClusterInfo *cluster,
447 const char *base_query,
448 const char *output_path);
449 bool check_for_data_type_usage(ClusterInfo *cluster,
450 const char *type_name,
451 const char *output_path);
452 void old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster);
453 void old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster);
454 void old_9_6_invalidate_hash_indexes(ClusterInfo *cluster,
457 void old_11_check_for_sql_identifier_data_type_usage(ClusterInfo *cluster);
458 void report_extension_updates(ClusterInfo *cluster);
461 void parallel_exec_prog(const char *log_file, const char *opt_log_file,
462 const char *fmt,...) pg_attribute_printf(3, 4);
463 void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
464 char *old_pgdata, char *new_pgdata,
465 char *old_tablespace);
466 bool reap_child(bool wait_for_child);