PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/pg_upgrade/pg_upgrade.h

https://github.com/bbt123/postgres
C Header | 494 lines | 304 code | 82 blank | 108 comment | 2 complexity | 17767f0fd6d4903d3512c1f303875bc0 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*
  2. * pg_upgrade.h
  3. *
  4. * Copyright (c) 2010-2014, PostgreSQL Global Development Group
  5. * contrib/pg_upgrade/pg_upgrade.h
  6. */
  7. #include <unistd.h>
  8. #include <assert.h>
  9. #include <sys/stat.h>
  10. #include <sys/time.h>
  11. #include "libpq-fe.h"
  12. /* Use port in the private/dynamic port number range */
  13. #define DEF_PGUPORT 50432
  14. /* Allocate for null byte */
  15. #define USER_NAME_SIZE 128
  16. #define MAX_STRING 1024
  17. #define LINE_ALLOC 4096
  18. #define QUERY_ALLOC 8192
  19. #define MIGRATOR_API_VERSION 1
  20. #define MESSAGE_WIDTH 60
  21. #define GET_MAJOR_VERSION(v) ((v) / 100)
  22. /* contains both global db information and CREATE DATABASE commands */
  23. #define GLOBALS_DUMP_FILE "pg_upgrade_dump_globals.sql"
  24. #define DB_DUMP_FILE_MASK "pg_upgrade_dump_%u.custom"
  25. #define DB_DUMP_LOG_FILE_MASK "pg_upgrade_dump_%u.log"
  26. #define SERVER_LOG_FILE "pg_upgrade_server.log"
  27. #define UTILITY_LOG_FILE "pg_upgrade_utility.log"
  28. #define INTERNAL_LOG_FILE "pg_upgrade_internal.log"
  29. extern char *output_files[];
  30. /*
  31. * WIN32 files do not accept writes from multiple processes
  32. *
  33. * On Win32, we can't send both pg_upgrade output and command output to the
  34. * same file because we get the error: "The process cannot access the file
  35. * because it is being used by another process." so send the pg_ctl
  36. * command-line output to a new file, rather than into the server log file.
  37. * Ideally we could use UTILITY_LOG_FILE for this, but some Windows platforms
  38. * keep the pg_ctl output file open by the running postmaster, even after
  39. * pg_ctl exits.
  40. *
  41. * We could use the Windows pgwin32_open() flags to allow shared file
  42. * writes but is unclear how all other tools would use those flags, so
  43. * we just avoid it and log a little differently on Windows; we adjust
  44. * the error message appropriately.
  45. */
  46. #ifndef WIN32
  47. #define SERVER_START_LOG_FILE SERVER_LOG_FILE
  48. #define SERVER_STOP_LOG_FILE SERVER_LOG_FILE
  49. #else
  50. #define SERVER_START_LOG_FILE "pg_upgrade_server_start.log"
  51. /*
  52. * "pg_ctl start" keeps SERVER_START_LOG_FILE and SERVER_LOG_FILE open
  53. * while the server is running, so we use UTILITY_LOG_FILE for "pg_ctl
  54. * stop".
  55. */
  56. #define SERVER_STOP_LOG_FILE UTILITY_LOG_FILE
  57. #endif
  58. #ifndef WIN32
  59. #define pg_copy_file copy_file
  60. #define pg_mv_file rename
  61. #define pg_link_file link
  62. #define PATH_SEPARATOR '/'
  63. #define RM_CMD "rm -f"
  64. #define RMDIR_CMD "rm -rf"
  65. #define SCRIPT_EXT "sh"
  66. #define ECHO_QUOTE "'"
  67. #define ECHO_BLANK ""
  68. #else
  69. #define pg_copy_file CopyFile
  70. #define pg_mv_file pgrename
  71. #define pg_link_file win32_pghardlink
  72. #define PATH_SEPARATOR '\\'
  73. #define RM_CMD "DEL /q"
  74. #define RMDIR_CMD "RMDIR /s/q"
  75. #define SCRIPT_EXT "bat"
  76. #define EXE_EXT ".exe"
  77. #define ECHO_QUOTE ""
  78. #define ECHO_BLANK "."
  79. #endif
  80. #define CLUSTER_NAME(cluster) ((cluster) == &old_cluster ? "old" : \
  81. (cluster) == &new_cluster ? "new" : "none")
  82. #define atooid(x) ((Oid) strtoul((x), NULL, 10))
  83. /* OID system catalog preservation added during PG 9.0 development */
  84. #define TABLE_SPACE_SUBDIRS_CAT_VER 201001111
  85. /* postmaster/postgres -b (binary_upgrade) flag added during PG 9.1 development */
  86. #define BINARY_UPGRADE_SERVER_FLAG_CAT_VER 201104251
  87. /*
  88. * Visibility map changed with this 9.2 commit,
  89. * 8f9fe6edce358f7904e0db119416b4d1080a83aa; pick later catalog version.
  90. */
  91. #define VISIBILITY_MAP_CRASHSAFE_CAT_VER 201107031
  92. /*
  93. * pg_multixact format changed in 9.3 commit 0ac5ad5134f2769ccbaefec73844f85,
  94. * ("Improve concurrency of foreign key locking") which also updated catalog
  95. * version to this value. pg_upgrade behavior depends on whether old and new
  96. * server versions are both newer than this, or only the new one is.
  97. */
  98. #define MULTIXACT_FORMATCHANGE_CAT_VER 201301231
  99. /*
  100. * Each relation is represented by a relinfo structure.
  101. */
  102. typedef struct
  103. {
  104. /* Can't use NAMEDATALEN; not guaranteed to fit on client */
  105. char *nspname; /* namespace name */
  106. char *relname; /* relation name */
  107. Oid reloid; /* relation oid */
  108. Oid relfilenode; /* relation relfile node */
  109. /* relation tablespace path, or "" for the cluster default */
  110. char *tablespace;
  111. bool nsp_alloc;
  112. bool tblsp_alloc;
  113. } RelInfo;
  114. typedef struct
  115. {
  116. RelInfo *rels;
  117. int nrels;
  118. } RelInfoArr;
  119. /*
  120. * The following structure represents a relation mapping.
  121. */
  122. typedef struct
  123. {
  124. const char *old_tablespace;
  125. const char *new_tablespace;
  126. const char *old_tablespace_suffix;
  127. const char *new_tablespace_suffix;
  128. Oid old_db_oid;
  129. Oid new_db_oid;
  130. /*
  131. * old/new relfilenodes might differ for pg_largeobject(_metadata) indexes
  132. * due to VACUUM FULL or REINDEX. Other relfilenodes are preserved.
  133. */
  134. Oid old_relfilenode;
  135. Oid new_relfilenode;
  136. /* the rest are used only for logging and error reporting */
  137. char *nspname; /* namespaces */
  138. char *relname;
  139. } FileNameMap;
  140. /*
  141. * Structure to store database information
  142. */
  143. typedef struct
  144. {
  145. Oid db_oid; /* oid of the database */
  146. char *db_name; /* database name */
  147. char db_tablespace[MAXPGPATH]; /* database default tablespace
  148. * path */
  149. RelInfoArr rel_arr; /* array of all user relinfos */
  150. } DbInfo;
  151. typedef struct
  152. {
  153. DbInfo *dbs; /* array of db infos */
  154. int ndbs; /* number of db infos */
  155. } DbInfoArr;
  156. /*
  157. * The following structure is used to hold pg_control information.
  158. * Rather than using the backend's control structure we use our own
  159. * structure to avoid pg_control version issues between releases.
  160. */
  161. typedef struct
  162. {
  163. uint32 ctrl_ver;
  164. uint32 cat_ver;
  165. char nextxlogfile[25];
  166. uint32 chkpnt_tli;
  167. uint32 chkpnt_nxtxid;
  168. uint32 chkpnt_nxtoid;
  169. uint32 chkpnt_nxtmulti;
  170. uint32 chkpnt_nxtmxoff;
  171. uint32 chkpnt_oldstMulti;
  172. uint32 align;
  173. uint32 blocksz;
  174. uint32 largesz;
  175. uint32 walsz;
  176. uint32 walseg;
  177. uint32 ident;
  178. uint32 index;
  179. uint32 toast;
  180. bool date_is_int;
  181. bool float8_pass_by_value;
  182. bool data_checksum_version;
  183. char *lc_collate;
  184. char *lc_ctype;
  185. char *encoding;
  186. } ControlData;
  187. /*
  188. * Enumeration to denote link modes
  189. */
  190. typedef enum
  191. {
  192. TRANSFER_MODE_COPY,
  193. TRANSFER_MODE_LINK
  194. } transferMode;
  195. /*
  196. * Enumeration to denote pg_log modes
  197. */
  198. typedef enum
  199. {
  200. PG_VERBOSE,
  201. PG_STATUS,
  202. PG_REPORT,
  203. PG_WARNING,
  204. PG_FATAL
  205. } eLogType;
  206. typedef long pgpid_t;
  207. /*
  208. * cluster
  209. *
  210. * information about each cluster
  211. */
  212. typedef struct
  213. {
  214. ControlData controldata; /* pg_control information */
  215. DbInfoArr dbarr; /* dbinfos array */
  216. char *pgdata; /* pathname for cluster's $PGDATA directory */
  217. char *pgconfig; /* pathname for cluster's config file
  218. * directory */
  219. char *bindir; /* pathname for cluster's executable directory */
  220. char *pgopts; /* options to pass to the server, like pg_ctl
  221. * -o */
  222. char *sockdir; /* directory for Unix Domain socket, if any */
  223. unsigned short port; /* port number where postmaster is waiting */
  224. uint32 major_version; /* PG_VERSION of cluster */
  225. char major_version_str[64]; /* string PG_VERSION of cluster */
  226. uint32 bin_version; /* version returned from pg_ctl */
  227. Oid pg_database_oid; /* OID of pg_database relation */
  228. Oid install_role_oid; /* OID of connected role */
  229. Oid role_count; /* number of roles defined in the cluster */
  230. const char *tablespace_suffix; /* directory specification */
  231. } ClusterInfo;
  232. /*
  233. * LogOpts
  234. */
  235. typedef struct
  236. {
  237. FILE *internal; /* internal log FILE */
  238. bool verbose; /* TRUE -> be verbose in messages */
  239. bool retain; /* retain log files on success */
  240. } LogOpts;
  241. /*
  242. * UserOpts
  243. */
  244. typedef struct
  245. {
  246. bool check; /* TRUE -> ask user for permission to make
  247. * changes */
  248. transferMode transfer_mode; /* copy files or link them? */
  249. int jobs;
  250. } UserOpts;
  251. /*
  252. * OSInfo
  253. */
  254. typedef struct
  255. {
  256. const char *progname; /* complete pathname for this program */
  257. char *exec_path; /* full path to my executable */
  258. char *user; /* username for clusters */
  259. bool user_specified; /* user specified on command-line */
  260. char **old_tablespaces; /* tablespaces */
  261. int num_old_tablespaces;
  262. char **libraries; /* loadable libraries */
  263. int num_libraries;
  264. ClusterInfo *running_cluster;
  265. } OSInfo;
  266. /*
  267. * Global variables
  268. */
  269. extern LogOpts log_opts;
  270. extern UserOpts user_opts;
  271. extern ClusterInfo old_cluster,
  272. new_cluster;
  273. extern OSInfo os_info;
  274. /* check.c */
  275. void output_check_banner(bool live_check);
  276. void check_and_dump_old_cluster(bool live_check,
  277. char **sequence_script_file_name);
  278. void check_new_cluster(void);
  279. void report_clusters_compatible(void);
  280. void issue_warnings(char *sequence_script_file_name);
  281. void output_completion_banner(char *analyze_script_file_name,
  282. char *deletion_script_file_name);
  283. void check_cluster_versions(void);
  284. void check_cluster_compatibility(bool live_check);
  285. void create_script_for_old_cluster_deletion(char **deletion_script_file_name);
  286. void create_script_for_cluster_analyze(char **analyze_script_file_name);
  287. /* controldata.c */
  288. void get_control_data(ClusterInfo *cluster, bool live_check);
  289. void check_control_data(ControlData *oldctrl, ControlData *newctrl);
  290. void disable_old_cluster(void);
  291. /* dump.c */
  292. void generate_old_dump(void);
  293. /* exec.c */
  294. #define EXEC_PSQL_ARGS "--echo-queries --set ON_ERROR_STOP=on --no-psqlrc --dbname=template1"
  295. bool
  296. exec_prog(const char *log_file, const char *opt_log_file,
  297. bool throw_error, const char *fmt,...)
  298. __attribute__((format(PG_PRINTF_ATTRIBUTE, 4, 5)));
  299. void verify_directories(void);
  300. bool pid_lock_file_exists(const char *datadir);
  301. /* file.c */
  302. #ifdef PAGE_CONVERSION
  303. typedef const char *(*pluginStartup) (uint16 migratorVersion,
  304. uint16 *pluginVersion, uint16 newPageVersion,
  305. uint16 oldPageVersion, void **pluginData);
  306. typedef const char *(*pluginConvertFile) (void *pluginData,
  307. const char *dstName, const char *srcName);
  308. typedef const char *(*pluginConvertPage) (void *pluginData,
  309. const char *dstPage, const char *srcPage);
  310. typedef const char *(*pluginShutdown) (void *pluginData);
  311. typedef struct
  312. {
  313. uint16 oldPageVersion; /* Page layout version of the old cluster */
  314. uint16 newPageVersion; /* Page layout version of the new cluster */
  315. uint16 pluginVersion; /* API version of converter plugin */
  316. void *pluginData; /* Plugin data (set by plugin) */
  317. pluginStartup startup; /* Pointer to plugin's startup function */
  318. pluginConvertFile convertFile; /* Pointer to plugin's file converter
  319. * function */
  320. pluginConvertPage convertPage; /* Pointer to plugin's page converter
  321. * function */
  322. pluginShutdown shutdown; /* Pointer to plugin's shutdown function */
  323. } pageCnvCtx;
  324. const pageCnvCtx *setupPageConverter(void);
  325. #else
  326. /* dummy */
  327. typedef void *pageCnvCtx;
  328. #endif
  329. const char *copyAndUpdateFile(pageCnvCtx *pageConverter, const char *src,
  330. const char *dst, bool force);
  331. const char *linkAndUpdateFile(pageCnvCtx *pageConverter, const char *src,
  332. const char *dst);
  333. void check_hard_link(void);
  334. FILE *fopen_priv(const char *path, const char *mode);
  335. /* function.c */
  336. void install_support_functions_in_new_db(const char *db_name);
  337. void uninstall_support_functions_from_new_cluster(void);
  338. void get_loadable_libraries(void);
  339. void check_loadable_libraries(void);
  340. /* info.c */
  341. FileNameMap *gen_db_file_maps(DbInfo *old_db,
  342. DbInfo *new_db, int *nmaps, const char *old_pgdata,
  343. const char *new_pgdata);
  344. void get_db_and_rel_infos(ClusterInfo *cluster);
  345. void print_maps(FileNameMap *maps, int n,
  346. const char *db_name);
  347. /* option.c */
  348. void parseCommandLine(int argc, char *argv[]);
  349. void adjust_data_dir(ClusterInfo *cluster);
  350. void get_sock_dir(ClusterInfo *cluster, bool live_check);
  351. /* relfilenode.c */
  352. void get_pg_database_relfilenode(ClusterInfo *cluster);
  353. void transfer_all_new_tablespaces(DbInfoArr *old_db_arr,
  354. DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata);
  355. void transfer_all_new_dbs(DbInfoArr *old_db_arr,
  356. DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata,
  357. char *old_tablespace);
  358. /* tablespace.c */
  359. void init_tablespaces(void);
  360. /* server.c */
  361. PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
  362. PGresult *
  363. executeQueryOrDie(PGconn *conn, const char *fmt,...)
  364. __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
  365. char *cluster_conn_opts(ClusterInfo *cluster);
  366. bool start_postmaster(ClusterInfo *cluster, bool throw_error);
  367. void stop_postmaster(bool fast);
  368. uint32 get_major_server_version(ClusterInfo *cluster);
  369. void check_pghost_envvar(void);
  370. /* util.c */
  371. char *quote_identifier(const char *s);
  372. int get_user_info(char **user_name_p);
  373. void check_ok(void);
  374. void
  375. report_status(eLogType type, const char *fmt,...)
  376. __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
  377. void
  378. pg_log(eLogType type, const char *fmt,...)
  379. __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
  380. void
  381. pg_fatal(const char *fmt,...)
  382. __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2), noreturn));
  383. void end_progress_output(void);
  384. void
  385. prep_status(const char *fmt,...)
  386. __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
  387. void check_ok(void);
  388. const char *getErrorText(int errNum);
  389. unsigned int str2uint(const char *str);
  390. void pg_putenv(const char *var, const char *val);
  391. /* version.c */
  392. void new_9_0_populate_pg_largeobject_metadata(ClusterInfo *cluster,
  393. bool check_mode);
  394. void old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster);
  395. /* version_old_8_3.c */
  396. void old_8_3_check_for_name_data_type_usage(ClusterInfo *cluster);
  397. void old_8_3_check_for_tsquery_usage(ClusterInfo *cluster);
  398. void old_8_3_check_ltree_usage(ClusterInfo *cluster);
  399. void old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode);
  400. void old_8_3_invalidate_hash_gin_indexes(ClusterInfo *cluster, bool check_mode);
  401. void old_8_3_invalidate_bpchar_pattern_ops_indexes(ClusterInfo *cluster,
  402. bool check_mode);
  403. char *old_8_3_create_sequence_script(ClusterInfo *cluster);
  404. /* parallel.c */
  405. void
  406. parallel_exec_prog(const char *log_file, const char *opt_log_file,
  407. const char *fmt,...)
  408. __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
  409. void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
  410. char *old_pgdata, char *new_pgdata,
  411. char *old_tablespace);
  412. bool reap_child(bool wait_for_child);