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