/* * parseCommandLine() * * Parses the command line (argc, argv[]) and loads structures */ void parseCommandLine(int argc, char *argv[]) { static struct option long_options[] = { {"old-datadir", required_argument, NULL, 'd'}, {"new-datadir", required_argument, NULL, 'D'}, {"old-bindir", required_argument, NULL, 'b'}, {"new-bindir", required_argument, NULL, 'B'}, {"old-options", required_argument, NULL, 'o'}, {"new-options", required_argument, NULL, 'O'}, {"old-port", required_argument, NULL, 'p'}, {"new-port", required_argument, NULL, 'P'}, {"username", required_argument, NULL, 'U'}, {"check", no_argument, NULL, 'c'}, {"link", no_argument, NULL, 'k'}, {"retain", no_argument, NULL, 'r'}, {"jobs", required_argument, NULL, 'j'}, {"verbose", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0} }; int option; /* Command line option */ int optindex = 0; /* used by getopt_long */ int os_user_effective_id; FILE *fp; char **filename; time_t run_time = time(NULL); user_opts.transfer_mode = TRANSFER_MODE_COPY; os_info.progname = get_progname(argv[0]); /* Process libpq env. variables; load values here for usage() output */ old_cluster.port = getenv("PGPORTOLD") ? atoi(getenv("PGPORTOLD")) : DEF_PGUPORT; new_cluster.port = getenv("PGPORTNEW") ? atoi(getenv("PGPORTNEW")) : DEF_PGUPORT; os_user_effective_id = get_user_info(&os_info.user); /* we override just the database user name; we got the OS id above */ if (getenv("PGUSER")) { pg_free(os_info.user); /* must save value, getenv()'s pointer is not stable */ os_info.user = pg_strdup(getenv("PGUSER")); } if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { usage(); exit(0); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { puts("pg_upgrade (PostgreSQL) " PG_VERSION); exit(0); } } /* Allow help and version to be run as root, so do the test here. */ if (os_user_effective_id == 0) pg_fatal("%s: cannot be run as root\n", os_info.progname); if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL) pg_fatal("cannot write to log file %s\n", INTERNAL_LOG_FILE); while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rU:v", long_options, &optindex)) != -1) { switch (option) { case 'b': old_cluster.bindir = pg_strdup(optarg); break; case 'B': new_cluster.bindir = pg_strdup(optarg); break; case 'c': user_opts.check = true; break; case 'd': old_cluster.pgdata = pg_strdup(optarg); old_cluster.pgconfig = pg_strdup(optarg); break; case 'D': new_cluster.pgdata = pg_strdup(optarg); new_cluster.pgconfig = pg_strdup(optarg); break; case 'j': user_opts.jobs = atoi(optarg); break; case 'k': user_opts.transfer_mode = TRANSFER_MODE_LINK; break; case 'o': old_cluster.pgopts = pg_strdup(optarg); break; case 'O': new_cluster.pgopts = pg_strdup(optarg); break; /* * Someday, the port number option could be removed and passed * using -o/-O, but that requires postmaster -C to be * supported on all old/new versions. */ case 'p': if ((old_cluster.port = atoi(optarg)) <= 0) { pg_fatal("invalid old port number\n"); exit(1); } break; case 'P': if ((new_cluster.port = atoi(optarg)) <= 0) { pg_fatal("invalid new port number\n"); exit(1); } break; case 'r': log_opts.retain = true; break; case 'U': pg_free(os_info.user); os_info.user = pg_strdup(optarg); os_info.user_specified = true; /* * Push the user name into the environment so pre-9.1 * pg_ctl/libpq uses it. */ pg_putenv("PGUSER", os_info.user); break; case 'v': pg_log(PG_REPORT, "Running in verbose mode\n"); log_opts.verbose = true; break; default: pg_fatal("Try \"%s --help\" for more information.\n", os_info.progname); break; } } /* label start of upgrade in logfiles */ for (filename = output_files; *filename != NULL; filename++) { if ((fp = fopen_priv(*filename, "a")) == NULL) pg_fatal("cannot write to log file %s\n", *filename); /* Start with newline because we might be appending to a file. */ fprintf(fp, "\n" "-----------------------------------------------------------------\n" " pg_upgrade run on %s" "-----------------------------------------------------------------\n\n", ctime(&run_time)); fclose(fp); } /* Turn off read-only mode; add prefix to PGOPTIONS? */ if (getenv("PGOPTIONS")) { char *pgoptions = psprintf("%s %s", FIX_DEFAULT_READ_ONLY, getenv("PGOPTIONS")); pg_putenv("PGOPTIONS", pgoptions); pfree(pgoptions); } else pg_putenv("PGOPTIONS", FIX_DEFAULT_READ_ONLY); /* Get values from env if not already set */ check_required_directory(&old_cluster.bindir, NULL, "PGBINOLD", "-b", "old cluster binaries reside"); check_required_directory(&new_cluster.bindir, NULL, "PGBINNEW", "-B", "new cluster binaries reside"); check_required_directory(&old_cluster.pgdata, &old_cluster.pgconfig, "PGDATAOLD", "-d", "old cluster data resides"); check_required_directory(&new_cluster.pgdata, &new_cluster.pgconfig, "PGDATANEW", "-D", "new cluster data resides"); }
/* * get_control_data() * * gets pg_control information in "ctrl". Assumes that bindir and * datadir are valid absolute paths to postgresql bin and pgdata * directories respectively *and* pg_resetxlog is version compatible * with datadir. The main purpose of this function is to get pg_control * data in a version independent manner. * * The approach taken here is to invoke pg_resetxlog with -n option * and then pipe its output. With little string parsing we get the * pg_control data. pg_resetxlog cannot be run while the server is running * so we use pg_controldata; pg_controldata doesn't provide all the fields * we need to actually perform the upgrade, but it provides enough for * check mode. We do not implement pg_resetxlog -n because it is hard to * return valid xid data for a running server. */ void get_control_data(ClusterInfo *cluster, bool live_check) { char cmd[MAXPGPATH]; char bufin[MAX_STRING]; FILE *output; char *p; bool got_xid = false; bool got_oid = false; bool got_nextxlogfile = false; bool got_multi = false; bool got_mxoff = false; bool got_oldestmulti = false; bool got_log_id = false; bool got_log_seg = false; bool got_tli = false; bool got_align = false; bool got_blocksz = false; bool got_largesz = false; bool got_walsz = false; bool got_walseg = false; bool got_ident = false; bool got_index = false; bool got_toast = false; bool got_large_object = false; bool got_date_is_int = false; bool got_float8_pass_by_value = false; bool got_data_checksum_version = false; char *lc_collate = NULL; char *lc_ctype = NULL; char *lc_monetary = NULL; char *lc_numeric = NULL; char *lc_time = NULL; char *lang = NULL; char *language = NULL; char *lc_all = NULL; char *lc_messages = NULL; uint32 logid = 0; uint32 segno = 0; uint32 tli = 0; /* * Because we test the pg_resetxlog output as strings, it has to be in * English. Copied from pg_regress.c. */ if (getenv("LC_COLLATE")) lc_collate = pg_strdup(getenv("LC_COLLATE")); if (getenv("LC_CTYPE")) lc_ctype = pg_strdup(getenv("LC_CTYPE")); if (getenv("LC_MONETARY")) lc_monetary = pg_strdup(getenv("LC_MONETARY")); if (getenv("LC_NUMERIC")) lc_numeric = pg_strdup(getenv("LC_NUMERIC")); if (getenv("LC_TIME")) lc_time = pg_strdup(getenv("LC_TIME")); if (getenv("LANG")) lang = pg_strdup(getenv("LANG")); if (getenv("LANGUAGE")) language = pg_strdup(getenv("LANGUAGE")); if (getenv("LC_ALL")) lc_all = pg_strdup(getenv("LC_ALL")); if (getenv("LC_MESSAGES")) lc_messages = pg_strdup(getenv("LC_MESSAGES")); pg_putenv("LC_COLLATE", NULL); pg_putenv("LC_CTYPE", NULL); pg_putenv("LC_MONETARY", NULL); pg_putenv("LC_NUMERIC", NULL); pg_putenv("LC_TIME", NULL); pg_putenv("LANG", #ifndef WIN32 NULL); #else /* On Windows the default locale cannot be English, so force it */ "en"); #endif pg_putenv("LANGUAGE", NULL); pg_putenv("LC_ALL", NULL); pg_putenv("LC_MESSAGES", "C"); snprintf(cmd, sizeof(cmd), "\"%s/%s \"%s\"", cluster->bindir, live_check ? "pg_controldata\"" : "pg_resetxlog\" -n", cluster->pgdata); fflush(stdout); fflush(stderr); if ((output = popen(cmd, "r")) == NULL) pg_fatal("Could not get control data using %s: %s\n", cmd, getErrorText(errno)); /* Only in <= 9.2 */ if (GET_MAJOR_VERSION(cluster->major_version) <= 902) { cluster->controldata.data_checksum_version = 0; got_data_checksum_version = true; } /* we have the result of cmd in "output". so parse it line by line now */ while (fgets(bufin, sizeof(bufin), output)) { pg_log(PG_VERBOSE, "%s", bufin); if ((p = strstr(bufin, "pg_control version number:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: pg_resetxlog problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.ctrl_ver = str2uint(p); } else if ((p = strstr(bufin, "Catalog version number:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.cat_ver = str2uint(p); } else if ((p = strstr(bufin, "First log segment after reset:")) != NULL) { /* Skip the colon and any whitespace after it */ p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p = strpbrk(p, "01234567890ABCDEF"); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); /* Make sure it looks like a valid WAL file name */ if (strspn(p, "0123456789ABCDEF") != 24) pg_fatal("%d: controldata retrieval problem\n", __LINE__); strlcpy(cluster->controldata.nextxlogfile, p, 25); got_nextxlogfile = true; } else if ((p = strstr(bufin, "First log file ID after reset:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ logid = str2uint(p); got_log_id = true; } else if ((p = strstr(bufin, "First log file segment after reset:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ segno = str2uint(p); got_log_seg = true; } else if ((p = strstr(bufin, "Latest checkpoint's TimeLineID:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.chkpnt_tli = str2uint(p); got_tli = true; } else if ((p = strstr(bufin, "Latest checkpoint's NextXID:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.chkpnt_nxtepoch = str2uint(p); p = strchr(p, '/'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove '/' char */ cluster->controldata.chkpnt_nxtxid = str2uint(p); got_xid = true; } else if ((p = strstr(bufin, "Latest checkpoint's NextOID:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.chkpnt_nxtoid = str2uint(p); got_oid = true; } else if ((p = strstr(bufin, "Latest checkpoint's NextMultiXactId:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.chkpnt_nxtmulti = str2uint(p); got_multi = true; } else if ((p = strstr(bufin, "Latest checkpoint's oldestMultiXid:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.chkpnt_oldstMulti = str2uint(p); got_oldestmulti = true; } else if ((p = strstr(bufin, "Latest checkpoint's NextMultiOffset:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.chkpnt_nxtmxoff = str2uint(p); got_mxoff = true; } else if ((p = strstr(bufin, "Maximum data alignment:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.align = str2uint(p); got_align = true; } else if ((p = strstr(bufin, "Database block size:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.blocksz = str2uint(p); got_blocksz = true; } else if ((p = strstr(bufin, "Blocks per segment of large relation:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.largesz = str2uint(p); got_largesz = true; } else if ((p = strstr(bufin, "WAL block size:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.walsz = str2uint(p); got_walsz = true; } else if ((p = strstr(bufin, "Bytes per WAL segment:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.walseg = str2uint(p); got_walseg = true; } else if ((p = strstr(bufin, "Maximum length of identifiers:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.ident = str2uint(p); got_ident = true; } else if ((p = strstr(bufin, "Maximum columns in an index:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.index = str2uint(p); got_index = true; } else if ((p = strstr(bufin, "Maximum size of a TOAST chunk:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.toast = str2uint(p); got_toast = true; } else if ((p = strstr(bufin, "Size of a large-object chunk:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.large_object = str2uint(p); got_large_object = true; } else if ((p = strstr(bufin, "Date/time type storage:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ cluster->controldata.date_is_int = strstr(p, "64-bit integers") != NULL; got_date_is_int = true; } else if ((p = strstr(bufin, "Float8 argument passing:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ /* used later for contrib check */ cluster->controldata.float8_pass_by_value = strstr(p, "by value") != NULL; got_float8_pass_by_value = true; } else if ((p = strstr(bufin, "checksum")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_fatal("%d: controldata retrieval problem\n", __LINE__); p++; /* remove ':' char */ /* used later for contrib check */ cluster->controldata.data_checksum_version = str2uint(p); got_data_checksum_version = true; } } if (output) pclose(output); /* * Restore environment variables */ pg_putenv("LC_COLLATE", lc_collate); pg_putenv("LC_CTYPE", lc_ctype); pg_putenv("LC_MONETARY", lc_monetary); pg_putenv("LC_NUMERIC", lc_numeric); pg_putenv("LC_TIME", lc_time); pg_putenv("LANG", lang); pg_putenv("LANGUAGE", language); pg_putenv("LC_ALL", lc_all); pg_putenv("LC_MESSAGES", lc_messages); pg_free(lc_collate); pg_free(lc_ctype); pg_free(lc_monetary); pg_free(lc_numeric); pg_free(lc_time); pg_free(lang); pg_free(language); pg_free(lc_all); pg_free(lc_messages); /* * Before 9.3, pg_resetxlog reported the xlogid and segno of the first log * file after reset as separate lines. Starting with 9.3, it reports the * WAL file name. If the old cluster is older than 9.3, we construct the * WAL file name from the xlogid and segno. */ if (GET_MAJOR_VERSION(cluster->major_version) <= 902) { if (got_log_id && got_log_seg) { snprintf(cluster->controldata.nextxlogfile, 25, "%08X%08X%08X", tli, logid, segno); got_nextxlogfile = true; } } /* verify that we got all the mandatory pg_control data */ if (!got_xid || !got_oid || !got_multi || !got_mxoff || (!got_oldestmulti && cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER) || (!live_check && !got_nextxlogfile) || !got_tli || !got_align || !got_blocksz || !got_largesz || !got_walsz || !got_walseg || !got_ident || !got_index || !got_toast || (!got_large_object && cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER) || !got_date_is_int || !got_float8_pass_by_value || !got_data_checksum_version) { pg_log(PG_REPORT, "The %s cluster lacks some required control information:\n", CLUSTER_NAME(cluster)); if (!got_xid) pg_log(PG_REPORT, " checkpoint next XID\n"); if (!got_oid) pg_log(PG_REPORT, " latest checkpoint next OID\n"); if (!got_multi) pg_log(PG_REPORT, " latest checkpoint next MultiXactId\n"); if (!got_mxoff) pg_log(PG_REPORT, " latest checkpoint next MultiXactOffset\n"); if (!got_oldestmulti && cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER) pg_log(PG_REPORT, " latest checkpoint oldest MultiXactId\n"); if (!live_check && !got_nextxlogfile) pg_log(PG_REPORT, " first WAL segment after reset\n"); if (!got_tli) pg_log(PG_REPORT, " latest checkpoint timeline ID\n"); if (!got_align) pg_log(PG_REPORT, " maximum alignment\n"); if (!got_blocksz) pg_log(PG_REPORT, " block size\n"); if (!got_largesz) pg_log(PG_REPORT, " large relation segment size\n"); if (!got_walsz) pg_log(PG_REPORT, " WAL block size\n"); if (!got_walseg) pg_log(PG_REPORT, " WAL segment size\n"); if (!got_ident) pg_log(PG_REPORT, " maximum identifier length\n"); if (!got_index) pg_log(PG_REPORT, " maximum number of indexed columns\n"); if (!got_toast) pg_log(PG_REPORT, " maximum TOAST chunk size\n"); if (!got_large_object && cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER) pg_log(PG_REPORT, " large-object chunk size\n"); if (!got_date_is_int) pg_log(PG_REPORT, " dates/times are integers?\n"); if (!got_float8_pass_by_value) pg_log(PG_REPORT, " float8 argument passing method\n"); /* value added in Postgres 9.3 */ if (!got_data_checksum_version) pg_log(PG_REPORT, " data checksum version\n"); pg_fatal("Cannot continue without required control information, terminating\n"); } }
/* * parseCommandLine() * * Parses the command line (argc, argv[]) and loads structures */ void parseCommandLine(int argc, char *argv[]) { static struct option long_options[] = { {"old-datadir", required_argument, NULL, 'd'}, {"new-datadir", required_argument, NULL, 'D'}, {"old-bindir", required_argument, NULL, 'b'}, {"new-bindir", required_argument, NULL, 'B'}, {"old-port", required_argument, NULL, 'p'}, {"new-port", required_argument, NULL, 'P'}, {"user", required_argument, NULL, 'u'}, {"check", no_argument, NULL, 'c'}, {"debug", no_argument, NULL, 'g'}, {"debugfile", required_argument, NULL, 'G'}, {"link", no_argument, NULL, 'k'}, {"logfile", required_argument, NULL, 'l'}, {"verbose", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0} }; int option; /* Command line option */ int optindex = 0; /* used by getopt_long */ int os_user_effective_id; user_opts.transfer_mode = TRANSFER_MODE_COPY; os_info.progname = get_progname(argv[0]); /* Process libpq env. variables; load values here for usage() output */ old_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT; new_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT; os_user_effective_id = get_user_info(&os_info.user); /* we override just the database user name; we got the OS id above */ if (getenv("PGUSER")) { pg_free(os_info.user); /* must save value, getenv()'s pointer is not stable */ os_info.user = pg_strdup(getenv("PGUSER")); } if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-?") == 0) { usage(); exit(0); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { puts("pg_upgrade (PostgreSQL) " PG_VERSION); exit(0); } } /* Allow help and version to be run as root, so do the test here. */ if (os_user_effective_id == 0) pg_log(PG_FATAL, "%s: cannot be run as root\n", os_info.progname); getcwd(os_info.cwd, MAXPGPATH); while ((option = getopt_long(argc, argv, "d:D:b:B:cgG:kl:p:P:u:v", long_options, &optindex)) != -1) { switch (option) { case 'b': old_cluster.bindir = pg_strdup(optarg); break; case 'B': new_cluster.bindir = pg_strdup(optarg); break; case 'c': user_opts.check = true; break; case 'd': old_cluster.pgdata = pg_strdup(optarg); break; case 'D': new_cluster.pgdata = pg_strdup(optarg); break; case 'g': pg_log(PG_REPORT, "Running in debug mode\n"); log_opts.debug = true; break; case 'G': if ((log_opts.debug_fd = fopen(optarg, "w")) == NULL) { pg_log(PG_FATAL, "cannot open debug file\n"); exit(1); } break; case 'k': user_opts.transfer_mode = TRANSFER_MODE_LINK; break; case 'l': log_opts.filename = pg_strdup(optarg); break; case 'p': if ((old_cluster.port = atoi(optarg)) <= 0) { pg_log(PG_FATAL, "invalid old port number\n"); exit(1); } break; case 'P': if ((new_cluster.port = atoi(optarg)) <= 0) { pg_log(PG_FATAL, "invalid new port number\n"); exit(1); } break; case 'u': pg_free(os_info.user); os_info.user = pg_strdup(optarg); /* * Push the user name into the environment so pre-9.1 * pg_ctl/libpq uses it. */ pg_putenv("PGUSER", os_info.user); break; case 'v': pg_log(PG_REPORT, "Running in verbose mode\n"); log_opts.verbose = true; break; default: pg_log(PG_FATAL, "Try \"%s --help\" for more information.\n", os_info.progname); break; } } if (log_opts.filename != NULL) { /* * We must use append mode so output generated by child processes via * ">>" will not be overwritten, and we want the file truncated on * start. */ /* truncate */ if ((log_opts.fd = fopen(log_opts.filename, "w")) == NULL) pg_log(PG_FATAL, "cannot write to log file %s\n", log_opts.filename); fclose(log_opts.fd); if ((log_opts.fd = fopen(log_opts.filename, "a")) == NULL) pg_log(PG_FATAL, "cannot write to log file %s\n", log_opts.filename); } else log_opts.filename = strdup(DEVNULL); /* if no debug file name, output to the terminal */ if (log_opts.debug && !log_opts.debug_fd) { log_opts.debug_fd = fopen(DEVTTY, "w"); if (!log_opts.debug_fd) pg_log(PG_FATAL, "cannot write to terminal\n"); } /* Get values from env if not already set */ validateDirectoryOption(&old_cluster.bindir, "OLDBINDIR", "-b", "old cluster binaries reside"); validateDirectoryOption(&new_cluster.bindir, "NEWBINDIR", "-B", "new cluster binaries reside"); validateDirectoryOption(&old_cluster.pgdata, "OLDDATADIR", "-d", "old cluster data resides"); validateDirectoryOption(&new_cluster.pgdata, "NEWDATADIR", "-D", "new cluster data resides"); get_pkglibdirs(); }
/* * get_control_data() * * gets pg_control information in "ctrl". Assumes that bindir and * datadir are valid absolute paths to postgresql bin and pgdata * directories respectively *and* pg_resetxlog is version compatible * with datadir. The main purpose of this function is to get pg_control * data in a version independent manner. * * The approach taken here is to invoke pg_resetxlog with -n option * and then pipe its output. With little string parsing we get the * pg_control data. pg_resetxlog cannot be run while the server is running * so we use pg_controldata; pg_controldata doesn't provide all the fields * we need to actually perform the upgrade, but it provides enough for * check mode. We do not implement pg_resetxlog -n because it is hard to * return valid xid data for a running server. */ void get_control_data(ClusterInfo *cluster, bool live_check) { char cmd[MAXPGPATH]; char bufin[MAX_STRING]; FILE *output; char *p; bool got_xid = false; bool got_oid = false; bool got_nextxlogfile = false; bool got_multi = false; bool got_mxoff = false; bool got_oldestmulti = false; bool got_log_id = false; bool got_log_seg = false; bool got_tli = false; bool got_align = false; bool got_blocksz = false; bool got_largesz = false; bool got_walsz = false; bool got_walseg = false; bool got_ident = false; bool got_index = false; bool got_toast = false; bool got_date_is_int = false; bool got_float8_pass_by_value = false; char *lc_collate = NULL; char *lc_ctype = NULL; char *lc_monetary = NULL; char *lc_numeric = NULL; char *lc_time = NULL; char *lang = NULL; char *language = NULL; char *lc_all = NULL; char *lc_messages = NULL; uint32 logid = 0; uint32 segno = 0; uint32 tli = 0; /* * Because we test the pg_resetxlog output as strings, it has to be in * English. Copied from pg_regress.c. */ if (getenv("LC_COLLATE")) lc_collate = pg_strdup(getenv("LC_COLLATE")); if (getenv("LC_CTYPE")) lc_ctype = pg_strdup(getenv("LC_CTYPE")); if (getenv("LC_MONETARY")) lc_monetary = pg_strdup(getenv("LC_MONETARY")); if (getenv("LC_NUMERIC")) lc_numeric = pg_strdup(getenv("LC_NUMERIC")); if (getenv("LC_TIME")) lc_time = pg_strdup(getenv("LC_TIME")); if (getenv("LANG")) lang = pg_strdup(getenv("LANG")); if (getenv("LANGUAGE")) language = pg_strdup(getenv("LANGUAGE")); if (getenv("LC_ALL")) lc_all = pg_strdup(getenv("LC_ALL")); if (getenv("LC_MESSAGES")) lc_messages = pg_strdup(getenv("LC_MESSAGES")); pg_putenv("LC_COLLATE", NULL); pg_putenv("LC_CTYPE", NULL); pg_putenv("LC_MONETARY", NULL); pg_putenv("LC_NUMERIC", NULL); pg_putenv("LC_TIME", NULL); pg_putenv("LANG", #ifndef WIN32 NULL); #else /* On Windows the default locale cannot be English, so force it */ "en"); #endif pg_putenv("LANGUAGE", NULL); pg_putenv("LC_ALL", NULL); pg_putenv("LC_MESSAGES", "C"); snprintf(cmd, sizeof(cmd), SYSTEMQUOTE "\"%s/%s \"%s\"" SYSTEMQUOTE, cluster->bindir, live_check ? "pg_controldata\"" : "pg_resetxlog\" -n", cluster->pgdata); fflush(stdout); fflush(stderr); if ((output = popen(cmd, "r")) == NULL) pg_log(PG_FATAL, "Could not get control data using %s: %s\n", cmd, getErrorText(errno)); /* Only pre-8.4 has these so if they are not set below we will check later */ cluster->controldata.lc_collate = NULL; cluster->controldata.lc_ctype = NULL; /* Only in <= 8.3 */ if (GET_MAJOR_VERSION(cluster->major_version) <= 803) { cluster->controldata.float8_pass_by_value = false; got_float8_pass_by_value = true; } /* we have the result of cmd in "output". so parse it line by line now */ while (fgets(bufin, sizeof(bufin), output)) { pg_log(PG_VERBOSE, "%s", bufin); #ifdef WIN32 /* * Due to an installer bug, LANG=C doesn't work for PG 8.3.3, but does * work 8.2.6 and 8.3.7, so check for non-ASCII output and suggest a * minor upgrade. */ if (GET_MAJOR_VERSION(cluster->major_version) <= 803) { for (p = bufin; *p; p++) if (!isascii(*p)) pg_log(PG_FATAL, "The 8.3 cluster's pg_controldata is incapable of outputting ASCII, even\n" "with LANG=C. You must upgrade this cluster to a newer version of PostgreSQL\n" "8.3 to fix this bug. PostgreSQL 8.3.7 and later are known to work properly.\n"); } #endif if ((p = strstr(bufin, "pg_control version number:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: pg_resetxlog problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.ctrl_ver = str2uint(p); } else if ((p = strstr(bufin, "Catalog version number:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.cat_ver = str2uint(p); } else if ((p = strstr(bufin, "First log segment after reset:")) != NULL) { /* Skip the colon and any whitespace after it */ p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p = strpbrk(p, "01234567890ABCDEF"); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); /* Make sure it looks like a valid WAL file name */ if (strspn(p, "0123456789ABCDEF") != 24) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); strlcpy(cluster->controldata.nextxlogfile, p, 25); got_nextxlogfile = true; } else if ((p = strstr(bufin, "First log file ID after reset:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ logid = str2uint(p); got_log_id = true; } else if ((p = strstr(bufin, "First log file segment after reset:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ segno = str2uint(p); got_log_seg = true; } else if ((p = strstr(bufin, "Latest checkpoint's TimeLineID:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.chkpnt_tli = str2uint(p); got_tli = true; } else if ((p = strstr(bufin, "Latest checkpoint's NextXID:")) != NULL) { char *op = strchr(p, '/'); if (op == NULL) op = strchr(p, ':'); if (op == NULL || strlen(op) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); op++; /* removing ':' char */ cluster->controldata.chkpnt_nxtxid = str2uint(op); got_xid = true; } else if ((p = strstr(bufin, "Latest checkpoint's NextOID:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.chkpnt_nxtoid = str2uint(p); got_oid = true; } else if ((p = strstr(bufin, "Latest checkpoint's NextMultiXactId:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.chkpnt_nxtmulti = str2uint(p); got_multi = true; } else if ((p = strstr(bufin, "Latest checkpoint's oldestMultiXid:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.chkpnt_oldstMulti = str2uint(p); got_oldestmulti = true; } else if ((p = strstr(bufin, "Latest checkpoint's NextMultiOffset:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.chkpnt_nxtmxoff = str2uint(p); got_mxoff = true; } else if ((p = strstr(bufin, "Maximum data alignment:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.align = str2uint(p); got_align = true; } else if ((p = strstr(bufin, "Database block size:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.blocksz = str2uint(p); got_blocksz = true; } else if ((p = strstr(bufin, "Blocks per segment of large relation:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.largesz = str2uint(p); got_largesz = true; } else if ((p = strstr(bufin, "WAL block size:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.walsz = str2uint(p); got_walsz = true; } else if ((p = strstr(bufin, "Bytes per WAL segment:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.walseg = str2uint(p); got_walseg = true; } else if ((p = strstr(bufin, "Maximum length of identifiers:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.ident = str2uint(p); got_ident = true; } else if ((p = strstr(bufin, "Maximum columns in an index:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.index = str2uint(p); got_index = true; } else if ((p = strstr(bufin, "Maximum size of a TOAST chunk:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.toast = str2uint(p); got_toast = true; } else if ((p = strstr(bufin, "Date/time type storage:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ cluster->controldata.date_is_int = strstr(p, "64-bit integers") != NULL; got_date_is_int = true; } else if ((p = strstr(bufin, "Float8 argument passing:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ /* used later for contrib check */ cluster->controldata.float8_pass_by_value = strstr(p, "by value") != NULL; got_float8_pass_by_value = true; } /* In pre-8.4 only */ else if ((p = strstr(bufin, "LC_COLLATE:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ /* skip leading spaces and remove trailing newline */ p += strspn(p, " "); if (strlen(p) > 0 && *(p + strlen(p) - 1) == '\n') *(p + strlen(p) - 1) = '\0'; cluster->controldata.lc_collate = pg_strdup(p); } /* In pre-8.4 only */ else if ((p = strstr(bufin, "LC_CTYPE:")) != NULL) { p = strchr(p, ':'); if (p == NULL || strlen(p) <= 1) pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__); p++; /* removing ':' char */ /* skip leading spaces and remove trailing newline */ p += strspn(p, " "); if (strlen(p) > 0 && *(p + strlen(p) - 1) == '\n') *(p + strlen(p) - 1) = '\0'; cluster->controldata.lc_ctype = pg_strdup(p); } } if (output) pclose(output); /* * Restore environment variables */ pg_putenv("LC_COLLATE", lc_collate); pg_putenv("LC_CTYPE", lc_ctype); pg_putenv("LC_MONETARY", lc_monetary); pg_putenv("LC_NUMERIC", lc_numeric); pg_putenv("LC_TIME", lc_time); pg_putenv("LANG", lang); pg_putenv("LANGUAGE", language); pg_putenv("LC_ALL", lc_all); pg_putenv("LC_MESSAGES", lc_messages); pg_free(lc_collate); pg_free(lc_ctype); pg_free(lc_monetary); pg_free(lc_numeric); pg_free(lc_time); pg_free(lang); pg_free(language); pg_free(lc_all); pg_free(lc_messages); /* * Before 9.3, pg_resetxlog reported the xlogid and segno of the first * log file after reset as separate lines. Starting with 9.3, it reports * the WAL file name. If the old cluster is older than 9.3, we construct * the WAL file name from the xlogid and segno. */ if (GET_MAJOR_VERSION(cluster->major_version) <= 902) { if (got_log_id && got_log_seg) { snprintf(cluster->controldata.nextxlogfile, 25, "%08X%08X%08X", tli, logid, segno); got_nextxlogfile = true; } } /* verify that we got all the mandatory pg_control data */ if (!got_xid || !got_oid || !got_multi || !got_mxoff || (!got_oldestmulti && cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER) || (!live_check && !got_nextxlogfile) || !got_tli || !got_align || !got_blocksz || !got_largesz || !got_walsz || !got_walseg || !got_ident || !got_index || !got_toast || !got_date_is_int || !got_float8_pass_by_value) { pg_log(PG_REPORT, "The %s cluster lacks some required control information:\n", CLUSTER_NAME(cluster)); if (!got_xid) pg_log(PG_REPORT, " checkpoint next XID\n"); if (!got_oid) pg_log(PG_REPORT, " latest checkpoint next OID\n"); if (!got_multi) pg_log(PG_REPORT, " latest checkpoint next MultiXactId\n"); if (!got_mxoff) pg_log(PG_REPORT, " latest checkpoint next MultiXactOffset\n"); if (!got_oldestmulti && cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER) pg_log(PG_REPORT, " latest checkpoint oldest MultiXactId\n"); if (!live_check && !got_nextxlogfile) pg_log(PG_REPORT, " first WAL segment after reset\n"); if (!got_tli) pg_log(PG_REPORT, " latest checkpoint timeline ID\n"); if (!got_align) pg_log(PG_REPORT, " maximum alignment\n"); if (!got_blocksz) pg_log(PG_REPORT, " block size\n"); if (!got_largesz) pg_log(PG_REPORT, " large relation segment size\n"); if (!got_walsz) pg_log(PG_REPORT, " WAL block size\n"); if (!got_walseg) pg_log(PG_REPORT, " WAL segment size\n"); if (!got_ident) pg_log(PG_REPORT, " maximum identifier length\n"); if (!got_index) pg_log(PG_REPORT, " maximum number of indexed columns\n"); if (!got_toast) pg_log(PG_REPORT, " maximum TOAST chunk size\n"); if (!got_date_is_int) pg_log(PG_REPORT, " dates/times are integers?\n"); /* value added in Postgres 8.4 */ if (!got_float8_pass_by_value) pg_log(PG_REPORT, " float8 argument passing method\n"); pg_log(PG_FATAL, "Cannot continue without required control information, terminating\n"); } }