2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* pg_upgrade.h
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2011-01-02 02:18:15 +08:00
|
|
|
* Copyright (c) 2010-2011, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/pg_upgrade.h
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "libpq-fe.h"
|
|
|
|
|
|
|
|
/* Allocate for null byte */
|
|
|
|
#define USER_NAME_SIZE 128
|
|
|
|
|
|
|
|
#define MAX_STRING 1024
|
|
|
|
#define LINE_ALLOC 4096
|
|
|
|
#define QUERY_ALLOC 8192
|
2010-07-07 03:19:02 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#define MIGRATOR_API_VERSION 1
|
|
|
|
|
|
|
|
#define MESSAGE_WIDTH "60"
|
|
|
|
|
|
|
|
#define OVERWRITE_MESSAGE " %-" MESSAGE_WIDTH "." MESSAGE_WIDTH "s\r"
|
|
|
|
#define GET_MAJOR_VERSION(v) ((v) / 100)
|
|
|
|
|
|
|
|
#define ALL_DUMP_FILE "pg_upgrade_dump_all.sql"
|
|
|
|
/* contains both global db information and CREATE DATABASE commands */
|
|
|
|
#define GLOBALS_DUMP_FILE "pg_upgrade_dump_globals.sql"
|
|
|
|
#define DB_DUMP_FILE "pg_upgrade_dump_db.sql"
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
#define pg_copy_file copy_file
|
|
|
|
#define pg_mv_file rename
|
|
|
|
#define pg_link_file link
|
2010-10-19 23:52:43 +08:00
|
|
|
#define RM_CMD "rm -f"
|
2010-05-12 10:19:11 +08:00
|
|
|
#define RMDIR_CMD "rm -rf"
|
2010-10-19 23:57:55 +08:00
|
|
|
#define SCRIPT_EXT "sh"
|
2010-05-12 10:19:11 +08:00
|
|
|
#else
|
|
|
|
#define pg_copy_file CopyFile
|
|
|
|
#define pg_mv_file pgrename
|
|
|
|
#define pg_link_file win32_pghardlink
|
|
|
|
#define sleep(x) Sleep(x * 1000)
|
2010-10-19 23:52:43 +08:00
|
|
|
#define RM_CMD "DEL /q"
|
2010-05-12 10:19:11 +08:00
|
|
|
#define RMDIR_CMD "RMDIR /s/q"
|
2010-10-19 23:57:55 +08:00
|
|
|
#define SCRIPT_EXT "bat"
|
2010-07-01 23:52:52 +08:00
|
|
|
#define EXE_EXT ".exe"
|
2010-05-12 10:19:11 +08:00
|
|
|
#endif
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
#define CLUSTER_NAME(cluster) ((cluster) == &old_cluster ? "old" : \
|
|
|
|
(cluster) == &new_cluster ? "new" : "none")
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-09-29 06:11:17 +08:00
|
|
|
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
/* OID system catalog preservation added during PG 9.0 development */
|
2010-07-07 03:19:02 +08:00
|
|
|
#define TABLE_SPACE_SUBDIRS 201001111
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Each relation is represented by a relinfo structure.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2010-07-07 03:19:02 +08:00
|
|
|
char nspname[NAMEDATALEN]; /* namespace name */
|
|
|
|
char relname[NAMEDATALEN]; /* relation name */
|
2011-01-08 10:25:34 +08:00
|
|
|
Oid reloid; /* relation oid */
|
|
|
|
Oid relfilenode; /* relation relfile node */
|
2010-07-07 03:19:02 +08:00
|
|
|
char tablespace[MAXPGPATH]; /* relations tablespace path */
|
2010-05-12 10:19:11 +08:00
|
|
|
} RelInfo;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
RelInfo *rels;
|
|
|
|
int nrels;
|
|
|
|
} RelInfoArr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following structure represents a relation mapping.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2010-10-20 08:33:19 +08:00
|
|
|
char old_dir[MAXPGPATH];
|
|
|
|
char new_dir[MAXPGPATH];
|
2011-01-06 00:37:08 +08:00
|
|
|
/*
|
|
|
|
* old/new relfilenodes might differ for pg_largeobject(_metadata) indexes
|
|
|
|
* due to VACUUM FULL or REINDEX. Other relfilenodes are preserved.
|
|
|
|
*/
|
|
|
|
Oid old_relfilenode;
|
|
|
|
Oid new_relfilenode;
|
|
|
|
/* the rest are used only for logging and error reporting */
|
2011-01-08 11:36:51 +08:00
|
|
|
char nspname[NAMEDATALEN]; /* namespaces */
|
|
|
|
char relname[NAMEDATALEN];
|
2010-05-12 10:19:11 +08:00
|
|
|
} FileNameMap;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Structure to store database information
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
Oid db_oid; /* oid of the database */
|
2010-05-14 08:13:38 +08:00
|
|
|
char db_name[NAMEDATALEN]; /* database name */
|
2010-05-12 10:19:11 +08:00
|
|
|
char db_tblspace[MAXPGPATH]; /* database default tablespace path */
|
|
|
|
RelInfoArr rel_arr; /* array of all user relinfos */
|
|
|
|
} DbInfo;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
DbInfo *dbs; /* array of db infos */
|
|
|
|
int ndbs; /* number of db infos */
|
|
|
|
} DbInfoArr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following structure is used to hold pg_control information.
|
|
|
|
* Rather than using the backend's control structure we use our own
|
|
|
|
* structure to avoid pg_control version issues between releases.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint32 ctrl_ver;
|
|
|
|
uint32 cat_ver;
|
|
|
|
uint32 logid;
|
|
|
|
uint32 nxtlogseg;
|
|
|
|
uint32 chkpnt_tli;
|
|
|
|
uint32 chkpnt_nxtxid;
|
|
|
|
uint32 chkpnt_nxtoid;
|
|
|
|
uint32 align;
|
|
|
|
uint32 blocksz;
|
|
|
|
uint32 largesz;
|
|
|
|
uint32 walsz;
|
|
|
|
uint32 walseg;
|
|
|
|
uint32 ident;
|
|
|
|
uint32 index;
|
|
|
|
uint32 toast;
|
|
|
|
bool date_is_int;
|
|
|
|
bool float8_pass_by_value;
|
|
|
|
char *lc_collate;
|
|
|
|
char *lc_ctype;
|
|
|
|
char *encoding;
|
|
|
|
} ControlData;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enumeration to denote link modes
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
TRANSFER_MODE_COPY,
|
|
|
|
TRANSFER_MODE_LINK
|
|
|
|
} transferMode;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enumeration to denote pg_log modes
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PG_INFO,
|
|
|
|
PG_REPORT,
|
|
|
|
PG_WARNING,
|
|
|
|
PG_FATAL,
|
|
|
|
PG_DEBUG
|
|
|
|
} eLogType;
|
|
|
|
|
|
|
|
|
|
|
|
typedef long pgpid_t;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cluster
|
|
|
|
*
|
|
|
|
* information about each cluster
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ControlData controldata; /* pg_control information */
|
2010-07-07 03:19:02 +08:00
|
|
|
DbInfoArr dbarr; /* dbinfos array */
|
|
|
|
char *pgdata; /* pathname for cluster's $PGDATA directory */
|
|
|
|
char *bindir; /* pathname for cluster's executable directory */
|
|
|
|
unsigned short port; /* port number where postmaster is waiting */
|
|
|
|
uint32 major_version; /* PG_VERSION of cluster */
|
2011-01-02 01:28:48 +08:00
|
|
|
char major_version_str[64]; /* string PG_VERSION of cluster */
|
2010-07-07 03:19:02 +08:00
|
|
|
Oid pg_database_oid; /* OID of pg_database relation */
|
|
|
|
char *libpath; /* pathname for cluster's pkglibdir */
|
|
|
|
char *tablespace_suffix; /* directory specification */
|
2010-05-12 10:19:11 +08:00
|
|
|
} ClusterInfo;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2010-10-20 10:31:17 +08:00
|
|
|
* LogOpts
|
2010-10-20 05:38:16 +08:00
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *filename; /* name of log file (may be /dev/null) */
|
|
|
|
FILE *fd; /* log FILE */
|
|
|
|
bool debug; /* TRUE -> log more information */
|
|
|
|
FILE *debug_fd; /* debug-level log FILE */
|
|
|
|
bool verbose; /* TRUE -> be verbose in messages */
|
2010-10-20 10:31:17 +08:00
|
|
|
} LogOpts;
|
2010-10-20 05:38:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UserOpts
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
bool check; /* TRUE -> ask user for permission to make
|
|
|
|
* changes */
|
|
|
|
transferMode transfer_mode; /* copy files or link them? */
|
|
|
|
} UserOpts;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OSInfo
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *progname; /* complete pathname for this program */
|
|
|
|
char *exec_path; /* full path to my executable */
|
|
|
|
char *user; /* username for clusters */
|
2010-07-07 03:19:02 +08:00
|
|
|
char cwd[MAXPGPATH]; /* current working directory, used for output */
|
2010-05-12 10:19:11 +08:00
|
|
|
char **tablespaces; /* tablespaces */
|
|
|
|
int num_tablespaces;
|
|
|
|
char **libraries; /* loadable libraries */
|
|
|
|
int num_libraries;
|
|
|
|
pgpid_t postmasterPID; /* PID of currently running postmaster */
|
2011-01-02 01:06:36 +08:00
|
|
|
ClusterInfo *running_cluster;
|
2010-10-20 05:38:16 +08:00
|
|
|
} OSInfo;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
2010-10-20 10:31:17 +08:00
|
|
|
extern LogOpts log_opts;
|
2010-10-20 06:37:04 +08:00
|
|
|
extern UserOpts user_opts;
|
2011-01-02 01:06:36 +08:00
|
|
|
extern ClusterInfo old_cluster, new_cluster;
|
2010-10-20 06:37:04 +08:00
|
|
|
extern OSInfo os_info;
|
2010-07-07 03:19:02 +08:00
|
|
|
extern char scandir_file_pattern[];
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* check.c */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
void output_check_banner(bool *live_check);
|
|
|
|
void check_old_cluster(bool live_check,
|
2010-07-07 03:19:02 +08:00
|
|
|
char **sequence_script_file_name);
|
2010-10-20 05:38:16 +08:00
|
|
|
void check_new_cluster(void);
|
|
|
|
void report_clusters_compatible(void);
|
2010-10-20 08:33:19 +08:00
|
|
|
void issue_warnings(char *sequence_script_file_name);
|
|
|
|
void output_completion_banner(char *deletion_script_file_name);
|
2010-10-20 05:38:16 +08:00
|
|
|
void check_cluster_versions(void);
|
|
|
|
void check_cluster_compatibility(bool live_check);
|
2010-10-20 06:37:04 +08:00
|
|
|
void create_script_for_old_cluster_deletion(char **deletion_script_file_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* controldata.c */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
void get_control_data(ClusterInfo *cluster, bool live_check);
|
|
|
|
void check_control_data(ControlData *oldctrl,
|
2010-05-12 10:19:11 +08:00
|
|
|
ControlData *newctrl);
|
|
|
|
|
|
|
|
|
|
|
|
/* dump.c */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
void generate_old_dump(void);
|
|
|
|
void split_old_dump(void);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* exec.c */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
int exec_prog(bool throw_error,
|
2010-07-07 03:19:02 +08:00
|
|
|
const char *cmd,...);
|
2010-10-20 05:38:16 +08:00
|
|
|
void verify_directories(void);
|
|
|
|
bool is_server_running(const char *datadir);
|
|
|
|
void rename_old_pg_control(void);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* file.c */
|
|
|
|
|
|
|
|
#ifdef PAGE_CONVERSION
|
|
|
|
typedef const char *(*pluginStartup) (uint16 migratorVersion,
|
|
|
|
uint16 *pluginVersion, uint16 newPageVersion,
|
|
|
|
uint16 oldPageVersion, void **pluginData);
|
|
|
|
typedef const char *(*pluginConvertFile) (void *pluginData,
|
|
|
|
const char *dstName, const char *srcName);
|
|
|
|
typedef const char *(*pluginConvertPage) (void *pluginData,
|
|
|
|
const char *dstPage, const char *srcPage);
|
|
|
|
typedef const char *(*pluginShutdown) (void *pluginData);
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2010-07-07 03:19:02 +08:00
|
|
|
uint16 oldPageVersion; /* Page layout version of the old cluster */
|
|
|
|
uint16 newPageVersion; /* Page layout version of the new cluster */
|
2010-05-12 10:19:11 +08:00
|
|
|
uint16 pluginVersion; /* API version of converter plugin */
|
2010-07-07 03:19:02 +08:00
|
|
|
void *pluginData; /* Plugin data (set by plugin) */
|
|
|
|
pluginStartup startup; /* Pointer to plugin's startup function */
|
|
|
|
pluginConvertFile convertFile; /* Pointer to plugin's file converter
|
2010-05-12 10:19:11 +08:00
|
|
|
* function */
|
2010-07-07 03:19:02 +08:00
|
|
|
pluginConvertPage convertPage; /* Pointer to plugin's page converter
|
2010-05-12 10:19:11 +08:00
|
|
|
* function */
|
|
|
|
pluginShutdown shutdown; /* Pointer to plugin's shutdown function */
|
|
|
|
} pageCnvCtx;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
const char *setupPageConverter(pageCnvCtx **result);
|
2010-05-12 10:19:11 +08:00
|
|
|
#else
|
|
|
|
/* dummy */
|
|
|
|
typedef void *pageCnvCtx;
|
|
|
|
#endif
|
|
|
|
|
2010-07-07 03:19:02 +08:00
|
|
|
int dir_matching_filenames(const struct dirent * scan_ent);
|
2010-10-20 08:33:19 +08:00
|
|
|
int pg_scandir(const char *dirname, struct dirent *** namelist,
|
|
|
|
int (*selector) (const struct dirent *));
|
|
|
|
const char *copyAndUpdateFile(pageCnvCtx *pageConverter, const char *src,
|
2010-05-12 10:19:11 +08:00
|
|
|
const char *dst, bool force);
|
2010-10-20 08:33:19 +08:00
|
|
|
const char *linkAndUpdateFile(pageCnvCtx *pageConverter, const char *src,
|
|
|
|
const char *dst);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
void check_hard_link(void);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* function.c */
|
|
|
|
|
2011-01-09 02:44:44 +08:00
|
|
|
void install_support_functions_in_new_db(const char *db_name);
|
|
|
|
void uninstall_support_functions_from_new_cluster(void);
|
2010-10-20 05:38:16 +08:00
|
|
|
void get_loadable_libraries(void);
|
|
|
|
void check_loadable_libraries(void);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* info.c */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
FileNameMap *gen_db_file_maps(DbInfo *old_db,
|
2010-07-07 03:19:02 +08:00
|
|
|
DbInfo *new_db, int *nmaps, const char *old_pgdata,
|
|
|
|
const char *new_pgdata);
|
2011-01-09 02:44:44 +08:00
|
|
|
void get_db_and_rel_infos(ClusterInfo *cluster);
|
2011-01-11 00:45:22 +08:00
|
|
|
void free_db_and_rel_infos(DbInfoArr *db_arr);
|
|
|
|
void print_maps(FileNameMap *maps, int n,
|
|
|
|
const char *db_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* option.c */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
void parseCommandLine(int argc, char *argv[]);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* relfilenode.c */
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
void get_pg_database_relfilenode(ClusterInfo *cluster);
|
2010-10-20 05:38:16 +08:00
|
|
|
const char *transfer_all_new_dbs(DbInfoArr *olddb_arr,
|
2010-05-12 10:19:11 +08:00
|
|
|
DbInfoArr *newdb_arr, char *old_pgdata, char *new_pgdata);
|
|
|
|
|
|
|
|
|
|
|
|
/* tablespace.c */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
void init_tablespaces(void);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* server.c */
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
PGconn *connectToServer(ClusterInfo *cluster, const char *db_name);
|
|
|
|
PGresult *executeQueryOrDie(PGconn *conn, const char *fmt,...);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
void start_postmaster(ClusterInfo *cluster, bool quiet);
|
2010-10-20 05:38:16 +08:00
|
|
|
void stop_postmaster(bool fast, bool quiet);
|
2011-01-02 01:28:48 +08:00
|
|
|
uint32 get_major_server_version(ClusterInfo *cluster);
|
2010-10-20 05:38:16 +08:00
|
|
|
void check_for_libpq_envvars(void);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* util.c */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
void exit_nicely(bool need_cleanup);
|
|
|
|
char *quote_identifier(const char *s);
|
|
|
|
int get_user_info(char **user_name);
|
|
|
|
void check_ok(void);
|
|
|
|
void report_status(eLogType type, const char *fmt,...);
|
|
|
|
void pg_log(eLogType type, char *fmt,...);
|
|
|
|
void prep_status(const char *fmt,...);
|
|
|
|
void check_ok(void);
|
|
|
|
char *pg_strdup(const char *s);
|
|
|
|
void *pg_malloc(int size);
|
2010-05-12 10:19:11 +08:00
|
|
|
void pg_free(void *ptr);
|
|
|
|
const char *getErrorText(int errNum);
|
2010-09-29 05:41:03 +08:00
|
|
|
unsigned int str2uint(const char *str);
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* version.c */
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
void new_9_0_populate_pg_largeobject_metadata(ClusterInfo *cluster,
|
|
|
|
bool check_mode);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* version_old_8_3.c */
|
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
void old_8_3_check_for_name_data_type_usage(ClusterInfo *cluster);
|
|
|
|
void old_8_3_check_for_tsquery_usage(ClusterInfo *cluster);
|
|
|
|
void old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode);
|
|
|
|
void old_8_3_invalidate_hash_gin_indexes(ClusterInfo *cluster, bool check_mode);
|
|
|
|
void old_8_3_invalidate_bpchar_pattern_ops_indexes(ClusterInfo *cluster,
|
|
|
|
bool check_mode);
|
|
|
|
char *old_8_3_create_sequence_script(ClusterInfo *cluster);
|