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