void item_list_append(ItemList *item_list, char *error_message) { ItemListCell *cell; cell = (ItemListCell *) pg_malloc0(sizeof(ItemListCell)); if (cell == NULL) { log_err(_("unable to allocate memory; terminating.\n")); exit(ERR_BAD_CONFIG); } cell->string = pg_malloc0(MAXLEN); strncpy(cell->string, error_message, MAXLEN); if (item_list->tail) { item_list->tail->next = cell; } else { item_list->head = cell; } item_list->tail = cell; }
void error_list_append(ErrorList *error_list, char *error_message) { ErrorListCell *cell; cell = (ErrorListCell *) pg_malloc0(sizeof(ErrorListCell)); if (cell == NULL) { log_err(_("unable to allocate memory; terminating.\n")); exit(ERR_BAD_CONFIG); } cell->error_message = pg_malloc0(MAXLEN); strncpy(cell->error_message, error_message, MAXLEN); if (error_list->tail) { error_list->tail->next = cell; } else { error_list->head = cell; } error_list->tail = cell; }
bool stop_backup(PGconn *conn, char *last_wal_segment) { char sqlquery[QUERY_STR_LEN]; PGresult *res; sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_xlogfile_name(pg_catalog.pg_stop_backup())"); res = PQexec(conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) { log_err(_("unable to stop backup: %s\n"), PQerrorMessage(conn)); PQclear(res); return false; } if (last_wal_segment != NULL) { char *last_wal_seg_pq = PQgetvalue(res, 0, 0); size_t buf_sz = strlen(last_wal_seg_pq); last_wal_segment = pg_malloc0(buf_sz + 1); xsnprintf(last_wal_segment, buf_sz + 1, "%s", last_wal_seg_pq); } PQclear(res); return true; }
bool start_backup(PGconn *conn, char *first_wal_segment, bool fast_checkpoint) { char sqlquery[QUERY_STR_LEN]; PGresult *res; sqlquery_snprintf(sqlquery, "SELECT pg_catalog.pg_xlogfile_name(pg_catalog.pg_start_backup('repmgr_standby_clone_%ld', %s))", time(NULL), fast_checkpoint ? "TRUE" : "FALSE"); log_debug(_("standby clone: %s\n"), sqlquery); res = PQexec(conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) { log_err(_("unable to start backup: %s\n"), PQerrorMessage(conn)); PQclear(res); return false; } if (first_wal_segment != NULL) { char *first_wal_seg_pq = PQgetvalue(res, 0, 0); size_t buf_sz = strlen(first_wal_seg_pq); first_wal_segment = pg_malloc0(buf_sz + 1); xsnprintf(first_wal_segment, buf_sz + 1, "%s", first_wal_seg_pq); } PQclear(res); return true; }
/* Allocate a new compressor */ CompressorState * AllocateCompressor(int compression, WriteFunc writeF) { CompressorState *cs; CompressionAlgorithm alg; int level; ParseCompressionOption(compression, &alg, &level); #ifndef HAVE_LIBZ if (alg == COMPR_ALG_LIBZ) exit_horribly(modulename, "not built with zlib support\n"); #endif cs = (CompressorState *) pg_malloc0(sizeof(CompressorState)); cs->writeF = writeF; cs->comprAlg = alg; /* * Perform compression algorithm specific initialization. */ #ifdef HAVE_LIBZ if (alg == COMPR_ALG_LIBZ) InitCompressorZlib(cs, level); #endif return cs; }
static void parse_event_notifications_list(t_configuration_options *options, const char *arg) { const char *arg_ptr; char event_type_buf[MAXLEN] = ""; char *dst_ptr = event_type_buf; for (arg_ptr = arg; arg_ptr <= (arg + strlen(arg)); arg_ptr++) { /* ignore whitespace */ if (*arg_ptr == ' ' || *arg_ptr == '\t') { continue; } /* * comma (or end-of-string) should mark the end of an event type - * just as long as there was something preceding it */ if ((*arg_ptr == ',' || *arg_ptr == '\0') && event_type_buf[0] != '\0') { EventNotificationListCell *cell; cell = (EventNotificationListCell *) pg_malloc0(sizeof(EventNotificationListCell)); if (cell == NULL) { log_err(_("unable to allocate memory; terminating\n")); exit(ERR_BAD_CONFIG); } strncpy(cell->event_type, event_type_buf, MAXLEN); if (options->event_notifications.tail) { options->event_notifications.tail->next = cell; } else { options->event_notifications.head = cell; } options->event_notifications.tail = cell; memset(event_type_buf, 0, MAXLEN); dst_ptr = event_type_buf; } /* ignore duplicated commas */ else if (*arg_ptr == ',') { continue; } else { *dst_ptr++ = *arg_ptr; } } }
/* * Split argument into old_dir and new_dir and append to tablespace mapping * list. * * Adapted from pg_basebackup.c */ static void tablespace_list_append(t_configuration_options *options, const char *arg) { TablespaceListCell *cell; char *dst; char *dst_ptr; const char *arg_ptr; cell = (TablespaceListCell *) pg_malloc0(sizeof(TablespaceListCell)); if (cell == NULL) { log_err(_("unable to allocate memory; terminating\n")); exit(ERR_BAD_CONFIG); } dst_ptr = dst = cell->old_dir; for (arg_ptr = arg; *arg_ptr; arg_ptr++) { if (dst_ptr - dst >= MAXPGPATH) { log_err(_("directory name too long\n")); exit(ERR_BAD_CONFIG); } if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=') ; /* skip backslash escaping = */ else if (*arg_ptr == '=' && (arg_ptr == arg || *(arg_ptr - 1) != '\\')) { if (*cell->new_dir) { log_err(_("multiple \"=\" signs in tablespace mapping\n")); exit(ERR_BAD_CONFIG); } else { dst = dst_ptr = cell->new_dir; } } else *dst_ptr++ = *arg_ptr; } if (!*cell->old_dir || !*cell->new_dir) { log_err(_("invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"\n"), arg); exit(ERR_BAD_CONFIG); } canonicalize_path(cell->old_dir); canonicalize_path(cell->new_dir); if (options->tablespace_mapping.tail) options->tablespace_mapping.tail->next = cell; else options->tablespace_mapping.head = cell; options->tablespace_mapping.tail = cell; }
/* * The avl* functions below provide a minimalistic implementation of AVL binary * trees, to efficiently collect the distinct values that will form the horizontal * and vertical headers. It only supports adding new values, no removal or even * search. */ static void avlInit(avl_tree *tree) { tree->end = (avl_node *) pg_malloc0(sizeof(avl_node)); tree->end->children[0] = tree->end->children[1] = tree->end; tree->count = 0; tree->root = tree->end; }
RowMap * RowMapInit() { RowMap *m = pg_malloc0(sizeof(RowMap)); m->ops.bsd_rbto_compare_nodes = compare_nodes; m->ops.bsd_rbto_compare_key = compare_key; m->ops.bsd_rbto_node_offset = 0; m->ops.bsd_rbto_context = 0; bsd_rb_tree_init(&m->tree, &m->ops); return m; }
void RowMapUpdate(RowMap *m, Row *row) { TreeNode *res = 0; TreeNode *new_node = pg_malloc0(sizeof(TreeNode)); new_node->row = *row; res = bsd_rb_tree_insert_node(&m->tree, new_node); if (res != new_node) { /* replace old contents, free unneeded new node */ RowCleanup(&res->row); res->row = *row; pg_free(new_node); } }
void * palloc0(Size size) { return pg_malloc0(size); }
void do_bdr_unregister(void) { PGconn *conn = NULL; ExtensionStatus extension_status = REPMGR_UNKNOWN; int target_node_id = UNKNOWN_NODE_ID; t_node_info node_info = T_NODE_INFO_INITIALIZER; RecordStatus record_status = RECORD_NOT_FOUND; bool node_record_deleted = false; PQExpBufferData event_details; char *dbname; /* sanity-check configuration for BDR-compatability */ if (config_file_options.replication_type != REPLICATION_TYPE_BDR) { log_error(_("cannot run BDR UNREGISTER on a non-BDR node")); exit(ERR_BAD_CONFIG); } dbname = pg_malloc0(MAXLEN); if (dbname == NULL) { log_error(_("unable to allocate memory; terminating.")); exit(ERR_OUT_OF_MEMORY); } /* store the database name for future reference */ get_conninfo_value(config_file_options.conninfo, "dbname", dbname); conn = establish_db_connection(config_file_options.conninfo, true); if (!is_bdr_db(conn, NULL)) { log_error(_("database \"%s\" is not BDR-enabled"), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } extension_status = get_repmgr_extension_status(conn, NULL); if (extension_status != REPMGR_INSTALLED) { log_error(_("repmgr is not installed on database \"%s\""), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } pfree(dbname); if (!is_bdr_repmgr(conn)) { log_error(_("repmgr metadatabase contains records for non-BDR nodes")); PQfinish(conn); exit(ERR_BAD_CONFIG); } initPQExpBuffer(&event_details); if (runtime_options.node_id != UNKNOWN_NODE_ID) target_node_id = runtime_options.node_id; else target_node_id = config_file_options.node_id; /* Check node exists and is really a BDR node */ record_status = get_node_record(conn, target_node_id, &node_info); if (record_status != RECORD_FOUND) { log_error(_("no record found for node %i"), target_node_id); PQfinish(conn); exit(ERR_BAD_CONFIG); } begin_transaction(conn); log_debug("unregistering node %i", target_node_id); node_record_deleted = delete_node_record(conn, target_node_id); if (node_record_deleted == false) { appendPQExpBuffer(&event_details, "unable to delete node record for node \"%s\" (ID: %i)", node_info.node_name, target_node_id); rollback_transaction(conn); } else { appendPQExpBuffer(&event_details, "node record deleted for node \"%s\" (ID: %i)", node_info.node_name, target_node_id); commit_transaction(conn); } /* Log the event */ create_event_notification( conn, &config_file_options, config_file_options.node_id, "bdr_unregister", true, event_details.data); PQfinish(conn); log_notice(_("bdr node \"%s\" (ID: %i) successfully unregistered"), node_info.node_name, target_node_id); termPQExpBuffer(&event_details); return; }
/* * do_bdr_register() * * As each BDR node is its own primary, registering a BDR node * will create the repmgr metadata schema if necessary. */ void do_bdr_register(void) { PGconn *conn = NULL; BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER; ExtensionStatus extension_status = REPMGR_UNKNOWN; t_node_info node_info = T_NODE_INFO_INITIALIZER; RecordStatus record_status = RECORD_NOT_FOUND; PQExpBufferData event_details; bool success = true; char *dbname = NULL; /* sanity-check configuration for BDR-compatability */ if (config_file_options.replication_type != REPLICATION_TYPE_BDR) { log_error(_("cannot run BDR REGISTER on a non-BDR node")); exit(ERR_BAD_CONFIG); } dbname = pg_malloc0(MAXLEN); if (dbname == NULL) { log_error(_("unable to allocate memory; terminating.")); exit(ERR_OUT_OF_MEMORY); } /* store the database name for future reference */ get_conninfo_value(config_file_options.conninfo, "dbname", dbname); conn = establish_db_connection(config_file_options.conninfo, true); if (!is_bdr_db(conn, NULL)) { log_error(_("database \"%s\" is not BDR-enabled"), dbname); log_hint(_("when using repmgr with BDR, the repmgr schema must be stored in the BDR database")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } /* Check that there are at most 2 BDR nodes */ get_all_bdr_node_records(conn, &bdr_nodes); if (bdr_nodes.node_count == 0) { log_error(_("database \"%s\" is BDR-enabled but no BDR nodes were found"), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } /* BDR 2 implementation is for 2 nodes only */ if (get_bdr_version_num() < 3 && bdr_nodes.node_count > 2) { log_error(_("repmgr can only support BDR 2.x clusters with 2 nodes")); log_detail(_("this BDR cluster has %i nodes"), bdr_nodes.node_count); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } if (get_bdr_version_num() > 2) { log_error(_("\"repmgr bdr register\" is for BDR 2.x only")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } /* check for a matching BDR node */ { PQExpBufferData bdr_local_node_name; bool node_match = false; initPQExpBuffer(&bdr_local_node_name); node_match = bdr_node_name_matches(conn, config_file_options.node_name, &bdr_local_node_name); if (node_match == false) { if (strlen(bdr_local_node_name.data)) { log_error(_("local node BDR node name is \"%s\", expected: \"%s\""), bdr_local_node_name.data, config_file_options.node_name); log_hint(_("\"node_name\" in repmgr.conf must match \"node_name\" in bdr.bdr_nodes")); } else { log_error(_("local node does not report BDR node name")); log_hint(_("ensure this is an active BDR node")); } PQfinish(conn); pfree(dbname); termPQExpBuffer(&bdr_local_node_name); exit(ERR_BAD_CONFIG); } termPQExpBuffer(&bdr_local_node_name); } /* check whether repmgr extension exists, and there are no non-BDR nodes registered */ extension_status = get_repmgr_extension_status(conn, NULL); if (extension_status == REPMGR_UNKNOWN) { log_error(_("unable to determine status of \"repmgr\" extension in database \"%s\""), dbname); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } if (extension_status == REPMGR_UNAVAILABLE) { log_error(_("\"repmgr\" extension is not available")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } if (extension_status == REPMGR_INSTALLED) { if (!is_bdr_repmgr(conn)) { log_error(_("repmgr metadatabase contains records for non-BDR nodes")); PQfinish(conn); pfree(dbname); exit(ERR_BAD_CONFIG); } } else { log_debug("creating repmgr extension in database \"%s\"", dbname); begin_transaction(conn); if (!create_repmgr_extension(conn)) { log_error(_("unable to create repmgr extension - see preceding error message(s); aborting")); rollback_transaction(conn); pfree(dbname); PQfinish(conn); exit(ERR_BAD_CONFIG); } commit_transaction(conn); } pfree(dbname); if (bdr_node_has_repmgr_set(conn, config_file_options.node_name) == false) { log_debug("bdr_node_has_repmgr_set() = false"); bdr_node_set_repmgr_set(conn, config_file_options.node_name); } /* * before adding the extension tables to the replication set, if any other * BDR nodes exist, populate repmgr.nodes with a copy of existing entries * * currently we won't copy the contents of any other tables * */ { NodeInfoList local_node_records = T_NODE_INFO_LIST_INITIALIZER; (void) get_all_node_records(conn, &local_node_records); if (local_node_records.node_count == 0) { BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER; BdrNodeInfoListCell *bdr_cell = NULL; get_all_bdr_node_records(conn, &bdr_nodes); if (bdr_nodes.node_count == 0) { log_error(_("unable to retrieve any BDR node records")); log_detail("%s", PQerrorMessage(conn)); PQfinish(conn); exit(ERR_BAD_CONFIG); } for (bdr_cell = bdr_nodes.head; bdr_cell; bdr_cell = bdr_cell->next) { PGconn *bdr_node_conn = NULL; NodeInfoList existing_nodes = T_NODE_INFO_LIST_INITIALIZER; NodeInfoListCell *cell = NULL; ExtensionStatus other_node_extension_status = REPMGR_UNKNOWN; /* skip the local node */ if (strncmp(node_info.node_name, bdr_cell->node_info->node_name, sizeof(node_info.node_name)) == 0) { continue; } log_debug("connecting to BDR node \"%s\" (conninfo: \"%s\")", bdr_cell->node_info->node_name, bdr_cell->node_info->node_local_dsn); bdr_node_conn = establish_db_connection_quiet(bdr_cell->node_info->node_local_dsn); if (PQstatus(bdr_node_conn) != CONNECTION_OK) { continue; } /* check repmgr schema exists, skip if not */ other_node_extension_status = get_repmgr_extension_status(bdr_node_conn, NULL); if (other_node_extension_status != REPMGR_INSTALLED) { continue; } (void) get_all_node_records(bdr_node_conn, &existing_nodes); for (cell = existing_nodes.head; cell; cell = cell->next) { log_debug("creating record for node \"%s\" (ID: %i)", cell->node_info->node_name, cell->node_info->node_id); create_node_record(conn, "bdr register", cell->node_info); } PQfinish(bdr_node_conn); break; } } } /* Add the repmgr extension tables to a replication set */ if (get_bdr_version_num() < 3) { add_extension_tables_to_bdr_replication_set(conn); } else { /* this is the only table we need to replicate */ char *replication_set = get_default_bdr_replication_set(conn); /* * this probably won't happen, but we need to be sure we're using * the replication set metadata correctly... */ if (conn == NULL) { log_error(_("unable to retrieve default BDR replication set")); log_hint(_("see preceding messages")); log_debug("check query in get_default_bdr_replication_set()"); exit(ERR_BAD_CONFIG); } if (is_table_in_bdr_replication_set(conn, "nodes", replication_set) == false) { add_table_to_bdr_replication_set(conn, "nodes", replication_set); } pfree(replication_set); } initPQExpBuffer(&event_details); begin_transaction(conn); /* * we'll check if a record exists (even if the schema was just created), * as there's a faint chance of a race condition */ record_status = get_node_record(conn, config_file_options.node_id, &node_info); /* Update internal node record */ node_info.type = BDR; node_info.node_id = config_file_options.node_id; node_info.upstream_node_id = NO_UPSTREAM_NODE; node_info.active = true; node_info.priority = config_file_options.priority; strncpy(node_info.node_name, config_file_options.node_name, sizeof(node_info.node_name)); strncpy(node_info.location, config_file_options.location, sizeof(node_info.location)); strncpy(node_info.conninfo, config_file_options.conninfo, sizeof(node_info.conninfo)); if (record_status == RECORD_FOUND) { bool node_updated = false; /* * At this point we will have established there are no non-BDR * records, so no need to verify the node type */ if (!runtime_options.force) { log_error(_("this node is already registered")); log_hint(_("use -F/--force to overwrite the existing node record")); rollback_transaction(conn); PQfinish(conn); exit(ERR_BAD_CONFIG); } /* * don't permit changing the node name - this must match the BDR node * name set when the node was registered. */ if (strncmp(node_info.node_name, config_file_options.node_name, sizeof(node_info.node_name)) != 0) { log_error(_("a record for node %i is already registered with node_name \"%s\""), config_file_options.node_id, node_info.node_name); log_hint(_("node_name configured in repmgr.conf is \"%s\""), config_file_options.node_name); rollback_transaction(conn); PQfinish(conn); exit(ERR_BAD_CONFIG); } node_updated = update_node_record(conn, "bdr register", &node_info); if (node_updated == true) { appendPQExpBuffer(&event_details, _("node record updated for node \"%s\" (%i)"), config_file_options.node_name, config_file_options.node_id); log_verbose(LOG_NOTICE, "%s", event_details.data); } else { success = false; } } else { /* create new node record */ bool node_created = create_node_record(conn, "bdr register", &node_info); if (node_created == true) { appendPQExpBuffer(&event_details, _("node record created for node \"%s\" (ID: %i)"), config_file_options.node_name, config_file_options.node_id); log_notice("%s", event_details.data); } else { success = false; } } if (success == false) { rollback_transaction(conn); PQfinish(conn); exit(ERR_DB_QUERY); } commit_transaction(conn); /* Log the event */ create_event_notification( conn, &config_file_options, config_file_options.node_id, "bdr_register", true, event_details.data); termPQExpBuffer(&event_details); PQfinish(conn); log_notice(_("BDR node %i registered (conninfo: %s)"), config_file_options.node_id, config_file_options.conninfo); return; }
/* * parse_slash_copy parses copy options from the given meta-command line. The * function then returns a dynamically allocated structure with the options, or * Null on parsing error. */ copy_options * parse_slash_copy(const char *args) { struct copy_options *result; char *token; const char *whitespace = " \t\n\r"; char nonstd_backslash = standard_strings() ? 0 : '\\'; if (!args) { psql_error("\\copy: arguments required\n"); return NULL; } result = pg_malloc0(sizeof(struct copy_options)); result->before_tofrom = pg_strdup(""); /* initialize for appending */ token = strtokx(args, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; /* The following can be removed when we drop 7.3 syntax support */ if (pg_strcasecmp(token, "binary") == 0) { xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; } /* Handle COPY (SELECT) case */ if (token[0] == '(') { int parens = 1; while (parens > 0) { xstrcat(&result->before_tofrom, " "); xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, "()", "\"'", nonstd_backslash, true, false, pset.encoding); if (!token) goto error; if (token[0] == '(') parens++; else if (token[0] == ')') parens--; } } xstrcat(&result->before_tofrom, " "); xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; /* * strtokx() will not have returned a multi-character token starting with * '.', so we don't need strcmp() here. Likewise for '(', etc, below. */ if (token[0] == '.') { /* handle schema . table */ xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; } if (token[0] == '(') { /* handle parenthesized column list */ for (;;) { xstrcat(&result->before_tofrom, " "); xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, "()", "\"", 0, false, false, pset.encoding); if (!token) goto error; if (token[0] == ')') break; } xstrcat(&result->before_tofrom, " "); xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; } if (pg_strcasecmp(token, "from") == 0) result->from = true; else if (pg_strcasecmp(token, "to") == 0) result->from = false; else goto error; /* { 'filename' | PROGRAM 'command' | STDIN | STDOUT | PSTDIN | PSTDOUT } */ token = strtokx(NULL, whitespace, ";", "'", 0, false, false, pset.encoding); if (!token) goto error; if (pg_strcasecmp(token, "program") == 0) { int toklen; token = strtokx(NULL, whitespace, ";", "'", 0, false, false, pset.encoding); if (!token) goto error; /* * The shell command must be quoted. This isn't fool-proof, but * catches most quoting errors. */ toklen = strlen(token); if (token[0] != '\'' || toklen < 2 || token[toklen - 1] != '\'') goto error; strip_quotes(token, '\'', 0, pset.encoding); result->program = true; result->file = pg_strdup(token); } else if (pg_strcasecmp(token, "stdin") == 0 || pg_strcasecmp(token, "stdout") == 0) { result->file = NULL; } else if (pg_strcasecmp(token, "pstdin") == 0 || pg_strcasecmp(token, "pstdout") == 0) { result->psql_inout = true; result->file = NULL; } else { /* filename can be optionally quoted */ strip_quotes(token, '\'', 0, pset.encoding); result->file = pg_strdup(token); expand_tilde(&result->file); } /* Collect the rest of the line (COPY options) */ token = strtokx(NULL, "", NULL, NULL, 0, false, false, pset.encoding); if (token) result->after_tofrom = pg_strdup(token); /* set data staging options to null */ result->tableName = NULL; result->columnList = NULL; return result; error: if (token) psql_error("\\copy: parse error at \"%s\"\n", token); else psql_error("\\copy: parse error at end of line\n"); free_copy_options(result); return NULL; }
static struct copy_options * parse_slash_copy(const char *args) { struct copy_options *result; char *token; const char *whitespace = " \t\n\r"; char nonstd_backslash = standard_strings() ? 0 : '\\'; if (!args) { psql_error("\\copy: arguments required\n"); return NULL; } result = pg_malloc0(sizeof(struct copy_options)); result->before_tofrom = pg_strdup(""); /* initialize for appending */ token = strtokx(args, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; /* The following can be removed when we drop 7.3 syntax support */ if (pg_strcasecmp(token, "binary") == 0) { xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; } /* Handle COPY (SELECT) case */ if (token[0] == '(') { int parens = 1; while (parens > 0) { xstrcat(&result->before_tofrom, " "); xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, "()", "\"'", nonstd_backslash, true, false, pset.encoding); if (!token) goto error; if (token[0] == '(') parens++; else if (token[0] == ')') parens--; } } xstrcat(&result->before_tofrom, " "); xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; /* * strtokx() will not have returned a multi-character token starting with * '.', so we don't need strcmp() here. Likewise for '(', etc, below. */ if (token[0] == '.') { /* handle schema . table */ xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; } if (token[0] == '(') { /* handle parenthesized column list */ for (;;) { xstrcat(&result->before_tofrom, " "); xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, "()", "\"", 0, false, false, pset.encoding); if (!token) goto error; if (token[0] == ')') break; } xstrcat(&result->before_tofrom, " "); xstrcat(&result->before_tofrom, token); token = strtokx(NULL, whitespace, ".,()", "\"", 0, false, false, pset.encoding); if (!token) goto error; } if (pg_strcasecmp(token, "from") == 0) result->from = true; else if (pg_strcasecmp(token, "to") == 0) result->from = false; else goto error; token = strtokx(NULL, whitespace, NULL, "'", 0, false, true, pset.encoding); if (!token) goto error; if (pg_strcasecmp(token, "stdin") == 0 || pg_strcasecmp(token, "stdout") == 0) { result->psql_inout = false; result->file = NULL; } else if (pg_strcasecmp(token, "pstdin") == 0 || pg_strcasecmp(token, "pstdout") == 0) { result->psql_inout = true; result->file = NULL; } else { result->psql_inout = false; result->file = pg_strdup(token); expand_tilde(&result->file); } /* Collect the rest of the line (COPY options) */ token = strtokx(NULL, "", NULL, NULL, 0, false, false, pset.encoding); if (token) result->after_tofrom = pg_strdup(token); return result; error: if (token) psql_error("\\copy: parse error at \"%s\"\n", token); else psql_error("\\copy: parse error at end of line\n"); free_copy_options(result); return NULL; }
/* * Connect to the server. Returns a valid PGconn pointer if connected, * or NULL on non-permanent error. On permanent error, the function will * call exit(1) directly. */ PGconn * GetConnection(void) { PGconn *tmpconn; int argcount = 4; /* dbname, replication, fallback_app_name, * password */ int i; const char **keywords; const char **values; char *password = NULL; const char *tmpparam; if (dbhost) argcount++; if (dbuser) argcount++; if (dbport) argcount++; keywords = pg_malloc0((argcount + 1) * sizeof(*keywords)); values = pg_malloc0((argcount + 1) * sizeof(*values)); keywords[0] = "dbname"; values[0] = "replication"; keywords[1] = "replication"; values[1] = "true"; keywords[2] = "fallback_application_name"; values[2] = progname; i = 3; if (dbhost) { keywords[i] = "host"; values[i] = dbhost; i++; } if (dbuser) { keywords[i] = "user"; values[i] = dbuser; i++; } if (dbport) { keywords[i] = "port"; values[i] = dbport; i++; } while (true) { if (password) free(password); if (dbpassword) { /* * We've saved a password when a previous connection succeeded, * meaning this is the call for a second session to the same * database, so just forcibly reuse that password. */ keywords[argcount - 1] = "password"; values[argcount - 1] = dbpassword; dbgetpassword = -1; /* Don't try again if this fails */ } else if (dbgetpassword == 1) { password = simple_prompt(_("Password: "******"password"; values[argcount - 1] = password; } tmpconn = PQconnectdbParams(keywords, values, true); /* * If there is too little memory even to allocate the PGconn object * and PQconnectdbParams returns NULL, we call exit(1) directly. */ if (!tmpconn) { fprintf(stderr, _("%s: could not connect to server\n"), progname); exit(1); } if (PQstatus(tmpconn) == CONNECTION_BAD && PQconnectionNeedsPassword(tmpconn) && dbgetpassword != -1) { dbgetpassword = 1; /* ask for password next time */ PQfinish(tmpconn); continue; } if (PQstatus(tmpconn) != CONNECTION_OK) { fprintf(stderr, _("%s: could not connect to server: %s\n"), progname, PQerrorMessage(tmpconn)); PQfinish(tmpconn); free(values); free(keywords); return NULL; } /* Connection ok! */ free(values); free(keywords); /* * Ensure we have the same value of integer timestamps as the server * we are connecting to. */ tmpparam = PQparameterStatus(tmpconn, "integer_datetimes"); if (!tmpparam) { fprintf(stderr, _("%s: could not determine server setting for integer_datetimes\n"), progname); PQfinish(tmpconn); exit(1); } #ifdef HAVE_INT64_TIMESTAMP if (strcmp(tmpparam, "on") != 0) #else if (strcmp(tmpparam, "off") != 0) #endif { fprintf(stderr, _("%s: integer_datetimes compile flag does not match server\n"), progname); PQfinish(tmpconn); exit(1); } /* Store the password for next run */ if (password) dbpassword = password; return tmpconn; } }
/* * parallel_transfer_all_new_dbs * * This has the same API as transfer_all_new_dbs, except it does parallel execution * by transferring multiple tablespaces in parallel */ void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata, char *old_tablespace) { #ifndef WIN32 pid_t child; #else HANDLE child; transfer_thread_arg *new_arg; #endif if (user_opts.jobs <= 1) /* throw_error must be true to allow jobs */ transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata, new_pgdata, NULL); else { /* parallel */ #ifdef WIN32 if (thread_handles == NULL) thread_handles = pg_malloc(user_opts.jobs * sizeof(HANDLE)); if (transfer_thread_args == NULL) { int i; transfer_thread_args = pg_malloc(user_opts.jobs * sizeof(transfer_thread_arg *)); /* * For safety and performance, we keep the args allocated during * the entire life of the process, and we don't free the args in a * thread different from the one that allocated it. */ for (i = 0; i < user_opts.jobs; i++) transfer_thread_args[i] = pg_malloc0(sizeof(transfer_thread_arg)); } cur_thread_args = (void **) transfer_thread_args; #endif /* harvest any dead children */ while (reap_child(false) == true) ; /* must we wait for a dead child? */ if (parallel_jobs >= user_opts.jobs) reap_child(true); /* set this before we start the job */ parallel_jobs++; /* Ensure stdio state is quiesced before forking */ fflush(NULL); #ifndef WIN32 child = fork(); if (child == 0) { transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata, new_pgdata, old_tablespace); /* if we take another exit path, it will be non-zero */ /* use _exit to skip atexit() functions */ _exit(0); } else if (child < 0) /* fork failed */ pg_fatal("could not create worker process: %s\n", strerror(errno)); #else /* empty array element are always at the end */ new_arg = transfer_thread_args[parallel_jobs - 1]; /* Can only pass one pointer into the function, so use a struct */ new_arg->old_db_arr = old_db_arr; new_arg->new_db_arr = new_db_arr; if (new_arg->old_pgdata) pg_free(new_arg->old_pgdata); new_arg->old_pgdata = pg_strdup(old_pgdata); if (new_arg->new_pgdata) pg_free(new_arg->new_pgdata); new_arg->new_pgdata = pg_strdup(new_pgdata); if (new_arg->old_tablespace) pg_free(new_arg->old_tablespace); new_arg->old_tablespace = old_tablespace ? pg_strdup(old_tablespace) : NULL; child = (HANDLE) _beginthreadex(NULL, 0, (void *) win32_transfer_all_new_dbs, new_arg, 0, NULL); if (child == 0) pg_fatal("could not create worker thread: %s\n", strerror(errno)); thread_handles[parallel_jobs - 1] = child; #endif } return; }
/* * parallel_exec_prog * * This has the same API as exec_prog, except it does parallel execution, * and therefore must throw errors and doesn't return an error status. */ void parallel_exec_prog(const char *log_file, const char *opt_log_file, const char *fmt,...) { va_list args; char cmd[MAX_STRING]; #ifndef WIN32 pid_t child; #else HANDLE child; exec_thread_arg *new_arg; #endif va_start(args, fmt); vsnprintf(cmd, sizeof(cmd), fmt, args); va_end(args); if (user_opts.jobs <= 1) /* throw_error must be true to allow jobs */ exec_prog(log_file, opt_log_file, true, "%s", cmd); else { /* parallel */ #ifdef WIN32 if (thread_handles == NULL) thread_handles = pg_malloc(user_opts.jobs * sizeof(HANDLE)); if (exec_thread_args == NULL) { int i; exec_thread_args = pg_malloc(user_opts.jobs * sizeof(exec_thread_arg *)); /* * For safety and performance, we keep the args allocated during * the entire life of the process, and we don't free the args in a * thread different from the one that allocated it. */ for (i = 0; i < user_opts.jobs; i++) exec_thread_args[i] = pg_malloc0(sizeof(exec_thread_arg)); } cur_thread_args = (void **) exec_thread_args; #endif /* harvest any dead children */ while (reap_child(false) == true) ; /* must we wait for a dead child? */ if (parallel_jobs >= user_opts.jobs) reap_child(true); /* set this before we start the job */ parallel_jobs++; /* Ensure stdio state is quiesced before forking */ fflush(NULL); #ifndef WIN32 child = fork(); if (child == 0) /* use _exit to skip atexit() functions */ _exit(!exec_prog(log_file, opt_log_file, true, "%s", cmd)); else if (child < 0) /* fork failed */ pg_fatal("could not create worker process: %s\n", strerror(errno)); #else /* empty array element are always at the end */ new_arg = exec_thread_args[parallel_jobs - 1]; /* Can only pass one pointer into the function, so use a struct */ if (new_arg->log_file) pg_free(new_arg->log_file); new_arg->log_file = pg_strdup(log_file); if (new_arg->opt_log_file) pg_free(new_arg->opt_log_file); new_arg->opt_log_file = opt_log_file ? pg_strdup(opt_log_file) : NULL; if (new_arg->cmd) pg_free(new_arg->cmd); new_arg->cmd = pg_strdup(cmd); child = (HANDLE) _beginthreadex(NULL, 0, (void *) win32_exec_prog, new_arg, 0, NULL); if (child == 0) pg_fatal("could not create worker thread: %s\n", strerror(errno)); thread_handles[parallel_jobs - 1] = child; #endif } return; }
/* * Initiate background process for receiving xlog during the backup. * The background stream will use its own database connection so we can * stream the logfile in parallel with the backups. */ static void StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier) { logstreamer_param *param; uint32 hi, lo; param = pg_malloc0(sizeof(logstreamer_param)); param->timeline = timeline; param->sysidentifier = sysidentifier; /* Convert the starting position */ if (sscanf(startpos, "%X/%X", &hi, &lo) != 2) { fprintf(stderr, _("%s: could not parse transaction log location \"%s\"\n"), progname, startpos); disconnect_and_exit(1); } param->startptr = ((uint64) hi) << 32 | lo; /* Round off to even segment position */ param->startptr -= param->startptr % XLOG_SEG_SIZE; #ifndef WIN32 /* Create our background pipe */ if (pipe(bgpipe) < 0) { fprintf(stderr, _("%s: could not create pipe for background process: %s\n"), progname, strerror(errno)); disconnect_and_exit(1); } #endif /* Get a second connection */ param->bgconn = GetConnection(); if (!param->bgconn) /* Error message already written in GetConnection() */ exit(1); /* * Always in plain format, so we can write to basedir/pg_xlog. But the * directory entry in the tar file may arrive later, so make sure it's * created before we start. */ snprintf(param->xlogdir, sizeof(param->xlogdir), "%s/pg_xlog", basedir); verify_dir_is_empty_or_create(param->xlogdir); /* * Start a child process and tell it to start streaming. On Unix, this is * a fork(). On Windows, we create a thread. */ #ifndef WIN32 bgchild = fork(); if (bgchild == 0) { /* in child process */ exit(LogStreamerMain(param)); } else if (bgchild < 0) { fprintf(stderr, _("%s: could not create background process: %s\n"), progname, strerror(errno)); disconnect_and_exit(1); } /* * Else we are in the parent process and all is well. */ #else /* WIN32 */ bgchild = _beginthreadex(NULL, 0, (void *) LogStreamerMain, param, 0, NULL); if (bgchild == 0) { fprintf(stderr, _("%s: could not create background thread: %s\n"), progname, strerror(errno)); disconnect_and_exit(1); } #endif }
/* * Connect to the server. Returns a valid PGconn pointer if connected, * or NULL on non-permanent error. On permanent error, the function will * call exit(1) directly. */ PGconn * GetConnection(void) { PGconn *tmpconn; int argcount = 7; /* dbname, replication, fallback_app_name, * host, user, port, password */ int i; const char **keywords; const char **values; const char *tmpparam; bool need_password; PQconninfoOption *conn_opts = NULL; PQconninfoOption *conn_opt; char *err_msg = NULL; /* * Merge the connection info inputs given in form of connection string, * options and default values (dbname=replication, replication=true, etc.) */ i = 0; if (connection_string) { conn_opts = PQconninfoParse(connection_string, &err_msg); if (conn_opts == NULL) { fprintf(stderr, "%s: %s", progname, err_msg); exit(1); } for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++) { if (conn_opt->val != NULL && conn_opt->val[0] != '\0') argcount++; } keywords = pg_malloc0((argcount + 1) * sizeof(*keywords)); values = pg_malloc0((argcount + 1) * sizeof(*values)); for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++) { if (conn_opt->val != NULL && conn_opt->val[0] != '\0') { keywords[i] = conn_opt->keyword; values[i] = conn_opt->val; i++; } } } else { keywords = pg_malloc0((argcount + 1) * sizeof(*keywords)); values = pg_malloc0((argcount + 1) * sizeof(*values)); } keywords[i] = "dbname"; values[i] = "replication"; i++; keywords[i] = "replication"; values[i] = "true"; i++; keywords[i] = "fallback_application_name"; values[i] = progname; i++; if (dbhost) { keywords[i] = "host"; values[i] = dbhost; i++; } if (dbuser) { keywords[i] = "user"; values[i] = dbuser; i++; } if (dbport) { keywords[i] = "port"; values[i] = dbport; i++; } /* If -W was given, force prompt for password, but only the first time */ need_password = (dbgetpassword == 1 && dbpassword == NULL); while (true) { /* Get a new password if appropriate */ if (need_password) { if (dbpassword) free(dbpassword); dbpassword = simple_prompt(_("Password: "******"password"; values[i] = dbpassword; } else { keywords[i] = NULL; values[i] = NULL; } tmpconn = PQconnectdbParams(keywords, values, true); /* * If there is too little memory even to allocate the PGconn object * and PQconnectdbParams returns NULL, we call exit(1) directly. */ if (!tmpconn) { fprintf(stderr, _("%s: could not connect to server\n"), progname); exit(1); } /* If we need a password and -w wasn't given, loop back and get one */ if (PQstatus(tmpconn) == CONNECTION_BAD && PQconnectionNeedsPassword(tmpconn) && dbgetpassword != -1) { PQfinish(tmpconn); need_password = true; } else break; } if (PQstatus(tmpconn) != CONNECTION_OK) { fprintf(stderr, _("%s: could not connect to server: %s\n"), progname, PQerrorMessage(tmpconn)); PQfinish(tmpconn); free(values); free(keywords); if (conn_opts) PQconninfoFree(conn_opts); return NULL; } /* Connection ok! */ free(values); free(keywords); if (conn_opts) PQconninfoFree(conn_opts); /* * Ensure we have the same value of integer timestamps as the server we * are connecting to. */ tmpparam = PQparameterStatus(tmpconn, "integer_datetimes"); if (!tmpparam) { fprintf(stderr, _("%s: could not determine server setting for integer_datetimes\n"), progname); PQfinish(tmpconn); exit(1); } #ifdef HAVE_INT64_TIMESTAMP if (strcmp(tmpparam, "on") != 0) #else if (strcmp(tmpparam, "off") != 0) #endif { fprintf(stderr, _("%s: integer_datetimes compile flag does not match server\n"), progname); PQfinish(tmpconn); exit(1); } return tmpconn; }