Debian Codegraph @

src/bin/pg_upgrade/pg_upgrade.h

raw
1 /*
2 * pg_upgrade.h
3 *
4 * Copyright (c) 2010-2025, 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 * The control file was changed to have the default char signedness,
130 * commit 44fe30fdab6746a287163e7cc093fd36cda8eb92
131 */
132 #define DEFAULT_CHAR_SIGNEDNESS_CAT_VER 202502212
133
134 /*
135 * Each relation is represented by a relinfo structure.
136 */
137 typedef struct
138 {
139 /* Can't use NAMEDATALEN; not guaranteed to be same on client */
140 char *nspname; /* namespace name */
141 char *relname; /* relation name */
142 Oid reloid; /* relation OID */
143 RelFileNumber relfilenumber; /* relation file number */
144 Oid indtable; /* if index, OID of its table, else 0 */
145 Oid toastheap; /* if toast table, OID of base table, else 0 */
146 char *tablespace; /* tablespace path; "" for cluster default */
147 bool nsp_alloc; /* should nspname be freed? */
148 bool tblsp_alloc; /* should tablespace be freed? */
149 } RelInfo;
150
151 typedef struct
152 {
153 RelInfo *rels;
154 int nrels;
155 } RelInfoArr;
156
157 /*
158 * Structure to store logical replication slot information.
159 */
160 typedef struct
161 {
162 char *slotname; /* slot name */
163 char *plugin; /* plugin */
164 bool two_phase; /* can the slot decode 2PC? */
165 bool caught_up; /* has the slot caught up to latest changes? */
166 bool invalid; /* if true, the slot is unusable */
167 bool failover; /* is the slot designated to be synced to the
168 * physical standby? */
169 } LogicalSlotInfo;
170
171 typedef struct
172 {
173 int nslots; /* number of logical slot infos */
174 LogicalSlotInfo *slots; /* array of logical slot infos */
175 } LogicalSlotInfoArr;
176
177 /*
178 * The following structure represents a relation mapping.
179 */
180 typedef struct
181 {
182 const char *old_tablespace;
183 const char *new_tablespace;
184 const char *old_tablespace_suffix;
185 const char *new_tablespace_suffix;
186 Oid db_oid;
187 RelFileNumber relfilenumber;
188 /* the rest are used only for logging and error reporting */
189 char *nspname; /* namespaces */
190 char *relname;
191 } FileNameMap;
192
193 /*
194 * Structure to store database information
195 */
196 typedef struct
197 {
198 Oid db_oid; /* oid of the database */
199 char *db_name; /* database name */
200 char db_tablespace[MAXPGPATH]; /* database default tablespace
201 * path */
202 RelInfoArr rel_arr; /* array of all user relinfos */
203 LogicalSlotInfoArr slot_arr; /* array of all LogicalSlotInfo */
204 } DbInfo;
205
206 /*
207 * Locale information about a database.
208 */
209 typedef struct
210 {
211 char *db_collate;
212 char *db_ctype;
213 char db_collprovider;
214 char *db_locale;
215 int db_encoding;
216 } DbLocaleInfo;
217
218 typedef struct
219 {
220 DbInfo *dbs; /* array of db infos */
221 int ndbs; /* number of db infos */
222 } DbInfoArr;
223
224 /*
225 * The following structure is used to hold pg_control information.
226 * Rather than using the backend's control structure we use our own
227 * structure to avoid pg_control version issues between releases.
228 */
229 typedef struct
230 {
231 uint32 ctrl_ver;
232 uint32 cat_ver;
233 char nextxlogfile[25];
234 uint32 chkpnt_nxtxid;
235 uint32 chkpnt_nxtepoch;
236 uint32 chkpnt_nxtoid;
237 uint32 chkpnt_nxtmulti;
238 uint32 chkpnt_nxtmxoff;
239 uint32 chkpnt_oldstMulti;
240 uint32 chkpnt_oldstxid;
241 uint32 align;
242 uint32 blocksz;
243 uint32 largesz;
244 uint32 walsz;
245 uint32 walseg;
246 uint32 ident;
247 uint32 index;
248 uint32 toast;
249 uint32 large_object;
250 bool date_is_int;
251 bool float8_pass_by_value;
252 uint32 data_checksum_version;
253 bool default_char_signedness;
254 } ControlData;
255
256 /*
257 * Enumeration to denote transfer modes
258 */
259 typedef enum
260 {
261 TRANSFER_MODE_CLONE,
262 TRANSFER_MODE_COPY,
263 TRANSFER_MODE_COPY_FILE_RANGE,
264 TRANSFER_MODE_LINK,
265 TRANSFER_MODE_SWAP,
266 } transferMode;
267
268 /*
269 * Enumeration to denote pg_log modes
270 */
271 typedef enum
272 {
273 PG_VERBOSE,
274 PG_STATUS, /* these messages do not get a newline added */
275 PG_REPORT_NONL, /* these too */
276 PG_REPORT,
277 PG_WARNING,
278 PG_FATAL,
279 } eLogType;
280
281
282 /*
283 * cluster
284 *
285 * information about each cluster
286 */
287 typedef struct
288 {
289 ControlData controldata; /* pg_control information */
290 DbLocaleInfo *template0; /* template0 locale info */
291 DbInfoArr dbarr; /* dbinfos array */
292 char *pgdata; /* pathname for cluster's $PGDATA directory */
293 char *pgconfig; /* pathname for cluster's config file
294 * directory */
295 char *bindir; /* pathname for cluster's executable directory */
296 char *pgopts; /* options to pass to the server, like pg_ctl
297 * -o */
298 char *sockdir; /* directory for Unix Domain socket, if any */
299 unsigned short port; /* port number where postmaster is waiting */
300 uint32 major_version; /* PG_VERSION of cluster */
301 char major_version_str[64]; /* string PG_VERSION of cluster */
302 uint32 bin_version; /* version returned from pg_ctl */
303 const char *tablespace_suffix; /* directory specification */
304 int nsubs; /* number of subscriptions */
305 } ClusterInfo;
306
307
308 /*
309 * LogOpts
310 */
311 typedef struct
312 {
313 FILE *internal; /* internal log FILE */
314 bool verbose; /* true -> be verbose in messages */
315 bool retain; /* retain log files on success */
316 /* Set of internal directories for output files */
317 char *rootdir; /* Root directory, aka pg_upgrade_output.d */
318 char *basedir; /* Base output directory, with timestamp */
319 char *dumpdir; /* Dumps */
320 char *logdir; /* Log files */
321 bool isatty; /* is stdout a tty */
322 } LogOpts;
323
324
325 /*
326 * UserOpts
327 */
328 typedef struct
329 {
330 bool check; /* check clusters only, don't change any data */
331 bool live_check; /* check clusters only, old server is running */
332 bool do_sync; /* flush changes to disk */
333 transferMode transfer_mode; /* copy files or link them? */
334 int jobs; /* number of processes/threads to use */
335 char *socketdir; /* directory to use for Unix sockets */
336 char *sync_method;
337 bool do_statistics; /* carry over statistics from old cluster */
338 int char_signedness; /* default char signedness: -1 for initial
339 * value, 1 for "signed" and 0 for
340 * "unsigned" */
341 } UserOpts;
342
343 typedef struct
344 {
345 char *name;
346 int dbnum;
347 } LibraryInfo;
348
349 /*
350 * OSInfo
351 */
352 typedef struct
353 {
354 const char *progname; /* complete pathname for this program */
355 char *user; /* username for clusters */
356 bool user_specified; /* user specified on command-line */
357 char **old_tablespaces; /* tablespaces */
358 int num_old_tablespaces;
359 LibraryInfo *libraries; /* loadable libraries */
360 int num_libraries;
361 ClusterInfo *running_cluster;
362 } OSInfo;
363
364
365 /* Function signature for data type check version hook */
366 typedef bool (*DataTypesUsageVersionCheck) (ClusterInfo *cluster);
367
368 /*
369 * Global variables
370 */
371 extern LogOpts log_opts;
372 extern UserOpts user_opts;
373 extern ClusterInfo old_cluster,
374 new_cluster;
375 extern OSInfo os_info;
376
377
378 /* check.c */
379
380 void output_check_banner(void);
381 void check_and_dump_old_cluster(void);
382 void check_new_cluster(void);
383 void report_clusters_compatible(void);
384 void issue_warnings_and_set_wal_level(void);
385 void output_completion_banner(char *deletion_script_file_name);
386 void check_cluster_versions(void);
387 void check_cluster_compatibility(void);
388 void create_script_for_old_cluster_deletion(char **deletion_script_file_name);
389
390
391 /* controldata.c */
392
393 void get_control_data(ClusterInfo *cluster);
394 void check_control_data(ControlData *oldctrl, ControlData *newctrl);
395 void disable_old_cluster(transferMode transfer_mode);
396
397
398 /* dump.c */
399
400 void generate_old_dump(void);
401
402
403 /* exec.c */
404
405 #define EXEC_PSQL_ARGS "--echo-queries --set ON_ERROR_STOP=on --no-psqlrc --dbname=template1"
406
407 bool exec_prog(const char *log_filename, const char *opt_log_file,
408 bool report_error, bool exit_on_error, const char *fmt,...) pg_attribute_printf(5, 6);
409 void verify_directories(void);
410 bool pid_lock_file_exists(const char *datadir);
411
412
413 /* file.c */
414
415 void cloneFile(const char *src, const char *dst,
416 const char *schemaName, const char *relName);
417 void copyFile(const char *src, const char *dst,
418 const char *schemaName, const char *relName);
419 void copyFileByRange(const char *src, const char *dst,
420 const char *schemaName, const char *relName);
421 void linkFile(const char *src, const char *dst,
422 const char *schemaName, const char *relName);
423 void rewriteVisibilityMap(const char *fromfile, const char *tofile,
424 const char *schemaName, const char *relName);
425 void check_file_clone(void);
426 void check_copy_file_range(void);
427 void check_hard_link(transferMode transfer_mode);
428
429 /* fopen_priv() is no longer different from fopen() */
430 #define fopen_priv(path, mode) fopen(path, mode)
431
432 /* function.c */
433
434 void get_loadable_libraries(void);
435 void check_loadable_libraries(void);
436
437 /* info.c */
438
439 FileNameMap *gen_db_file_maps(DbInfo *old_db,
440 DbInfo *new_db, int *nmaps, const char *old_pgdata,
441 const char *new_pgdata);
442 void get_db_rel_and_slot_infos(ClusterInfo *cluster);
443 int count_old_cluster_logical_slots(void);
444 void get_subscription_count(ClusterInfo *cluster);
445
446 /* option.c */
447
448 void parseCommandLine(int argc, char *argv[]);
449 void adjust_data_dir(ClusterInfo *cluster);
450 void get_sock_dir(ClusterInfo *cluster);
451
452 /* relfilenumber.c */
453
454 void transfer_all_new_tablespaces(DbInfoArr *old_db_arr,
455 DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata);
456 void transfer_all_new_dbs(DbInfoArr *old_db_arr,
457 DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata,
458 char *old_tablespace);
459
460 /* tablespace.c */
461
462 void init_tablespaces(void);
463
464
465 /* server.c */
466
467 PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
468 PGresult *executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2, 3);
469
470 char *cluster_conn_opts(ClusterInfo *cluster);
471
472 bool start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error);
473 void stop_postmaster(bool in_atexit);
474 uint32 get_major_server_version(ClusterInfo *cluster);
475 void check_pghost_envvar(void);
476
477
478 /* util.c */
479
480 char *quote_identifier(const char *s);
481 int get_user_info(char **user_name_p);
482 void check_ok(void);
483 void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
484 void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2, 3);
485 pg_noreturn void pg_fatal(const char *fmt,...) pg_attribute_printf(1, 2);
486 void end_progress_output(void);
487 void cleanup_output_dirs(void);
488 void prep_status(const char *fmt,...) pg_attribute_printf(1, 2);
489 void prep_status_progress(const char *fmt,...) pg_attribute_printf(1, 2);
490 unsigned int str2uint(const char *str);
491
492
493 /* version.c */
494
495 bool jsonb_9_4_check_applicable(ClusterInfo *cluster);
496 bool protocol_negotiation_supported(const ClusterInfo *cluster);
497 void old_9_6_invalidate_hash_indexes(ClusterInfo *cluster,
498 bool check_mode);
499
500 void report_extension_updates(ClusterInfo *cluster);
501
502 /* parallel.c */
503 void parallel_exec_prog(const char *log_file, const char *opt_log_file,
504 const char *fmt,...) pg_attribute_printf(3, 4);
505 void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
506 char *old_pgdata, char *new_pgdata,
507 char *old_tablespace);
508 bool reap_child(bool wait_for_child);
509
510 /* task.c */
511
512 typedef void (*UpgradeTaskProcessCB) (DbInfo *dbinfo, PGresult *res, void *arg);
513
514 /* struct definition is private to task.c */
515 typedef struct UpgradeTask UpgradeTask;
516
517 UpgradeTask *upgrade_task_create(void);
518 void upgrade_task_add_step(UpgradeTask *task, const char *query,
519 UpgradeTaskProcessCB process_cb, bool free_result,
520 void *arg);
521 void upgrade_task_run(const UpgradeTask *task, const ClusterInfo *cluster);
522 void upgrade_task_free(UpgradeTask *task);
523
524 /* convenient type for common private data needed by several tasks */
525 typedef struct
526 {
527 FILE *file;
528 char path[MAXPGPATH];
529 } UpgradeTaskReport;