PostgreSQL Source Code  git master
pg_upgrade.h
Go to the documentation of this file.
1 /*
2  * pg_upgrade.h
3  *
4  * Copyright (c) 2010-2023, PostgreSQL Global Development Group
5  * src/bin/pg_upgrade/pg_upgrade.h
6  */
7 
8 #include <unistd.h>
9 #include <assert.h>
10 #include <sys/stat.h>
11 #include <sys/time.h>
12 
13 #include "common/relpath.h"
14 #include "libpq-fe.h"
15 
16 /* For now, pg_upgrade does not use common/logging.c; use our own pg_fatal */
17 #undef pg_fatal
18 
19 /* Use port in the private/dynamic port number range */
20 #define DEF_PGUPORT 50432
21 
22 #define MAX_STRING 1024
23 #define QUERY_ALLOC 8192
24 
25 #define MESSAGE_WIDTH 60
26 
27 #define GET_MAJOR_VERSION(v) ((v) / 100)
28 
29 /* contains both global db information and CREATE DATABASE commands */
30 #define GLOBALS_DUMP_FILE "pg_upgrade_dump_globals.sql"
31 #define DB_DUMP_FILE_MASK "pg_upgrade_dump_%u.custom"
32 
33 /*
34  * Base directories that include all the files generated internally, from the
35  * root path of the new cluster. The paths are dynamically built as of
36  * BASE_OUTPUTDIR/$timestamp/{LOG_OUTPUTDIR,DUMP_OUTPUTDIR} to ensure their
37  * uniqueness in each run.
38  */
39 #define BASE_OUTPUTDIR "pg_upgrade_output.d"
40 #define LOG_OUTPUTDIR "log"
41 #define DUMP_OUTPUTDIR "dump"
42 
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"
47 
48 extern char *output_files[];
49 
50 /*
51  * WIN32 files do not accept writes from multiple processes
52  *
53  * On Win32, we can't send both pg_upgrade output and command output to the
54  * same file because we get the error: "The process cannot access the file
55  * because it is being used by another process." so send the pg_ctl
56  * command-line output to a new file, rather than into the server log file.
57  * Ideally we could use UTILITY_LOG_FILE for this, but some Windows platforms
58  * keep the pg_ctl output file open by the running postmaster, even after
59  * pg_ctl exits.
60  *
61  * We could use the Windows pgwin32_open() flags to allow shared file
62  * writes but is unclear how all other tools would use those flags, so
63  * we just avoid it and log a little differently on Windows; we adjust
64  * the error message appropriately.
65  */
66 #ifndef WIN32
67 #define SERVER_START_LOG_FILE SERVER_LOG_FILE
68 #define SERVER_STOP_LOG_FILE SERVER_LOG_FILE
69 #else
70 #define SERVER_START_LOG_FILE "pg_upgrade_server_start.log"
71 /*
72  * "pg_ctl start" keeps SERVER_START_LOG_FILE and SERVER_LOG_FILE open
73  * while the server is running, so we use UTILITY_LOG_FILE for "pg_ctl
74  * stop".
75  */
76 #define SERVER_STOP_LOG_FILE UTILITY_LOG_FILE
77 #endif
78 
79 
80 #ifndef WIN32
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 "'"
89 #define ECHO_BLANK ""
90 #else
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"
99 #define EXE_EXT ".exe"
100 #define ECHO_QUOTE ""
101 #define ECHO_BLANK "."
102 #endif
103 
104 
105 /*
106  * The format of visibility map was changed with this 9.6 commit.
107  */
108 #define VISIBILITY_MAP_FROZEN_BIT_CAT_VER 201603011
109 
110 /*
111  * pg_multixact format changed in 9.3 commit 0ac5ad5134f2769ccbaefec73844f85,
112  * ("Improve concurrency of foreign key locking") which also updated catalog
113  * version to this value. pg_upgrade behavior depends on whether old and new
114  * server versions are both newer than this, or only the new one is.
115  */
116 #define MULTIXACT_FORMATCHANGE_CAT_VER 201301231
117 
118 /*
119  * large object chunk size added to pg_controldata,
120  * commit 5f93c37805e7485488480916b4585e098d3cc883
121  */
122 #define LARGE_OBJECT_SIZE_PG_CONTROL_VER 942
123 
124 /*
125  * change in JSONB format during 9.4 beta
126  */
127 #define JSONB_FORMAT_CHANGE_CAT_VER 201409291
128 
129 
130 /*
131  * Each relation is represented by a relinfo structure.
132  */
133 typedef struct
134 {
135  /* Can't use NAMEDATALEN; not guaranteed to be same on client */
136  char *nspname; /* namespace name */
137  char *relname; /* relation name */
138  Oid reloid; /* relation OID */
139  RelFileNumber relfilenumber; /* relation file number */
140  Oid indtable; /* if index, OID of its table, else 0 */
141  Oid toastheap; /* if toast table, OID of base table, else 0 */
142  char *tablespace; /* tablespace path; "" for cluster default */
143  bool nsp_alloc; /* should nspname be freed? */
144  bool tblsp_alloc; /* should tablespace be freed? */
145 } RelInfo;
146 
147 typedef struct
148 {
149  RelInfo *rels;
150  int nrels;
151 } RelInfoArr;
152 
153 /*
154  * The following structure represents a relation mapping.
155  */
156 typedef struct
157 {
158  const char *old_tablespace;
159  const char *new_tablespace;
160  const char *old_tablespace_suffix;
161  const char *new_tablespace_suffix;
162  Oid db_oid;
163  RelFileNumber relfilenumber;
164  /* the rest are used only for logging and error reporting */
165  char *nspname; /* namespaces */
166  char *relname;
167 } FileNameMap;
168 
169 /*
170  * Structure to store database information
171  */
172 typedef struct
173 {
174  Oid db_oid; /* oid of the database */
175  char *db_name; /* database name */
176  char db_tablespace[MAXPGPATH]; /* database default tablespace
177  * path */
178  char *db_collate;
179  char *db_ctype;
180  char db_collprovider;
181  char *db_iculocale;
182  int db_encoding;
183  RelInfoArr rel_arr; /* array of all user relinfos */
184 } DbInfo;
185 
186 typedef struct
187 {
188  DbInfo *dbs; /* array of db infos */
189  int ndbs; /* number of db infos */
190 } DbInfoArr;
191 
192 /*
193  * The following structure is used to hold pg_control information.
194  * Rather than using the backend's control structure we use our own
195  * structure to avoid pg_control version issues between releases.
196  */
197 typedef struct
198 {
199  uint32 ctrl_ver;
200  uint32 cat_ver;
201  char nextxlogfile[25];
202  uint32 chkpnt_nxtxid;
203  uint32 chkpnt_nxtepoch;
204  uint32 chkpnt_nxtoid;
205  uint32 chkpnt_nxtmulti;
206  uint32 chkpnt_nxtmxoff;
207  uint32 chkpnt_oldstMulti;
208  uint32 chkpnt_oldstxid;
209  uint32 align;
210  uint32 blocksz;
211  uint32 largesz;
212  uint32 walsz;
213  uint32 walseg;
214  uint32 ident;
215  uint32 index;
216  uint32 toast;
217  uint32 large_object;
218  bool date_is_int;
219  bool float8_pass_by_value;
220  uint32 data_checksum_version;
221 } ControlData;
222 
223 /*
224  * Enumeration to denote transfer modes
225  */
226 typedef enum
227 {
228  TRANSFER_MODE_CLONE,
229  TRANSFER_MODE_COPY,
230  TRANSFER_MODE_LINK
231 } transferMode;
232 
233 /*
234  * Enumeration to denote pg_log modes
235  */
236 typedef enum
237 {
238  PG_VERBOSE,
239  PG_STATUS, /* these messages do not get a newline added */
240  PG_REPORT_NONL, /* these too */
241  PG_REPORT,
242  PG_WARNING,
243  PG_FATAL
244 } eLogType;
245 
246 
247 /*
248  * cluster
249  *
250  * information about each cluster
251  */
252 typedef struct
253 {
254  ControlData controldata; /* pg_control information */
255  DbInfoArr dbarr; /* dbinfos array */
256  char *pgdata; /* pathname for cluster's $PGDATA directory */
257  char *pgconfig; /* pathname for cluster's config file
258  * directory */
259  char *bindir; /* pathname for cluster's executable directory */
260  char *pgopts; /* options to pass to the server, like pg_ctl
261  * -o */
262  char *sockdir; /* directory for Unix Domain socket, if any */
263  unsigned short port; /* port number where postmaster is waiting */
264  uint32 major_version; /* PG_VERSION of cluster */
265  char major_version_str[64]; /* string PG_VERSION of cluster */
266  uint32 bin_version; /* version returned from pg_ctl */
267  const char *tablespace_suffix; /* directory specification */
268 } ClusterInfo;
269 
270 
271 /*
272  * LogOpts
273 */
274 typedef struct
275 {
276  FILE *internal; /* internal log FILE */
277  bool verbose; /* true -> be verbose in messages */
278  bool retain; /* retain log files on success */
279  /* Set of internal directories for output files */
280  char *rootdir; /* Root directory, aka pg_upgrade_output.d */
281  char *basedir; /* Base output directory, with timestamp */
282  char *dumpdir; /* Dumps */
283  char *logdir; /* Log files */
284  bool isatty; /* is stdout a tty */
285 } LogOpts;
286 
287 
288 /*
289  * UserOpts
290 */
291 typedef struct
292 {
293  bool check; /* true -> ask user for permission to make
294  * changes */
295  bool do_sync; /* flush changes to disk */
296  transferMode transfer_mode; /* copy files or link them? */
297  int jobs; /* number of processes/threads to use */
298  char *socketdir; /* directory to use for Unix sockets */
299 } UserOpts;
300 
301 typedef struct
302 {
303  char *name;
304  int dbnum;
305 } LibraryInfo;
306 
307 /*
308  * OSInfo
309  */
310 typedef struct
311 {
312  const char *progname; /* complete pathname for this program */
313  char *user; /* username for clusters */
314  bool user_specified; /* user specified on command-line */
315  char **old_tablespaces; /* tablespaces */
316  int num_old_tablespaces;
317  LibraryInfo *libraries; /* loadable libraries */
318  int num_libraries;
319  ClusterInfo *running_cluster;
320 } OSInfo;
321 
322 
323 /*
324  * Global variables
325  */
326 extern LogOpts log_opts;
327 extern UserOpts user_opts;
328 extern ClusterInfo old_cluster,
329  new_cluster;
330 extern OSInfo os_info;
331 
332 
333 /* check.c */
334 
335 void output_check_banner(bool live_check);
336 void check_and_dump_old_cluster(bool live_check);
337 void check_new_cluster(void);
338 void report_clusters_compatible(void);
339 void issue_warnings_and_set_wal_level(void);
340 void output_completion_banner(char *deletion_script_file_name);
341 void check_cluster_versions(void);
342 void check_cluster_compatibility(bool live_check);
343 void create_script_for_old_cluster_deletion(char **deletion_script_file_name);
344 
345 
346 /* controldata.c */
347 
348 void get_control_data(ClusterInfo *cluster, bool live_check);
349 void check_control_data(ControlData *oldctrl, ControlData *newctrl);
350 void disable_old_cluster(void);
351 
352 
353 /* dump.c */
354 
355 void generate_old_dump(void);
356 
357 
358 /* exec.c */
359 
360 #define EXEC_PSQL_ARGS "--echo-queries --set ON_ERROR_STOP=on --no-psqlrc --dbname=template1"
361 
362 bool exec_prog(const char *log_filename, const char *opt_log_file,
363  bool report_error, bool exit_on_error, const char *fmt,...) pg_attribute_printf(5, 6);
364 void verify_directories(void);
365 bool pid_lock_file_exists(const char *datadir);
366 
367 
368 /* file.c */
369 
370 void cloneFile(const char *src, const char *dst,
371  const char *schemaName, const char *relName);
372 void copyFile(const char *src, const char *dst,
373  const char *schemaName, const char *relName);
374 void linkFile(const char *src, const char *dst,
375  const char *schemaName, const char *relName);
376 void rewriteVisibilityMap(const char *fromfile, const char *tofile,
377  const char *schemaName, const char *relName);
378 void check_file_clone(void);
379 void check_hard_link(void);
380 
381 /* fopen_priv() is no longer different from fopen() */
382 #define fopen_priv(path, mode) fopen(path, mode)
383 
384 /* function.c */
385 
386 void get_loadable_libraries(void);
387 void check_loadable_libraries(void);
388 
389 /* info.c */
390 
391 FileNameMap *gen_db_file_maps(DbInfo *old_db,
392  DbInfo *new_db, int *nmaps, const char *old_pgdata,
393  const char *new_pgdata);
394 void get_db_and_rel_infos(ClusterInfo *cluster);
395 
396 /* option.c */
397 
398 void parseCommandLine(int argc, char *argv[]);
399 void adjust_data_dir(ClusterInfo *cluster);
400 void get_sock_dir(ClusterInfo *cluster, bool live_check);
401 
402 /* relfilenumber.c */
403 
404 void transfer_all_new_tablespaces(DbInfoArr *old_db_arr,
405  DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata);
406 void transfer_all_new_dbs(DbInfoArr *old_db_arr,
407  DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata,
408  char *old_tablespace);
409 
410 /* tablespace.c */
411 
412 void init_tablespaces(void);
413 
414 
415 /* server.c */
416 
417 PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
418 PGresult *executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2, 3);
419 
420 char *cluster_conn_opts(ClusterInfo *cluster);
421 
422 bool start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error);
423 void stop_postmaster(bool in_atexit);
424 uint32 get_major_server_version(ClusterInfo *cluster);
425 void check_pghost_envvar(void);
426 
427 
428 /* util.c */
429 
430 char *quote_identifier(const char *s);
431 int get_user_info(char **user_name_p);
432 void check_ok(void);
433 void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
434 void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
435 void pg_fatal(const char *fmt,...) pg_attribute_printf(1, 2) pg_attribute_noreturn();
436 void end_progress_output(void);
437 void cleanup_output_dirs(void);
438 void prep_status(const char *fmt,...) pg_attribute_printf(1, 2);
439 void prep_status_progress(const char *fmt,...) pg_attribute_printf(1, 2);
440 unsigned int str2uint(const char *str);
441 
442 
443 /* version.c */
444 
445 bool check_for_data_types_usage(ClusterInfo *cluster,
446  const char *base_query,
447  const char *output_path);
448 bool check_for_data_type_usage(ClusterInfo *cluster,
449  const char *type_name,
450  const char *output_path);
451 void old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster);
452 void old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster);
453 void old_9_6_invalidate_hash_indexes(ClusterInfo *cluster,
454  bool check_mode);
455 
456 void old_11_check_for_sql_identifier_data_type_usage(ClusterInfo *cluster);
457 void report_extension_updates(ClusterInfo *cluster);
458 
459 /* parallel.c */
460 void parallel_exec_prog(const char *log_file, const char *opt_log_file,
461  const char *fmt,...) pg_attribute_printf(3, 4);
462 void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
463  char *old_pgdata, char *new_pgdata,
464  char *old_tablespace);
465 bool reap_child(bool wait_for_child);
char * output_files[]
Definition: pg_upgrade.c:66