/* * dump.c * * dump functions * * Copyright (c) 2010-2026, PostgreSQL Global Development Group * src/bin/pg_upgrade/dump.c */ #include "postgres_fe.h" #include "fe_utils/string_utils.h" #include "pg_upgrade.h" void generate_old_dump(void) { int dbnum; prep_status("Creating dump of global objects"); /* run new pg_dumpall binary for globals */ exec_prog(UTILITY_LOG_FILE, NULL, true, true, "\"%s/pg_dumpall\" %s%s --globals-only --quote-all-identifiers " "--binary-upgrade %s --no-sync -f \"%s/%s\"", new_cluster.bindir, cluster_conn_opts(&old_cluster), protocol_negotiation_supported(&old_cluster) ? "" : " -d \"max_protocol_version=3.0\"", log_opts.verbose ? "--verbose" : "", log_opts.dumpdir, GLOBALS_DUMP_FILE); check_ok(); prep_status_progress("Creating dump of database schemas"); /* create per-db dump files */ for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++) { char sql_file_name[MAXPGPATH], /* [PG_CUSTOM: RUDB_UPGRADE] BEGIN: Системные данные RuDB переносятся отдельно от relation-file upgrade. */ log_file_name[MAXPGPATH], product_file_name[MAXPGPATH], product_log_file_name[MAXPGPATH], product_catalog_file_name[MAXPGPATH], product_catalog_log_file_name[MAXPGPATH]; DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum]; PGconn *conn; PGresult *product_schema_res; const char *product_schema_opts; PQExpBufferData connstr, escaped_connstr, product_catalog_opts; bool has_sys; bool has_vectorizer; /* [PG_CUSTOM: RUDB_UPGRADE] END */ initPQExpBuffer(&connstr); appendPQExpBufferStr(&connstr, "dbname="); appendConnStrVal(&connstr, old_db->db_name); if (!protocol_negotiation_supported(&old_cluster)) appendPQExpBufferStr(&connstr, " max_protocol_version=3.0"); initPQExpBuffer(&escaped_connstr); appendShellString(&escaped_connstr, connstr.data); termPQExpBuffer(&connstr); pg_log(PG_STATUS, "%s", old_db->db_name); /* [PG_CUSTOM: RUDB_UPGRADE] BEGIN: Системные данные RuDB переносятся отдельно от relation-file upgrade. */ conn = connectToServer(&old_cluster, old_db->db_name); product_schema_res = executeQueryOrDie(conn, "SELECT " "EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = 'SYS'), " "EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = 'pgedge_vectorizer'), " "EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'pg_catalog' AND c.relname = 'rudb_awr_snapshot_id_seq'), " "EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'pg_catalog' AND c.relname = 'rudb_awr_snapshots'), " "EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'pg_catalog' AND c.relname = 'rudb_awr_settings'), " "EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'pg_catalog' AND c.relname = 'rudb_ash_history_sqltext'), " "EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'pg_catalog' AND c.relname = 'rudb_ash_history_samples'), " "EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'pg_catalog' AND c.relname = 'dbms_soda_collections'), " "EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'pg_catalog' AND c.relname = 'dbms_soda_indexes'), " "EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'SYS' AND c.relname = 'AQ$_SYSTEM_PROPS' AND c.relkind = 'r')"); old_db->has_aq_system_props = strcmp(PQgetvalue(product_schema_res, 0, 9), "t") == 0; has_sys = strcmp(PQgetvalue(product_schema_res, 0, 0), "t") == 0; has_vectorizer = strcmp(PQgetvalue(product_schema_res, 0, 1), "t") == 0; initPQExpBuffer(&product_catalog_opts); if (strcmp(PQgetvalue(product_schema_res, 0, 2), "t") == 0) appendPQExpBufferStr(&product_catalog_opts, " --table=pg_catalog.rudb_awr_snapshot_id_seq"); if (strcmp(PQgetvalue(product_schema_res, 0, 3), "t") == 0) appendPQExpBufferStr(&product_catalog_opts, " --table=pg_catalog.rudb_awr_snapshots"); if (strcmp(PQgetvalue(product_schema_res, 0, 4), "t") == 0) appendPQExpBufferStr(&product_catalog_opts, " --table=pg_catalog.rudb_awr_settings"); if (strcmp(PQgetvalue(product_schema_res, 0, 5), "t") == 0) appendPQExpBufferStr(&product_catalog_opts, " --table=pg_catalog.rudb_ash_history_sqltext"); if (strcmp(PQgetvalue(product_schema_res, 0, 6), "t") == 0) appendPQExpBufferStr(&product_catalog_opts, " --table=pg_catalog.rudb_ash_history_samples"); if (strcmp(PQgetvalue(product_schema_res, 0, 7), "t") == 0) appendPQExpBufferStr(&product_catalog_opts, " --table=pg_catalog.dbms_soda_collections"); if (strcmp(PQgetvalue(product_schema_res, 0, 8), "t") == 0) appendPQExpBufferStr(&product_catalog_opts, " --table=pg_catalog.dbms_soda_indexes"); if (product_catalog_opts.len == 0) appendPQExpBufferStr(&product_catalog_opts, " --exclude-schema='*'"); PQclear(product_schema_res); PQfinish(conn); if (has_sys && has_vectorizer) product_schema_opts = "--schema=\\\"SYS\\\" --schema=pgedge_vectorizer"; else if (has_sys) product_schema_opts = "--schema=\\\"SYS\\\""; else if (has_vectorizer) product_schema_opts = "--schema=pgedge_vectorizer"; else product_schema_opts = "--exclude-schema='*'"; snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid); snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid); snprintf(product_file_name, sizeof(product_file_name), PRODUCT_DATA_DUMP_FILE_MASK, old_db->db_oid); snprintf(product_log_file_name, sizeof(product_log_file_name), PRODUCT_DATA_DUMP_LOG_FILE_MASK, old_db->db_oid); snprintf(product_catalog_file_name, sizeof(product_catalog_file_name), PRODUCT_CATALOG_DATA_DUMP_FILE_MASK, old_db->db_oid); snprintf(product_catalog_log_file_name, sizeof(product_catalog_log_file_name), PRODUCT_CATALOG_DATA_DUMP_LOG_FILE_MASK, old_db->db_oid); /* [PG_CUSTOM: RUDB_UPGRADE] END */ parallel_exec_prog(log_file_name, NULL, "\"%s/pg_dump\" %s --no-data %s %s --quote-all-identifiers " "--binary-upgrade --format=custom %s --no-sync --file=\"%s/%s\" %s", new_cluster.bindir, cluster_conn_opts(&old_cluster), (user_opts.transfer_mode == TRANSFER_MODE_SWAP) ? "" : "--sequence-data", log_opts.verbose ? "--verbose" : "", user_opts.do_statistics ? "--statistics" : "--no-statistics", log_opts.dumpdir, sql_file_name, escaped_connstr.data); /* [PG_CUSTOM: RUDB_UPGRADE] BEGIN: Системные данные RuDB переносятся отдельно от relation-file upgrade. */ /* * RuDB creates product schemas during initdb. Their relation OIDs are * therefore version-specific bootstrap OIDs and cannot be transferred * by pg_upgrade's ordinary relation-file map. Preserve their state * separately as logical data; definitions come from the new initdb. */ parallel_exec_prog(product_log_file_name, NULL, "\"%s/pg_dump\" %s --data-only %s " "%s --quote-all-identifiers --format=custom --no-sync " "--file=\"%s/%s\" %s", new_cluster.bindir, cluster_conn_opts(&old_cluster), product_schema_opts, log_opts.verbose ? "--verbose" : "", log_opts.dumpdir, product_file_name, escaped_connstr.data); /* * AWR/ASH and SODA keep persistent RuDB state in pg_catalog rather * than in a dedicated product schema. Explicit table selection is * required because pg_dump excludes system catalogs by default. * Keep this archive separate from the schema archive: pg_dump treats * --table and --schema as intersecting selection, not as a union. */ parallel_exec_prog(product_catalog_log_file_name, NULL, "\"%s/pg_dump\" %s --data-only %s " "%s --quote-all-identifiers --format=custom --no-sync " "--file=\"%s/%s\" %s", new_cluster.bindir, cluster_conn_opts(&old_cluster), product_catalog_opts.data, log_opts.verbose ? "--verbose" : "", log_opts.dumpdir, product_catalog_file_name, escaped_connstr.data); termPQExpBuffer(&product_catalog_opts); /* [PG_CUSTOM: RUDB_UPGRADE] END */ termPQExpBuffer(&escaped_connstr); } /* reap all children */ while (reap_child(true) == true) ; end_progress_output(); check_ok(); }