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-2022, 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 "libpq-fe.h"
14 
15 /* For now, pg_upgrade does not use common/logging.c; use our own pg_fatal */
16 #undef pg_fatal
17 
18 /* Use port in the private/dynamic port number range */
19 #define DEF_PGUPORT 50432
20 
21 #define MAX_STRING 1024
22 #define QUERY_ALLOC 8192
23 
24 #define MESSAGE_WIDTH 60
25 
26 #define GET_MAJOR_VERSION(v) ((v) / 100)
27 
28 /* contains both global db information and CREATE DATABASE commands */
29 #define GLOBALS_DUMP_FILE "pg_upgrade_dump_globals.sql"
30 #define DB_DUMP_FILE_MASK "pg_upgrade_dump_%u.custom"
31 
32 /*
33  * Base directories that include all the files generated internally, from the
34  * root path of the new cluster. The paths are dynamically built as of
35  * BASE_OUTPUTDIR/$timestamp/{LOG_OUTPUTDIR,DUMP_OUTPUTDIR} to ensure their
36  * uniqueness in each run.
37  */
38 #define BASE_OUTPUTDIR "pg_upgrade_output.d"
39 #define LOG_OUTPUTDIR "log"
40 #define DUMP_OUTPUTDIR "dump"
41 
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"
46 
47 extern char *output_files[];
48 
49 /*
50  * WIN32 files do not accept writes from multiple processes
51  *
52  * On Win32, we can't send both pg_upgrade output and command output to the
53  * same file because we get the error: "The process cannot access the file
54  * because it is being used by another process." so send the pg_ctl
55  * command-line output to a new file, rather than into the server log file.
56  * Ideally we could use UTILITY_LOG_FILE for this, but some Windows platforms
57  * keep the pg_ctl output file open by the running postmaster, even after
58  * pg_ctl exits.
59  *
60  * We could use the Windows pgwin32_open() flags to allow shared file
61  * writes but is unclear how all other tools would use those flags, so
62  * we just avoid it and log a little differently on Windows; we adjust
63  * the error message appropriately.
64  */
65 #ifndef WIN32
66 #define SERVER_START_LOG_FILE SERVER_LOG_FILE
67 #define SERVER_STOP_LOG_FILE SERVER_LOG_FILE
68 #else
69 #define SERVER_START_LOG_FILE "pg_upgrade_server_start.log"
70 /*
71  * "pg_ctl start" keeps SERVER_START_LOG_FILE and SERVER_LOG_FILE open
72  * while the server is running, so we use UTILITY_LOG_FILE for "pg_ctl
73  * stop".
74  */
75 #define SERVER_STOP_LOG_FILE UTILITY_LOG_FILE
76 #endif
77 
78 
79 #ifndef WIN32
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 "'"
88 #define ECHO_BLANK ""
89 #else
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"
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  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? */
144 } RelInfo;
145 
146 typedef struct
147 {
148  RelInfo *rels;
149  int nrels;
150 } RelInfoArr;
151 
152 /*
153  * The following structure represents a relation mapping.
154  */
155 typedef struct
156 {
157  const char *old_tablespace;
158  const char *new_tablespace;
159  const char *old_tablespace_suffix;
160  const char *new_tablespace_suffix;
161  Oid db_oid;
162  Oid relfilenode;
163  /* the rest are used only for logging and error reporting */
164  char *nspname; /* namespaces */
165  char *relname;
166 } FileNameMap;
167 
168 /*
169  * Structure to store database information
170  */
171 typedef struct
172 {
173  Oid db_oid; /* oid of the database */
174  char *db_name; /* database name */
175  char db_tablespace[MAXPGPATH]; /* database default tablespace
176  * path */
177  char *db_collate;
178  char *db_ctype;
179  char db_collprovider;
180  char *db_iculocale;
181  int db_encoding;
182  RelInfoArr rel_arr; /* array of all user relinfos */
183 } DbInfo;
184 
185 typedef struct
186 {
187  DbInfo *dbs; /* array of db infos */
188  int ndbs; /* number of db infos */
189 } DbInfoArr;
190 
191 /*
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.
195  */
196 typedef struct
197 {
198  uint32 ctrl_ver;
199  uint32 cat_ver;
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;
208  uint32 align;
209  uint32 blocksz;
210  uint32 largesz;
211  uint32 walsz;
212  uint32 walseg;
213  uint32 ident;
214  uint32 index;
215  uint32 toast;
216  uint32 large_object;
217  bool date_is_int;
218  bool float8_pass_by_value;
219  bool data_checksum_version;
220 } ControlData;
221 
222 /*
223  * Enumeration to denote transfer modes
224  */
225 typedef enum
226 {
227  TRANSFER_MODE_CLONE,
228  TRANSFER_MODE_COPY,
229  TRANSFER_MODE_LINK
230 } transferMode;
231 
232 /*
233  * Enumeration to denote pg_log modes
234  */
235 typedef enum
236 {
237  PG_VERBOSE,
238  PG_STATUS,
239  PG_REPORT,
240  PG_WARNING,
241  PG_FATAL
242 } eLogType;
243 
244 
245 typedef long pgpid_t;
246 
247 
248 /*
249  * cluster
250  *
251  * information about each cluster
252  */
253 typedef struct
254 {
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
259  * directory */
260  char *bindir; /* pathname for cluster's executable directory */
261  char *pgopts; /* options to pass to the server, like pg_ctl
262  * -o */
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 */
269 } ClusterInfo;
270 
271 
272 /*
273  * LogOpts
274 */
275 typedef struct
276 {
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 */
286 } LogOpts;
287 
288 
289 /*
290  * UserOpts
291 */
292 typedef struct
293 {
294  bool check; /* true -> ask user for permission to make
295  * changes */
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 */
300 } UserOpts;
301 
302 typedef struct
303 {
304  char *name;
305  int dbnum;
306 } LibraryInfo;
307 
308 /*
309  * OSInfo
310  */
311 typedef struct
312 {
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 */
319  int num_libraries;
320  ClusterInfo *running_cluster;
321 } OSInfo;
322 
323 
324 /*
325  * Global variables
326  */
327 extern LogOpts log_opts;
328 extern UserOpts user_opts;
329 extern ClusterInfo old_cluster,
330  new_cluster;
331 extern OSInfo os_info;
332 
333 
334 /* check.c */
335 
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);
345 
346 
347 /* controldata.c */
348 
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);
352 
353 
354 /* dump.c */
355 
356 void generate_old_dump(void);
357 
358 
359 /* exec.c */
360 
361 #define EXEC_PSQL_ARGS "--echo-queries --set ON_ERROR_STOP=on --no-psqlrc --dbname=template1"
362 
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);
367 
368 
369 /* file.c */
370 
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);
381 
382 /* fopen_priv() is no longer different from fopen() */
383 #define fopen_priv(path, mode) fopen(path, mode)
384 
385 /* function.c */
386 
387 void get_loadable_libraries(void);
388 void check_loadable_libraries(void);
389 
390 /* info.c */
391 
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);
396 
397 /* option.c */
398 
399 void parseCommandLine(int argc, char *argv[]);
400 void adjust_data_dir(ClusterInfo *cluster);
401 void get_sock_dir(ClusterInfo *cluster, bool live_check);
402 
403 /* relfilenode.c */
404 
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);
410 
411 /* tablespace.c */
412 
413 void init_tablespaces(void);
414 
415 
416 /* server.c */
417 
418 PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
419 PGresult *executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2, 3);
420 
421 char *cluster_conn_opts(ClusterInfo *cluster);
422 
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);
427 
428 
429 /* util.c */
430 
431 char *quote_identifier(const char *s);
432 int get_user_info(char **user_name_p);
433 void check_ok(void);
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);
442 
443 
444 /* version.c */
445 
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,
455  bool check_mode);
456 
457 void old_11_check_for_sql_identifier_data_type_usage(ClusterInfo *cluster);
458 void report_extension_updates(ClusterInfo *cluster);
459 
460 /* parallel.c */
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);
char * output_files[]
Definition: pg_upgrade.c:66