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-2024, 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 62
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 ECHO_QUOTE ""
100 #define ECHO_BLANK "."
101 #endif
102 
103 
104 /*
105  * The format of visibility map was changed with this 9.6 commit.
106  */
107 #define VISIBILITY_MAP_FROZEN_BIT_CAT_VER 201603011
108 
109 /*
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.
114  */
115 #define MULTIXACT_FORMATCHANGE_CAT_VER 201301231
116 
117 /*
118  * large object chunk size added to pg_controldata,
119  * commit 5f93c37805e7485488480916b4585e098d3cc883
120  */
121 #define LARGE_OBJECT_SIZE_PG_CONTROL_VER 942
122 
123 /*
124  * change in JSONB format during 9.4 beta
125  */
126 #define JSONB_FORMAT_CHANGE_CAT_VER 201409291
127 
128 
129 /*
130  * Each relation is represented by a relinfo structure.
131  */
132 typedef struct
133 {
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? */
144 } RelInfo;
145 
146 typedef struct
147 {
148  RelInfo *rels;
149  int nrels;
150 } RelInfoArr;
151 
152 /*
153  * Structure to store logical replication slot information.
154  */
155 typedef struct
156 {
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? */
164 } LogicalSlotInfo;
165 
166 typedef struct
167 {
168  int nslots; /* number of logical slot infos */
169  LogicalSlotInfo *slots; /* array of logical slot infos */
170 } LogicalSlotInfoArr;
171 
172 /*
173  * The following structure represents a relation mapping.
174  */
175 typedef struct
176 {
177  const char *old_tablespace;
178  const char *new_tablespace;
179  const char *old_tablespace_suffix;
180  const char *new_tablespace_suffix;
181  Oid db_oid;
182  RelFileNumber relfilenumber;
183  /* the rest are used only for logging and error reporting */
184  char *nspname; /* namespaces */
185  char *relname;
186 } FileNameMap;
187 
188 /*
189  * Structure to store database information
190  */
191 typedef struct
192 {
193  Oid db_oid; /* oid of the database */
194  char *db_name; /* database name */
195  char db_tablespace[MAXPGPATH]; /* database default tablespace
196  * path */
197  RelInfoArr rel_arr; /* array of all user relinfos */
198  LogicalSlotInfoArr slot_arr; /* array of all LogicalSlotInfo */
199 } DbInfo;
200 
201 /*
202  * Locale information about a database.
203  */
204 typedef struct
205 {
206  char *db_collate;
207  char *db_ctype;
208  char db_collprovider;
209  char *db_locale;
210  int db_encoding;
211 } DbLocaleInfo;
212 
213 typedef struct
214 {
215  DbInfo *dbs; /* array of db infos */
216  int ndbs; /* number of db infos */
217 } DbInfoArr;
218 
219 /*
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.
223  */
224 typedef struct
225 {
226  uint32 ctrl_ver;
227  uint32 cat_ver;
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;
236  uint32 align;
237  uint32 blocksz;
238  uint32 largesz;
239  uint32 walsz;
240  uint32 walseg;
241  uint32 ident;
242  uint32 index;
243  uint32 toast;
244  uint32 large_object;
245  bool date_is_int;
246  bool float8_pass_by_value;
247  uint32 data_checksum_version;
248 } ControlData;
249 
250 /*
251  * Enumeration to denote transfer modes
252  */
253 typedef enum
254 {
255  TRANSFER_MODE_CLONE,
256  TRANSFER_MODE_COPY,
257  TRANSFER_MODE_COPY_FILE_RANGE,
258  TRANSFER_MODE_LINK,
259 } transferMode;
260 
261 /*
262  * Enumeration to denote pg_log modes
263  */
264 typedef enum
265 {
266  PG_VERBOSE,
267  PG_STATUS, /* these messages do not get a newline added */
268  PG_REPORT_NONL, /* these too */
269  PG_REPORT,
270  PG_WARNING,
271  PG_FATAL,
272 } eLogType;
273 
274 
275 /*
276  * cluster
277  *
278  * information about each cluster
279  */
280 typedef struct
281 {
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
287  * directory */
288  char *bindir; /* pathname for cluster's executable directory */
289  char *pgopts; /* options to pass to the server, like pg_ctl
290  * -o */
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 */
298 } ClusterInfo;
299 
300 
301 /*
302  * LogOpts
303 */
304 typedef struct
305 {
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 */
315 } LogOpts;
316 
317 
318 /*
319  * UserOpts
320 */
321 typedef struct
322 {
323  bool check; /* check clusters only, don't change any data */
324  bool do_sync; /* flush changes to disk */
325  transferMode transfer_mode; /* copy files or link them? */
326  int jobs; /* number of processes/threads to use */
327  char *socketdir; /* directory to use for Unix sockets */
328  char *sync_method;
329 } UserOpts;
330 
331 typedef struct
332 {
333  char *name;
334  int dbnum;
335 } LibraryInfo;
336 
337 /*
338  * OSInfo
339  */
340 typedef struct
341 {
342  const char *progname; /* complete pathname for this program */
343  char *user; /* username for clusters */
344  bool user_specified; /* user specified on command-line */
345  char **old_tablespaces; /* tablespaces */
346  int num_old_tablespaces;
347  LibraryInfo *libraries; /* loadable libraries */
348  int num_libraries;
349  ClusterInfo *running_cluster;
350 } OSInfo;
351 
352 
353 /* Function signature for data type check version hook */
354 typedef bool (*DataTypesUsageVersionCheck) (ClusterInfo *cluster);
355 
356 /*
357  * Global variables
358  */
359 extern LogOpts log_opts;
360 extern UserOpts user_opts;
361 extern ClusterInfo old_cluster,
362  new_cluster;
363 extern OSInfo os_info;
364 
365 
366 /* check.c */
367 
368 void output_check_banner(bool live_check);
369 void check_and_dump_old_cluster(bool live_check);
370 void check_new_cluster(void);
371 void report_clusters_compatible(void);
372 void issue_warnings_and_set_wal_level(void);
373 void output_completion_banner(char *deletion_script_file_name);
374 void check_cluster_versions(void);
375 void check_cluster_compatibility(bool live_check);
376 void create_script_for_old_cluster_deletion(char **deletion_script_file_name);
377 
378 
379 /* controldata.c */
380 
381 void get_control_data(ClusterInfo *cluster, bool live_check);
382 void check_control_data(ControlData *oldctrl, ControlData *newctrl);
383 void disable_old_cluster(void);
384 
385 
386 /* dump.c */
387 
388 void generate_old_dump(void);
389 
390 
391 /* exec.c */
392 
393 #define EXEC_PSQL_ARGS "--echo-queries --set ON_ERROR_STOP=on --no-psqlrc --dbname=template1"
394 
395 bool exec_prog(const char *log_filename, const char *opt_log_file,
396  bool report_error, bool exit_on_error, const char *fmt,...) pg_attribute_printf(5, 6);
397 void verify_directories(void);
398 bool pid_lock_file_exists(const char *datadir);
399 
400 
401 /* file.c */
402 
403 void cloneFile(const char *src, const char *dst,
404  const char *schemaName, const char *relName);
405 void copyFile(const char *src, const char *dst,
406  const char *schemaName, const char *relName);
407 void copyFileByRange(const char *src, const char *dst,
408  const char *schemaName, const char *relName);
409 void linkFile(const char *src, const char *dst,
410  const char *schemaName, const char *relName);
411 void rewriteVisibilityMap(const char *fromfile, const char *tofile,
412  const char *schemaName, const char *relName);
413 void check_file_clone(void);
414 void check_copy_file_range(void);
415 void check_hard_link(void);
416 
417 /* fopen_priv() is no longer different from fopen() */
418 #define fopen_priv(path, mode) fopen(path, mode)
419 
420 /* function.c */
421 
422 void get_loadable_libraries(void);
423 void check_loadable_libraries(void);
424 
425 /* info.c */
426 
427 FileNameMap *gen_db_file_maps(DbInfo *old_db,
428  DbInfo *new_db, int *nmaps, const char *old_pgdata,
429  const char *new_pgdata);
430 void get_db_rel_and_slot_infos(ClusterInfo *cluster, bool live_check);
431 int count_old_cluster_logical_slots(void);
432 void get_subscription_count(ClusterInfo *cluster);
433 
434 /* option.c */
435 
436 void parseCommandLine(int argc, char *argv[]);
437 void adjust_data_dir(ClusterInfo *cluster);
438 void get_sock_dir(ClusterInfo *cluster, bool live_check);
439 
440 /* relfilenumber.c */
441 
442 void transfer_all_new_tablespaces(DbInfoArr *old_db_arr,
443  DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata);
444 void transfer_all_new_dbs(DbInfoArr *old_db_arr,
445  DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata,
446  char *old_tablespace);
447 
448 /* tablespace.c */
449 
450 void init_tablespaces(void);
451 
452 
453 /* server.c */
454 
455 PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
456 PGresult *executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2, 3);
457 
458 char *cluster_conn_opts(ClusterInfo *cluster);
459 
460 bool start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error);
461 void stop_postmaster(bool in_atexit);
462 uint32 get_major_server_version(ClusterInfo *cluster);
463 void check_pghost_envvar(void);
464 
465 
466 /* util.c */
467 
468 char *quote_identifier(const char *s);
469 int get_user_info(char **user_name_p);
470 void check_ok(void);
471 void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
472 void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
473 void pg_fatal(const char *fmt,...) pg_attribute_printf(1, 2) pg_attribute_noreturn();
474 void end_progress_output(void);
475 void cleanup_output_dirs(void);
476 void prep_status(const char *fmt,...) pg_attribute_printf(1, 2);
477 void prep_status_progress(const char *fmt,...) pg_attribute_printf(1, 2);
478 unsigned int str2uint(const char *str);
479 
480 
481 /* version.c */
482 
483 bool jsonb_9_4_check_applicable(ClusterInfo *cluster);
484 void old_9_6_invalidate_hash_indexes(ClusterInfo *cluster,
485  bool check_mode);
486 
487 void report_extension_updates(ClusterInfo *cluster);
488 
489 /* parallel.c */
490 void parallel_exec_prog(const char *log_file, const char *opt_log_file,
491  const char *fmt,...) pg_attribute_printf(3, 4);
492 void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
493  char *old_pgdata, char *new_pgdata,
494  char *old_tablespace);
495 bool reap_child(bool wait_for_child);
char * output_files[]
Definition: pg_upgrade.c:75