diff --git a/.github/workflows/test-master.yml b/.github/workflows/test-master.yml new file mode 100644 index 00000000..d948f917 --- /dev/null +++ b/.github/workflows/test-master.yml @@ -0,0 +1,101 @@ +name: pgactive PG-Master CI +on: + #schedule: + # # Runs every day at 5am. + # - cron: '0 5 * * *' + #push: + # paths-ignore: + # - 'docs/**' + #pull_request: + # paths-ignore: + # - 'docs/**' + workflow_dispatch: +jobs: + test: + defaults: + run: + shell: sh + + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + version: [ + master + ] + + runs-on: ${{ matrix.os }} + timeout-minutes: 120 + + steps: + - name: Checkout pgactive + uses: actions/checkout@v4 + with: + path: pgactive + + - name: Checkout Postgres + run: | + sudo apt-get -y -q install libipc-run-perl build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt-dev libssl-dev libxml2-utils xsltproc ccache pkg-config libicu-dev + git clone --depth 1 --branch ${{ matrix.version }} https://github.com/postgres/postgres.git + + - if: ${{ matrix.version == 'REL_11_STABLE' }} + name: Apply per-test-script runtime display patch (REL_11_STABLE only) + run: | + cd postgres + git apply $GITHUB_WORKSPACE/pgactive/src/compat/11/v1-0001-Add-per-test-script-runtime-display-to-pg_regress.patch + git status + + - name: Build Postgres + run: | + cd $GITHUB_WORKSPACE/postgres + sh configure --prefix=$PWD/inst/ --enable-debug --enable-cassert --enable-tap-tests CFLAGS="-ggdb3 -O0" + make -j4 install + + # Install extensions required for pgactive tests + make -C contrib/btree_gist install + make -C contrib/cube install + make -C contrib/hstore install + make -C contrib/pg_trgm install + + - name: Check pgactive code indentation + run: | + cd $GITHUB_WORKSPACE/pgactive + ls -1 src/compat/??/pg_dump/*.[ch] >$GITHUB_WORKSPACE/postgres/pgindent.ignore + + cd $GITHUB_WORKSPACE/postgres + make -C src/tools/pg_bsd_indent/ -j4 install + src/tools/pgindent/pgindent --indent=$GITHUB_WORKSPACE/postgres/src/tools/pg_bsd_indent/pg_bsd_indent --excludes=pgindent.ignore --diff $GITHUB_WORKSPACE/pgactive > pgindent.diffs + test -s pgindent.diffs && cat pgindent.diffs && exit 1 || exit 0 + + - name: Build pgactive + run: | + cd pgactive + PATH=$GITHUB_WORKSPACE/postgres/inst/bin:"$PATH" + sh configure + make PROFILE="-Wall -Wmissing-prototypes -Werror=maybe-uninitialized -Werror" -j1 all install + + - name: Run pgactive core tests + run: | + cd pgactive + make regress_check + + - name: Show pgactive core tests diff + if: ${{ failure() }} + run: | + cat pgactive/test/regression.diffs + + - name: Run pgactive extended tests + run: | + cd pgactive + make PROVE_FLAGS="--timer -v" prove_check + + - name: Upload test artifacts + if: ${{ failure() }} + uses: actions/upload-artifact@v4 + with: + name: test-artifact-${{ matrix.os }}-${{ matrix.version }} + path: | + postgres/pgindent.diffs + pgactive/test/regression.diffs + pgactive/test/tmp_check/log + retention-days: 1 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 83c92e7d..58ae458e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -68,22 +68,6 @@ jobs: make -C contrib/hstore install make -C contrib/pg_trgm install - - if: ${{ matrix.version == 'REL_17_STABLE' }} - name: Check pgactive code indentation (REL_17_STABLE only) - run: | - cd postgres - make -C src/tools/pg_bsd_indent/ -j4 install - cat << EOF >> pgindent.ignore - src/compat/13/pg_dump/* - src/compat/14/pg_dump/* - src/compat/15/pg_dump/* - src/compat/16/pg_dump/* - src/compat/17/pg_dump/* - src/compat/18/pg_dump/* - EOF - src/tools/pgindent/pgindent --indent=$GITHUB_WORKSPACE/postgres/src/tools/pg_bsd_indent/pg_bsd_indent --excludes=pgindent.ignore --diff $GITHUB_WORKSPACE/pgactive > pgindent.diffs - test -s pgindent.diffs && cat pgindent.diffs && exit 1 || exit 0 - - name: Build pgactive run: | cd pgactive diff --git a/include/pgactive.h b/include/pgactive.h index 305ec41b..74487886 100644 --- a/include/pgactive.h +++ b/include/pgactive.h @@ -37,6 +37,15 @@ #include "pgactive_compat.h" #include "nodes/execnodes.h" +/* pg_fallthrough was introduced in PG 19 (c.h) */ +#ifndef pg_fallthrough +#if __has_attribute(fallthrough) +#define pg_fallthrough __attribute__((fallthrough)) +#else +#define pg_fallthrough +#endif +#endif + #define NODEID_BITS 10 #define MAX_NODE_ID ((1 << NODEID_BITS) - 1) @@ -446,11 +455,12 @@ extern bool pgactive_log_conflicts_to_table; extern bool pgactive_log_conflicts_to_logfile; extern bool pgactive_conflict_logging_include_tuples; -/* replaced by pgactive_skip_ddl_replication for now -extern bool pgactive_permit_ddl_locking; -extern bool pgactive_permit_unsafe_commands; -extern bool pgactive_skip_ddl_locking; -*/ +/* + * replaced by pgactive_skip_ddl_replication for now + * extern bool pgactive_permit_ddl_locking; + * extern bool pgactive_permit_unsafe_commands; + * extern bool pgactive_skip_ddl_locking; + */ extern bool pgactive_skip_ddl_replication; extern bool prev_pgactive_skip_ddl_replication; extern bool pgactive_do_not_replicate; @@ -774,7 +784,7 @@ extern struct pg_conn *pgactive_establish_connection_and_slot(const char *dsn, const char *application_name_suffix, Name out_slot_name, pgactiveNodeId * out_nodeid, - RepOriginId *out_rep_origin_id, + RepOriginId * out_rep_origin_id, char *out_snapshot); extern PGconn *pgactive_connect_nonrepl(const char *connstring, diff --git a/meson.build b/meson.build new file mode 100644 index 00000000..20a78061 --- /dev/null +++ b/meson.build @@ -0,0 +1,390 @@ +project( + 'pgactive', + 'c', + version: '2.1.7', + license: 'Apache-2.0', + meson_version: '>= 0.57.0', + default_options: [ + 'c_std=gnu99', + 'warning_level=2', + ], +) + +# PostgreSQL server headers require POSIX/GNU extensions +add_project_arguments('-D_GNU_SOURCE', language: 'c') + +# ---- PostgreSQL detection ---- +pg_config = find_program( + get_option('pg_config'), + required: true, +) + +pg_bindir = run_command(pg_config, '--bindir', check: true).stdout().strip() +pg_includedir = run_command(pg_config, '--includedir', check: true).stdout().strip() +pg_includedir_server = run_command(pg_config, '--includedir-server', check: true).stdout().strip() +pg_pkglibdir = run_command(pg_config, '--pkglibdir', check: true).stdout().strip() +pg_sharedir = run_command(pg_config, '--sharedir', check: true).stdout().strip() +pg_libdir = run_command(pg_config, '--libdir', check: true).stdout().strip() +pg_pgxs = run_command(pg_config, '--pgxs', check: true).stdout().strip() +pg_version = run_command(pg_config, '--version', check: true).stdout().strip() + +# Extract major version number (e.g. "PostgreSQL 18.0" → "18", "PostgreSQL 19devel" → "19", +# "PostgreSQL 19beta1" → "19"). Matches configure.in logic: strip non-digits, take first 2. +pg_major = run_command('sh', '-c', + 'echo "$1" | sed "s/[^0-9]//g" | cut -c 1-2', + 'sh', pg_version.split(' ')[1], + check: true).stdout().strip() +message('Building against PostgreSQL @0@'.format(pg_major)) + +pg_inc = include_directories(pg_includedir, pg_includedir_server) +pg_inc_internal = include_directories(pg_includedir / 'postgresql' / 'internal') + +# pgxs directory (for test perl modules and pg_regress) +pgxs_dir = run_command('dirname', pg_pgxs, check: true).stdout().strip() +pgxs_basedir = run_command('dirname', pgxs_dir, check: true).stdout().strip() + +# libpq +libpq = dependency('libpq', required: false) +if not libpq.found() + libpq = declare_dependency( + include_directories: include_directories( + run_command(pg_config, '--includedir', check: true).stdout().strip(), + ), + dependencies: [ + meson.get_compiler('c').find_library('pq', + dirs: [pg_libdir], + ), + ], + ) +endif + +# pgfeutils (needed by pgactive_dump) +cc = meson.get_compiler('c') +libpgfeutils = cc.find_library('pgfeutils', dirs: [pg_libdir], required: false) +libpgcommon = cc.find_library('pgcommon', dirs: [pg_libdir], required: false) +libpgport = cc.find_library('pgport', dirs: [pg_libdir], required: false) + +# ---- Version header generation ---- +git = find_program('git', required: false) +if git.found() + git_hash = run_command( + git, 'rev-parse', '--short', 'HEAD', + check: false, + ).stdout().strip() +else + git_hash = 'unknown' +endif + +version_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() + +version_h = configure_file( + input: 'include/pgactive_version.h.in', + output: 'pgactive_version.h', + configuration: { + 'pgactive_VERSION_DATE': '"@0@"'.format(version_date), + 'pgactive_VERSION_GITHASH': '"@0@"'.format(git_hash), + }, +) + +# ---- Config header generation (pgactive_config_generated.h) ---- +cdata = configuration_data() + +cdata.set('HAVE_ACCESS_COMMITTS_H', + cc.has_header('access/committs.h', include_directories: pg_inc) ? 1 : false) +cdata.set('HAVE_ACCESS_COMMIT_TS_H', + cc.has_header('access/commit_ts.h', include_directories: pg_inc) ? 1 : false) +cdata.set('HAVE_REPLICATION_ORIGIN_H', + cc.has_header('replication/origin.h', include_directories: pg_inc) ? 1 : false) +cdata.set('HAVE_REPLICATION_REPLICATION_IDENTIFIER_H', + cc.has_header('replication/replication_identifier.h', include_directories: pg_inc) ? 1 : false) + +cdata.set_quoted('PACKAGE_BUGREPORT', '') +cdata.set_quoted('PACKAGE_NAME', 'pgactive') +cdata.set_quoted('PACKAGE_STRING', 'pgactive ' + meson.project_version()) +cdata.set_quoted('PACKAGE_TARNAME', 'pgactive') +cdata.set_quoted('PACKAGE_URL', '') +cdata.set_quoted('PACKAGE_VERSION', meson.project_version()) + +configure_file( + input: 'include/pgactive_config_generated.h.in', + output: 'pgactive_config_generated.h', + configuration: cdata, +) + +# ---- Include directories ---- +# '.' adds both source root and build root (where generated headers land) +pgactive_inc = include_directories( + '.', + 'include', + 'src/compat/@0@'.format(pg_major), +) + +# ---- Shared library: pgactive.so ---- +pgactive_sources = files( + 'src/pgactive.c', + 'src/pgactive_apply.c', + 'src/pgactive_catalogs.c', + 'src/pgactive_commandfilter.c', + 'src/pgactive_common.c', + 'src/pgactive_conflict_handlers.c', + 'src/pgactive_conflict_logging.c', + 'src/pgactive_count.c', + 'src/pgactive_dbcache.c', + 'src/pgactive_ddlrep.c', + 'src/pgactive_ddlrep_truncate.c', + 'src/pgactive_elog.c', + 'src/pgactive_executor.c', + 'src/pgactive_init_replica.c', + 'src/pgactive_locks.c', + 'src/pgactive_messaging.c', + 'src/pgactive_monitoring.c', + 'src/pgactive_node_identifier.c', + 'src/pgactive_nodecache.c', + 'src/pgactive_output.c', + 'src/pgactive_perdb.c', + 'src/pgactive_protocol.c', + 'src/pgactive_relcache.c', + 'src/pgactive_remotecalls.c', + 'src/pgactive_seq.c', + 'src/pgactive_shmem.c', + 'src/pgactive_supervisor.c', + 'src/pgactive_user_mapping.c', +) + +# OpenSSL and compression libs (needed by server headers and pgactive_dump) +libssl = dependency('openssl', required: false) +libz = dependency('zlib', required: false) +liblz4 = dependency('liblz4', required: false) +libzstd = dependency('libzstd', required: false) + +# On macOS, shared modules loaded into postgres need -bundle_loader +pg_backend = pg_bindir / 'postgres' +pgactive_link_args = [] +if host_machine.system() == 'darwin' + pgactive_link_args += ['-bundle_loader', pg_backend] +endif + +pgactive_lib = shared_module( + 'pgactive', + pgactive_sources, + include_directories: [pg_inc, pgactive_inc], + dependencies: [libpq, libssl], + c_args: ['-DMODULE_PATHNAME="$libdir/pgactive"'], + link_args: pgactive_link_args, + name_prefix: '', + install: true, + install_dir: pg_pkglibdir, +) + +# ---- pgactive_init_copy binary ---- +pgactive_init_copy = executable( + 'pgactive_init_copy', + files( + 'src/pgactive_init_copy.c', + 'src/pgactive_common.c', + ), + include_directories: [pg_inc, pg_inc_internal, pgactive_inc], + dependencies: [libpq, libpgport, libpgcommon], + install: true, + install_dir: pg_bindir, +) + +# ---- pgactive_dump binary ---- +dump_dir = 'src/compat/@0@/pg_dump'.format(pg_major) + +pgactive_dump_sources = files( + dump_dir / 'pg_dump.c', + dump_dir / 'common.c', + dump_dir / 'pg_dump_sort.c', + dump_dir / 'pg_backup_archiver.c', + dump_dir / 'pg_backup_db.c', + dump_dir / 'pg_backup_custom.c', + dump_dir / 'pg_backup_null.c', + dump_dir / 'pg_backup_tar.c', + dump_dir / 'pg_backup_directory.c', + dump_dir / 'pg_backup_utils.c', + dump_dir / 'parallel.c', + dump_dir / 'dumputils.c', + dump_dir / 'compress_io.c', + dump_dir / 'string_utils.c', +) + +# Conditional sources (PG >= 16) +foreach f : ['compress_lz4.c', 'compress_gzip.c', 'compress_none.c', 'compress_zstd.c', 'filter.c'] + p = dump_dir / f + if run_command('test', '-f', p, check: false).returncode() == 0 + pgactive_dump_sources += files(p) + endif +endforeach + +# connectdb.c (PG >= 18) +connectdb_path = dump_dir / 'connectdb.c' +if run_command('test', '-f', connectdb_path, check: false).returncode() == 0 + pgactive_dump_sources += files(connectdb_path) +endif + +pgactive_dump_inc = include_directories(dump_dir) + + +pgactive_dump = executable( + 'pgactive_dump', + pgactive_dump_sources, + include_directories: [pg_inc, pg_inc_internal, pgactive_inc, pgactive_dump_inc], + dependencies: [libpq, libpgfeutils, libpgport, libpgcommon, libssl, libz, liblz4, libzstd], + install: true, + install_dir: pg_bindir, +) + +# ---- Extension data files ---- +install_data( + 'pgactive.control', + install_dir: pg_sharedir / 'extension', +) + +sql_files = run_command( + 'find', '.', '-maxdepth', '1', '-name', 'pgactive--*.sql', + check: true, +).stdout().strip().split('\n') + +foreach f : sql_files + install_data(f, install_dir: pg_sharedir / 'extension') +endforeach + + +# ===================================================================== +# Tests +# ===================================================================== +if get_option('tests') + + pg_regress_bin = pg_pgxs.replace('/pgxs/src/makefiles/pgxs.mk', + '/pgxs/src/test/regress/pg_regress') + + prove = find_program('prove', required: false) + + # GNU getopt is required by run_tests script (macOS ships BSD getopt) + gnu_getopt = find_program('getopt', dirs: ['/opt/homebrew/opt/gnu-getopt/bin', '/usr/local/opt/gnu-getopt/bin'], required: false) + getopt_dir = '' + if gnu_getopt.found() + getopt_dir = run_command('dirname', gnu_getopt.full_path(), check: true).stdout().strip() + ':' + endif + + # Common env for both test targets + test_env_base = { + 'PATH': getopt_dir + pg_bindir + ':' + run_command('sh', '-c', 'echo $PATH', check: true).stdout().strip(), + 'PG_CONFIG': pg_config.full_path(), + 'PG_REGRESS': pg_regress_bin, + } + + # ------------------------------------------------------------------ + # regress_check — pg_regress based SQL regression tests + # + # Mirrors: make regress_check + # ./test/run_tests --config test/pgactive_regress.conf \ + # --testbinary src/test/regress/pg_regress + # ------------------------------------------------------------------ + + # DDL tests (transparent DDL replication) + ddl_tests = [ + 'ddl_enable_ddl', + 'ddl_create', + 'ddl_alter_table', + 'ddl_extension', + 'ddl_function', + 'ddl_grant', + 'ddl_mixed', + 'ddl_namespace', + 'ddl_sequence', + 'ddl_view', + 'ddl_disable_ddl', + ] + + # DDL tests via pgactive_replicate_ddl_command wrapper + ddl_fn_tests = [] + foreach t : ddl_tests + ddl_fn_tests += 'ddl_fn/' + t + endforeach + + regress_tests = [ + 'init', + 'init_pgactive', + 'guc', + 'schema_desync', + 'seq', + 'ddl_lock', + ] + ddl_tests + ddl_fn_tests + [ + 'dml_basic', + 'dml_contrib', + 'dml_delete_pk', + 'dml_extended', + 'dml_replicate_ddl_command', + 'dml_missing_pk', + 'dml_toasted', + 'detach_pgactive', + ] + + # Build the run_tests wrapper from run_tests.in + run_tests_script = configure_file( + input: 'test/run_tests.in', + output: 'run_tests', + configuration: { + 'PG_CONFIG': pg_config.full_path(), + 'srcdir': meson.current_source_dir(), + 'abs_srcdir': meson.current_source_dir(), + 'pgactive_PG_MAJORVERSION': pg_major, + }, + ) + + # regress_check: uses the run_tests wrapper just like the Makefile + test( + 'regress_check', + find_program('bash'), + args: [ + run_tests_script, + '--config', meson.current_source_dir() / 'test' / 'pgactive_regress.conf', + '--testbinary', 'src/test/regress/pg_regress', + ] + regress_tests, + env: test_env_base, + depends: [pgactive_lib, pgactive_init_copy, pgactive_dump], + timeout: 1200, + is_parallel: false, + suite: 'regress', + ) + + # ------------------------------------------------------------------ + # prove_check — Perl TAP tests + # + # Mirrors: make prove_check + # cd $(srcdir) && prove test/t/*.pl + # ------------------------------------------------------------------ + if prove.found() + # Determine the pgxs test perl dir for PostgreSQL::Test::Cluster etc. + pgxs_test_perl = pgxs_basedir / 'src' / 'test' / 'perl' + + prove_env = test_env_base + prove_env += { + 'TESTDATADIR': meson.current_build_dir() / 'tmp_check', + 'TESTLOGDIR': meson.current_build_dir() / 'tmp_check' / 'log', + 'PGPORT': '65432', + 'top_builddir': meson.current_build_dir(), + 'srcdir': meson.current_source_dir(), + # Ensure perl can find PostgreSQL::Test::Cluster + 'PERL5LIB': pgxs_test_perl, + } + + test( + 'prove_check', + prove, + args: [ + '--verbose', + '--timer', + meson.current_source_dir() / 'test' / 't', + ], + env: prove_env, + depends: [pgactive_lib, pgactive_init_copy, pgactive_dump], + timeout: 1800, + is_parallel: false, + suite: 'tap', + ) + endif + +endif diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 00000000..786cbe33 --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,4 @@ +option('pg_config', type: 'string', value: 'pg_config', + description: 'Path to pg_config') +option('tests', type: 'boolean', value: true, + description: 'Enable regress_check and prove_check test targets') diff --git a/src/compat/19/pg_dump.patch b/src/compat/19/pg_dump.patch new file mode 100644 index 00000000..047112ae --- /dev/null +++ b/src/compat/19/pg_dump.patch @@ -0,0 +1,87 @@ +diff --git a/src/compat/19/pg_dump/pg_backup.h b/src/compat/19/pg_dump/pg_backup.h +index fda912b..5fbeae4 100644 +--- a/src/compat/19/pg_dump/pg_backup.h ++++ b/src/compat/19/pg_dump/pg_backup.h +@@ -173,6 +173,7 @@ typedef struct _dumpOptions + ConnParams cparams; + + int binary_upgrade; ++ int pgactive_init_node; + + /* various user-settable parameters */ + int dumpSections; /* bitmask of chosen sections */ +diff --git a/src/compat/19/pg_dump/pg_dump.c b/src/compat/19/pg_dump/pg_dump.c +index d56dcc7..f6b479e 100644 +--- a/src/compat/19/pg_dump/pg_dump.c ++++ b/src/compat/19/pg_dump/pg_dump.c +@@ -492,6 +492,7 @@ main(int argc, char **argv) + */ + {"attribute-inserts", no_argument, &dopt.column_inserts, 1}, + {"binary-upgrade", no_argument, &dopt.binary_upgrade, 1}, ++ {"pgactive-init-node", no_argument, &dopt.pgactive_init_node, 1}, + {"column-inserts", no_argument, &dopt.column_inserts, 1}, + {"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1}, + {"disable-triggers", no_argument, &dopt.disable_triggers, 1}, +@@ -10247,6 +10248,8 @@ shouldPrintColumn(const DumpOptions *dopt, const TableInfo *tbinfo, int colno) + { + if (dopt->binary_upgrade) + return true; ++ if (dopt->pgactive_init_node) ++ return true; + if (tbinfo->attisdropped[colno]) + return false; + return (tbinfo->attislocal[colno] || tbinfo->ispartition); +@@ -16309,7 +16312,7 @@ dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo) + res = ExecuteSqlQueryForSingleRow(fout, query->data); + fdwname = PQgetvalue(res, 0, 0); + +- appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname); ++ appendPQExpBuffer(q, "CREATE SERVER IF NOT EXISTS %s", qsrvname); + if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0) + { + appendPQExpBufferStr(q, " TYPE "); +@@ -16437,7 +16440,7 @@ dumpUserMappings(Archive *fout, + umoptions = PQgetvalue(res, i, i_umoptions); + + resetPQExpBuffer(q); +- appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename)); ++ appendPQExpBuffer(q, "CREATE USER MAPPING IF NOT EXISTS FOR %s", fmtId(usename)); + appendPQExpBuffer(q, " SERVER %s", fmtId(servername)); + + if (umoptions && strlen(umoptions) > 0) +@@ -17997,6 +18000,35 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) + } + } + ++ /* Some of the binary compatibility is needed for pgactive as well. */ ++ if (dopt->pgactive_init_node && tbinfo->relkind == RELKIND_RELATION) ++ { ++ for (j = 0; j < tbinfo->numatts; j++) ++ { ++ if (!tbinfo->attisdropped[j]) ++ continue; ++ ++ appendPQExpBufferStr(q, "\n-- For pgactive init, recreate dropped column.\n"); ++ appendPQExpBuffer(q, "UPDATE pg_catalog.pg_attribute\n" ++ "SET attlen = %d, " ++ "attalign = '%c', attbyval = false\n" ++ "WHERE attname = ", ++ tbinfo->attlen[j], ++ tbinfo->attalign[j]); ++ appendStringLiteralAH(q, tbinfo->attnames[j], fout); ++ appendPQExpBufferStr(q, "\n AND attrelid = "); ++ appendStringLiteralAH(q, qualrelname, fout); ++ appendPQExpBufferStr(q, "::pg_catalog.regclass;\n"); ++ ++ if (tbinfo->relkind == RELKIND_RELATION || ++ tbinfo->relkind == RELKIND_PARTITIONED_TABLE) ++ appendPQExpBuffer(q, "ALTER TABLE ONLY %s ", qualrelname); ++ else ++ appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ", qualrelname); ++ ++ appendPQExpBuffer(q, "DROP COLUMN %s;\n", fmtId(tbinfo->attnames[j])); ++ } ++ } + /* + * In binary_upgrade mode, arrange to restore the old relfrozenxid and + * relminmxid of all vacuumable relations. (While vacuum.c processes diff --git a/src/compat/19/pg_dump/common.c b/src/compat/19/pg_dump/common.c new file mode 100644 index 00000000..d1431c5c --- /dev/null +++ b/src/compat/19/pg_dump/common.c @@ -0,0 +1,1169 @@ +/*------------------------------------------------------------------------- + * + * common.c + * Catalog routines used by pg_dump; long ago these were shared + * by another dump tool, but not anymore. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/bin/pg_dump/common.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include + +#include "catalog/pg_am_d.h" +#include "catalog/pg_class_d.h" +#include "catalog/pg_collation_d.h" +#include "catalog/pg_extension_d.h" +#include "catalog/pg_namespace_d.h" +#include "catalog/pg_operator_d.h" +#include "catalog/pg_proc_d.h" +#include "catalog/pg_publication_d.h" +#include "catalog/pg_subscription_d.h" +#include "catalog/pg_type_d.h" +#include "common/hashfn.h" +#include "pg_backup_utils.h" +#include "pg_dump.h" + +/* + * Variables for mapping DumpId to DumpableObject + */ +static DumpableObject **dumpIdMap = NULL; +static int allocedDumpIds = 0; +static DumpId lastDumpId = 0; /* Note: 0 is InvalidDumpId */ + +/* + * Infrastructure for mapping CatalogId to DumpableObject + * + * We use a hash table generated by simplehash.h. That infrastructure + * requires all the hash table entries to be the same size, and it also + * expects that it can move them around when resizing the table. So we + * cannot make the DumpableObjects be elements of the hash table directly; + * instead, the hash table elements contain pointers to DumpableObjects. + * This does have the advantage of letting us map multiple CatalogIds + * to one DumpableObject, which is useful for blobs. + * + * It turns out to be convenient to also use this data structure to map + * CatalogIds to owning extensions, if any. Since extension membership + * data is read before creating most DumpableObjects, either one of dobj + * and ext could be NULL. + */ +typedef struct _catalogIdMapEntry +{ + CatalogId catId; /* the indexed CatalogId */ + uint32 status; /* hash status */ + uint32 hashval; /* hash code for the CatalogId */ + DumpableObject *dobj; /* the associated DumpableObject, if any */ + ExtensionInfo *ext; /* owning extension, if any */ +} CatalogIdMapEntry; + +#define SH_PREFIX catalogid +#define SH_ELEMENT_TYPE CatalogIdMapEntry +#define SH_KEY_TYPE CatalogId +#define SH_KEY catId +#define SH_HASH_KEY(tb, key) hash_bytes((const unsigned char *) &(key), sizeof(CatalogId)) +#define SH_EQUAL(tb, a, b) ((a).oid == (b).oid && (a).tableoid == (b).tableoid) +#define SH_STORE_HASH +#define SH_GET_HASH(tb, a) (a)->hashval +#define SH_SCOPE static inline +#define SH_RAW_ALLOCATOR pg_malloc0 +#define SH_DECLARE +#define SH_DEFINE +#include "lib/simplehash.h" + +#define CATALOGIDHASH_INITIAL_SIZE 10000 + +static catalogid_hash *catalogIdHash = NULL; + +static void flagInhTables(Archive *fout, TableInfo *tblinfo, int numTables, + InhInfo *inhinfo, int numInherits); +static void flagInhIndexes(Archive *fout, TableInfo *tblinfo, int numTables); +static void flagInhAttrs(Archive *fout, DumpOptions *dopt, TableInfo *tblinfo, + int numTables); +static int strInArray(const char *pattern, char **arr, int arr_size); +static IndxInfo *findIndexByOid(Oid oid); + + +/* + * getSchemaData + * Collect information about all potentially dumpable objects + */ +TableInfo * +getSchemaData(Archive *fout, int *numTablesPtr) +{ + TableInfo *tblinfo; + ExtensionInfo *extinfo; + InhInfo *inhinfo; + int numTables; + int numExtensions; + int numInherits; + + /* + * We must read extensions and extension membership info first, because + * extension membership needs to be consultable during decisions about + * whether other objects are to be dumped. + */ + pg_log_info("reading extensions"); + extinfo = getExtensions(fout, &numExtensions); + + pg_log_info("identifying extension members"); + getExtensionMembership(fout, extinfo, numExtensions); + + pg_log_info("reading schemas"); + getNamespaces(fout); + + /* + * getTables should be done as soon as possible, so as to minimize the + * window between starting our transaction and acquiring per-table locks. + * However, we have to do getNamespaces first because the tables get + * linked to their containing namespaces during getTables. + */ + pg_log_info("reading user-defined tables"); + tblinfo = getTables(fout, &numTables); + + getOwnedSeqs(fout, tblinfo, numTables); + + pg_log_info("reading user-defined functions"); + getFuncs(fout); + + /* this must be after getTables and getFuncs */ + pg_log_info("reading user-defined types"); + getTypes(fout); + + /* this must be after getFuncs, too */ + pg_log_info("reading procedural languages"); + getProcLangs(fout); + + pg_log_info("reading user-defined aggregate functions"); + getAggregates(fout); + + pg_log_info("reading user-defined operators"); + getOperators(fout); + + pg_log_info("reading user-defined access methods"); + getAccessMethods(fout); + + pg_log_info("reading user-defined operator classes"); + getOpclasses(fout); + + pg_log_info("reading user-defined operator families"); + getOpfamilies(fout); + + pg_log_info("reading user-defined text search parsers"); + getTSParsers(fout); + + pg_log_info("reading user-defined text search templates"); + getTSTemplates(fout); + + pg_log_info("reading user-defined text search dictionaries"); + getTSDictionaries(fout); + + pg_log_info("reading user-defined text search configurations"); + getTSConfigurations(fout); + + pg_log_info("reading user-defined foreign-data wrappers"); + getForeignDataWrappers(fout); + + pg_log_info("reading user-defined foreign servers"); + getForeignServers(fout); + + pg_log_info("reading default privileges"); + getDefaultACLs(fout); + + pg_log_info("reading user-defined collations"); + getCollations(fout); + + pg_log_info("reading user-defined conversions"); + getConversions(fout); + + pg_log_info("reading type casts"); + getCasts(fout); + + pg_log_info("reading transforms"); + getTransforms(fout); + + pg_log_info("reading table inheritance information"); + inhinfo = getInherits(fout, &numInherits); + + pg_log_info("reading event triggers"); + getEventTriggers(fout); + + /* Identify extension configuration tables that should be dumped */ + pg_log_info("finding extension tables"); + processExtensionTables(fout, extinfo, numExtensions); + + /* Link tables to parents, mark parents of target tables interesting */ + pg_log_info("finding inheritance relationships"); + flagInhTables(fout, tblinfo, numTables, inhinfo, numInherits); + + pg_log_info("reading column info for interesting tables"); + getTableAttrs(fout, tblinfo, numTables); + + pg_log_info("flagging inherited columns in subtables"); + flagInhAttrs(fout, fout->dopt, tblinfo, numTables); + + pg_log_info("reading partitioning data"); + getPartitioningInfo(fout); + + pg_log_info("reading indexes"); + getIndexes(fout, tblinfo, numTables); + + pg_log_info("flagging indexes in partitioned tables"); + flagInhIndexes(fout, tblinfo, numTables); + + pg_log_info("reading extended statistics"); + getExtendedStatistics(fout); + + pg_log_info("reading constraints"); + getConstraints(fout, tblinfo, numTables); + + pg_log_info("reading triggers"); + getTriggers(fout, tblinfo, numTables); + + pg_log_info("reading rewrite rules"); + getRules(fout); + + pg_log_info("reading policies"); + getPolicies(fout, tblinfo, numTables); + + pg_log_info("reading publications"); + getPublications(fout); + + pg_log_info("reading publication membership of tables"); + getPublicationTables(fout, tblinfo, numTables); + + pg_log_info("reading publication membership of schemas"); + getPublicationNamespaces(fout); + + pg_log_info("reading subscriptions"); + getSubscriptions(fout); + + pg_log_info("reading subscription membership of relations"); + getSubscriptionRelations(fout); + + free(inhinfo); /* not needed any longer */ + + *numTablesPtr = numTables; + return tblinfo; +} + +/* flagInhTables - + * Fill in parent link fields of tables for which we need that information, + * mark parents of target tables as interesting, and create + * TableAttachInfo objects for partitioned tables with appropriate + * dependency links. + * + * Note that only direct ancestors of targets are marked interesting. + * This is sufficient; we don't much care whether they inherited their + * attributes or not. + * + * modifies tblinfo + */ +static void +flagInhTables(Archive *fout, TableInfo *tblinfo, int numTables, + InhInfo *inhinfo, int numInherits) +{ + TableInfo *child = NULL; + TableInfo *parent = NULL; + int i, + j; + + /* + * Set up links from child tables to their parents. + * + * We used to attempt to skip this work for tables that are not to be + * dumped; but the optimizable cases are rare in practice, and setting up + * these links in bulk is cheaper than the old way. (Note in particular + * that it's very rare for a child to have more than one parent.) + */ + for (i = 0; i < numInherits; i++) + { + /* + * Skip a hashtable lookup if it's same table as last time. This is + * unlikely for the child, but less so for the parent. (Maybe we + * should ask the backend for a sorted array to make it more likely? + * Not clear the sorting effort would be repaid, though.) + */ + if (child == NULL || + child->dobj.catId.oid != inhinfo[i].inhrelid) + { + child = findTableByOid(inhinfo[i].inhrelid); + + /* + * If we find no TableInfo, assume the pg_inherits entry is for a + * partitioned index, which we don't need to track. + */ + if (child == NULL) + continue; + } + if (parent == NULL || + parent->dobj.catId.oid != inhinfo[i].inhparent) + { + parent = findTableByOid(inhinfo[i].inhparent); + if (parent == NULL) + pg_fatal("failed sanity check, parent OID %u of table \"%s\" (OID %u) not found", + inhinfo[i].inhparent, + child->dobj.name, + child->dobj.catId.oid); + } + /* Add this parent to the child's list of parents. */ + if (child->numParents > 0) + child->parents = pg_realloc_array(child->parents, + TableInfo *, + child->numParents + 1); + else + child->parents = pg_malloc_array(TableInfo *, 1); + child->parents[child->numParents++] = parent; + } + + /* + * Now consider all child tables and mark parents interesting as needed. + */ + for (i = 0; i < numTables; i++) + { + /* + * If needed, mark the parents as interesting for getTableAttrs and + * getIndexes. We only need this for direct parents of dumpable + * tables. + */ + if (tblinfo[i].dobj.dump) + { + int numParents = tblinfo[i].numParents; + TableInfo **parents = tblinfo[i].parents; + + for (j = 0; j < numParents; j++) + parents[j]->interesting = true; + } + + /* Create TableAttachInfo object if needed */ + if ((tblinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) && + tblinfo[i].ispartition) + { + TableAttachInfo *attachinfo; + + /* With partitions there can only be one parent */ + if (tblinfo[i].numParents != 1) + pg_fatal("invalid number of parents %d for table \"%s\"", + tblinfo[i].numParents, + tblinfo[i].dobj.name); + + attachinfo = palloc_object(TableAttachInfo); + attachinfo->dobj.objType = DO_TABLE_ATTACH; + attachinfo->dobj.catId.tableoid = 0; + attachinfo->dobj.catId.oid = 0; + AssignDumpId(&attachinfo->dobj); + attachinfo->dobj.name = pg_strdup(tblinfo[i].dobj.name); + attachinfo->dobj.namespace = tblinfo[i].dobj.namespace; + attachinfo->parentTbl = tblinfo[i].parents[0]; + attachinfo->partitionTbl = &tblinfo[i]; + + /* + * We must state the DO_TABLE_ATTACH object's dependencies + * explicitly, since it will not match anything in pg_depend. + * + * Give it dependencies on both the partition table and the parent + * table, so that it will not be executed till both of those + * exist. (There's no need to care what order those are created + * in.) + */ + addObjectDependency(&attachinfo->dobj, tblinfo[i].dobj.dumpId); + addObjectDependency(&attachinfo->dobj, tblinfo[i].parents[0]->dobj.dumpId); + } + } +} + +/* + * flagInhIndexes - + * Create IndexAttachInfo objects for partitioned indexes, and add + * appropriate dependency links. + */ +static void +flagInhIndexes(Archive *fout, TableInfo tblinfo[], int numTables) +{ + int i, + j; + + for (i = 0; i < numTables; i++) + { + if (!tblinfo[i].ispartition || tblinfo[i].numParents == 0) + continue; + + Assert(tblinfo[i].numParents == 1); + + for (j = 0; j < tblinfo[i].numIndexes; j++) + { + IndxInfo *index = &(tblinfo[i].indexes[j]); + IndxInfo *parentidx; + IndexAttachInfo *attachinfo; + + if (index->parentidx == 0) + continue; + + parentidx = findIndexByOid(index->parentidx); + if (parentidx == NULL) + continue; + + attachinfo = pg_malloc_object(IndexAttachInfo); + + attachinfo->dobj.objType = DO_INDEX_ATTACH; + attachinfo->dobj.catId.tableoid = 0; + attachinfo->dobj.catId.oid = 0; + AssignDumpId(&attachinfo->dobj); + attachinfo->dobj.name = pg_strdup(index->dobj.name); + attachinfo->dobj.namespace = index->indextable->dobj.namespace; + attachinfo->parentIdx = parentidx; + attachinfo->partitionIdx = index; + + /* + * We must state the DO_INDEX_ATTACH object's dependencies + * explicitly, since it will not match anything in pg_depend. + * + * Give it dependencies on both the partition index and the parent + * index, so that it will not be executed till both of those + * exist. (There's no need to care what order those are created + * in.) + * + * In addition, give it dependencies on the indexes' underlying + * tables. This does nothing of great value so far as serial + * restore ordering goes, but it ensures that a parallel restore + * will not try to run the ATTACH concurrently with other + * operations on those tables. + */ + addObjectDependency(&attachinfo->dobj, index->dobj.dumpId); + addObjectDependency(&attachinfo->dobj, parentidx->dobj.dumpId); + addObjectDependency(&attachinfo->dobj, + index->indextable->dobj.dumpId); + addObjectDependency(&attachinfo->dobj, + parentidx->indextable->dobj.dumpId); + + /* keep track of the list of partitions in the parent index */ + simple_ptr_list_append(&parentidx->partattaches, &attachinfo->dobj); + } + } +} + +/* flagInhAttrs - + * for each dumpable table in tblinfo, flag its inherited attributes + * + * What we need to do here is: + * + * - Detect child columns that inherit NOT NULL bits from their parents, so + * that we needn't specify that again for the child. For versions 18 and + * up, this is needed when the parent is NOT VALID and the child isn't. + * + * - Detect child columns that have DEFAULT NULL when their parents had some + * non-null default. In this case, we make up a dummy AttrDefInfo object so + * that we'll correctly emit the necessary DEFAULT NULL clause; otherwise + * the backend will apply an inherited default to the column. + * + * - Detect child columns that have a generation expression and all their + * parents also have the same generation expression, and if so suppress the + * child's expression. The child will inherit the generation expression + * automatically, so there's no need to dump it. This improves the dump's + * compatibility with pre-v16 servers, which didn't allow the child's + * expression to be given explicitly. Exceptions: If it's a partition or + * we are in binary upgrade mode, we dump such expressions anyway because + * in those cases inherited tables are recreated standalone first and then + * reattached to the parent. (See also the logic in dumpTableSchema().) + * + * modifies tblinfo + */ +static void +flagInhAttrs(Archive *fout, DumpOptions *dopt, TableInfo *tblinfo, int numTables) +{ + int i, + j, + k; + + /* + * We scan the tables in OID order, since that's how tblinfo[] is sorted. + * Hence we will typically visit parents before their children --- but + * that is *not* guaranteed. Thus this loop must be careful that it does + * not alter table properties in a way that could change decisions made at + * child tables during other iterations. + */ + for (i = 0; i < numTables; i++) + { + TableInfo *tbinfo = &(tblinfo[i]); + int numParents; + TableInfo **parents; + + /* Some kinds never have parents */ + if (tbinfo->relkind == RELKIND_SEQUENCE || + tbinfo->relkind == RELKIND_VIEW || + tbinfo->relkind == RELKIND_MATVIEW || + tbinfo->relkind == RELKIND_PROPGRAPH) + continue; + + /* Don't bother computing anything for non-target tables, either */ + if (!tbinfo->dobj.dump) + continue; + + numParents = tbinfo->numParents; + parents = tbinfo->parents; + + if (numParents == 0) + continue; /* nothing to see here, move along */ + + /* For each column, search for matching column names in parent(s) */ + for (j = 0; j < tbinfo->numatts; j++) + { + bool foundNotNull; /* Attr was NOT NULL in a parent */ + bool foundDefault; /* Found a default in a parent */ + bool foundSameGenerated; /* Found matching GENERATED */ + bool foundDiffGenerated; /* Found non-matching GENERATED */ + bool allNotNullsInvalid = true; /* is NOT NULL NOT VALID + * on all parents? */ + + /* no point in examining dropped columns */ + if (tbinfo->attisdropped[j]) + continue; + + foundNotNull = false; + foundDefault = false; + foundSameGenerated = false; + foundDiffGenerated = false; + for (k = 0; k < numParents; k++) + { + TableInfo *parent = parents[k]; + int inhAttrInd; + + inhAttrInd = strInArray(tbinfo->attnames[j], + parent->attnames, + parent->numatts); + if (inhAttrInd >= 0) + { + AttrDefInfo *parentDef = parent->attrdefs[inhAttrInd]; + + /* + * Account for each parent having a not-null constraint. + * In versions 18 and later, we don't need this (and those + * didn't have NO INHERIT.) + */ + if (fout->remoteVersion < 180000 && + parent->notnull_constrs[inhAttrInd] != NULL) + foundNotNull = true; + + /* + * Keep track of whether all the parents that have a + * not-null constraint on this column have it as NOT + * VALID; if they all are, arrange to have it printed for + * this column. If at least one parent has it as valid, + * there's no need. + */ + if (fout->remoteVersion >= 180000 && + parent->notnull_constrs[inhAttrInd] && + !parent->notnull_invalid[inhAttrInd]) + allNotNullsInvalid = false; + + foundDefault |= (parentDef != NULL && + strcmp(parentDef->adef_expr, "NULL") != 0 && + !parent->attgenerated[inhAttrInd]); + if (parent->attgenerated[inhAttrInd]) + { + /* these pointer nullness checks are just paranoia */ + if (parentDef != NULL && + tbinfo->attrdefs[j] != NULL && + strcmp(parentDef->adef_expr, + tbinfo->attrdefs[j]->adef_expr) == 0) + foundSameGenerated = true; + else + foundDiffGenerated = true; + } + } + } + + /* + * In versions < 18, for lack of a better system, we arbitrarily + * decide that a not-null constraint is not locally defined if at + * least one of the parents has it. + */ + if (fout->remoteVersion < 180000 && foundNotNull) + tbinfo->notnull_islocal[j] = false; + + /* + * For versions >18, we must print the not-null constraint locally + * for this table even if it isn't really locally defined, but is + * valid for the child and no parent has it as valid. + */ + if (fout->remoteVersion >= 180000 && allNotNullsInvalid) + tbinfo->notnull_islocal[j] = true; + + /* + * Manufacture a DEFAULT NULL clause if necessary. This breaks + * the advice given above to avoid changing state that might get + * inspected in other loop iterations. We prevent trouble by + * having the foundDefault test above check whether adef_expr is + * "NULL", so that it will reach the same conclusion before or + * after this is done. + */ + if (foundDefault && tbinfo->attrdefs[j] == NULL) + { + AttrDefInfo *attrDef; + + attrDef = pg_malloc_object(AttrDefInfo); + attrDef->dobj.objType = DO_ATTRDEF; + attrDef->dobj.catId.tableoid = 0; + attrDef->dobj.catId.oid = 0; + AssignDumpId(&attrDef->dobj); + attrDef->dobj.name = pg_strdup(tbinfo->dobj.name); + attrDef->dobj.namespace = tbinfo->dobj.namespace; + attrDef->dobj.dump = tbinfo->dobj.dump; + + attrDef->adtable = tbinfo; + attrDef->adnum = j + 1; + attrDef->adef_expr = pg_strdup("NULL"); + + /* Will column be dumped explicitly? */ + if (shouldPrintColumn(dopt, tbinfo, j)) + { + attrDef->separate = false; + /* No dependency needed: NULL cannot have dependencies */ + } + else + { + /* column will be suppressed, print default separately */ + attrDef->separate = true; + /* ensure it comes out after the table */ + addObjectDependency(&attrDef->dobj, + tbinfo->dobj.dumpId); + } + + tbinfo->attrdefs[j] = attrDef; + } + + /* No need to dump generation expression if it's inheritable */ + if (foundSameGenerated && !foundDiffGenerated && + !tbinfo->ispartition && !dopt->binary_upgrade) + tbinfo->attrdefs[j]->dobj.dump = DUMP_COMPONENT_NONE; + } + } +} + +/* + * AssignDumpId + * Given a newly-created dumpable object, assign a dump ID, + * and enter the object into the lookup tables. + * + * The caller is expected to have filled in objType and catId, + * but not any of the other standard fields of a DumpableObject. + */ +void +AssignDumpId(DumpableObject *dobj) +{ + dobj->dumpId = ++lastDumpId; + dobj->name = NULL; /* must be set later */ + dobj->namespace = NULL; /* may be set later */ + dobj->dump = DUMP_COMPONENT_ALL; /* default assumption */ + dobj->dump_contains = DUMP_COMPONENT_ALL; /* default assumption */ + /* All objects have definitions; we may set more components bits later */ + dobj->components = DUMP_COMPONENT_DEFINITION; + dobj->ext_member = false; /* default assumption */ + dobj->depends_on_ext = false; /* default assumption */ + dobj->dependencies = NULL; + dobj->nDeps = 0; + dobj->allocDeps = 0; + + /* Add object to dumpIdMap[], enlarging that array if need be */ + while (dobj->dumpId >= allocedDumpIds) + { + int newAlloc; + + if (allocedDumpIds <= 0) + { + newAlloc = 256; + dumpIdMap = pg_malloc_array(DumpableObject *, newAlloc); + } + else + { + newAlloc = allocedDumpIds * 2; + dumpIdMap = pg_realloc_array(dumpIdMap, DumpableObject *, newAlloc); + } + memset(dumpIdMap + allocedDumpIds, 0, + (newAlloc - allocedDumpIds) * sizeof(DumpableObject *)); + allocedDumpIds = newAlloc; + } + dumpIdMap[dobj->dumpId] = dobj; + + /* If it has a valid CatalogId, enter it into the hash table */ + if (OidIsValid(dobj->catId.tableoid)) + { + CatalogIdMapEntry *entry; + bool found; + + /* Initialize CatalogId hash table if not done yet */ + if (catalogIdHash == NULL) + catalogIdHash = catalogid_create(CATALOGIDHASH_INITIAL_SIZE, NULL); + + entry = catalogid_insert(catalogIdHash, dobj->catId, &found); + if (!found) + { + entry->dobj = NULL; + entry->ext = NULL; + } + Assert(entry->dobj == NULL); + entry->dobj = dobj; + } +} + +/* + * recordAdditionalCatalogID + * Record an additional catalog ID for the given DumpableObject + */ +void +recordAdditionalCatalogID(CatalogId catId, DumpableObject *dobj) +{ + CatalogIdMapEntry *entry; + bool found; + + /* CatalogId hash table must exist, if we have a DumpableObject */ + Assert(catalogIdHash != NULL); + + /* Add reference to CatalogId hash */ + entry = catalogid_insert(catalogIdHash, catId, &found); + if (!found) + { + entry->dobj = NULL; + entry->ext = NULL; + } + Assert(entry->dobj == NULL); + entry->dobj = dobj; +} + +/* + * Assign a DumpId that's not tied to a DumpableObject. + * + * This is used when creating a "fixed" ArchiveEntry that doesn't need to + * participate in the sorting logic. + */ +DumpId +createDumpId(void) +{ + return ++lastDumpId; +} + +/* + * Return the largest DumpId so far assigned + */ +DumpId +getMaxDumpId(void) +{ + return lastDumpId; +} + +/* + * Find a DumpableObject by dump ID + * + * Returns NULL for invalid ID + */ +DumpableObject * +findObjectByDumpId(DumpId dumpId) +{ + if (dumpId <= 0 || dumpId >= allocedDumpIds) + return NULL; /* out of range? */ + return dumpIdMap[dumpId]; +} + +/* + * Find a DumpableObject by catalog ID + * + * Returns NULL for unknown ID + */ +DumpableObject * +findObjectByCatalogId(CatalogId catalogId) +{ + CatalogIdMapEntry *entry; + + if (catalogIdHash == NULL) + return NULL; /* no objects exist yet */ + + entry = catalogid_lookup(catalogIdHash, catalogId); + if (entry == NULL) + return NULL; + return entry->dobj; +} + +/* + * Build an array of pointers to all known dumpable objects + * + * This simply creates a modifiable copy of the internal map. + */ +void +getDumpableObjects(DumpableObject ***objs, int *numObjs) +{ + int i, + j; + + *objs = pg_malloc_array(DumpableObject *, allocedDumpIds); + j = 0; + for (i = 1; i < allocedDumpIds; i++) + { + if (dumpIdMap[i]) + (*objs)[j++] = dumpIdMap[i]; + } + *numObjs = j; +} + +/* + * Add a dependency link to a DumpableObject + * + * Note: duplicate dependencies are currently not eliminated + */ +void +addObjectDependency(DumpableObject *dobj, DumpId refId) +{ + if (dobj->nDeps >= dobj->allocDeps) + { + if (dobj->allocDeps <= 0) + { + dobj->allocDeps = 16; + dobj->dependencies = pg_malloc_array(DumpId, dobj->allocDeps); + } + else + { + dobj->allocDeps *= 2; + dobj->dependencies = pg_realloc_array(dobj->dependencies, + DumpId, dobj->allocDeps); + } + } + dobj->dependencies[dobj->nDeps++] = refId; +} + +/* + * Remove a dependency link from a DumpableObject + * + * If there are multiple links, all are removed + */ +void +removeObjectDependency(DumpableObject *dobj, DumpId refId) +{ + int i; + int j = 0; + + for (i = 0; i < dobj->nDeps; i++) + { + if (dobj->dependencies[i] != refId) + dobj->dependencies[j++] = dobj->dependencies[i]; + } + dobj->nDeps = j; +} + + +/* + * findTableByOid + * finds the DumpableObject for the table with the given oid + * returns NULL if not found + */ +TableInfo * +findTableByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = RelationRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_TABLE); + return (TableInfo *) dobj; +} + +/* + * findIndexByOid + * finds the DumpableObject for the index with the given oid + * returns NULL if not found + */ +static IndxInfo * +findIndexByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = RelationRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_INDEX); + return (IndxInfo *) dobj; +} + +/* + * findTypeByOid + * finds the DumpableObject for the type with the given oid + * returns NULL if not found + */ +TypeInfo * +findTypeByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = TypeRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || + dobj->objType == DO_TYPE || dobj->objType == DO_DUMMY_TYPE); + return (TypeInfo *) dobj; +} + +/* + * findFuncByOid + * finds the DumpableObject for the function with the given oid + * returns NULL if not found + */ +FuncInfo * +findFuncByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = ProcedureRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_FUNC); + return (FuncInfo *) dobj; +} + +/* + * findOprByOid + * finds the DumpableObject for the operator with the given oid + * returns NULL if not found + */ +OprInfo * +findOprByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = OperatorRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_OPERATOR); + return (OprInfo *) dobj; +} + +/* + * findAccessMethodByOid + * finds the DumpableObject for the access method with the given oid + * returns NULL if not found + */ +AccessMethodInfo * +findAccessMethodByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = AccessMethodRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_ACCESS_METHOD); + return (AccessMethodInfo *) dobj; +} + +/* + * findCollationByOid + * finds the DumpableObject for the collation with the given oid + * returns NULL if not found + */ +CollInfo * +findCollationByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = CollationRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_COLLATION); + return (CollInfo *) dobj; +} + +/* + * findNamespaceByOid + * finds the DumpableObject for the namespace with the given oid + * returns NULL if not found + */ +NamespaceInfo * +findNamespaceByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = NamespaceRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_NAMESPACE); + return (NamespaceInfo *) dobj; +} + +/* + * findExtensionByOid + * finds the DumpableObject for the extension with the given oid + * returns NULL if not found + */ +ExtensionInfo * +findExtensionByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = ExtensionRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_EXTENSION); + return (ExtensionInfo *) dobj; +} + +/* + * findPublicationByOid + * finds the DumpableObject for the publication with the given oid + * returns NULL if not found + */ +PublicationInfo * +findPublicationByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = PublicationRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_PUBLICATION); + return (PublicationInfo *) dobj; +} + +/* + * findSubscriptionByOid + * finds the DumpableObject for the subscription with the given oid + * returns NULL if not found + */ +SubscriptionInfo * +findSubscriptionByOid(Oid oid) +{ + CatalogId catId; + DumpableObject *dobj; + + catId.tableoid = SubscriptionRelationId; + catId.oid = oid; + dobj = findObjectByCatalogId(catId); + Assert(dobj == NULL || dobj->objType == DO_SUBSCRIPTION); + return (SubscriptionInfo *) dobj; +} + + +/* + * recordExtensionMembership + * Record that the object identified by the given catalog ID + * belongs to the given extension + */ +void +recordExtensionMembership(CatalogId catId, ExtensionInfo *ext) +{ + CatalogIdMapEntry *entry; + bool found; + + /* CatalogId hash table must exist, if we have an ExtensionInfo */ + Assert(catalogIdHash != NULL); + + /* Add reference to CatalogId hash */ + entry = catalogid_insert(catalogIdHash, catId, &found); + if (!found) + { + entry->dobj = NULL; + entry->ext = NULL; + } + Assert(entry->ext == NULL); + entry->ext = ext; +} + +/* + * findOwningExtension + * return owning extension for specified catalog ID, or NULL if none + */ +ExtensionInfo * +findOwningExtension(CatalogId catalogId) +{ + CatalogIdMapEntry *entry; + + if (catalogIdHash == NULL) + return NULL; /* no objects exist yet */ + + entry = catalogid_lookup(catalogIdHash, catalogId); + if (entry == NULL) + return NULL; + return entry->ext; +} + + +/* + * parseOidArray + * parse a string of numbers delimited by spaces into a character array + * + * Note: actually this is used for both Oids and potentially-signed + * attribute numbers. This should cause no trouble, but we could split + * the function into two functions with different argument types if it does. + */ + +void +parseOidArray(const char *str, Oid *array, int arraysize) +{ + int j, + argNum; + char temp[100]; + char s; + + argNum = 0; + j = 0; + for (;;) + { + s = *str++; + if (s == ' ' || s == '\0') + { + if (j > 0) + { + if (argNum >= arraysize) + pg_fatal("could not parse numeric array \"%s\": too many numbers", str); + temp[j] = '\0'; + array[argNum++] = atooid(temp); + j = 0; + } + if (s == '\0') + break; + } + else + { + if (!(isdigit((unsigned char) s) || s == '-') || + j >= sizeof(temp) - 1) + pg_fatal("could not parse numeric array \"%s\": invalid character in number", str); + temp[j++] = s; + } + } + + while (argNum < arraysize) + array[argNum++] = InvalidOid; +} + + +/* + * strInArray: + * takes in a string and a string array and the number of elements in the + * string array. + * returns the index if the string is somewhere in the array, -1 otherwise + */ + +static int +strInArray(const char *pattern, char **arr, int arr_size) +{ + int i; + + for (i = 0; i < arr_size; i++) + { + if (strcmp(pattern, arr[i]) == 0) + return i; + } + return -1; +} diff --git a/src/compat/19/pg_dump/compress_gzip.c b/src/compat/19/pg_dump/compress_gzip.c new file mode 100644 index 00000000..60c553ba --- /dev/null +++ b/src/compat/19/pg_dump/compress_gzip.c @@ -0,0 +1,462 @@ +/*------------------------------------------------------------------------- + * + * compress_gzip.c + * Routines for archivers to read or write a gzip compressed data stream. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/compress_gzip.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" +#include + +#include "compress_gzip.h" +#include "pg_backup_utils.h" + +#ifdef HAVE_LIBZ +#include + +/* + * We don't use the gzgetc() macro, because zlib's configuration logic is not + * robust enough to guarantee that the macro will have the same ideas about + * struct field layout as the library itself does; see for example + * https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=59711 + * Instead, #undef the macro and fall back to the underlying function. + */ +#undef gzgetc + +/*---------------------- + * Compressor API + *---------------------- + */ +typedef struct GzipCompressorState +{ + z_streamp zp; + + void *outbuf; + size_t outsize; +} GzipCompressorState; + +/* Private routines that support gzip compressed data I/O */ +static void DeflateCompressorInit(CompressorState *cs); +static void DeflateCompressorEnd(ArchiveHandle *AH, CompressorState *cs); +static void DeflateCompressorCommon(ArchiveHandle *AH, CompressorState *cs, + bool flush); +static void EndCompressorGzip(ArchiveHandle *AH, CompressorState *cs); +static void WriteDataToArchiveGzip(ArchiveHandle *AH, CompressorState *cs, + const void *data, size_t dLen); +static void ReadDataFromArchiveGzip(ArchiveHandle *AH, CompressorState *cs); + +static void +DeflateCompressorInit(CompressorState *cs) +{ + GzipCompressorState *gzipcs; + z_streamp zp; + + gzipcs = pg_malloc0_object(GzipCompressorState); + zp = gzipcs->zp = pg_malloc_object(z_stream); + zp->zalloc = Z_NULL; + zp->zfree = Z_NULL; + zp->opaque = Z_NULL; + + /* + * outsize is the buffer size we tell zlib it can output to. We actually + * allocate one extra byte because some routines want to append a trailing + * zero byte to the zlib output. + */ + gzipcs->outsize = DEFAULT_IO_BUFFER_SIZE; + gzipcs->outbuf = pg_malloc(gzipcs->outsize + 1); + + /* -Z 0 uses the "None" compressor -- not zlib with no compression */ + Assert(cs->compression_spec.level != 0); + + if (deflateInit(zp, cs->compression_spec.level) != Z_OK) + pg_fatal("could not initialize compression library: %s", zp->msg); + + /* Just be paranoid - maybe End is called after Start, with no Write */ + zp->next_out = gzipcs->outbuf; + zp->avail_out = gzipcs->outsize; + + /* Keep track of gzipcs */ + cs->private_data = gzipcs; +} + +static void +DeflateCompressorEnd(ArchiveHandle *AH, CompressorState *cs) +{ + GzipCompressorState *gzipcs = (GzipCompressorState *) cs->private_data; + z_streamp zp; + + zp = gzipcs->zp; + zp->next_in = NULL; + zp->avail_in = 0; + + /* Flush any remaining data from zlib buffer */ + DeflateCompressorCommon(AH, cs, true); + + if (deflateEnd(zp) != Z_OK) + pg_fatal("could not close compression stream: %s", zp->msg); + + pg_free(gzipcs->outbuf); + pg_free(gzipcs->zp); + pg_free(gzipcs); + cs->private_data = NULL; +} + +static void +DeflateCompressorCommon(ArchiveHandle *AH, CompressorState *cs, bool flush) +{ + GzipCompressorState *gzipcs = (GzipCompressorState *) cs->private_data; + z_streamp zp = gzipcs->zp; + void *out = gzipcs->outbuf; + int res = Z_OK; + + while (gzipcs->zp->avail_in != 0 || flush) + { + res = deflate(zp, flush ? Z_FINISH : Z_NO_FLUSH); + if (res == Z_STREAM_ERROR) + pg_fatal("could not compress data: %s", zp->msg); + if ((flush && (zp->avail_out < gzipcs->outsize)) + || (zp->avail_out == 0) + || (zp->avail_in != 0) + ) + { + /* + * Extra paranoia: avoid zero-length chunks, since a zero length + * chunk is the EOF marker in the custom format. This should never + * happen but ... + */ + if (zp->avail_out < gzipcs->outsize) + { + /* + * Any write function should do its own error checking but to + * make sure we do a check here as well ... + */ + size_t len = gzipcs->outsize - zp->avail_out; + + cs->writeF(AH, out, len); + } + zp->next_out = out; + zp->avail_out = gzipcs->outsize; + } + + if (res == Z_STREAM_END) + break; + } +} + +static void +EndCompressorGzip(ArchiveHandle *AH, CompressorState *cs) +{ + /* If deflation was initialized, finalize it */ + if (cs->private_data) + DeflateCompressorEnd(AH, cs); +} + +static void +WriteDataToArchiveGzip(ArchiveHandle *AH, CompressorState *cs, + const void *data, size_t dLen) +{ + GzipCompressorState *gzipcs = (GzipCompressorState *) cs->private_data; + + gzipcs->zp->next_in = data; + gzipcs->zp->avail_in = dLen; + DeflateCompressorCommon(AH, cs, false); +} + +static void +ReadDataFromArchiveGzip(ArchiveHandle *AH, CompressorState *cs) +{ + z_streamp zp; + char *out; + int res = Z_OK; + size_t cnt; + char *buf; + size_t buflen; + + zp = pg_malloc_object(z_stream); + zp->zalloc = Z_NULL; + zp->zfree = Z_NULL; + zp->opaque = Z_NULL; + + buflen = DEFAULT_IO_BUFFER_SIZE; + buf = pg_malloc(buflen); + + out = pg_malloc(DEFAULT_IO_BUFFER_SIZE + 1); + + if (inflateInit(zp) != Z_OK) + pg_fatal("could not initialize compression library: %s", + zp->msg); + + /* no minimal chunk size for zlib */ + while ((cnt = cs->readF(AH, &buf, &buflen))) + { + zp->next_in = (void *) buf; + zp->avail_in = cnt; + + while (zp->avail_in > 0) + { + zp->next_out = (void *) out; + zp->avail_out = DEFAULT_IO_BUFFER_SIZE; + + res = inflate(zp, 0); + if (res != Z_OK && res != Z_STREAM_END) + pg_fatal("could not uncompress data: %s", zp->msg); + + out[DEFAULT_IO_BUFFER_SIZE - zp->avail_out] = '\0'; + ahwrite(out, 1, DEFAULT_IO_BUFFER_SIZE - zp->avail_out, AH); + } + } + + zp->next_in = NULL; + zp->avail_in = 0; + while (res != Z_STREAM_END) + { + zp->next_out = (void *) out; + zp->avail_out = DEFAULT_IO_BUFFER_SIZE; + res = inflate(zp, 0); + if (res != Z_OK && res != Z_STREAM_END) + pg_fatal("could not uncompress data: %s", zp->msg); + + out[DEFAULT_IO_BUFFER_SIZE - zp->avail_out] = '\0'; + ahwrite(out, 1, DEFAULT_IO_BUFFER_SIZE - zp->avail_out, AH); + } + + if (inflateEnd(zp) != Z_OK) + pg_fatal("could not close compression library: %s", zp->msg); + + free(buf); + free(out); + free(zp); +} + +/* Public routines that support gzip compressed data I/O */ +void +InitCompressorGzip(CompressorState *cs, + const pg_compress_specification compression_spec) +{ + cs->readData = ReadDataFromArchiveGzip; + cs->writeData = WriteDataToArchiveGzip; + cs->end = EndCompressorGzip; + + cs->compression_spec = compression_spec; + + /* + * If the caller has defined a write function, prepare the necessary + * state. Note that if the data is empty, End may be called immediately + * after Init, without ever calling Write. + */ + if (cs->writeF) + DeflateCompressorInit(cs); +} + + +/*---------------------- + * Compress File API + *---------------------- + */ + +static size_t +Gzip_read(void *ptr, size_t size, CompressFileHandle *CFH) +{ + gzFile gzfp = (gzFile) CFH->private_data; + int gzret; + + /* Reading zero bytes must be a no-op */ + if (size == 0) + return 0; + + gzret = gzread(gzfp, ptr, size); + + /* + * gzread returns zero on EOF as well as some error conditions, and less + * than zero on other error conditions, so we need to inspect for EOF on + * zero. + */ + if (gzret <= 0) + { + int errnum; + const char *errmsg; + + if (gzret == 0 && gzeof(gzfp)) + return 0; + + errmsg = gzerror(gzfp, &errnum); + + pg_fatal("could not read from input file: %s", + errnum == Z_ERRNO ? strerror(errno) : errmsg); + } + + return (size_t) gzret; +} + +static void +Gzip_write(const void *ptr, size_t size, CompressFileHandle *CFH) +{ + gzFile gzfp = (gzFile) CFH->private_data; + int errnum; + const char *errmsg; + + if (gzwrite(gzfp, ptr, size) != size) + { + errmsg = gzerror(gzfp, &errnum); + pg_fatal("could not write to file: %s", + errnum == Z_ERRNO ? strerror(errno) : errmsg); + } +} + +static int +Gzip_getc(CompressFileHandle *CFH) +{ + gzFile gzfp = (gzFile) CFH->private_data; + int ret; + + errno = 0; + ret = gzgetc(gzfp); + if (ret == EOF) + { + if (!gzeof(gzfp)) + pg_fatal("could not read from input file: %m"); + else + pg_fatal("could not read from input file: end of file"); + } + + return ret; +} + +static char * +Gzip_gets(char *ptr, int size, CompressFileHandle *CFH) +{ + gzFile gzfp = (gzFile) CFH->private_data; + + return gzgets(gzfp, ptr, size); +} + +static bool +Gzip_close(CompressFileHandle *CFH) +{ + gzFile gzfp = (gzFile) CFH->private_data; + + CFH->private_data = NULL; + + return gzclose(gzfp) == Z_OK; +} + +static bool +Gzip_eof(CompressFileHandle *CFH) +{ + gzFile gzfp = (gzFile) CFH->private_data; + + return gzeof(gzfp) == 1; +} + +static const char * +Gzip_get_error(CompressFileHandle *CFH) +{ + gzFile gzfp = (gzFile) CFH->private_data; + const char *errmsg; + int errnum; + + errmsg = gzerror(gzfp, &errnum); + if (errnum == Z_ERRNO) + errmsg = strerror(errno); + + return errmsg; +} + +static bool +Gzip_open(const char *path, int fd, const char *mode, CompressFileHandle *CFH) +{ + gzFile gzfp; + char mode_compression[32]; + + if (CFH->compression_spec.level != Z_DEFAULT_COMPRESSION) + { + /* + * user has specified a compression level, so tell zlib to use it + */ + snprintf(mode_compression, sizeof(mode_compression), "%s%d", + mode, CFH->compression_spec.level); + } + else + strcpy(mode_compression, mode); + + if (fd >= 0) + { + int dup_fd = dup(fd); + + if (dup_fd < 0) + return false; + gzfp = gzdopen(dup_fd, mode_compression); + if (gzfp == NULL) + { + close(dup_fd); + return false; + } + } + else + { + gzfp = gzopen(path, mode_compression); + if (gzfp == NULL) + return false; + } + + CFH->private_data = gzfp; + + return true; +} + +static bool +Gzip_open_write(const char *path, const char *mode, CompressFileHandle *CFH) +{ + char *fname; + bool ret; + int save_errno; + + fname = psprintf("%s.gz", path); + ret = CFH->open_func(fname, -1, mode, CFH); + + save_errno = errno; + pg_free(fname); + errno = save_errno; + + return ret; +} + +void +InitCompressFileHandleGzip(CompressFileHandle *CFH, + const pg_compress_specification compression_spec) +{ + CFH->open_func = Gzip_open; + CFH->open_write_func = Gzip_open_write; + CFH->read_func = Gzip_read; + CFH->write_func = Gzip_write; + CFH->gets_func = Gzip_gets; + CFH->getc_func = Gzip_getc; + CFH->close_func = Gzip_close; + CFH->eof_func = Gzip_eof; + CFH->get_error_func = Gzip_get_error; + + CFH->compression_spec = compression_spec; + + CFH->private_data = NULL; +} +#else /* HAVE_LIBZ */ +void +InitCompressorGzip(CompressorState *cs, + const pg_compress_specification compression_spec) +{ + pg_fatal("this build does not support compression with %s", "gzip"); +} + +void +InitCompressFileHandleGzip(CompressFileHandle *CFH, + const pg_compress_specification compression_spec) +{ + pg_fatal("this build does not support compression with %s", "gzip"); +} +#endif /* HAVE_LIBZ */ diff --git a/src/compat/19/pg_dump/compress_gzip.h b/src/compat/19/pg_dump/compress_gzip.h new file mode 100644 index 00000000..af1a2a34 --- /dev/null +++ b/src/compat/19/pg_dump/compress_gzip.h @@ -0,0 +1,24 @@ +/*------------------------------------------------------------------------- + * + * compress_gzip.h + * GZIP interface to compress_io.c routines + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/compress_gzip.h + * + *------------------------------------------------------------------------- + */ +#ifndef _COMPRESS_GZIP_H_ +#define _COMPRESS_GZIP_H_ + +#include "compress_io.h" + +extern void InitCompressorGzip(CompressorState *cs, + const pg_compress_specification compression_spec); +extern void InitCompressFileHandleGzip(CompressFileHandle *CFH, + const pg_compress_specification compression_spec); + +#endif /* _COMPRESS_GZIP_H_ */ diff --git a/src/compat/19/pg_dump/compress_io.c b/src/compat/19/pg_dump/compress_io.c new file mode 100644 index 00000000..52652b0d --- /dev/null +++ b/src/compat/19/pg_dump/compress_io.c @@ -0,0 +1,300 @@ +/*------------------------------------------------------------------------- + * + * compress_io.c + * Routines for archivers to write an uncompressed or compressed data + * stream. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * This file includes two APIs for dealing with compressed data. The first + * provides more flexibility, using callbacks to read/write data from the + * underlying stream. The second API is a wrapper around fopen and + * friends, providing an interface similar to those, but abstracts away + * the possible compression. The second API is aimed for the resulting + * files to be easily manipulated with an external compression utility + * program. + * + * Compressor API + * -------------- + * + * The interface for writing to an archive consists of three functions: + * AllocateCompressor, writeData, and EndCompressor. First you call + * AllocateCompressor, then write all the data by calling writeData as many + * times as needed, and finally EndCompressor. writeData will call the + * WriteFunc that was provided to AllocateCompressor for each chunk of + * compressed data. + * + * The interface for reading an archive consists of the same three functions: + * AllocateCompressor, readData, and EndCompressor. First you call + * AllocateCompressor, then read all the data by calling readData to read the + * whole compressed stream which repeatedly calls the given ReadFunc. ReadFunc + * returns the compressed data one chunk at a time. Then readData decompresses + * it and passes the decompressed data to ahwrite(), until ReadFunc returns 0 + * to signal EOF. The interface is the same for compressed and uncompressed + * streams. + * + * Compressed stream API + * ---------------------- + * + * The compressed stream API is providing a set of function pointers for + * opening, reading, writing, and finally closing files. The implemented + * function pointers are documented in the corresponding header file and are + * common for all streams. It allows the caller to use the same functions for + * both compressed and uncompressed streams. + * + * The interface consists of three functions, InitCompressFileHandle, + * InitDiscoverCompressFileHandle, and EndCompressFileHandle. If the + * compression is known, then start by calling InitCompressFileHandle, + * otherwise discover it by using InitDiscoverCompressFileHandle. Then call + * the function pointers as required for the read/write operations. Finally + * call EndCompressFileHandle to end the stream. + * + * InitDiscoverCompressFileHandle tries to infer the compression by the + * filename suffix. If the suffix is not yet known then it tries to simply + * open the file and if it fails, it tries to open the same file with + * compressed suffixes (.gz, .lz4 and .zst, in this order). + * + * IDENTIFICATION + * src/bin/pg_dump/compress_io.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include +#include + +#include "compress_gzip.h" +#include "compress_io.h" +#include "compress_lz4.h" +#include "compress_none.h" +#include "compress_zstd.h" + +/*---------------------- + * Generic functions + *---------------------- + */ + +/* + * Checks whether support for a compression algorithm is implemented in + * pg_dump/restore. + * + * On success returns NULL, otherwise returns a malloc'ed string which can be + * used by the caller in an error message. + */ +char * +supports_compression(const pg_compress_specification compression_spec) +{ + const pg_compress_algorithm algorithm = compression_spec.algorithm; + bool supported = false; + + if (algorithm == PG_COMPRESSION_NONE) + supported = true; +#ifdef HAVE_LIBZ + if (algorithm == PG_COMPRESSION_GZIP) + supported = true; +#endif +#ifdef USE_LZ4 + if (algorithm == PG_COMPRESSION_LZ4) + supported = true; +#endif +#ifdef USE_ZSTD + if (algorithm == PG_COMPRESSION_ZSTD) + supported = true; +#endif + + if (!supported) + return psprintf(_("this build does not support compression with %s"), + get_compress_algorithm_name(algorithm)); + + return NULL; +} + +/*---------------------- + * Compressor API + *---------------------- + */ + +/* + * Allocate a new compressor. + */ +CompressorState * +AllocateCompressor(const pg_compress_specification compression_spec, + ReadFunc readF, WriteFunc writeF) +{ + CompressorState *cs; + + cs = pg_malloc0_object(CompressorState); + cs->readF = readF; + cs->writeF = writeF; + + if (compression_spec.algorithm == PG_COMPRESSION_NONE) + InitCompressorNone(cs, compression_spec); + else if (compression_spec.algorithm == PG_COMPRESSION_GZIP) + InitCompressorGzip(cs, compression_spec); + else if (compression_spec.algorithm == PG_COMPRESSION_LZ4) + InitCompressorLZ4(cs, compression_spec); + else if (compression_spec.algorithm == PG_COMPRESSION_ZSTD) + InitCompressorZstd(cs, compression_spec); + + return cs; +} + +/* + * Terminate compression library context and flush its buffers. + */ +void +EndCompressor(ArchiveHandle *AH, CompressorState *cs) +{ + cs->end(AH, cs); + pg_free(cs); +} + +/*---------------------- + * Compressed stream API + *---------------------- + */ + +/* + * Private routines + */ +static int +hasSuffix(const char *filename, const char *suffix) +{ + int filenamelen = strlen(filename); + int suffixlen = strlen(suffix); + + if (filenamelen < suffixlen) + return 0; + + return memcmp(&filename[filenamelen - suffixlen], + suffix, + suffixlen) == 0; +} + +/* free() without changing errno; useful in several places below */ +static void +free_keep_errno(void *p) +{ + int save_errno = errno; + + free(p); + errno = save_errno; +} + +/* + * Public interface + */ + +/* + * Initialize a compress file handle for the specified compression algorithm. + */ +CompressFileHandle * +InitCompressFileHandle(const pg_compress_specification compression_spec) +{ + CompressFileHandle *CFH; + + CFH = pg_malloc0_object(CompressFileHandle); + + if (compression_spec.algorithm == PG_COMPRESSION_NONE) + InitCompressFileHandleNone(CFH, compression_spec); + else if (compression_spec.algorithm == PG_COMPRESSION_GZIP) + InitCompressFileHandleGzip(CFH, compression_spec); + else if (compression_spec.algorithm == PG_COMPRESSION_LZ4) + InitCompressFileHandleLZ4(CFH, compression_spec); + else if (compression_spec.algorithm == PG_COMPRESSION_ZSTD) + InitCompressFileHandleZstd(CFH, compression_spec); + + return CFH; +} + +/* + * Checks if a compressed file (with the specified extension) exists. + * + * The filename of the tested file is stored to fname buffer (the existing + * buffer is freed, new buffer is allocated and returned through the pointer). + */ +static bool +check_compressed_file(const char *path, char **fname, char *ext) +{ + free_keep_errno(*fname); + *fname = psprintf("%s.%s", path, ext); + return (access(*fname, F_OK) == 0); +} + +/* + * Open a file for reading. 'path' is the file to open, and 'mode' should + * be either "r" or "rb". + * + * If the file at 'path' contains the suffix of a supported compression method, + * currently this includes ".gz", ".lz4" and ".zst", then this compression will be used + * throughout. Otherwise the compression will be inferred by iteratively trying + * to open the file at 'path', first as is, then by appending known compression + * suffixes. So if you pass "foo" as 'path', this will open either "foo" or + * "foo.{gz,lz4,zst}", trying in that order. + * + * On failure, return NULL with an error code in errno. + */ +CompressFileHandle * +InitDiscoverCompressFileHandle(const char *path, const char *mode) +{ + CompressFileHandle *CFH = NULL; + struct stat st; + char *fname; + pg_compress_specification compression_spec = {0}; + + compression_spec.algorithm = PG_COMPRESSION_NONE; + + Assert(strcmp(mode, PG_BINARY_R) == 0); + + fname = pg_strdup(path); + + if (hasSuffix(fname, ".gz")) + compression_spec.algorithm = PG_COMPRESSION_GZIP; + else if (hasSuffix(fname, ".lz4")) + compression_spec.algorithm = PG_COMPRESSION_LZ4; + else if (hasSuffix(fname, ".zst")) + compression_spec.algorithm = PG_COMPRESSION_ZSTD; + else + { + if (stat(path, &st) == 0) + compression_spec.algorithm = PG_COMPRESSION_NONE; + else if (check_compressed_file(path, &fname, "gz")) + compression_spec.algorithm = PG_COMPRESSION_GZIP; + else if (check_compressed_file(path, &fname, "lz4")) + compression_spec.algorithm = PG_COMPRESSION_LZ4; + else if (check_compressed_file(path, &fname, "zst")) + compression_spec.algorithm = PG_COMPRESSION_ZSTD; + } + + CFH = InitCompressFileHandle(compression_spec); + errno = 0; + if (!CFH->open_func(fname, -1, mode, CFH)) + { + free_keep_errno(CFH); + CFH = NULL; + } + free_keep_errno(fname); + + return CFH; +} + +/* + * Close an open file handle and release its memory. + * + * On failure, returns false and sets errno appropriately. + */ +bool +EndCompressFileHandle(CompressFileHandle *CFH) +{ + bool ret = false; + + errno = 0; + if (CFH->private_data) + ret = CFH->close_func(CFH); + + free_keep_errno(CFH); + + return ret; +} diff --git a/src/compat/19/pg_dump/compress_io.h b/src/compat/19/pg_dump/compress_io.h new file mode 100644 index 00000000..ed7b14f0 --- /dev/null +++ b/src/compat/19/pg_dump/compress_io.h @@ -0,0 +1,208 @@ +/*------------------------------------------------------------------------- + * + * compress_io.h + * Interface to compress_io.c routines + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/compress_io.h + * + *------------------------------------------------------------------------- + */ + +#ifndef __COMPRESS_IO__ +#define __COMPRESS_IO__ + +#include "pg_backup_archiver.h" + +/* + * Default size used for IO buffers + * + * When changing this value, it's necessary to check the relevant test cases + * still exercise all the branches. This applies especially if the value is + * increased, in which case some loops may not get iterated. + */ +#define DEFAULT_IO_BUFFER_SIZE (128 * 1024) + +extern char *supports_compression(const pg_compress_specification compression_spec); + +/* + * Prototype for callback function used in writeData() + */ +typedef void (*WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len); + +/* + * Prototype for callback function used in readData() + * + * readData will call the read function repeatedly, until it returns 0 to signal + * EOF. readData passes a buffer to read the data into in *buf, of length + * *buflen. If that's not big enough for the callback function, it can free() it + * and malloc() a new one, returning the new buffer and its size in *buf and + * *buflen. + * + * Returns the number of bytes read into *buf, or 0 on EOF. + */ +typedef size_t (*ReadFunc) (ArchiveHandle *AH, char **buf, size_t *buflen); + +typedef struct CompressorState CompressorState; +struct CompressorState +{ + /* + * Read all compressed data from the input stream (via readF) and print it + * out with ahwrite(). + */ + void (*readData) (ArchiveHandle *AH, CompressorState *cs); + + /* + * Compress and write data to the output stream (via writeF). + */ + void (*writeData) (ArchiveHandle *AH, CompressorState *cs, + const void *data, size_t dLen); + + /* + * End compression and flush internal buffers if any. + */ + void (*end) (ArchiveHandle *AH, CompressorState *cs); + + /* + * Callback function to read from an already processed input stream + */ + ReadFunc readF; + + /* + * Callback function to write an already processed chunk of data. + */ + WriteFunc writeF; + + /* + * Compression specification for this state. + */ + pg_compress_specification compression_spec; + + /* + * Private data to be used by the compressor. + */ + void *private_data; +}; + +extern CompressorState *AllocateCompressor(const pg_compress_specification compression_spec, + ReadFunc readF, + WriteFunc writeF); +extern void EndCompressor(ArchiveHandle *AH, CompressorState *cs); + +/* + * Compress File Handle + */ +typedef struct CompressFileHandle CompressFileHandle; + +struct CompressFileHandle +{ + /* + * Open a file in mode. + * + * Pass either 'path' or 'fd' depending on whether a file path or a file + * descriptor is available. 'mode' can be one of 'r', 'rb', 'w', 'wb', + * 'a', and 'ab'. Requires an already initialized CompressFileHandle. + * + * Returns true on success and false on error. + */ + bool (*open_func) (const char *path, int fd, const char *mode, + CompressFileHandle *CFH); + + /* + * Open a file for writing. + * + * 'mode' can be one of 'w', 'wb', 'a', and 'ab'. Requires an already + * initialized CompressFileHandle. + * + * Returns true on success and false on error. + */ + bool (*open_write_func) (const char *path, const char *mode, + CompressFileHandle *CFH); + + /* + * Read up to 'size' bytes of data from the file and store them into + * 'ptr'. + * + * Returns number of bytes read (this might be less than 'size' if EOF was + * reached). Exits via pg_fatal for all error conditions. + */ + size_t (*read_func) (void *ptr, size_t size, + CompressFileHandle *CFH); + + /* + * Write 'size' bytes of data into the file from 'ptr'. + * + * Returns nothing, exits via pg_fatal for all error conditions. + */ + void (*write_func) (const void *ptr, size_t size, + CompressFileHandle *CFH); + + /* + * Read at most size - 1 characters from the compress file handle into + * 's'. + * + * Stop if an EOF or a newline is found first. 's' is always null + * terminated and contains the newline if it was found. + * + * Returns 's' on success, and NULL on error or when end of file occurs + * while no characters have been read. + */ + char *(*gets_func) (char *s, int size, CompressFileHandle *CFH); + + /* + * Read the next character from the compress file handle as 'unsigned + * char' cast into 'int'. + * + * Returns the character read on success and throws an internal error + * otherwise. It treats EOF as error. + */ + int (*getc_func) (CompressFileHandle *CFH); + + /* + * Test if EOF is reached in the compress file handle. + * + * Returns true if it is reached. + */ + bool (*eof_func) (CompressFileHandle *CFH); + + /* + * Close an open file handle. + * + * Returns true on success and false on error. + */ + bool (*close_func) (CompressFileHandle *CFH); + + /* + * Get a pointer to a string that describes an error that occurred during + * a compress file handle operation. + */ + const char *(*get_error_func) (CompressFileHandle *CFH); + + /* + * Compression specification for this file handle. + */ + pg_compress_specification compression_spec; + + /* + * Private data to be used by the compressor. + */ + void *private_data; +}; + +/* + * Initialize a compress file handle with the requested compression. + */ +extern CompressFileHandle *InitCompressFileHandle(const pg_compress_specification compression_spec); + +/* + * Initialize a compress file stream. Infer the compression algorithm + * from 'path', either by examining its suffix or by appending the supported + * suffixes in 'path'. + */ +extern CompressFileHandle *InitDiscoverCompressFileHandle(const char *path, + const char *mode); +extern bool EndCompressFileHandle(CompressFileHandle *CFH); +#endif diff --git a/src/compat/19/pg_dump/compress_lz4.c b/src/compat/19/pg_dump/compress_lz4.c new file mode 100644 index 00000000..0a787211 --- /dev/null +++ b/src/compat/19/pg_dump/compress_lz4.c @@ -0,0 +1,804 @@ +/*------------------------------------------------------------------------- + * + * compress_lz4.c + * Routines for archivers to write a LZ4 compressed data stream. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/compress_lz4.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" +#include + +#include "compress_lz4.h" +#include "pg_backup_utils.h" + +#ifdef USE_LZ4 +#include + +/* + * LZ4F_HEADER_SIZE_MAX first appeared in v1.7.5 of the library. + * Redefine it for installations with a lesser version. + */ +#ifndef LZ4F_HEADER_SIZE_MAX +#define LZ4F_HEADER_SIZE_MAX 32 +#endif + +/*--------------------------------- + * Common to both compression APIs + *--------------------------------- + */ + +/* + * (de)compression state used by both the Compressor and Stream APIs. + */ +typedef struct LZ4State +{ + /* + * Used by the Stream API to keep track of the file stream. + */ + FILE *fp; + + LZ4F_preferences_t prefs; + + LZ4F_compressionContext_t ctx; + LZ4F_decompressionContext_t dtx; + + /* + * Used by the Stream API's lazy initialization. + */ + bool inited; + + /* + * Used by the Stream API to distinguish between compression and + * decompression operations. + */ + bool compressing; + + /* + * I/O buffer area. + */ + char *buffer; /* buffer for compressed data */ + size_t buflen; /* allocated size of buffer */ + size_t bufdata; /* amount of valid data currently in buffer */ + /* These fields are used only while decompressing: */ + size_t bufnext; /* next buffer position to decompress */ + char *outbuf; /* buffer for decompressed data */ + size_t outbuflen; /* allocated size of outbuf */ + size_t outbufdata; /* amount of valid data currently in outbuf */ + size_t outbufnext; /* next outbuf position to return */ + + /* + * Used by both APIs to keep track of error codes. + */ + size_t errcode; +} LZ4State; + +/* + * LZ4State_compression_init + * Initialize the required LZ4State members for compression. + * + * Write the LZ4 frame header in a buffer keeping track of its length. Users of + * this function can choose when and how to write the header to a file stream. + * + * Returns true on success. In case of a failure returns false, and stores the + * error code in state->errcode. + */ +static bool +LZ4State_compression_init(LZ4State *state) +{ + size_t status; + + /* + * Compute size needed for buffer, assuming we will present at most + * DEFAULT_IO_BUFFER_SIZE input bytes at a time. + */ + state->buflen = LZ4F_compressBound(DEFAULT_IO_BUFFER_SIZE, &state->prefs); + + /* + * Add some slop to ensure we're not forced to flush every time. + * + * The present slop factor of 50% is chosen so that the typical output + * block size is about 128K when DEFAULT_IO_BUFFER_SIZE = 128K. We might + * need a different slop factor to maintain that equivalence if + * DEFAULT_IO_BUFFER_SIZE is changed dramatically. + */ + state->buflen += state->buflen / 2; + + /* + * LZ4F_compressBegin requires a buffer that is greater or equal to + * LZ4F_HEADER_SIZE_MAX. Verify that the requirement is met. + */ + if (state->buflen < LZ4F_HEADER_SIZE_MAX) + state->buflen = LZ4F_HEADER_SIZE_MAX; + + status = LZ4F_createCompressionContext(&state->ctx, LZ4F_VERSION); + if (LZ4F_isError(status)) + { + state->errcode = status; + return false; + } + + state->buffer = pg_malloc(state->buflen); + + /* + * Insert LZ4 header into buffer. + */ + status = LZ4F_compressBegin(state->ctx, + state->buffer, state->buflen, + &state->prefs); + if (LZ4F_isError(status)) + { + state->errcode = status; + return false; + } + + state->bufdata = status; + + return true; +} + +/*---------------------- + * Compressor API + *---------------------- + */ + +/* Private routines that support LZ4 compressed data I/O */ + +static void +ReadDataFromArchiveLZ4(ArchiveHandle *AH, CompressorState *cs) +{ + size_t r; + size_t readbuflen; + char *outbuf; + char *readbuf; + LZ4F_decompressionContext_t ctx = NULL; + LZ4F_decompressOptions_t dec_opt; + LZ4F_errorCode_t status; + + memset(&dec_opt, 0, sizeof(dec_opt)); + status = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION); + if (LZ4F_isError(status)) + pg_fatal("could not create LZ4 decompression context: %s", + LZ4F_getErrorName(status)); + + outbuf = pg_malloc(DEFAULT_IO_BUFFER_SIZE); + readbuf = pg_malloc(DEFAULT_IO_BUFFER_SIZE); + readbuflen = DEFAULT_IO_BUFFER_SIZE; + while ((r = cs->readF(AH, &readbuf, &readbuflen)) > 0) + { + char *readp; + char *readend; + + /* Process one chunk */ + readp = readbuf; + readend = readbuf + r; + while (readp < readend) + { + size_t out_size = DEFAULT_IO_BUFFER_SIZE; + size_t read_size = readend - readp; + + status = LZ4F_decompress(ctx, outbuf, &out_size, + readp, &read_size, &dec_opt); + if (LZ4F_isError(status)) + pg_fatal("could not decompress: %s", + LZ4F_getErrorName(status)); + + ahwrite(outbuf, 1, out_size, AH); + readp += read_size; + } + } + + pg_free(outbuf); + pg_free(readbuf); + + status = LZ4F_freeDecompressionContext(ctx); + if (LZ4F_isError(status)) + pg_fatal("could not free LZ4 decompression context: %s", + LZ4F_getErrorName(status)); +} + +static void +WriteDataToArchiveLZ4(ArchiveHandle *AH, CompressorState *cs, + const void *data, size_t dLen) +{ + LZ4State *state = (LZ4State *) cs->private_data; + size_t remaining = dLen; + + while (remaining > 0) + { + size_t chunk; + size_t required; + size_t status; + + /* We don't try to present more than DEFAULT_IO_BUFFER_SIZE bytes */ + chunk = Min(remaining, (size_t) DEFAULT_IO_BUFFER_SIZE); + + /* If not enough space, must flush buffer */ + required = LZ4F_compressBound(chunk, &state->prefs); + if (required > state->buflen - state->bufdata) + { + cs->writeF(AH, state->buffer, state->bufdata); + state->bufdata = 0; + } + + status = LZ4F_compressUpdate(state->ctx, + state->buffer + state->bufdata, + state->buflen - state->bufdata, + data, chunk, NULL); + + if (LZ4F_isError(status)) + pg_fatal("could not compress data: %s", + LZ4F_getErrorName(status)); + + state->bufdata += status; + + data = ((const char *) data) + chunk; + remaining -= chunk; + } +} + +static void +EndCompressorLZ4(ArchiveHandle *AH, CompressorState *cs) +{ + LZ4State *state = (LZ4State *) cs->private_data; + size_t required; + size_t status; + + /* Nothing needs to be done */ + if (!state) + return; + + /* We might need to flush the buffer to make room for LZ4F_compressEnd */ + required = LZ4F_compressBound(0, &state->prefs); + if (required > state->buflen - state->bufdata) + { + cs->writeF(AH, state->buffer, state->bufdata); + state->bufdata = 0; + } + + status = LZ4F_compressEnd(state->ctx, + state->buffer + state->bufdata, + state->buflen - state->bufdata, + NULL); + if (LZ4F_isError(status)) + pg_fatal("could not end compression: %s", + LZ4F_getErrorName(status)); + state->bufdata += status; + + /* Write the final bufferload */ + cs->writeF(AH, state->buffer, state->bufdata); + + status = LZ4F_freeCompressionContext(state->ctx); + if (LZ4F_isError(status)) + pg_fatal("could not end compression: %s", + LZ4F_getErrorName(status)); + + pg_free(state->buffer); + pg_free(state); + + cs->private_data = NULL; +} + +/* + * Public routines that support LZ4 compressed data I/O + */ +void +InitCompressorLZ4(CompressorState *cs, const pg_compress_specification compression_spec) +{ + LZ4State *state; + + cs->readData = ReadDataFromArchiveLZ4; + cs->writeData = WriteDataToArchiveLZ4; + cs->end = EndCompressorLZ4; + + cs->compression_spec = compression_spec; + + /* + * Read operations have access to the whole input. No state needs to be + * carried between calls. + */ + if (cs->readF) + return; + + state = pg_malloc0_object(LZ4State); + if (cs->compression_spec.level >= 0) + state->prefs.compressionLevel = cs->compression_spec.level; + + if (!LZ4State_compression_init(state)) + pg_fatal("could not initialize LZ4 compression: %s", + LZ4F_getErrorName(state->errcode)); + + cs->private_data = state; +} + +/*---------------------- + * Compress Stream API + *---------------------- + */ + + +/* + * LZ4 equivalent to feof() or gzeof(). Return true iff there is no + * more buffered data and the end of the input file has been reached. + */ +static bool +LZ4Stream_eof(CompressFileHandle *CFH) +{ + LZ4State *state = (LZ4State *) CFH->private_data; + + return state->outbufnext >= state->outbufdata && + state->bufnext >= state->bufdata && + feof(state->fp); +} + +static const char * +LZ4Stream_get_error(CompressFileHandle *CFH) +{ + LZ4State *state = (LZ4State *) CFH->private_data; + const char *errmsg; + + if (LZ4F_isError(state->errcode)) + errmsg = LZ4F_getErrorName(state->errcode); + else + errmsg = strerror(errno); + + return errmsg; +} + +/* + * Initialize an already alloc'ed LZ4State struct for subsequent calls. + * + * Creates the necessary contexts for either compression or decompression. When + * compressing data (indicated by compressing=true), it additionally writes the + * LZ4 header in the output buffer. + * + * It's expected that a not-yet-initialized LZ4State will be zero-filled. + * + * Returns true on success. In case of a failure returns false, and stores the + * error code in state->errcode. + */ +static bool +LZ4Stream_init(LZ4State *state, bool compressing) +{ + size_t status; + + if (state->inited) + return true; + + state->compressing = compressing; + + if (state->compressing) + { + if (!LZ4State_compression_init(state)) + return false; + } + else + { + status = LZ4F_createDecompressionContext(&state->dtx, LZ4F_VERSION); + if (LZ4F_isError(status)) + { + state->errcode = status; + return false; + } + + state->buflen = DEFAULT_IO_BUFFER_SIZE; + state->buffer = pg_malloc(state->buflen); + state->outbuflen = DEFAULT_IO_BUFFER_SIZE; + state->outbuf = pg_malloc(state->outbuflen); + } + + state->inited = true; + return true; +} + +/* + * The workhorse for reading decompressed content out of an LZ4 compressed + * stream. + * + * It will read up to 'ptrsize' decompressed content, or up to the new line + * char if one is found first when the eol_flag is set. + * + * Returns the number of bytes of decompressed data copied into the ptr + * buffer, or -1 in case of error. + */ +static int +LZ4Stream_read_internal(LZ4State *state, void *ptr, int ptrsize, bool eol_flag) +{ + int dsize = 0; + int remaining = ptrsize; + + /* Lazy init */ + if (!LZ4Stream_init(state, false /* decompressing */ )) + { + pg_log_error("unable to initialize LZ4 library: %s", + LZ4F_getErrorName(state->errcode)); + return -1; + } + + /* Loop until postcondition is satisfied */ + while (remaining > 0) + { + /* + * If we already have some decompressed data, return that. + */ + if (state->outbufnext < state->outbufdata) + { + char *outptr = state->outbuf + state->outbufnext; + size_t readlen = state->outbufdata - state->outbufnext; + bool eol_found = false; + + if (readlen > remaining) + readlen = remaining; + /* If eol_flag is set, don't read beyond a newline */ + if (eol_flag) + { + char *eolptr = memchr(outptr, '\n', readlen); + + if (eolptr) + { + readlen = eolptr - outptr + 1; + eol_found = true; + } + } + memcpy(ptr, outptr, readlen); + ptr = ((char *) ptr) + readlen; + state->outbufnext += readlen; + dsize += readlen; + remaining -= readlen; + if (eol_found || remaining == 0) + break; + /* We must have emptied outbuf */ + Assert(state->outbufnext >= state->outbufdata); + } + + /* + * If we don't have any pending compressed data, load more into + * state->buffer. + */ + if (state->bufnext >= state->bufdata) + { + size_t rsize; + + rsize = fread(state->buffer, 1, state->buflen, state->fp); + if (rsize < state->buflen && !feof(state->fp)) + { + pg_log_error("could not read from input file: %m"); + return -1; + } + if (rsize == 0) + break; /* must be EOF */ + state->bufdata = rsize; + state->bufnext = 0; + } + + /* + * Decompress some data into state->outbuf. + */ + { + size_t status; + size_t outlen = state->outbuflen; + size_t inlen = state->bufdata - state->bufnext; + + status = LZ4F_decompress(state->dtx, + state->outbuf, &outlen, + state->buffer + state->bufnext, + &inlen, + NULL); + if (LZ4F_isError(status)) + { + state->errcode = status; + pg_log_error("could not read from input file: %s", + LZ4F_getErrorName(state->errcode)); + return -1; + } + state->bufnext += inlen; + state->outbufdata = outlen; + state->outbufnext = 0; + } + } + + return dsize; +} + +/* + * Compress size bytes from ptr and write them to the stream. + */ +static void +LZ4Stream_write(const void *ptr, size_t size, CompressFileHandle *CFH) +{ + LZ4State *state = (LZ4State *) CFH->private_data; + size_t remaining = size; + + /* Lazy init */ + if (!LZ4Stream_init(state, true)) + pg_fatal("unable to initialize LZ4 library: %s", + LZ4F_getErrorName(state->errcode)); + + while (remaining > 0) + { + size_t chunk; + size_t required; + size_t status; + + /* We don't try to present more than DEFAULT_IO_BUFFER_SIZE bytes */ + chunk = Min(remaining, (size_t) DEFAULT_IO_BUFFER_SIZE); + + /* If not enough space, must flush buffer */ + required = LZ4F_compressBound(chunk, &state->prefs); + if (required > state->buflen - state->bufdata) + { + errno = 0; + if (fwrite(state->buffer, 1, state->bufdata, state->fp) != state->bufdata) + { + errno = (errno) ? errno : ENOSPC; + pg_fatal("error during writing: %m"); + } + state->bufdata = 0; + } + + status = LZ4F_compressUpdate(state->ctx, + state->buffer + state->bufdata, + state->buflen - state->bufdata, + ptr, chunk, NULL); + if (LZ4F_isError(status)) + pg_fatal("error during writing: %s", LZ4F_getErrorName(status)); + state->bufdata += status; + + ptr = ((const char *) ptr) + chunk; + remaining -= chunk; + } +} + +/* + * fread() equivalent implementation for LZ4 compressed files. + */ +static size_t +LZ4Stream_read(void *ptr, size_t size, CompressFileHandle *CFH) +{ + LZ4State *state = (LZ4State *) CFH->private_data; + int ret; + + if ((ret = LZ4Stream_read_internal(state, ptr, size, false)) < 0) + pg_fatal("could not read from input file: %s", LZ4Stream_get_error(CFH)); + + return (size_t) ret; +} + +/* + * fgetc() equivalent implementation for LZ4 compressed files. + */ +static int +LZ4Stream_getc(CompressFileHandle *CFH) +{ + LZ4State *state = (LZ4State *) CFH->private_data; + unsigned char c; + + if (LZ4Stream_read_internal(state, &c, 1, false) <= 0) + { + if (!LZ4Stream_eof(CFH)) + pg_fatal("could not read from input file: %s", LZ4Stream_get_error(CFH)); + else + pg_fatal("could not read from input file: end of file"); + } + + return c; +} + +/* + * fgets() equivalent implementation for LZ4 compressed files. + */ +static char * +LZ4Stream_gets(char *ptr, int size, CompressFileHandle *CFH) +{ + LZ4State *state = (LZ4State *) CFH->private_data; + int ret; + + ret = LZ4Stream_read_internal(state, ptr, size - 1, true); + + /* + * LZ4Stream_read_internal returning 0 or -1 means that it was either an + * EOF or an error, but gets_func is defined to return NULL in either case + * so we can treat both the same here. + */ + if (ret <= 0) + return NULL; + + /* + * Our caller expects the return string to be NULL terminated and we know + * that ret is greater than zero. + */ + ptr[ret - 1] = '\0'; + + return ptr; +} + +/* + * Finalize (de)compression of a stream. When compressing it will write any + * remaining content and/or generated footer from the LZ4 API. + */ +static bool +LZ4Stream_close(CompressFileHandle *CFH) +{ + FILE *fp; + LZ4State *state = (LZ4State *) CFH->private_data; + size_t required; + size_t status; + int ret; + bool success = true; + + fp = state->fp; + if (state->inited) + { + if (state->compressing) + { + /* We might need to flush the buffer to make room */ + required = LZ4F_compressBound(0, &state->prefs); + if (required > state->buflen - state->bufdata) + { + errno = 0; + if (fwrite(state->buffer, 1, state->bufdata, state->fp) != state->bufdata) + { + errno = (errno) ? errno : ENOSPC; + pg_log_error("could not write to output file: %m"); + success = false; + } + state->bufdata = 0; + } + + status = LZ4F_compressEnd(state->ctx, + state->buffer + state->bufdata, + state->buflen - state->bufdata, + NULL); + if (LZ4F_isError(status)) + { + pg_log_error("could not end compression: %s", + LZ4F_getErrorName(status)); + success = false; + } + else + state->bufdata += status; + + errno = 0; + if (fwrite(state->buffer, 1, state->bufdata, state->fp) != state->bufdata) + { + errno = (errno) ? errno : ENOSPC; + pg_log_error("could not write to output file: %m"); + success = false; + } + + status = LZ4F_freeCompressionContext(state->ctx); + if (LZ4F_isError(status)) + { + pg_log_error("could not end compression: %s", + LZ4F_getErrorName(status)); + success = false; + } + } + else + { + status = LZ4F_freeDecompressionContext(state->dtx); + if (LZ4F_isError(status)) + { + pg_log_error("could not end decompression: %s", + LZ4F_getErrorName(status)); + success = false; + } + pg_free(state->outbuf); + } + + pg_free(state->buffer); + } + + pg_free(state); + CFH->private_data = NULL; + + errno = 0; + ret = fclose(fp); + if (ret != 0) + { + pg_log_error("could not close file: %m"); + success = false; + } + + return success; +} + +static bool +LZ4Stream_open(const char *path, int fd, const char *mode, + CompressFileHandle *CFH) +{ + LZ4State *state = (LZ4State *) CFH->private_data; + + if (fd >= 0) + { + int dup_fd = dup(fd); + + if (dup_fd < 0) + { + state->errcode = errno; + return false; + } + state->fp = fdopen(dup_fd, mode); + if (state->fp == NULL) + { + state->errcode = errno; + close(dup_fd); + return false; + } + } + else + { + state->fp = fopen(path, mode); + if (state->fp == NULL) + { + state->errcode = errno; + return false; + } + } + + return true; +} + +static bool +LZ4Stream_open_write(const char *path, const char *mode, CompressFileHandle *CFH) +{ + char *fname; + int save_errno; + bool ret; + + fname = psprintf("%s.lz4", path); + ret = CFH->open_func(fname, -1, mode, CFH); + + save_errno = errno; + pg_free(fname); + errno = save_errno; + + return ret; +} + +/* + * Public routines + */ +void +InitCompressFileHandleLZ4(CompressFileHandle *CFH, + const pg_compress_specification compression_spec) +{ + LZ4State *state; + + CFH->open_func = LZ4Stream_open; + CFH->open_write_func = LZ4Stream_open_write; + CFH->read_func = LZ4Stream_read; + CFH->write_func = LZ4Stream_write; + CFH->gets_func = LZ4Stream_gets; + CFH->getc_func = LZ4Stream_getc; + CFH->eof_func = LZ4Stream_eof; + CFH->close_func = LZ4Stream_close; + CFH->get_error_func = LZ4Stream_get_error; + + CFH->compression_spec = compression_spec; + state = pg_malloc0_object(LZ4State); + if (CFH->compression_spec.level >= 0) + state->prefs.compressionLevel = CFH->compression_spec.level; + + CFH->private_data = state; +} +#else /* USE_LZ4 */ +void +InitCompressorLZ4(CompressorState *cs, + const pg_compress_specification compression_spec) +{ + pg_fatal("this build does not support compression with %s", "LZ4"); +} + +void +InitCompressFileHandleLZ4(CompressFileHandle *CFH, + const pg_compress_specification compression_spec) +{ + pg_fatal("this build does not support compression with %s", "LZ4"); +} +#endif /* USE_LZ4 */ diff --git a/src/compat/19/pg_dump/compress_lz4.h b/src/compat/19/pg_dump/compress_lz4.h new file mode 100644 index 00000000..7360a469 --- /dev/null +++ b/src/compat/19/pg_dump/compress_lz4.h @@ -0,0 +1,24 @@ +/*------------------------------------------------------------------------- + * + * compress_lz4.h + * LZ4 interface to compress_io.c routines + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/compress_lz4.h + * + *------------------------------------------------------------------------- + */ +#ifndef _COMPRESS_LZ4_H_ +#define _COMPRESS_LZ4_H_ + +#include "compress_io.h" + +extern void InitCompressorLZ4(CompressorState *cs, + const pg_compress_specification compression_spec); +extern void InitCompressFileHandleLZ4(CompressFileHandle *CFH, + const pg_compress_specification compression_spec); + +#endif /* _COMPRESS_LZ4_H_ */ diff --git a/src/compat/19/pg_dump/compress_none.c b/src/compat/19/pg_dump/compress_none.c new file mode 100644 index 00000000..743e2ce9 --- /dev/null +++ b/src/compat/19/pg_dump/compress_none.c @@ -0,0 +1,287 @@ +/*------------------------------------------------------------------------- + * + * compress_none.c + * Routines for archivers to read or write an uncompressed stream. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/compress_none.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" +#include + +#include "compress_none.h" +#include "pg_backup_utils.h" + +/*---------------------- + * Compressor API + *---------------------- + */ + +/* + * We buffer outgoing data, just to ensure that data blocks written to the + * archive file are of reasonable size. The read side could use this struct, + * but there's no need because it does not retain data across calls. + */ +typedef struct NoneCompressorState +{ + char *buffer; /* buffer for unwritten data */ + size_t buflen; /* allocated size of buffer */ + size_t bufdata; /* amount of valid data currently in buffer */ +} NoneCompressorState; + +/* + * Private routines + */ + +static void +ReadDataFromArchiveNone(ArchiveHandle *AH, CompressorState *cs) +{ + size_t cnt; + char *buf; + size_t buflen; + + buflen = DEFAULT_IO_BUFFER_SIZE; + buf = pg_malloc(buflen); + + while ((cnt = cs->readF(AH, &buf, &buflen))) + { + ahwrite(buf, 1, cnt, AH); + } + + free(buf); +} + + +static void +WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs, + const void *data, size_t dLen) +{ + NoneCompressorState *nonecs = (NoneCompressorState *) cs->private_data; + size_t remaining = dLen; + + while (remaining > 0) + { + size_t chunk; + + /* Dump buffer if full */ + if (nonecs->bufdata >= nonecs->buflen) + { + cs->writeF(AH, nonecs->buffer, nonecs->bufdata); + nonecs->bufdata = 0; + } + /* And fill it */ + chunk = nonecs->buflen - nonecs->bufdata; + if (chunk > remaining) + chunk = remaining; + memcpy(nonecs->buffer + nonecs->bufdata, data, chunk); + nonecs->bufdata += chunk; + data = ((const char *) data) + chunk; + remaining -= chunk; + } +} + +static void +EndCompressorNone(ArchiveHandle *AH, CompressorState *cs) +{ + NoneCompressorState *nonecs = (NoneCompressorState *) cs->private_data; + + if (nonecs) + { + /* Dump buffer if nonempty */ + if (nonecs->bufdata > 0) + cs->writeF(AH, nonecs->buffer, nonecs->bufdata); + /* Free working state */ + pg_free(nonecs->buffer); + pg_free(nonecs); + cs->private_data = NULL; + } +} + +/* + * Public interface + */ + +void +InitCompressorNone(CompressorState *cs, + const pg_compress_specification compression_spec) +{ + cs->readData = ReadDataFromArchiveNone; + cs->writeData = WriteDataToArchiveNone; + cs->end = EndCompressorNone; + + cs->compression_spec = compression_spec; + + /* + * If the caller has defined a write function, prepare the necessary + * buffer. + */ + if (cs->writeF) + { + NoneCompressorState *nonecs; + + nonecs = pg_malloc_object(NoneCompressorState); + nonecs->buflen = DEFAULT_IO_BUFFER_SIZE; + nonecs->buffer = pg_malloc(nonecs->buflen); + nonecs->bufdata = 0; + + cs->private_data = nonecs; + } +} + + +/*---------------------- + * Compress File API + *---------------------- + */ + +/* + * Private routines + */ + +static size_t +read_none(void *ptr, size_t size, CompressFileHandle *CFH) +{ + FILE *fp = (FILE *) CFH->private_data; + size_t ret; + + ret = fread(ptr, 1, size, fp); + if (ferror(fp)) + pg_fatal("could not read from input file: %m"); + + return ret; +} + +static void +write_none(const void *ptr, size_t size, CompressFileHandle *CFH) +{ + size_t ret; + + errno = 0; + ret = fwrite(ptr, 1, size, (FILE *) CFH->private_data); + if (ret != size) + { + errno = (errno) ? errno : ENOSPC; + pg_fatal("could not write to file: %m"); + } +} + +static const char * +get_error_none(CompressFileHandle *CFH) +{ + return strerror(errno); +} + +static char * +gets_none(char *ptr, int size, CompressFileHandle *CFH) +{ + return fgets(ptr, size, (FILE *) CFH->private_data); +} + +static int +getc_none(CompressFileHandle *CFH) +{ + FILE *fp = (FILE *) CFH->private_data; + int ret; + + ret = fgetc(fp); + if (ret == EOF) + { + if (!feof(fp)) + pg_fatal("could not read from input file: %m"); + else + pg_fatal("could not read from input file: end of file"); + } + + return ret; +} + +static bool +close_none(CompressFileHandle *CFH) +{ + FILE *fp = (FILE *) CFH->private_data; + int ret = 0; + + CFH->private_data = NULL; + + if (fp) + { + errno = 0; + ret = fclose(fp); + if (ret != 0) + pg_log_error("could not close file: %m"); + } + + return ret == 0; +} + +static bool +eof_none(CompressFileHandle *CFH) +{ + return feof((FILE *) CFH->private_data) != 0; +} + +static bool +open_none(const char *path, int fd, const char *mode, CompressFileHandle *CFH) +{ + Assert(CFH->private_data == NULL); + + if (fd >= 0) + { + int dup_fd = dup(fd); + + if (dup_fd < 0) + return false; + CFH->private_data = fdopen(dup_fd, mode); + if (CFH->private_data == NULL) + { + close(dup_fd); + return false; + } + } + else + { + CFH->private_data = fopen(path, mode); + if (CFH->private_data == NULL) + return false; + } + + return true; +} + +static bool +open_write_none(const char *path, const char *mode, CompressFileHandle *CFH) +{ + Assert(CFH->private_data == NULL); + + CFH->private_data = fopen(path, mode); + if (CFH->private_data == NULL) + return false; + + return true; +} + +/* + * Public interface + */ + +void +InitCompressFileHandleNone(CompressFileHandle *CFH, + const pg_compress_specification compression_spec) +{ + CFH->open_func = open_none; + CFH->open_write_func = open_write_none; + CFH->read_func = read_none; + CFH->write_func = write_none; + CFH->gets_func = gets_none; + CFH->getc_func = getc_none; + CFH->close_func = close_none; + CFH->eof_func = eof_none; + CFH->get_error_func = get_error_none; + + CFH->private_data = NULL; +} diff --git a/src/compat/19/pg_dump/compress_none.h b/src/compat/19/pg_dump/compress_none.h new file mode 100644 index 00000000..5134f012 --- /dev/null +++ b/src/compat/19/pg_dump/compress_none.h @@ -0,0 +1,24 @@ +/*------------------------------------------------------------------------- + * + * compress_none.h + * Uncompressed interface to compress_io.c routines + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/compress_none.h + * + *------------------------------------------------------------------------- + */ +#ifndef _COMPRESS_NONE_H_ +#define _COMPRESS_NONE_H_ + +#include "compress_io.h" + +extern void InitCompressorNone(CompressorState *cs, + const pg_compress_specification compression_spec); +extern void InitCompressFileHandleNone(CompressFileHandle *CFH, + const pg_compress_specification compression_spec); + +#endif /* _COMPRESS_NONE_H_ */ diff --git a/src/compat/19/pg_dump/compress_zstd.c b/src/compat/19/pg_dump/compress_zstd.c new file mode 100644 index 00000000..68f1d815 --- /dev/null +++ b/src/compat/19/pg_dump/compress_zstd.c @@ -0,0 +1,594 @@ +/*------------------------------------------------------------------------- + * + * compress_zstd.c + * Routines for archivers to write a Zstd compressed data stream. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/compress_zstd.c + * + *------------------------------------------------------------------------- + */ + +#include "postgres_fe.h" +#include + +#include "compress_zstd.h" +#include "pg_backup_utils.h" + +#ifndef USE_ZSTD + +void +InitCompressorZstd(CompressorState *cs, const pg_compress_specification compression_spec) +{ + pg_fatal("this build does not support compression with %s", "ZSTD"); +} + +void +InitCompressFileHandleZstd(CompressFileHandle *CFH, const pg_compress_specification compression_spec) +{ + pg_fatal("this build does not support compression with %s", "ZSTD"); +} + +#else + +#include + +typedef struct ZstdCompressorState +{ + /* This is a normal file to which we read/write compressed data */ + FILE *fp; + + ZSTD_CStream *cstream; + ZSTD_DStream *dstream; + ZSTD_outBuffer output; + ZSTD_inBuffer input; + + /* pointer to a static string like from strerror(), for Zstd_write() */ + const char *zstderror; +} ZstdCompressorState; + +static ZSTD_CStream *_ZstdCStreamParams(pg_compress_specification compress); +static void EndCompressorZstd(ArchiveHandle *AH, CompressorState *cs); +static void WriteDataToArchiveZstd(ArchiveHandle *AH, CompressorState *cs, + const void *data, size_t dLen); +static void ReadDataFromArchiveZstd(ArchiveHandle *AH, CompressorState *cs); + +static void +_Zstd_CCtx_setParam_or_die(ZSTD_CStream *cstream, + ZSTD_cParameter param, int value, char *paramname) +{ + size_t res; + + res = ZSTD_CCtx_setParameter(cstream, param, value); + if (ZSTD_isError(res)) + pg_fatal("could not set compression parameter \"%s\": %s", + paramname, ZSTD_getErrorName(res)); +} + +/* Return a compression stream with parameters set per argument */ +static ZSTD_CStream * +_ZstdCStreamParams(pg_compress_specification compress) +{ + ZSTD_CStream *cstream; + + cstream = ZSTD_createCStream(); + if (cstream == NULL) + pg_fatal("could not initialize compression library"); + + _Zstd_CCtx_setParam_or_die(cstream, ZSTD_c_compressionLevel, + compress.level, "level"); + + if (compress.options & PG_COMPRESSION_OPTION_LONG_DISTANCE) + _Zstd_CCtx_setParam_or_die(cstream, + ZSTD_c_enableLongDistanceMatching, + compress.long_distance, "long"); + + return cstream; +} + +/* Helper function for WriteDataToArchiveZstd and EndCompressorZstd */ +static void +_ZstdWriteCommon(ArchiveHandle *AH, CompressorState *cs, bool flush) +{ + ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->private_data; + ZSTD_inBuffer *input = &zstdcs->input; + ZSTD_outBuffer *output = &zstdcs->output; + + /* Loop while there's any input or until flushed */ + while (input->pos < input->size || flush) + { + size_t res; + + res = ZSTD_compressStream2(zstdcs->cstream, output, + input, flush ? ZSTD_e_end : ZSTD_e_continue); + + if (ZSTD_isError(res)) + pg_fatal("could not compress data: %s", ZSTD_getErrorName(res)); + + /* Dump output buffer if full, or if we're told to flush */ + if (output->pos >= output->size || flush) + { + cs->writeF(AH, output->dst, output->pos); + output->pos = 0; + } + + if (res == 0) + break; /* End of frame or all input consumed */ + } +} + +static void +EndCompressorZstd(ArchiveHandle *AH, CompressorState *cs) +{ + ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->private_data; + + if (cs->readF != NULL) + { + Assert(zstdcs->cstream == NULL); + ZSTD_freeDStream(zstdcs->dstream); + pg_free(unconstify(void *, zstdcs->input.src)); + } + else if (cs->writeF != NULL) + { + Assert(zstdcs->dstream == NULL); + _ZstdWriteCommon(AH, cs, true); + ZSTD_freeCStream(zstdcs->cstream); + } + + /* output buffer may be allocated in either mode */ + pg_free(zstdcs->output.dst); + pg_free(zstdcs); + cs->private_data = NULL; +} + +static void +WriteDataToArchiveZstd(ArchiveHandle *AH, CompressorState *cs, + const void *data, size_t dLen) +{ + ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->private_data; + + zstdcs->input.src = data; + zstdcs->input.size = dLen; + zstdcs->input.pos = 0; + + _ZstdWriteCommon(AH, cs, false); +} + +static void +ReadDataFromArchiveZstd(ArchiveHandle *AH, CompressorState *cs) +{ + ZstdCompressorState *zstdcs = (ZstdCompressorState *) cs->private_data; + ZSTD_outBuffer *output = &zstdcs->output; + ZSTD_inBuffer *input = &zstdcs->input; + size_t input_allocated_size = ZSTD_DStreamInSize(); + size_t res; + + for (;;) + { + size_t cnt; + + /* + * Read compressed data. Note that readF can resize the buffer; the + * new size is tracked and used for future loops. + */ + input->size = input_allocated_size; + cnt = cs->readF(AH, (char **) unconstify(void **, &input->src), &input->size); + + /* ensure that readF didn't *shrink* the buffer */ + Assert(input->size >= input_allocated_size); + input_allocated_size = input->size; + input->size = cnt; + input->pos = 0; + + if (cnt == 0) + break; + + /* Now decompress */ + while (input->pos < input->size) + { + output->pos = 0; + res = ZSTD_decompressStream(zstdcs->dstream, output, input); + if (ZSTD_isError(res)) + pg_fatal("could not decompress data: %s", ZSTD_getErrorName(res)); + + /* + * then write the decompressed data to the output handle + */ + ((char *) output->dst)[output->pos] = '\0'; + ahwrite(output->dst, 1, output->pos, AH); + + if (res == 0) + break; /* End of frame */ + } + } +} + +/* Public routine that supports Zstd compressed data I/O */ +void +InitCompressorZstd(CompressorState *cs, + const pg_compress_specification compression_spec) +{ + ZstdCompressorState *zstdcs; + + cs->readData = ReadDataFromArchiveZstd; + cs->writeData = WriteDataToArchiveZstd; + cs->end = EndCompressorZstd; + + cs->compression_spec = compression_spec; + + zstdcs = pg_malloc0_object(ZstdCompressorState); + cs->private_data = zstdcs; + + /* We expect that exactly one of readF/writeF is specified */ + Assert((cs->readF == NULL) != (cs->writeF == NULL)); + + if (cs->readF != NULL) + { + zstdcs->dstream = ZSTD_createDStream(); + if (zstdcs->dstream == NULL) + pg_fatal("could not initialize compression library"); + + zstdcs->input.size = ZSTD_DStreamInSize(); + zstdcs->input.src = pg_malloc(zstdcs->input.size); + + /* + * output.size is the buffer size we tell zstd it can output to. + * Allocate an additional byte such that ReadDataFromArchiveZstd() can + * call ahwrite() with a null-terminated string, which is an optimized + * case in ExecuteSqlCommandBuf(). + */ + zstdcs->output.size = ZSTD_DStreamOutSize(); + zstdcs->output.dst = pg_malloc(zstdcs->output.size + 1); + } + else if (cs->writeF != NULL) + { + zstdcs->cstream = _ZstdCStreamParams(cs->compression_spec); + + zstdcs->output.size = ZSTD_CStreamOutSize(); + zstdcs->output.dst = pg_malloc(zstdcs->output.size); + zstdcs->output.pos = 0; + } +} + +/* + * Compressed stream API + */ + +static size_t +Zstd_read_internal(void *ptr, size_t size, CompressFileHandle *CFH, bool exit_on_error) +{ + ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data; + ZSTD_inBuffer *input = &zstdcs->input; + ZSTD_outBuffer *output = &zstdcs->output; + size_t input_allocated_size = ZSTD_DStreamInSize(); + size_t res, + cnt; + + /* + * If this is the first call to the reading function, initialize the + * required datastructures. + */ + if (zstdcs->dstream == NULL) + { + zstdcs->input.src = pg_malloc0(input_allocated_size); + zstdcs->dstream = ZSTD_createDStream(); + if (zstdcs->dstream == NULL) + { + if (exit_on_error) + pg_fatal("could not initialize compression library"); + return -1; + } + } + + output->size = size; + output->dst = ptr; + output->pos = 0; + + while (output->pos < output->size) + { + Assert(input->pos <= input->size); + Assert(input->size <= input_allocated_size); + + /* + * If the input is completely consumed, start back at the beginning + */ + if (input->pos == input->size) + { + /* input->size is size produced by "fread" */ + input->size = 0; + /* input->pos is position consumed by decompress */ + input->pos = 0; + } + + /* read compressed data if we must produce more input */ + if (input->pos == input->size) + { + cnt = fread(unconstify(void *, input->src), 1, input_allocated_size, zstdcs->fp); + if (ferror(zstdcs->fp)) + { + if (exit_on_error) + pg_fatal("could not read from input file: %m"); + return -1; + } + + input->size = cnt; + + Assert(cnt <= input_allocated_size); + + /* If we have no more input to consume, we're done */ + if (cnt == 0) + break; + } + + while (input->pos < input->size) + { + /* now decompress */ + res = ZSTD_decompressStream(zstdcs->dstream, output, input); + + if (ZSTD_isError(res)) + { + if (exit_on_error) + pg_fatal("could not decompress data: %s", ZSTD_getErrorName(res)); + return -1; + } + + if (output->pos == output->size) + break; /* No more room for output */ + + if (res == 0) + break; /* End of frame */ + } + } + + return output->pos; +} + +static void +Zstd_write(const void *ptr, size_t size, CompressFileHandle *CFH) +{ + ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data; + ZSTD_inBuffer *input = &zstdcs->input; + ZSTD_outBuffer *output = &zstdcs->output; + size_t res, + cnt; + + input->src = ptr; + input->size = size; + input->pos = 0; + + if (zstdcs->cstream == NULL) + { + zstdcs->output.size = ZSTD_CStreamOutSize(); + zstdcs->output.dst = pg_malloc(zstdcs->output.size); + zstdcs->output.pos = 0; + zstdcs->cstream = _ZstdCStreamParams(CFH->compression_spec); + if (zstdcs->cstream == NULL) + pg_fatal("could not initialize compression library"); + } + + /* Consume all input, to be flushed later */ + while (input->pos < input->size) + { + res = ZSTD_compressStream2(zstdcs->cstream, output, input, ZSTD_e_continue); + if (ZSTD_isError(res)) + pg_fatal("could not write to file: %s", ZSTD_getErrorName(res)); + + /* Dump output buffer if full */ + if (output->pos >= output->size) + { + errno = 0; + cnt = fwrite(output->dst, 1, output->pos, zstdcs->fp); + if (cnt != output->pos) + { + errno = (errno) ? errno : ENOSPC; + pg_fatal("could not write to file: %m"); + } + output->pos = 0; + } + } +} + +static int +Zstd_getc(CompressFileHandle *CFH) +{ + unsigned char ret; + + if (CFH->read_func(&ret, 1, CFH) != 1) + pg_fatal("could not read from input file: end of file"); + return ret; +} + +static char * +Zstd_gets(char *buf, int len, CompressFileHandle *CFH) +{ + int i; + + Assert(len > 0); + + /* + * Read one byte at a time until newline or EOF. This is only used to read + * the list of LOs, and the I/O is buffered anyway. + */ + for (i = 0; i < len - 1; ++i) + { + if (Zstd_read_internal(&buf[i], 1, CFH, false) != 1) + break; + if (buf[i] == '\n') + { + ++i; + break; + } + } + buf[i] = '\0'; + return i > 0 ? buf : NULL; +} + +static size_t +Zstd_read(void *ptr, size_t size, CompressFileHandle *CFH) +{ + return Zstd_read_internal(ptr, size, CFH, true); +} + +static bool +Zstd_close(CompressFileHandle *CFH) +{ + ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data; + bool success = true; + + if (zstdcs->cstream) + { + size_t res, + cnt; + ZSTD_inBuffer *input = &zstdcs->input; + ZSTD_outBuffer *output = &zstdcs->output; + + /* Loop until the compression buffers are fully consumed */ + for (;;) + { + res = ZSTD_compressStream2(zstdcs->cstream, output, input, ZSTD_e_end); + if (ZSTD_isError(res)) + { + zstdcs->zstderror = ZSTD_getErrorName(res); + success = false; + break; + } + + errno = 0; + cnt = fwrite(output->dst, 1, output->pos, zstdcs->fp); + if (cnt != output->pos) + { + errno = (errno) ? errno : ENOSPC; + zstdcs->zstderror = strerror(errno); + success = false; + break; + } + output->pos = 0; + + if (res == 0) + break; /* End of frame */ + } + + ZSTD_freeCStream(zstdcs->cstream); + pg_free(zstdcs->output.dst); + } + + if (zstdcs->dstream) + { + ZSTD_freeDStream(zstdcs->dstream); + pg_free(unconstify(void *, zstdcs->input.src)); + } + + errno = 0; + if (fclose(zstdcs->fp) != 0) + { + zstdcs->zstderror = strerror(errno); + success = false; + } + + pg_free(zstdcs); + CFH->private_data = NULL; + return success; +} + +static bool +Zstd_eof(CompressFileHandle *CFH) +{ + ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data; + + return feof(zstdcs->fp); +} + +static bool +Zstd_open(const char *path, int fd, const char *mode, + CompressFileHandle *CFH) +{ + FILE *fp; + ZstdCompressorState *zstdcs; + + /* + * Clear state storage to avoid having the fd point to non-NULL memory on + * error return. + */ + CFH->private_data = NULL; + + zstdcs = (ZstdCompressorState *) pg_malloc_extended(sizeof(*zstdcs), + MCXT_ALLOC_NO_OOM | MCXT_ALLOC_ZERO); + if (!zstdcs) + { + errno = ENOMEM; + return false; + } + + if (fd >= 0) + { + int dup_fd = dup(fd); + + if (dup_fd < 0) + { + pg_free(zstdcs); + return false; + } + fp = fdopen(dup_fd, mode); + if (fp == NULL) + { + close(dup_fd); + pg_free(zstdcs); + return false; + } + } + else + { + fp = fopen(path, mode); + if (fp == NULL) + { + pg_free(zstdcs); + return false; + } + } + + zstdcs->fp = fp; + CFH->private_data = zstdcs; + + return true; +} + +static bool +Zstd_open_write(const char *path, const char *mode, CompressFileHandle *CFH) +{ + char fname[MAXPGPATH]; + + sprintf(fname, "%s.zst", path); + return CFH->open_func(fname, -1, mode, CFH); +} + +static const char * +Zstd_get_error(CompressFileHandle *CFH) +{ + ZstdCompressorState *zstdcs = (ZstdCompressorState *) CFH->private_data; + + return zstdcs->zstderror; +} + +void +InitCompressFileHandleZstd(CompressFileHandle *CFH, + const pg_compress_specification compression_spec) +{ + CFH->open_func = Zstd_open; + CFH->open_write_func = Zstd_open_write; + CFH->read_func = Zstd_read; + CFH->write_func = Zstd_write; + CFH->gets_func = Zstd_gets; + CFH->getc_func = Zstd_getc; + CFH->close_func = Zstd_close; + CFH->eof_func = Zstd_eof; + CFH->get_error_func = Zstd_get_error; + + CFH->compression_spec = compression_spec; + + CFH->private_data = NULL; +} + +#endif /* USE_ZSTD */ diff --git a/src/compat/19/pg_dump/compress_zstd.h b/src/compat/19/pg_dump/compress_zstd.h new file mode 100644 index 00000000..1222d710 --- /dev/null +++ b/src/compat/19/pg_dump/compress_zstd.h @@ -0,0 +1,25 @@ +/*------------------------------------------------------------------------- + * + * compress_zstd.h + * Zstd interface to compress_io.c routines + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/compress_zstd.h + * + *------------------------------------------------------------------------- + */ + +#ifndef COMPRESS_ZSTD_H +#define COMPRESS_ZSTD_H + +#include "compress_io.h" + +extern void InitCompressorZstd(CompressorState *cs, + const pg_compress_specification compression_spec); +extern void InitCompressFileHandleZstd(CompressFileHandle *CFH, + const pg_compress_specification compression_spec); + +#endif /* COMPRESS_ZSTD_H */ diff --git a/src/compat/19/pg_dump/connectdb.c b/src/compat/19/pg_dump/connectdb.c new file mode 100644 index 00000000..f3ce8b1c --- /dev/null +++ b/src/compat/19/pg_dump/connectdb.c @@ -0,0 +1,295 @@ +/*------------------------------------------------------------------------- + * + * connectdb.c + * This is a common file connection to the database. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/connectdb.c + * + *------------------------------------------------------------------------- + */ + +#include "postgres_fe.h" + +#include "common/connect.h" +#include "common/logging.h" +#include "common/string.h" +#include "connectdb.h" +#include "dumputils.h" +#include "fe_utils/string_utils.h" + +static char *constructConnStr(const char **keywords, const char **values); + +/* + * ConnectDatabase + * + * Make a database connection with the given parameters. An + * interactive password prompt is automatically issued if required. + * + * If fail_on_error is false, we return NULL without printing any message + * on failure, but preserve any prompted password for the next try. + * + * On success, the 'connstr' is set to a connection string containing + * the options used and 'server_version' is set to version so that caller + * can use them. + */ +PGconn * +ConnectDatabase(const char *dbname, const char *connection_string, + const char *pghost, const char *pgport, const char *pguser, + trivalue prompt_password, bool fail_on_error, const char *progname, + const char **connstr, int *server_version, char *password, + char *override_dbname) +{ + PGconn *conn; + bool new_pass; + const char *remoteversion_str; + int my_version; + const char **keywords = NULL; + const char **values = NULL; + PQconninfoOption *conn_opts = NULL; + int server_version_temp; + + if (prompt_password == TRI_YES && !password) + password = simple_prompt("Password: ", false); + + /* + * Start the connection. Loop until we have a password if requested by + * backend. + */ + do + { + int argcount = 8; + PQconninfoOption *conn_opt; + char *err_msg = NULL; + int i = 0; + + free(keywords); + free(values); + PQconninfoFree(conn_opts); + + /* + * Merge the connection info inputs given in form of connection string + * and other options. Explicitly discard any dbname value in the + * connection string; otherwise, PQconnectdbParams() would interpret + * that value as being itself a connection string. + */ + if (connection_string) + { + conn_opts = PQconninfoParse(connection_string, &err_msg); + if (conn_opts == NULL) + pg_fatal("%s", err_msg); + + for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++) + { + if (conn_opt->val != NULL && conn_opt->val[0] != '\0' && + strcmp(conn_opt->keyword, "dbname") != 0) + argcount++; + } + + keywords = pg_malloc0_array(const char *, (argcount + 1)); + values = pg_malloc0_array(const char *, (argcount + 1)); + + for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++) + { + if (conn_opt->val != NULL && conn_opt->val[0] != '\0' && + strcmp(conn_opt->keyword, "dbname") != 0) + { + keywords[i] = conn_opt->keyword; + values[i] = conn_opt->val; + i++; + } + } + } + else + { + keywords = pg_malloc0_array(const char *, (argcount + 1)); + values = pg_malloc0_array(const char *, (argcount + 1)); + } + + if (pghost) + { + keywords[i] = "host"; + values[i] = pghost; + i++; + } + if (pgport) + { + keywords[i] = "port"; + values[i] = pgport; + i++; + } + if (pguser) + { + keywords[i] = "user"; + values[i] = pguser; + i++; + } + if (password) + { + keywords[i] = "password"; + values[i] = password; + i++; + } + if (dbname) + { + keywords[i] = "dbname"; + values[i] = dbname; + i++; + } + if (override_dbname) + { + keywords[i] = "dbname"; + values[i] = override_dbname; + i++; + } + + keywords[i] = "fallback_application_name"; + values[i] = progname; + i++; + + new_pass = false; + conn = PQconnectdbParams(keywords, values, true); + + if (!conn) + pg_fatal("could not connect to database \"%s\"", dbname); + + if (PQstatus(conn) == CONNECTION_BAD && + PQconnectionNeedsPassword(conn) && + !password && + prompt_password != TRI_NO) + { + PQfinish(conn); + password = simple_prompt("Password: ", false); + new_pass = true; + } + } while (new_pass); + + /* check to see that the backend connection was successfully made */ + if (PQstatus(conn) == CONNECTION_BAD) + { + if (fail_on_error) + pg_fatal("%s", PQerrorMessage(conn)); + else + { + PQfinish(conn); + + free(keywords); + free(values); + PQconninfoFree(conn_opts); + + return NULL; + } + } + + /* + * Ok, connected successfully. If requested, remember the options used, in + * the form of a connection string. + */ + if (connstr) + *connstr = constructConnStr(keywords, values); + + free(keywords); + free(values); + PQconninfoFree(conn_opts); + + /* Check version */ + remoteversion_str = PQparameterStatus(conn, "server_version"); + if (!remoteversion_str) + pg_fatal("could not get server version"); + + server_version_temp = PQserverVersion(conn); + if (server_version_temp == 0) + pg_fatal("could not parse server version \"%s\"", + remoteversion_str); + + /* If requested, then copy server version to out variable. */ + if (server_version) + *server_version = server_version_temp; + + my_version = PG_VERSION_NUM; + + /* + * We allow the server to be back to 9.2, and up to any minor release of + * our own major version. (See also version check in pg_dump.c.) + */ + if (my_version != server_version_temp + && (server_version_temp < 90200 || + (server_version_temp / 100) > (my_version / 100))) + { + pg_log_error("aborting because of server version mismatch"); + pg_log_error_detail("server version: %s; %s version: %s", + remoteversion_str, progname, PG_VERSION); + exit_nicely(1); + } + + PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL)); + + return conn; +} + +/* + * constructConnStr + * + * Construct a connection string from the given keyword/value pairs. It is + * used to pass the connection options to the pg_dump subprocess. + * + * The following parameters are excluded: + * dbname - varies in each pg_dump invocation + * password - it's not secure to pass a password on the command line + * fallback_application_name - we'll let pg_dump set it + */ +static char * +constructConnStr(const char **keywords, const char **values) +{ + PQExpBuffer buf = createPQExpBuffer(); + char *connstr; + int i; + bool firstkeyword = true; + + /* Construct a new connection string in key='value' format. */ + for (i = 0; keywords[i] != NULL; i++) + { + if (strcmp(keywords[i], "dbname") == 0 || + strcmp(keywords[i], "password") == 0 || + strcmp(keywords[i], "fallback_application_name") == 0) + continue; + + if (!firstkeyword) + appendPQExpBufferChar(buf, ' '); + firstkeyword = false; + appendPQExpBuffer(buf, "%s=", keywords[i]); + appendConnStrVal(buf, values[i]); + } + + connstr = pg_strdup(buf->data); + destroyPQExpBuffer(buf); + return connstr; +} + +/* + * executeQuery + * + * Run a query, return the results, exit program on failure. + */ +PGresult * +executeQuery(PGconn *conn, const char *query) +{ + PGresult *res; + + pg_log_info("executing %s", query); + + res = PQexec(conn, query); + if (!res || + PQresultStatus(res) != PGRES_TUPLES_OK) + { + pg_log_error("query failed: %s", PQerrorMessage(conn)); + pg_log_error_detail("Query was: %s", query); + PQfinish(conn); + exit_nicely(1); + } + + return res; +} diff --git a/src/compat/19/pg_dump/connectdb.h b/src/compat/19/pg_dump/connectdb.h new file mode 100644 index 00000000..67813853 --- /dev/null +++ b/src/compat/19/pg_dump/connectdb.h @@ -0,0 +1,26 @@ +/*------------------------------------------------------------------------- + * + * connectdb.h + * Common header file for connection to the database. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/connectdb.h + * + *------------------------------------------------------------------------- + */ +#ifndef CONNECTDB_H +#define CONNECTDB_H + +#include "pg_backup.h" +#include "pg_backup_utils.h" + +extern PGconn *ConnectDatabase(const char *dbname, const char *connection_string, const char *pghost, + const char *pgport, const char *pguser, + trivalue prompt_password, bool fail_on_error, + const char *progname, const char **connstr, int *server_version, + char *password, char *override_dbname); +extern PGresult *executeQuery(PGconn *conn, const char *query); +#endif /* CONNECTDB_H */ diff --git a/src/compat/19/pg_dump/dumputils.c b/src/compat/19/pg_dump/dumputils.c new file mode 100644 index 00000000..dfb1f603 --- /dev/null +++ b/src/compat/19/pg_dump/dumputils.c @@ -0,0 +1,1005 @@ +/*------------------------------------------------------------------------- + * + * Utility routines for SQL dumping + * + * Basically this is stuff that is useful in both pg_dump and pg_dumpall. + * + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/bin/pg_dump/dumputils.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include + +#include "common/file_perm.h" +#include "common/logging.h" +#include "dumputils.h" +#include "fe_utils/string_utils.h" + +static const char restrict_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + +static bool parseAclItem(const char *item, const char *type, + const char *name, const char *subname, int remoteVersion, + PQExpBuffer grantee, PQExpBuffer grantor, + PQExpBuffer privs, PQExpBuffer privswgo); +static char *dequoteAclUserName(PQExpBuffer output, char *input); +static void AddAcl(PQExpBuffer aclbuf, const char *keyword, + const char *subname); + + +/* + * Sanitize a string to be included in an SQL comment or TOC listing, by + * replacing any newlines with spaces. This ensures each logical output line + * is in fact one physical output line, to prevent corruption of the dump + * (which could, in the worst case, present an SQL injection vulnerability + * if someone were to incautiously load a dump containing objects with + * maliciously crafted names). + * + * The result is a freshly malloc'd string. If the input string is NULL, + * return a malloc'ed empty string, unless want_hyphen, in which case return a + * malloc'ed hyphen. + * + * Note that we currently don't bother to quote names, meaning that the name + * fields aren't automatically parseable. "pg_restore -L" doesn't care because + * it only examines the dumpId field, but someday we might want to try harder. + */ +char * +sanitize_line(const char *str, bool want_hyphen) +{ + char *result; + char *s; + + if (!str) + return pg_strdup(want_hyphen ? "-" : ""); + + result = pg_strdup(str); + + for (s = result; *s != '\0'; s++) + { + if (*s == '\n' || *s == '\r') + *s = ' '; + } + + return result; +} + + +/* + * Build GRANT/REVOKE command(s) for an object. + * + * name: the object name, in the form to use in the commands (already quoted) + * subname: the sub-object name, if any (already quoted); NULL if none + * nspname: the namespace the object is in (NULL if none); not pre-quoted + * type: the object type (as seen in GRANT command: must be one of + * TABLE, SEQUENCE, FUNCTION, PROCEDURE, LANGUAGE, SCHEMA, DATABASE, TABLESPACE, + * FOREIGN DATA WRAPPER, SERVER, PARAMETER or LARGE OBJECT) + * acls: the ACL string fetched from the database + * baseacls: the initial ACL string for this object + * owner: username of object owner (will be passed through fmtId); can be + * NULL or empty string to indicate "no owner known" + * prefix: string to prefix to each generated command; typically empty + * remoteVersion: version of database + * + * Returns true if okay, false if could not parse the acl string. + * The resulting commands (if any) are appended to the contents of 'sql'. + * + * baseacls is typically the result of acldefault() for the object's type + * and owner. However, if there is a pg_init_privs entry for the object, + * it should instead be the initprivs ACLs. When acls is itself a + * pg_init_privs entry, baseacls is what to dump that relative to; then + * it can be either an acldefault() value or an empty ACL "{}". + * + * Note: when processing a default ACL, prefix is "ALTER DEFAULT PRIVILEGES " + * or something similar, and name is an empty string. + * + * Note: beware of passing a fmtId() result directly as 'name' or 'subname', + * since this routine uses fmtId() internally. + */ +bool +buildACLCommands(const char *name, const char *subname, const char *nspname, + const char *type, const char *acls, const char *baseacls, + const char *owner, const char *prefix, int remoteVersion, + PQExpBuffer sql) +{ + bool ok = true; + char **aclitems = NULL; + char **baseitems = NULL; + char **grantitems = NULL; + char **revokeitems = NULL; + int naclitems = 0; + int nbaseitems = 0; + int ngrantitems = 0; + int nrevokeitems = 0; + int i; + PQExpBuffer grantee, + grantor, + privs, + privswgo; + PQExpBuffer firstsql, + secondsql; + + /* + * If the acl was NULL (initial default state), we need do nothing. Note + * that this is distinguishable from all-privileges-revoked, which will + * look like an empty array ("{}"). + */ + if (acls == NULL || *acls == '\0') + return true; /* object has default permissions */ + + /* treat empty-string owner same as NULL */ + if (owner && *owner == '\0') + owner = NULL; + + /* Parse the acls array */ + if (!parsePGArray(acls, &aclitems, &naclitems)) + { + free(aclitems); + return false; + } + + /* Parse the baseacls too */ + if (!parsePGArray(baseacls, &baseitems, &nbaseitems)) + { + free(aclitems); + free(baseitems); + return false; + } + + /* + * Compare the actual ACL with the base ACL, extracting the privileges + * that need to be granted (i.e., are in the actual ACL but not the base + * ACL) and the ones that need to be revoked (the reverse). We use plain + * string comparisons to check for matches. In principle that could be + * fooled by extraneous issues such as whitespace, but since all these + * strings are the work of aclitemout(), it should be OK in practice. + * Besides, a false mismatch will just cause the output to be a little + * more verbose than it really needed to be. + */ + grantitems = pg_malloc_array(char *, naclitems); + for (i = 0; i < naclitems; i++) + { + bool found = false; + + for (int j = 0; j < nbaseitems; j++) + { + if (strcmp(aclitems[i], baseitems[j]) == 0) + { + found = true; + break; + } + } + if (!found) + grantitems[ngrantitems++] = aclitems[i]; + } + revokeitems = pg_malloc_array(char *, nbaseitems); + for (i = 0; i < nbaseitems; i++) + { + bool found = false; + + for (int j = 0; j < naclitems; j++) + { + if (strcmp(baseitems[i], aclitems[j]) == 0) + { + found = true; + break; + } + } + if (!found) + revokeitems[nrevokeitems++] = baseitems[i]; + } + + /* Prepare working buffers */ + grantee = createPQExpBuffer(); + grantor = createPQExpBuffer(); + privs = createPQExpBuffer(); + privswgo = createPQExpBuffer(); + + /* + * At the end, these two will be pasted together to form the result. + */ + firstsql = createPQExpBuffer(); + secondsql = createPQExpBuffer(); + + /* + * Build REVOKE statements for ACLs listed in revokeitems[]. + */ + for (i = 0; i < nrevokeitems; i++) + { + if (!parseAclItem(revokeitems[i], + type, name, subname, remoteVersion, + grantee, grantor, privs, NULL)) + { + ok = false; + break; + } + + if (privs->len > 0) + { + appendPQExpBuffer(firstsql, "%sREVOKE %s ON %s ", + prefix, privs->data, type); + if (nspname && *nspname) + appendPQExpBuffer(firstsql, "%s.", fmtId(nspname)); + if (name && *name) + appendPQExpBuffer(firstsql, "%s ", name); + appendPQExpBufferStr(firstsql, "FROM "); + if (grantee->len == 0) + appendPQExpBufferStr(firstsql, "PUBLIC;\n"); + else + appendPQExpBuffer(firstsql, "%s;\n", + fmtId(grantee->data)); + } + } + + /* + * At this point we have issued REVOKE statements for all initial and + * default privileges that are no longer present on the object, so we are + * almost ready to GRANT the privileges listed in grantitems[]. + * + * We still need some hacking though to cover the case where new default + * public privileges are added in new versions: the REVOKE ALL will revoke + * them, leading to behavior different from what the old version had, + * which is generally not what's wanted. So add back default privs if the + * source database is too old to have had that particular priv. (As of + * right now, no such cases exist in supported versions.) + */ + + /* + * Scan individual ACL items to be granted. + * + * The order in which privileges appear in the ACL string (the order they + * have been GRANT'd in, which the backend maintains) must be preserved to + * ensure that GRANTs WITH GRANT OPTION and subsequent GRANTs based on + * those are dumped in the correct order. However, some old server + * versions will show grants to PUBLIC before the owner's own grants; for + * consistency's sake, force the owner's grants to be output first. + */ + for (i = 0; i < ngrantitems; i++) + { + if (parseAclItem(grantitems[i], type, name, subname, remoteVersion, + grantee, grantor, privs, privswgo)) + { + /* + * If the grantor isn't the owner, we'll need to use SET SESSION + * AUTHORIZATION to become the grantor. Issue the SET/RESET only + * if there's something useful to do. + */ + if (privs->len > 0 || privswgo->len > 0) + { + PQExpBuffer thissql; + + /* Set owner as grantor if that's not explicit in the ACL */ + if (grantor->len == 0 && owner) + printfPQExpBuffer(grantor, "%s", owner); + + /* Make sure owner's own grants are output before others */ + if (owner && + strcmp(grantee->data, owner) == 0 && + strcmp(grantor->data, owner) == 0) + thissql = firstsql; + else + thissql = secondsql; + + if (grantor->len > 0 + && (!owner || strcmp(owner, grantor->data) != 0)) + appendPQExpBuffer(thissql, "SET SESSION AUTHORIZATION %s;\n", + fmtId(grantor->data)); + + if (privs->len > 0) + { + appendPQExpBuffer(thissql, "%sGRANT %s ON %s ", + prefix, privs->data, type); + if (nspname && *nspname) + appendPQExpBuffer(thissql, "%s.", fmtId(nspname)); + if (name && *name) + appendPQExpBuffer(thissql, "%s ", name); + appendPQExpBufferStr(thissql, "TO "); + if (grantee->len == 0) + appendPQExpBufferStr(thissql, "PUBLIC;\n"); + else + appendPQExpBuffer(thissql, "%s;\n", fmtId(grantee->data)); + } + if (privswgo->len > 0) + { + appendPQExpBuffer(thissql, "%sGRANT %s ON %s ", + prefix, privswgo->data, type); + if (nspname && *nspname) + appendPQExpBuffer(thissql, "%s.", fmtId(nspname)); + if (name && *name) + appendPQExpBuffer(thissql, "%s ", name); + appendPQExpBufferStr(thissql, "TO "); + if (grantee->len == 0) + appendPQExpBufferStr(thissql, "PUBLIC"); + else + appendPQExpBufferStr(thissql, fmtId(grantee->data)); + appendPQExpBufferStr(thissql, " WITH GRANT OPTION;\n"); + } + + if (grantor->len > 0 + && (!owner || strcmp(owner, grantor->data) != 0)) + appendPQExpBufferStr(thissql, "RESET SESSION AUTHORIZATION;\n"); + } + } + else + { + /* parseAclItem failed, give up */ + ok = false; + break; + } + } + + destroyPQExpBuffer(grantee); + destroyPQExpBuffer(grantor); + destroyPQExpBuffer(privs); + destroyPQExpBuffer(privswgo); + + appendPQExpBuffer(sql, "%s%s", firstsql->data, secondsql->data); + destroyPQExpBuffer(firstsql); + destroyPQExpBuffer(secondsql); + + free(aclitems); + free(baseitems); + free(grantitems); + free(revokeitems); + + return ok; +} + +/* + * Build ALTER DEFAULT PRIVILEGES command(s) for a single pg_default_acl entry. + * + * type: the object type (TABLES, FUNCTIONS, etc) + * nspname: schema name, or NULL for global default privileges + * acls: the ACL string fetched from the database + * acldefault: the appropriate default ACL for the object type and owner + * owner: username of privileges owner (will be passed through fmtId) + * remoteVersion: version of database + * + * Returns true if okay, false if could not parse the acl string. + * The resulting commands (if any) are appended to the contents of 'sql'. + */ +bool +buildDefaultACLCommands(const char *type, const char *nspname, + const char *acls, const char *acldefault, + const char *owner, + int remoteVersion, + PQExpBuffer sql) +{ + PQExpBuffer prefix; + + prefix = createPQExpBuffer(); + + /* + * We incorporate the target role directly into the command, rather than + * playing around with SET ROLE or anything like that. This is so that a + * permissions error leads to nothing happening, rather than changing + * default privileges for the wrong user. + */ + appendPQExpBuffer(prefix, "ALTER DEFAULT PRIVILEGES FOR ROLE %s ", + fmtId(owner)); + if (nspname) + appendPQExpBuffer(prefix, "IN SCHEMA %s ", fmtId(nspname)); + + /* + * There's no such thing as initprivs for a default ACL, so the base ACL + * is always just the object-type-specific default. + */ + if (!buildACLCommands("", NULL, NULL, type, + acls, acldefault, owner, + prefix->data, remoteVersion, sql)) + { + destroyPQExpBuffer(prefix); + return false; + } + + destroyPQExpBuffer(prefix); + + return true; +} + +/* + * This will parse an aclitem string, having the general form + * username=privilegecodes/grantor + * + * Returns true on success, false on parse error. On success, the components + * of the string are returned in the PQExpBuffer parameters. + * + * The returned grantee string will be the dequoted username, or an empty + * string in the case of a grant to PUBLIC. The returned grantor is the + * dequoted grantor name. Privilege characters are translated to GRANT/REVOKE + * comma-separated privileges lists. If "privswgo" is non-NULL, the result is + * separate lists for privileges with grant option ("privswgo") and without + * ("privs"). Otherwise, "privs" bears every relevant privilege, ignoring the + * grant option distinction. + * + * Note: for cross-version compatibility, it's important to use ALL to + * represent the privilege sets whenever appropriate. + */ +static bool +parseAclItem(const char *item, const char *type, + const char *name, const char *subname, int remoteVersion, + PQExpBuffer grantee, PQExpBuffer grantor, + PQExpBuffer privs, PQExpBuffer privswgo) +{ + char *buf; + bool all_with_go = true; + bool all_without_go = true; + char *eqpos; + char *slpos; + char *pos; + + buf = pg_strdup(item); + + /* user or group name is string up to = */ + eqpos = dequoteAclUserName(grantee, buf); + if (*eqpos != '=') + { + pg_free(buf); + return false; + } + + /* grantor should appear after / */ + slpos = strchr(eqpos + 1, '/'); + if (slpos) + { + *slpos++ = '\0'; + slpos = dequoteAclUserName(grantor, slpos); + if (*slpos != '\0') + { + pg_free(buf); + return false; + } + } + else + { + pg_free(buf); + return false; + } + + /* privilege codes */ +#define CONVERT_PRIV(code, keywd) \ +do { \ + if ((pos = strchr(eqpos + 1, code))) \ + { \ + if (*(pos + 1) == '*' && privswgo != NULL) \ + { \ + AddAcl(privswgo, keywd, subname); \ + all_without_go = false; \ + } \ + else \ + { \ + AddAcl(privs, keywd, subname); \ + all_with_go = false; \ + } \ + } \ + else \ + all_with_go = all_without_go = false; \ +} while (0) + + resetPQExpBuffer(privs); + resetPQExpBuffer(privswgo); + + if (strcmp(type, "TABLE") == 0 || strcmp(type, "SEQUENCE") == 0 || + strcmp(type, "TABLES") == 0 || strcmp(type, "SEQUENCES") == 0) + { + CONVERT_PRIV('r', "SELECT"); + + if (strcmp(type, "SEQUENCE") == 0 || + strcmp(type, "SEQUENCES") == 0) + /* sequence only */ + CONVERT_PRIV('U', "USAGE"); + else + { + /* table only */ + CONVERT_PRIV('a', "INSERT"); + CONVERT_PRIV('x', "REFERENCES"); + /* rest are not applicable to columns */ + if (subname == NULL) + { + CONVERT_PRIV('d', "DELETE"); + CONVERT_PRIV('t', "TRIGGER"); + CONVERT_PRIV('D', "TRUNCATE"); + CONVERT_PRIV('m', "MAINTAIN"); + } + } + + /* UPDATE */ + CONVERT_PRIV('w', "UPDATE"); + } + else if (strcmp(type, "PROPERTY GRAPH") == 0 || + strcmp(type, "PROPERTY GRAPHS") == 0) + CONVERT_PRIV('r', "SELECT"); + else if (strcmp(type, "FUNCTION") == 0 || + strcmp(type, "FUNCTIONS") == 0) + CONVERT_PRIV('X', "EXECUTE"); + else if (strcmp(type, "PROCEDURE") == 0 || + strcmp(type, "PROCEDURES") == 0) + CONVERT_PRIV('X', "EXECUTE"); + else if (strcmp(type, "LANGUAGE") == 0) + CONVERT_PRIV('U', "USAGE"); + else if (strcmp(type, "SCHEMA") == 0 || + strcmp(type, "SCHEMAS") == 0) + { + CONVERT_PRIV('C', "CREATE"); + CONVERT_PRIV('U', "USAGE"); + } + else if (strcmp(type, "DATABASE") == 0) + { + CONVERT_PRIV('C', "CREATE"); + CONVERT_PRIV('c', "CONNECT"); + CONVERT_PRIV('T', "TEMPORARY"); + } + else if (strcmp(type, "TABLESPACE") == 0) + CONVERT_PRIV('C', "CREATE"); + else if (strcmp(type, "TYPE") == 0 || + strcmp(type, "TYPES") == 0) + CONVERT_PRIV('U', "USAGE"); + else if (strcmp(type, "FOREIGN DATA WRAPPER") == 0) + CONVERT_PRIV('U', "USAGE"); + else if (strcmp(type, "FOREIGN SERVER") == 0) + CONVERT_PRIV('U', "USAGE"); + else if (strcmp(type, "FOREIGN TABLE") == 0) + CONVERT_PRIV('r', "SELECT"); + else if (strcmp(type, "PARAMETER") == 0) + { + CONVERT_PRIV('s', "SET"); + CONVERT_PRIV('A', "ALTER SYSTEM"); + } + else if (strcmp(type, "LARGE OBJECT") == 0 || + strcmp(type, "LARGE OBJECTS") == 0) + { + CONVERT_PRIV('r', "SELECT"); + CONVERT_PRIV('w', "UPDATE"); + } + else + abort(); + +#undef CONVERT_PRIV + + if (all_with_go) + { + resetPQExpBuffer(privs); + printfPQExpBuffer(privswgo, "ALL"); + if (subname) + appendPQExpBuffer(privswgo, "(%s)", subname); + } + else if (all_without_go) + { + resetPQExpBuffer(privswgo); + printfPQExpBuffer(privs, "ALL"); + if (subname) + appendPQExpBuffer(privs, "(%s)", subname); + } + + pg_free(buf); + + return true; +} + +/* + * Transfer the role name at *input into the output buffer, adding + * quoting according to the same rules as putid() in backend's acl.c. + */ +void +quoteAclUserName(PQExpBuffer output, const char *input) +{ + const char *src; + bool safe = true; + + for (src = input; *src; src++) + { + /* This test had better match what putid() does */ + if (!isalnum((unsigned char) *src) && *src != '_') + { + safe = false; + break; + } + } + if (!safe) + appendPQExpBufferChar(output, '"'); + for (src = input; *src; src++) + { + /* A double quote character in a username is encoded as "" */ + if (*src == '"') + appendPQExpBufferChar(output, '"'); + appendPQExpBufferChar(output, *src); + } + if (!safe) + appendPQExpBufferChar(output, '"'); +} + +/* + * Transfer a user or group name starting at *input into the output buffer, + * dequoting if needed. Returns a pointer to just past the input name. + * The name is taken to end at an unquoted '=' or end of string. + * Note: unlike quoteAclUserName(), this first clears the output buffer. + */ +static char * +dequoteAclUserName(PQExpBuffer output, char *input) +{ + resetPQExpBuffer(output); + + while (*input && *input != '=') + { + /* + * If user name isn't quoted, then just add it to the output buffer + */ + if (*input != '"') + appendPQExpBufferChar(output, *input++); + else + { + /* Otherwise, it's a quoted username */ + input++; + /* Loop until we come across an unescaped quote */ + while (!(*input == '"' && *(input + 1) != '"')) + { + if (*input == '\0') + return input; /* really a syntax error... */ + + /* + * Quoting convention is to escape " as "". Keep this code in + * sync with putid() in backend's acl.c. + */ + if (*input == '"' && *(input + 1) == '"') + input++; + appendPQExpBufferChar(output, *input++); + } + input++; + } + } + return input; +} + +/* + * Append a privilege keyword to a keyword list, inserting comma if needed. + */ +static void +AddAcl(PQExpBuffer aclbuf, const char *keyword, const char *subname) +{ + if (aclbuf->len > 0) + appendPQExpBufferChar(aclbuf, ','); + appendPQExpBufferStr(aclbuf, keyword); + if (subname) + appendPQExpBuffer(aclbuf, "(%s)", subname); +} + + +/* + * buildShSecLabelQuery + * + * Build a query to retrieve security labels for a shared object. + * The object is identified by its OID plus the name of the catalog + * it can be found in (e.g., "pg_database" for database names). + * The query is appended to "sql". (We don't execute it here so as to + * keep this file free of assumptions about how to deal with SQL errors.) + */ +void +buildShSecLabelQuery(const char *catalog_name, Oid objectId, + PQExpBuffer sql) +{ + appendPQExpBuffer(sql, + "SELECT provider, label FROM pg_catalog.pg_shseclabel " + "WHERE classoid = 'pg_catalog.%s'::pg_catalog.regclass " + "AND objoid = '%u'", catalog_name, objectId); +} + +/* + * emitShSecLabels + * + * Construct SECURITY LABEL commands using the data retrieved by the query + * generated by buildShSecLabelQuery, and append them to "buffer". + * Here, the target object is identified by its type name (e.g. "DATABASE") + * and its name (not pre-quoted). + */ +void +emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, + const char *objtype, const char *objname) +{ + int i; + + for (i = 0; i < PQntuples(res); i++) + { + char *provider = PQgetvalue(res, i, 0); + char *label = PQgetvalue(res, i, 1); + + /* must use fmtId result before calling it again */ + appendPQExpBuffer(buffer, + "SECURITY LABEL FOR %s ON %s", + fmtId(provider), objtype); + appendPQExpBuffer(buffer, + " %s IS ", + fmtId(objname)); + appendStringLiteralConn(buffer, label, conn); + appendPQExpBufferStr(buffer, ";\n"); + } +} + + +/* + * Detect whether the given GUC variable is of GUC_LIST_QUOTE type. + * + * It'd be better if we could inquire this directly from the backend; but even + * if there were a function for that, it could only tell us about variables + * currently known to guc.c, so that it'd be unsafe for extensions to declare + * GUC_LIST_QUOTE variables anyway. Lacking a solution for that, it doesn't + * seem worth the work to do more than have this list, which must be kept in + * sync with the variables actually marked GUC_LIST_QUOTE in guc_parameters.dat. + */ +bool +variable_is_guc_list_quote(const char *name) +{ + if (pg_strcasecmp(name, "local_preload_libraries") == 0 || + pg_strcasecmp(name, "oauth_validator_libraries") == 0 || + pg_strcasecmp(name, "search_path") == 0 || + pg_strcasecmp(name, "session_preload_libraries") == 0 || + pg_strcasecmp(name, "shared_preload_libraries") == 0 || + pg_strcasecmp(name, "temp_tablespaces") == 0 || + pg_strcasecmp(name, "unix_socket_directories") == 0) + return true; + else + return false; +} + +/* + * SplitGUCList --- parse a string containing identifiers or file names + * + * This is used to split the value of a GUC_LIST_QUOTE GUC variable, without + * presuming whether the elements will be taken as identifiers or file names. + * See comparable code in src/backend/utils/adt/varlena.c. + * + * Inputs: + * rawstring: the input string; must be overwritable! On return, it's + * been modified to contain the separated identifiers. + * separator: the separator punctuation expected between identifiers + * (typically '.' or ','). Whitespace may also appear around + * identifiers. + * Outputs: + * namelist: receives a malloc'd, null-terminated array of pointers to + * identifiers within rawstring. Caller should free this + * even on error return. + * + * Returns true if okay, false if there is a syntax error in the string. + */ +bool +SplitGUCList(char *rawstring, char separator, + char ***namelist) +{ + char *nextp = rawstring; + bool done = false; + char **nextptr; + + /* + * Since we disallow empty identifiers, this is a conservative + * overestimate of the number of pointers we could need. Allow one for + * list terminator. + */ + *namelist = nextptr = + pg_malloc_array(char *, (strlen(rawstring) / 2 + 2)); + *nextptr = NULL; + + while (isspace((unsigned char) *nextp)) + nextp++; /* skip leading whitespace */ + + if (*nextp == '\0') + return true; /* empty string represents empty list */ + + /* At the top of the loop, we are at start of a new identifier. */ + do + { + char *curname; + char *endp; + + if (*nextp == '"') + { + /* Quoted name --- collapse quote-quote pairs */ + curname = nextp + 1; + for (;;) + { + endp = strchr(nextp + 1, '"'); + if (endp == NULL) + return false; /* mismatched quotes */ + if (endp[1] != '"') + break; /* found end of quoted name */ + /* Collapse adjacent quotes into one quote, and look again */ + memmove(endp, endp + 1, strlen(endp)); + nextp = endp; + } + /* endp now points at the terminating quote */ + nextp = endp + 1; + } + else + { + /* Unquoted name --- extends to separator or whitespace */ + curname = nextp; + while (*nextp && *nextp != separator && + !isspace((unsigned char) *nextp)) + nextp++; + endp = nextp; + if (curname == nextp) + return false; /* empty unquoted name not allowed */ + } + + while (isspace((unsigned char) *nextp)) + nextp++; /* skip trailing whitespace */ + + if (*nextp == separator) + { + nextp++; + while (isspace((unsigned char) *nextp)) + nextp++; /* skip leading whitespace for next */ + /* we expect another name, so done remains false */ + } + else if (*nextp == '\0') + done = true; + else + return false; /* invalid syntax */ + + /* Now safe to overwrite separator with a null */ + *endp = '\0'; + + /* + * Finished isolating current name --- add it to output array + */ + *nextptr++ = curname; + + /* Loop back if we didn't reach end of string */ + } while (!done); + + *nextptr = NULL; + return true; +} + +/* + * Helper function for dumping "ALTER DATABASE/ROLE SET ..." commands. + * + * Parse the contents of configitem (a "name=value" string), wrap it in + * a complete ALTER command, and append it to buf. + * + * type is DATABASE or ROLE, and name is the name of the database or role. + * If we need an "IN" clause, type2 and name2 similarly define what to put + * there; otherwise they should be NULL. + * conn is used only to determine string-literal quoting conventions. + */ +void +makeAlterConfigCommand(PGconn *conn, const char *configitem, + const char *type, const char *name, + const char *type2, const char *name2, + PQExpBuffer buf) +{ + char *mine; + char *pos; + + /* Parse the configitem. If we can't find an "=", silently do nothing. */ + mine = pg_strdup(configitem); + pos = strchr(mine, '='); + if (pos == NULL) + { + pg_free(mine); + return; + } + *pos++ = '\0'; + + /* Build the command, with suitable quoting for everything. */ + appendPQExpBuffer(buf, "ALTER %s %s ", type, fmtId(name)); + if (type2 != NULL && name2 != NULL) + appendPQExpBuffer(buf, "IN %s %s ", type2, fmtId(name2)); + appendPQExpBuffer(buf, "SET %s TO ", fmtId(mine)); + + /* + * Variables that are marked GUC_LIST_QUOTE were already fully quoted by + * flatten_set_variable_args() before they were put into the setconfig + * array. However, because the quoting rules used there aren't exactly + * like SQL's, we have to break the list value apart and then quote the + * elements as string literals. (The elements may be double-quoted as-is, + * but we can't just feed them to the SQL parser; it would do the wrong + * thing with elements that are zero-length or longer than NAMEDATALEN.) + * Also, we need a special case for empty lists. + * + * Variables that are not so marked should just be emitted as simple + * string literals. If the variable is not known to + * variable_is_guc_list_quote(), we'll do that; this makes it unsafe to + * use GUC_LIST_QUOTE for extension variables. + */ + if (variable_is_guc_list_quote(mine)) + { + char **namelist; + char **nameptr; + + /* Parse string into list of identifiers */ + /* this shouldn't fail really */ + if (SplitGUCList(pos, ',', &namelist)) + { + /* Special case: represent an empty list as NULL */ + if (*namelist == NULL) + appendPQExpBufferStr(buf, "NULL"); + for (nameptr = namelist; *nameptr; nameptr++) + { + if (nameptr != namelist) + appendPQExpBufferStr(buf, ", "); + appendStringLiteralConn(buf, *nameptr, conn); + } + } + pg_free(namelist); + } + else + appendStringLiteralConn(buf, pos, conn); + + appendPQExpBufferStr(buf, ";\n"); + + pg_free(mine); +} + +/* + * create_or_open_dir + * + * This will create a new directory with the given dirname. If there is + * already an empty directory with that name, then use it. + */ +void +create_or_open_dir(const char *dirname) +{ + int ret; + + switch ((ret = pg_check_dir(dirname))) + { + case -1: + /* opendir failed but not with ENOENT */ + pg_fatal("could not open directory \"%s\": %m", dirname); + break; + case 0: + /* directory does not exist */ + if (mkdir(dirname, pg_dir_create_mode) < 0) + pg_fatal("could not create directory \"%s\": %m", dirname); + break; + case 1: + /* exists and is empty, fix perms */ + if (chmod(dirname, pg_dir_create_mode) != 0) + pg_fatal("could not change permissions of directory \"%s\": %m", + dirname); + break; + default: + /* exists and is not empty */ + pg_fatal("directory \"%s\" is not empty", dirname); + } +} + +/* + * Generates a valid restrict key (i.e., an alphanumeric string) for use with + * psql's \restrict and \unrestrict meta-commands. For safety, the value is + * chosen at random. + */ +char * +generate_restrict_key(void) +{ + uint8 buf[64]; + char *ret = palloc(sizeof(buf)); + + if (!pg_strong_random(buf, sizeof(buf))) + return NULL; + + for (int i = 0; i < sizeof(buf) - 1; i++) + { + uint8 idx = buf[i] % strlen(restrict_chars); + + ret[i] = restrict_chars[idx]; + } + ret[sizeof(buf) - 1] = '\0'; + + return ret; +} + +/* + * Checks that a given restrict key (intended for use with psql's \restrict and + * \unrestrict meta-commands) contains only alphanumeric characters. + */ +bool +valid_restrict_key(const char *restrict_key) +{ + return restrict_key != NULL && + restrict_key[0] != '\0' && + strspn(restrict_key, restrict_chars) == strlen(restrict_key); +} diff --git a/src/compat/19/pg_dump/dumputils.h b/src/compat/19/pg_dump/dumputils.h new file mode 100644 index 00000000..d231ce1d --- /dev/null +++ b/src/compat/19/pg_dump/dumputils.h @@ -0,0 +1,72 @@ +/*------------------------------------------------------------------------- + * + * Utility routines for SQL dumping + * + * Basically this is stuff that is useful in both pg_dump and pg_dumpall. + * + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/bin/pg_dump/dumputils.h + * + *------------------------------------------------------------------------- + */ +#ifndef DUMPUTILS_H +#define DUMPUTILS_H + +#include "libpq-fe.h" +#include "pqexpbuffer.h" + +/* + * Preferred strftime(3) format specifier for printing timestamps in pg_dump + * and friends. + * + * We don't print the timezone on Windows, because the names are long and + * localized, which means they may contain characters in various random + * encodings; this has been seen to cause encoding errors when reading the + * dump script. One could now possibly get around that by using %z, but %z + * was previously not portable to pre-C99 systems, and also previously %z + * didn't actually act differently from %Z on Windows. But of these problems + * might be obsolete now. + */ +#ifndef WIN32 +#define PGDUMP_STRFTIME_FMT "%Y-%m-%d %H:%M:%S %Z" +#else +#define PGDUMP_STRFTIME_FMT "%Y-%m-%d %H:%M:%S" +#endif + + +extern char *sanitize_line(const char *str, bool want_hyphen); +extern bool buildACLCommands(const char *name, const char *subname, const char *nspname, + const char *type, const char *acls, const char *baseacls, + const char *owner, const char *prefix, int remoteVersion, + PQExpBuffer sql); +extern bool buildDefaultACLCommands(const char *type, const char *nspname, + const char *acls, const char *acldefault, + const char *owner, + int remoteVersion, + PQExpBuffer sql); + +extern void quoteAclUserName(PQExpBuffer output, const char *input); + +extern void buildShSecLabelQuery(const char *catalog_name, + Oid objectId, PQExpBuffer sql); +extern void emitShSecLabels(PGconn *conn, PGresult *res, + PQExpBuffer buffer, const char *objtype, const char *objname); + +extern bool variable_is_guc_list_quote(const char *name); + +extern bool SplitGUCList(char *rawstring, char separator, + char ***namelist); + +extern void makeAlterConfigCommand(PGconn *conn, const char *configitem, + const char *type, const char *name, + const char *type2, const char *name2, + PQExpBuffer buf); +extern void create_or_open_dir(const char *dirname); + +extern char *generate_restrict_key(void); +extern bool valid_restrict_key(const char *restrict_key); + +#endif /* DUMPUTILS_H */ diff --git a/src/compat/19/pg_dump/filter.c b/src/compat/19/pg_dump/filter.c new file mode 100644 index 00000000..fbff69d4 --- /dev/null +++ b/src/compat/19/pg_dump/filter.c @@ -0,0 +1,475 @@ +/*------------------------------------------------------------------------- + * + * filter.c + * Implementation of simple filter file parser + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/filter.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include "common/logging.h" +#include "common/string.h" +#include "filter.h" +#include "lib/stringinfo.h" +#include "pqexpbuffer.h" + +#define is_keyword_str(cstr, str, bytes) \ + ((strlen(cstr) == (bytes)) && (pg_strncasecmp((cstr), (str), (bytes)) == 0)) + +/* + * Following routines are called from pg_dump, pg_dumpall and pg_restore. + * Since the implementation of exit_nicely is application specific, each + * application need to pass a function pointer to the exit_nicely function to + * use for exiting on errors. + */ + +/* + * Opens filter's file and initialize fstate structure. + */ +void +filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit) +{ + fstate->filename = filename; + fstate->lineno = 0; + fstate->exit_nicely = f_exit; + initStringInfo(&fstate->linebuff); + + if (strcmp(filename, "-") != 0) + { + fstate->fp = fopen(filename, "r"); + if (!fstate->fp) + { + pg_log_error("could not open filter file \"%s\": %m", filename); + fstate->exit_nicely(1); + } + } + else + fstate->fp = stdin; +} + +/* + * Release allocated resources for the given filter. + */ +void +filter_free(FilterStateData *fstate) +{ + if (!fstate) + return; + + free(fstate->linebuff.data); + fstate->linebuff.data = NULL; + + if (fstate->fp && fstate->fp != stdin) + { + if (fclose(fstate->fp) != 0) + pg_log_error("could not close filter file \"%s\": %m", fstate->filename); + + fstate->fp = NULL; + } +} + +/* + * Translate FilterObjectType enum to string. The main purpose is for error + * message formatting. + */ +const char * +filter_object_type_name(FilterObjectType fot) +{ + switch (fot) + { + case FILTER_OBJECT_TYPE_NONE: + return "comment or empty line"; + case FILTER_OBJECT_TYPE_TABLE_DATA: + return "table data"; + case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN: + return "table data and children"; + case FILTER_OBJECT_TYPE_DATABASE: + return "database"; + case FILTER_OBJECT_TYPE_EXTENSION: + return "extension"; + case FILTER_OBJECT_TYPE_FOREIGN_DATA: + return "foreign data"; + case FILTER_OBJECT_TYPE_FUNCTION: + return "function"; + case FILTER_OBJECT_TYPE_INDEX: + return "index"; + case FILTER_OBJECT_TYPE_SCHEMA: + return "schema"; + case FILTER_OBJECT_TYPE_TABLE: + return "table"; + case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN: + return "table and children"; + case FILTER_OBJECT_TYPE_TRIGGER: + return "trigger"; + } + + /* should never get here */ + pg_unreachable(); +} + +/* + * Returns true when keyword is one of supported object types, and + * set related objtype. Returns false, when keyword is not assigned + * with known object type. + */ +static bool +get_object_type(const char *keyword, int size, FilterObjectType *objtype) +{ + if (is_keyword_str("table_data", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_TABLE_DATA; + else if (is_keyword_str("table_data_and_children", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN; + else if (is_keyword_str("database", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_DATABASE; + else if (is_keyword_str("extension", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_EXTENSION; + else if (is_keyword_str("foreign_data", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_FOREIGN_DATA; + else if (is_keyword_str("function", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_FUNCTION; + else if (is_keyword_str("index", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_INDEX; + else if (is_keyword_str("schema", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_SCHEMA; + else if (is_keyword_str("table", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_TABLE; + else if (is_keyword_str("table_and_children", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN; + else if (is_keyword_str("trigger", keyword, size)) + *objtype = FILTER_OBJECT_TYPE_TRIGGER; + else + return false; + + return true; +} + + +void +pg_log_filter_error(FilterStateData *fstate, const char *fmt,...) +{ + va_list argp; + char buf[256]; + + va_start(argp, fmt); + vsnprintf(buf, sizeof(buf), fmt, argp); + va_end(argp); + + if (fstate->fp == stdin) + pg_log_error("invalid format in filter read from standard input on line %d: %s", + fstate->lineno, buf); + else + pg_log_error("invalid format in filter read from file \"%s\" on line %d: %s", + fstate->filename, fstate->lineno, buf); +} + +/* + * filter_get_keyword - read the next filter keyword from buffer + * + * Search for keywords (strings of non-whitespace characters) in the passed + * in line buffer. Returns NULL when the buffer is empty or no keyword exists. + * The length of the found keyword is returned in the size parameter. + */ +static const char * +filter_get_keyword(const char **line, int *size) +{ + const char *ptr = *line; + const char *result = NULL; + + /* The passed buffer must not be NULL */ + Assert(*line != NULL); + + /* Set returned length preemptively in case no keyword is found */ + *size = 0; + + /* Skip initial whitespace */ + while (isspace((unsigned char) *ptr)) + ptr++; + + /* Grab one keyword that's the string of non-whitespace characters */ + if (*ptr != '\0' && !isspace((unsigned char) *ptr)) + { + result = ptr++; + + while (*ptr != '\0' && !isspace((unsigned char) *ptr)) + ptr++; + + *size = ptr - result; + } + + *line = ptr; + + return result; +} + +/* + * read_quoted_string - read quoted possibly multi line string + * + * Reads a quoted string which can span over multiple lines and returns a + * pointer to next char after ending double quotes; it will exit on errors. + */ +static const char * +read_quoted_string(FilterStateData *fstate, + const char *str, + PQExpBuffer pattern) +{ + appendPQExpBufferChar(pattern, '"'); + str++; + + while (1) + { + /* + * We can ignore \r or \n chars because the string is read by + * pg_get_line_buf, so these chars should be just trailing chars. + */ + if (*str == '\r' || *str == '\n') + { + str++; + continue; + } + + if (*str == '\0') + { + Assert(fstate->linebuff.data); + + if (!pg_get_line_buf(fstate->fp, &fstate->linebuff)) + { + if (ferror(fstate->fp)) + pg_log_error("could not read from filter file \"%s\": %m", + fstate->filename); + else + pg_log_filter_error(fstate, _("unexpected end of file")); + + fstate->exit_nicely(1); + } + + str = fstate->linebuff.data; + + appendPQExpBufferChar(pattern, '\n'); + fstate->lineno++; + } + + if (*str == '"') + { + appendPQExpBufferChar(pattern, '"'); + str++; + + if (*str == '"') + { + appendPQExpBufferChar(pattern, '"'); + str++; + } + else + break; + } + else if (*str == '\\') + { + str++; + if (*str == 'n') + appendPQExpBufferChar(pattern, '\n'); + else if (*str == '\\') + appendPQExpBufferChar(pattern, '\\'); + + str++; + } + else + appendPQExpBufferChar(pattern, *str++); + } + + return str; +} + +/* + * read_pattern - reads on object pattern from input + * + * This function will parse any valid identifier (quoted or not, qualified or + * not), which can also includes the full signature for routines. + * Note that this function takes special care to sanitize the detected + * identifier (removing extraneous whitespaces or other unnecessary + * characters). This is necessary as most backup/restore filtering functions + * only recognize identifiers if they are written exactly the same way as + * they are output by the server. + * + * Returns a pointer to next character after the found identifier and exits + * on error. + */ +static const char * +read_pattern(FilterStateData *fstate, const char *str, PQExpBuffer pattern) +{ + bool skip_space = true; + bool found_space = false; + + /* Skip initial whitespace */ + while (isspace((unsigned char) *str)) + str++; + + if (*str == '\0') + { + pg_log_filter_error(fstate, _("missing object name pattern")); + fstate->exit_nicely(1); + } + + while (*str && *str != '#') + { + while (*str && !isspace((unsigned char) *str) && !strchr("#,.()\"", *str)) + { + /* + * Append space only when it is allowed, and when it was found in + * original string. + */ + if (!skip_space && found_space) + { + appendPQExpBufferChar(pattern, ' '); + skip_space = true; + } + + appendPQExpBufferChar(pattern, *str++); + } + + skip_space = false; + + if (*str == '"') + { + if (found_space) + appendPQExpBufferChar(pattern, ' '); + + str = read_quoted_string(fstate, str, pattern); + } + else if (*str == ',') + { + appendPQExpBufferStr(pattern, ", "); + skip_space = true; + str++; + } + else if (*str && strchr(".()", *str)) + { + appendPQExpBufferChar(pattern, *str++); + skip_space = true; + } + + found_space = false; + + /* skip ending whitespaces */ + while (isspace((unsigned char) *str)) + { + found_space = true; + str++; + } + } + + return str; +} + +/* + * filter_read_item - Read command/type/pattern triplet from a filter file + * + * This will parse one filter item from the filter file, and while it is a + * row based format a pattern may span more than one line due to how object + * names can be constructed. The expected format of the filter file is: + * + * + * + * command can be "include" or "exclude". + * + * Supported object types are described by enum FilterObjectType + * (see function get_object_type). + * + * pattern can be any possibly-quoted and possibly-qualified identifier. It + * follows the same rules as other object include and exclude functions so it + * can also use wildcards. + * + * Returns true when one filter item was successfully read and parsed. When + * object name contains \n chars, then more than one line from input file can + * be processed. Returns false when the filter file reaches EOF. In case of + * error, the function will emit an appropriate error message and exit. + */ +bool +filter_read_item(FilterStateData *fstate, + char **objname, + FilterCommandType *comtype, + FilterObjectType *objtype) +{ + if (pg_get_line_buf(fstate->fp, &fstate->linebuff)) + { + const char *str = fstate->linebuff.data; + const char *keyword; + int size; + PQExpBufferData pattern; + + fstate->lineno++; + + /* Skip initial white spaces */ + while (isspace((unsigned char) *str)) + str++; + + /* + * Skip empty lines or lines where the first non-whitespace character + * is a hash indicating a comment. + */ + if (*str != '\0' && *str != '#') + { + /* + * First we expect sequence of two keywords, {include|exclude} + * followed by the object type to operate on. + */ + keyword = filter_get_keyword(&str, &size); + if (!keyword) + { + pg_log_filter_error(fstate, + _("no filter command found (expected \"include\" or \"exclude\")")); + fstate->exit_nicely(1); + } + + if (is_keyword_str("include", keyword, size)) + *comtype = FILTER_COMMAND_TYPE_INCLUDE; + else if (is_keyword_str("exclude", keyword, size)) + *comtype = FILTER_COMMAND_TYPE_EXCLUDE; + else + { + pg_log_filter_error(fstate, + _("invalid filter command (expected \"include\" or \"exclude\")")); + fstate->exit_nicely(1); + } + + keyword = filter_get_keyword(&str, &size); + if (!keyword) + { + pg_log_filter_error(fstate, _("missing filter object type")); + fstate->exit_nicely(1); + } + + if (!get_object_type(keyword, size, objtype)) + { + pg_log_filter_error(fstate, + _("unsupported filter object type: \"%.*s\""), size, keyword); + fstate->exit_nicely(1); + } + + initPQExpBuffer(&pattern); + + str = read_pattern(fstate, str, &pattern); + *objname = pattern.data; + } + else + { + *objname = NULL; + *comtype = FILTER_COMMAND_TYPE_NONE; + *objtype = FILTER_OBJECT_TYPE_NONE; + } + + return true; + } + + if (ferror(fstate->fp)) + { + pg_log_error("could not read from filter file \"%s\": %m", fstate->filename); + fstate->exit_nicely(1); + } + + return false; +} diff --git a/src/compat/19/pg_dump/filter.h b/src/compat/19/pg_dump/filter.h new file mode 100644 index 00000000..b87cd6de --- /dev/null +++ b/src/compat/19/pg_dump/filter.h @@ -0,0 +1,71 @@ +/*------------------------------------------------------------------------- + * + * filter.h + * Common header file for the parser of filter file + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/filter.h + * + *------------------------------------------------------------------------- + */ +#ifndef FILTER_H +#define FILTER_H + +#include "lib/stringinfo.h" + +/* Function signature for exit_nicely functions */ +typedef void (*exit_function) (int status); + +/* + * State data for reading filter items from stream + */ +typedef struct +{ + FILE *fp; + const char *filename; + exit_function exit_nicely; + int lineno; + StringInfoData linebuff; +} FilterStateData; + +/* + * List of command types that can be specified in filter file + */ +typedef enum +{ + FILTER_COMMAND_TYPE_NONE, + FILTER_COMMAND_TYPE_INCLUDE, + FILTER_COMMAND_TYPE_EXCLUDE, +} FilterCommandType; + +/* + * List of objects that can be specified in filter file + */ +typedef enum +{ + FILTER_OBJECT_TYPE_NONE, + FILTER_OBJECT_TYPE_TABLE_DATA, + FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN, + FILTER_OBJECT_TYPE_DATABASE, + FILTER_OBJECT_TYPE_EXTENSION, + FILTER_OBJECT_TYPE_FOREIGN_DATA, + FILTER_OBJECT_TYPE_FUNCTION, + FILTER_OBJECT_TYPE_INDEX, + FILTER_OBJECT_TYPE_SCHEMA, + FILTER_OBJECT_TYPE_TABLE, + FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN, + FILTER_OBJECT_TYPE_TRIGGER, +} FilterObjectType; + +extern const char *filter_object_type_name(FilterObjectType fot); +extern void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit); +extern void filter_free(FilterStateData *fstate); +extern void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...) + pg_attribute_printf(2, 3); +extern bool filter_read_item(FilterStateData *fstate, char **objname, + FilterCommandType *comtype, FilterObjectType *objtype); + +#endif /* FILTER_H */ diff --git a/src/compat/19/pg_dump/parallel.c b/src/compat/19/pg_dump/parallel.c new file mode 100644 index 00000000..a7bed5ec --- /dev/null +++ b/src/compat/19/pg_dump/parallel.c @@ -0,0 +1,1817 @@ +/*------------------------------------------------------------------------- + * + * parallel.c + * + * Parallel support for pg_dump and pg_restore + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/parallel.c + * + *------------------------------------------------------------------------- + */ + +/* + * Parallel operation works like this: + * + * The original, leader process calls ParallelBackupStart(), which forks off + * the desired number of worker processes, which each enter WaitForCommands(). + * + * The leader process dispatches an individual work item to one of the worker + * processes in DispatchJobForTocEntry(). We send a command string such as + * "DUMP 1234" or "RESTORE 1234", where 1234 is the TocEntry ID. + * The worker process receives and decodes the command and passes it to the + * routine pointed to by AH->WorkerJobDumpPtr or AH->WorkerJobRestorePtr, + * which are routines of the current archive format. That routine performs + * the required action (dump or restore) and returns an integer status code. + * This is passed back to the leader where we pass it to the + * ParallelCompletionPtr callback function that was passed to + * DispatchJobForTocEntry(). The callback function does state updating + * for the leader control logic in pg_backup_archiver.c. + * + * In principle additional archive-format-specific information might be needed + * in commands or worker status responses, but so far that hasn't proved + * necessary, since workers have full copies of the ArchiveHandle/TocEntry + * data structures. Remember that we have forked off the workers only after + * we have read in the catalog. That's why our worker processes can also + * access the catalog information. (In the Windows case, the workers are + * threads in the same process. To avoid problems, they work with cloned + * copies of the Archive data structure; see RunWorker().) + * + * In the leader process, the workerStatus field for each worker has one of + * the following values: + * WRKR_NOT_STARTED: we've not yet forked this worker + * WRKR_IDLE: it's waiting for a command + * WRKR_WORKING: it's working on a command + * WRKR_TERMINATED: process ended + * The pstate->te[] entry for each worker is valid when it's in WRKR_WORKING + * state, and must be NULL in other states. + */ + +#include "postgres_fe.h" + +#ifndef WIN32 +#include +#include +#include +#include +#include +#endif + +#include "fe_utils/string_utils.h" +#include "parallel.h" +#include "pg_backup_utils.h" +#ifdef WIN32 +#include "port/pg_bswap.h" +#endif + +/* Mnemonic macros for indexing the fd array returned by pipe(2) */ +#define PIPE_READ 0 +#define PIPE_WRITE 1 + +#define NO_SLOT (-1) /* Failure result for GetIdleWorker() */ + +/* Worker process statuses */ +typedef enum +{ + WRKR_NOT_STARTED = 0, + WRKR_IDLE, + WRKR_WORKING, + WRKR_TERMINATED, +} T_WorkerStatus; + +#define WORKER_IS_RUNNING(workerStatus) \ + ((workerStatus) == WRKR_IDLE || (workerStatus) == WRKR_WORKING) + +/* + * Private per-parallel-worker state (typedef for this is in parallel.h). + * + * Much of this is valid only in the leader process (or, on Windows, should + * be touched only by the leader thread). But the AH field should be touched + * only by workers. The pipe descriptors are valid everywhere. + */ +struct ParallelSlot +{ + T_WorkerStatus workerStatus; /* see enum above */ + + /* These fields are valid if workerStatus == WRKR_WORKING: */ + ParallelCompletionPtr callback; /* function to call on completion */ + void *callback_data; /* passthrough data for it */ + + ArchiveHandle *AH; /* Archive data worker is using */ + + int pipeRead; /* leader's end of the pipes */ + int pipeWrite; + int pipeRevRead; /* child's end of the pipes */ + int pipeRevWrite; + + /* Child process/thread identity info: */ +#ifdef WIN32 + uintptr_t hThread; + unsigned int threadId; +#else + pid_t pid; +#endif +}; + +#ifdef WIN32 + +/* + * Structure to hold info passed by _beginthreadex() to the function it calls + * via its single allowed argument. + */ +typedef struct +{ + ArchiveHandle *AH; /* leader database connection */ + ParallelSlot *slot; /* this worker's parallel slot */ +} WorkerInfo; + +/* Windows implementation of pipe access */ +static int pgpipe(int handles[2]); +#define piperead(a,b,c) recv(a,b,c,0) +#define pipewrite(a,b,c) send(a,b,c,0) + +#else /* !WIN32 */ + +/* Non-Windows implementation of pipe access */ +#define pgpipe(a) pipe(a) +#define piperead(a,b,c) read(a,b,c) +#define pipewrite(a,b,c) write(a,b,c) + +#endif /* WIN32 */ + +/* + * State info for archive_close_connection() shutdown callback. + */ +typedef struct ShutdownInformation +{ + ParallelState *pstate; + Archive *AHX; +} ShutdownInformation; + +static ShutdownInformation shutdown_info; + +/* + * State info for signal handling. + * We assume signal_info initializes to zeroes. + * + * On Unix, myAH is the leader DB connection in the leader process, and the + * worker's own connection in worker processes. On Windows, we have only one + * instance of signal_info, so myAH is the leader connection and the worker + * connections must be dug out of pstate->parallelSlot[]. + */ +typedef struct DumpSignalInformation +{ + ArchiveHandle *myAH; /* database connection to issue cancel for */ + ParallelState *pstate; /* parallel state, if any */ + bool handler_set; /* signal handler set up in this process? */ +#ifndef WIN32 + bool am_worker; /* am I a worker process? */ +#endif +} DumpSignalInformation; + +static volatile DumpSignalInformation signal_info; + +#ifdef WIN32 +static CRITICAL_SECTION signal_info_lock; +#endif + +/* + * Write a simple string to stderr --- must be safe in a signal handler. + * We ignore the write() result since there's not much we could do about it. + * Certain compilers make that harder than it ought to be. + */ +#define write_stderr(str) \ + do { \ + const char *str_ = (str); \ + int rc_; \ + rc_ = write(fileno(stderr), str_, strlen(str_)); \ + (void) rc_; \ + } while (0) + + +#ifdef WIN32 +/* file-scope variables */ +static DWORD tls_index; + +/* globally visible variables (needed by exit_nicely) */ +bool parallel_init_done = false; +DWORD mainThreadId; +#endif /* WIN32 */ + +/* Local function prototypes */ +static ParallelSlot *GetMyPSlot(ParallelState *pstate); +static void archive_close_connection(int code, void *arg); +static void ShutdownWorkersHard(ParallelState *pstate); +static void WaitForTerminatingWorkers(ParallelState *pstate); +static void set_cancel_handler(void); +static void set_cancel_pstate(ParallelState *pstate); +static void set_cancel_slot_archive(ParallelSlot *slot, ArchiveHandle *AH); +static void RunWorker(ArchiveHandle *AH, ParallelSlot *slot); +static int GetIdleWorker(ParallelState *pstate); +static bool HasEveryWorkerTerminated(ParallelState *pstate); +static void lockTableForWorker(ArchiveHandle *AH, TocEntry *te); +static void WaitForCommands(ArchiveHandle *AH, int pipefd[2]); +static bool ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, + bool do_wait); +static char *getMessageFromLeader(int pipefd[2]); +static void sendMessageToLeader(int pipefd[2], const char *str); +static int select_loop(int maxFd, fd_set *workerset); +static char *getMessageFromWorker(ParallelState *pstate, + bool do_wait, int *worker); +static void sendMessageToWorker(ParallelState *pstate, + int worker, const char *str); +static char *readMessageFromPipe(int fd); + +#define messageStartsWith(msg, prefix) \ + (strncmp(msg, prefix, strlen(prefix)) == 0) + + +/* + * Initialize parallel dump support --- should be called early in process + * startup. (Currently, this is called whether or not we intend parallel + * activity.) + */ +void +init_parallel_dump_utils(void) +{ +#ifdef WIN32 + if (!parallel_init_done) + { + WSADATA wsaData; + int err; + + /* Prepare for threaded operation */ + tls_index = TlsAlloc(); + mainThreadId = GetCurrentThreadId(); + + /* Initialize socket access */ + err = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (err != 0) + pg_fatal("%s() failed: error code %d", "WSAStartup", err); + + parallel_init_done = true; + } +#endif +} + +/* + * Find the ParallelSlot for the current worker process or thread. + * + * Returns NULL if no matching slot is found (this implies we're the leader). + */ +static ParallelSlot * +GetMyPSlot(ParallelState *pstate) +{ + int i; + + for (i = 0; i < pstate->numWorkers; i++) + { +#ifdef WIN32 + if (pstate->parallelSlot[i].threadId == GetCurrentThreadId()) +#else + if (pstate->parallelSlot[i].pid == getpid()) +#endif + return &(pstate->parallelSlot[i]); + } + + return NULL; +} + +/* + * A thread-local version of getLocalPQExpBuffer(). + * + * Non-reentrant but reduces memory leakage: we'll consume one buffer per + * thread, which is much better than one per fmtId/fmtQualifiedId call. + */ +#ifdef WIN32 +static PQExpBuffer +getThreadLocalPQExpBuffer(void) +{ + /* + * The Tls code goes awry if we use a static var, so we provide for both + * static and auto, and omit any use of the static var when using Tls. We + * rely on TlsGetValue() to return 0 if the value is not yet set. + */ + static PQExpBuffer s_id_return = NULL; + PQExpBuffer id_return; + + if (parallel_init_done) + id_return = (PQExpBuffer) TlsGetValue(tls_index); + else + id_return = s_id_return; + + if (id_return) /* first time through? */ + { + /* same buffer, just wipe contents */ + resetPQExpBuffer(id_return); + } + else + { + /* new buffer */ + id_return = createPQExpBuffer(); + if (parallel_init_done) + TlsSetValue(tls_index, id_return); + else + s_id_return = id_return; + } + + return id_return; +} +#endif /* WIN32 */ + +/* + * pg_dump and pg_restore call this to register the cleanup handler + * as soon as they've created the ArchiveHandle. + */ +void +on_exit_close_archive(Archive *AHX) +{ + shutdown_info.AHX = AHX; + on_exit_nicely(archive_close_connection, &shutdown_info); +} + +/* + * Update the archive handle in the on_exit callback registered by + * on_exit_close_archive(). When pg_restore processes a pg_dumpall archive + * containing multiple databases, each database is restored from a separate + * archive. After closing one archive and opening the next, we update the + * shutdown_info to reference the new archive handle so the cleanup callback + * will close the correct archive on exit. + */ +void +replace_on_exit_close_archive(Archive *AHX) +{ + shutdown_info.AHX = AHX; +} + +/* + * on_exit_nicely handler for shutting down database connections and + * worker processes cleanly. + */ +static void +archive_close_connection(int code, void *arg) +{ + ShutdownInformation *si = (ShutdownInformation *) arg; + + if (si->pstate) + { + /* In parallel mode, must figure out who we are */ + ParallelSlot *slot = GetMyPSlot(si->pstate); + + if (!slot) + { + /* + * We're the leader. Forcibly shut down workers, then close our + * own database connection, if any. + */ + ShutdownWorkersHard(si->pstate); + + if (si->AHX) + DisconnectDatabase(si->AHX); + } + else + { + /* + * We're a worker. Shut down our own DB connection if any. On + * Windows, we also have to close our communication sockets, to + * emulate what will happen on Unix when the worker process exits. + * (Without this, if this is a premature exit, the leader would + * fail to detect it because there would be no EOF condition on + * the other end of the pipe.) + */ + if (slot->AH) + DisconnectDatabase(&(slot->AH->public)); + +#ifdef WIN32 + closesocket(slot->pipeRevRead); + closesocket(slot->pipeRevWrite); +#endif + } + } + else + { + /* Non-parallel operation: just kill the leader DB connection */ + if (si->AHX) + DisconnectDatabase(si->AHX); + } +} + +/* + * Forcibly shut down any remaining workers, waiting for them to finish. + * + * Note that we don't expect to come here during normal exit (the workers + * should be long gone, and the ParallelState too). We're only here in a + * pg_fatal() situation, so intervening to cancel active commands is + * appropriate. + */ +static void +ShutdownWorkersHard(ParallelState *pstate) +{ + int i; + + /* + * Close our write end of the sockets so that any workers waiting for + * commands know they can exit. (Note: some of the pipeWrite fields might + * still be zero, if we failed to initialize all the workers. Hence, just + * ignore errors here.) + */ + for (i = 0; i < pstate->numWorkers; i++) + closesocket(pstate->parallelSlot[i].pipeWrite); + + /* + * Force early termination of any commands currently in progress. + */ +#ifndef WIN32 + /* On non-Windows, send SIGTERM to each worker process. */ + for (i = 0; i < pstate->numWorkers; i++) + { + pid_t pid = pstate->parallelSlot[i].pid; + + if (pid != 0) + kill(pid, SIGTERM); + } +#else + + /* + * On Windows, send query cancels directly to the workers' backends. Use + * a critical section to ensure worker threads don't change state. + */ + EnterCriticalSection(&signal_info_lock); + for (i = 0; i < pstate->numWorkers; i++) + { + ArchiveHandle *AH = pstate->parallelSlot[i].AH; + char errbuf[1]; + + if (AH != NULL && AH->connCancel != NULL) + (void) PQcancel(AH->connCancel, errbuf, sizeof(errbuf)); + } + LeaveCriticalSection(&signal_info_lock); +#endif + + /* Now wait for them to terminate. */ + WaitForTerminatingWorkers(pstate); +} + +/* + * Wait for all workers to terminate. + */ +static void +WaitForTerminatingWorkers(ParallelState *pstate) +{ + while (!HasEveryWorkerTerminated(pstate)) + { + ParallelSlot *slot = NULL; + int j; + +#ifndef WIN32 + /* On non-Windows, use wait() to wait for next worker to end */ + int status; + pid_t pid = wait(&status); + + /* Find dead worker's slot, and clear the PID field */ + for (j = 0; j < pstate->numWorkers; j++) + { + slot = &(pstate->parallelSlot[j]); + if (slot->pid == pid) + { + slot->pid = 0; + break; + } + } +#else /* WIN32 */ + /* On Windows, we must use WaitForMultipleObjects() */ + HANDLE *lpHandles = pg_malloc_array(HANDLE, pstate->numWorkers); + int nrun = 0; + DWORD ret; + uintptr_t hThread; + + for (j = 0; j < pstate->numWorkers; j++) + { + if (WORKER_IS_RUNNING(pstate->parallelSlot[j].workerStatus)) + { + lpHandles[nrun] = (HANDLE) pstate->parallelSlot[j].hThread; + nrun++; + } + } + ret = WaitForMultipleObjects(nrun, lpHandles, false, INFINITE); + Assert(ret != WAIT_FAILED); + hThread = (uintptr_t) lpHandles[ret - WAIT_OBJECT_0]; + free(lpHandles); + + /* Find dead worker's slot, and clear the hThread field */ + for (j = 0; j < pstate->numWorkers; j++) + { + slot = &(pstate->parallelSlot[j]); + if (slot->hThread == hThread) + { + /* For cleanliness, close handles for dead threads */ + CloseHandle((HANDLE) slot->hThread); + slot->hThread = (uintptr_t) INVALID_HANDLE_VALUE; + break; + } + } +#endif /* WIN32 */ + + /* On all platforms, update workerStatus and te[] as well */ + Assert(j < pstate->numWorkers); + slot->workerStatus = WRKR_TERMINATED; + pstate->te[j] = NULL; + } +} + + +/* + * Code for responding to cancel interrupts (SIGINT, control-C, etc) + * + * This doesn't quite belong in this module, but it needs access to the + * ParallelState data, so there's not really a better place either. + * + * When we get a cancel interrupt, we could just die, but in pg_restore that + * could leave a SQL command (e.g., CREATE INDEX on a large table) running + * for a long time. Instead, we try to send a cancel request and then die. + * pg_dump probably doesn't really need this, but we might as well use it + * there too. Note that sending the cancel directly from the signal handler + * is safe because PQcancel() is written to make it so. + * + * In parallel operation on Unix, each process is responsible for canceling + * its own connection (this must be so because nobody else has access to it). + * Furthermore, the leader process should attempt to forward its signal to + * each child. In simple manual use of pg_dump/pg_restore, forwarding isn't + * needed because typing control-C at the console would deliver SIGINT to + * every member of the terminal process group --- but in other scenarios it + * might be that only the leader gets signaled. + * + * On Windows, the cancel handler runs in a separate thread, because that's + * how SetConsoleCtrlHandler works. We make it stop worker threads, send + * cancels on all active connections, and then return FALSE, which will allow + * the process to die. For safety's sake, we use a critical section to + * protect the PGcancel structures against being changed while the signal + * thread runs. + */ + +#ifndef WIN32 + +/* + * Signal handler (Unix only) + */ +static void +sigTermHandler(SIGNAL_ARGS) +{ + int i; + char errbuf[1]; + + /* + * Some platforms allow delivery of new signals to interrupt an active + * signal handler. That could muck up our attempt to send PQcancel, so + * disable the signals that set_cancel_handler enabled. + */ + pqsignal(SIGINT, PG_SIG_IGN); + pqsignal(SIGTERM, PG_SIG_IGN); + pqsignal(SIGQUIT, PG_SIG_IGN); + + /* + * If we're in the leader, forward signal to all workers. (It seems best + * to do this before PQcancel; killing the leader transaction will result + * in invalid-snapshot errors from active workers, which maybe we can + * quiet by killing workers first.) Ignore any errors. + */ + if (signal_info.pstate != NULL) + { + for (i = 0; i < signal_info.pstate->numWorkers; i++) + { + pid_t pid = signal_info.pstate->parallelSlot[i].pid; + + if (pid != 0) + kill(pid, SIGTERM); + } + } + + /* + * Send QueryCancel if we have a connection to send to. Ignore errors, + * there's not much we can do about them anyway. + */ + if (signal_info.myAH != NULL && signal_info.myAH->connCancel != NULL) + (void) PQcancel(signal_info.myAH->connCancel, errbuf, sizeof(errbuf)); + + /* + * Report we're quitting, using nothing more complicated than write(2). + * When in parallel operation, only the leader process should do this. + */ + if (!signal_info.am_worker) + { + if (progname) + { + write_stderr(progname); + write_stderr(": "); + } + write_stderr("terminated by user\n"); + } + + /* + * And die, using _exit() not exit() because the latter will invoke atexit + * handlers that can fail if we interrupted related code. + */ + _exit(1); +} + +/* + * Enable cancel interrupt handler, if not already done. + */ +static void +set_cancel_handler(void) +{ + /* + * When forking, signal_info.handler_set will propagate into the new + * process, but that's fine because the signal handler state does too. + */ + if (!signal_info.handler_set) + { + signal_info.handler_set = true; + + pqsignal(SIGINT, sigTermHandler); + pqsignal(SIGTERM, sigTermHandler); + pqsignal(SIGQUIT, sigTermHandler); + } +} + +#else /* WIN32 */ + +/* + * Console interrupt handler --- runs in a newly-started thread. + * + * After stopping other threads and sending cancel requests on all open + * connections, we return FALSE which will allow the default ExitProcess() + * action to be taken. + */ +static BOOL WINAPI +consoleHandler(DWORD dwCtrlType) +{ + int i; + char errbuf[1]; + + if (dwCtrlType == CTRL_C_EVENT || + dwCtrlType == CTRL_BREAK_EVENT) + { + /* Critical section prevents changing data we look at here */ + EnterCriticalSection(&signal_info_lock); + + /* + * If in parallel mode, stop worker threads and send QueryCancel to + * their connected backends. The main point of stopping the worker + * threads is to keep them from reporting the query cancels as errors, + * which would clutter the user's screen. We needn't stop the leader + * thread since it won't be doing much anyway. Do this before + * canceling the main transaction, else we might get invalid-snapshot + * errors reported before we can stop the workers. Ignore errors, + * there's not much we can do about them anyway. + */ + if (signal_info.pstate != NULL) + { + for (i = 0; i < signal_info.pstate->numWorkers; i++) + { + ParallelSlot *slot = &(signal_info.pstate->parallelSlot[i]); + ArchiveHandle *AH = slot->AH; + HANDLE hThread = (HANDLE) slot->hThread; + + /* + * Using TerminateThread here may leave some resources leaked, + * but it doesn't matter since we're about to end the whole + * process. + */ + if (hThread != INVALID_HANDLE_VALUE) + TerminateThread(hThread, 0); + + if (AH != NULL && AH->connCancel != NULL) + (void) PQcancel(AH->connCancel, errbuf, sizeof(errbuf)); + } + } + + /* + * Send QueryCancel to leader connection, if enabled. Ignore errors, + * there's not much we can do about them anyway. + */ + if (signal_info.myAH != NULL && signal_info.myAH->connCancel != NULL) + (void) PQcancel(signal_info.myAH->connCancel, + errbuf, sizeof(errbuf)); + + LeaveCriticalSection(&signal_info_lock); + + /* + * Report we're quitting, using nothing more complicated than + * write(2). (We might be able to get away with using pg_log_*() + * here, but since we terminated other threads uncleanly above, it + * seems better to assume as little as possible.) + */ + if (progname) + { + write_stderr(progname); + write_stderr(": "); + } + write_stderr("terminated by user\n"); + } + + /* Always return FALSE to allow signal handling to continue */ + return FALSE; +} + +/* + * Enable cancel interrupt handler, if not already done. + */ +static void +set_cancel_handler(void) +{ + if (!signal_info.handler_set) + { + signal_info.handler_set = true; + + InitializeCriticalSection(&signal_info_lock); + + SetConsoleCtrlHandler(consoleHandler, TRUE); + } +} + +#endif /* WIN32 */ + + +/* + * set_archive_cancel_info + * + * Fill AH->connCancel with cancellation info for the specified database + * connection; or clear it if conn is NULL. + */ +void +set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn) +{ + PGcancel *oldConnCancel; + + /* + * Activate the interrupt handler if we didn't yet in this process. On + * Windows, this also initializes signal_info_lock; therefore it's + * important that this happen at least once before we fork off any + * threads. + */ + set_cancel_handler(); + + /* + * On Unix, we assume that storing a pointer value is atomic with respect + * to any possible signal interrupt. On Windows, use a critical section. + */ + +#ifdef WIN32 + EnterCriticalSection(&signal_info_lock); +#endif + + /* Free the old one if we have one */ + oldConnCancel = AH->connCancel; + /* be sure interrupt handler doesn't use pointer while freeing */ + AH->connCancel = NULL; + + if (oldConnCancel != NULL) + PQfreeCancel(oldConnCancel); + + /* Set the new one if specified */ + if (conn) + AH->connCancel = PQgetCancel(conn); + + /* + * On Unix, there's only ever one active ArchiveHandle per process, so we + * can just set signal_info.myAH unconditionally. On Windows, do that + * only in the main thread; worker threads have to make sure their + * ArchiveHandle appears in the pstate data, which is dealt with in + * RunWorker(). + */ +#ifndef WIN32 + signal_info.myAH = AH; +#else + if (mainThreadId == GetCurrentThreadId()) + signal_info.myAH = AH; +#endif + +#ifdef WIN32 + LeaveCriticalSection(&signal_info_lock); +#endif +} + +/* + * set_cancel_pstate + * + * Set signal_info.pstate to point to the specified ParallelState, if any. + * We need this mainly to have an interlock against Windows signal thread. + */ +static void +set_cancel_pstate(ParallelState *pstate) +{ +#ifdef WIN32 + EnterCriticalSection(&signal_info_lock); +#endif + + signal_info.pstate = pstate; + +#ifdef WIN32 + LeaveCriticalSection(&signal_info_lock); +#endif +} + +/* + * set_cancel_slot_archive + * + * Set ParallelSlot's AH field to point to the specified archive, if any. + * We need this mainly to have an interlock against Windows signal thread. + */ +static void +set_cancel_slot_archive(ParallelSlot *slot, ArchiveHandle *AH) +{ +#ifdef WIN32 + EnterCriticalSection(&signal_info_lock); +#endif + + slot->AH = AH; + +#ifdef WIN32 + LeaveCriticalSection(&signal_info_lock); +#endif +} + + +/* + * This function is called by both Unix and Windows variants to set up + * and run a worker process. Caller should exit the process (or thread) + * upon return. + */ +static void +RunWorker(ArchiveHandle *AH, ParallelSlot *slot) +{ + int pipefd[2]; + + /* fetch child ends of pipes */ + pipefd[PIPE_READ] = slot->pipeRevRead; + pipefd[PIPE_WRITE] = slot->pipeRevWrite; + + /* + * Clone the archive so that we have our own state to work with, and in + * particular our own database connection. + * + * We clone on Unix as well as Windows, even though technically we don't + * need to because fork() gives us a copy in our own address space + * already. But CloneArchive resets the state information and also clones + * the database connection which both seem kinda helpful. + */ + AH = CloneArchive(AH); + + /* Remember cloned archive where signal handler can find it */ + set_cancel_slot_archive(slot, AH); + + /* + * Call the setup worker function that's defined in the ArchiveHandle. + */ + (AH->SetupWorkerPtr) ((Archive *) AH); + + /* + * Execute commands until done. + */ + WaitForCommands(AH, pipefd); + + /* + * Disconnect from database and clean up. + */ + set_cancel_slot_archive(slot, NULL); + DisconnectDatabase(&(AH->public)); + DeCloneArchive(AH); +} + +/* + * Thread base function for Windows + */ +#ifdef WIN32 +static unsigned __stdcall +init_spawned_worker_win32(WorkerInfo *wi) +{ + ArchiveHandle *AH = wi->AH; + ParallelSlot *slot = wi->slot; + + /* Don't need WorkerInfo anymore */ + free(wi); + + /* Run the worker ... */ + RunWorker(AH, slot); + + /* Exit the thread */ + _endthreadex(0); + return 0; +} +#endif /* WIN32 */ + +/* + * This function starts a parallel dump or restore by spawning off the worker + * processes. For Windows, it creates a number of threads; on Unix the + * workers are created with fork(). + */ +ParallelState * +ParallelBackupStart(ArchiveHandle *AH) +{ + ParallelState *pstate; + int i; + + Assert(AH->public.numWorkers > 0); + + pstate = pg_malloc_object(ParallelState); + + pstate->numWorkers = AH->public.numWorkers; + pstate->te = NULL; + pstate->parallelSlot = NULL; + + if (AH->public.numWorkers == 1) + return pstate; + + /* Create status arrays, being sure to initialize all fields to 0 */ + pstate->te = + pg_malloc0_array(TocEntry *, pstate->numWorkers); + pstate->parallelSlot = + pg_malloc0_array(ParallelSlot, pstate->numWorkers); + +#ifdef WIN32 + /* Make fmtId() and fmtQualifiedId() use thread-local storage */ + getLocalPQExpBuffer = getThreadLocalPQExpBuffer; +#endif + + /* + * Set the pstate in shutdown_info, to tell the exit handler that it must + * clean up workers as well as the main database connection. But we don't + * set this in signal_info yet, because we don't want child processes to + * inherit non-NULL signal_info.pstate. + */ + shutdown_info.pstate = pstate; + + /* + * Temporarily disable query cancellation on the leader connection. This + * ensures that child processes won't inherit valid AH->connCancel + * settings and thus won't try to issue cancels against the leader's + * connection. No harm is done if we fail while it's disabled, because + * the leader connection is idle at this point anyway. + */ + set_archive_cancel_info(AH, NULL); + + /* Ensure stdio state is quiesced before forking */ + fflush(NULL); + + /* Create desired number of workers */ + for (i = 0; i < pstate->numWorkers; i++) + { +#ifdef WIN32 + WorkerInfo *wi; + uintptr_t handle; +#else + pid_t pid; +#endif + ParallelSlot *slot = &(pstate->parallelSlot[i]); + int pipeMW[2], + pipeWM[2]; + + /* Create communication pipes for this worker */ + if (pgpipe(pipeMW) < 0 || pgpipe(pipeWM) < 0) + pg_fatal("could not create communication channels: %m"); + + /* leader's ends of the pipes */ + slot->pipeRead = pipeWM[PIPE_READ]; + slot->pipeWrite = pipeMW[PIPE_WRITE]; + /* child's ends of the pipes */ + slot->pipeRevRead = pipeMW[PIPE_READ]; + slot->pipeRevWrite = pipeWM[PIPE_WRITE]; + +#ifdef WIN32 + /* Create transient structure to pass args to worker function */ + wi = pg_malloc_object(WorkerInfo); + + wi->AH = AH; + wi->slot = slot; + + handle = _beginthreadex(NULL, 0, (void *) &init_spawned_worker_win32, + wi, 0, &(slot->threadId)); + slot->hThread = handle; + slot->workerStatus = WRKR_IDLE; +#else /* !WIN32 */ + pid = fork(); + if (pid == 0) + { + /* we are the worker */ + int j; + + /* this is needed for GetMyPSlot() */ + slot->pid = getpid(); + + /* instruct signal handler that we're in a worker now */ + signal_info.am_worker = true; + + /* close read end of Worker -> Leader */ + closesocket(pipeWM[PIPE_READ]); + /* close write end of Leader -> Worker */ + closesocket(pipeMW[PIPE_WRITE]); + + /* + * Close all inherited fds for communication of the leader with + * previously-forked workers. + */ + for (j = 0; j < i; j++) + { + closesocket(pstate->parallelSlot[j].pipeRead); + closesocket(pstate->parallelSlot[j].pipeWrite); + } + + /* Run the worker ... */ + RunWorker(AH, slot); + + /* We can just exit(0) when done */ + exit(0); + } + else if (pid < 0) + { + /* fork failed */ + pg_fatal("could not create worker process: %m"); + } + + /* In Leader after successful fork */ + slot->pid = pid; + slot->workerStatus = WRKR_IDLE; + + /* close read end of Leader -> Worker */ + closesocket(pipeMW[PIPE_READ]); + /* close write end of Worker -> Leader */ + closesocket(pipeWM[PIPE_WRITE]); +#endif /* WIN32 */ + } + + /* + * Having forked off the workers, disable SIGPIPE so that leader isn't + * killed if it tries to send a command to a dead worker. We don't want + * the workers to inherit this setting, though. + */ +#ifndef WIN32 + pqsignal(SIGPIPE, PG_SIG_IGN); +#endif + + /* + * Re-establish query cancellation on the leader connection. + */ + set_archive_cancel_info(AH, AH->connection); + + /* + * Tell the cancel signal handler to forward signals to worker processes, + * too. (As with query cancel, we did not need this earlier because the + * workers have not yet been given anything to do; if we die before this + * point, any already-started workers will see EOF and quit promptly.) + */ + set_cancel_pstate(pstate); + + return pstate; +} + +/* + * Close down a parallel dump or restore. + */ +void +ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate) +{ + int i; + + /* No work if non-parallel */ + if (pstate->numWorkers == 1) + return; + + /* There should not be any unfinished jobs */ + Assert(IsEveryWorkerIdle(pstate)); + + /* Close the sockets so that the workers know they can exit */ + for (i = 0; i < pstate->numWorkers; i++) + { + closesocket(pstate->parallelSlot[i].pipeRead); + closesocket(pstate->parallelSlot[i].pipeWrite); + } + + /* Wait for them to exit */ + WaitForTerminatingWorkers(pstate); + + /* + * Unlink pstate from shutdown_info, so the exit handler will not try to + * use it; and likewise unlink from signal_info. + */ + shutdown_info.pstate = NULL; + set_cancel_pstate(NULL); + + /* Release state (mere neatnik-ism, since we're about to terminate) */ + free(pstate->te); + free(pstate->parallelSlot); + free(pstate); +} + +/* + * These next four functions handle construction and parsing of the command + * strings and response strings for parallel workers. + * + * Currently, these can be the same regardless of which archive format we are + * processing. In future, we might want to let format modules override these + * functions to add format-specific data to a command or response. + */ + +/* + * buildWorkerCommand: format a command string to send to a worker. + * + * The string is built in the caller-supplied buffer of size buflen. + */ +static void +buildWorkerCommand(ArchiveHandle *AH, TocEntry *te, T_Action act, + char *buf, int buflen) +{ + if (act == ACT_DUMP) + snprintf(buf, buflen, "DUMP %d", te->dumpId); + else if (act == ACT_RESTORE) + snprintf(buf, buflen, "RESTORE %d", te->dumpId); + else + Assert(false); +} + +/* + * parseWorkerCommand: interpret a command string in a worker. + */ +static void +parseWorkerCommand(ArchiveHandle *AH, TocEntry **te, T_Action *act, + const char *msg) +{ + DumpId dumpId; + int nBytes; + + if (messageStartsWith(msg, "DUMP ")) + { + *act = ACT_DUMP; + sscanf(msg, "DUMP %d%n", &dumpId, &nBytes); + Assert(nBytes == strlen(msg)); + *te = getTocEntryByDumpId(AH, dumpId); + Assert(*te != NULL); + } + else if (messageStartsWith(msg, "RESTORE ")) + { + *act = ACT_RESTORE; + sscanf(msg, "RESTORE %d%n", &dumpId, &nBytes); + Assert(nBytes == strlen(msg)); + *te = getTocEntryByDumpId(AH, dumpId); + Assert(*te != NULL); + } + else + pg_fatal("unrecognized command received from leader: \"%s\"", + msg); +} + +/* + * buildWorkerResponse: format a response string to send to the leader. + * + * The string is built in the caller-supplied buffer of size buflen. + */ +static void +buildWorkerResponse(ArchiveHandle *AH, TocEntry *te, T_Action act, int status, + char *buf, int buflen) +{ + snprintf(buf, buflen, "OK %d %d %d", + te->dumpId, + status, + status == WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0); +} + +/* + * parseWorkerResponse: parse the status message returned by a worker. + * + * Returns the integer status code, and may update fields of AH and/or te. + */ +static int +parseWorkerResponse(ArchiveHandle *AH, TocEntry *te, + const char *msg) +{ + DumpId dumpId; + int nBytes, + n_errors; + int status = 0; + + if (messageStartsWith(msg, "OK ")) + { + sscanf(msg, "OK %d %d %d%n", &dumpId, &status, &n_errors, &nBytes); + + Assert(dumpId == te->dumpId); + Assert(nBytes == strlen(msg)); + + AH->public.n_errors += n_errors; + } + else + pg_fatal("invalid message received from worker: \"%s\"", + msg); + + return status; +} + +/* + * Dispatch a job to some free worker. + * + * te is the TocEntry to be processed, act is the action to be taken on it. + * callback is the function to call on completion of the job. + * + * If no worker is currently available, this will block, and previously + * registered callback functions may be called. + */ +void +DispatchJobForTocEntry(ArchiveHandle *AH, + ParallelState *pstate, + TocEntry *te, + T_Action act, + ParallelCompletionPtr callback, + void *callback_data) +{ + int worker; + char buf[256]; + + /* Get a worker, waiting if none are idle */ + while ((worker = GetIdleWorker(pstate)) == NO_SLOT) + WaitForWorkers(AH, pstate, WFW_ONE_IDLE); + + /* Construct and send command string */ + buildWorkerCommand(AH, te, act, buf, sizeof(buf)); + + sendMessageToWorker(pstate, worker, buf); + + /* Remember worker is busy, and which TocEntry it's working on */ + pstate->parallelSlot[worker].workerStatus = WRKR_WORKING; + pstate->parallelSlot[worker].callback = callback; + pstate->parallelSlot[worker].callback_data = callback_data; + pstate->te[worker] = te; +} + +/* + * Find an idle worker and return its slot number. + * Return NO_SLOT if none are idle. + */ +static int +GetIdleWorker(ParallelState *pstate) +{ + int i; + + for (i = 0; i < pstate->numWorkers; i++) + { + if (pstate->parallelSlot[i].workerStatus == WRKR_IDLE) + return i; + } + return NO_SLOT; +} + +/* + * Return true iff no worker is running. + */ +static bool +HasEveryWorkerTerminated(ParallelState *pstate) +{ + int i; + + for (i = 0; i < pstate->numWorkers; i++) + { + if (WORKER_IS_RUNNING(pstate->parallelSlot[i].workerStatus)) + return false; + } + return true; +} + +/* + * Return true iff every worker is in the WRKR_IDLE state. + */ +bool +IsEveryWorkerIdle(ParallelState *pstate) +{ + int i; + + for (i = 0; i < pstate->numWorkers; i++) + { + if (pstate->parallelSlot[i].workerStatus != WRKR_IDLE) + return false; + } + return true; +} + +/* + * Acquire lock on a table to be dumped by a worker process. + * + * The leader process is already holding an ACCESS SHARE lock. Ordinarily + * it's no problem for a worker to get one too, but if anything else besides + * pg_dump is running, there's a possible deadlock: + * + * 1) Leader dumps the schema and locks all tables in ACCESS SHARE mode. + * 2) Another process requests an ACCESS EXCLUSIVE lock (which is not granted + * because the leader holds a conflicting ACCESS SHARE lock). + * 3) A worker process also requests an ACCESS SHARE lock to read the table. + * The worker is enqueued behind the ACCESS EXCLUSIVE lock request. + * 4) Now we have a deadlock, since the leader is effectively waiting for + * the worker. The server cannot detect that, however. + * + * To prevent an infinite wait, prior to touching a table in a worker, request + * a lock in ACCESS SHARE mode but with NOWAIT. If we don't get the lock, + * then we know that somebody else has requested an ACCESS EXCLUSIVE lock and + * so we have a deadlock. We must fail the backup in that case. + */ +static void +lockTableForWorker(ArchiveHandle *AH, TocEntry *te) +{ + const char *qualId; + PQExpBuffer query; + PGresult *res; + + /* Nothing to do for BLOBS */ + if (strcmp(te->desc, "BLOBS") == 0) + return; + + query = createPQExpBuffer(); + + qualId = fmtQualifiedId(te->namespace, te->tag); + + appendPQExpBuffer(query, "LOCK TABLE %s IN ACCESS SHARE MODE NOWAIT", + qualId); + + res = PQexec(AH->connection, query->data); + + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + pg_fatal("could not obtain lock on relation \"%s\"\n" + "This usually means that someone requested an ACCESS EXCLUSIVE lock " + "on the table after the pg_dump parent process had gotten the " + "initial ACCESS SHARE lock on the table.", qualId); + + PQclear(res); + destroyPQExpBuffer(query); +} + +/* + * WaitForCommands: main routine for a worker process. + * + * Read and execute commands from the leader until we see EOF on the pipe. + */ +static void +WaitForCommands(ArchiveHandle *AH, int pipefd[2]) +{ + char *command; + TocEntry *te; + T_Action act; + int status = 0; + char buf[256]; + + for (;;) + { + if (!(command = getMessageFromLeader(pipefd))) + { + /* EOF, so done */ + return; + } + + /* Decode the command */ + parseWorkerCommand(AH, &te, &act, command); + + if (act == ACT_DUMP) + { + /* Acquire lock on this table within the worker's session */ + lockTableForWorker(AH, te); + + /* Perform the dump command */ + status = (AH->WorkerJobDumpPtr) (AH, te); + } + else if (act == ACT_RESTORE) + { + /* Perform the restore command */ + status = (AH->WorkerJobRestorePtr) (AH, te); + } + else + Assert(false); + + /* Return status to leader */ + buildWorkerResponse(AH, te, act, status, buf, sizeof(buf)); + + sendMessageToLeader(pipefd, buf); + + /* command was pg_malloc'd and we are responsible for free()ing it. */ + free(command); + } +} + +/* + * Check for status messages from workers. + * + * If do_wait is true, wait to get a status message; otherwise, just return + * immediately if there is none available. + * + * When we get a status message, we pass the status code to the callback + * function that was specified to DispatchJobForTocEntry, then reset the + * worker status to IDLE. + * + * Returns true if we collected a status message, else false. + * + * XXX is it worth checking for more than one status message per call? + * It seems somewhat unlikely that multiple workers would finish at exactly + * the same time. + */ +static bool +ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait) +{ + int worker; + char *msg; + + /* Try to collect a status message */ + msg = getMessageFromWorker(pstate, do_wait, &worker); + + if (!msg) + { + /* If do_wait is true, we must have detected EOF on some socket */ + if (do_wait) + pg_fatal("a worker process died unexpectedly"); + return false; + } + + /* Process it and update our idea of the worker's status */ + if (messageStartsWith(msg, "OK ")) + { + ParallelSlot *slot = &pstate->parallelSlot[worker]; + TocEntry *te = pstate->te[worker]; + int status; + + status = parseWorkerResponse(AH, te, msg); + slot->callback(AH, te, status, slot->callback_data); + slot->workerStatus = WRKR_IDLE; + pstate->te[worker] = NULL; + } + else + pg_fatal("invalid message received from worker: \"%s\"", + msg); + + /* Free the string returned from getMessageFromWorker */ + free(msg); + + return true; +} + +/* + * Check for status results from workers, waiting if necessary. + * + * Available wait modes are: + * WFW_NO_WAIT: reap any available status, but don't block + * WFW_GOT_STATUS: wait for at least one more worker to finish + * WFW_ONE_IDLE: wait for at least one worker to be idle + * WFW_ALL_IDLE: wait for all workers to be idle + * + * Any received results are passed to the callback specified to + * DispatchJobForTocEntry. + * + * This function is executed in the leader process. + */ +void +WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode) +{ + bool do_wait = false; + + /* + * In GOT_STATUS mode, always block waiting for a message, since we can't + * return till we get something. In other modes, we don't block the first + * time through the loop. + */ + if (mode == WFW_GOT_STATUS) + { + /* Assert that caller knows what it's doing */ + Assert(!IsEveryWorkerIdle(pstate)); + do_wait = true; + } + + for (;;) + { + /* + * Check for status messages, even if we don't need to block. We do + * not try very hard to reap all available messages, though, since + * there's unlikely to be more than one. + */ + if (ListenToWorkers(AH, pstate, do_wait)) + { + /* + * If we got a message, we are done by definition for GOT_STATUS + * mode, and we can also be certain that there's at least one idle + * worker. So we're done in all but ALL_IDLE mode. + */ + if (mode != WFW_ALL_IDLE) + return; + } + + /* Check whether we must wait for new status messages */ + switch (mode) + { + case WFW_NO_WAIT: + return; /* never wait */ + case WFW_GOT_STATUS: + Assert(false); /* can't get here, because we waited */ + break; + case WFW_ONE_IDLE: + if (GetIdleWorker(pstate) != NO_SLOT) + return; + break; + case WFW_ALL_IDLE: + if (IsEveryWorkerIdle(pstate)) + return; + break; + } + + /* Loop back, and this time wait for something to happen */ + do_wait = true; + } +} + +/* + * Read one command message from the leader, blocking if necessary + * until one is available, and return it as a malloc'd string. + * On EOF, return NULL. + * + * This function is executed in worker processes. + */ +static char * +getMessageFromLeader(int pipefd[2]) +{ + return readMessageFromPipe(pipefd[PIPE_READ]); +} + +/* + * Send a status message to the leader. + * + * This function is executed in worker processes. + */ +static void +sendMessageToLeader(int pipefd[2], const char *str) +{ + int len = strlen(str) + 1; + + if (pipewrite(pipefd[PIPE_WRITE], str, len) != len) + pg_fatal("could not write to the communication channel: %m"); +} + +/* + * Wait until some descriptor in "workerset" becomes readable. + * Returns -1 on error, else the number of readable descriptors. + */ +static int +select_loop(int maxFd, fd_set *workerset) +{ + int i; + fd_set saveSet = *workerset; + + for (;;) + { + *workerset = saveSet; + i = select(maxFd + 1, workerset, NULL, NULL, NULL); + +#ifndef WIN32 + if (i < 0 && errno == EINTR) + continue; +#else + if (i == SOCKET_ERROR && WSAGetLastError() == WSAEINTR) + continue; +#endif + break; + } + + return i; +} + + +/* + * Check for messages from worker processes. + * + * If a message is available, return it as a malloc'd string, and put the + * index of the sending worker in *worker. + * + * If nothing is available, wait if "do_wait" is true, else return NULL. + * + * If we detect EOF on any socket, we'll return NULL. It's not great that + * that's hard to distinguish from the no-data-available case, but for now + * our one caller is okay with that. + * + * This function is executed in the leader process. + */ +static char * +getMessageFromWorker(ParallelState *pstate, bool do_wait, int *worker) +{ + int i; + fd_set workerset; + int maxFd = -1; + struct timeval nowait = {0, 0}; + + /* construct bitmap of socket descriptors for select() */ + FD_ZERO(&workerset); + for (i = 0; i < pstate->numWorkers; i++) + { + if (!WORKER_IS_RUNNING(pstate->parallelSlot[i].workerStatus)) + continue; + FD_SET(pstate->parallelSlot[i].pipeRead, &workerset); + if (pstate->parallelSlot[i].pipeRead > maxFd) + maxFd = pstate->parallelSlot[i].pipeRead; + } + + if (do_wait) + { + i = select_loop(maxFd, &workerset); + Assert(i != 0); + } + else + { + if ((i = select(maxFd + 1, &workerset, NULL, NULL, &nowait)) == 0) + return NULL; + } + + if (i < 0) + pg_fatal("%s() failed: %m", "select"); + + for (i = 0; i < pstate->numWorkers; i++) + { + char *msg; + + if (!WORKER_IS_RUNNING(pstate->parallelSlot[i].workerStatus)) + continue; + if (!FD_ISSET(pstate->parallelSlot[i].pipeRead, &workerset)) + continue; + + /* + * Read the message if any. If the socket is ready because of EOF, + * we'll return NULL instead (and the socket will stay ready, so the + * condition will persist). + * + * Note: because this is a blocking read, we'll wait if only part of + * the message is available. Waiting a long time would be bad, but + * since worker status messages are short and are always sent in one + * operation, it shouldn't be a problem in practice. + */ + msg = readMessageFromPipe(pstate->parallelSlot[i].pipeRead); + *worker = i; + return msg; + } + Assert(false); + return NULL; +} + +/* + * Send a command message to the specified worker process. + * + * This function is executed in the leader process. + */ +static void +sendMessageToWorker(ParallelState *pstate, int worker, const char *str) +{ + int len = strlen(str) + 1; + + if (pipewrite(pstate->parallelSlot[worker].pipeWrite, str, len) != len) + { + pg_fatal("could not write to the communication channel: %m"); + } +} + +/* + * Read one message from the specified pipe (fd), blocking if necessary + * until one is available, and return it as a malloc'd string. + * On EOF, return NULL. + * + * A "message" on the channel is just a null-terminated string. + */ +static char * +readMessageFromPipe(int fd) +{ + char *msg; + int msgsize, + bufsize; + int ret; + + /* + * In theory, if we let piperead() read multiple bytes, it might give us + * back fragments of multiple messages. (That can't actually occur, since + * neither leader nor workers send more than one message without waiting + * for a reply, but we don't wish to assume that here.) For simplicity, + * read a byte at a time until we get the terminating '\0'. This method + * is a bit inefficient, but since this is only used for relatively short + * command and status strings, it shouldn't matter. + */ + bufsize = 64; /* could be any number */ + msg = (char *) pg_malloc(bufsize); + msgsize = 0; + for (;;) + { + Assert(msgsize < bufsize); + ret = piperead(fd, msg + msgsize, 1); + if (ret <= 0) + break; /* error or connection closure */ + + Assert(ret == 1); + + if (msg[msgsize] == '\0') + return msg; /* collected whole message */ + + msgsize++; + if (msgsize == bufsize) /* enlarge buffer if needed */ + { + bufsize += 16; /* could be any number */ + msg = (char *) pg_realloc(msg, bufsize); + } + } + + /* Other end has closed the connection */ + pg_free(msg); + return NULL; +} + +#ifdef WIN32 + +/* + * This is a replacement version of pipe(2) for Windows which allows the pipe + * handles to be used in select(). + * + * Reads and writes on the pipe must go through piperead()/pipewrite(). + * + * For consistency with Unix we declare the returned handles as "int". + * This is okay even on WIN64 because system handles are not more than + * 32 bits wide, but we do have to do some casting. + */ +static int +pgpipe(int handles[2]) +{ + pgsocket s, + tmp_sock; + struct sockaddr_in serv_addr; + int len = sizeof(serv_addr); + + /* We have to use the Unix socket invalid file descriptor value here. */ + handles[0] = handles[1] = -1; + + /* + * setup listen socket + */ + if ((s = socket(AF_INET, SOCK_STREAM, 0)) == PGINVALID_SOCKET) + { + pg_log_error("pgpipe: could not create socket: error code %d", + WSAGetLastError()); + return -1; + } + + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = pg_hton16(0); + serv_addr.sin_addr.s_addr = pg_hton32(INADDR_LOOPBACK); + if (bind(s, (SOCKADDR *) &serv_addr, len) == SOCKET_ERROR) + { + pg_log_error("pgpipe: could not bind: error code %d", + WSAGetLastError()); + closesocket(s); + return -1; + } + if (listen(s, 1) == SOCKET_ERROR) + { + pg_log_error("pgpipe: could not listen: error code %d", + WSAGetLastError()); + closesocket(s); + return -1; + } + if (getsockname(s, (SOCKADDR *) &serv_addr, &len) == SOCKET_ERROR) + { + pg_log_error("pgpipe: %s() failed: error code %d", "getsockname", + WSAGetLastError()); + closesocket(s); + return -1; + } + + /* + * setup pipe handles + */ + if ((tmp_sock = socket(AF_INET, SOCK_STREAM, 0)) == PGINVALID_SOCKET) + { + pg_log_error("pgpipe: could not create second socket: error code %d", + WSAGetLastError()); + closesocket(s); + return -1; + } + handles[1] = (int) tmp_sock; + + if (connect(handles[1], (SOCKADDR *) &serv_addr, len) == SOCKET_ERROR) + { + pg_log_error("pgpipe: could not connect socket: error code %d", + WSAGetLastError()); + closesocket(handles[1]); + handles[1] = -1; + closesocket(s); + return -1; + } + if ((tmp_sock = accept(s, (SOCKADDR *) &serv_addr, &len)) == PGINVALID_SOCKET) + { + pg_log_error("pgpipe: could not accept connection: error code %d", + WSAGetLastError()); + closesocket(handles[1]); + handles[1] = -1; + closesocket(s); + return -1; + } + handles[0] = (int) tmp_sock; + + closesocket(s); + return 0; +} + +#endif /* WIN32 */ diff --git a/src/compat/19/pg_dump/parallel.h b/src/compat/19/pg_dump/parallel.h new file mode 100644 index 00000000..f7557cd0 --- /dev/null +++ b/src/compat/19/pg_dump/parallel.h @@ -0,0 +1,85 @@ +/*------------------------------------------------------------------------- + * + * parallel.h + * + * Parallel support for pg_dump and pg_restore + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/bin/pg_dump/parallel.h + * + *------------------------------------------------------------------------- + */ + +#ifndef PG_DUMP_PARALLEL_H +#define PG_DUMP_PARALLEL_H + +#include + +#include "pg_backup_archiver.h" + +/* Function to call in leader process on completion of a worker task */ +typedef void (*ParallelCompletionPtr) (ArchiveHandle *AH, + TocEntry *te, + int status, + void *callback_data); + +/* Wait options for WaitForWorkers */ +typedef enum +{ + WFW_NO_WAIT, + WFW_GOT_STATUS, + WFW_ONE_IDLE, + WFW_ALL_IDLE, +} WFW_WaitOption; + +/* + * Maximum number of parallel jobs allowed. + * + * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) + * parallel jobs because that's the maximum limit for the + * WaitForMultipleObjects() call. + */ +#ifdef WIN32 +#define PG_MAX_JOBS MAXIMUM_WAIT_OBJECTS +#else +#define PG_MAX_JOBS INT_MAX +#endif + +/* ParallelSlot is an opaque struct known only within parallel.c */ +typedef struct ParallelSlot ParallelSlot; + +/* Overall state for parallel.c */ +typedef struct ParallelState +{ + int numWorkers; /* allowed number of workers */ + /* these arrays have numWorkers entries, one per worker: */ + TocEntry **te; /* item being worked on, or NULL */ + ParallelSlot *parallelSlot; /* private info about each worker */ +} ParallelState; + +#ifdef WIN32 +extern bool parallel_init_done; +extern DWORD mainThreadId; +#endif + +extern void init_parallel_dump_utils(void); + +extern bool IsEveryWorkerIdle(ParallelState *pstate); +extern void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, + WFW_WaitOption mode); + +extern ParallelState *ParallelBackupStart(ArchiveHandle *AH); +extern void DispatchJobForTocEntry(ArchiveHandle *AH, + ParallelState *pstate, + TocEntry *te, + T_Action act, + ParallelCompletionPtr callback, + void *callback_data); +extern void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate); + +extern void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn); + +#endif /* PG_DUMP_PARALLEL_H */ diff --git a/src/compat/19/pg_dump/pg_backup.h b/src/compat/19/pg_dump/pg_backup.h new file mode 100644 index 00000000..5fbeae41 --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup.h @@ -0,0 +1,348 @@ +/*------------------------------------------------------------------------- + * + * pg_backup.h + * + * Public interface to the pg_dump archiver routines. + * + * See the headers to pg_restore for more details. + * + * Copyright (c) 2000, Philip Warner + * Rights are granted to use this software in any way so long + * as this notice is not removed. + * + * The author is not responsible for loss or damages that may + * result from its use. + * + * + * IDENTIFICATION + * src/bin/pg_dump/pg_backup.h + * + *------------------------------------------------------------------------- + */ + +#ifndef PG_BACKUP_H +#define PG_BACKUP_H + +#include "common/compression.h" +#include "common/file_utils.h" +#include "fe_utils/simple_list.h" +#include "libpq-fe.h" + + +typedef enum trivalue +{ + TRI_DEFAULT, + TRI_NO, + TRI_YES, +} trivalue; + +typedef enum _archiveFormat +{ + archUnknown = 0, + archCustom = 1, + archTar = 3, + archNull = 4, + archDirectory = 5, +} ArchiveFormat; + +typedef enum _archiveMode +{ + archModeAppend, + archModeWrite, + archModeRead, +} ArchiveMode; + +typedef enum _teSection +{ + SECTION_NONE = 1, /* comments, ACLs, etc; can be anywhere */ + SECTION_PRE_DATA, /* stuff to be processed before data */ + SECTION_DATA, /* table data, large objects, LO comments */ + SECTION_POST_DATA, /* stuff to be processed after data */ +} teSection; + +/* We need one enum entry per prepared query in pg_dump */ +enum _dumpPreparedQueries +{ + PREPQUERY_DUMPAGG, + PREPQUERY_DUMPBASETYPE, + PREPQUERY_DUMPCOMPOSITETYPE, + PREPQUERY_DUMPDOMAIN, + PREPQUERY_DUMPENUMTYPE, + PREPQUERY_DUMPEXTSTATSOBJSTATS, + PREPQUERY_DUMPFUNC, + PREPQUERY_DUMPOPR, + PREPQUERY_DUMPRANGETYPE, + PREPQUERY_DUMPTABLEATTACH, + PREPQUERY_GETATTRIBUTESTATS, + PREPQUERY_GETCOLUMNACLS, + PREPQUERY_GETDOMAINCONSTRAINTS, +}; + +#define NUM_PREP_QUERIES (PREPQUERY_GETDOMAINCONSTRAINTS + 1) + +/* Parameters needed by ConnectDatabase; same for dump and restore */ +typedef struct _connParams +{ + /* These fields record the actual command line parameters */ + char *dbname; /* this may be a connstring! */ + char *pgport; + char *pghost; + char *username; + trivalue promptPassword; + /* If not NULL, this overrides the dbname obtained from command line */ + /* (but *only* the DB name, not anything else in the connstring) */ + char *override_dbname; +} ConnParams; + +typedef struct _restoreOptions +{ + int createDB; /* Issue commands to create the database */ + int noOwner; /* Don't try to match original object owner */ + int noTableAm; /* Don't issue table-AM-related commands */ + int noTablespace; /* Don't issue tablespace-related commands */ + int disable_triggers; /* disable triggers during data-only + * restore */ + int use_setsessauth; /* Use SET SESSION AUTHORIZATION commands + * instead of OWNER TO */ + char *superuser; /* Username to use as superuser */ + char *use_role; /* Issue SET ROLE to this */ + int dropSchema; + int disable_dollar_quoting; + int dump_inserts; /* 0 = COPY, otherwise rows per INSERT */ + int column_inserts; + int if_exists; + int no_comments; /* Skip comments */ + int no_policies; /* Skip row security policies */ + int no_publications; /* Skip publication entries */ + int no_security_labels; /* Skip security label entries */ + int no_subscriptions; /* Skip subscription entries */ + int strict_names; + + const char *filename; + int dumpSections; + int verbose; + int aclsSkip; + const char *lockWaitTimeout; + int include_everything; + + int tocSummary; + char *tocFile; + int format; + char *formatName; + + int selTypes; + int selIndex; + int selFunction; + int selTrigger; + int selTable; + SimpleStringList indexNames; + SimpleStringList functionNames; + SimpleStringList schemaNames; + SimpleStringList schemaExcludeNames; + SimpleStringList triggerNames; + SimpleStringList tableNames; + + int useDB; + ConnParams cparams; /* parameters to use if useDB */ + + int noDataForFailedTables; + int exit_on_error; + pg_compress_specification compression_spec; /* Specification for + * compression */ + int suppressDumpWarnings; /* Suppress output of WARNING entries + * to stderr */ + + bool single_txn; /* restore all TOCs in one transaction */ + int txn_size; /* restore this many TOCs per txn, if > 0 */ + + bool *idWanted; /* array showing which dump IDs to emit */ + int enable_row_security; + int sequence_data; /* dump sequence data even in schema-only mode */ + int binary_upgrade; + + /* flags derived from the user-settable flags */ + bool dumpSchema; + bool dumpData; + bool dumpStatistics; + + char *restrict_key; +} RestoreOptions; + +typedef struct _dumpOptions +{ + ConnParams cparams; + + int binary_upgrade; + int pgactive_init_node; + + /* various user-settable parameters */ + int dumpSections; /* bitmask of chosen sections */ + bool aclsSkip; + const char *lockWaitTimeout; + int dump_inserts; /* 0 = COPY, otherwise rows per INSERT */ + + /* flags for various command-line long options */ + int disable_dollar_quoting; + int column_inserts; + int if_exists; + int no_comments; + int no_policies; /* Skip row security policies */ + int no_publications; + int no_security_labels; + int no_subscriptions; + int no_toast_compression; + int no_unlogged_table_data; + int serializable_deferrable; + int disable_triggers; + int outputNoTableAm; + int outputNoTablespaces; + int use_setsessauth; + int enable_row_security; + int load_via_partition_root; + + /* default, if no "inclusion" switches appear, is to dump everything */ + bool include_everything; + + int outputClean; + int outputCreateDB; + bool outputLOs; + bool dontOutputLOs; + int outputNoOwner; + char *outputSuperuser; + + int sequence_data; /* dump sequence data even in schema-only mode */ + int do_nothing; + + /* flags derived from the user-settable flags */ + bool dumpSchema; + bool dumpData; + bool dumpStatistics; + + char *restrict_key; +} DumpOptions; + +/* + * We may want to have some more user-readable data, but in the mean + * time this gives us some abstraction and type checking. + */ +typedef struct Archive +{ + DumpOptions *dopt; /* options, if dumping */ + RestoreOptions *ropt; /* options, if restoring */ + + int verbose; + char *remoteVersionStr; /* server's version string */ + int remoteVersion; /* same in numeric form */ + bool isStandby; /* is server a standby node */ + + int minRemoteVersion; /* allowable range */ + int maxRemoteVersion; + + int numWorkers; /* number of parallel processes */ + char *sync_snapshot_id; /* sync snapshot id for parallel operation */ + + /* info needed for string escaping */ + int encoding; /* libpq code for client_encoding */ + bool std_strings; /* standard_conforming_strings */ + + /* other important stuff */ + char *searchpath; /* search_path to set during restore */ + char *use_role; /* Issue SET ROLE to this */ + + /* error handling */ + bool exit_on_error; /* whether to exit on SQL errors... */ + int n_errors; /* number of errors (if no die) */ + + /* prepared-query status */ + bool *is_prepared; /* indexed by enum _dumpPreparedQueries */ + + /* The rest is private */ +} Archive; + + +/* + * pg_dump uses two different mechanisms for identifying database objects: + * + * CatalogId represents an object by the tableoid and oid of its defining + * entry in the system catalogs. We need this to interpret pg_depend entries, + * for instance. + * + * DumpId is a simple sequential integer counter assigned as dumpable objects + * are identified during a pg_dump run. We use DumpId internally in preference + * to CatalogId for two reasons: it's more compact, and we can assign DumpIds + * to "objects" that don't have a separate CatalogId. For example, it is + * convenient to consider a table, its data, and its ACL as three separate + * dumpable "objects" with distinct DumpIds --- this lets us reason about the + * order in which to dump these things. + */ + +typedef struct +{ + /* Note: this struct must not contain any unused bytes */ + Oid tableoid; + Oid oid; +} CatalogId; + +typedef int DumpId; + +#define InvalidDumpId 0 + +/* + * Function pointer prototypes for assorted callback methods. + */ +typedef void (*SetupWorkerPtrType) (Archive *AH); + +/* + * Main archiver interface. + */ + +extern void ConnectDatabaseAhx(Archive *AHX, + const ConnParams *cparams, + bool isReconnect); +extern void DisconnectDatabase(Archive *AHX); +extern PGconn *GetConnection(Archive *AHX); + +/* Called to write *data* to the archive */ +extern void WriteData(Archive *AHX, const void *data, size_t dLen); + +extern int StartLO(Archive *AHX, Oid oid); +extern int EndLO(Archive *AHX, Oid oid); + +extern void CloseArchive(Archive *AHX); + +extern void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt); + +extern void ProcessArchiveRestoreOptions(Archive *AHX); + +extern void RestoreArchive(Archive *AHX, bool append_data); + +/* Open an existing archive */ +extern Archive *OpenArchive(const char *FileSpec, const ArchiveFormat fmt); + +/* Create a new archive */ +extern Archive *CreateArchive(const char *FileSpec, const ArchiveFormat fmt, + const pg_compress_specification compression_spec, + bool dosync, ArchiveMode mode, + SetupWorkerPtrType setupDumpWorker, + DataDirSyncMethod sync_method); + +/* The --list option */ +extern void PrintTOCSummary(Archive *AHX); + +extern RestoreOptions *NewRestoreOptions(void); + +extern DumpOptions *NewDumpOptions(void); +extern void InitDumpOptions(DumpOptions *opts); +extern DumpOptions *dumpOptionsFromRestoreOptions(RestoreOptions *ropt); + +/* Rearrange and filter TOC entries */ +extern void SortTocFromFile(Archive *AHX); + +/* Convenience functions used only when writing DATA */ +extern void archputs(const char *s, Archive *AH); +extern int archprintf(Archive *AH, const char *fmt,...) pg_attribute_printf(2, 3); + +#define appendStringLiteralAH(buf,str,AH) \ + appendStringLiteral(buf, str, (AH)->encoding, (AH)->std_strings) + +#endif /* PG_BACKUP_H */ diff --git a/src/compat/19/pg_dump/pg_backup_archiver.c b/src/compat/19/pg_dump/pg_backup_archiver.c new file mode 100644 index 00000000..fecf6f2d --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_archiver.c @@ -0,0 +1,5280 @@ +/*------------------------------------------------------------------------- + * + * pg_backup_archiver.c + * + * Private implementation of the archiver routines. + * + * See the headers to pg_restore for more details. + * + * Copyright (c) 2000, Philip Warner + * Rights are granted to use this software in any way so long + * as this notice is not removed. + * + * The author is not responsible for loss or damages that may + * result from its use. + * + * + * IDENTIFICATION + * src/bin/pg_dump/pg_backup_archiver.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include +#include +#include +#include +#include +#ifdef WIN32 +#include +#endif + +#include "catalog/pg_class_d.h" +#include "catalog/pg_largeobject_metadata_d.h" +#include "catalog/pg_shdepend_d.h" +#include "common/string.h" +#include "compress_io.h" +#include "dumputils.h" +#include "fe_utils/string_utils.h" +#include "lib/binaryheap.h" +#include "lib/stringinfo.h" +#include "libpq/libpq-fs.h" +#include "parallel.h" +#include "pg_backup_archiver.h" +#include "pg_backup_db.h" +#include "pg_backup_utils.h" +#include "pgtar.h" + +#define TEXT_DUMP_HEADER "--\n-- PostgreSQL database dump\n--\n\n" +#define TEXT_DUMPALL_HEADER "--\n-- PostgreSQL database cluster dump\n--\n\n" + +#define TOC_PREFIX_NONE "" +#define TOC_PREFIX_DATA "Data for " +#define TOC_PREFIX_STATS "Statistics for " + +static ArchiveHandle *_allocAH(const char *FileSpec, const ArchiveFormat fmt, + const pg_compress_specification compression_spec, + bool dosync, ArchiveMode mode, + SetupWorkerPtrType setupWorkerPtr, + DataDirSyncMethod sync_method); +static void _getObjectDescription(PQExpBuffer buf, const TocEntry *te); +static void _printTocEntry(ArchiveHandle *AH, TocEntry *te, const char *pfx); +static void _doSetFixedOutputState(ArchiveHandle *AH); +static void _doSetSessionAuth(ArchiveHandle *AH, const char *user); +static void _reconnectToDB(ArchiveHandle *AH, const char *dbname); +static void _becomeUser(ArchiveHandle *AH, const char *user); +static void _becomeOwner(ArchiveHandle *AH, TocEntry *te); +static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName); +static void _selectTablespace(ArchiveHandle *AH, const char *tablespace); +static void _selectTableAccessMethod(ArchiveHandle *AH, const char *tableam); +static void _printTableAccessMethodNoStorage(ArchiveHandle *AH, + TocEntry *te); +static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te); +static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te); +static void processSearchPathEntry(ArchiveHandle *AH, TocEntry *te); +static int _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH); +static RestorePass _tocEntryRestorePass(TocEntry *te); +static bool _tocEntryIsACL(TocEntry *te); +static void _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te); +static void _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te); +static bool is_load_via_partition_root(TocEntry *te); +static void buildTocEntryArrays(ArchiveHandle *AH); +static void _moveBefore(TocEntry *pos, TocEntry *te); +static int _discoverArchiveFormat(ArchiveHandle *AH); + +static int RestoringToDB(ArchiveHandle *AH); +static void dump_lo_buf(ArchiveHandle *AH); +static void dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim); +static void SetOutput(ArchiveHandle *AH, const char *filename, + const pg_compress_specification compression_spec, bool append_data); +static CompressFileHandle *SaveOutput(ArchiveHandle *AH); +static void RestoreOutput(ArchiveHandle *AH, CompressFileHandle *savedOutput); + +static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel); +static void restore_toc_entries_prefork(ArchiveHandle *AH, + TocEntry *pending_list); +static void restore_toc_entries_parallel(ArchiveHandle *AH, + ParallelState *pstate, + TocEntry *pending_list); +static void restore_toc_entries_postfork(ArchiveHandle *AH, + TocEntry *pending_list); +static void pending_list_header_init(TocEntry *l); +static void pending_list_append(TocEntry *l, TocEntry *te); +static void pending_list_remove(TocEntry *te); +static int TocEntrySizeCompareQsort(const void *p1, const void *p2); +static int TocEntrySizeCompareBinaryheap(void *p1, void *p2, void *arg); +static void move_to_ready_heap(TocEntry *pending_list, + binaryheap *ready_heap, + RestorePass pass); +static TocEntry *pop_next_work_item(binaryheap *ready_heap, + ParallelState *pstate); +static void mark_dump_job_done(ArchiveHandle *AH, + TocEntry *te, + int status, + void *callback_data); +static void mark_restore_job_done(ArchiveHandle *AH, + TocEntry *te, + int status, + void *callback_data); +static void fix_dependencies(ArchiveHandle *AH); +static bool has_lock_conflicts(TocEntry *te1, TocEntry *te2); +static void repoint_table_dependencies(ArchiveHandle *AH); +static void identify_locking_dependencies(ArchiveHandle *AH, TocEntry *te); +static void reduce_dependencies(ArchiveHandle *AH, TocEntry *te, + binaryheap *ready_heap); +static void mark_create_done(ArchiveHandle *AH, TocEntry *te); +static void inhibit_data_for_failed_table(ArchiveHandle *AH, TocEntry *te); + +static void StrictNamesCheck(RestoreOptions *ropt); + + +/* + * Allocate a new DumpOptions block containing all default values. + */ +DumpOptions * +NewDumpOptions(void) +{ + DumpOptions *opts = pg_malloc_object(DumpOptions); + + InitDumpOptions(opts); + return opts; +} + +/* + * Initialize a DumpOptions struct to all default values + */ +void +InitDumpOptions(DumpOptions *opts) +{ + memset(opts, 0, sizeof(DumpOptions)); + /* set any fields that shouldn't default to zeroes */ + opts->include_everything = true; + opts->cparams.promptPassword = TRI_DEFAULT; + opts->dumpSections = DUMP_UNSECTIONED; + opts->dumpSchema = true; + opts->dumpData = true; + opts->dumpStatistics = false; +} + +/* + * Create a freshly allocated DumpOptions with options equivalent to those + * found in the given RestoreOptions. + */ +DumpOptions * +dumpOptionsFromRestoreOptions(RestoreOptions *ropt) +{ + DumpOptions *dopt = NewDumpOptions(); + + /* this is the inverse of what's at the end of pg_dump.c's main() */ + dopt->cparams.dbname = ropt->cparams.dbname ? pg_strdup(ropt->cparams.dbname) : NULL; + dopt->cparams.pgport = ropt->cparams.pgport ? pg_strdup(ropt->cparams.pgport) : NULL; + dopt->cparams.pghost = ropt->cparams.pghost ? pg_strdup(ropt->cparams.pghost) : NULL; + dopt->cparams.username = ropt->cparams.username ? pg_strdup(ropt->cparams.username) : NULL; + dopt->cparams.promptPassword = ropt->cparams.promptPassword; + dopt->outputClean = ropt->dropSchema; + dopt->dumpData = ropt->dumpData; + dopt->dumpSchema = ropt->dumpSchema; + dopt->dumpSections = ropt->dumpSections; + dopt->dumpStatistics = ropt->dumpStatistics; + dopt->if_exists = ropt->if_exists; + dopt->column_inserts = ropt->column_inserts; + dopt->aclsSkip = ropt->aclsSkip; + dopt->outputSuperuser = ropt->superuser; + dopt->outputCreateDB = ropt->createDB; + dopt->outputNoOwner = ropt->noOwner; + dopt->outputNoTableAm = ropt->noTableAm; + dopt->outputNoTablespaces = ropt->noTablespace; + dopt->disable_triggers = ropt->disable_triggers; + dopt->use_setsessauth = ropt->use_setsessauth; + dopt->disable_dollar_quoting = ropt->disable_dollar_quoting; + dopt->dump_inserts = ropt->dump_inserts; + dopt->no_comments = ropt->no_comments; + dopt->no_policies = ropt->no_policies; + dopt->no_publications = ropt->no_publications; + dopt->no_security_labels = ropt->no_security_labels; + dopt->no_subscriptions = ropt->no_subscriptions; + dopt->lockWaitTimeout = ropt->lockWaitTimeout; + dopt->include_everything = ropt->include_everything; + dopt->enable_row_security = ropt->enable_row_security; + dopt->sequence_data = ropt->sequence_data; + dopt->restrict_key = ropt->restrict_key ? pg_strdup(ropt->restrict_key) : NULL; + + return dopt; +} + + +/* + * Wrapper functions. + * + * The objective is to make writing new formats and dumpers as simple + * as possible, if necessary at the expense of extra function calls etc. + * + */ + +/* + * The dump worker setup needs lots of knowledge of the internals of pg_dump, + * so it's defined in pg_dump.c and passed into OpenArchive. The restore worker + * setup doesn't need to know anything much, so it's defined here. + */ +static void +setupRestoreWorker(Archive *AHX) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + AH->ReopenPtr(AH); +} + + +/* Create a new archive */ +/* Public */ +Archive * +CreateArchive(const char *FileSpec, const ArchiveFormat fmt, + const pg_compress_specification compression_spec, + bool dosync, ArchiveMode mode, + SetupWorkerPtrType setupDumpWorker, + DataDirSyncMethod sync_method) + +{ + ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression_spec, + dosync, mode, setupDumpWorker, sync_method); + + return (Archive *) AH; +} + +/* Open an existing archive */ +/* Public */ +Archive * +OpenArchive(const char *FileSpec, const ArchiveFormat fmt) +{ + ArchiveHandle *AH; + pg_compress_specification compression_spec = {0}; + + compression_spec.algorithm = PG_COMPRESSION_NONE; + AH = _allocAH(FileSpec, fmt, compression_spec, true, + archModeRead, setupRestoreWorker, + DATA_DIR_SYNC_METHOD_FSYNC); + + return (Archive *) AH; +} + +/* Public */ +void +CloseArchive(Archive *AHX) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + AH->ClosePtr(AH); + + /* Close the output */ + errno = 0; + if (!EndCompressFileHandle(AH->OF)) + pg_fatal("could not close output file: %m"); +} + +/* Public */ +void +SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt) +{ + /* Caller can omit dump options, in which case we synthesize them */ + if (dopt == NULL && ropt != NULL) + dopt = dumpOptionsFromRestoreOptions(ropt); + + /* Save options for later access */ + AH->dopt = dopt; + AH->ropt = ropt; +} + +/* Public */ +void +ProcessArchiveRestoreOptions(Archive *AHX) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + RestoreOptions *ropt = AH->public.ropt; + TocEntry *te; + teSection curSection; + + /* Decide which TOC entries will be dumped/restored, and mark them */ + curSection = SECTION_PRE_DATA; + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + /* + * When writing an archive, we also take this opportunity to check + * that we have generated the entries in a sane order that respects + * the section divisions. When reading, don't complain, since buggy + * old versions of pg_dump might generate out-of-order archives. + */ + if (AH->mode != archModeRead) + { + switch (te->section) + { + case SECTION_NONE: + /* ok to be anywhere */ + break; + case SECTION_PRE_DATA: + if (curSection != SECTION_PRE_DATA) + pg_log_warning("archive items not in correct section order"); + break; + case SECTION_DATA: + if (curSection == SECTION_POST_DATA) + pg_log_warning("archive items not in correct section order"); + break; + case SECTION_POST_DATA: + /* ok no matter which section we were in */ + break; + default: + pg_fatal("unexpected section code %d", + (int) te->section); + break; + } + } + + if (te->section != SECTION_NONE) + curSection = te->section; + + te->reqs = _tocEntryRequired(te, curSection, AH); + } + + /* Enforce strict names checking */ + if (ropt->strict_names) + StrictNamesCheck(ropt); +} + +/* + * RestoreArchive + * + * If append_data is set, then append data into file as we are restoring dump + * of multiple databases which was taken by pg_dumpall. + */ +void +RestoreArchive(Archive *AHX, bool append_data) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + RestoreOptions *ropt = AH->public.ropt; + bool parallel_mode; + TocEntry *te; + CompressFileHandle *sav; + + AH->stage = STAGE_INITIALIZING; + + /* + * If we're going to do parallel restore, there are some restrictions. + */ + parallel_mode = (AH->public.numWorkers > 1 && ropt->useDB); + if (parallel_mode) + { + /* We haven't got round to making this work for all archive formats */ + if (AH->ClonePtr == NULL || AH->ReopenPtr == NULL) + pg_fatal("parallel restore is not supported with this archive file format"); + + /* Doesn't work if the archive represents dependencies as OIDs */ + if (AH->version < K_VERS_1_8) + pg_fatal("parallel restore is not supported with archives made by pre-8.0 pg_dump"); + + /* + * It's also not gonna work if we can't reopen the input file, so + * let's try that immediately. + */ + AH->ReopenPtr(AH); + } + + /* + * Make sure we won't need (de)compression we haven't got + */ + if (AH->PrintTocDataPtr != NULL) + { + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if (te->hadDumper && (te->reqs & REQ_DATA) != 0) + { + char *errmsg = supports_compression(AH->compression_spec); + + if (errmsg) + pg_fatal("cannot restore from compressed archive (%s)", + errmsg); + else + break; + } + } + } + + /* + * Prepare index arrays, so we can assume we have them throughout restore. + * It's possible we already did this, though. + */ + if (AH->tocsByDumpId == NULL) + buildTocEntryArrays(AH); + + /* + * If we're using a DB connection, then connect it. + */ + if (ropt->useDB) + { + pg_log_info("connecting to database for restore"); + if (AH->version < K_VERS_1_3) + pg_fatal("direct database connections are not supported in pre-1.3 archives"); + + /* + * We don't want to guess at whether the dump will successfully + * restore; allow the attempt regardless of the version of the restore + * target. + */ + AHX->minRemoteVersion = 0; + AHX->maxRemoteVersion = 9999999; + + ConnectDatabaseAhx(AHX, &ropt->cparams, false); + + /* + * If we're talking to the DB directly, don't send comments since they + * obscure SQL when displaying errors + */ + AH->noTocComments = 1; + } + + /* + * Work out if we have an implied schema-less restore. This can happen if + * the dump excluded the schema or the user has used a toc list to exclude + * all of the schema data. All we do is look for schema entries - if none + * are found then we unset the dumpSchema flag. + * + * We could scan for wanted TABLE entries, but that is not the same as + * data-only. At this stage, it seems unnecessary (6-Mar-2001). + */ + if (ropt->dumpSchema) + { + bool no_schema_found = true; + + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if ((te->reqs & REQ_SCHEMA) != 0) + { + no_schema_found = false; + break; + } + } + if (no_schema_found) + { + ropt->dumpSchema = false; + pg_log_info("implied no-schema restore"); + } + } + + /* + * Setup the output file if necessary. + */ + sav = SaveOutput(AH); + if (ropt->filename || ropt->compression_spec.algorithm != PG_COMPRESSION_NONE) + SetOutput(AH, ropt->filename, ropt->compression_spec, append_data); + + ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n"); + + /* + * If generating plain-text output, enter restricted mode to block any + * unexpected psql meta-commands. A malicious source might try to inject + * a variety of things via bogus responses to queries. While we cannot + * prevent such sources from affecting the destination at restore time, we + * can block psql meta-commands so that the client machine that runs psql + * with the dump output remains unaffected. + */ + if (ropt->restrict_key) + ahprintf(AH, "\\restrict %s\n\n", ropt->restrict_key); + + if (AH->archiveRemoteVersion) + ahprintf(AH, "-- Dumped from database version %s\n", + AH->archiveRemoteVersion); + if (AH->archiveDumpVersion) + ahprintf(AH, "-- Dumped by pg_dump version %s\n", + AH->archiveDumpVersion); + + ahprintf(AH, "\n"); + + if (AH->public.verbose) + dumpTimestamp(AH, "Started on", AH->createDate); + + if (ropt->single_txn) + { + if (AH->connection) + StartTransaction(AHX); + else + ahprintf(AH, "BEGIN;\n\n"); + } + + /* + * Establish important parameter values right away. + */ + _doSetFixedOutputState(AH); + + AH->stage = STAGE_PROCESSING; + + /* + * Drop the items at the start, in reverse order + */ + if (ropt->dropSchema) + { + for (te = AH->toc->prev; te != AH->toc; te = te->prev) + { + AH->currentTE = te; + + /* + * In createDB mode, issue a DROP *only* for the database as a + * whole. Issuing drops against anything else would be wrong, + * because at this point we're connected to the wrong database. + * (The DATABASE PROPERTIES entry, if any, should be treated like + * the DATABASE entry.) + */ + if (ropt->createDB) + { + if (strcmp(te->desc, "DATABASE") != 0 && + strcmp(te->desc, "DATABASE PROPERTIES") != 0) + continue; + } + + /* Otherwise, drop anything that's selected and has a dropStmt */ + if (((te->reqs & (REQ_SCHEMA | REQ_DATA)) != 0) && te->dropStmt) + { + bool not_allowed_in_txn = false; + + pg_log_info("dropping %s %s", te->desc, te->tag); + + /* + * In --transaction-size mode, we have to temporarily exit our + * transaction block to drop objects that can't be dropped + * within a transaction. + */ + if (ropt->txn_size > 0) + { + if (strcmp(te->desc, "DATABASE") == 0 || + strcmp(te->desc, "DATABASE PROPERTIES") == 0) + { + not_allowed_in_txn = true; + if (AH->connection) + CommitTransaction(AHX); + else + ahprintf(AH, "COMMIT;\n"); + } + } + + /* Select owner and schema as necessary */ + _becomeOwner(AH, te); + _selectOutputSchema(AH, te->namespace); + + /* + * Now emit the DROP command, if the object has one. Note we + * don't necessarily emit it verbatim; at this point we add an + * appropriate IF EXISTS clause, if the user requested it. + */ + if (strcmp(te->desc, "BLOB METADATA") == 0) + { + /* We must generate the per-blob commands */ + if (ropt->if_exists) + IssueCommandPerBlob(AH, te, + "SELECT pg_catalog.lo_unlink(oid) " + "FROM pg_catalog.pg_largeobject_metadata " + "WHERE oid = '", "'"); + else + IssueCommandPerBlob(AH, te, + "SELECT pg_catalog.lo_unlink('", + "')"); + } + else if (*te->dropStmt != '\0') + { + if (!ropt->if_exists || + strncmp(te->dropStmt, "--", 2) == 0) + { + /* + * Without --if-exists, or if it's just a comment (as + * happens for the public schema), print the dropStmt + * as-is. + */ + ahprintf(AH, "%s", te->dropStmt); + } + else + { + /* + * Inject an appropriate spelling of "if exists". For + * old-style large objects, we have a routine that + * knows how to do it, without depending on + * te->dropStmt; use that. For other objects we need + * to parse the command. + */ + if (strcmp(te->desc, "BLOB") == 0) + { + DropLOIfExists(AH, te->catalogId.oid); + } + else + { + char *dropStmt = pg_strdup(te->dropStmt); + char *dropStmtOrig = dropStmt; + PQExpBuffer ftStmt = createPQExpBuffer(); + + /* + * Need to inject IF EXISTS clause after ALTER + * TABLE part in ALTER TABLE .. DROP statement + */ + if (strncmp(dropStmt, "ALTER TABLE", 11) == 0) + { + appendPQExpBufferStr(ftStmt, + "ALTER TABLE IF EXISTS"); + dropStmt = dropStmt + 11; + } + + /* + * ALTER TABLE..ALTER COLUMN..DROP DEFAULT does + * not support the IF EXISTS clause, and therefore + * we simply emit the original command for DEFAULT + * objects (modulo the adjustment made above). + * + * Likewise, don't mess with DATABASE PROPERTIES. + * + * If we used CREATE OR REPLACE VIEW as a means of + * quasi-dropping an ON SELECT rule, that should + * be emitted unchanged as well. + * + * For other object types, we need to extract the + * first part of the DROP which includes the + * object type. Most of the time this matches + * te->desc, so search for that; however for the + * different kinds of CONSTRAINTs, we know to + * search for hardcoded "DROP CONSTRAINT" instead. + */ + if (strcmp(te->desc, "DEFAULT") == 0 || + strcmp(te->desc, "DATABASE PROPERTIES") == 0 || + strncmp(dropStmt, "CREATE OR REPLACE VIEW", 22) == 0) + appendPQExpBufferStr(ftStmt, dropStmt); + else + { + char buffer[40]; + char *mark; + + if (strcmp(te->desc, "CONSTRAINT") == 0 || + strcmp(te->desc, "CHECK CONSTRAINT") == 0 || + strcmp(te->desc, "FK CONSTRAINT") == 0) + strcpy(buffer, "DROP CONSTRAINT"); + else + snprintf(buffer, sizeof(buffer), "DROP %s", + te->desc); + + mark = strstr(dropStmt, buffer); + + if (mark) + { + *mark = '\0'; + appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s", + dropStmt, buffer, + mark + strlen(buffer)); + } + else + { + /* complain and emit unmodified command */ + pg_log_warning("could not find where to insert IF EXISTS in statement \"%s\"", + dropStmtOrig); + appendPQExpBufferStr(ftStmt, dropStmt); + } + } + + ahprintf(AH, "%s", ftStmt->data); + + destroyPQExpBuffer(ftStmt); + pg_free(dropStmtOrig); + } + } + } + + /* + * In --transaction-size mode, re-establish the transaction + * block if needed; otherwise, commit after every N drops. + */ + if (ropt->txn_size > 0) + { + if (not_allowed_in_txn) + { + if (AH->connection) + StartTransaction(AHX); + else + ahprintf(AH, "BEGIN;\n"); + AH->txnCount = 0; + } + else if (++AH->txnCount >= ropt->txn_size) + { + if (AH->connection) + { + CommitTransaction(AHX); + StartTransaction(AHX); + } + else + ahprintf(AH, "COMMIT;\nBEGIN;\n"); + AH->txnCount = 0; + } + } + } + } + + /* + * _selectOutputSchema may have set currSchema to reflect the effect + * of a "SET search_path" command it emitted. However, by now we may + * have dropped that schema; or it might not have existed in the first + * place. In either case the effective value of search_path will not + * be what we think. Forcibly reset currSchema so that we will + * re-establish the search_path setting when needed (after creating + * the schema). + * + * If we treated users as pg_dump'able objects then we'd need to reset + * currUser here too. + */ + free(AH->currSchema); + AH->currSchema = NULL; + } + + if (parallel_mode) + { + /* + * In parallel mode, turn control over to the parallel-restore logic. + */ + ParallelState *pstate; + TocEntry pending_list; + + /* The archive format module may need some setup for this */ + if (AH->PrepParallelRestorePtr) + AH->PrepParallelRestorePtr(AH); + + pending_list_header_init(&pending_list); + + /* This runs PRE_DATA items and then disconnects from the database */ + restore_toc_entries_prefork(AH, &pending_list); + Assert(AH->connection == NULL); + + /* ParallelBackupStart() will actually fork the processes */ + pstate = ParallelBackupStart(AH); + restore_toc_entries_parallel(AH, pstate, &pending_list); + ParallelBackupEnd(AH, pstate); + + /* reconnect the leader and see if we missed something */ + restore_toc_entries_postfork(AH, &pending_list); + Assert(AH->connection != NULL); + } + else + { + /* + * In serial mode, process everything in three phases: normal items, + * then ACLs, then post-ACL items. We might be able to skip one or + * both extra phases in some cases, eg data-only restores. + */ + bool haveACL = false; + bool havePostACL = false; + + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) == 0) + continue; /* ignore if not to be dumped at all */ + + /* Skip if no-tablespace is given. */ + if (ropt->noTablespace && te && te->desc && + (strcmp(te->desc, "TABLESPACE") == 0)) + continue; + + /* + * Skip DROP DATABASE/ROLES/TABLESPACE if we didn't specify + * --clean + */ + if (!ropt->dropSchema && te && te->desc && + strcmp(te->desc, "DROP_GLOBAL") == 0) + continue; + + switch (_tocEntryRestorePass(te)) + { + case RESTORE_PASS_MAIN: + (void) restore_toc_entry(AH, te, false); + break; + case RESTORE_PASS_ACL: + haveACL = true; + break; + case RESTORE_PASS_POST_ACL: + havePostACL = true; + break; + } + } + + if (haveACL) + { + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) != 0 && + _tocEntryRestorePass(te) == RESTORE_PASS_ACL) + (void) restore_toc_entry(AH, te, false); + } + } + + if (havePostACL) + { + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) != 0 && + _tocEntryRestorePass(te) == RESTORE_PASS_POST_ACL) + (void) restore_toc_entry(AH, te, false); + } + } + } + + /* + * Close out any persistent transaction we may have. While these two + * cases are started in different places, we can end both cases here. + */ + if (ropt->single_txn || ropt->txn_size > 0) + { + if (AH->connection) + CommitTransaction(AHX); + else + ahprintf(AH, "COMMIT;\n\n"); + } + + if (AH->public.verbose) + dumpTimestamp(AH, "Completed on", time(NULL)); + + ahprintf(AH, "--\n-- PostgreSQL database dump complete\n--\n\n"); + + /* + * If generating plain-text output, exit restricted mode at the very end + * of the script. This is not pro forma; in particular, pg_dumpall + * requires this when transitioning from one database to another. + */ + if (ropt->restrict_key) + ahprintf(AH, "\\unrestrict %s\n\n", ropt->restrict_key); + + /* + * Clean up & we're done. + */ + AH->stage = STAGE_FINALIZING; + + if (ropt->filename || ropt->compression_spec.algorithm != PG_COMPRESSION_NONE) + RestoreOutput(AH, sav); + + if (ropt->useDB) + DisconnectDatabase(&AH->public); +} + +/* + * Restore a single TOC item. Used in both parallel and non-parallel restore; + * is_parallel is true if we are in a worker child process. + * + * Returns 0 normally, but WORKER_CREATE_DONE or WORKER_INHIBIT_DATA if + * the parallel parent has to make the corresponding status update. + */ +static int +restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel) +{ + RestoreOptions *ropt = AH->public.ropt; + int status = WORKER_OK; + int reqs; + bool defnDumped; + + AH->currentTE = te; + + /* Dump any relevant dump warnings to stderr */ + if (!ropt->suppressDumpWarnings && strcmp(te->desc, "WARNING") == 0) + { + if (ropt->dumpSchema && te->defn != NULL && strlen(te->defn) != 0) + pg_log_warning("warning from original dump file: %s", te->defn); + else if (te->copyStmt != NULL && strlen(te->copyStmt) != 0) + pg_log_warning("warning from original dump file: %s", te->copyStmt); + } + + /* Work out what, if anything, we want from this entry */ + reqs = te->reqs; + + defnDumped = false; + + /* + * If it has a schema component that we want, then process that + */ + if ((reqs & REQ_SCHEMA) != 0) + { + bool object_is_db = false; + + /* + * In --transaction-size mode, must exit our transaction block to + * create a database or set its properties. + */ + if (strcmp(te->desc, "DATABASE") == 0 || + strcmp(te->desc, "DATABASE PROPERTIES") == 0) + { + object_is_db = true; + if (ropt->txn_size > 0) + { + if (AH->connection) + CommitTransaction(&AH->public); + else + ahprintf(AH, "COMMIT;\n\n"); + } + } + + /* Show namespace in log message if available */ + if (te->namespace) + pg_log_info("creating %s \"%s.%s\"", + te->desc, te->namespace, te->tag); + else + pg_log_info("creating %s \"%s\"", + te->desc, te->tag); + + _printTocEntry(AH, te, TOC_PREFIX_NONE); + defnDumped = true; + + if (strcmp(te->desc, "TABLE") == 0) + { + if (AH->lastErrorTE == te) + { + /* + * We failed to create the table. If + * --no-data-for-failed-tables was given, mark the + * corresponding TABLE DATA to be ignored. + * + * In the parallel case this must be done in the parent, so we + * just set the return value. + */ + if (ropt->noDataForFailedTables) + { + if (is_parallel) + status = WORKER_INHIBIT_DATA; + else + inhibit_data_for_failed_table(AH, te); + } + } + else + { + /* + * We created the table successfully. Mark the corresponding + * TABLE DATA for possible truncation. + * + * In the parallel case this must be done in the parent, so we + * just set the return value. + */ + if (is_parallel) + status = WORKER_CREATE_DONE; + else + mark_create_done(AH, te); + } + } + + /* + * If we created a DB, connect to it. Also, if we changed DB + * properties, reconnect to ensure that relevant GUC settings are + * applied to our session. (That also restarts the transaction block + * in --transaction-size mode.) + */ + if (object_is_db) + { + pg_log_info("connecting to new database \"%s\"", te->tag); + _reconnectToDB(AH, te->tag); + } + } + + /* + * If it has a data component that we want, then process that + */ + if ((reqs & REQ_DATA) != 0) + { + /* + * hadDumper will be set if there is genuine data component for this + * node. Otherwise, we need to check the defn field for statements + * that need to be executed in data-only restores. + */ + if (te->hadDumper) + { + /* + * If we can output the data, then restore it. + */ + if (AH->PrintTocDataPtr != NULL) + { + _printTocEntry(AH, te, TOC_PREFIX_DATA); + + if (strcmp(te->desc, "BLOBS") == 0 || + strcmp(te->desc, "BLOB COMMENTS") == 0) + { + pg_log_info("processing %s", te->desc); + + _selectOutputSchema(AH, "pg_catalog"); + + /* Send BLOB COMMENTS data to ExecuteSimpleCommands() */ + if (strcmp(te->desc, "BLOB COMMENTS") == 0) + AH->outputKind = OUTPUT_OTHERDATA; + + AH->PrintTocDataPtr(AH, te); + + AH->outputKind = OUTPUT_SQLCMDS; + } + else + { + bool use_truncate; + + _disableTriggersIfNecessary(AH, te); + + /* Select owner and schema as necessary */ + _becomeOwner(AH, te); + _selectOutputSchema(AH, te->namespace); + + pg_log_info("processing data for table \"%s.%s\"", + te->namespace, te->tag); + + /* + * In parallel restore, if we created the table earlier in + * this run (so that we know it is empty) and we are not + * restoring a load-via-partition-root data item then we + * wrap the COPY in a transaction and precede it with a + * TRUNCATE. If wal_level is set to minimal this prevents + * WAL-logging the COPY. This obtains a speedup similar + * to that from using single_txn mode in non-parallel + * restores. + * + * We mustn't do this for load-via-partition-root cases + * because some data might get moved across partition + * boundaries, risking deadlock and/or loss of previously + * loaded data. (We assume that all partitions of a + * partitioned table will be treated the same way.) + */ + use_truncate = is_parallel && te->created && + !is_load_via_partition_root(te); + + if (use_truncate) + { + /* + * Parallel restore is always talking directly to a + * server, so no need to see if we should issue BEGIN. + */ + StartTransaction(&AH->public); + + /* + * Issue TRUNCATE with ONLY so that child tables are + * not wiped. + */ + ahprintf(AH, "TRUNCATE TABLE ONLY %s;\n\n", + fmtQualifiedId(te->namespace, te->tag)); + } + + /* + * If we have a copy statement, use it. + */ + if (te->copyStmt && strlen(te->copyStmt) > 0) + { + ahprintf(AH, "%s", te->copyStmt); + AH->outputKind = OUTPUT_COPYDATA; + } + else + AH->outputKind = OUTPUT_OTHERDATA; + + AH->PrintTocDataPtr(AH, te); + + /* + * Terminate COPY if needed. + */ + if (AH->outputKind == OUTPUT_COPYDATA && + RestoringToDB(AH)) + EndDBCopyMode(&AH->public, te->tag); + AH->outputKind = OUTPUT_SQLCMDS; + + /* close out the transaction started above */ + if (use_truncate) + CommitTransaction(&AH->public); + + _enableTriggersIfNecessary(AH, te); + } + } + } + else if (!defnDumped) + { + /* If we haven't already dumped the defn part, do so now */ + pg_log_info("executing %s %s", te->desc, te->tag); + _printTocEntry(AH, te, TOC_PREFIX_NONE); + } + } + + /* + * If it has a statistics component that we want, then process that + */ + if ((reqs & REQ_STATS) != 0) + _printTocEntry(AH, te, TOC_PREFIX_STATS); + + /* + * If we emitted anything for this TOC entry, that counts as one action + * against the transaction-size limit. Commit if it's time to. + */ + if ((reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) != 0 && ropt->txn_size > 0) + { + if (++AH->txnCount >= ropt->txn_size) + { + if (AH->connection) + { + CommitTransaction(&AH->public); + StartTransaction(&AH->public); + } + else + ahprintf(AH, "COMMIT;\nBEGIN;\n\n"); + AH->txnCount = 0; + } + } + + if (AH->public.n_errors > 0 && status == WORKER_OK) + status = WORKER_IGNORED_ERRORS; + + return status; +} + +/* + * Allocate a new RestoreOptions block. + * This is mainly so we can initialize it, but also for future expansion, + */ +RestoreOptions * +NewRestoreOptions(void) +{ + RestoreOptions *opts; + + opts = pg_malloc0_object(RestoreOptions); + + /* set any fields that shouldn't default to zeroes */ + opts->format = archUnknown; + opts->cparams.promptPassword = TRI_DEFAULT; + opts->dumpSections = DUMP_UNSECTIONED; + opts->compression_spec.algorithm = PG_COMPRESSION_NONE; + opts->compression_spec.level = 0; + opts->dumpSchema = true; + opts->dumpData = true; + opts->dumpStatistics = true; + + return opts; +} + +static void +_disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te) +{ + RestoreOptions *ropt = AH->public.ropt; + + /* This hack is only needed in a data-only restore */ + if (ropt->dumpSchema || !ropt->disable_triggers) + return; + + pg_log_info("disabling triggers for %s", te->tag); + + /* + * Become superuser if possible, since they are the only ones who can + * disable constraint triggers. If -S was not given, assume the initial + * user identity is a superuser. (XXX would it be better to become the + * table owner?) + */ + _becomeUser(AH, ropt->superuser); + + /* + * Disable them. + */ + ahprintf(AH, "ALTER TABLE %s DISABLE TRIGGER ALL;\n\n", + fmtQualifiedId(te->namespace, te->tag)); +} + +static void +_enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te) +{ + RestoreOptions *ropt = AH->public.ropt; + + /* This hack is only needed in a data-only restore */ + if (ropt->dumpSchema || !ropt->disable_triggers) + return; + + pg_log_info("enabling triggers for %s", te->tag); + + /* + * Become superuser if possible, since they are the only ones who can + * disable constraint triggers. If -S was not given, assume the initial + * user identity is a superuser. (XXX would it be better to become the + * table owner?) + */ + _becomeUser(AH, ropt->superuser); + + /* + * Enable them. + */ + ahprintf(AH, "ALTER TABLE %s ENABLE TRIGGER ALL;\n\n", + fmtQualifiedId(te->namespace, te->tag)); +} + +/* + * Detect whether a TABLE DATA TOC item is performing "load via partition + * root", that is the target table is an ancestor partition rather than the + * table the TOC item is nominally for. + * + * In newer archive files this can be detected by checking for a special + * comment placed in te->defn. In older files we have to fall back to seeing + * if the COPY statement targets the named table or some other one. This + * will not work for data dumped as INSERT commands, so we could give a false + * negative in that case; fortunately, that's a rarely-used option. + */ +static bool +is_load_via_partition_root(TocEntry *te) +{ + if (te->defn && + strncmp(te->defn, "-- load via partition root ", 27) == 0) + return true; + if (te->copyStmt && *te->copyStmt) + { + PQExpBuffer copyStmt = createPQExpBuffer(); + bool result; + + /* + * Build the initial part of the COPY as it would appear if the + * nominal target table is the actual target. If we see anything + * else, it must be a load-via-partition-root case. + */ + appendPQExpBuffer(copyStmt, "COPY %s ", + fmtQualifiedId(te->namespace, te->tag)); + result = strncmp(te->copyStmt, copyStmt->data, copyStmt->len) != 0; + destroyPQExpBuffer(copyStmt); + return result; + } + /* Assume it's not load-via-partition-root */ + return false; +} + +/* + * This is a routine that is part of the dumper interface, hence the 'Archive*' parameter. + */ + +/* Public */ +void +WriteData(Archive *AHX, const void *data, size_t dLen) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + if (!AH->currToc) + pg_fatal("internal error -- WriteData cannot be called outside the context of a DataDumper routine"); + + AH->WriteDataPtr(AH, data, dLen); +} + +/* + * Create a new TOC entry. The TOC was designed as a TOC, but is now the + * repository for all metadata. But the name has stuck. + * + * The new entry is added to the Archive's TOC list. Most callers can ignore + * the result value because nothing else need be done, but a few want to + * manipulate the TOC entry further. + */ + +/* Public */ +TocEntry * +ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, + ArchiveOpts *opts) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + TocEntry *newToc; + + newToc = pg_malloc0_object(TocEntry); + + AH->tocCount++; + if (dumpId > AH->maxDumpId) + AH->maxDumpId = dumpId; + + newToc->prev = AH->toc->prev; + newToc->next = AH->toc; + AH->toc->prev->next = newToc; + AH->toc->prev = newToc; + + newToc->catalogId = catalogId; + newToc->dumpId = dumpId; + newToc->section = opts->section; + + newToc->tag = pg_strdup(opts->tag); + newToc->namespace = opts->namespace ? pg_strdup(opts->namespace) : NULL; + newToc->tablespace = opts->tablespace ? pg_strdup(opts->tablespace) : NULL; + newToc->tableam = opts->tableam ? pg_strdup(opts->tableam) : NULL; + newToc->relkind = opts->relkind; + newToc->owner = opts->owner ? pg_strdup(opts->owner) : NULL; + newToc->desc = pg_strdup(opts->description); + newToc->defn = opts->createStmt ? pg_strdup(opts->createStmt) : NULL; + newToc->dropStmt = opts->dropStmt ? pg_strdup(opts->dropStmt) : NULL; + newToc->copyStmt = opts->copyStmt ? pg_strdup(opts->copyStmt) : NULL; + + if (opts->nDeps > 0) + { + newToc->dependencies = pg_malloc_array(DumpId, opts->nDeps); + memcpy(newToc->dependencies, opts->deps, opts->nDeps * sizeof(DumpId)); + newToc->nDeps = opts->nDeps; + } + else + { + newToc->dependencies = NULL; + newToc->nDeps = 0; + } + + newToc->dataDumper = opts->dumpFn; + newToc->dataDumperArg = opts->dumpArg; + newToc->hadDumper = opts->dumpFn ? true : false; + + newToc->defnDumper = opts->defnFn; + newToc->defnDumperArg = opts->defnArg; + + newToc->formatData = NULL; + newToc->dataLength = 0; + + if (AH->ArchiveEntryPtr != NULL) + AH->ArchiveEntryPtr(AH, newToc); + + return newToc; +} + +/* Public */ +void +PrintTOCSummary(Archive *AHX) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + RestoreOptions *ropt = AH->public.ropt; + TocEntry *te; + pg_compress_specification out_compression_spec = {0}; + teSection curSection; + CompressFileHandle *sav; + const char *fmtName; + char stamp_str[64]; + + /* TOC is always uncompressed */ + out_compression_spec.algorithm = PG_COMPRESSION_NONE; + + sav = SaveOutput(AH); + if (ropt->filename) + SetOutput(AH, ropt->filename, out_compression_spec, false); + + if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT, + localtime(&AH->createDate)) == 0) + strcpy(stamp_str, "[unknown]"); + + ahprintf(AH, ";\n; Archive created at %s\n", stamp_str); + ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %s\n", + sanitize_line(AH->archdbname, false), + AH->tocCount, + get_compress_algorithm_name(AH->compression_spec.algorithm)); + + switch (AH->format) + { + case archCustom: + fmtName = "CUSTOM"; + break; + case archDirectory: + fmtName = "DIRECTORY"; + break; + case archTar: + fmtName = "TAR"; + break; + default: + fmtName = "UNKNOWN"; + } + + ahprintf(AH, "; Dump Version: %d.%d-%d\n", + ARCHIVE_MAJOR(AH->version), ARCHIVE_MINOR(AH->version), ARCHIVE_REV(AH->version)); + ahprintf(AH, "; Format: %s\n", fmtName); + ahprintf(AH, "; Integer: %zu bytes\n", AH->intSize); + ahprintf(AH, "; Offset: %zu bytes\n", AH->offSize); + if (AH->archiveRemoteVersion) + ahprintf(AH, "; Dumped from database version: %s\n", + AH->archiveRemoteVersion); + if (AH->archiveDumpVersion) + ahprintf(AH, "; Dumped by pg_dump version: %s\n", + AH->archiveDumpVersion); + + ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n"); + + curSection = SECTION_PRE_DATA; + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + /* This bit must match ProcessArchiveRestoreOptions' marking logic */ + if (te->section != SECTION_NONE) + curSection = te->section; + te->reqs = _tocEntryRequired(te, curSection, AH); + /* Now, should we print it? */ + if (ropt->verbose || + (te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) != 0) + { + char *sanitized_name; + char *sanitized_schema; + char *sanitized_owner; + + /* + */ + sanitized_name = sanitize_line(te->tag, false); + sanitized_schema = sanitize_line(te->namespace, true); + sanitized_owner = sanitize_line(te->owner, false); + + ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId, + te->catalogId.tableoid, te->catalogId.oid, + te->desc, sanitized_schema, sanitized_name, + sanitized_owner); + + free(sanitized_name); + free(sanitized_schema); + free(sanitized_owner); + } + if (ropt->verbose && te->nDeps > 0) + { + int i; + + ahprintf(AH, ";\tdepends on:"); + for (i = 0; i < te->nDeps; i++) + ahprintf(AH, " %d", te->dependencies[i]); + ahprintf(AH, "\n"); + } + } + + /* Enforce strict names checking */ + if (ropt->strict_names) + StrictNamesCheck(ropt); + + if (ropt->filename) + RestoreOutput(AH, sav); +} + +/*********** + * Large Object Archival + ***********/ + +/* Called by a dumper to signal start of a LO */ +int +StartLO(Archive *AHX, Oid oid) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + if (!AH->StartLOPtr) + pg_fatal("large-object output not supported in chosen format"); + + AH->StartLOPtr(AH, AH->currToc, oid); + + return 1; +} + +/* Called by a dumper to signal end of a LO */ +int +EndLO(Archive *AHX, Oid oid) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + if (AH->EndLOPtr) + AH->EndLOPtr(AH, AH->currToc, oid); + + return 1; +} + +/********** + * Large Object Restoration + **********/ + +/* + * Called by a format handler before a group of LOs is restored + */ +void +StartRestoreLOs(ArchiveHandle *AH) +{ + RestoreOptions *ropt = AH->public.ropt; + + /* + * LOs must be restored within a transaction block, since we need the LO + * handle to stay open while we write it. Establish a transaction unless + * there's one being used globally. + */ + if (!(ropt->single_txn || ropt->txn_size > 0)) + { + if (AH->connection) + StartTransaction(&AH->public); + else + ahprintf(AH, "BEGIN;\n\n"); + } + + AH->loCount = 0; +} + +/* + * Called by a format handler after a group of LOs is restored + */ +void +EndRestoreLOs(ArchiveHandle *AH) +{ + RestoreOptions *ropt = AH->public.ropt; + + if (!(ropt->single_txn || ropt->txn_size > 0)) + { + if (AH->connection) + CommitTransaction(&AH->public); + else + ahprintf(AH, "COMMIT;\n\n"); + } + + pg_log_info(ngettext("restored %d large object", + "restored %d large objects", + AH->loCount), + AH->loCount); +} + + +/* + * Called by a format handler to initiate restoration of a LO + */ +void +StartRestoreLO(ArchiveHandle *AH, Oid oid, bool drop) +{ + bool old_lo_style = (AH->version < K_VERS_1_12); + Oid loOid; + + AH->loCount++; + + /* Initialize the LO Buffer */ + if (AH->lo_buf == NULL) + { + /* First time through (in this process) so allocate the buffer */ + AH->lo_buf_size = LOBBUFSIZE; + AH->lo_buf = pg_malloc(LOBBUFSIZE); + } + AH->lo_buf_used = 0; + + pg_log_info("restoring large object with OID %u", oid); + + /* With an old archive we must do drop and create logic here */ + if (old_lo_style && drop) + DropLOIfExists(AH, oid); + + if (AH->connection) + { + if (old_lo_style) + { + loOid = lo_create(AH->connection, oid); + if (loOid == 0 || loOid != oid) + pg_fatal("could not create large object %u: %s", + oid, PQerrorMessage(AH->connection)); + } + AH->loFd = lo_open(AH->connection, oid, INV_WRITE); + if (AH->loFd == -1) + pg_fatal("could not open large object %u: %s", + oid, PQerrorMessage(AH->connection)); + } + else + { + if (old_lo_style) + ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n", + oid, INV_WRITE); + else + ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n", + oid, INV_WRITE); + } + + AH->writingLO = true; +} + +void +EndRestoreLO(ArchiveHandle *AH, Oid oid) +{ + if (AH->lo_buf_used > 0) + { + /* Write remaining bytes from the LO buffer */ + dump_lo_buf(AH); + } + + AH->writingLO = false; + + if (AH->connection) + { + lo_close(AH->connection, AH->loFd); + AH->loFd = -1; + } + else + { + ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n"); + } +} + +/*********** + * Sorting and Reordering + ***********/ + +void +SortTocFromFile(Archive *AHX) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + RestoreOptions *ropt = AH->public.ropt; + FILE *fh; + StringInfoData linebuf; + + /* Allocate space for the 'wanted' array, and init it */ + ropt->idWanted = pg_malloc0_array(bool, AH->maxDumpId); + + /* Setup the file */ + fh = fopen(ropt->tocFile, PG_BINARY_R); + if (!fh) + pg_fatal("could not open TOC file \"%s\": %m", ropt->tocFile); + + initStringInfo(&linebuf); + + while (pg_get_line_buf(fh, &linebuf)) + { + char *cmnt; + char *endptr; + DumpId id; + TocEntry *te; + + /* Truncate line at comment, if any */ + cmnt = strchr(linebuf.data, ';'); + if (cmnt != NULL) + { + cmnt[0] = '\0'; + linebuf.len = cmnt - linebuf.data; + } + + /* Ignore if all blank */ + if (strspn(linebuf.data, " \t\r\n") == linebuf.len) + continue; + + /* Get an ID, check it's valid and not already seen */ + id = strtol(linebuf.data, &endptr, 10); + if (endptr == linebuf.data || id <= 0 || id > AH->maxDumpId || + ropt->idWanted[id - 1]) + { + pg_log_warning("line ignored: %s", linebuf.data); + continue; + } + + /* Find TOC entry */ + te = getTocEntryByDumpId(AH, id); + if (!te) + pg_fatal("could not find entry for ID %d", + id); + + /* Mark it wanted */ + ropt->idWanted[id - 1] = true; + + /* + * Move each item to the end of the list as it is selected, so that + * they are placed in the desired order. Any unwanted items will end + * up at the front of the list, which may seem unintuitive but it's + * what we need. In an ordinary serial restore that makes no + * difference, but in a parallel restore we need to mark unrestored + * items' dependencies as satisfied before we start examining + * restorable items. Otherwise they could have surprising + * side-effects on the order in which restorable items actually get + * restored. + */ + _moveBefore(AH->toc, te); + } + + pg_free(linebuf.data); + + if (fclose(fh) != 0) + pg_fatal("could not close TOC file: %m"); +} + +/********************** + * Convenience functions that look like standard IO functions + * for writing data when in dump mode. + **********************/ + +/* Public */ +void +archputs(const char *s, Archive *AH) +{ + WriteData(AH, s, strlen(s)); +} + +/* Public */ +int +archprintf(Archive *AH, const char *fmt,...) +{ + int save_errno = errno; + char *p; + size_t len = 128; /* initial assumption about buffer size */ + size_t cnt; + + for (;;) + { + va_list args; + + /* Allocate work buffer. */ + p = (char *) pg_malloc(len); + + /* Try to format the data. */ + errno = save_errno; + va_start(args, fmt); + cnt = pvsnprintf(p, len, fmt, args); + va_end(args); + + if (cnt < len) + break; /* success */ + + /* Release buffer and loop around to try again with larger len. */ + free(p); + len = cnt; + } + + WriteData(AH, p, cnt); + free(p); + return (int) cnt; +} + + +/******************************* + * Stuff below here should be 'private' to the archiver routines + * + * If append_data is set, then append data into file as we are restoring dump + * of multiple databases which was taken by pg_dumpall. + *******************************/ + +static void +SetOutput(ArchiveHandle *AH, const char *filename, + const pg_compress_specification compression_spec, + bool append_data) +{ + CompressFileHandle *CFH; + const char *mode; + int fn = -1; + + if (filename) + { + if (strcmp(filename, "-") == 0) + fn = fileno(stdout); + } + else if (AH->FH) + fn = fileno(AH->FH); + else if (AH->fSpec) + { + filename = AH->fSpec; + } + else + fn = fileno(stdout); + + if (append_data || AH->mode == archModeAppend) + mode = PG_BINARY_A; + else + mode = PG_BINARY_W; + + CFH = InitCompressFileHandle(compression_spec); + + if (!CFH->open_func(filename, fn, mode, CFH)) + { + if (filename) + pg_fatal("could not open output file \"%s\": %m", filename); + else + pg_fatal("could not open output file: %m"); + } + + AH->OF = CFH; +} + +static CompressFileHandle * +SaveOutput(ArchiveHandle *AH) +{ + return (CompressFileHandle *) AH->OF; +} + +static void +RestoreOutput(ArchiveHandle *AH, CompressFileHandle *savedOutput) +{ + errno = 0; + if (!EndCompressFileHandle(AH->OF)) + pg_fatal("could not close output file: %m"); + + AH->OF = savedOutput; +} + + + +/* + * Print formatted text to the output file (usually stdout). + */ +int +ahprintf(ArchiveHandle *AH, const char *fmt,...) +{ + int save_errno = errno; + char *p; + size_t len = 128; /* initial assumption about buffer size */ + size_t cnt; + + for (;;) + { + va_list args; + + /* Allocate work buffer. */ + p = (char *) pg_malloc(len); + + /* Try to format the data. */ + errno = save_errno; + va_start(args, fmt); + cnt = pvsnprintf(p, len, fmt, args); + va_end(args); + + if (cnt < len) + break; /* success */ + + /* Release buffer and loop around to try again with larger len. */ + free(p); + len = cnt; + } + + ahwrite(p, 1, cnt, AH); + free(p); + return (int) cnt; +} + +/* + * Single place for logic which says 'We are restoring to a direct DB connection'. + */ +static int +RestoringToDB(ArchiveHandle *AH) +{ + RestoreOptions *ropt = AH->public.ropt; + + return (ropt && ropt->useDB && AH->connection); +} + +/* + * Dump the current contents of the LO data buffer while writing a LO + */ +static void +dump_lo_buf(ArchiveHandle *AH) +{ + if (AH->connection) + { + int res; + + res = lo_write(AH->connection, AH->loFd, AH->lo_buf, AH->lo_buf_used); + pg_log_debug(ngettext("wrote %zu byte of large object data (result = %d)", + "wrote %zu bytes of large object data (result = %d)", + AH->lo_buf_used), + AH->lo_buf_used, res); + /* We assume there are no short writes, only errors */ + if (res != AH->lo_buf_used) + warn_or_exit_horribly(AH, "could not write to large object: %s", + PQerrorMessage(AH->connection)); + } + else + { + PQExpBuffer buf = createPQExpBuffer(); + + appendByteaLiteralAHX(buf, + (const unsigned char *) AH->lo_buf, + AH->lo_buf_used, + AH); + + /* Hack: turn off writingLO so ahwrite doesn't recurse to here */ + AH->writingLO = false; + ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data); + AH->writingLO = true; + + destroyPQExpBuffer(buf); + } + AH->lo_buf_used = 0; +} + + +/* + * Write buffer to the output file (usually stdout). This is used for + * outputting 'restore' scripts etc. It is even possible for an archive + * format to create a custom output routine to 'fake' a restore if it + * wants to generate a script (see TAR output). + */ +void +ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH) +{ + int bytes_written = 0; + + if (AH->writingLO) + { + size_t remaining = size * nmemb; + + while (AH->lo_buf_used + remaining > AH->lo_buf_size) + { + size_t avail = AH->lo_buf_size - AH->lo_buf_used; + + memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, avail); + ptr = (const char *) ptr + avail; + remaining -= avail; + AH->lo_buf_used += avail; + dump_lo_buf(AH); + } + + memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, remaining); + AH->lo_buf_used += remaining; + + bytes_written = size * nmemb; + } + else if (AH->CustomOutPtr) + bytes_written = AH->CustomOutPtr(AH, ptr, size * nmemb); + + /* + * If we're doing a restore, and it's direct to DB, and we're connected + * then send it to the DB. + */ + else if (RestoringToDB(AH)) + bytes_written = ExecuteSqlCommandBuf(&AH->public, (const char *) ptr, size * nmemb); + else + { + CompressFileHandle *CFH = (CompressFileHandle *) AH->OF; + + CFH->write_func(ptr, size * nmemb, CFH); + bytes_written = size * nmemb; + } + + if (bytes_written != size * nmemb) + WRITE_ERROR_EXIT; +} + +/* on some error, we may decide to go on... */ +void +warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...) +{ + va_list ap; + + switch (AH->stage) + { + + case STAGE_NONE: + /* Do nothing special */ + break; + + case STAGE_INITIALIZING: + if (AH->stage != AH->lastErrorStage) + pg_log_info("while INITIALIZING:"); + break; + + case STAGE_PROCESSING: + if (AH->stage != AH->lastErrorStage) + pg_log_info("while PROCESSING TOC:"); + break; + + case STAGE_FINALIZING: + if (AH->stage != AH->lastErrorStage) + pg_log_info("while FINALIZING:"); + break; + } + if (AH->currentTE != NULL && AH->currentTE != AH->lastErrorTE) + { + pg_log_info("from TOC entry %d; %u %u %s %s %s", + AH->currentTE->dumpId, + AH->currentTE->catalogId.tableoid, + AH->currentTE->catalogId.oid, + AH->currentTE->desc ? AH->currentTE->desc : "(no desc)", + AH->currentTE->tag ? AH->currentTE->tag : "(no tag)", + AH->currentTE->owner ? AH->currentTE->owner : "(no owner)"); + } + AH->lastErrorStage = AH->stage; + AH->lastErrorTE = AH->currentTE; + + va_start(ap, fmt); + pg_log_generic_v(PG_LOG_ERROR, PG_LOG_PRIMARY, fmt, ap); + va_end(ap); + + if (AH->public.exit_on_error) + exit_nicely(1); + else + AH->public.n_errors++; +} + +#ifdef NOT_USED + +static void +_moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te) +{ + /* Unlink te from list */ + te->prev->next = te->next; + te->next->prev = te->prev; + + /* and insert it after "pos" */ + te->prev = pos; + te->next = pos->next; + pos->next->prev = te; + pos->next = te; +} +#endif + +static void +_moveBefore(TocEntry *pos, TocEntry *te) +{ + /* Unlink te from list */ + te->prev->next = te->next; + te->next->prev = te->prev; + + /* and insert it before "pos" */ + te->prev = pos->prev; + te->next = pos; + pos->prev->next = te; + pos->prev = te; +} + +/* + * Build index arrays for the TOC list + * + * This should be invoked only after we have created or read in all the TOC + * items. + * + * The arrays are indexed by dump ID (so entry zero is unused). Note that the + * array entries run only up to maxDumpId. We might see dependency dump IDs + * beyond that (if the dump was partial); so always check the array bound + * before trying to touch an array entry. + */ +static void +buildTocEntryArrays(ArchiveHandle *AH) +{ + DumpId maxDumpId = AH->maxDumpId; + TocEntry *te; + + AH->tocsByDumpId = pg_malloc0_array(TocEntry *, (maxDumpId + 1)); + AH->tableDataId = pg_malloc0_array(DumpId, (maxDumpId + 1)); + + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + /* this check is purely paranoia, maxDumpId should be correct */ + if (te->dumpId <= 0 || te->dumpId > maxDumpId) + pg_fatal("bad dumpId"); + + /* tocsByDumpId indexes all TOCs by their dump ID */ + AH->tocsByDumpId[te->dumpId] = te; + + /* + * tableDataId provides the TABLE DATA item's dump ID for each TABLE + * TOC entry that has a DATA item. We compute this by reversing the + * TABLE DATA item's dependency, knowing that a TABLE DATA item has + * just one dependency and it is the TABLE item. + */ + if (strcmp(te->desc, "TABLE DATA") == 0 && te->nDeps > 0) + { + DumpId tableId = te->dependencies[0]; + + /* + * The TABLE item might not have been in the archive, if this was + * a data-only dump; but its dump ID should be less than its data + * item's dump ID, so there should be a place for it in the array. + */ + if (tableId <= 0 || tableId > maxDumpId) + pg_fatal("bad table dumpId for TABLE DATA item"); + + AH->tableDataId[tableId] = te->dumpId; + } + } +} + +TocEntry * +getTocEntryByDumpId(ArchiveHandle *AH, DumpId id) +{ + /* build index arrays if we didn't already */ + if (AH->tocsByDumpId == NULL) + buildTocEntryArrays(AH); + + if (id > 0 && id <= AH->maxDumpId) + return AH->tocsByDumpId[id]; + + return NULL; +} + +int +TocIDRequired(ArchiveHandle *AH, DumpId id) +{ + TocEntry *te = getTocEntryByDumpId(AH, id); + + if (!te) + return 0; + + return te->reqs; +} + +size_t +WriteOffset(ArchiveHandle *AH, pgoff_t o, int wasSet) +{ + int off; + + /* Save the flag */ + AH->WriteBytePtr(AH, wasSet); + + /* Write out pgoff_t smallest byte first, prevents endian mismatch */ + for (off = 0; off < sizeof(pgoff_t); off++) + { + AH->WriteBytePtr(AH, o & 0xFF); + o >>= 8; + } + return sizeof(pgoff_t) + 1; +} + +int +ReadOffset(ArchiveHandle *AH, pgoff_t *o) +{ + int i; + int off; + int offsetFlg; + + /* Initialize to zero */ + *o = 0; + + /* Check for old version */ + if (AH->version < K_VERS_1_7) + { + /* Prior versions wrote offsets using WriteInt */ + i = ReadInt(AH); + /* -1 means not set */ + if (i < 0) + return K_OFFSET_POS_NOT_SET; + else if (i == 0) + return K_OFFSET_NO_DATA; + + /* Cast to pgoff_t because it was written as an int. */ + *o = (pgoff_t) i; + return K_OFFSET_POS_SET; + } + + /* + * Read the flag indicating the state of the data pointer. Check if valid + * and die if not. + * + * This used to be handled by a negative or zero pointer, now we use an + * extra byte specifically for the state. + */ + offsetFlg = AH->ReadBytePtr(AH) & 0xFF; + + switch (offsetFlg) + { + case K_OFFSET_POS_NOT_SET: + case K_OFFSET_NO_DATA: + case K_OFFSET_POS_SET: + + break; + + default: + pg_fatal("unexpected data offset flag %d", offsetFlg); + } + + /* + * Read the bytes + */ + for (off = 0; off < AH->offSize; off++) + { + if (off < sizeof(pgoff_t)) + *o |= ((pgoff_t) (AH->ReadBytePtr(AH))) << (off * 8); + else + { + if (AH->ReadBytePtr(AH) != 0) + pg_fatal("file offset in dump file is too large"); + } + } + + return offsetFlg; +} + +size_t +WriteInt(ArchiveHandle *AH, int i) +{ + int b; + + /* + * This is a bit yucky, but I don't want to make the binary format very + * dependent on representation, and not knowing much about it, I write out + * a sign byte. If you change this, don't forget to change the file + * version #, and modify ReadInt to read the new format AS WELL AS the old + * formats. + */ + + /* SIGN byte */ + if (i < 0) + { + AH->WriteBytePtr(AH, 1); + i = -i; + } + else + AH->WriteBytePtr(AH, 0); + + for (b = 0; b < AH->intSize; b++) + { + AH->WriteBytePtr(AH, i & 0xFF); + i >>= 8; + } + + return AH->intSize + 1; +} + +int +ReadInt(ArchiveHandle *AH) +{ + int res = 0; + int bv, + b; + int sign = 0; /* Default positive */ + int bitShift = 0; + + if (AH->version > K_VERS_1_0) + /* Read a sign byte */ + sign = AH->ReadBytePtr(AH); + + for (b = 0; b < AH->intSize; b++) + { + bv = AH->ReadBytePtr(AH) & 0xFF; + if (bv != 0) + res = res + (bv << bitShift); + bitShift += 8; + } + + if (sign) + res = -res; + + return res; +} + +size_t +WriteStr(ArchiveHandle *AH, const char *c) +{ + size_t res; + + if (c) + { + int len = strlen(c); + + res = WriteInt(AH, len); + AH->WriteBufPtr(AH, c, len); + res += len; + } + else + res = WriteInt(AH, -1); + + return res; +} + +char * +ReadStr(ArchiveHandle *AH) +{ + char *buf; + int l; + + l = ReadInt(AH); + if (l < 0) + buf = NULL; + else + { + buf = (char *) pg_malloc(l + 1); + AH->ReadBufPtr(AH, buf, l); + + buf[l] = '\0'; + } + + return buf; +} + +static bool +_fileExistsInDirectory(const char *dir, const char *filename) +{ + struct stat st; + char buf[MAXPGPATH]; + + if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH) + pg_fatal("directory name too long: \"%s\"", dir); + + return (stat(buf, &st) == 0 && S_ISREG(st.st_mode)); +} + +static int +_discoverArchiveFormat(ArchiveHandle *AH) +{ + FILE *fh; + char sig[6]; /* More than enough */ + size_t cnt; + int wantClose = 0; + + pg_log_debug("attempting to ascertain archive format"); + + free(AH->lookahead); + + AH->readHeader = 0; + AH->lookaheadSize = 512; + AH->lookahead = pg_malloc0(512); + AH->lookaheadLen = 0; + AH->lookaheadPos = 0; + + if (AH->fSpec) + { + struct stat st; + + wantClose = 1; + + /* + * Check if the specified archive is a directory. If so, check if + * there's a "toc.dat" (or "toc.dat.{gz,lz4,zst}") file in it. + */ + if (stat(AH->fSpec, &st) == 0 && S_ISDIR(st.st_mode)) + { + AH->format = archDirectory; + if (_fileExistsInDirectory(AH->fSpec, "toc.dat")) + return AH->format; +#ifdef HAVE_LIBZ + if (_fileExistsInDirectory(AH->fSpec, "toc.dat.gz")) + return AH->format; +#endif +#ifdef USE_LZ4 + if (_fileExistsInDirectory(AH->fSpec, "toc.dat.lz4")) + return AH->format; +#endif +#ifdef USE_ZSTD + if (_fileExistsInDirectory(AH->fSpec, "toc.dat.zst")) + return AH->format; +#endif + pg_fatal("directory \"%s\" does not appear to be a valid archive (\"toc.dat\" does not exist)", + AH->fSpec); + fh = NULL; /* keep compiler quiet */ + } + else + { + fh = fopen(AH->fSpec, PG_BINARY_R); + if (!fh) + pg_fatal("could not open input file \"%s\": %m", AH->fSpec); + } + } + else + { + fh = stdin; + if (!fh) + pg_fatal("could not open input file: %m"); + } + + if ((cnt = fread(sig, 1, 5, fh)) != 5) + { + if (ferror(fh)) + pg_fatal("could not read input file: %m"); + else + pg_fatal("input file is too short (read %zu, expected 5)", cnt); + } + + /* Save it, just in case we need it later */ + memcpy(&AH->lookahead[0], sig, 5); + AH->lookaheadLen = 5; + + if (strncmp(sig, "PGDMP", 5) == 0) + { + /* It's custom format, stop here */ + AH->format = archCustom; + AH->readHeader = 1; + } + else + { + /* + * *Maybe* we have a tar archive format file or a text dump ... So, + * read first 512 byte header... + */ + cnt = fread(&AH->lookahead[AH->lookaheadLen], 1, 512 - AH->lookaheadLen, fh); + /* read failure is checked below */ + AH->lookaheadLen += cnt; + + if (AH->lookaheadLen >= strlen(TEXT_DUMPALL_HEADER) && + (strncmp(AH->lookahead, TEXT_DUMP_HEADER, strlen(TEXT_DUMP_HEADER)) == 0 || + strncmp(AH->lookahead, TEXT_DUMPALL_HEADER, strlen(TEXT_DUMPALL_HEADER)) == 0)) + { + /* + * looks like it's probably a text format dump. so suggest they + * try psql + */ + pg_fatal("input file appears to be a text format dump. Please use psql."); + } + + if (AH->lookaheadLen != 512) + { + if (feof(fh)) + pg_fatal("input file does not appear to be a valid archive (too short?)"); + else + READ_ERROR_EXIT(fh); + } + + if (!isValidTarHeader(AH->lookahead)) + pg_fatal("input file does not appear to be a valid tar archive"); + + AH->format = archTar; + } + + /* Close the file if we opened it */ + if (wantClose) + { + if (fclose(fh) != 0) + pg_fatal("could not close input file: %m"); + /* Forget lookahead, since we'll re-read header after re-opening */ + AH->readHeader = 0; + AH->lookaheadLen = 0; + } + + return AH->format; +} + + +/* + * Allocate an archive handle + */ +static ArchiveHandle * +_allocAH(const char *FileSpec, const ArchiveFormat fmt, + const pg_compress_specification compression_spec, + bool dosync, ArchiveMode mode, + SetupWorkerPtrType setupWorkerPtr, DataDirSyncMethod sync_method) +{ + ArchiveHandle *AH; + CompressFileHandle *CFH; + pg_compress_specification out_compress_spec = {0}; + + pg_log_debug("allocating AH for %s, format %d", + FileSpec ? FileSpec : "(stdio)", fmt); + + AH = pg_malloc0_object(ArchiveHandle); + + AH->version = K_VERS_SELF; + + /* initialize for backwards compatible string processing */ + AH->public.encoding = 0; /* PG_SQL_ASCII */ + AH->public.std_strings = true; + + /* sql error handling */ + AH->public.exit_on_error = true; + AH->public.n_errors = 0; + + AH->archiveDumpVersion = PG_VERSION; + + AH->createDate = time(NULL); + + AH->intSize = sizeof(int); + AH->offSize = sizeof(pgoff_t); + if (FileSpec) + { + AH->fSpec = pg_strdup(FileSpec); + + /* + * Not used; maybe later.... + * + * AH->workDir = pg_strdup(FileSpec); for(i=strlen(FileSpec) ; i > 0 ; + * i--) if (AH->workDir[i-1] == '/') + */ + } + else + AH->fSpec = NULL; + + AH->currUser = NULL; /* unknown */ + AH->currSchema = NULL; /* ditto */ + AH->currTablespace = NULL; /* ditto */ + AH->currTableAm = NULL; /* ditto */ + + AH->toc = pg_malloc0_object(TocEntry); + + AH->toc->next = AH->toc; + AH->toc->prev = AH->toc; + + AH->mode = mode; + AH->compression_spec = compression_spec; + AH->dosync = dosync; + AH->sync_method = sync_method; + + memset(&(AH->sqlparse), 0, sizeof(AH->sqlparse)); + + /* Open stdout with no compression for AH output handle */ + out_compress_spec.algorithm = PG_COMPRESSION_NONE; + CFH = InitCompressFileHandle(out_compress_spec); + if (!CFH->open_func(NULL, fileno(stdout), PG_BINARY_A, CFH)) + pg_fatal("could not open stdout for appending: %m"); + AH->OF = CFH; + + /* + * On Windows, we need to use binary mode to read/write non-text files, + * which include all archive formats as well as compressed plain text. + * Force stdin/stdout into binary mode if that is what we are using. + */ +#ifdef WIN32 + if ((fmt != archNull || compression_spec.algorithm != PG_COMPRESSION_NONE) && + (AH->fSpec == NULL || strcmp(AH->fSpec, "") == 0)) + { + if (mode == archModeWrite) + _setmode(fileno(stdout), O_BINARY); + else + _setmode(fileno(stdin), O_BINARY); + } +#endif + + AH->SetupWorkerPtr = setupWorkerPtr; + + if (fmt == archUnknown) + AH->format = _discoverArchiveFormat(AH); + else + AH->format = fmt; + + switch (AH->format) + { + case archCustom: + InitArchiveFmt_Custom(AH); + break; + + case archNull: + InitArchiveFmt_Null(AH); + break; + + case archDirectory: + InitArchiveFmt_Directory(AH); + break; + + case archTar: + InitArchiveFmt_Tar(AH); + break; + + default: + pg_fatal("unrecognized file format \"%d\"", AH->format); + } + + return AH; +} + +/* + * Write out all data (tables & LOs) + */ +void +WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate) +{ + TocEntry *te; + + if (pstate && pstate->numWorkers > 1) + { + /* + * In parallel mode, this code runs in the leader process. We + * construct an array of candidate TEs, then sort it into decreasing + * size order, then dispatch each TE to a data-transfer worker. By + * dumping larger tables first, we avoid getting into a situation + * where we're down to one job and it's big, losing parallelism. + */ + TocEntry **tes; + int ntes; + + tes = pg_malloc_array(TocEntry *, AH->tocCount); + ntes = 0; + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + /* Consider only TEs with dataDumper functions ... */ + if (!te->dataDumper) + continue; + /* ... and ignore ones not enabled for dump */ + if ((te->reqs & REQ_DATA) == 0) + continue; + + tes[ntes++] = te; + } + + if (ntes > 1) + qsort(tes, ntes, sizeof(TocEntry *), TocEntrySizeCompareQsort); + + for (int i = 0; i < ntes; i++) + DispatchJobForTocEntry(AH, pstate, tes[i], ACT_DUMP, + mark_dump_job_done, NULL); + + pg_free(tes); + + /* Now wait for workers to finish. */ + WaitForWorkers(AH, pstate, WFW_ALL_IDLE); + } + else + { + /* Non-parallel mode: just dump all candidate TEs sequentially. */ + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + /* Must have same filter conditions as above */ + if (!te->dataDumper) + continue; + if ((te->reqs & REQ_DATA) == 0) + continue; + + WriteDataChunksForTocEntry(AH, te); + } + } +} + + +/* + * Callback function that's invoked in the leader process after a step has + * been parallel dumped. + * + * We don't need to do anything except check for worker failure. + */ +static void +mark_dump_job_done(ArchiveHandle *AH, + TocEntry *te, + int status, + void *callback_data) +{ + pg_log_info("finished item %d %s %s", + te->dumpId, te->desc, te->tag); + + if (status != 0) + pg_fatal("worker process failed: exit code %d", + status); +} + + +void +WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te) +{ + StartDataPtrType startPtr; + EndDataPtrType endPtr; + + AH->currToc = te; + + if (strcmp(te->desc, "BLOBS") == 0) + { + startPtr = AH->StartLOsPtr; + endPtr = AH->EndLOsPtr; + } + else + { + startPtr = AH->StartDataPtr; + endPtr = AH->EndDataPtr; + } + + if (startPtr != NULL) + (*startPtr) (AH, te); + + /* + * The user-provided DataDumper routine needs to call AH->WriteData + */ + te->dataDumper((Archive *) AH, te->dataDumperArg); + + if (endPtr != NULL) + (*endPtr) (AH, te); + + AH->currToc = NULL; +} + +void +WriteToc(ArchiveHandle *AH) +{ + TocEntry *te; + char workbuf[32]; + int tocCount; + int i; + + /* count entries that will actually be dumped */ + tocCount = 0; + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS | REQ_SPECIAL)) != 0) + tocCount++; + } + + /* printf("%d TOC Entries to save\n", tocCount); */ + + WriteInt(AH, tocCount); + + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS | REQ_SPECIAL)) == 0) + continue; + + WriteInt(AH, te->dumpId); + WriteInt(AH, te->dataDumper ? 1 : 0); + + /* OID is recorded as a string for historical reasons */ + sprintf(workbuf, "%u", te->catalogId.tableoid); + WriteStr(AH, workbuf); + sprintf(workbuf, "%u", te->catalogId.oid); + WriteStr(AH, workbuf); + + WriteStr(AH, te->tag); + WriteStr(AH, te->desc); + WriteInt(AH, te->section); + + if (te->defnLen) + { + /* + * defnLen should only be set for custom format's second call to + * WriteToc(), which rewrites the TOC in place to update data + * offsets. Instead of calling the defnDumper a second time + * (which could involve re-executing queries), just skip writing + * the entry. While regenerating the definition should + * theoretically produce the same result as before, it's expensive + * and feels risky. + * + * The custom format only calls WriteToc() a second time if + * fseeko() is usable (see _CloseArchive() in pg_backup_custom.c), + * so we can safely use it without checking. For other formats, + * we fail because one of our assumptions must no longer hold + * true. + * + * XXX This is a layering violation, but the alternative is an + * awkward and complicated callback infrastructure for this + * special case. This might be worth revisiting in the future. + */ + if (AH->format != archCustom) + pg_fatal("unexpected TOC entry in WriteToc(): %d %s %s", + te->dumpId, te->desc, te->tag); + + if (fseeko(AH->FH, te->defnLen, SEEK_CUR) != 0) + pg_fatal("error during file seek: %m"); + } + else if (te->defnDumper) + { + char *defn = te->defnDumper((Archive *) AH, te->defnDumperArg, te); + + te->defnLen = WriteStr(AH, defn); + pg_free(defn); + } + else + WriteStr(AH, te->defn); + + WriteStr(AH, te->dropStmt); + WriteStr(AH, te->copyStmt); + WriteStr(AH, te->namespace); + WriteStr(AH, te->tablespace); + WriteStr(AH, te->tableam); + WriteInt(AH, te->relkind); + WriteStr(AH, te->owner); + WriteStr(AH, "false"); + + /* Dump list of dependencies */ + for (i = 0; i < te->nDeps; i++) + { + sprintf(workbuf, "%d", te->dependencies[i]); + WriteStr(AH, workbuf); + } + WriteStr(AH, NULL); /* Terminate List */ + + if (AH->WriteExtraTocPtr) + AH->WriteExtraTocPtr(AH, te); + } +} + +void +ReadToc(ArchiveHandle *AH) +{ + int i; + char *tmp; + DumpId *deps; + int depIdx; + int depSize; + TocEntry *te; + bool is_supported; + + AH->tocCount = ReadInt(AH); + AH->maxDumpId = 0; + + for (i = 0; i < AH->tocCount; i++) + { + te = pg_malloc0_object(TocEntry); + te->dumpId = ReadInt(AH); + + if (te->dumpId > AH->maxDumpId) + AH->maxDumpId = te->dumpId; + + /* Sanity check */ + if (te->dumpId <= 0) + pg_fatal("entry ID %d out of range -- perhaps a corrupt TOC", + te->dumpId); + + te->hadDumper = ReadInt(AH); + + if (AH->version >= K_VERS_1_8) + { + tmp = ReadStr(AH); + sscanf(tmp, "%u", &te->catalogId.tableoid); + free(tmp); + } + else + te->catalogId.tableoid = InvalidOid; + tmp = ReadStr(AH); + sscanf(tmp, "%u", &te->catalogId.oid); + free(tmp); + + te->tag = ReadStr(AH); + te->desc = ReadStr(AH); + + if (AH->version >= K_VERS_1_11) + { + te->section = ReadInt(AH); + } + else + { + /* + * Rules for pre-8.4 archives wherein pg_dump hasn't classified + * the entries into sections. This list need not cover entry + * types added later than 8.4. + */ + if (strcmp(te->desc, "COMMENT") == 0 || + strcmp(te->desc, "ACL") == 0 || + strcmp(te->desc, "ACL LANGUAGE") == 0) + te->section = SECTION_NONE; + else if (strcmp(te->desc, "TABLE DATA") == 0 || + strcmp(te->desc, "BLOBS") == 0 || + strcmp(te->desc, "BLOB COMMENTS") == 0) + te->section = SECTION_DATA; + else if (strcmp(te->desc, "CONSTRAINT") == 0 || + strcmp(te->desc, "CHECK CONSTRAINT") == 0 || + strcmp(te->desc, "FK CONSTRAINT") == 0 || + strcmp(te->desc, "INDEX") == 0 || + strcmp(te->desc, "RULE") == 0 || + strcmp(te->desc, "TRIGGER") == 0) + te->section = SECTION_POST_DATA; + else + te->section = SECTION_PRE_DATA; + } + + te->defn = ReadStr(AH); + te->dropStmt = ReadStr(AH); + + if (AH->version >= K_VERS_1_3) + te->copyStmt = ReadStr(AH); + + if (AH->version >= K_VERS_1_6) + te->namespace = ReadStr(AH); + + if (AH->version >= K_VERS_1_10) + te->tablespace = ReadStr(AH); + + if (AH->version >= K_VERS_1_14) + te->tableam = ReadStr(AH); + + if (AH->version >= K_VERS_1_16) + te->relkind = ReadInt(AH); + + te->owner = ReadStr(AH); + is_supported = true; + if (AH->version < K_VERS_1_9) + is_supported = false; + else + { + tmp = ReadStr(AH); + + if (strcmp(tmp, "true") == 0) + is_supported = false; + + free(tmp); + } + + if (!is_supported) + pg_log_warning("restoring tables WITH OIDS is not supported anymore"); + + /* Read TOC entry dependencies */ + if (AH->version >= K_VERS_1_5) + { + depSize = 100; + deps = pg_malloc_array(DumpId, depSize); + depIdx = 0; + for (;;) + { + tmp = ReadStr(AH); + if (!tmp) + break; /* end of list */ + if (depIdx >= depSize) + { + depSize *= 2; + deps = pg_realloc_array(deps, DumpId, depSize); + } + sscanf(tmp, "%d", &deps[depIdx]); + free(tmp); + depIdx++; + } + + if (depIdx > 0) /* We have a non-null entry */ + { + deps = pg_realloc_array(deps, DumpId, depIdx); + te->dependencies = deps; + te->nDeps = depIdx; + } + else + { + free(deps); + te->dependencies = NULL; + te->nDeps = 0; + } + } + else + { + te->dependencies = NULL; + te->nDeps = 0; + } + te->dataLength = 0; + + if (AH->ReadExtraTocPtr) + AH->ReadExtraTocPtr(AH, te); + + pg_log_debug("read TOC entry %d (ID %d) for %s %s", + i, te->dumpId, te->desc, te->tag); + + /* link completed entry into TOC circular list */ + te->prev = AH->toc->prev; + AH->toc->prev->next = te; + AH->toc->prev = te; + te->next = AH->toc; + + /* special processing immediately upon read for some items */ + if (strcmp(te->desc, "ENCODING") == 0) + processEncodingEntry(AH, te); + else if (strcmp(te->desc, "STDSTRINGS") == 0) + processStdStringsEntry(AH, te); + else if (strcmp(te->desc, "SEARCHPATH") == 0) + processSearchPathEntry(AH, te); + } +} + +static void +processEncodingEntry(ArchiveHandle *AH, TocEntry *te) +{ + /* te->defn should have the form SET client_encoding = 'foo'; */ + char *defn = pg_strdup(te->defn); + char *ptr1; + char *ptr2 = NULL; + int encoding; + + ptr1 = strchr(defn, '\''); + if (ptr1) + ptr2 = strchr(++ptr1, '\''); + if (ptr2) + { + *ptr2 = '\0'; + encoding = pg_char_to_encoding(ptr1); + if (encoding < 0) + pg_fatal("unrecognized encoding \"%s\"", + ptr1); + AH->public.encoding = encoding; + setFmtEncoding(encoding); + } + else + pg_fatal("invalid ENCODING item: %s", + te->defn); + + free(defn); +} + +static void +processStdStringsEntry(ArchiveHandle *AH, TocEntry *te) +{ + /* te->defn should have the form SET standard_conforming_strings = 'x'; */ + char *ptr1; + + ptr1 = strchr(te->defn, '\''); + if (ptr1 && strncmp(ptr1, "'on'", 4) == 0) + AH->public.std_strings = true; + else if (ptr1 && strncmp(ptr1, "'off'", 5) == 0) + AH->public.std_strings = false; + else + pg_fatal("invalid STDSTRINGS item: %s", + te->defn); +} + +static void +processSearchPathEntry(ArchiveHandle *AH, TocEntry *te) +{ + /* + * te->defn should contain a command to set search_path. We just copy it + * verbatim for use later. + */ + AH->public.searchpath = pg_strdup(te->defn); +} + +static void +StrictNamesCheck(RestoreOptions *ropt) +{ + const char *missing_name; + + Assert(ropt->strict_names); + + if (ropt->schemaNames.head != NULL) + { + missing_name = simple_string_list_not_touched(&ropt->schemaNames); + if (missing_name != NULL) + pg_fatal("schema \"%s\" not found", missing_name); + } + + if (ropt->tableNames.head != NULL) + { + missing_name = simple_string_list_not_touched(&ropt->tableNames); + if (missing_name != NULL) + pg_fatal("table \"%s\" not found", missing_name); + } + + if (ropt->indexNames.head != NULL) + { + missing_name = simple_string_list_not_touched(&ropt->indexNames); + if (missing_name != NULL) + pg_fatal("index \"%s\" not found", missing_name); + } + + if (ropt->functionNames.head != NULL) + { + missing_name = simple_string_list_not_touched(&ropt->functionNames); + if (missing_name != NULL) + pg_fatal("function \"%s\" not found", missing_name); + } + + if (ropt->triggerNames.head != NULL) + { + missing_name = simple_string_list_not_touched(&ropt->triggerNames); + if (missing_name != NULL) + pg_fatal("trigger \"%s\" not found", missing_name); + } +} + +/* + * Determine whether we want to restore this TOC entry. + * + * Returns 0 if entry should be skipped, or some combination of the + * REQ_SCHEMA, REQ_DATA, and REQ_STATS bits if we want to restore schema, data + * and/or statistics portions of this TOC entry, or REQ_SPECIAL if it's a + * special entry. + */ +static int +_tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH) +{ + int res = REQ_SCHEMA | REQ_DATA; + RestoreOptions *ropt = AH->public.ropt; + + /* + * For binary upgrade mode, dump pg_largeobject_metadata and the + * associated pg_shdepend rows. This is faster to restore than the + * equivalent set of large object commands. + */ + if (ropt->binary_upgrade && strcmp(te->desc, "TABLE DATA") == 0 && + (te->catalogId.oid == LargeObjectMetadataRelationId || + te->catalogId.oid == SharedDependRelationId)) + return REQ_DATA; + + /* These items are treated specially */ + if (strcmp(te->desc, "ENCODING") == 0 || + strcmp(te->desc, "STDSTRINGS") == 0 || + strcmp(te->desc, "SEARCHPATH") == 0) + return REQ_SPECIAL; + + if ((strcmp(te->desc, "STATISTICS DATA") == 0) || + (strcmp(te->desc, "EXTENDED STATISTICS DATA") == 0)) + { + if (!ropt->dumpStatistics) + return 0; + + res = REQ_STATS; + } + + /* + * DATABASE and DATABASE PROPERTIES also have a special rule: they are + * restored in createDB mode, and not restored otherwise, independently of + * all else. + */ + if (strcmp(te->desc, "DATABASE") == 0 || + strcmp(te->desc, "DATABASE PROPERTIES") == 0) + { + if (ropt->createDB) + return REQ_SCHEMA; + else + return 0; + } + + /* + * Global object TOC entries (e.g., ROLEs or TABLESPACEs) must not be + * ignored. + */ + if (strcmp(te->desc, "ROLE") == 0 || + strcmp(te->desc, "ROLE PROPERTIES") == 0 || + strcmp(te->desc, "TABLESPACE") == 0 || + strcmp(te->desc, "DROP_GLOBAL") == 0) + return REQ_SCHEMA; + + /* + * Process exclusions that affect certain classes of TOC entries. + */ + + /* If it's an ACL, maybe ignore it */ + if (ropt->aclsSkip && _tocEntryIsACL(te)) + return 0; + + /* If it's a comment, maybe ignore it */ + if (ropt->no_comments && strcmp(te->desc, "COMMENT") == 0) + return 0; + + /* If it's a policy, maybe ignore it */ + if (ropt->no_policies && + (strcmp(te->desc, "POLICY") == 0 || + strcmp(te->desc, "ROW SECURITY") == 0)) + return 0; + + /* + * If it's a comment on a policy, a publication, or a subscription, maybe + * ignore it. + */ + if (strcmp(te->desc, "COMMENT") == 0) + { + if (ropt->no_policies && + strncmp(te->tag, "POLICY", strlen("POLICY")) == 0) + return 0; + + if (ropt->no_publications && + strncmp(te->tag, "PUBLICATION", strlen("PUBLICATION")) == 0) + return 0; + + if (ropt->no_subscriptions && + strncmp(te->tag, "SUBSCRIPTION", strlen("SUBSCRIPTION")) == 0) + return 0; + + /* + * Comments on global objects (ROLEs or TABLESPACEs) should not be + * skipped, since global objects themselves are never skipped. + */ + if (strncmp(te->tag, "ROLE", strlen("ROLE")) == 0 || + strncmp(te->tag, "TABLESPACE", strlen("TABLESPACE")) == 0) + return REQ_SCHEMA; + } + + /* + * If it's a publication or a table part of a publication, maybe ignore + * it. + */ + if (ropt->no_publications && + (strcmp(te->desc, "PUBLICATION") == 0 || + strcmp(te->desc, "PUBLICATION TABLE") == 0 || + strcmp(te->desc, "PUBLICATION TABLES IN SCHEMA") == 0)) + return 0; + + /* If it's a security label, maybe ignore it */ + if (ropt->no_security_labels && strcmp(te->desc, "SECURITY LABEL") == 0) + return 0; + + /* + * If it's a security label on a publication or a subscription, maybe + * ignore it. + */ + if (strcmp(te->desc, "SECURITY LABEL") == 0) + { + if (ropt->no_publications && + strncmp(te->tag, "PUBLICATION", strlen("PUBLICATION")) == 0) + return 0; + + if (ropt->no_subscriptions && + strncmp(te->tag, "SUBSCRIPTION", strlen("SUBSCRIPTION")) == 0) + return 0; + + /* + * Security labels on global objects (ROLEs or TABLESPACEs) should not + * be skipped, since global objects themselves are never skipped. + */ + if (strncmp(te->tag, "ROLE", strlen("ROLE")) == 0 || + strncmp(te->tag, "TABLESPACE", strlen("TABLESPACE")) == 0) + return REQ_SCHEMA; + } + + /* If it's a subscription, maybe ignore it */ + if (ropt->no_subscriptions && strcmp(te->desc, "SUBSCRIPTION") == 0) + return 0; + + /* Ignore it if section is not to be dumped/restored */ + switch (curSection) + { + case SECTION_PRE_DATA: + if (!(ropt->dumpSections & DUMP_PRE_DATA)) + return 0; + break; + case SECTION_DATA: + if (!(ropt->dumpSections & DUMP_DATA)) + return 0; + break; + case SECTION_POST_DATA: + if (!(ropt->dumpSections & DUMP_POST_DATA)) + return 0; + break; + default: + /* shouldn't get here, really, but ignore it */ + return 0; + } + + /* Ignore it if rejected by idWanted[] (cf. SortTocFromFile) */ + if (ropt->idWanted && !ropt->idWanted[te->dumpId - 1]) + return 0; + + /* + * Check options for selective dump/restore. + */ + if (strcmp(te->desc, "ACL") == 0 || + strcmp(te->desc, "COMMENT") == 0 || + strcmp(te->desc, "STATISTICS DATA") == 0 || + strcmp(te->desc, "SECURITY LABEL") == 0) + { + /* Database properties react to createDB, not selectivity options. */ + if (strncmp(te->tag, "DATABASE ", 9) == 0) + { + if (!ropt->createDB) + return 0; + } + else if (ropt->schemaNames.head != NULL || + ropt->schemaExcludeNames.head != NULL || + ropt->selTypes) + { + /* + * In a selective dump/restore, we want to restore these dependent + * TOC entry types only if their parent object is being restored. + * Without selectivity options, we let through everything in the + * archive. Note there may be such entries with no parent, eg + * non-default ACLs for built-in objects. Also, we make + * per-column ACLs additionally depend on the table's ACL if any + * to ensure correct restore order, so those dependencies should + * be ignored in this check. + * + * This code depends on the parent having been marked already, + * which should be the case; if it isn't, perhaps due to + * SortTocFromFile rearrangement, skipping the dependent entry + * seems prudent anyway. + * + * Ideally we'd handle, eg, table CHECK constraints this way too. + * But it's hard to tell which of their dependencies is the one to + * consult. + */ + bool dumpthis = false; + + for (int i = 0; i < te->nDeps; i++) + { + TocEntry *pte = getTocEntryByDumpId(AH, te->dependencies[i]); + + if (!pte) + continue; /* probably shouldn't happen */ + if (strcmp(pte->desc, "ACL") == 0) + continue; /* ignore dependency on another ACL */ + if (pte->reqs == 0) + continue; /* this object isn't marked, so ignore it */ + /* Found a parent to be dumped, so we want to dump this too */ + dumpthis = true; + break; + } + if (!dumpthis) + return 0; + } + } + else + { + /* Apply selective-restore rules for standalone TOC entries. */ + if (ropt->schemaNames.head != NULL) + { + /* If no namespace is specified, it means all. */ + if (!te->namespace) + return 0; + if (!simple_string_list_member(&ropt->schemaNames, te->namespace)) + return 0; + } + + if (ropt->schemaExcludeNames.head != NULL && + te->namespace && + simple_string_list_member(&ropt->schemaExcludeNames, te->namespace)) + return 0; + + if (ropt->selTypes) + { + if (strcmp(te->desc, "TABLE") == 0 || + strcmp(te->desc, "TABLE DATA") == 0 || + strcmp(te->desc, "VIEW") == 0 || + strcmp(te->desc, "FOREIGN TABLE") == 0 || + strcmp(te->desc, "MATERIALIZED VIEW") == 0 || + strcmp(te->desc, "MATERIALIZED VIEW DATA") == 0 || + strcmp(te->desc, "SEQUENCE") == 0 || + strcmp(te->desc, "SEQUENCE SET") == 0) + { + if (!ropt->selTable) + return 0; + if (ropt->tableNames.head != NULL && + !simple_string_list_member(&ropt->tableNames, te->tag)) + return 0; + } + else if (strcmp(te->desc, "INDEX") == 0) + { + if (!ropt->selIndex) + return 0; + if (ropt->indexNames.head != NULL && + !simple_string_list_member(&ropt->indexNames, te->tag)) + return 0; + } + else if (strcmp(te->desc, "FUNCTION") == 0 || + strcmp(te->desc, "AGGREGATE") == 0 || + strcmp(te->desc, "PROCEDURE") == 0) + { + if (!ropt->selFunction) + return 0; + if (ropt->functionNames.head != NULL && + !simple_string_list_member(&ropt->functionNames, te->tag)) + return 0; + } + else if (strcmp(te->desc, "TRIGGER") == 0) + { + if (!ropt->selTrigger) + return 0; + if (ropt->triggerNames.head != NULL && + !simple_string_list_member(&ropt->triggerNames, te->tag)) + return 0; + } + else + return 0; + } + } + + + /* + * Determine whether the TOC entry contains schema and/or data components, + * and mask off inapplicable REQ bits. If it had a dataDumper, assume + * it's both schema and data. Otherwise it's probably schema-only, but + * there are exceptions. + */ + if (!te->hadDumper) + { + /* + * Special Case: If 'SEQUENCE SET' or anything to do with LOs, then it + * is considered a data entry. We don't need to check for BLOBS or + * old-style BLOB COMMENTS entries, because they will have hadDumper = + * true ... but we do need to check new-style BLOB ACLs, comments, + * etc. + */ + if (strcmp(te->desc, "SEQUENCE SET") == 0 || + strcmp(te->desc, "BLOB") == 0 || + strcmp(te->desc, "BLOB METADATA") == 0 || + (strcmp(te->desc, "ACL") == 0 && + strncmp(te->tag, "LARGE OBJECT", 12) == 0) || + (strcmp(te->desc, "COMMENT") == 0 && + strncmp(te->tag, "LARGE OBJECT", 12) == 0) || + (strcmp(te->desc, "SECURITY LABEL") == 0 && + strncmp(te->tag, "LARGE OBJECT", 12) == 0)) + res = res & REQ_DATA; + else + res = res & ~REQ_DATA; + } + + /* + * If there's no definition command, there's no schema component. Treat + * "load via partition root" comments as not schema. + */ + if (!te->defn || !te->defn[0] || + strncmp(te->defn, "-- load via partition root ", 27) == 0) + res = res & ~REQ_SCHEMA; + + /* + * Special case: type with tag; this is obsolete and we + * always ignore it. + */ + if ((strcmp(te->desc, "") == 0) && (strcmp(te->tag, "Max OID") == 0)) + return 0; + + /* Mask it if we don't want data */ + if (!ropt->dumpData) + { + /* + * The sequence_data option overrides dumpData for SEQUENCE SET. + * + * In binary-upgrade mode, even with dumpData unset, we do not mask + * out large objects. (Only large object definitions, comments and + * other metadata should be generated in binary-upgrade mode, not the + * actual data, but that need not concern us here.) + */ + if (!(ropt->sequence_data && strcmp(te->desc, "SEQUENCE SET") == 0) && + !(ropt->binary_upgrade && + (strcmp(te->desc, "BLOB") == 0 || + strcmp(te->desc, "BLOB METADATA") == 0 || + (strcmp(te->desc, "ACL") == 0 && + strncmp(te->tag, "LARGE OBJECT", 12) == 0) || + (strcmp(te->desc, "COMMENT") == 0 && + strncmp(te->tag, "LARGE OBJECT", 12) == 0) || + (strcmp(te->desc, "SECURITY LABEL") == 0 && + strncmp(te->tag, "LARGE OBJECT", 12) == 0)))) + res = res & (REQ_SCHEMA | REQ_STATS); + } + + /* Mask it if we don't want schema */ + if (!ropt->dumpSchema) + res = res & (REQ_DATA | REQ_STATS); + + return res; +} + +/* + * Identify which pass we should restore this TOC entry in. + * + * See notes with the RestorePass typedef in pg_backup_archiver.h. + */ +static RestorePass +_tocEntryRestorePass(TocEntry *te) +{ + /* "ACL LANGUAGE" was a crock emitted only in PG 7.4 */ + if (strcmp(te->desc, "ACL") == 0 || + strcmp(te->desc, "ACL LANGUAGE") == 0 || + strcmp(te->desc, "DEFAULT ACL") == 0) + return RESTORE_PASS_ACL; + if (strcmp(te->desc, "EVENT TRIGGER") == 0 || + strcmp(te->desc, "MATERIALIZED VIEW DATA") == 0) + return RESTORE_PASS_POST_ACL; + + /* + * Comments and security labels need to be emitted in the same pass as + * their parent objects. ACLs haven't got comments and security labels, + * and neither do matview data objects, but event triggers do. + * (Fortunately, event triggers haven't got ACLs, or we'd need yet another + * weird special case.) + */ + if ((strcmp(te->desc, "COMMENT") == 0 || + strcmp(te->desc, "SECURITY LABEL") == 0) && + strncmp(te->tag, "EVENT TRIGGER ", 14) == 0) + return RESTORE_PASS_POST_ACL; + + /* + * If statistics data is dependent on materialized view data, it must be + * deferred to RESTORE_PASS_POST_ACL. Those entries are already marked as + * SECTION_POST_DATA, and some other stats entries (e.g., index stats) + * will also be marked as SECTION_POST_DATA. Additionally, our lookahead + * code in fetchAttributeStats() assumes that we dump all statistics data + * entries in TOC order. To ensure this assumption holds, we move all + * statistics data entries in SECTION_POST_DATA to RESTORE_PASS_POST_ACL. + */ + if (strcmp(te->desc, "STATISTICS DATA") == 0 && + te->section == SECTION_POST_DATA) + return RESTORE_PASS_POST_ACL; + + /* All else can be handled in the main pass. */ + return RESTORE_PASS_MAIN; +} + +/* + * Identify TOC entries that are ACLs. + * + * Note: it seems worth duplicating some code here to avoid a hard-wired + * assumption that these are exactly the same entries that we restore during + * the RESTORE_PASS_ACL phase. + */ +static bool +_tocEntryIsACL(TocEntry *te) +{ + /* "ACL LANGUAGE" was a crock emitted only in PG 7.4 */ + if (strcmp(te->desc, "ACL") == 0 || + strcmp(te->desc, "ACL LANGUAGE") == 0 || + strcmp(te->desc, "DEFAULT ACL") == 0) + return true; + return false; +} + +/* + * Issue SET commands for parameters that we want to have set the same way + * at all times during execution of a restore script. + */ +static void +_doSetFixedOutputState(ArchiveHandle *AH) +{ + RestoreOptions *ropt = AH->public.ropt; + + /* + * Disable timeouts to allow for slow commands, idle parallel workers, etc + */ + ahprintf(AH, "SET statement_timeout = 0;\n"); + ahprintf(AH, "SET lock_timeout = 0;\n"); + ahprintf(AH, "SET idle_in_transaction_session_timeout = 0;\n"); + ahprintf(AH, "SET transaction_timeout = 0;\n"); + + /* Select the correct character set encoding */ + ahprintf(AH, "SET client_encoding = '%s';\n", + pg_encoding_to_char(AH->public.encoding)); + + /* Select the correct string literal syntax */ + ahprintf(AH, "SET standard_conforming_strings = %s;\n", + AH->public.std_strings ? "on" : "off"); + + /* Select the role to be used during restore */ + if (ropt && ropt->use_role) + ahprintf(AH, "SET ROLE %s;\n", fmtId(ropt->use_role)); + + /* Select the dump-time search_path */ + if (AH->public.searchpath) + ahprintf(AH, "%s", AH->public.searchpath); + + /* Make sure function checking is disabled */ + ahprintf(AH, "SET check_function_bodies = false;\n"); + + /* Ensure that all valid XML data will be accepted */ + ahprintf(AH, "SET xmloption = content;\n"); + + /* Avoid annoying notices etc */ + ahprintf(AH, "SET client_min_messages = warning;\n"); + + /* Adjust row-security state */ + if (ropt && ropt->enable_row_security) + ahprintf(AH, "SET row_security = on;\n"); + else + ahprintf(AH, "SET row_security = off;\n"); + + /* + * In --transaction-size mode, we should always be in a transaction when + * we begin to restore objects. + */ + if (ropt && ropt->txn_size > 0) + { + if (AH->connection) + StartTransaction(&AH->public); + else + ahprintf(AH, "\nBEGIN;\n"); + AH->txnCount = 0; + } + + ahprintf(AH, "\n"); +} + +/* + * Issue a SET SESSION AUTHORIZATION command. Caller is responsible + * for updating state if appropriate. If user is NULL or an empty string, + * the specification DEFAULT will be used. + */ +static void +_doSetSessionAuth(ArchiveHandle *AH, const char *user) +{ + PQExpBuffer cmd = createPQExpBuffer(); + + appendPQExpBufferStr(cmd, "SET SESSION AUTHORIZATION "); + + /* + * SQL requires a string literal here. Might as well be correct. + */ + if (user && *user) + appendStringLiteralAHX(cmd, user, AH); + else + appendPQExpBufferStr(cmd, "DEFAULT"); + appendPQExpBufferChar(cmd, ';'); + + if (RestoringToDB(AH)) + { + PGresult *res; + + res = PQexec(AH->connection, cmd->data); + + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + /* NOT warn_or_exit_horribly... use -O instead to skip this. */ + pg_fatal("could not set session user to \"%s\": %s", + user, PQerrorMessage(AH->connection)); + + PQclear(res); + } + else + ahprintf(AH, "%s\n\n", cmd->data); + + destroyPQExpBuffer(cmd); +} + + +/* + * Issue the commands to connect to the specified database. + * + * If we're currently restoring right into a database, this will + * actually establish a connection. Otherwise it puts a \connect into + * the script output. + */ +static void +_reconnectToDB(ArchiveHandle *AH, const char *dbname) +{ + if (RestoringToDB(AH)) + ReconnectToServer(AH, dbname); + else + { + PQExpBufferData connectbuf; + RestoreOptions *ropt = AH->public.ropt; + + /* + * We must temporarily exit restricted mode for \connect, etc. + * Anything added between this line and the following \restrict must + * be careful to avoid any possible meta-command injection vectors. + */ + ahprintf(AH, "\\unrestrict %s\n", ropt->restrict_key); + + initPQExpBuffer(&connectbuf); + appendPsqlMetaConnect(&connectbuf, dbname); + ahprintf(AH, "%s", connectbuf.data); + termPQExpBuffer(&connectbuf); + + ahprintf(AH, "\\restrict %s\n\n", ropt->restrict_key); + } + + /* + * NOTE: currUser keeps track of what the imaginary session user in our + * script is. It's now effectively reset to the original userID. + */ + free(AH->currUser); + AH->currUser = NULL; + + /* don't assume we still know the output schema, tablespace, etc either */ + free(AH->currSchema); + AH->currSchema = NULL; + + free(AH->currTableAm); + AH->currTableAm = NULL; + + free(AH->currTablespace); + AH->currTablespace = NULL; + + /* re-establish fixed state */ + _doSetFixedOutputState(AH); +} + +/* + * Become the specified user, and update state to avoid redundant commands + * + * NULL or empty argument is taken to mean restoring the session default + */ +static void +_becomeUser(ArchiveHandle *AH, const char *user) +{ + if (!user) + user = ""; /* avoid null pointers */ + + if (AH->currUser && strcmp(AH->currUser, user) == 0) + return; /* no need to do anything */ + + _doSetSessionAuth(AH, user); + + /* + * NOTE: currUser keeps track of what the imaginary session user in our + * script is + */ + free(AH->currUser); + AH->currUser = pg_strdup(user); +} + +/* + * Become the owner of the given TOC entry object. If + * changes in ownership are not allowed, this doesn't do anything. + */ +static void +_becomeOwner(ArchiveHandle *AH, TocEntry *te) +{ + RestoreOptions *ropt = AH->public.ropt; + + if (ropt && (ropt->noOwner || !ropt->use_setsessauth)) + return; + + _becomeUser(AH, te->owner); +} + + +/* + * Issue the commands to select the specified schema as the current schema + * in the target database. + */ +static void +_selectOutputSchema(ArchiveHandle *AH, const char *schemaName) +{ + PQExpBuffer qry; + + /* + * If there was a SEARCHPATH TOC entry, we're supposed to just stay with + * that search_path rather than switching to entry-specific paths. + * Otherwise, it's an old archive that will not restore correctly unless + * we set the search_path as it's expecting. + */ + if (AH->public.searchpath) + return; + + if (!schemaName || *schemaName == '\0' || + (AH->currSchema && strcmp(AH->currSchema, schemaName) == 0)) + return; /* no need to do anything */ + + qry = createPQExpBuffer(); + + appendPQExpBuffer(qry, "SET search_path = %s", + fmtId(schemaName)); + if (strcmp(schemaName, "pg_catalog") != 0) + appendPQExpBufferStr(qry, ", pg_catalog"); + + if (RestoringToDB(AH)) + { + PGresult *res; + + res = PQexec(AH->connection, qry->data); + + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + warn_or_exit_horribly(AH, + "could not set \"search_path\" to \"%s\": %s", + schemaName, PQerrorMessage(AH->connection)); + + PQclear(res); + } + else + ahprintf(AH, "%s;\n\n", qry->data); + + free(AH->currSchema); + AH->currSchema = pg_strdup(schemaName); + + destroyPQExpBuffer(qry); +} + +/* + * Issue the commands to select the specified tablespace as the current one + * in the target database. + */ +static void +_selectTablespace(ArchiveHandle *AH, const char *tablespace) +{ + RestoreOptions *ropt = AH->public.ropt; + PQExpBuffer qry; + const char *want, + *have; + + /* do nothing in --no-tablespaces mode */ + if (ropt->noTablespace) + return; + + have = AH->currTablespace; + want = tablespace; + + /* no need to do anything for non-tablespace object */ + if (!want) + return; + + if (have && strcmp(want, have) == 0) + return; /* no need to do anything */ + + qry = createPQExpBuffer(); + + if (strcmp(want, "") == 0) + { + /* We want the tablespace to be the database's default */ + appendPQExpBufferStr(qry, "SET default_tablespace = ''"); + } + else + { + /* We want an explicit tablespace */ + appendPQExpBuffer(qry, "SET default_tablespace = %s", fmtId(want)); + } + + if (RestoringToDB(AH)) + { + PGresult *res; + + res = PQexec(AH->connection, qry->data); + + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + warn_or_exit_horribly(AH, + "could not set \"default_tablespace\" to %s: %s", + fmtId(want), PQerrorMessage(AH->connection)); + + PQclear(res); + } + else + ahprintf(AH, "%s;\n\n", qry->data); + + free(AH->currTablespace); + AH->currTablespace = pg_strdup(want); + + destroyPQExpBuffer(qry); +} + +/* + * Set the proper default_table_access_method value for the table. + */ +static void +_selectTableAccessMethod(ArchiveHandle *AH, const char *tableam) +{ + RestoreOptions *ropt = AH->public.ropt; + PQExpBuffer cmd; + const char *want, + *have; + + /* do nothing in --no-table-access-method mode */ + if (ropt->noTableAm) + return; + + have = AH->currTableAm; + want = tableam; + + if (!want) + return; + + if (have && strcmp(want, have) == 0) + return; + + cmd = createPQExpBuffer(); + appendPQExpBuffer(cmd, "SET default_table_access_method = %s;", fmtId(want)); + + if (RestoringToDB(AH)) + { + PGresult *res; + + res = PQexec(AH->connection, cmd->data); + + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + warn_or_exit_horribly(AH, + "could not set \"default_table_access_method\": %s", + PQerrorMessage(AH->connection)); + + PQclear(res); + } + else + ahprintf(AH, "%s\n\n", cmd->data); + + destroyPQExpBuffer(cmd); + + free(AH->currTableAm); + AH->currTableAm = pg_strdup(want); +} + +/* + * Set the proper default table access method for a table without storage. + * Currently, this is required only for partitioned tables with a table AM. + */ +static void +_printTableAccessMethodNoStorage(ArchiveHandle *AH, TocEntry *te) +{ + RestoreOptions *ropt = AH->public.ropt; + const char *tableam = te->tableam; + PQExpBuffer cmd; + + /* do nothing in --no-table-access-method mode */ + if (ropt->noTableAm) + return; + + if (!tableam) + return; + + Assert(te->relkind == RELKIND_PARTITIONED_TABLE); + + cmd = createPQExpBuffer(); + + appendPQExpBufferStr(cmd, "ALTER TABLE "); + appendPQExpBuffer(cmd, "%s ", fmtQualifiedId(te->namespace, te->tag)); + appendPQExpBuffer(cmd, "SET ACCESS METHOD %s;", + fmtId(tableam)); + + if (RestoringToDB(AH)) + { + PGresult *res; + + res = PQexec(AH->connection, cmd->data); + + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + warn_or_exit_horribly(AH, + "could not alter table access method: %s", + PQerrorMessage(AH->connection)); + PQclear(res); + } + else + ahprintf(AH, "%s\n\n", cmd->data); + + destroyPQExpBuffer(cmd); +} + +/* + * Extract an object description for a TOC entry, and append it to buf. + * + * This is used for ALTER ... OWNER TO. + * + * If the object type has no owner, do nothing. + */ +static void +_getObjectDescription(PQExpBuffer buf, const TocEntry *te) +{ + const char *type = te->desc; + + /* objects that don't require special decoration */ + if (strcmp(type, "COLLATION") == 0 || + strcmp(type, "CONVERSION") == 0 || + strcmp(type, "DOMAIN") == 0 || + strcmp(type, "FOREIGN TABLE") == 0 || + strcmp(type, "MATERIALIZED VIEW") == 0 || + strcmp(type, "PROPERTY GRAPH") == 0 || + strcmp(type, "SEQUENCE") == 0 || + strcmp(type, "STATISTICS") == 0 || + strcmp(type, "TABLE") == 0 || + strcmp(type, "TEXT SEARCH DICTIONARY") == 0 || + strcmp(type, "TEXT SEARCH CONFIGURATION") == 0 || + strcmp(type, "TYPE") == 0 || + strcmp(type, "VIEW") == 0 || + /* non-schema-specified objects */ + strcmp(type, "DATABASE") == 0 || + strcmp(type, "PROCEDURAL LANGUAGE") == 0 || + strcmp(type, "SCHEMA") == 0 || + strcmp(type, "EVENT TRIGGER") == 0 || + strcmp(type, "FOREIGN DATA WRAPPER") == 0 || + strcmp(type, "SERVER") == 0 || + strcmp(type, "PUBLICATION") == 0 || + strcmp(type, "SUBSCRIPTION") == 0) + { + appendPQExpBuffer(buf, "%s ", type); + if (te->namespace && *te->namespace) + appendPQExpBuffer(buf, "%s.", fmtId(te->namespace)); + appendPQExpBufferStr(buf, fmtId(te->tag)); + } + /* LOs just have a name, but it's numeric so must not use fmtId */ + else if (strcmp(type, "BLOB") == 0) + { + appendPQExpBuffer(buf, "LARGE OBJECT %s", te->tag); + } + + /* + * These object types require additional decoration. Fortunately, the + * information needed is exactly what's in the DROP command. + */ + else if (strcmp(type, "AGGREGATE") == 0 || + strcmp(type, "FUNCTION") == 0 || + strcmp(type, "OPERATOR") == 0 || + strcmp(type, "OPERATOR CLASS") == 0 || + strcmp(type, "OPERATOR FAMILY") == 0 || + strcmp(type, "PROCEDURE") == 0) + { + /* Chop "DROP " off the front and make a modifiable copy */ + char *first = pg_strdup(te->dropStmt + 5); + char *last; + + /* point to last character in string */ + last = first + strlen(first) - 1; + + /* Strip off any ';' or '\n' at the end */ + while (last >= first && (*last == '\n' || *last == ';')) + last--; + *(last + 1) = '\0'; + + appendPQExpBufferStr(buf, first); + + free(first); + return; + } + /* these object types don't have separate owners */ + else if (strcmp(type, "CAST") == 0 || + strcmp(type, "CHECK CONSTRAINT") == 0 || + strcmp(type, "CONSTRAINT") == 0 || + strcmp(type, "DROP_GLOBAL") == 0 || + strcmp(type, "ROLE PROPERTIES") == 0 || + strcmp(type, "ROLE") == 0 || + strcmp(type, "DATABASE PROPERTIES") == 0 || + strcmp(type, "DEFAULT") == 0 || + strcmp(type, "FK CONSTRAINT") == 0 || + strcmp(type, "INDEX") == 0 || + strcmp(type, "RULE") == 0 || + strcmp(type, "TRIGGER") == 0 || + strcmp(type, "ROW SECURITY") == 0 || + strcmp(type, "POLICY") == 0 || + strcmp(type, "USER MAPPING") == 0) + { + /* do nothing */ + } + else + pg_fatal("don't know how to set owner for object type \"%s\"", type); +} + +/* + * Emit the SQL commands to create the object represented by a TOC entry + * + * This now also includes issuing an ALTER OWNER command to restore the + * object's ownership, if wanted. But note that the object's permissions + * will remain at default, until the matching ACL TOC entry is restored. + */ +static void +_printTocEntry(ArchiveHandle *AH, TocEntry *te, const char *pfx) +{ + RestoreOptions *ropt = AH->public.ropt; + + /* + * Select owner, schema, tablespace and default AM as necessary. The + * default access method for partitioned tables is handled after + * generating the object definition, as it requires an ALTER command + * rather than SET. + */ + _becomeOwner(AH, te); + _selectOutputSchema(AH, te->namespace); + _selectTablespace(AH, te->tablespace); + if (te->relkind != RELKIND_PARTITIONED_TABLE) + _selectTableAccessMethod(AH, te->tableam); + + /* Emit header comment for item */ + if (!AH->noTocComments) + { + char *sanitized_name; + char *sanitized_schema; + char *sanitized_owner; + + ahprintf(AH, "--\n"); + if (AH->public.verbose) + { + ahprintf(AH, "-- TOC entry %d (class %u OID %u)\n", + te->dumpId, te->catalogId.tableoid, te->catalogId.oid); + if (te->nDeps > 0) + { + int i; + + ahprintf(AH, "-- Dependencies:"); + for (i = 0; i < te->nDeps; i++) + ahprintf(AH, " %d", te->dependencies[i]); + ahprintf(AH, "\n"); + } + } + + sanitized_name = sanitize_line(te->tag, false); + sanitized_schema = sanitize_line(te->namespace, true); + sanitized_owner = sanitize_line(ropt->noOwner ? NULL : te->owner, true); + + ahprintf(AH, "-- %sName: %s; Type: %s; Schema: %s; Owner: %s", + pfx, sanitized_name, te->desc, sanitized_schema, + sanitized_owner); + + free(sanitized_name); + free(sanitized_schema); + free(sanitized_owner); + + if (te->tablespace && strlen(te->tablespace) > 0 && !ropt->noTablespace) + { + char *sanitized_tablespace; + + sanitized_tablespace = sanitize_line(te->tablespace, false); + ahprintf(AH, "; Tablespace: %s", sanitized_tablespace); + free(sanitized_tablespace); + } + ahprintf(AH, "\n"); + + if (AH->PrintExtraTocPtr != NULL) + AH->PrintExtraTocPtr(AH, te); + ahprintf(AH, "--\n\n"); + } + + /* + * Actually print the definition. Normally we can just print the defn + * string if any, but we have four special cases: + * + * 1. A crude hack for suppressing AUTHORIZATION clause that old pg_dump + * versions put into CREATE SCHEMA. Don't mutate the variant for schema + * "public" that is a comment. We have to do this when --no-owner mode is + * selected. This is ugly, but I see no other good way ... + * + * 2. BLOB METADATA entries need special processing since their defn + * strings are just lists of OIDs, not complete SQL commands. + * + * 3. ACL LARGE OBJECTS entries need special processing because they + * contain only one copy of the ACL GRANT/REVOKE commands, which we must + * apply to each large object listed in the associated BLOB METADATA. + * + * 4. Entries with a defnDumper need to call it to generate the + * definition. This is primarily intended to provide a way to save memory + * for objects that would otherwise need a lot of it (e.g., statistics + * data). + */ + if (ropt->noOwner && + strcmp(te->desc, "SCHEMA") == 0 && strncmp(te->defn, "--", 2) != 0) + { + ahprintf(AH, "CREATE SCHEMA %s;\n\n\n", fmtId(te->tag)); + } + else if (strcmp(te->desc, "BLOB METADATA") == 0) + { + IssueCommandPerBlob(AH, te, "SELECT pg_catalog.lo_create('", "')"); + } + else if (strcmp(te->desc, "ACL") == 0 && + strncmp(te->tag, "LARGE OBJECTS", 13) == 0) + { + IssueACLPerBlob(AH, te); + } + else if (te->defnLen && AH->format != archTar) + { + /* + * If defnLen is set, the defnDumper has already been called for this + * TOC entry. We don't normally expect a defnDumper to be called for + * a TOC entry a second time in _printTocEntry(), but there's an + * exception. The tar format first calls WriteToc(), which scans the + * entire TOC, and then it later calls RestoreArchive() to generate + * restore.sql, which scans the TOC again. There doesn't appear to be + * a good way to prevent a second defnDumper call in this case without + * storing the definition in memory, which defeats the purpose. This + * second defnDumper invocation should generate the same output as the + * first, but even if it doesn't, the worst-case scenario is that + * restore.sql might have different statistics data than the archive. + * + * In all other cases, encountering a TOC entry a second time in + * _printTocEntry() is unexpected, so we fail because one of our + * assumptions must no longer hold true. + * + * XXX This is a layering violation, but the alternative is an awkward + * and complicated callback infrastructure for this special case. This + * might be worth revisiting in the future. + */ + pg_fatal("unexpected TOC entry in _printTocEntry(): %d %s %s", + te->dumpId, te->desc, te->tag); + } + else if (te->defnDumper) + { + char *defn = te->defnDumper((Archive *) AH, te->defnDumperArg, te); + + te->defnLen = ahprintf(AH, "%s\n\n", defn); + pg_free(defn); + } + else if (te->defn && strlen(te->defn) > 0) + { + ahprintf(AH, "%s\n\n", te->defn); + + /* + * If the defn string contains multiple SQL commands, txn_size mode + * should count it as N actions not one. But rather than build a full + * SQL parser, approximate this by counting semicolons. One case + * where that tends to be badly fooled is function definitions, so + * ignore them. (restore_toc_entry will count one action anyway.) + */ + if (ropt->txn_size > 0 && + strcmp(te->desc, "FUNCTION") != 0 && + strcmp(te->desc, "PROCEDURE") != 0) + { + const char *p = te->defn; + int nsemis = 0; + + while ((p = strchr(p, ';')) != NULL) + { + nsemis++; + p++; + } + if (nsemis > 1) + AH->txnCount += nsemis - 1; + } + } + + /* + * If we aren't using SET SESSION AUTH to determine ownership, we must + * instead issue an ALTER OWNER command. Schema "public" is special; when + * a dump emits a comment in lieu of creating it, we use ALTER OWNER even + * when using SET SESSION for all other objects. We assume that anything + * without a DROP command is not a separately ownable object. + */ + if (!ropt->noOwner && + (!ropt->use_setsessauth || + (strcmp(te->desc, "SCHEMA") == 0 && + strncmp(te->defn, "--", 2) == 0)) && + te->owner && strlen(te->owner) > 0 && + te->dropStmt && strlen(te->dropStmt) > 0) + { + if (strcmp(te->desc, "BLOB METADATA") == 0) + { + /* BLOB METADATA needs special code to handle multiple LOs */ + char *cmdEnd = psprintf(" OWNER TO %s", fmtId(te->owner)); + + IssueCommandPerBlob(AH, te, "ALTER LARGE OBJECT ", cmdEnd); + pg_free(cmdEnd); + } + else + { + /* For all other cases, we can use _getObjectDescription */ + PQExpBufferData temp; + + initPQExpBuffer(&temp); + _getObjectDescription(&temp, te); + + /* + * If _getObjectDescription() didn't fill the buffer, then there + * is no owner. + */ + if (temp.data[0]) + ahprintf(AH, "ALTER %s OWNER TO %s;\n\n", + temp.data, fmtId(te->owner)); + termPQExpBuffer(&temp); + } + } + + /* + * Select a partitioned table's default AM, once the table definition has + * been generated. + */ + if (te->relkind == RELKIND_PARTITIONED_TABLE) + _printTableAccessMethodNoStorage(AH, te); + + /* + * If it's an ACL entry, it might contain SET SESSION AUTHORIZATION + * commands, so we can no longer assume we know the current auth setting. + */ + if (_tocEntryIsACL(te)) + { + free(AH->currUser); + AH->currUser = NULL; + } +} + +/* + * Write the file header for a custom-format archive + */ +void +WriteHead(ArchiveHandle *AH) +{ + struct tm crtm; + + AH->WriteBufPtr(AH, "PGDMP", 5); /* Magic code */ + AH->WriteBytePtr(AH, ARCHIVE_MAJOR(AH->version)); + AH->WriteBytePtr(AH, ARCHIVE_MINOR(AH->version)); + AH->WriteBytePtr(AH, ARCHIVE_REV(AH->version)); + AH->WriteBytePtr(AH, AH->intSize); + AH->WriteBytePtr(AH, AH->offSize); + AH->WriteBytePtr(AH, AH->format); + AH->WriteBytePtr(AH, AH->compression_spec.algorithm); + crtm = *localtime(&AH->createDate); + WriteInt(AH, crtm.tm_sec); + WriteInt(AH, crtm.tm_min); + WriteInt(AH, crtm.tm_hour); + WriteInt(AH, crtm.tm_mday); + WriteInt(AH, crtm.tm_mon); + WriteInt(AH, crtm.tm_year); + WriteInt(AH, crtm.tm_isdst); + WriteStr(AH, PQdb(AH->connection)); + WriteStr(AH, AH->public.remoteVersionStr); + WriteStr(AH, PG_VERSION); +} + +void +ReadHead(ArchiveHandle *AH) +{ + char *errmsg; + char vmaj, + vmin, + vrev; + int fmt; + + /* + * If we haven't already read the header, do so. + * + * NB: this code must agree with _discoverArchiveFormat(). Maybe find a + * way to unify the cases? + */ + if (!AH->readHeader) + { + char tmpMag[7]; + + AH->ReadBufPtr(AH, tmpMag, 5); + + if (strncmp(tmpMag, "PGDMP", 5) != 0) + pg_fatal("did not find magic string in file header"); + } + + vmaj = AH->ReadBytePtr(AH); + vmin = AH->ReadBytePtr(AH); + + if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */ + vrev = AH->ReadBytePtr(AH); + else + vrev = 0; + + AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev); + + if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX) + pg_fatal("unsupported version (%d.%d) in file header", + vmaj, vmin); + + AH->intSize = AH->ReadBytePtr(AH); + if (AH->intSize > 32) + pg_fatal("sanity check on integer size (%zu) failed", AH->intSize); + + if (AH->intSize > sizeof(int)) + pg_log_warning("archive was made on a machine with larger integers, some operations might fail"); + + if (AH->version >= K_VERS_1_7) + AH->offSize = AH->ReadBytePtr(AH); + else + AH->offSize = AH->intSize; + + fmt = AH->ReadBytePtr(AH); + + if (AH->format != fmt) + pg_fatal("expected format (%d) differs from format found in file (%d)", + AH->format, fmt); + + if (AH->version >= K_VERS_1_15) + AH->compression_spec.algorithm = AH->ReadBytePtr(AH); + else if (AH->version >= K_VERS_1_2) + { + /* Guess the compression method based on the level */ + if (AH->version < K_VERS_1_4) + AH->compression_spec.level = AH->ReadBytePtr(AH); + else + AH->compression_spec.level = ReadInt(AH); + + if (AH->compression_spec.level != 0) + AH->compression_spec.algorithm = PG_COMPRESSION_GZIP; + } + else + AH->compression_spec.algorithm = PG_COMPRESSION_GZIP; + + errmsg = supports_compression(AH->compression_spec); + if (errmsg) + { + pg_log_warning("archive is compressed, but this installation does not support compression (%s) -- no data will be available", + errmsg); + pg_free(errmsg); + } + + if (AH->version >= K_VERS_1_4) + { + struct tm crtm; + + crtm.tm_sec = ReadInt(AH); + crtm.tm_min = ReadInt(AH); + crtm.tm_hour = ReadInt(AH); + crtm.tm_mday = ReadInt(AH); + crtm.tm_mon = ReadInt(AH); + crtm.tm_year = ReadInt(AH); + crtm.tm_isdst = ReadInt(AH); + + /* + * Newer versions of glibc have mktime() report failure if tm_isdst is + * inconsistent with the prevailing timezone, e.g. tm_isdst = 1 when + * TZ=UTC. This is problematic when restoring an archive under a + * different timezone setting. If we get a failure, try again with + * tm_isdst set to -1 ("don't know"). + * + * XXX with or without this hack, we reconstruct createDate + * incorrectly when the prevailing timezone is different from + * pg_dump's. Next time we bump the archive version, we should flush + * this representation and store a plain seconds-since-the-Epoch + * timestamp instead. + */ + AH->createDate = mktime(&crtm); + if (AH->createDate == (time_t) -1) + { + crtm.tm_isdst = -1; + AH->createDate = mktime(&crtm); + if (AH->createDate == (time_t) -1) + pg_log_warning("invalid creation date in header"); + } + } + + if (AH->version >= K_VERS_1_4) + { + AH->archdbname = ReadStr(AH); + } + + if (AH->version >= K_VERS_1_10) + { + AH->archiveRemoteVersion = ReadStr(AH); + AH->archiveDumpVersion = ReadStr(AH); + } +} + + +/* + * checkSeek + * check to see if ftell/fseek can be performed. + */ +bool +checkSeek(FILE *fp) +{ + pgoff_t tpos; + + /* Check that ftello works on this file */ + tpos = ftello(fp); + if (tpos < 0) + return false; + + /* + * Check that fseeko(SEEK_SET) works, too. NB: we used to try to test + * this with fseeko(fp, 0, SEEK_CUR). But some platforms treat that as a + * successful no-op even on files that are otherwise unseekable. + */ + if (fseeko(fp, tpos, SEEK_SET) != 0) + return false; + + return true; +} + + +/* + * dumpTimestamp + */ +static void +dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim) +{ + char buf[64]; + + if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&tim)) != 0) + ahprintf(AH, "-- %s %s\n\n", msg, buf); +} + +/* + * Main engine for parallel restore. + * + * Parallel restore is done in three phases. In this first phase, + * we'll process all SECTION_PRE_DATA TOC entries that are allowed to be + * processed in the RESTORE_PASS_MAIN pass. (In practice, that's all + * PRE_DATA items other than ACLs.) Entries we can't process now are + * added to the pending_list for later phases to deal with. + */ +static void +restore_toc_entries_prefork(ArchiveHandle *AH, TocEntry *pending_list) +{ + bool skipped_some; + TocEntry *next_work_item; + + pg_log_debug("entering restore_toc_entries_prefork"); + + /* Adjust dependency information */ + fix_dependencies(AH); + + /* + * Do all the early stuff in a single connection in the parent. There's no + * great point in running it in parallel, in fact it will actually run + * faster in a single connection because we avoid all the connection and + * setup overhead. Also, pre-9.2 pg_dump versions were not very good + * about showing all the dependencies of SECTION_PRE_DATA items, so we do + * not risk trying to process them out-of-order. + * + * Stuff that we can't do immediately gets added to the pending_list. + * Note: we don't yet filter out entries that aren't going to be restored. + * They might participate in dependency chains connecting entries that + * should be restored, so we treat them as live until we actually process + * them. + * + * Note: as of 9.2, it should be guaranteed that all PRE_DATA items appear + * before DATA items, and all DATA items before POST_DATA items. That is + * not certain to be true in older archives, though, and in any case use + * of a list file would destroy that ordering (cf. SortTocFromFile). So + * this loop cannot assume that it holds. + */ + AH->restorePass = RESTORE_PASS_MAIN; + skipped_some = false; + for (next_work_item = AH->toc->next; next_work_item != AH->toc; next_work_item = next_work_item->next) + { + bool do_now = true; + + if (next_work_item->section != SECTION_PRE_DATA) + { + /* DATA and POST_DATA items are just ignored for now */ + if (next_work_item->section == SECTION_DATA || + next_work_item->section == SECTION_POST_DATA) + { + do_now = false; + skipped_some = true; + } + else + { + /* + * SECTION_NONE items, such as comments, can be processed now + * if we are still in the PRE_DATA part of the archive. Once + * we've skipped any items, we have to consider whether the + * comment's dependencies are satisfied, so skip it for now. + */ + if (skipped_some) + do_now = false; + } + } + + /* + * Also skip items that need to be forced into later passes. We need + * not set skipped_some in this case, since by assumption no main-pass + * items could depend on these. + */ + if (_tocEntryRestorePass(next_work_item) != RESTORE_PASS_MAIN) + do_now = false; + + if (do_now) + { + /* OK, restore the item and update its dependencies */ + pg_log_info("processing item %d %s %s", + next_work_item->dumpId, + next_work_item->desc, next_work_item->tag); + + (void) restore_toc_entry(AH, next_work_item, false); + + /* Reduce dependencies, but don't move anything to ready_heap */ + reduce_dependencies(AH, next_work_item, NULL); + } + else + { + /* Nope, so add it to pending_list */ + pending_list_append(pending_list, next_work_item); + } + } + + /* + * In --transaction-size mode, we must commit the open transaction before + * dropping the database connection. This also ensures that child workers + * can see the objects we've created so far. + */ + if (AH->public.ropt->txn_size > 0) + CommitTransaction(&AH->public); + + /* + * Now close parent connection in prep for parallel steps. We do this + * mainly to ensure that we don't exceed the specified number of parallel + * connections. + */ + DisconnectDatabase(&AH->public); + + /* blow away any transient state from the old connection */ + free(AH->currUser); + AH->currUser = NULL; + free(AH->currSchema); + AH->currSchema = NULL; + free(AH->currTablespace); + AH->currTablespace = NULL; + free(AH->currTableAm); + AH->currTableAm = NULL; +} + +/* + * Main engine for parallel restore. + * + * Parallel restore is done in three phases. In this second phase, + * we process entries by dispatching them to parallel worker children + * (processes on Unix, threads on Windows), each of which connects + * separately to the database. Inter-entry dependencies are respected, + * and so is the RestorePass multi-pass structure. When we can no longer + * make any entries ready to process, we exit. Normally, there will be + * nothing left to do; but if there is, the third phase will mop up. + */ +static void +restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate, + TocEntry *pending_list) +{ + binaryheap *ready_heap; + TocEntry *next_work_item; + + pg_log_debug("entering restore_toc_entries_parallel"); + + /* Set up ready_heap with enough room for all known TocEntrys */ + ready_heap = binaryheap_allocate(AH->tocCount, + TocEntrySizeCompareBinaryheap, + NULL); + + /* + * The pending_list contains all items that we need to restore. Move all + * items that are available to process immediately into the ready_heap. + * After this setup, the pending list is everything that needs to be done + * but is blocked by one or more dependencies, while the ready heap + * contains items that have no remaining dependencies and are OK to + * process in the current restore pass. + */ + AH->restorePass = RESTORE_PASS_MAIN; + move_to_ready_heap(pending_list, ready_heap, AH->restorePass); + + /* + * main parent loop + * + * Keep going until there is no worker still running AND there is no work + * left to be done. Note invariant: at top of loop, there should always + * be at least one worker available to dispatch a job to. + */ + pg_log_info("entering main parallel loop"); + + for (;;) + { + /* Look for an item ready to be dispatched to a worker */ + next_work_item = pop_next_work_item(ready_heap, pstate); + if (next_work_item != NULL) + { + /* If not to be restored, don't waste time launching a worker */ + if ((next_work_item->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) == 0) + { + pg_log_info("skipping item %d %s %s", + next_work_item->dumpId, + next_work_item->desc, next_work_item->tag); + /* Update its dependencies as though we'd completed it */ + reduce_dependencies(AH, next_work_item, ready_heap); + /* Loop around to see if anything else can be dispatched */ + continue; + } + + pg_log_info("launching item %d %s %s", + next_work_item->dumpId, + next_work_item->desc, next_work_item->tag); + + /* Dispatch to some worker */ + DispatchJobForTocEntry(AH, pstate, next_work_item, ACT_RESTORE, + mark_restore_job_done, ready_heap); + } + else if (IsEveryWorkerIdle(pstate)) + { + /* + * Nothing is ready and no worker is running, so we're done with + * the current pass or maybe with the whole process. + */ + if (AH->restorePass == RESTORE_PASS_LAST) + break; /* No more parallel processing is possible */ + + /* Advance to next restore pass */ + AH->restorePass++; + /* That probably allows some stuff to be made ready */ + move_to_ready_heap(pending_list, ready_heap, AH->restorePass); + /* Loop around to see if anything's now ready */ + continue; + } + else + { + /* + * We have nothing ready, but at least one child is working, so + * wait for some subjob to finish. + */ + } + + /* + * Before dispatching another job, check to see if anything has + * finished. We should check every time through the loop so as to + * reduce dependencies as soon as possible. If we were unable to + * dispatch any job this time through, wait until some worker finishes + * (and, hopefully, unblocks some pending item). If we did dispatch + * something, continue as soon as there's at least one idle worker. + * Note that in either case, there's guaranteed to be at least one + * idle worker when we return to the top of the loop. This ensures we + * won't block inside DispatchJobForTocEntry, which would be + * undesirable: we'd rather postpone dispatching until we see what's + * been unblocked by finished jobs. + */ + WaitForWorkers(AH, pstate, + next_work_item ? WFW_ONE_IDLE : WFW_GOT_STATUS); + } + + /* There should now be nothing in ready_heap. */ + Assert(binaryheap_empty(ready_heap)); + + binaryheap_free(ready_heap); + + pg_log_info("finished main parallel loop"); +} + +/* + * Main engine for parallel restore. + * + * Parallel restore is done in three phases. In this third phase, + * we mop up any remaining TOC entries by processing them serially. + * This phase normally should have nothing to do, but if we've somehow + * gotten stuck due to circular dependencies or some such, this provides + * at least some chance of completing the restore successfully. + */ +static void +restore_toc_entries_postfork(ArchiveHandle *AH, TocEntry *pending_list) +{ + RestoreOptions *ropt = AH->public.ropt; + TocEntry *te; + + pg_log_debug("entering restore_toc_entries_postfork"); + + /* + * Now reconnect the single parent connection. + */ + ConnectDatabaseAhx((Archive *) AH, &ropt->cparams, true); + + /* re-establish fixed state */ + _doSetFixedOutputState(AH); + + /* + * Make sure there is no work left due to, say, circular dependencies, or + * some other pathological condition. If so, do it in the single parent + * connection. We don't sweat about RestorePass ordering; it's likely we + * already violated that. + */ + for (te = pending_list->pending_next; te != pending_list; te = te->pending_next) + { + pg_log_info("processing missed item %d %s %s", + te->dumpId, te->desc, te->tag); + (void) restore_toc_entry(AH, te, false); + } +} + +/* + * Check if te1 has an exclusive lock requirement for an item that te2 also + * requires, whether or not te2's requirement is for an exclusive lock. + */ +static bool +has_lock_conflicts(TocEntry *te1, TocEntry *te2) +{ + int j, + k; + + for (j = 0; j < te1->nLockDeps; j++) + { + for (k = 0; k < te2->nDeps; k++) + { + if (te1->lockDeps[j] == te2->dependencies[k]) + return true; + } + } + return false; +} + + +/* + * Initialize the header of the pending-items list. + * + * This is a circular list with a dummy TocEntry as header, just like the + * main TOC list; but we use separate list links so that an entry can be in + * the main TOC list as well as in the pending list. + */ +static void +pending_list_header_init(TocEntry *l) +{ + l->pending_prev = l->pending_next = l; +} + +/* Append te to the end of the pending-list headed by l */ +static void +pending_list_append(TocEntry *l, TocEntry *te) +{ + te->pending_prev = l->pending_prev; + l->pending_prev->pending_next = te; + l->pending_prev = te; + te->pending_next = l; +} + +/* Remove te from the pending-list */ +static void +pending_list_remove(TocEntry *te) +{ + te->pending_prev->pending_next = te->pending_next; + te->pending_next->pending_prev = te->pending_prev; + te->pending_prev = NULL; + te->pending_next = NULL; +} + + +/* qsort comparator for sorting TocEntries by dataLength */ +static int +TocEntrySizeCompareQsort(const void *p1, const void *p2) +{ + const TocEntry *te1 = *(const TocEntry *const *) p1; + const TocEntry *te2 = *(const TocEntry *const *) p2; + + /* Sort by decreasing dataLength */ + if (te1->dataLength > te2->dataLength) + return -1; + if (te1->dataLength < te2->dataLength) + return 1; + + /* For equal dataLengths, sort by dumpId, just to be stable */ + if (te1->dumpId < te2->dumpId) + return -1; + if (te1->dumpId > te2->dumpId) + return 1; + + return 0; +} + +/* binaryheap comparator for sorting TocEntries by dataLength */ +static int +TocEntrySizeCompareBinaryheap(void *p1, void *p2, void *arg) +{ + /* return opposite of qsort comparator for max-heap */ + return -TocEntrySizeCompareQsort(&p1, &p2); +} + + +/* + * Move all immediately-ready items from pending_list to ready_heap. + * + * Items are considered ready if they have no remaining dependencies and + * they belong in the current restore pass. (See also reduce_dependencies, + * which applies the same logic one-at-a-time.) + */ +static void +move_to_ready_heap(TocEntry *pending_list, + binaryheap *ready_heap, + RestorePass pass) +{ + TocEntry *te; + TocEntry *next_te; + + for (te = pending_list->pending_next; te != pending_list; te = next_te) + { + /* must save list link before possibly removing te from list */ + next_te = te->pending_next; + + if (te->depCount == 0 && + _tocEntryRestorePass(te) == pass) + { + /* Remove it from pending_list ... */ + pending_list_remove(te); + /* ... and add to ready_heap */ + binaryheap_add(ready_heap, te); + } + } +} + +/* + * Find the next work item (if any) that is capable of being run now, + * and remove it from the ready_heap. + * + * Returns the item, or NULL if nothing is runnable. + * + * To qualify, the item must have no remaining dependencies + * and no requirements for locks that are incompatible with + * items currently running. Items in the ready_heap are known to have + * no remaining dependencies, but we have to check for lock conflicts. + */ +static TocEntry * +pop_next_work_item(binaryheap *ready_heap, + ParallelState *pstate) +{ + /* + * Search the ready_heap until we find a suitable item. Note that we do a + * sequential scan through the heap nodes, so even though we will first + * try to choose the highest-priority item, we might end up picking + * something with a much lower priority. However, we expect that we will + * typically be able to pick one of the first few items, which should + * usually have a relatively high priority. + */ + for (int i = 0; i < binaryheap_size(ready_heap); i++) + { + TocEntry *te = (TocEntry *) binaryheap_get_node(ready_heap, i); + bool conflicts = false; + + /* + * Check to see if the item would need exclusive lock on something + * that a currently running item also needs lock on, or vice versa. If + * so, we don't want to schedule them together. + */ + for (int k = 0; k < pstate->numWorkers; k++) + { + TocEntry *running_te = pstate->te[k]; + + if (running_te == NULL) + continue; + if (has_lock_conflicts(te, running_te) || + has_lock_conflicts(running_te, te)) + { + conflicts = true; + break; + } + } + + if (conflicts) + continue; + + /* passed all tests, so this item can run */ + binaryheap_remove_node(ready_heap, i); + return te; + } + + pg_log_debug("no item ready"); + return NULL; +} + + +/* + * Restore a single TOC item in parallel with others + * + * this is run in the worker, i.e. in a thread (Windows) or a separate process + * (everything else). A worker process executes several such work items during + * a parallel backup or restore. Once we terminate here and report back that + * our work is finished, the leader process will assign us a new work item. + */ +int +parallel_restore(ArchiveHandle *AH, TocEntry *te) +{ + int status; + + Assert(AH->connection != NULL); + + /* Count only errors associated with this TOC entry */ + AH->public.n_errors = 0; + + /* Restore the TOC item */ + status = restore_toc_entry(AH, te, true); + + return status; +} + + +/* + * Callback function that's invoked in the leader process after a step has + * been parallel restored. + * + * Update status and reduce the dependency count of any dependent items. + */ +static void +mark_restore_job_done(ArchiveHandle *AH, + TocEntry *te, + int status, + void *callback_data) +{ + binaryheap *ready_heap = (binaryheap *) callback_data; + + pg_log_info("finished item %d %s %s", + te->dumpId, te->desc, te->tag); + + if (status == WORKER_CREATE_DONE) + mark_create_done(AH, te); + else if (status == WORKER_INHIBIT_DATA) + { + inhibit_data_for_failed_table(AH, te); + AH->public.n_errors++; + } + else if (status == WORKER_IGNORED_ERRORS) + AH->public.n_errors++; + else if (status != 0) + pg_fatal("worker process failed: exit code %d", + status); + + reduce_dependencies(AH, te, ready_heap); +} + + +/* + * Process the dependency information into a form useful for parallel restore. + * + * This function takes care of fixing up some missing or badly designed + * dependencies, and then prepares subsidiary data structures that will be + * used in the main parallel-restore logic, including: + * 1. We build the revDeps[] arrays of incoming dependency dumpIds. + * 2. We set up depCount fields that are the number of as-yet-unprocessed + * dependencies for each TOC entry. + * + * We also identify locking dependencies so that we can avoid trying to + * schedule conflicting items at the same time. + */ +static void +fix_dependencies(ArchiveHandle *AH) +{ + TocEntry *te; + int i; + + /* + * Initialize the depCount/revDeps/nRevDeps fields, and make sure the TOC + * items are marked as not being in any parallel-processing list. + */ + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + te->depCount = te->nDeps; + te->revDeps = NULL; + te->nRevDeps = 0; + te->pending_prev = NULL; + te->pending_next = NULL; + } + + /* + * POST_DATA items that are shown as depending on a table need to be + * re-pointed to depend on that table's data, instead. This ensures they + * won't get scheduled until the data has been loaded. + */ + repoint_table_dependencies(AH); + + /* + * Pre-8.4 versions of pg_dump neglected to set up a dependency from BLOB + * COMMENTS to BLOBS. Cope. (We assume there's only one BLOBS and only + * one BLOB COMMENTS in such files.) + */ + if (AH->version < K_VERS_1_11) + { + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if (strcmp(te->desc, "BLOB COMMENTS") == 0 && te->nDeps == 0) + { + TocEntry *te2; + + for (te2 = AH->toc->next; te2 != AH->toc; te2 = te2->next) + { + if (strcmp(te2->desc, "BLOBS") == 0) + { + te->dependencies = pg_malloc_object(DumpId); + te->dependencies[0] = te2->dumpId; + te->nDeps++; + te->depCount++; + break; + } + } + break; + } + } + } + + /* + * At this point we start to build the revDeps reverse-dependency arrays, + * so all changes of dependencies must be complete. + */ + + /* + * Count the incoming dependencies for each item. Also, it is possible + * that the dependencies list items that are not in the archive at all + * (that should not happen in 9.2 and later, but is highly likely in older + * archives). Subtract such items from the depCounts. + */ + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + for (i = 0; i < te->nDeps; i++) + { + DumpId depid = te->dependencies[i]; + + if (depid <= AH->maxDumpId && AH->tocsByDumpId[depid] != NULL) + AH->tocsByDumpId[depid]->nRevDeps++; + else + te->depCount--; + } + } + + /* + * Allocate space for revDeps[] arrays, and reset nRevDeps so we can use + * it as a counter below. + */ + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if (te->nRevDeps > 0) + te->revDeps = pg_malloc_array(DumpId, te->nRevDeps); + te->nRevDeps = 0; + } + + /* + * Build the revDeps[] arrays of incoming-dependency dumpIds. This had + * better agree with the loops above. + */ + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + for (i = 0; i < te->nDeps; i++) + { + DumpId depid = te->dependencies[i]; + + if (depid <= AH->maxDumpId && AH->tocsByDumpId[depid] != NULL) + { + TocEntry *otherte = AH->tocsByDumpId[depid]; + + otherte->revDeps[otherte->nRevDeps++] = te->dumpId; + } + } + } + + /* + * Lastly, work out the locking dependencies. + */ + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + te->lockDeps = NULL; + te->nLockDeps = 0; + identify_locking_dependencies(AH, te); + } +} + +/* + * Change dependencies on table items to depend on table data items instead, + * but only in POST_DATA items. + * + * Also, for any item having such dependency(s), set its dataLength to the + * largest dataLength of the table data items it depends on. This ensures + * that parallel restore will prioritize larger jobs (index builds, FK + * constraint checks, etc) over smaller ones, avoiding situations where we + * end a restore with only one active job working on a large table. + */ +static void +repoint_table_dependencies(ArchiveHandle *AH) +{ + TocEntry *te; + int i; + DumpId olddep; + + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + if (te->section != SECTION_POST_DATA) + continue; + for (i = 0; i < te->nDeps; i++) + { + olddep = te->dependencies[i]; + if (olddep <= AH->maxDumpId && + AH->tableDataId[olddep] != 0) + { + DumpId tabledataid = AH->tableDataId[olddep]; + TocEntry *tabledatate = AH->tocsByDumpId[tabledataid]; + + te->dependencies[i] = tabledataid; + te->dataLength = Max(te->dataLength, tabledatate->dataLength); + pg_log_debug("transferring dependency %d -> %d to %d", + te->dumpId, olddep, tabledataid); + } + } + } +} + +/* + * Identify which objects we'll need exclusive lock on in order to restore + * the given TOC entry (*other* than the one identified by the TOC entry + * itself). Record their dump IDs in the entry's lockDeps[] array. + */ +static void +identify_locking_dependencies(ArchiveHandle *AH, TocEntry *te) +{ + DumpId *lockids; + int nlockids; + int i; + + /* + * We only care about this for POST_DATA items. PRE_DATA items are not + * run in parallel, and DATA items are all independent by assumption. + */ + if (te->section != SECTION_POST_DATA) + return; + + /* Quick exit if no dependencies at all */ + if (te->nDeps == 0) + return; + + /* + * Most POST_DATA items are ALTER TABLEs or some moral equivalent of that, + * and hence require exclusive lock. However, we know that CREATE INDEX + * does not. (Maybe someday index-creating CONSTRAINTs will fall in that + * category too ... but today is not that day.) + */ + if (strcmp(te->desc, "INDEX") == 0) + return; + + /* + * We assume the entry requires exclusive lock on each TABLE or TABLE DATA + * item listed among its dependencies. Originally all of these would have + * been TABLE items, but repoint_table_dependencies would have repointed + * them to the TABLE DATA items if those are present (which they might not + * be, eg in a schema-only dump). Note that all of the entries we are + * processing here are POST_DATA; otherwise there might be a significant + * difference between a dependency on a table and a dependency on its + * data, so that closer analysis would be needed here. + */ + lockids = pg_malloc_array(DumpId, te->nDeps); + nlockids = 0; + for (i = 0; i < te->nDeps; i++) + { + DumpId depid = te->dependencies[i]; + + if (depid <= AH->maxDumpId && AH->tocsByDumpId[depid] != NULL && + ((strcmp(AH->tocsByDumpId[depid]->desc, "TABLE DATA") == 0) || + strcmp(AH->tocsByDumpId[depid]->desc, "TABLE") == 0)) + lockids[nlockids++] = depid; + } + + if (nlockids == 0) + { + free(lockids); + return; + } + + te->lockDeps = pg_realloc_array(lockids, DumpId, nlockids); + te->nLockDeps = nlockids; +} + +/* + * Remove the specified TOC entry from the depCounts of items that depend on + * it, thereby possibly making them ready-to-run. Any pending item that + * becomes ready should be moved to the ready_heap, if that's provided. + */ +static void +reduce_dependencies(ArchiveHandle *AH, TocEntry *te, + binaryheap *ready_heap) +{ + int i; + + pg_log_debug("reducing dependencies for %d", te->dumpId); + + for (i = 0; i < te->nRevDeps; i++) + { + TocEntry *otherte = AH->tocsByDumpId[te->revDeps[i]]; + + Assert(otherte->depCount > 0); + otherte->depCount--; + + /* + * It's ready if it has no remaining dependencies, and it belongs in + * the current restore pass, and it is currently a member of the + * pending list (that check is needed to prevent double restore in + * some cases where a list-file forces out-of-order restoring). + * However, if ready_heap == NULL then caller doesn't want any list + * memberships changed. + */ + if (otherte->depCount == 0 && + _tocEntryRestorePass(otherte) == AH->restorePass && + otherte->pending_prev != NULL && + ready_heap != NULL) + { + /* Remove it from pending list ... */ + pending_list_remove(otherte); + /* ... and add to ready_heap */ + binaryheap_add(ready_heap, otherte); + } + } +} + +/* + * Set the created flag on the DATA member corresponding to the given + * TABLE member + */ +static void +mark_create_done(ArchiveHandle *AH, TocEntry *te) +{ + if (AH->tableDataId[te->dumpId] != 0) + { + TocEntry *ted = AH->tocsByDumpId[AH->tableDataId[te->dumpId]]; + + ted->created = true; + } +} + +/* + * Mark the DATA member corresponding to the given TABLE member + * as not wanted + */ +static void +inhibit_data_for_failed_table(ArchiveHandle *AH, TocEntry *te) +{ + pg_log_info("table \"%s\" could not be created, will not restore its data", + te->tag); + + if (AH->tableDataId[te->dumpId] != 0) + { + TocEntry *ted = AH->tocsByDumpId[AH->tableDataId[te->dumpId]]; + + ted->reqs = 0; + } +} + +/* + * Clone and de-clone routines used in parallel restoration. + * + * Enough of the structure is cloned to ensure that there is no + * conflict between different threads each with their own clone. + */ +ArchiveHandle * +CloneArchive(ArchiveHandle *AH) +{ + ArchiveHandle *clone; + + /* Make a "flat" copy */ + clone = pg_malloc_object(ArchiveHandle); + memcpy(clone, AH, sizeof(ArchiveHandle)); + + /* Likewise flat-copy the RestoreOptions, so we can alter them locally */ + clone->public.ropt = pg_malloc_object(RestoreOptions); + memcpy(clone->public.ropt, AH->public.ropt, sizeof(RestoreOptions)); + + /* Handle format-independent fields */ + memset(&(clone->sqlparse), 0, sizeof(clone->sqlparse)); + + /* The clone will have its own connection, so disregard connection state */ + clone->connection = NULL; + clone->connCancel = NULL; + clone->currUser = NULL; + clone->currSchema = NULL; + clone->currTableAm = NULL; + clone->currTablespace = NULL; + + /* savedPassword must be local in case we change it while connecting */ + if (clone->savedPassword) + clone->savedPassword = pg_strdup(clone->savedPassword); + + /* clone has its own error count, too */ + clone->public.n_errors = 0; + + /* clones should not share lo_buf */ + clone->lo_buf = NULL; + + /* + * Clone connections disregard --transaction-size; they must commit after + * each command so that the results are immediately visible to other + * workers. + */ + clone->public.ropt->txn_size = 0; + + /* + * Connect our new clone object to the database, using the same connection + * parameters used for the original connection. + */ + ConnectDatabaseAhx((Archive *) clone, &clone->public.ropt->cparams, true); + + /* re-establish fixed state */ + if (AH->mode == archModeRead) + _doSetFixedOutputState(clone); + /* in write case, setupDumpWorker will fix up connection state */ + + /* Let the format-specific code have a chance too */ + clone->ClonePtr(clone); + + Assert(clone->connection != NULL); + return clone; +} + +/* + * Release clone-local storage. + * + * Note: we assume any clone-local connection was already closed. + */ +void +DeCloneArchive(ArchiveHandle *AH) +{ + /* Should not have an open database connection */ + Assert(AH->connection == NULL); + + /* Clear format-specific state */ + AH->DeClonePtr(AH); + + /* Clear state allocated by CloneArchive */ + if (AH->sqlparse.curCmd) + destroyPQExpBuffer(AH->sqlparse.curCmd); + + /* Clear any connection-local state */ + free(AH->currUser); + free(AH->currSchema); + free(AH->currTablespace); + free(AH->currTableAm); + free(AH->savedPassword); + + free(AH); +} diff --git a/src/compat/19/pg_dump/pg_backup_archiver.h b/src/compat/19/pg_dump/pg_backup_archiver.h new file mode 100644 index 00000000..9c3aca65 --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_archiver.h @@ -0,0 +1,477 @@ +/*------------------------------------------------------------------------- + * + * pg_backup_archiver.h + * + * Private interface to the pg_dump archiver routines. + * It is NOT intended that these routines be called by any + * dumper directly. + * + * See the headers to pg_restore for more details. + * + * Copyright (c) 2000, Philip Warner + * Rights are granted to use this software in any way so long + * as this notice is not removed. + * + * The author is not responsible for loss or damages that may + * result from its use. + * + * + * IDENTIFICATION + * src/bin/pg_dump/pg_backup_archiver.h + * + *------------------------------------------------------------------------- + */ +#ifndef __PG_BACKUP_ARCHIVE__ +#define __PG_BACKUP_ARCHIVE__ + +#include + +#include "libpq-fe.h" +#include "pg_backup.h" +#include "pqexpbuffer.h" + +#define LOBBUFSIZE 16384 + +/* Data block types */ +#define BLK_DATA 1 +#define BLK_BLOBS 3 + +/* Encode version components into a convenient integer */ +#define MAKE_ARCHIVE_VERSION(major, minor, rev) (((major) * 256 + (minor)) * 256 + (rev)) + +#define ARCHIVE_MAJOR(version) (((version) >> 16) & 255) +#define ARCHIVE_MINOR(version) (((version) >> 8) & 255) +#define ARCHIVE_REV(version) (((version) ) & 255) + +/* Historical version numbers (checked in code) */ +#define K_VERS_1_0 MAKE_ARCHIVE_VERSION(1, 0, 0) +#define K_VERS_1_2 MAKE_ARCHIVE_VERSION(1, 2, 0) /* Allow No ZLIB */ +#define K_VERS_1_3 MAKE_ARCHIVE_VERSION(1, 3, 0) /* BLOBS */ +#define K_VERS_1_4 MAKE_ARCHIVE_VERSION(1, 4, 0) /* Date & name in header */ +#define K_VERS_1_5 MAKE_ARCHIVE_VERSION(1, 5, 0) /* Handle dependencies */ +#define K_VERS_1_6 MAKE_ARCHIVE_VERSION(1, 6, 0) /* Schema field in TOCs */ +#define K_VERS_1_7 MAKE_ARCHIVE_VERSION(1, 7, 0) /* File Offset size in + * header */ +#define K_VERS_1_8 MAKE_ARCHIVE_VERSION(1, 8, 0) /* change interpretation + * of ID numbers and + * dependencies */ +#define K_VERS_1_9 MAKE_ARCHIVE_VERSION(1, 9, 0) /* add default_with_oids + * tracking */ +#define K_VERS_1_10 MAKE_ARCHIVE_VERSION(1, 10, 0) /* add tablespace */ +#define K_VERS_1_11 MAKE_ARCHIVE_VERSION(1, 11, 0) /* add toc section + * indicator */ +#define K_VERS_1_12 MAKE_ARCHIVE_VERSION(1, 12, 0) /* add separate BLOB + * entries */ +#define K_VERS_1_13 MAKE_ARCHIVE_VERSION(1, 13, 0) /* change search_path + * behavior */ +#define K_VERS_1_14 MAKE_ARCHIVE_VERSION(1, 14, 0) /* add tableam */ +#define K_VERS_1_15 MAKE_ARCHIVE_VERSION(1, 15, 0) /* add + * compression_algorithm + * in header */ +#define K_VERS_1_16 MAKE_ARCHIVE_VERSION(1, 16, 0) /* BLOB METADATA entries + * and multiple BLOBS, + * relkind */ + +/* Current archive version number (the format we can output) */ +#define K_VERS_MAJOR 1 +#define K_VERS_MINOR 16 +#define K_VERS_REV 0 +#define K_VERS_SELF MAKE_ARCHIVE_VERSION(K_VERS_MAJOR, K_VERS_MINOR, K_VERS_REV) + +/* Newest format we can read */ +#define K_VERS_MAX MAKE_ARCHIVE_VERSION(K_VERS_MAJOR, K_VERS_MINOR, 255) + + +/* Flags to indicate disposition of offsets stored in files */ +#define K_OFFSET_POS_NOT_SET 1 +#define K_OFFSET_POS_SET 2 +#define K_OFFSET_NO_DATA 3 + +/* + * Special exit values from worker children. We reserve 0 for normal + * success; 1 and other small values should be interpreted as crashes. + */ +#define WORKER_OK 0 +#define WORKER_CREATE_DONE 10 +#define WORKER_INHIBIT_DATA 11 +#define WORKER_IGNORED_ERRORS 12 + +typedef struct _archiveHandle ArchiveHandle; +typedef struct _tocEntry TocEntry; +struct ParallelState; + +#define READ_ERROR_EXIT(fd) \ + do { \ + if (feof(fd)) \ + pg_fatal("could not read from input file: end of file"); \ + else \ + pg_fatal("could not read from input file: %m"); \ + } while (0) + +#define WRITE_ERROR_EXIT \ + do { \ + pg_fatal("could not write to output file: %m"); \ + } while (0) + +typedef enum T_Action +{ + ACT_DUMP, + ACT_RESTORE, +} T_Action; + +typedef void (*ClosePtrType) (ArchiveHandle *AH); +typedef void (*ReopenPtrType) (ArchiveHandle *AH); +typedef void (*ArchiveEntryPtrType) (ArchiveHandle *AH, TocEntry *te); + +typedef void (*StartDataPtrType) (ArchiveHandle *AH, TocEntry *te); +typedef void (*WriteDataPtrType) (ArchiveHandle *AH, const void *data, size_t dLen); +typedef void (*EndDataPtrType) (ArchiveHandle *AH, TocEntry *te); + +typedef void (*StartLOsPtrType) (ArchiveHandle *AH, TocEntry *te); +typedef void (*StartLOPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid); +typedef void (*EndLOPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid); +typedef void (*EndLOsPtrType) (ArchiveHandle *AH, TocEntry *te); + +typedef int (*WriteBytePtrType) (ArchiveHandle *AH, const int i); +typedef int (*ReadBytePtrType) (ArchiveHandle *AH); +typedef void (*WriteBufPtrType) (ArchiveHandle *AH, const void *c, size_t len); +typedef void (*ReadBufPtrType) (ArchiveHandle *AH, void *buf, size_t len); +typedef void (*WriteExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te); +typedef void (*ReadExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te); +typedef void (*PrintExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te); +typedef void (*PrintTocDataPtrType) (ArchiveHandle *AH, TocEntry *te); + +typedef void (*PrepParallelRestorePtrType) (ArchiveHandle *AH); +typedef void (*ClonePtrType) (ArchiveHandle *AH); +typedef void (*DeClonePtrType) (ArchiveHandle *AH); + +typedef int (*WorkerJobDumpPtrType) (ArchiveHandle *AH, TocEntry *te); +typedef int (*WorkerJobRestorePtrType) (ArchiveHandle *AH, TocEntry *te); + +typedef size_t (*CustomOutPtrType) (ArchiveHandle *AH, const void *buf, size_t len); + +typedef enum +{ + SQL_SCAN = 0, /* normal */ + SQL_IN_SINGLE_QUOTE, /* '...' literal */ + SQL_IN_DOUBLE_QUOTE, /* "..." identifier */ +} sqlparseState; + +typedef struct +{ + sqlparseState state; /* see above */ + bool backSlash; /* next char is backslash quoted? */ + PQExpBuffer curCmd; /* incomplete line (NULL if not created) */ +} sqlparseInfo; + +typedef enum +{ + STAGE_NONE = 0, + STAGE_INITIALIZING, + STAGE_PROCESSING, + STAGE_FINALIZING, +} ArchiverStage; + +typedef enum +{ + OUTPUT_SQLCMDS = 0, /* emitting general SQL commands */ + OUTPUT_COPYDATA, /* writing COPY data */ + OUTPUT_OTHERDATA, /* writing data as INSERT commands */ +} ArchiverOutput; + +/* + * For historical reasons, ACL items are interspersed with everything else in + * a dump file's TOC; typically they're right after the object they're for. + * However, we need to restore data before ACLs, as otherwise a read-only + * table (ie one where the owner has revoked her own INSERT privilege) causes + * data restore failures. On the other hand, matview REFRESH commands should + * come out after ACLs, as otherwise non-superuser-owned matviews might not + * be able to execute. (If the permissions at the time of dumping would not + * allow a REFRESH, too bad; we won't fix that for you.) We also want event + * triggers to be restored after ACLs, so that they can't mess those up. + * + * These considerations force us to make three passes over the TOC, + * restoring the appropriate subset of items in each pass. We assume that + * the dependency sort resulted in an appropriate ordering of items within + * each subset. + * + * XXX This mechanism should be superseded by tracking dependencies on ACLs + * properly; but we'll still need it for old dump files even after that. + */ +typedef enum +{ + RESTORE_PASS_MAIN = 0, /* Main pass (most TOC item types) */ + RESTORE_PASS_ACL, /* ACL item types */ + RESTORE_PASS_POST_ACL, /* Event trigger and matview refresh items */ + +#define RESTORE_PASS_LAST RESTORE_PASS_POST_ACL +} RestorePass; + +#define REQ_SCHEMA 0x01 /* want schema */ +#define REQ_DATA 0x02 /* want data */ +#define REQ_STATS 0x04 +#define REQ_SPECIAL 0x08 /* for special TOC entries */ + +struct _archiveHandle +{ + Archive public; /* Public part of archive */ + int version; /* Version of file */ + + char *archiveRemoteVersion; /* When reading an archive, the + * version of the dumped DB */ + char *archiveDumpVersion; /* When reading an archive, the version of + * the dumper */ + + size_t intSize; /* Size of an integer in the archive */ + size_t offSize; /* Size of a file offset in the archive - + * Added V1.7 */ + ArchiveFormat format; /* Archive format */ + + sqlparseInfo sqlparse; /* state for parsing INSERT data */ + + time_t createDate; /* Date archive created */ + + /* + * Fields used when discovering archive format. For tar format, we load + * the first block into the lookahead buffer, and verify that it looks + * like a tar header. The tar module must then consume bytes from the + * lookahead buffer before reading any more from the file. For custom + * format, we load only the "PGDMP" marker into the buffer, and then set + * readHeader after confirming it matches. The buffer is vestigial in + * this case, as the subsequent code just checks readHeader and doesn't + * examine the buffer. + */ + int readHeader; /* Set if we already read "PGDMP" marker */ + char *lookahead; /* Buffer used when reading header to discover + * format */ + size_t lookaheadSize; /* Allocated size of buffer */ + size_t lookaheadLen; /* Length of valid data in lookahead */ + size_t lookaheadPos; /* Current read position in lookahead buffer */ + + ArchiveEntryPtrType ArchiveEntryPtr; /* Called for each metadata object */ + StartDataPtrType StartDataPtr; /* Called when table data is about to be + * dumped */ + WriteDataPtrType WriteDataPtr; /* Called to send some table data to the + * archive */ + EndDataPtrType EndDataPtr; /* Called when table data dump is finished */ + WriteBytePtrType WriteBytePtr; /* Write a byte to output */ + ReadBytePtrType ReadBytePtr; /* Read a byte from an archive */ + WriteBufPtrType WriteBufPtr; /* Write a buffer of output to the archive */ + ReadBufPtrType ReadBufPtr; /* Read a buffer of input from the archive */ + ClosePtrType ClosePtr; /* Close the archive */ + ReopenPtrType ReopenPtr; /* Reopen the archive */ + WriteExtraTocPtrType WriteExtraTocPtr; /* Write extra TOC entry data + * associated with the current + * archive format */ + ReadExtraTocPtrType ReadExtraTocPtr; /* Read extra info associated with + * archive format */ + PrintExtraTocPtrType PrintExtraTocPtr; /* Extra TOC info for format */ + PrintTocDataPtrType PrintTocDataPtr; + + StartLOsPtrType StartLOsPtr; + EndLOsPtrType EndLOsPtr; + StartLOPtrType StartLOPtr; + EndLOPtrType EndLOPtr; + + SetupWorkerPtrType SetupWorkerPtr; + WorkerJobDumpPtrType WorkerJobDumpPtr; + WorkerJobRestorePtrType WorkerJobRestorePtr; + + PrepParallelRestorePtrType PrepParallelRestorePtr; + ClonePtrType ClonePtr; /* Clone format-specific fields */ + DeClonePtrType DeClonePtr; /* Clean up cloned fields */ + + CustomOutPtrType CustomOutPtr; /* Alternative script output routine */ + + /* Stuff for direct DB connection */ + char *archdbname; /* DB name *read* from archive */ + char *savedPassword; /* password for ropt->username, if known */ + char *use_role; + PGconn *connection; + /* If connCancel isn't NULL, SIGINT handler will send a cancel */ + PGcancel *volatile connCancel; + + int connectToDB; /* Flag to indicate if direct DB connection is + * required */ + ArchiverOutput outputKind; /* Flag for what we're currently writing */ + bool pgCopyIn; /* Currently in libpq 'COPY IN' mode. */ + + int loFd; + bool writingLO; + int loCount; /* # of LOs restored */ + + char *fSpec; /* Archive File Spec */ + FILE *FH; /* General purpose file handle */ + void *OF; /* Output file */ + + struct _tocEntry *toc; /* Header of circular list of TOC entries */ + int tocCount; /* Number of TOC entries */ + DumpId maxDumpId; /* largest DumpId among all TOC entries */ + + /* arrays created after the TOC list is complete: */ + struct _tocEntry **tocsByDumpId; /* TOCs indexed by dumpId */ + DumpId *tableDataId; /* TABLE DATA ids, indexed by table dumpId */ + + struct _tocEntry *currToc; /* Used when dumping data */ + pg_compress_specification compression_spec; /* Requested specification for + * compression */ + bool dosync; /* data requested to be synced on sight */ + DataDirSyncMethod sync_method; + ArchiveMode mode; /* File mode - r or w */ + void *formatData; /* Header data specific to file format */ + + /* these vars track state to avoid sending redundant SET commands */ + char *currUser; /* current username, or NULL if unknown */ + char *currSchema; /* current schema, or NULL */ + char *currTablespace; /* current tablespace, or NULL */ + char *currTableAm; /* current table access method, or NULL */ + + /* in --transaction-size mode, this counts objects emitted in cur xact */ + int txnCount; + + void *lo_buf; + size_t lo_buf_used; + size_t lo_buf_size; + + int noTocComments; + ArchiverStage stage; + ArchiverStage lastErrorStage; + RestorePass restorePass; /* used only during parallel restore */ + struct _tocEntry *currentTE; + struct _tocEntry *lastErrorTE; +}; + + +typedef char *(*DefnDumperPtr) (Archive *AH, const void *userArg, const TocEntry *te); +typedef int (*DataDumperPtr) (Archive *AH, const void *userArg); + +struct _tocEntry +{ + struct _tocEntry *prev; + struct _tocEntry *next; + CatalogId catalogId; + DumpId dumpId; + teSection section; + bool hadDumper; /* Archiver was passed a dumper routine (used + * in restore) */ + char *tag; /* index tag */ + char *namespace; /* null or empty string if not in a schema */ + char *tablespace; /* null if not in a tablespace; empty string + * means use database default */ + char *tableam; /* table access method, only for TABLE tags */ + char relkind; /* relation kind, only for TABLE tags */ + char *owner; + char *desc; + char *defn; + char *dropStmt; + char *copyStmt; + DumpId *dependencies; /* dumpIds of objects this one depends on */ + int nDeps; /* number of dependencies */ + + DataDumperPtr dataDumper; /* Routine to dump data for object */ + const void *dataDumperArg; /* Arg for above routine */ + void *formatData; /* TOC Entry data specific to file format */ + + DefnDumperPtr defnDumper; /* routine to dump definition statement */ + const void *defnDumperArg; /* arg for above routine */ + size_t defnLen; /* length of dumped definition */ + + /* working state while dumping/restoring */ + pgoff_t dataLength; /* item's data size; 0 if none or unknown */ + int reqs; /* do we need schema and/or data of object + * (REQ_* bit mask) */ + bool created; /* set for DATA member if TABLE was created */ + + /* working state (needed only for parallel restore) */ + struct _tocEntry *pending_prev; /* list links for pending-items list; */ + struct _tocEntry *pending_next; /* NULL if not in that list */ + int depCount; /* number of dependencies not yet restored */ + DumpId *revDeps; /* dumpIds of objects depending on this one */ + int nRevDeps; /* number of such dependencies */ + DumpId *lockDeps; /* dumpIds of objects this one needs lock on */ + int nLockDeps; /* number of such dependencies */ +}; + +extern int parallel_restore(ArchiveHandle *AH, TocEntry *te); +extern void on_exit_close_archive(Archive *AHX); +extern void replace_on_exit_close_archive(Archive *AHX); + +extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...) pg_attribute_printf(2, 3); + +/* Options for ArchiveEntry */ +typedef struct _archiveOpts +{ + const char *tag; + const char *namespace; + const char *tablespace; + const char *tableam; + char relkind; + const char *owner; + const char *description; + teSection section; + const char *createStmt; + const char *dropStmt; + const char *copyStmt; + const DumpId *deps; + int nDeps; + DataDumperPtr dumpFn; + const void *dumpArg; + DefnDumperPtr defnFn; + const void *defnArg; +} ArchiveOpts; +#define ARCHIVE_OPTS(...) &(ArchiveOpts){__VA_ARGS__} +/* Called to add a TOC entry */ +extern TocEntry *ArchiveEntry(Archive *AHX, CatalogId catalogId, + DumpId dumpId, ArchiveOpts *opts); + +extern void WriteHead(ArchiveHandle *AH); +extern void ReadHead(ArchiveHandle *AH); +extern void WriteToc(ArchiveHandle *AH); +extern void ReadToc(ArchiveHandle *AH); +extern void WriteDataChunks(ArchiveHandle *AH, struct ParallelState *pstate); +extern void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te); +extern ArchiveHandle *CloneArchive(ArchiveHandle *AH); +extern void DeCloneArchive(ArchiveHandle *AH); + +extern int TocIDRequired(ArchiveHandle *AH, DumpId id); +TocEntry *getTocEntryByDumpId(ArchiveHandle *AH, DumpId id); +extern bool checkSeek(FILE *fp); + +#define appendStringLiteralAHX(buf,str,AH) \ + appendStringLiteral(buf, str, (AH)->public.encoding, (AH)->public.std_strings) + +#define appendByteaLiteralAHX(buf,str,len,AH) \ + appendByteaLiteral(buf, str, len, (AH)->public.std_strings) + +/* + * Mandatory routines for each supported format + */ + +extern size_t WriteInt(ArchiveHandle *AH, int i); +extern int ReadInt(ArchiveHandle *AH); +extern char *ReadStr(ArchiveHandle *AH); +extern size_t WriteStr(ArchiveHandle *AH, const char *c); + +int ReadOffset(ArchiveHandle *, pgoff_t *); +size_t WriteOffset(ArchiveHandle *, pgoff_t, int); + +extern void StartRestoreLOs(ArchiveHandle *AH); +extern void StartRestoreLO(ArchiveHandle *AH, Oid oid, bool drop); +extern void EndRestoreLO(ArchiveHandle *AH, Oid oid); +extern void EndRestoreLOs(ArchiveHandle *AH); + +extern void InitArchiveFmt_Custom(ArchiveHandle *AH); +extern void InitArchiveFmt_Null(ArchiveHandle *AH); +extern void InitArchiveFmt_Directory(ArchiveHandle *AH); +extern void InitArchiveFmt_Tar(ArchiveHandle *AH); + +extern void ReconnectToServer(ArchiveHandle *AH, const char *dbname); +extern void IssueCommandPerBlob(ArchiveHandle *AH, TocEntry *te, + const char *cmdBegin, const char *cmdEnd); +extern void IssueACLPerBlob(ArchiveHandle *AH, TocEntry *te); +extern void DropLOIfExists(ArchiveHandle *AH, Oid oid); + +void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH); +int ahprintf(ArchiveHandle *AH, const char *fmt,...) pg_attribute_printf(2, 3); + +#endif diff --git a/src/compat/19/pg_dump/pg_backup_custom.c b/src/compat/19/pg_dump/pg_backup_custom.c new file mode 100644 index 00000000..52990620 --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_custom.c @@ -0,0 +1,1032 @@ +/*------------------------------------------------------------------------- + * + * pg_backup_custom.c + * + * Implements the custom output format. + * + * The comments with the routines in this code are a good place to + * understand how to write a new format. + * + * See the headers to pg_restore for more details. + * + * Copyright (c) 2000, Philip Warner + * Rights are granted to use this software in any way so long + * as this notice is not removed. + * + * The author is not responsible for loss or damages that may + * and any liability will be limited to the time taken to fix any + * related bug. + * + * + * IDENTIFICATION + * src/bin/pg_dump/pg_backup_custom.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include "common/file_utils.h" +#include "compress_io.h" +#include "pg_backup_utils.h" + +/*-------- + * Routines in the format interface + *-------- + */ + +static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te); +static void _StartData(ArchiveHandle *AH, TocEntry *te); +static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen); +static void _EndData(ArchiveHandle *AH, TocEntry *te); +static int _WriteByte(ArchiveHandle *AH, const int i); +static int _ReadByte(ArchiveHandle *AH); +static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len); +static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len); +static void _CloseArchive(ArchiveHandle *AH); +static void _ReopenArchive(ArchiveHandle *AH); +static void _PrintTocData(ArchiveHandle *AH, TocEntry *te); +static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te); +static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te); +static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te); + +static void _PrintData(ArchiveHandle *AH); +static void _skipData(ArchiveHandle *AH); +static void _skipLOs(ArchiveHandle *AH); + +static void _StartLOs(ArchiveHandle *AH, TocEntry *te); +static void _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid); +static void _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid); +static void _EndLOs(ArchiveHandle *AH, TocEntry *te); +static void _LoadLOs(ArchiveHandle *AH, bool drop); + +static void _PrepParallelRestore(ArchiveHandle *AH); +static void _Clone(ArchiveHandle *AH); +static void _DeClone(ArchiveHandle *AH); + +static int _WorkerJobRestoreCustom(ArchiveHandle *AH, TocEntry *te); + +typedef struct +{ + CompressorState *cs; + int hasSeek; + /* lastFilePos is used only when reading, and may be invalid if !hasSeek */ + pgoff_t lastFilePos; /* position after last data block we've read */ +} lclContext; + +typedef struct +{ + int dataState; + pgoff_t dataPos; /* valid only if dataState=K_OFFSET_POS_SET */ +} lclTocEntry; + + +/*------ + * Static declarations + *------ + */ +static void _readBlockHeader(ArchiveHandle *AH, int *type, int *id); +static pgoff_t _getFilePos(ArchiveHandle *AH, lclContext *ctx); + +static void _CustomWriteFunc(ArchiveHandle *AH, const char *buf, size_t len); +static size_t _CustomReadFunc(ArchiveHandle *AH, char **buf, size_t *buflen); + + +/* + * Init routine required by ALL formats. This is a global routine + * and should be declared in pg_backup_archiver.h + * + * It's task is to create any extra archive context (using AH->formatData), + * and to initialize the supported function pointers. + * + * It should also prepare whatever its input source is for reading/writing, + * and in the case of a read mode connection, it should load the Header & TOC. + */ +void +InitArchiveFmt_Custom(ArchiveHandle *AH) +{ + lclContext *ctx; + + /* Assuming static functions, this can be copied for each format. */ + AH->ArchiveEntryPtr = _ArchiveEntry; + AH->StartDataPtr = _StartData; + AH->WriteDataPtr = _WriteData; + AH->EndDataPtr = _EndData; + AH->WriteBytePtr = _WriteByte; + AH->ReadBytePtr = _ReadByte; + AH->WriteBufPtr = _WriteBuf; + AH->ReadBufPtr = _ReadBuf; + AH->ClosePtr = _CloseArchive; + AH->ReopenPtr = _ReopenArchive; + AH->PrintTocDataPtr = _PrintTocData; + AH->ReadExtraTocPtr = _ReadExtraToc; + AH->WriteExtraTocPtr = _WriteExtraToc; + AH->PrintExtraTocPtr = _PrintExtraToc; + + AH->StartLOsPtr = _StartLOs; + AH->StartLOPtr = _StartLO; + AH->EndLOPtr = _EndLO; + AH->EndLOsPtr = _EndLOs; + + AH->PrepParallelRestorePtr = _PrepParallelRestore; + AH->ClonePtr = _Clone; + AH->DeClonePtr = _DeClone; + + /* no parallel dump in the custom archive, only parallel restore */ + AH->WorkerJobDumpPtr = NULL; + AH->WorkerJobRestorePtr = _WorkerJobRestoreCustom; + + /* Set up a private area. */ + ctx = pg_malloc0_object(lclContext); + AH->formatData = ctx; + + /* + * Now open the file + */ + if (AH->mode == archModeWrite) + { + if (AH->fSpec && strcmp(AH->fSpec, "") != 0) + { + AH->FH = fopen(AH->fSpec, PG_BINARY_W); + if (!AH->FH) + pg_fatal("could not open output file \"%s\": %m", AH->fSpec); + } + else + { + AH->FH = stdout; + if (!AH->FH) + pg_fatal("could not open output file: %m"); + } + + ctx->hasSeek = checkSeek(AH->FH); + } + else + { + if (AH->fSpec && strcmp(AH->fSpec, "") != 0) + { + AH->FH = fopen(AH->fSpec, PG_BINARY_R); + if (!AH->FH) + pg_fatal("could not open input file \"%s\": %m", AH->fSpec); + } + else + { + AH->FH = stdin; + if (!AH->FH) + pg_fatal("could not open input file: %m"); + } + + ctx->hasSeek = checkSeek(AH->FH); + + ReadHead(AH); + ReadToc(AH); + + /* + * Remember location of first data block (i.e., the point after TOC) + * in case we have to search for desired data blocks. + */ + ctx->lastFilePos = _getFilePos(AH, ctx); + } +} + +/* + * Called by the Archiver when the dumper creates a new TOC entry. + * + * Optional. + * + * Set up extract format-related TOC data. +*/ +static void +_ArchiveEntry(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *ctx; + + ctx = pg_malloc0_object(lclTocEntry); + if (te->dataDumper) + ctx->dataState = K_OFFSET_POS_NOT_SET; + else + ctx->dataState = K_OFFSET_NO_DATA; + + te->formatData = ctx; +} + +/* + * Called by the Archiver to save any extra format-related TOC entry + * data. + * + * Optional. + * + * Use the Archiver routines to write data - they are non-endian, and + * maintain other important file information. + */ +static void +_WriteExtraToc(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *ctx = (lclTocEntry *) te->formatData; + + WriteOffset(AH, ctx->dataPos, ctx->dataState); +} + +/* + * Called by the Archiver to read any extra format-related TOC data. + * + * Optional. + * + * Needs to match the order defined in _WriteExtraToc, and should also + * use the Archiver input routines. + */ +static void +_ReadExtraToc(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *ctx = (lclTocEntry *) te->formatData; + + if (ctx == NULL) + { + ctx = pg_malloc0_object(lclTocEntry); + te->formatData = ctx; + } + + ctx->dataState = ReadOffset(AH, &(ctx->dataPos)); + + /* + * Prior to V1.7 (pg7.3), we dumped the data size as an int now we don't + * dump it at all. + */ + if (AH->version < K_VERS_1_7) + ReadInt(AH); +} + +/* + * Called by the Archiver when restoring an archive to output a comment + * that includes useful information about the TOC entry. + * + * Optional. + */ +static void +_PrintExtraToc(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *ctx = (lclTocEntry *) te->formatData; + + if (AH->public.verbose) + ahprintf(AH, "-- Data Pos: " INT64_FORMAT "\n", + (int64) ctx->dataPos); +} + +/* + * Called by the archiver when saving TABLE DATA (not schema). This routine + * should save whatever format-specific information is needed to read + * the archive back. + * + * It is called just prior to the dumper's 'DataDumper' routine being called. + * + * Optional, but strongly recommended. + * + */ +static void +_StartData(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + tctx->dataPos = _getFilePos(AH, ctx); + if (tctx->dataPos >= 0) + tctx->dataState = K_OFFSET_POS_SET; + + _WriteByte(AH, BLK_DATA); /* Block type */ + WriteInt(AH, te->dumpId); /* For sanity check */ + + ctx->cs = AllocateCompressor(AH->compression_spec, + NULL, + _CustomWriteFunc); +} + +/* + * Called by archiver when dumper calls WriteData. This routine is + * called for both LO and table data; it is the responsibility of + * the format to manage each kind of data using StartLO/StartData. + * + * It should only be called from within a DataDumper routine. + * + * Mandatory. + */ +static void +_WriteData(ArchiveHandle *AH, const void *data, size_t dLen) +{ + lclContext *ctx = (lclContext *) AH->formatData; + CompressorState *cs = ctx->cs; + + if (dLen > 0) + /* writeData() internally throws write errors */ + cs->writeData(AH, cs, data, dLen); +} + +/* + * Called by the archiver when a dumper's 'DataDumper' routine has + * finished. + * + * Mandatory. + */ +static void +_EndData(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + EndCompressor(AH, ctx->cs); + ctx->cs = NULL; + + /* Send the end marker */ + WriteInt(AH, 0); +} + +/* + * Called by the archiver when starting to save BLOB DATA (not schema). + * This routine should save whatever format-specific information is needed + * to read the LOs back into memory. + * + * It is called just prior to the dumper's DataDumper routine. + * + * Optional, but strongly recommended. + */ +static void +_StartLOs(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + tctx->dataPos = _getFilePos(AH, ctx); + if (tctx->dataPos >= 0) + tctx->dataState = K_OFFSET_POS_SET; + + _WriteByte(AH, BLK_BLOBS); /* Block type */ + WriteInt(AH, te->dumpId); /* For sanity check */ +} + +/* + * Called by the archiver when the dumper calls StartLO. + * + * Mandatory. + * + * Must save the passed OID for retrieval at restore-time. + */ +static void +_StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + if (oid == 0) + pg_fatal("invalid OID for large object"); + + WriteInt(AH, oid); + + ctx->cs = AllocateCompressor(AH->compression_spec, + NULL, + _CustomWriteFunc); +} + +/* + * Called by the archiver when the dumper calls EndLO. + * + * Optional. + */ +static void +_EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + EndCompressor(AH, ctx->cs); + /* Send the end marker */ + WriteInt(AH, 0); +} + +/* + * Called by the archiver when finishing saving BLOB DATA. + * + * Optional. + */ +static void +_EndLOs(ArchiveHandle *AH, TocEntry *te) +{ + /* Write out a fake zero OID to mark end-of-LOs. */ + WriteInt(AH, 0); +} + +/* + * Print data for a given TOC entry + */ +static void +_PrintTocData(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + int blkType; + int id; + + if (tctx->dataState == K_OFFSET_NO_DATA) + return; + + if (!ctx->hasSeek || tctx->dataState == K_OFFSET_POS_NOT_SET) + { + /* + * We cannot seek directly to the desired block. Instead, skip over + * block headers until we find the one we want. Remember the + * positions of skipped-over blocks, so that if we later decide we + * need to read one, we'll be able to seek to it. + * + * When our input file is seekable, we can do the search starting from + * the point after the last data block we scanned in previous + * iterations of this function. + */ + if (ctx->hasSeek) + { + if (fseeko(AH->FH, ctx->lastFilePos, SEEK_SET) != 0) + pg_fatal("error during file seek: %m"); + } + + for (;;) + { + pgoff_t thisBlkPos = _getFilePos(AH, ctx); + + _readBlockHeader(AH, &blkType, &id); + + if (blkType == EOF || id == te->dumpId) + break; + + /* Remember the block position, if we got one */ + if (thisBlkPos >= 0) + { + TocEntry *otherte = getTocEntryByDumpId(AH, id); + + if (otherte && otherte->formatData) + { + lclTocEntry *othertctx = (lclTocEntry *) otherte->formatData; + + /* + * Note: on Windows, multiple threads might access/update + * the same lclTocEntry concurrently, but that should be + * safe as long as we update dataPos before dataState. + * Ideally, we'd use pg_write_barrier() to enforce that, + * but the needed infrastructure doesn't exist in frontend + * code. But Windows only runs on machines with strong + * store ordering, so it should be okay for now. + */ + if (othertctx->dataState == K_OFFSET_POS_NOT_SET) + { + othertctx->dataPos = thisBlkPos; + othertctx->dataState = K_OFFSET_POS_SET; + } + else if (othertctx->dataPos != thisBlkPos || + othertctx->dataState != K_OFFSET_POS_SET) + { + /* sanity check */ + pg_log_warning("data block %d has wrong seek position", + id); + } + } + } + + switch (blkType) + { + case BLK_DATA: + _skipData(AH); + break; + + case BLK_BLOBS: + _skipLOs(AH); + break; + + default: /* Always have a default */ + pg_fatal("unrecognized data block type (%d) while searching archive", + blkType); + break; + } + } + } + else + { + /* We can just seek to the place we need to be. */ + if (fseeko(AH->FH, tctx->dataPos, SEEK_SET) != 0) + pg_fatal("error during file seek: %m"); + + _readBlockHeader(AH, &blkType, &id); + } + + /* + * If we reached EOF without finding the block we want, then either it + * doesn't exist, or it does but we lack the ability to seek back to it. + */ + if (blkType == EOF) + { + if (!ctx->hasSeek) + pg_fatal("could not find block ID %d in archive -- " + "possibly due to out-of-order restore request, " + "which cannot be handled due to non-seekable input file", + te->dumpId); + else + pg_fatal("could not find block ID %d in archive -- " + "possibly corrupt archive", + te->dumpId); + } + + /* Are we sane? */ + if (id != te->dumpId) + pg_fatal("found unexpected block ID (%d) when reading data -- expected %d", + id, te->dumpId); + + switch (blkType) + { + case BLK_DATA: + _PrintData(AH); + break; + + case BLK_BLOBS: + _LoadLOs(AH, AH->public.ropt->dropSchema); + break; + + default: /* Always have a default */ + pg_fatal("unrecognized data block type %d while restoring archive", + blkType); + break; + } + + /* + * If our input file is seekable but lacks data offsets, update our + * knowledge of where to start future searches from. (Note that we did + * not update the current TE's dataState/dataPos. We could have, but + * there is no point since it will not be visited again.) + */ + if (ctx->hasSeek && tctx->dataState == K_OFFSET_POS_NOT_SET) + { + pgoff_t curPos = _getFilePos(AH, ctx); + + if (curPos > ctx->lastFilePos) + ctx->lastFilePos = curPos; + } +} + +/* + * Print data from current file position. +*/ +static void +_PrintData(ArchiveHandle *AH) +{ + CompressorState *cs; + + cs = AllocateCompressor(AH->compression_spec, + _CustomReadFunc, NULL); + cs->readData(AH, cs); + EndCompressor(AH, cs); +} + +static void +_LoadLOs(ArchiveHandle *AH, bool drop) +{ + Oid oid; + + StartRestoreLOs(AH); + + oid = ReadInt(AH); + while (oid != 0) + { + StartRestoreLO(AH, oid, drop); + _PrintData(AH); + EndRestoreLO(AH, oid); + oid = ReadInt(AH); + } + + EndRestoreLOs(AH); +} + +/* + * Skip the LOs from the current file position. + * LOs are written sequentially as data blocks (see below). + * Each LO is preceded by its original OID. + * A zero OID indicates the end of the LOs. + */ +static void +_skipLOs(ArchiveHandle *AH) +{ + Oid oid; + + oid = ReadInt(AH); + while (oid != 0) + { + _skipData(AH); + oid = ReadInt(AH); + } +} + +/* + * Skip data from current file position. + * Data blocks are formatted as an integer length, followed by data. + * A zero length indicates the end of the block. +*/ +static void +_skipData(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + size_t blkLen; + char *buf = NULL; + size_t buflen = 0; + + blkLen = ReadInt(AH); + while (blkLen != 0) + { + /* + * Seeks of less than stdio's buffer size are less efficient than just + * reading the data, at least on common platforms. We don't know the + * buffer size for sure, but 4kB is the usual value. (While pg_dump + * currently tries to avoid producing such short data blocks, older + * dump files often contain them.) + */ + if (ctx->hasSeek && blkLen >= 4 * 1024) + { + if (fseeko(AH->FH, blkLen, SEEK_CUR) != 0) + pg_fatal("error during file seek: %m"); + } + else + { + if (blkLen > buflen) + { + free(buf); + buflen = Max(blkLen, 4 * 1024); + buf = (char *) pg_malloc(buflen); + } + if (fread(buf, 1, blkLen, AH->FH) != blkLen) + { + if (feof(AH->FH)) + pg_fatal("could not read from input file: end of file"); + else + pg_fatal("could not read from input file: %m"); + } + } + + blkLen = ReadInt(AH); + } + + free(buf); +} + +/* + * Write a byte of data to the archive. + * + * Mandatory. + * + * Called by the archiver to do integer & byte output to the archive. + */ +static int +_WriteByte(ArchiveHandle *AH, const int i) +{ + if (fputc(i, AH->FH) == EOF) + WRITE_ERROR_EXIT; + + return 1; +} + +/* + * Read a byte of data from the archive. + * + * Mandatory + * + * Called by the archiver to read bytes & integers from the archive. + * EOF should be treated as a fatal error. + */ +static int +_ReadByte(ArchiveHandle *AH) +{ + int res; + + res = getc(AH->FH); + if (res == EOF) + READ_ERROR_EXIT(AH->FH); + return res; +} + +/* + * Write a buffer of data to the archive. + * + * Mandatory. + * + * Called by the archiver to write a block of bytes to the archive. + */ +static void +_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len) +{ + if (fwrite(buf, 1, len, AH->FH) != len) + WRITE_ERROR_EXIT; +} + +/* + * Read a block of bytes from the archive. + * + * Mandatory. + * + * Called by the archiver to read a block of bytes from the archive + */ +static void +_ReadBuf(ArchiveHandle *AH, void *buf, size_t len) +{ + if (fread(buf, 1, len, AH->FH) != len) + READ_ERROR_EXIT(AH->FH); +} + +/* + * Close the archive. + * + * Mandatory. + * + * When writing the archive, this is the routine that actually starts + * the process of saving it to files. No data should be written prior + * to this point, since the user could sort the TOC after creating it. + * + * If an archive is to be written, this routine must call: + * WriteHead to save the archive header + * WriteToc to save the TOC entries + * WriteDataChunks to save all data & LOs. + * + */ +static void +_CloseArchive(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + pgoff_t tpos; + + if (AH->mode == archModeWrite) + { + WriteHead(AH); + /* Remember TOC's seek position for use below */ + tpos = ftello(AH->FH); + if (tpos < 0 && ctx->hasSeek) + pg_fatal("could not determine seek position in archive file: %m"); + WriteToc(AH); + WriteDataChunks(AH, NULL); + + /* + * If possible, re-write the TOC in order to update the data offset + * information. This is not essential, as pg_restore can cope in most + * cases without it; but it can make pg_restore significantly faster + * in some situations (especially parallel restore). We can skip this + * step if we're not dumping any data; there are no offsets to update + * in that case. + */ + if (ctx->hasSeek && AH->public.dopt->dumpData && + fseeko(AH->FH, tpos, SEEK_SET) == 0) + WriteToc(AH); + } + + if (fclose(AH->FH) != 0) + pg_fatal("could not close archive file: %m"); + + /* Sync the output file if one is defined */ + if (AH->dosync && AH->mode == archModeWrite && AH->fSpec) + (void) fsync_fname(AH->fSpec, false); + + AH->FH = NULL; +} + +/* + * Reopen the archive's file handle. + * + * We close the original file handle, except on Windows. (The difference + * is because on Windows, this is used within a multithreading context, + * and we don't want a thread closing the parent file handle.) + */ +static void +_ReopenArchive(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + pgoff_t tpos; + + if (AH->mode == archModeWrite) + pg_fatal("can only reopen input archives"); + + /* + * These two cases are user-facing errors since they represent unsupported + * (but not invalid) use-cases. Word the error messages appropriately. + */ + if (AH->fSpec == NULL || strcmp(AH->fSpec, "") == 0) + pg_fatal("parallel restore from standard input is not supported"); + if (!ctx->hasSeek) + pg_fatal("parallel restore from non-seekable file is not supported"); + + tpos = ftello(AH->FH); + if (tpos < 0) + pg_fatal("could not determine seek position in archive file: %m"); + +#ifndef WIN32 + if (fclose(AH->FH) != 0) + pg_fatal("could not close archive file: %m"); +#endif + + AH->FH = fopen(AH->fSpec, PG_BINARY_R); + if (!AH->FH) + pg_fatal("could not open input file \"%s\": %m", AH->fSpec); + + if (fseeko(AH->FH, tpos, SEEK_SET) != 0) + pg_fatal("could not set seek position in archive file: %m"); +} + +/* + * Prepare for parallel restore. + * + * The main thing that needs to happen here is to fill in TABLE DATA and BLOBS + * TOC entries' dataLength fields with appropriate values to guide the + * ordering of restore jobs. The source of said data is format-dependent, + * as is the exact meaning of the values. + * + * A format module might also choose to do other setup here. + */ +static void +_PrepParallelRestore(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + TocEntry *prev_te = NULL; + lclTocEntry *prev_tctx = NULL; + TocEntry *te; + + /* + * Knowing that the data items were dumped out in TOC order, we can + * reconstruct the length of each item as the delta to the start offset of + * the next data item. + */ + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + /* + * Ignore entries without a known data offset; if we were unable to + * seek to rewrite the TOC when creating the archive, this'll be all + * of them, and we'll end up with no size estimates. + */ + if (tctx->dataState != K_OFFSET_POS_SET) + continue; + + /* Compute previous data item's length */ + if (prev_te) + { + if (tctx->dataPos > prev_tctx->dataPos) + prev_te->dataLength = tctx->dataPos - prev_tctx->dataPos; + } + + prev_te = te; + prev_tctx = tctx; + } + + /* If OK to seek, we can determine the length of the last item */ + if (prev_te && ctx->hasSeek) + { + pgoff_t endpos; + + if (fseeko(AH->FH, 0, SEEK_END) != 0) + pg_fatal("error during file seek: %m"); + endpos = ftello(AH->FH); + if (endpos > prev_tctx->dataPos) + prev_te->dataLength = endpos - prev_tctx->dataPos; + } +} + +/* + * Clone format-specific fields during parallel restoration. + */ +static void +_Clone(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + /* + * Each thread must have private lclContext working state. + */ + AH->formatData = pg_malloc_object(lclContext); + memcpy(AH->formatData, ctx, sizeof(lclContext)); + ctx = (lclContext *) AH->formatData; + + /* sanity check, shouldn't happen */ + if (ctx->cs != NULL) + pg_fatal("compressor active"); + + /* + * We intentionally do not clone TOC-entry-local state: it's useful to + * share knowledge about where the data blocks are across threads. + * _PrintTocData has to be careful about the order of operations on that + * state, though. + */ +} + +static void +_DeClone(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + free(ctx); +} + +/* + * This function is executed in the child of a parallel restore from a + * custom-format archive and restores the actual data for one TOC entry. + */ +static int +_WorkerJobRestoreCustom(ArchiveHandle *AH, TocEntry *te) +{ + return parallel_restore(AH, te); +} + +/*-------------------------------------------------- + * END OF FORMAT CALLBACKS + *-------------------------------------------------- + */ + +/* + * Get the current position in the archive file. + * + * With a non-seekable archive file, we may not be able to obtain the + * file position. If so, just return -1. It's not too important in + * that case because we won't be able to rewrite the TOC to fill in + * data block offsets anyway. + */ +static pgoff_t +_getFilePos(ArchiveHandle *AH, lclContext *ctx) +{ + pgoff_t pos; + + pos = ftello(AH->FH); + if (pos < 0) + { + /* Not expected if we found we can seek. */ + if (ctx->hasSeek) + pg_fatal("could not determine seek position in archive file: %m"); + } + return pos; +} + +/* + * Read a data block header. The format changed in V1.3, so we + * centralize the code here for simplicity. Returns *type = EOF + * if at EOF. + */ +static void +_readBlockHeader(ArchiveHandle *AH, int *type, int *id) +{ + int byt; + + /* + * Note: if we are at EOF with a pre-1.3 input file, we'll pg_fatal() + * inside ReadInt rather than returning EOF. It doesn't seem worth + * jumping through hoops to deal with that case better, because no such + * files are likely to exist in the wild: only some 7.1 development + * versions of pg_dump ever generated such files. + */ + if (AH->version < K_VERS_1_3) + *type = BLK_DATA; + else + { + byt = getc(AH->FH); + *type = byt; + if (byt == EOF) + { + *id = 0; /* don't return an uninitialized value */ + return; + } + } + + *id = ReadInt(AH); +} + +/* + * Callback function for writeData. Writes one block of (compressed) + * data to the archive. + */ +static void +_CustomWriteFunc(ArchiveHandle *AH, const char *buf, size_t len) +{ + /* never write 0-byte blocks (this should not happen) */ + if (len > 0) + { + WriteInt(AH, len); + _WriteBuf(AH, buf, len); + } +} + +/* + * Callback function for readData. To keep things simple, we + * always read one compressed block at a time. + */ +static size_t +_CustomReadFunc(ArchiveHandle *AH, char **buf, size_t *buflen) +{ + size_t blkLen; + + /* Read length */ + blkLen = ReadInt(AH); + if (blkLen == 0) + return 0; + + /* If the caller's buffer is not large enough, allocate a bigger one */ + if (blkLen > *buflen) + { + free(*buf); + *buf = (char *) pg_malloc(blkLen); + *buflen = blkLen; + } + + /* exits app on read errors */ + _ReadBuf(AH, *buf, blkLen); + + return blkLen; +} diff --git a/src/compat/19/pg_dump/pg_backup_db.c b/src/compat/19/pg_dump/pg_backup_db.c new file mode 100644 index 00000000..5c349279 --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_db.c @@ -0,0 +1,619 @@ +/*------------------------------------------------------------------------- + * + * pg_backup_db.c + * + * Implements the basic DB functions used by the archiver. + * + * IDENTIFICATION + * src/bin/pg_dump/pg_backup_db.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include +#include +#ifdef HAVE_TERMIOS_H +#include +#endif + +#include "common/connect.h" +#include "common/string.h" +#include "connectdb.h" +#include "parallel.h" +#include "pg_backup_archiver.h" +#include "pg_backup_db.h" +#include "pg_backup_utils.h" + +static void _check_database_version(ArchiveHandle *AH); +static void notice_processor(void *arg, const char *message); + +static void +_check_database_version(ArchiveHandle *AH) +{ + const char *remoteversion_str; + int remoteversion; + PGresult *res; + + remoteversion_str = PQparameterStatus(AH->connection, "server_version"); + remoteversion = PQserverVersion(AH->connection); + if (remoteversion == 0 || !remoteversion_str) + pg_fatal("could not get \"server_version\" from libpq"); + + AH->public.remoteVersionStr = pg_strdup(remoteversion_str); + AH->public.remoteVersion = remoteversion; + if (!AH->archiveRemoteVersion) + AH->archiveRemoteVersion = AH->public.remoteVersionStr; + + if (remoteversion != PG_VERSION_NUM + && (remoteversion < AH->public.minRemoteVersion || + remoteversion > AH->public.maxRemoteVersion)) + { + pg_log_error("aborting because of server version mismatch"); + pg_log_error_detail("server version: %s; %s version: %s", + remoteversion_str, progname, PG_VERSION); + exit(1); + } + + /* + * Check if server is in recovery mode, which means we are on a hot + * standby. + */ + res = ExecuteSqlQueryForSingleRow((Archive *) AH, + "SELECT pg_catalog.pg_is_in_recovery()"); + AH->public.isStandby = (strcmp(PQgetvalue(res, 0, 0), "t") == 0); + PQclear(res); +} + +/* + * Reconnect to the server. If dbname is not NULL, use that database, + * else the one associated with the archive handle. + */ +void +ReconnectToServer(ArchiveHandle *AH, const char *dbname) +{ + PGconn *oldConn = AH->connection; + RestoreOptions *ropt = AH->public.ropt; + + /* + * Save the dbname, if given, in override_dbname so that it will also + * affect any later reconnection attempt. + */ + if (dbname) + ropt->cparams.override_dbname = pg_strdup(dbname); + + /* + * Note: we want to establish the new connection, and in particular update + * ArchiveHandle's connCancel, before closing old connection. Otherwise + * an ill-timed SIGINT could try to access a dead connection. + */ + AH->connection = NULL; /* dodge error check in ConnectDatabaseAhx */ + + ConnectDatabaseAhx((Archive *) AH, &ropt->cparams, true); + + PQfinish(oldConn); +} + +/* + * Make, or remake, a database connection with the given parameters. + * + * The resulting connection handle is stored in AHX->connection. + * + * An interactive password prompt is automatically issued if required. + * We store the results of that in AHX->savedPassword. + * Note: it's not really all that sensible to use a single-entry password + * cache if the username keeps changing. In current usage, however, the + * username never does change, so one savedPassword is sufficient. + */ +void +ConnectDatabaseAhx(Archive *AHX, + const ConnParams *cparams, + bool isReconnect) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + trivalue prompt_password; + char *password; + + if (AH->connection) + pg_fatal("already connected to a database"); + + /* Never prompt for a password during a reconnection */ + prompt_password = isReconnect ? TRI_NO : cparams->promptPassword; + + password = AH->savedPassword; + + if (prompt_password == TRI_YES && password == NULL) + password = simple_prompt("Password: ", false); + + AH->connection = ConnectDatabase(cparams->dbname, NULL, cparams->pghost, + cparams->pgport, cparams->username, + prompt_password, true, + progname, NULL, NULL, password, cparams->override_dbname); + + /* Start strict; later phases may override this. */ + PQclear(ExecuteSqlQueryForSingleRow((Archive *) AH, + ALWAYS_SECURE_SEARCH_PATH_SQL)); + + if (password && password != AH->savedPassword) + free(password); + + /* + * We want to remember connection's actual password, whether or not we got + * it by prompting. So we don't just store the password variable. + */ + if (PQconnectionUsedPassword(AH->connection)) + { + free(AH->savedPassword); + AH->savedPassword = pg_strdup(PQpass(AH->connection)); + } + + /* check for version mismatch */ + _check_database_version(AH); + + PQsetNoticeProcessor(AH->connection, notice_processor, NULL); + + /* arrange for SIGINT to issue a query cancel on this connection */ + set_archive_cancel_info(AH, AH->connection); +} + +/* + * Close the connection to the database and also cancel off the query if we + * have one running. + */ +void +DisconnectDatabase(Archive *AHX) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + char errbuf[1]; + + if (!AH->connection) + return; + + if (AH->connCancel) + { + /* + * If we have an active query, send a cancel before closing, ignoring + * any errors. This is of no use for a normal exit, but might be + * helpful during pg_fatal(). + */ + if (PQtransactionStatus(AH->connection) == PQTRANS_ACTIVE) + (void) PQcancel(AH->connCancel, errbuf, sizeof(errbuf)); + + /* + * Prevent signal handler from sending a cancel after this. + */ + set_archive_cancel_info(AH, NULL); + } + + PQfinish(AH->connection); + AH->connection = NULL; +} + +PGconn * +GetConnection(Archive *AHX) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + return AH->connection; +} + +static void +notice_processor(void *arg, const char *message) +{ + pg_log_info("%s", message); +} + +/* Like pg_fatal(), but with a complaint about a particular query. */ +static void +die_on_query_failure(ArchiveHandle *AH, const char *query) +{ + pg_log_error("query failed: %s", + PQerrorMessage(AH->connection)); + pg_log_error_detail("Query was: %s", query); + exit(1); +} + +void +ExecuteSqlStatement(Archive *AHX, const char *query) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + PGresult *res; + + res = PQexec(AH->connection, query); + if (PQresultStatus(res) != PGRES_COMMAND_OK) + die_on_query_failure(AH, query); + PQclear(res); +} + +PGresult * +ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + PGresult *res; + + res = PQexec(AH->connection, query); + if (PQresultStatus(res) != status) + die_on_query_failure(AH, query); + return res; +} + +/* + * Execute an SQL query and verify that we got exactly one row back. + */ +PGresult * +ExecuteSqlQueryForSingleRow(Archive *fout, const char *query) +{ + PGresult *res; + int ntups; + + res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK); + + /* Expecting a single result only */ + ntups = PQntuples(res); + if (ntups != 1) + pg_fatal(ngettext("query returned %d row instead of one: %s", + "query returned %d rows instead of one: %s", + ntups), + ntups, query); + + return res; +} + +/* + * Convenience function to send a query. + * Monitors result to detect COPY statements + */ +static void +ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc) +{ + PGconn *conn = AH->connection; + PGresult *res; + +#ifdef NOT_USED + fprintf(stderr, "Executing: '%s'\n\n", qry); +#endif + res = PQexec(conn, qry); + + switch (PQresultStatus(res)) + { + case PGRES_COMMAND_OK: + case PGRES_TUPLES_OK: + case PGRES_EMPTY_QUERY: + /* A-OK */ + break; + case PGRES_COPY_IN: + /* Assume this is an expected result */ + AH->pgCopyIn = true; + break; + default: + /* trouble */ + warn_or_exit_horribly(AH, "%s: %sCommand was: %s", + desc, PQerrorMessage(conn), qry); + break; + } + + PQclear(res); +} + + +/* + * Process non-COPY table data (that is, INSERT commands). + * + * The commands have been run together as one long string for compressibility, + * and we are receiving them in bufferloads with arbitrary boundaries, so we + * have to locate command boundaries and save partial commands across calls. + * All state must be kept in AH->sqlparse, not in local variables of this + * routine. We assume that AH->sqlparse was filled with zeroes when created. + * + * We have to lex the data to the extent of identifying literals and quoted + * identifiers, so that we can recognize statement-terminating semicolons. + * We assume that INSERT data will not contain SQL comments, E'' literals, + * or dollar-quoted strings, so this is much simpler than a full SQL lexer. + * + * Note: when restoring from a pre-9.0 dump file, this code is also used to + * process BLOB COMMENTS data, which has the same problem of containing + * multiple SQL commands that might be split across bufferloads. Fortunately, + * that data won't contain anything complicated to lex either. + */ +static void +ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen) +{ + const char *qry = buf; + const char *eos = buf + bufLen; + + /* initialize command buffer if first time through */ + if (AH->sqlparse.curCmd == NULL) + AH->sqlparse.curCmd = createPQExpBuffer(); + + for (; qry < eos; qry++) + { + char ch = *qry; + + /* For neatness, we skip any newlines between commands */ + if (!(ch == '\n' && AH->sqlparse.curCmd->len == 0)) + appendPQExpBufferChar(AH->sqlparse.curCmd, ch); + + switch (AH->sqlparse.state) + { + case SQL_SCAN: /* Default state == 0, set in _allocAH */ + if (ch == ';') + { + /* + * We've found the end of a statement. Send it and reset + * the buffer. + */ + ExecuteSqlCommand(AH, AH->sqlparse.curCmd->data, + "could not execute query"); + resetPQExpBuffer(AH->sqlparse.curCmd); + } + else if (ch == '\'') + { + AH->sqlparse.state = SQL_IN_SINGLE_QUOTE; + AH->sqlparse.backSlash = false; + } + else if (ch == '"') + { + AH->sqlparse.state = SQL_IN_DOUBLE_QUOTE; + } + break; + + case SQL_IN_SINGLE_QUOTE: + /* We needn't handle '' specially */ + if (ch == '\'' && !AH->sqlparse.backSlash) + AH->sqlparse.state = SQL_SCAN; + else if (ch == '\\' && !AH->public.std_strings) + AH->sqlparse.backSlash = !AH->sqlparse.backSlash; + else + AH->sqlparse.backSlash = false; + break; + + case SQL_IN_DOUBLE_QUOTE: + /* We needn't handle "" specially */ + if (ch == '"') + AH->sqlparse.state = SQL_SCAN; + break; + } + } +} + + +/* + * Implement ahwrite() for direct-to-DB restore + */ +int +ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + if (AH->outputKind == OUTPUT_COPYDATA) + { + /* + * COPY data. + * + * We drop the data on the floor if libpq has failed to enter COPY + * mode; this allows us to behave reasonably when trying to continue + * after an error in a COPY command. + */ + if (AH->pgCopyIn && + PQputCopyData(AH->connection, buf, bufLen) <= 0) + pg_fatal("error returned by PQputCopyData: %s", + PQerrorMessage(AH->connection)); + } + else if (AH->outputKind == OUTPUT_OTHERDATA) + { + /* + * Table data expressed as INSERT commands; or, in old dump files, + * BLOB COMMENTS data (which is expressed as COMMENT ON commands). + */ + ExecuteSimpleCommands(AH, buf, bufLen); + } + else + { + /* + * General SQL commands; we assume that commands will not be split + * across calls. + * + * In most cases the data passed to us will be a null-terminated + * string, but if it's not, we have to add a trailing null. + */ + if (buf[bufLen] == '\0') + ExecuteSqlCommand(AH, buf, "could not execute query"); + else + { + char *str = (char *) pg_malloc(bufLen + 1); + + memcpy(str, buf, bufLen); + str[bufLen] = '\0'; + ExecuteSqlCommand(AH, str, "could not execute query"); + free(str); + } + } + + return bufLen; +} + +/* + * Terminate a COPY operation during direct-to-DB restore + */ +void +EndDBCopyMode(Archive *AHX, const char *tocEntryTag) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + if (AH->pgCopyIn) + { + PGresult *res; + + if (PQputCopyEnd(AH->connection, NULL) <= 0) + pg_fatal("error returned by PQputCopyEnd: %s", + PQerrorMessage(AH->connection)); + + /* Check command status and return to normal libpq state */ + res = PQgetResult(AH->connection); + if (PQresultStatus(res) != PGRES_COMMAND_OK) + warn_or_exit_horribly(AH, "COPY failed for table \"%s\": %s", + tocEntryTag, PQerrorMessage(AH->connection)); + PQclear(res); + + /* Do this to ensure we've pumped libpq back to idle state */ + if (PQgetResult(AH->connection) != NULL) + pg_log_warning("unexpected extra results during COPY of table \"%s\"", + tocEntryTag); + + AH->pgCopyIn = false; + } +} + +void +StartTransaction(Archive *AHX) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction"); +} + +void +CommitTransaction(Archive *AHX) +{ + ArchiveHandle *AH = (ArchiveHandle *) AHX; + + ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction"); +} + +/* + * Issue per-blob commands for the large object(s) listed in the TocEntry + * + * The TocEntry's defn string is assumed to consist of large object OIDs, + * one per line. Wrap these in the given SQL command fragments and issue + * the commands. (cmdEnd need not include a semicolon.) + */ +void +IssueCommandPerBlob(ArchiveHandle *AH, TocEntry *te, + const char *cmdBegin, const char *cmdEnd) +{ + /* Make a writable copy of the command string */ + char *buf = pg_strdup(te->defn); + RestoreOptions *ropt = AH->public.ropt; + char *st; + char *en; + + st = buf; + while ((en = strchr(st, '\n')) != NULL) + { + *en++ = '\0'; + ahprintf(AH, "%s%s%s;\n", cmdBegin, st, cmdEnd); + + /* In --transaction-size mode, count each command as an action */ + if (ropt && ropt->txn_size > 0) + { + if (++AH->txnCount >= ropt->txn_size) + { + if (AH->connection) + { + CommitTransaction(&AH->public); + StartTransaction(&AH->public); + } + else + ahprintf(AH, "COMMIT;\nBEGIN;\n\n"); + AH->txnCount = 0; + } + } + + st = en; + } + ahprintf(AH, "\n"); + pg_free(buf); +} + +/* + * Process a "LARGE OBJECTS" ACL TocEntry. + * + * To save space in the dump file, the TocEntry contains only one copy + * of the required GRANT/REVOKE commands, written to apply to the first + * blob in the group (although we do not depend on that detail here). + * We must expand the text to generate commands for all the blobs listed + * in the associated BLOB METADATA entry. + */ +void +IssueACLPerBlob(ArchiveHandle *AH, TocEntry *te) +{ + TocEntry *blobte = getTocEntryByDumpId(AH, te->dependencies[0]); + char *buf; + char *st; + char *st2; + char *en; + bool inquotes; + + if (!blobte) + pg_fatal("could not find entry for ID %d", te->dependencies[0]); + Assert(strcmp(blobte->desc, "BLOB METADATA") == 0); + + /* Make a writable copy of the ACL commands string */ + buf = pg_strdup(te->defn); + + /* + * We have to parse out the commands sufficiently to locate the blob OIDs + * and find the command-ending semicolons. The commands should not + * contain anything hard to parse except for double-quoted role names, + * which are easy to ignore. Once we've split apart the first and second + * halves of a command, apply IssueCommandPerBlob. (This means the + * updates on the blobs are interleaved if there's multiple commands, but + * that should cause no trouble.) + */ + inquotes = false; + st = en = buf; + st2 = NULL; + while (*en) + { + /* Ignore double-quoted material */ + if (*en == '"') + inquotes = !inquotes; + if (inquotes) + { + en++; + continue; + } + /* If we found "LARGE OBJECT", that's the end of the first half */ + if (strncmp(en, "LARGE OBJECT ", 13) == 0) + { + /* Terminate the first-half string */ + en += 13; + Assert(isdigit((unsigned char) *en)); + *en++ = '\0'; + /* Skip the rest of the blob OID */ + while (isdigit((unsigned char) *en)) + en++; + /* Second half starts here */ + Assert(st2 == NULL); + st2 = en; + } + /* If we found semicolon, that's the end of the second half */ + else if (*en == ';') + { + /* Terminate the second-half string */ + *en++ = '\0'; + Assert(st2 != NULL); + /* Issue this command for each blob */ + IssueCommandPerBlob(AH, blobte, st, st2); + /* For neatness, skip whitespace before the next command */ + while (isspace((unsigned char) *en)) + en++; + /* Reset for new command */ + st = en; + st2 = NULL; + } + else + en++; + } + pg_free(buf); +} + +void +DropLOIfExists(ArchiveHandle *AH, Oid oid) +{ + ahprintf(AH, + "SELECT pg_catalog.lo_unlink(oid) " + "FROM pg_catalog.pg_largeobject_metadata " + "WHERE oid = '%u';\n", + oid); +} diff --git a/src/compat/19/pg_dump/pg_backup_db.h b/src/compat/19/pg_dump/pg_backup_db.h new file mode 100644 index 00000000..8888dd34 --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_db.h @@ -0,0 +1,26 @@ +/* + * Definitions for pg_backup_db.c + * + * IDENTIFICATION + * src/bin/pg_dump/pg_backup_db.h + */ + +#ifndef PG_BACKUP_DB_H +#define PG_BACKUP_DB_H + +#include "pg_backup.h" + + +extern int ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen); + +extern void ExecuteSqlStatement(Archive *AHX, const char *query); +extern PGresult *ExecuteSqlQuery(Archive *AHX, const char *query, + ExecStatusType status); +extern PGresult *ExecuteSqlQueryForSingleRow(Archive *fout, const char *query); + +extern void EndDBCopyMode(Archive *AHX, const char *tocEntryTag); + +extern void StartTransaction(Archive *AHX); +extern void CommitTransaction(Archive *AHX); + +#endif diff --git a/src/compat/19/pg_dump/pg_backup_directory.c b/src/compat/19/pg_dump/pg_backup_directory.c new file mode 100644 index 00000000..d6a1428c --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_directory.c @@ -0,0 +1,820 @@ +/*------------------------------------------------------------------------- + * + * pg_backup_directory.c + * + * A directory format dump is a directory, which contains a "toc.dat" file + * for the TOC, and a separate file for each data entry, named ".dat". + * Large objects are stored in separate files named "blob_.dat", + * and there's a plain-text TOC file for each BLOBS TOC entry named + * "blobs_.toc" (or just "blobs.toc" in archive versions before 16). + * + * If compression is used, each data file is individually compressed and the + * ".gz" suffix is added to the filenames. The TOC files are never + * compressed by pg_dump, however they are accepted with the .gz suffix too, + * in case the user has manually compressed them with 'gzip'. + * + * NOTE: This format is identical to the files written in the tar file in + * the 'tar' format, except that we don't write the restore.sql file (TODO), + * and the tar format doesn't support compression. Please keep the formats in + * sync. + * + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * Portions Copyright (c) 2000, Philip Warner + * + * Rights are granted to use this software in any way so long + * as this notice is not removed. + * + * The author is not responsible for loss or damages that may + * result from its use. + * + * IDENTIFICATION + * src/bin/pg_dump/pg_backup_directory.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include +#include + +#include "common/file_utils.h" +#include "compress_io.h" +#include "dumputils.h" +#include "parallel.h" +#include "pg_backup_utils.h" + +typedef struct +{ + /* + * Our archive location. This is basically what the user specified as his + * backup file but of course here it is a directory. + */ + char *directory; + + CompressFileHandle *dataFH; /* currently open data file */ + CompressFileHandle *LOsTocFH; /* file handle for blobs_NNN.toc */ + ParallelState *pstate; /* for parallel backup / restore */ +} lclContext; + +typedef struct +{ + char *filename; /* filename excluding the directory (basename) */ +} lclTocEntry; + +/* prototypes for private functions */ +static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te); +static void _StartData(ArchiveHandle *AH, TocEntry *te); +static void _EndData(ArchiveHandle *AH, TocEntry *te); +static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen); +static int _WriteByte(ArchiveHandle *AH, const int i); +static int _ReadByte(ArchiveHandle *AH); +static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len); +static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len); +static void _CloseArchive(ArchiveHandle *AH); +static void _ReopenArchive(ArchiveHandle *AH); +static void _PrintTocData(ArchiveHandle *AH, TocEntry *te); + +static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te); +static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te); +static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te); + +static void _StartLOs(ArchiveHandle *AH, TocEntry *te); +static void _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid); +static void _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid); +static void _EndLOs(ArchiveHandle *AH, TocEntry *te); +static void _LoadLOs(ArchiveHandle *AH, TocEntry *te); + +static void _PrepParallelRestore(ArchiveHandle *AH); +static void _Clone(ArchiveHandle *AH); +static void _DeClone(ArchiveHandle *AH); + +static int _WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te); +static int _WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te); + +static void setFilePath(ArchiveHandle *AH, char *buf, + const char *relativeFilename); + +/* + * Init routine required by ALL formats. This is a global routine + * and should be declared in pg_backup_archiver.h + * + * Its task is to create any extra archive context (using AH->formatData), + * and to initialize the supported function pointers. + * + * It should also prepare whatever its input source is for reading/writing, + * and in the case of a read mode connection, it should load the Header & TOC. + */ +void +InitArchiveFmt_Directory(ArchiveHandle *AH) +{ + lclContext *ctx; + + /* Assuming static functions, this can be copied for each format. */ + AH->ArchiveEntryPtr = _ArchiveEntry; + AH->StartDataPtr = _StartData; + AH->WriteDataPtr = _WriteData; + AH->EndDataPtr = _EndData; + AH->WriteBytePtr = _WriteByte; + AH->ReadBytePtr = _ReadByte; + AH->WriteBufPtr = _WriteBuf; + AH->ReadBufPtr = _ReadBuf; + AH->ClosePtr = _CloseArchive; + AH->ReopenPtr = _ReopenArchive; + AH->PrintTocDataPtr = _PrintTocData; + AH->ReadExtraTocPtr = _ReadExtraToc; + AH->WriteExtraTocPtr = _WriteExtraToc; + AH->PrintExtraTocPtr = _PrintExtraToc; + + AH->StartLOsPtr = _StartLOs; + AH->StartLOPtr = _StartLO; + AH->EndLOPtr = _EndLO; + AH->EndLOsPtr = _EndLOs; + + AH->PrepParallelRestorePtr = _PrepParallelRestore; + AH->ClonePtr = _Clone; + AH->DeClonePtr = _DeClone; + + AH->WorkerJobRestorePtr = _WorkerJobRestoreDirectory; + AH->WorkerJobDumpPtr = _WorkerJobDumpDirectory; + + /* Set up our private context */ + ctx = pg_malloc0_object(lclContext); + AH->formatData = ctx; + + ctx->dataFH = NULL; + ctx->LOsTocFH = NULL; + + /* + * Now open the TOC file + */ + + if (!AH->fSpec || strcmp(AH->fSpec, "") == 0) + pg_fatal("no output directory specified"); + + ctx->directory = AH->fSpec; + + if (AH->mode == archModeWrite) + { + /* we accept an empty existing directory */ + create_or_open_dir(ctx->directory); + } + else + { /* Read Mode */ + char fname[MAXPGPATH]; + CompressFileHandle *tocFH; + + setFilePath(AH, fname, "toc.dat"); + + tocFH = InitDiscoverCompressFileHandle(fname, PG_BINARY_R); + if (tocFH == NULL) + pg_fatal("could not open input file \"%s\": %m", fname); + + ctx->dataFH = tocFH; + + /* + * The TOC of a directory format dump shares the format code of the + * tar format. + */ + AH->format = archTar; + ReadHead(AH); + AH->format = archDirectory; + ReadToc(AH); + + /* Nothing else in the file, so close it again... */ + if (!EndCompressFileHandle(tocFH)) + pg_fatal("could not close TOC file: %m"); + ctx->dataFH = NULL; + } +} + +/* + * Called by the Archiver when the dumper creates a new TOC entry. + * + * We determine the filename for this entry. +*/ +static void +_ArchiveEntry(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *tctx; + char fn[MAXPGPATH]; + + tctx = pg_malloc0_object(lclTocEntry); + if (strcmp(te->desc, "BLOBS") == 0) + { + snprintf(fn, MAXPGPATH, "blobs_%d.toc", te->dumpId); + tctx->filename = pg_strdup(fn); + } + else if (te->dataDumper) + { + snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId); + tctx->filename = pg_strdup(fn); + } + else + tctx->filename = NULL; + + te->formatData = tctx; +} + +/* + * Called by the Archiver to save any extra format-related TOC entry + * data. + * + * Use the Archiver routines to write data - they are non-endian, and + * maintain other important file information. + */ +static void +_WriteExtraToc(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + /* + * A dumpable object has set tctx->filename, any other object has not. + * (see _ArchiveEntry). + */ + if (tctx->filename) + WriteStr(AH, tctx->filename); + else + WriteStr(AH, ""); +} + +/* + * Called by the Archiver to read any extra format-related TOC data. + * + * Needs to match the order defined in _WriteExtraToc, and should also + * use the Archiver input routines. + */ +static void +_ReadExtraToc(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + if (tctx == NULL) + { + tctx = pg_malloc0_object(lclTocEntry); + te->formatData = tctx; + } + + tctx->filename = ReadStr(AH); + if (strlen(tctx->filename) == 0) + { + free(tctx->filename); + tctx->filename = NULL; + } +} + +/* + * Called by the Archiver when restoring an archive to output a comment + * that includes useful information about the TOC entry. + */ +static void +_PrintExtraToc(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + if (AH->public.verbose && tctx->filename) + ahprintf(AH, "-- File: %s\n", tctx->filename); +} + +/* + * Called by the archiver when saving TABLE DATA (not schema). This routine + * should save whatever format-specific information is needed to read + * the archive back. + * + * It is called just prior to the dumper's 'DataDumper' routine being called. + * + * We create the data file for writing. + */ +static void +_StartData(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + lclContext *ctx = (lclContext *) AH->formatData; + char fname[MAXPGPATH]; + + setFilePath(AH, fname, tctx->filename); + + ctx->dataFH = InitCompressFileHandle(AH->compression_spec); + + if (!ctx->dataFH->open_write_func(fname, PG_BINARY_W, ctx->dataFH)) + pg_fatal("could not open output file \"%s\": %m", fname); +} + +/* + * Called by archiver when dumper calls WriteData. This routine is + * called for both LO and table data; it is the responsibility of + * the format to manage each kind of data using StartLO/StartData. + * + * It should only be called from within a DataDumper routine. + * + * We write the data to the open data file. + */ +static void +_WriteData(ArchiveHandle *AH, const void *data, size_t dLen) +{ + lclContext *ctx = (lclContext *) AH->formatData; + CompressFileHandle *CFH = ctx->dataFH; + + if (dLen <= 0) + return; + CFH->write_func(data, dLen, CFH); +} + +/* + * Called by the archiver when a dumper's 'DataDumper' routine has + * finished. + * + * We close the data file. + */ +static void +_EndData(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + /* Close the file */ + if (!EndCompressFileHandle(ctx->dataFH)) + pg_fatal("could not close data file: %m"); + + ctx->dataFH = NULL; +} + +/* + * Print data for a given file (can be a LO as well) + */ +static void +_PrintFileData(ArchiveHandle *AH, char *filename) +{ + size_t cnt; + char *buf; + size_t buflen; + CompressFileHandle *CFH; + + if (!filename) + return; + + CFH = InitDiscoverCompressFileHandle(filename, PG_BINARY_R); + if (!CFH) + pg_fatal("could not open input file \"%s\": %m", filename); + + buflen = DEFAULT_IO_BUFFER_SIZE; + buf = pg_malloc(buflen); + + while ((cnt = CFH->read_func(buf, buflen, CFH)) > 0) + { + ahwrite(buf, 1, cnt, AH); + } + + free(buf); + if (!EndCompressFileHandle(CFH)) + pg_fatal("could not close data file \"%s\": %m", filename); +} + +/* + * Print data for a given TOC entry +*/ +static void +_PrintTocData(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + if (!tctx->filename) + return; + + if (strcmp(te->desc, "BLOBS") == 0) + _LoadLOs(AH, te); + else + { + char fname[MAXPGPATH]; + + setFilePath(AH, fname, tctx->filename); + _PrintFileData(AH, fname); + } +} + +static void +_LoadLOs(ArchiveHandle *AH, TocEntry *te) +{ + Oid oid; + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + CompressFileHandle *CFH; + char tocfname[MAXPGPATH]; + char line[MAXPGPATH]; + + StartRestoreLOs(AH); + + /* + * Note: before archive v16, there was always only one BLOBS TOC entry, + * now there can be multiple. Furthermore, although the actual filename + * was always "blobs.toc" before v16, the value of tctx->filename did not + * match that before commit 548e50976 fixed it. For simplicity we assume + * it must be "blobs.toc" in all archives before v16. + */ + if (AH->version < K_VERS_1_16) + setFilePath(AH, tocfname, "blobs.toc"); + else + setFilePath(AH, tocfname, tctx->filename); + + CFH = ctx->LOsTocFH = InitDiscoverCompressFileHandle(tocfname, PG_BINARY_R); + + if (ctx->LOsTocFH == NULL) + pg_fatal("could not open large object TOC file \"%s\" for input: %m", + tocfname); + + /* Read the LOs TOC file line-by-line, and process each LO */ + while ((CFH->gets_func(line, MAXPGPATH, CFH)) != NULL) + { + char lofname[MAXPGPATH + 1]; + char path[MAXPGPATH]; + + /* Can't overflow because line and lofname are the same length */ + if (sscanf(line, "%u %" CppAsString2(MAXPGPATH) "s\n", &oid, lofname) != 2) + pg_fatal("invalid line in large object TOC file \"%s\": \"%s\"", + tocfname, line); + + StartRestoreLO(AH, oid, AH->public.ropt->dropSchema); + snprintf(path, MAXPGPATH, "%s/%s", ctx->directory, lofname); + _PrintFileData(AH, path); + EndRestoreLO(AH, oid); + } + if (!CFH->eof_func(CFH)) + pg_fatal("error reading large object TOC file \"%s\"", + tocfname); + + if (!EndCompressFileHandle(ctx->LOsTocFH)) + pg_fatal("could not close large object TOC file \"%s\": %m", + tocfname); + + ctx->LOsTocFH = NULL; + + EndRestoreLOs(AH); +} + + +/* + * Write a byte of data to the archive. + * Called by the archiver to do integer & byte output to the archive. + * These routines are only used to read & write the headers & TOC. + */ +static int +_WriteByte(ArchiveHandle *AH, const int i) +{ + unsigned char c = (unsigned char) i; + lclContext *ctx = (lclContext *) AH->formatData; + CompressFileHandle *CFH = ctx->dataFH; + + CFH->write_func(&c, 1, CFH); + return 1; +} + +/* + * Read a byte of data from the archive. + * Called by the archiver to read bytes & integers from the archive. + * These routines are only used to read & write headers & TOC. + * EOF should be treated as a fatal error. + */ +static int +_ReadByte(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + CompressFileHandle *CFH = ctx->dataFH; + + return CFH->getc_func(CFH); +} + +/* + * Write a buffer of data to the archive. + * Called by the archiver to write a block of bytes to the TOC or a data file. + */ +static void +_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len) +{ + lclContext *ctx = (lclContext *) AH->formatData; + CompressFileHandle *CFH = ctx->dataFH; + + CFH->write_func(buf, len, CFH); +} + +/* + * Read a block of bytes from the archive. + * + * Called by the archiver to read a block of bytes from the archive + */ +static void +_ReadBuf(ArchiveHandle *AH, void *buf, size_t len) +{ + lclContext *ctx = (lclContext *) AH->formatData; + CompressFileHandle *CFH = ctx->dataFH; + + /* + * We do not expect a short read, so fail if we get one. The read_func + * already dealt with any outright I/O error. + */ + if (CFH->read_func(buf, len, CFH) != len) + pg_fatal("could not read from input file: end of file"); +} + +/* + * Close the archive. + * + * When writing the archive, this is the routine that actually starts + * the process of saving it to files. No data should be written prior + * to this point, since the user could sort the TOC after creating it. + * + * If an archive is to be written, this routine must call: + * WriteHead to save the archive header + * WriteToc to save the TOC entries + * WriteDataChunks to save all data & LOs. + */ +static void +_CloseArchive(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + if (AH->mode == archModeWrite) + { + CompressFileHandle *tocFH; + pg_compress_specification compression_spec = {0}; + char fname[MAXPGPATH]; + + setFilePath(AH, fname, "toc.dat"); + + /* this will actually fork the processes for a parallel backup */ + ctx->pstate = ParallelBackupStart(AH); + + /* The TOC is always created uncompressed */ + compression_spec.algorithm = PG_COMPRESSION_NONE; + tocFH = InitCompressFileHandle(compression_spec); + if (!tocFH->open_write_func(fname, PG_BINARY_W, tocFH)) + pg_fatal("could not open output file \"%s\": %m", fname); + ctx->dataFH = tocFH; + + /* + * Write 'tar' in the format field of the toc.dat file. The directory + * is compatible with 'tar', so there's no point having a different + * format code for it. + */ + AH->format = archTar; + WriteHead(AH); + AH->format = archDirectory; + WriteToc(AH); + if (!EndCompressFileHandle(tocFH)) + pg_fatal("could not close TOC file: %m"); + WriteDataChunks(AH, ctx->pstate); + + ParallelBackupEnd(AH, ctx->pstate); + + /* + * In directory mode, there is no need to sync all the entries + * individually. Just recurse once through all the files generated. + */ + if (AH->dosync) + sync_dir_recurse(ctx->directory, AH->sync_method); + } + AH->FH = NULL; +} + +/* + * Reopen the archive's file handle. + */ +static void +_ReopenArchive(ArchiveHandle *AH) +{ + /* + * Our TOC is in memory, our data files are opened by each child anyway as + * they are separate. We support reopening the archive by just doing + * nothing. + */ +} + +/* + * LO support + */ + +/* + * Called by the archiver when starting to save BLOB DATA (not schema). + * It is called just prior to the dumper's DataDumper routine. + * + * We open the large object TOC file here, so that we can append a line to + * it for each LO. + */ +static void +_StartLOs(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + pg_compress_specification compression_spec = {0}; + char fname[MAXPGPATH]; + + setFilePath(AH, fname, tctx->filename); + + /* The LO TOC file is never compressed */ + compression_spec.algorithm = PG_COMPRESSION_NONE; + ctx->LOsTocFH = InitCompressFileHandle(compression_spec); + if (!ctx->LOsTocFH->open_write_func(fname, "ab", ctx->LOsTocFH)) + pg_fatal("could not open output file \"%s\": %m", fname); +} + +/* + * Called by the archiver when we're about to start dumping a LO. + * + * We create a file to write the LO to. + */ +static void +_StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid) +{ + lclContext *ctx = (lclContext *) AH->formatData; + char fname[MAXPGPATH]; + + snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid); + + ctx->dataFH = InitCompressFileHandle(AH->compression_spec); + if (!ctx->dataFH->open_write_func(fname, PG_BINARY_W, ctx->dataFH)) + pg_fatal("could not open output file \"%s\": %m", fname); +} + +/* + * Called by the archiver when the dumper is finished writing a LO. + * + * We close the LO file and write an entry to the LO TOC file for it. + */ +static void +_EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid) +{ + lclContext *ctx = (lclContext *) AH->formatData; + CompressFileHandle *CFH = ctx->LOsTocFH; + char buf[50]; + int len; + + /* Close the BLOB data file itself */ + if (!EndCompressFileHandle(ctx->dataFH)) + pg_fatal("could not close LO data file: %m"); + ctx->dataFH = NULL; + + /* register the LO in blobs_NNN.toc */ + len = snprintf(buf, sizeof(buf), "%u blob_%u.dat\n", oid, oid); + CFH->write_func(buf, len, CFH); +} + +/* + * Called by the archiver when finishing saving BLOB DATA. + * + * We close the LOs TOC file. + */ +static void +_EndLOs(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + if (!EndCompressFileHandle(ctx->LOsTocFH)) + pg_fatal("could not close LOs TOC file: %m"); + ctx->LOsTocFH = NULL; +} + +/* + * Gets a relative file name and prepends the output directory, writing the + * result to buf. The caller needs to make sure that buf is MAXPGPATH bytes + * big. Can't use a static char[MAXPGPATH] inside the function because we run + * multithreaded on Windows. + */ +static void +setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename) +{ + lclContext *ctx = (lclContext *) AH->formatData; + char *dname; + + dname = ctx->directory; + + if (strlen(dname) + 1 + strlen(relativeFilename) + 1 > MAXPGPATH) + pg_fatal("file name too long: \"%s\"", dname); + + strcpy(buf, dname); + strcat(buf, "/"); + strcat(buf, relativeFilename); +} + +/* + * Prepare for parallel restore. + * + * The main thing that needs to happen here is to fill in TABLE DATA and BLOBS + * TOC entries' dataLength fields with appropriate values to guide the + * ordering of restore jobs. The source of said data is format-dependent, + * as is the exact meaning of the values. + * + * A format module might also choose to do other setup here. + */ +static void +_PrepParallelRestore(ArchiveHandle *AH) +{ + TocEntry *te; + + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + char fname[MAXPGPATH]; + struct stat st; + + /* + * A dumpable object has set tctx->filename, any other object has not. + * (see _ArchiveEntry). + */ + if (tctx->filename == NULL) + continue; + + /* We may ignore items not due to be restored */ + if ((te->reqs & (REQ_DATA | REQ_STATS)) == 0) + continue; + + /* + * Stat the file and, if successful, put its size in dataLength. When + * using compression, the physical file size might not be a very good + * guide to the amount of work involved in restoring the file, but we + * only need an approximate indicator of that. + */ + setFilePath(AH, fname, tctx->filename); + + if (stat(fname, &st) == 0) + te->dataLength = st.st_size; + else if (AH->compression_spec.algorithm != PG_COMPRESSION_NONE) + { + if (AH->compression_spec.algorithm == PG_COMPRESSION_GZIP) + strlcat(fname, ".gz", sizeof(fname)); + else if (AH->compression_spec.algorithm == PG_COMPRESSION_LZ4) + strlcat(fname, ".lz4", sizeof(fname)); + else if (AH->compression_spec.algorithm == PG_COMPRESSION_ZSTD) + strlcat(fname, ".zst", sizeof(fname)); + + if (stat(fname, &st) == 0) + te->dataLength = st.st_size; + } + + /* + * If this is a BLOBS entry, what we stat'd was blobs_NNN.toc, which + * most likely is a lot smaller than the actual blob data. We don't + * have a cheap way to estimate how much smaller, but fortunately it + * doesn't matter too much as long as we get the LOs processed + * reasonably early. Arbitrarily scale up by a factor of 1K. + */ + if (strcmp(te->desc, "BLOBS") == 0) + te->dataLength *= 1024; + } +} + +/* + * Clone format-specific fields during parallel restoration. + */ +static void +_Clone(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + AH->formatData = pg_malloc_object(lclContext); + memcpy(AH->formatData, ctx, sizeof(lclContext)); + ctx = (lclContext *) AH->formatData; + + /* + * TOC-entry-local state isn't an issue because any one TOC entry is + * touched by just one worker child. + */ + + /* + * We also don't copy the ParallelState pointer (pstate), only the leader + * process ever writes to it. + */ +} + +static void +_DeClone(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + free(ctx); +} + +/* + * This function is executed in the child of a parallel backup for a + * directory-format archive and dumps the actual data for one TOC entry. + */ +static int +_WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te) +{ + /* + * This function returns void. We either fail and die horribly or + * succeed... A failure will be detected by the parent when the child dies + * unexpectedly. + */ + WriteDataChunksForTocEntry(AH, te); + + return 0; +} + +/* + * This function is executed in the child of a parallel restore from a + * directory-format archive and restores the actual data for one TOC entry. + */ +static int +_WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te) +{ + return parallel_restore(AH, te); +} diff --git a/src/compat/19/pg_dump/pg_backup_null.c b/src/compat/19/pg_dump/pg_backup_null.c new file mode 100644 index 00000000..a3257f4f --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_null.c @@ -0,0 +1,224 @@ +/*------------------------------------------------------------------------- + * + * pg_backup_null.c + * + * Implementation of an archive that is never saved; it is used by + * pg_dump to output a plain text SQL script instead of saving + * a real archive. + * + * See the headers to pg_restore for more details. + * + * Copyright (c) 2000, Philip Warner + * Rights are granted to use this software in any way so long + * as this notice is not removed. + * + * The author is not responsible for loss or damages that may + * result from its use. + * + * + * IDENTIFICATION + * src/bin/pg_dump/pg_backup_null.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include "fe_utils/string_utils.h" +#include "libpq/libpq-fs.h" +#include "pg_backup_archiver.h" +#include "pg_backup_utils.h" + +static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen); +static void _WriteLOData(ArchiveHandle *AH, const void *data, size_t dLen); +static void _EndData(ArchiveHandle *AH, TocEntry *te); +static int _WriteByte(ArchiveHandle *AH, const int i); +static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len); +static void _CloseArchive(ArchiveHandle *AH); +static void _PrintTocData(ArchiveHandle *AH, TocEntry *te); +static void _StartLOs(ArchiveHandle *AH, TocEntry *te); +static void _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid); +static void _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid); +static void _EndLOs(ArchiveHandle *AH, TocEntry *te); + + +/* + * Initializer + */ +void +InitArchiveFmt_Null(ArchiveHandle *AH) +{ + /* Assuming static functions, this can be copied for each format. */ + AH->WriteDataPtr = _WriteData; + AH->EndDataPtr = _EndData; + AH->WriteBytePtr = _WriteByte; + AH->WriteBufPtr = _WriteBuf; + AH->ClosePtr = _CloseArchive; + AH->ReopenPtr = NULL; + AH->PrintTocDataPtr = _PrintTocData; + + AH->StartLOsPtr = _StartLOs; + AH->StartLOPtr = _StartLO; + AH->EndLOPtr = _EndLO; + AH->EndLOsPtr = _EndLOs; + AH->ClonePtr = NULL; + AH->DeClonePtr = NULL; + + /* + * Now prevent reading... + */ + if (AH->mode == archModeRead) + pg_fatal("this format cannot be read"); +} + +/* + * - Start a new TOC entry + */ + +/* + * Called by dumper via archiver from within a data dump routine + */ +static void +_WriteData(ArchiveHandle *AH, const void *data, size_t dLen) +{ + /* Just send it to output, ahwrite() already errors on failure */ + ahwrite(data, 1, dLen, AH); +} + +/* + * Called by dumper via archiver from within a data dump routine + * We substitute this for _WriteData while emitting a LO + */ +static void +_WriteLOData(ArchiveHandle *AH, const void *data, size_t dLen) +{ + if (dLen > 0) + { + PQExpBuffer buf = createPQExpBuffer(); + + appendByteaLiteralAHX(buf, + (const unsigned char *) data, + dLen, + AH); + + ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data); + + destroyPQExpBuffer(buf); + } +} + +static void +_EndData(ArchiveHandle *AH, TocEntry *te) +{ + ahprintf(AH, "\n\n"); +} + +/* + * Called by the archiver when starting to save BLOB DATA (not schema). + * This routine should save whatever format-specific information is needed + * to read the LOs back into memory. + * + * It is called just prior to the dumper's DataDumper routine. + * + * Optional, but strongly recommended. + */ +static void +_StartLOs(ArchiveHandle *AH, TocEntry *te) +{ + ahprintf(AH, "BEGIN;\n\n"); +} + +/* + * Called by the archiver when the dumper calls StartLO. + * + * Mandatory. + * + * Must save the passed OID for retrieval at restore-time. + */ +static void +_StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid) +{ + bool old_lo_style = (AH->version < K_VERS_1_12); + + if (oid == 0) + pg_fatal("invalid OID for large object"); + + /* With an old archive we must do drop and create logic here */ + if (old_lo_style && AH->public.ropt->dropSchema) + DropLOIfExists(AH, oid); + + if (old_lo_style) + ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n", + oid, INV_WRITE); + else + ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n", + oid, INV_WRITE); + + AH->WriteDataPtr = _WriteLOData; +} + +/* + * Called by the archiver when the dumper calls EndLO. + * + * Optional. + */ +static void +_EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid) +{ + AH->WriteDataPtr = _WriteData; + + ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n"); +} + +/* + * Called by the archiver when finishing saving BLOB DATA. + * + * Optional. + */ +static void +_EndLOs(ArchiveHandle *AH, TocEntry *te) +{ + ahprintf(AH, "COMMIT;\n\n"); +} + +/*------ + * Called as part of a RestoreArchive call; for the NULL archive, this + * just sends the data for a given TOC entry to the output. + *------ + */ +static void +_PrintTocData(ArchiveHandle *AH, TocEntry *te) +{ + if (te->dataDumper) + { + AH->currToc = te; + + if (strcmp(te->desc, "BLOBS") == 0) + _StartLOs(AH, te); + + te->dataDumper((Archive *) AH, te->dataDumperArg); + + if (strcmp(te->desc, "BLOBS") == 0) + _EndLOs(AH, te); + + AH->currToc = NULL; + } +} + +static int +_WriteByte(ArchiveHandle *AH, const int i) +{ + /* Don't do anything */ + return 0; +} + +static void +_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len) +{ + /* Don't do anything */ +} + +static void +_CloseArchive(ArchiveHandle *AH) +{ + /* Nothing to do */ +} diff --git a/src/compat/19/pg_dump/pg_backup_tar.c b/src/compat/19/pg_dump/pg_backup_tar.c new file mode 100644 index 00000000..a3879410 --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_tar.c @@ -0,0 +1,1197 @@ +/*------------------------------------------------------------------------- + * + * pg_backup_tar.c + * + * This file is copied from the 'files' format file, but dumps data into + * one temp file then sends it to the output TAR archive. + * + * The tar format also includes a 'restore.sql' script which is there for + * the benefit of humans. This script is never used by pg_restore. + * + * NOTE: If you untar the created 'tar' file, the resulting files are + * compatible with the 'directory' format. Please keep the two formats in + * sync. + * + * See the headers to pg_backup_directory & pg_restore for more details. + * + * Copyright (c) 2000, Philip Warner + * Rights are granted to use this software in any way so long + * as this notice is not removed. + * + * The author is not responsible for loss or damages that may + * result from its use. + * + * + * IDENTIFICATION + * src/bin/pg_dump/pg_backup_tar.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include +#include +#include +#include + +#include "common/file_utils.h" +#include "fe_utils/string_utils.h" +#include "pg_backup_archiver.h" +#include "pg_backup_tar.h" +#include "pg_backup_utils.h" +#include "pgtar.h" + +static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te); +static void _StartData(ArchiveHandle *AH, TocEntry *te); +static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen); +static void _EndData(ArchiveHandle *AH, TocEntry *te); +static int _WriteByte(ArchiveHandle *AH, const int i); +static int _ReadByte(ArchiveHandle *AH); +static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len); +static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len); +static void _CloseArchive(ArchiveHandle *AH); +static void _PrintTocData(ArchiveHandle *AH, TocEntry *te); +static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te); +static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te); +static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te); + +static void _StartLOs(ArchiveHandle *AH, TocEntry *te); +static void _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid); +static void _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid); +static void _EndLOs(ArchiveHandle *AH, TocEntry *te); + +#define K_STD_BUF_SIZE 1024 + + +typedef struct +{ + FILE *nFH; + FILE *tarFH; + FILE *tmpFH; + char *targetFile; + char mode; + pgoff_t pos; + pgoff_t fileLen; + ArchiveHandle *AH; +} TAR_MEMBER; + +typedef struct +{ + int hasSeek; + pgoff_t filePos; + TAR_MEMBER *loToc; + FILE *tarFH; + pgoff_t tarFHpos; + pgoff_t tarNextMember; + TAR_MEMBER *FH; + int isSpecialScript; + TAR_MEMBER *scriptTH; +} lclContext; + +typedef struct +{ + TAR_MEMBER *TH; + char *filename; +} lclTocEntry; + +static void _LoadLOs(ArchiveHandle *AH, TocEntry *te); + +static TAR_MEMBER *tarOpen(ArchiveHandle *AH, const char *filename, char mode); +static void tarClose(ArchiveHandle *AH, TAR_MEMBER *th); + +#ifdef __NOT_USED__ +static char *tarGets(char *buf, size_t len, TAR_MEMBER *th); +#endif +static int tarPrintf(TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(2, 3); + +static void _tarAddFile(ArchiveHandle *AH, TAR_MEMBER *th); +static TAR_MEMBER *_tarPositionTo(ArchiveHandle *AH, const char *filename); +static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th); +static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th); +static void _tarWriteHeader(TAR_MEMBER *th); +static int _tarGetHeader(ArchiveHandle *AH, TAR_MEMBER *th); +static size_t _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh); + +static size_t _scriptOut(ArchiveHandle *AH, const void *buf, size_t len); + +/* + * Initializer + */ +void +InitArchiveFmt_Tar(ArchiveHandle *AH) +{ + lclContext *ctx; + + /* Assuming static functions, this can be copied for each format. */ + AH->ArchiveEntryPtr = _ArchiveEntry; + AH->StartDataPtr = _StartData; + AH->WriteDataPtr = _WriteData; + AH->EndDataPtr = _EndData; + AH->WriteBytePtr = _WriteByte; + AH->ReadBytePtr = _ReadByte; + AH->WriteBufPtr = _WriteBuf; + AH->ReadBufPtr = _ReadBuf; + AH->ClosePtr = _CloseArchive; + AH->ReopenPtr = NULL; + AH->PrintTocDataPtr = _PrintTocData; + AH->ReadExtraTocPtr = _ReadExtraToc; + AH->WriteExtraTocPtr = _WriteExtraToc; + AH->PrintExtraTocPtr = _PrintExtraToc; + + AH->StartLOsPtr = _StartLOs; + AH->StartLOPtr = _StartLO; + AH->EndLOPtr = _EndLO; + AH->EndLOsPtr = _EndLOs; + AH->ClonePtr = NULL; + AH->DeClonePtr = NULL; + + AH->WorkerJobDumpPtr = NULL; + AH->WorkerJobRestorePtr = NULL; + + /* + * Set up some special context used in compressing data. + */ + ctx = pg_malloc0_object(lclContext); + AH->formatData = ctx; + ctx->filePos = 0; + ctx->isSpecialScript = 0; + + /* + * Now open the tar file, and load the TOC if we're in read mode. + */ + if (AH->mode == archModeWrite) + { + if (AH->fSpec && strcmp(AH->fSpec, "") != 0) + { + ctx->tarFH = fopen(AH->fSpec, PG_BINARY_W); + if (ctx->tarFH == NULL) + pg_fatal("could not open TOC file \"%s\" for output: %m", + AH->fSpec); + } + else + { + ctx->tarFH = stdout; + if (ctx->tarFH == NULL) + pg_fatal("could not open TOC file for output: %m"); + } + + ctx->tarFHpos = 0; + + /* + * Make unbuffered since we will dup() it, and the buffers screw each + * other + */ + /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */ + + ctx->hasSeek = checkSeek(ctx->tarFH); + + /* + * We don't support compression because reading the files back is not + * possible since gzdopen uses buffered IO which totally screws file + * positioning. + */ + if (AH->compression_spec.algorithm != PG_COMPRESSION_NONE) + pg_fatal("compression is not supported by tar archive format"); + } + else + { /* Read Mode */ + if (AH->fSpec && strcmp(AH->fSpec, "") != 0) + { + ctx->tarFH = fopen(AH->fSpec, PG_BINARY_R); + if (ctx->tarFH == NULL) + pg_fatal("could not open TOC file \"%s\" for input: %m", + AH->fSpec); + } + else + { + ctx->tarFH = stdin; + if (ctx->tarFH == NULL) + pg_fatal("could not open TOC file for input: %m"); + } + + /* + * Make unbuffered since we will dup() it, and the buffers screw each + * other + */ + /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */ + + ctx->tarFHpos = 0; + + ctx->hasSeek = checkSeek(ctx->tarFH); + + ctx->FH = tarOpen(AH, "toc.dat", 'r'); + ReadHead(AH); + ReadToc(AH); + tarClose(AH, ctx->FH); /* Nothing else in the file... */ + } +} + +/* + * - Start a new TOC entry + * Setup the output file name. + */ +static void +_ArchiveEntry(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *ctx; + char fn[K_STD_BUF_SIZE]; + + ctx = pg_malloc0_object(lclTocEntry); + if (te->dataDumper != NULL) + { + snprintf(fn, sizeof(fn), "%d.dat", te->dumpId); + ctx->filename = pg_strdup(fn); + } + else + { + ctx->filename = NULL; + ctx->TH = NULL; + } + te->formatData = ctx; +} + +static void +_WriteExtraToc(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *ctx = (lclTocEntry *) te->formatData; + + if (ctx->filename) + WriteStr(AH, ctx->filename); + else + WriteStr(AH, ""); +} + +static void +_ReadExtraToc(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *ctx = (lclTocEntry *) te->formatData; + + if (ctx == NULL) + { + ctx = pg_malloc0_object(lclTocEntry); + te->formatData = ctx; + } + + ctx->filename = ReadStr(AH); + if (strlen(ctx->filename) == 0) + { + free(ctx->filename); + ctx->filename = NULL; + } + ctx->TH = NULL; +} + +static void +_PrintExtraToc(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *ctx = (lclTocEntry *) te->formatData; + + if (AH->public.verbose && ctx->filename != NULL) + ahprintf(AH, "-- File: %s\n", ctx->filename); +} + +static void +_StartData(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + tctx->TH = tarOpen(AH, tctx->filename, 'w'); +} + +static TAR_MEMBER * +tarOpen(ArchiveHandle *AH, const char *filename, char mode) +{ + lclContext *ctx = (lclContext *) AH->formatData; + TAR_MEMBER *tm; + + if (mode == 'r') + { + tm = _tarPositionTo(AH, filename); + if (!tm) /* Not found */ + { + if (filename) + { + /* + * Couldn't find the requested file. Future: do SEEK(0) and + * retry. + */ + pg_fatal("could not find file \"%s\" in archive", filename); + } + else + { + /* Any file OK, none left, so return NULL */ + return NULL; + } + } + + if (AH->compression_spec.algorithm == PG_COMPRESSION_NONE) + tm->nFH = ctx->tarFH; + else + pg_fatal("compression is not supported by tar archive format"); + } + else + { + int old_umask; + + tm = pg_malloc0_object(TAR_MEMBER); + + /* + * POSIX does not require, but permits, tmpfile() to restrict file + * permissions. Given an OS crash after we write data, the filesystem + * might retain the data but forget tmpfile()'s unlink(). If so, the + * file mode protects confidentiality of the data written. + */ + old_umask = umask(S_IRWXG | S_IRWXO); + +#ifndef WIN32 + tm->tmpFH = tmpfile(); +#else + + /* + * On WIN32, tmpfile() generates a filename in the root directory, + * which requires administrative permissions on certain systems. Loop + * until we find a unique file name we can create. + */ + while (1) + { + char *name; + int fd; + + name = _tempnam(NULL, "pg_temp_"); + if (name == NULL) + break; + fd = open(name, O_RDWR | O_CREAT | O_EXCL | O_BINARY | + O_TEMPORARY, S_IRUSR | S_IWUSR); + free(name); + + if (fd != -1) /* created a file */ + { + tm->tmpFH = fdopen(fd, "w+b"); + break; + } + else if (errno != EEXIST) /* failure other than file exists */ + break; + } +#endif + + if (tm->tmpFH == NULL) + pg_fatal("could not generate temporary file name: %m"); + + umask(old_umask); + + if (AH->compression_spec.algorithm == PG_COMPRESSION_NONE) + tm->nFH = tm->tmpFH; + else + pg_fatal("compression is not supported by tar archive format"); + + tm->AH = AH; + tm->targetFile = pg_strdup(filename); + } + + tm->mode = mode; + tm->tarFH = ctx->tarFH; + + return tm; +} + +static void +tarClose(ArchiveHandle *AH, TAR_MEMBER *th) +{ + if (AH->compression_spec.algorithm != PG_COMPRESSION_NONE) + pg_fatal("compression is not supported by tar archive format"); + + if (th->mode == 'w') + _tarAddFile(AH, th); /* This will close the temp file */ + + /* + * else Nothing to do for normal read since we don't dup() normal file + * handle, and we don't use temp files. + */ + + free(th->targetFile); + + th->nFH = NULL; +} + +#ifdef __NOT_USED__ +static char * +tarGets(char *buf, size_t len, TAR_MEMBER *th) +{ + char *s; + size_t cnt = 0; + char c = ' '; + int eof = 0; + + /* Can't read past logical EOF */ + if (len > (th->fileLen - th->pos)) + len = th->fileLen - th->pos; + + while (cnt < len && c != '\n') + { + if (_tarReadRaw(th->AH, &c, 1, th, NULL) <= 0) + { + eof = 1; + break; + } + buf[cnt++] = c; + } + + if (eof && cnt == 0) + s = NULL; + else + { + buf[cnt++] = '\0'; + s = buf; + } + + if (s) + { + len = strlen(s); + th->pos += len; + } + + return s; +} +#endif + +/* + * Just read bytes from the archive. This is the low level read routine + * that is used for ALL reads on a tar file. + */ +static size_t +_tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh) +{ + lclContext *ctx = (lclContext *) AH->formatData; + size_t avail; + size_t used = 0; + size_t res = 0; + + Assert(th || fh); + + avail = AH->lookaheadLen - AH->lookaheadPos; + if (avail > 0) + { + /* We have some lookahead bytes to use */ + if (avail >= len) /* Just use the lookahead buffer */ + used = len; + else + used = avail; + + /* Copy, and adjust buffer pos */ + memcpy(buf, AH->lookahead + AH->lookaheadPos, used); + AH->lookaheadPos += used; + + /* Adjust required length */ + len -= used; + } + + /* Read the file if len > 0 */ + if (len > 0) + { + if (fh) + { + res = fread(&((char *) buf)[used], 1, len, fh); + if (res != len && !feof(fh)) + READ_ERROR_EXIT(fh); + } + else if (th) + { + res = fread(&((char *) buf)[used], 1, len, th->nFH); + if (res != len && !feof(th->nFH)) + READ_ERROR_EXIT(th->nFH); + } + } + + ctx->tarFHpos += res + used; + + return (res + used); +} + +static size_t +tarRead(void *buf, size_t len, TAR_MEMBER *th) +{ + size_t res; + + if (th->pos + len > th->fileLen) + len = th->fileLen - th->pos; + + if (len <= 0) + return 0; + + res = _tarReadRaw(th->AH, buf, len, th, NULL); + + th->pos += res; + + return res; +} + +static size_t +tarWrite(const void *buf, size_t len, TAR_MEMBER *th) +{ + size_t res; + + res = fwrite(buf, 1, len, th->nFH); + + th->pos += res; + return res; +} + +static void +_WriteData(ArchiveHandle *AH, const void *data, size_t dLen) +{ + lclTocEntry *tctx = (lclTocEntry *) AH->currToc->formatData; + + if (tarWrite(data, dLen, tctx->TH) != dLen) + WRITE_ERROR_EXIT; +} + +static void +_EndData(ArchiveHandle *AH, TocEntry *te) +{ + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + /* Close the file */ + tarClose(AH, tctx->TH); + tctx->TH = NULL; +} + +/* + * Print data for a given file + */ +static void +_PrintFileData(ArchiveHandle *AH, char *filename) +{ + lclContext *ctx = (lclContext *) AH->formatData; + char buf[4096]; + size_t cnt; + TAR_MEMBER *th; + + if (!filename) + return; + + th = tarOpen(AH, filename, 'r'); + ctx->FH = th; + + while ((cnt = tarRead(buf, 4095, th)) > 0) + { + buf[cnt] = '\0'; + ahwrite(buf, 1, cnt, AH); + } + + tarClose(AH, th); +} + + +/* + * Print data for a given TOC entry +*/ +static void +_PrintTocData(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + int pos1; + + if (!tctx->filename) + return; + + /* + * If we're writing the special restore.sql script, emit a suitable + * command to include each table's data from the corresponding file. + * + * In the COPY case this is a bit klugy because the regular COPY command + * was already printed before we get control. + */ + if (ctx->isSpecialScript) + { + if (te->copyStmt) + { + /* Abort the COPY FROM stdin */ + ahprintf(AH, "\\.\n"); + + /* + * The COPY statement should look like "COPY ... FROM stdin;\n", + * see dumpTableData(). + */ + pos1 = (int) strlen(te->copyStmt) - 13; + if (pos1 < 6 || strncmp(te->copyStmt, "COPY ", 5) != 0 || + strcmp(te->copyStmt + pos1, " FROM stdin;\n") != 0) + pg_fatal("unexpected COPY statement syntax: \"%s\"", + te->copyStmt); + + /* Emit all but the FROM part ... */ + ahwrite(te->copyStmt, 1, pos1, AH); + /* ... and insert modified FROM */ + ahprintf(AH, " FROM '$$PATH$$/%s';\n\n", tctx->filename); + } + else + { + /* --inserts mode, no worries, just include the data file */ + ahprintf(AH, "\\i $$PATH$$/%s\n\n", tctx->filename); + } + + return; + } + + if (strcmp(te->desc, "BLOBS") == 0) + _LoadLOs(AH, te); + else + _PrintFileData(AH, tctx->filename); +} + +static void +_LoadLOs(ArchiveHandle *AH, TocEntry *te) +{ + Oid oid; + lclContext *ctx = (lclContext *) AH->formatData; + TAR_MEMBER *th; + size_t cnt; + bool foundLO = false; + char buf[4096]; + + StartRestoreLOs(AH); + + /* + * The blobs_NNN.toc or blobs.toc file is fairly useless to us because it + * will appear only after the associated blob_NNN.dat files. For archive + * versions >= 16 we can look at the BLOBS entry's te->tag to discover the + * OID of the first blob we want to restore, and then search forward to + * find the appropriate blob_.dat file. For older versions we rely + * on the knowledge that there was only one BLOBS entry and just search + * for the first blob_.dat file. Once we find the first blob file to + * restore, restore all blobs until we reach the blobs[_NNN].toc file. + */ + if (AH->version >= K_VERS_1_16) + { + /* We rely on atooid to not complain about nnnn..nnnn tags */ + oid = atooid(te->tag); + snprintf(buf, sizeof(buf), "blob_%u.dat", oid); + th = tarOpen(AH, buf, 'r'); /* Advance to first desired file */ + } + else + th = tarOpen(AH, NULL, 'r'); /* Open next file */ + + while (th != NULL) + { + ctx->FH = th; + + if (strncmp(th->targetFile, "blob_", 5) == 0) + { + oid = atooid(&th->targetFile[5]); + if (oid != 0) + { + pg_log_info("restoring large object with OID %u", oid); + + StartRestoreLO(AH, oid, AH->public.ropt->dropSchema); + + while ((cnt = tarRead(buf, 4095, th)) > 0) + { + buf[cnt] = '\0'; + ahwrite(buf, 1, cnt, AH); + } + EndRestoreLO(AH, oid); + foundLO = true; + } + tarClose(AH, th); + } + else + { + tarClose(AH, th); + + /* + * Once we have found the first LO, stop at the first non-LO entry + * (which will be 'blobs[_NNN].toc'). This coding would eat all + * the rest of the archive if there are no LOs ... but this + * function shouldn't be called at all in that case. + */ + if (foundLO) + break; + } + + th = tarOpen(AH, NULL, 'r'); + } + EndRestoreLOs(AH); +} + + +static int +_WriteByte(ArchiveHandle *AH, const int i) +{ + lclContext *ctx = (lclContext *) AH->formatData; + char b = i; /* Avoid endian problems */ + + if (tarWrite(&b, 1, ctx->FH) != 1) + WRITE_ERROR_EXIT; + + ctx->filePos += 1; + return 1; +} + +static int +_ReadByte(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + size_t res; + unsigned char c; + + res = tarRead(&c, 1, ctx->FH); + if (res != 1) + /* We already would have exited for errors on reads, must be EOF */ + pg_fatal("could not read from input file: end of file"); + ctx->filePos += 1; + return c; +} + +static void +_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + if (tarWrite(buf, len, ctx->FH) != len) + WRITE_ERROR_EXIT; + + ctx->filePos += len; +} + +static void +_ReadBuf(ArchiveHandle *AH, void *buf, size_t len) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + if (tarRead(buf, len, ctx->FH) != len) + /* We already would have exited for errors on reads, must be EOF */ + pg_fatal("could not read from input file: end of file"); + + ctx->filePos += len; +} + +static void +_CloseArchive(ArchiveHandle *AH) +{ + lclContext *ctx = (lclContext *) AH->formatData; + TAR_MEMBER *th; + RestoreOptions *ropt; + RestoreOptions *savRopt; + DumpOptions *savDopt; + int savVerbose, + i; + + if (AH->mode == archModeWrite) + { + /* + * Write the Header & TOC to the archive FIRST + */ + th = tarOpen(AH, "toc.dat", 'w'); + ctx->FH = th; + WriteHead(AH); + WriteToc(AH); + tarClose(AH, th); /* Not needed any more */ + + /* + * Now send the data (tables & LOs) + */ + WriteDataChunks(AH, NULL); + + /* + * Now this format wants to append a script which does a full restore + * if the files have been extracted. + */ + th = tarOpen(AH, "restore.sql", 'w'); + + tarPrintf(th, "--\n" + "-- NOTE:\n" + "--\n" + "-- File paths need to be edited. Search for $$PATH$$ and\n" + "-- replace it with the path to the directory containing\n" + "-- the extracted data files.\n" + "--\n"); + + AH->CustomOutPtr = _scriptOut; + + ctx->isSpecialScript = 1; + ctx->scriptTH = th; + + ropt = NewRestoreOptions(); + memcpy(ropt, AH->public.ropt, sizeof(RestoreOptions)); + ropt->filename = NULL; + ropt->dropSchema = 1; + ropt->superuser = NULL; + ropt->suppressDumpWarnings = true; + + savDopt = AH->public.dopt; + savRopt = AH->public.ropt; + + SetArchiveOptions((Archive *) AH, NULL, ropt); + + savVerbose = AH->public.verbose; + AH->public.verbose = 0; + + RestoreArchive((Archive *) AH, false); + + SetArchiveOptions((Archive *) AH, savDopt, savRopt); + + AH->public.verbose = savVerbose; + + tarClose(AH, th); + + ctx->isSpecialScript = 0; + + /* + * EOF marker for tar files is two blocks of NULLs. + */ + for (i = 0; i < TAR_BLOCK_SIZE * 2; i++) + { + if (fputc(0, ctx->tarFH) == EOF) + WRITE_ERROR_EXIT; + } + + /* Sync the output file if one is defined */ + if (AH->dosync && AH->fSpec) + (void) fsync_fname(AH->fSpec, false); + } + + AH->FH = NULL; +} + +static size_t +_scriptOut(ArchiveHandle *AH, const void *buf, size_t len) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + return tarWrite(buf, len, ctx->scriptTH); +} + +/* + * Large Object support + */ + +/* + * Called by the archiver when starting to save BLOB DATA (not schema). + * This routine should save whatever format-specific information is needed + * to read the LOs back into memory. + * + * It is called just prior to the dumper's DataDumper routine. + * + * Optional, but strongly recommended. + * + */ +static void +_StartLOs(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + char fname[K_STD_BUF_SIZE]; + + sprintf(fname, "blobs_%d.toc", te->dumpId); + ctx->loToc = tarOpen(AH, fname, 'w'); +} + +/* + * Called by the archiver when the dumper calls StartLO. + * + * Mandatory. + * + * Must save the passed OID for retrieval at restore-time. + */ +static void +_StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid) +{ + lclContext *ctx = (lclContext *) AH->formatData; + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + char fname[255]; + + if (oid == 0) + pg_fatal("invalid OID for large object (%u)", oid); + + if (AH->compression_spec.algorithm != PG_COMPRESSION_NONE) + pg_fatal("compression is not supported by tar archive format"); + + sprintf(fname, "blob_%u.dat", oid); + + tarPrintf(ctx->loToc, "%u %s\n", oid, fname); + + tctx->TH = tarOpen(AH, fname, 'w'); +} + +/* + * Called by the archiver when the dumper calls EndLO. + * + * Optional. + * + */ +static void +_EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid) +{ + lclTocEntry *tctx = (lclTocEntry *) te->formatData; + + tarClose(AH, tctx->TH); +} + +/* + * Called by the archiver when finishing saving BLOB DATA. + * + * Optional. + * + */ +static void +_EndLOs(ArchiveHandle *AH, TocEntry *te) +{ + lclContext *ctx = (lclContext *) AH->formatData; + + /* Write out a fake zero OID to mark end-of-LOs. */ + /* WriteInt(AH, 0); */ + + tarClose(AH, ctx->loToc); +} + + + +/*------------ + * TAR Support + *------------ + */ + +static int +tarPrintf(TAR_MEMBER *th, const char *fmt,...) +{ + int save_errno = errno; + char *p; + size_t len = 128; /* initial assumption about buffer size */ + size_t cnt; + + for (;;) + { + va_list args; + + /* Allocate work buffer. */ + p = (char *) pg_malloc(len); + + /* Try to format the data. */ + errno = save_errno; + va_start(args, fmt); + cnt = pvsnprintf(p, len, fmt, args); + va_end(args); + + if (cnt < len) + break; /* success */ + + /* Release buffer and loop around to try again with larger len. */ + free(p); + len = cnt; + } + + cnt = tarWrite(p, cnt, th); + free(p); + return (int) cnt; +} + +/* Given the member, write the TAR header & copy the file */ +static void +_tarAddFile(ArchiveHandle *AH, TAR_MEMBER *th) +{ + lclContext *ctx = (lclContext *) AH->formatData; + FILE *tmp = th->tmpFH; /* Grab it for convenience */ + char buf[32768]; + size_t cnt; + pgoff_t len = 0; + size_t res; + size_t i, + pad; + + /* + * Find file len & go back to start. + */ + if (fseeko(tmp, 0, SEEK_END) != 0) + pg_fatal("error during file seek: %m"); + th->fileLen = ftello(tmp); + if (th->fileLen < 0) + pg_fatal("could not determine seek position in archive file: %m"); + if (fseeko(tmp, 0, SEEK_SET) != 0) + pg_fatal("error during file seek: %m"); + + _tarWriteHeader(th); + + while ((cnt = fread(buf, 1, sizeof(buf), tmp)) > 0) + { + if ((res = fwrite(buf, 1, cnt, th->tarFH)) != cnt) + WRITE_ERROR_EXIT; + len += res; + } + if (!feof(tmp)) + READ_ERROR_EXIT(tmp); + + if (fclose(tmp) != 0) /* This *should* delete it... */ + pg_fatal("could not close temporary file: %m"); + + if (len != th->fileLen) + pg_fatal("actual file length (%lld) does not match expected (%lld)", + (long long) len, (long long) th->fileLen); + + pad = tarPaddingBytesRequired(len); + for (i = 0; i < pad; i++) + { + if (fputc('\0', th->tarFH) == EOF) + WRITE_ERROR_EXIT; + } + + ctx->tarFHpos += len + pad; +} + +/* Locate the file in the archive, read header and position to data */ +static TAR_MEMBER * +_tarPositionTo(ArchiveHandle *AH, const char *filename) +{ + lclContext *ctx = (lclContext *) AH->formatData; + TAR_MEMBER *th = pg_malloc0_object(TAR_MEMBER); + char c; + char header[TAR_BLOCK_SIZE]; + size_t i, + len, + blks; + int id; + + th->AH = AH; + + /* Go to end of current file, if any */ + if (ctx->tarFHpos != 0) + { + pg_log_debug("moving from position %lld to next member at file position %lld", + (long long) ctx->tarFHpos, (long long) ctx->tarNextMember); + + while (ctx->tarFHpos < ctx->tarNextMember) + _tarReadRaw(AH, &c, 1, NULL, ctx->tarFH); + } + + pg_log_debug("now at file position %lld", (long long) ctx->tarFHpos); + + /* We are at the start of the file, or at the next member */ + + /* Get the header */ + if (!_tarGetHeader(AH, th)) + { + if (filename) + pg_fatal("could not find header for file \"%s\" in tar archive", filename); + else + { + /* + * We're just scanning the archive for the next file, so return + * null + */ + free(th); + return NULL; + } + } + + while (filename != NULL && strcmp(th->targetFile, filename) != 0) + { + pg_log_debug("skipping tar member %s", th->targetFile); + + id = atoi(th->targetFile); + if ((TocIDRequired(AH, id) & REQ_DATA) != 0) + pg_fatal("restoring data out of order is not supported in this archive format: " + "\"%s\" is required, but comes before \"%s\" in the archive file.", + th->targetFile, filename); + + /* Header doesn't match, so read to next header */ + len = th->fileLen; + len += tarPaddingBytesRequired(th->fileLen); + blks = len / TAR_BLOCK_SIZE; /* # of tar blocks */ + + for (i = 0; i < blks; i++) + _tarReadRaw(AH, &header[0], TAR_BLOCK_SIZE, NULL, ctx->tarFH); + + if (!_tarGetHeader(AH, th)) + pg_fatal("could not find header for file \"%s\" in tar archive", filename); + } + + ctx->tarNextMember = ctx->tarFHpos + th->fileLen + + tarPaddingBytesRequired(th->fileLen); + th->pos = 0; + + return th; +} + +/* Read & verify a header */ +static int +_tarGetHeader(ArchiveHandle *AH, TAR_MEMBER *th) +{ + lclContext *ctx = (lclContext *) AH->formatData; + char h[TAR_BLOCK_SIZE]; + char tag[100 + 1]; + int sum, + chk; + pgoff_t len; + pgoff_t hPos; + bool gotBlock = false; + + while (!gotBlock) + { + /* Save the pos for reporting purposes */ + hPos = ctx->tarFHpos; + + /* Read the next tar block, return EOF, exit if short */ + len = _tarReadRaw(AH, h, TAR_BLOCK_SIZE, NULL, ctx->tarFH); + if (len == 0) /* EOF */ + return 0; + + if (len != TAR_BLOCK_SIZE) + pg_fatal(ngettext("incomplete tar header found (%lu byte)", + "incomplete tar header found (%lu bytes)", + len), + (unsigned long) len); + + /* Calc checksum */ + chk = tarChecksum(h); + sum = read_tar_number(&h[TAR_OFFSET_CHECKSUM], 8); + + /* + * If the checksum failed, see if it is a null block. If so, silently + * continue to the next block. + */ + if (chk == sum) + gotBlock = true; + else + { + int i; + + for (i = 0; i < TAR_BLOCK_SIZE; i++) + { + if (h[i] != 0) + { + gotBlock = true; + break; + } + } + } + } + + /* Name field is 100 bytes, might not be null-terminated */ + strlcpy(tag, &h[TAR_OFFSET_NAME], 100 + 1); + + len = read_tar_number(&h[TAR_OFFSET_SIZE], 12); + + pg_log_debug("TOC Entry %s at %llu (length %llu, checksum %d)", + tag, (unsigned long long) hPos, (unsigned long long) len, sum); + + if (chk != sum) + pg_fatal("corrupt tar header found in %s (expected %d, computed %d) file position %llu", + tag, sum, chk, (unsigned long long) ftello(ctx->tarFH)); + + th->targetFile = pg_strdup(tag); + th->fileLen = len; + + return 1; +} + + +static void +_tarWriteHeader(TAR_MEMBER *th) +{ + char h[TAR_BLOCK_SIZE]; + + tarCreateHeader(h, th->targetFile, NULL, th->fileLen, + 0600, 04000, 02000, time(NULL)); + + /* Now write the completed header. */ + if (fwrite(h, 1, TAR_BLOCK_SIZE, th->tarFH) != TAR_BLOCK_SIZE) + WRITE_ERROR_EXIT; +} diff --git a/src/compat/19/pg_dump/pg_backup_tar.h b/src/compat/19/pg_dump/pg_backup_tar.h new file mode 100644 index 00000000..0277f08f --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_tar.h @@ -0,0 +1,37 @@ +/* + * src/bin/pg_dump/pg_backup_tar.h + * + * TAR Header (see "ustar interchange format" in POSIX 1003.1) + * + * Offset Length Contents + * 0 100 bytes File name ('\0' terminated, 99 maximum length) + * 100 8 bytes File mode (in octal ascii) + * 108 8 bytes User ID (in octal ascii) + * 116 8 bytes Group ID (in octal ascii) + * 124 12 bytes File size (in octal ascii) + * 136 12 bytes Modify time (Unix timestamp in octal ascii) + * 148 8 bytes Header checksum (in octal ascii) + * 156 1 bytes Type flag (see below) + * 157 100 bytes Linkname, if symlink ('\0' terminated, 99 maximum length) + * 257 6 bytes Magic ("ustar\0") + * 263 2 bytes Version ("00") + * 265 32 bytes User name ('\0' terminated, 31 maximum length) + * 297 32 bytes Group name ('\0' terminated, 31 maximum length) + * 329 8 bytes Major device ID (in octal ascii) + * 337 8 bytes Minor device ID (in octal ascii) + * 345 155 bytes File name prefix (not used in our implementation) + * 500 12 bytes Padding + * + * 512 (s+p)bytes File contents, padded out to 512-byte boundary + */ + +/* The type flag defines the type of file */ +#define LF_OLDNORMAL '\0' /* Normal disk file, Unix compatible */ +#define LF_NORMAL '0' /* Normal disk file */ +#define LF_LINK '1' /* Link to previously dumped file */ +#define LF_SYMLINK '2' /* Symbolic link */ +#define LF_CHR '3' /* Character special file */ +#define LF_BLK '4' /* Block special file */ +#define LF_DIR '5' /* Directory */ +#define LF_FIFO '6' /* FIFO special file */ +#define LF_CONTIG '7' /* Contiguous file */ diff --git a/src/compat/19/pg_dump/pg_backup_utils.c b/src/compat/19/pg_dump/pg_backup_utils.c new file mode 100644 index 00000000..0368f762 --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_utils.c @@ -0,0 +1,106 @@ +/*------------------------------------------------------------------------- + * + * pg_backup_utils.c + * Utility routines shared by pg_dump and pg_restore + * + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/bin/pg_dump/pg_backup_utils.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#ifdef WIN32 +#include "parallel.h" +#endif +#include "pg_backup_utils.h" + +/* Globals exported by this file */ +const char *progname = NULL; + +#define MAX_ON_EXIT_NICELY 20 + +static struct +{ + on_exit_nicely_callback function; + void *arg; +} on_exit_nicely_list[MAX_ON_EXIT_NICELY]; + +static int on_exit_nicely_index; + +/* + * Parse a --section=foo command line argument. + * + * Set or update the bitmask in *dumpSections according to arg. + * dumpSections is initialised as DUMP_UNSECTIONED by pg_dump and + * pg_restore so they can know if this has even been called. + */ +void +set_dump_section(const char *arg, int *dumpSections) +{ + /* if this is the first call, clear all the bits */ + if (*dumpSections == DUMP_UNSECTIONED) + *dumpSections = 0; + + if (strcmp(arg, "pre-data") == 0) + *dumpSections |= DUMP_PRE_DATA; + else if (strcmp(arg, "data") == 0) + *dumpSections |= DUMP_DATA; + else if (strcmp(arg, "post-data") == 0) + *dumpSections |= DUMP_POST_DATA; + else + { + pg_log_error("unrecognized section name: \"%s\"", arg); + pg_log_error_hint("Try \"%s --help\" for more information.", progname); + exit_nicely(1); + } +} + + +/* Register a callback to be run when exit_nicely is invoked. */ +void +on_exit_nicely(on_exit_nicely_callback function, void *arg) +{ + if (on_exit_nicely_index >= MAX_ON_EXIT_NICELY) + pg_fatal("out of on_exit_nicely slots"); + on_exit_nicely_list[on_exit_nicely_index].function = function; + on_exit_nicely_list[on_exit_nicely_index].arg = arg; + on_exit_nicely_index++; +} + +/* + * Run accumulated on_exit_nicely callbacks in reverse order and then exit + * without printing any message. + * + * If running in a parallel worker thread on Windows, we only exit the thread, + * not the whole process. + * + * Note that in parallel operation on Windows, the callback(s) will be run + * by each thread since the list state is necessarily shared by all threads; + * each callback must contain logic to ensure it does only what's appropriate + * for its thread. On Unix, callbacks are also run by each process, but only + * for callbacks established before we fork off the child processes. (It'd + * be cleaner to reset the list after fork(), and let each child establish + * its own callbacks; but then the behavior would be completely inconsistent + * between Windows and Unix. For now, just be sure to establish callbacks + * before forking to avoid inconsistency.) + */ +void +exit_nicely(int code) +{ + int i; + + for (i = on_exit_nicely_index - 1; i >= 0; i--) + on_exit_nicely_list[i].function(code, + on_exit_nicely_list[i].arg); + +#ifdef WIN32 + if (parallel_init_done && GetCurrentThreadId() != mainThreadId) + _endthreadex(code); +#endif + + exit(code); +} diff --git a/src/compat/19/pg_dump/pg_backup_utils.h b/src/compat/19/pg_dump/pg_backup_utils.h new file mode 100644 index 00000000..9e98ed11 --- /dev/null +++ b/src/compat/19/pg_dump/pg_backup_utils.h @@ -0,0 +1,41 @@ +/*------------------------------------------------------------------------- + * + * pg_backup_utils.h + * Utility routines shared by pg_dump and pg_restore. + * + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/bin/pg_dump/pg_backup_utils.h + * + *------------------------------------------------------------------------- + */ + +#ifndef PG_BACKUP_UTILS_H +#define PG_BACKUP_UTILS_H + +#include "common/logging.h" + +/* bits returned by set_dump_section */ +#define DUMP_PRE_DATA 0x01 +#define DUMP_DATA 0x02 +#define DUMP_POST_DATA 0x04 +#define DUMP_UNSECTIONED 0xff + +typedef void (*on_exit_nicely_callback) (int code, void *arg); + +extern const char *progname; + +extern void set_dump_section(const char *arg, int *dumpSections); +extern void on_exit_nicely(on_exit_nicely_callback function, void *arg); +pg_noreturn extern void exit_nicely(int code); + +/* In pg_dump, we modify pg_fatal to call exit_nicely instead of exit */ +#undef pg_fatal +#define pg_fatal(...) do { \ + pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \ + exit_nicely(1); \ + } while(0) + +#endif /* PG_BACKUP_UTILS_H */ diff --git a/src/compat/19/pg_dump/pg_dump.c b/src/compat/19/pg_dump/pg_dump.c new file mode 100644 index 00000000..f6b479ea --- /dev/null +++ b/src/compat/19/pg_dump/pg_dump.c @@ -0,0 +1,21134 @@ +/*------------------------------------------------------------------------- + * + * pg_dump.c + * pg_dump is a utility for dumping out a postgres database + * into a script file. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * pg_dump will read the system catalogs in a database and dump out a + * script that reproduces the schema in terms of SQL that is understood + * by PostgreSQL + * + * Note that pg_dump runs in a transaction-snapshot mode transaction, + * so it sees a consistent snapshot of the database including system + * catalogs. However, it relies in part on various specialized backend + * functions like pg_get_indexdef(), and those things tend to look at + * the currently committed state. So it is possible to get 'cache + * lookup failed' error if someone performs DDL changes while a dump is + * happening. The window for this sort of thing is from the acquisition + * of the transaction snapshot to getSchemaData() (when pg_dump acquires + * AccessShareLock on every table it intends to dump). It isn't very large, + * but it can happen. + * + * http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php + * + * IDENTIFICATION + * src/bin/pg_dump/pg_dump.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include +#include +#include +#ifdef HAVE_TERMIOS_H +#include +#endif + +#include "access/attnum.h" +#include "access/sysattr.h" +#include "access/transam.h" +#include "catalog/pg_aggregate_d.h" +#include "catalog/pg_am_d.h" +#include "catalog/pg_attribute_d.h" +#include "catalog/pg_authid_d.h" +#include "catalog/pg_cast_d.h" +#include "catalog/pg_class_d.h" +#include "catalog/pg_constraint_d.h" +#include "catalog/pg_default_acl_d.h" +#include "catalog/pg_largeobject_d.h" +#include "catalog/pg_largeobject_metadata_d.h" +#include "catalog/pg_proc_d.h" +#include "catalog/pg_publication_d.h" +#include "catalog/pg_shdepend_d.h" +#include "catalog/pg_subscription_d.h" +#include "catalog/pg_type_d.h" +#include "common/connect.h" +#include "common/int.h" +#include "common/relpath.h" +#include "common/shortest_dec.h" +#include "compress_io.h" +#include "dumputils.h" +#include "fe_utils/option_utils.h" +#include "fe_utils/string_utils.h" +#include "filter.h" +#include "getopt_long.h" +#include "libpq/libpq-fs.h" +#include "parallel.h" +#include "pg_backup_db.h" +#include "pg_backup_utils.h" +#include "pg_dump.h" +#include "statistics/statistics_format.h" +#include "storage/block.h" + +typedef struct +{ + Oid roleoid; /* role's OID */ + const char *rolename; /* role's name */ +} RoleNameItem; + +typedef struct +{ + const char *descr; /* comment for an object */ + Oid classoid; /* object class (catalog OID) */ + Oid objoid; /* object OID */ + int objsubid; /* subobject (table column #) */ +} CommentItem; + +typedef struct +{ + const char *provider; /* label provider of this security label */ + const char *label; /* security label for an object */ + Oid classoid; /* object class (catalog OID) */ + Oid objoid; /* object OID */ + int objsubid; /* subobject (table column #) */ +} SecLabelItem; + +typedef struct +{ + Oid oid; /* object OID */ + char relkind; /* object kind */ + RelFileNumber relfilenumber; /* object filenode */ + Oid toast_oid; /* toast table OID */ + RelFileNumber toast_relfilenumber; /* toast table filenode */ + Oid toast_index_oid; /* toast table index OID */ + RelFileNumber toast_index_relfilenumber; /* toast table index filenode */ +} BinaryUpgradeClassOidItem; + +/* sequence types */ +typedef enum SeqType +{ + SEQTYPE_SMALLINT, + SEQTYPE_INTEGER, + SEQTYPE_BIGINT, +} SeqType; + +static const char *const SeqTypeNames[] = +{ + [SEQTYPE_SMALLINT] = "smallint", + [SEQTYPE_INTEGER] = "integer", + [SEQTYPE_BIGINT] = "bigint", +}; + +StaticAssertDecl(lengthof(SeqTypeNames) == (SEQTYPE_BIGINT + 1), + "array length mismatch"); + +typedef struct +{ + Oid oid; /* sequence OID */ + SeqType seqtype; /* data type of sequence */ + bool cycled; /* whether sequence cycles */ + int64 minv; /* minimum value */ + int64 maxv; /* maximum value */ + int64 startv; /* start value */ + int64 incby; /* increment value */ + int64 cache; /* cache size */ + int64 last_value; /* last value of sequence */ + bool is_called; /* whether nextval advances before returning */ + bool null_seqtuple; /* did pg_get_sequence_data return nulls? */ +} SequenceItem; + +typedef enum OidOptions +{ + zeroIsError = 1, + zeroAsStar = 2, + zeroAsNone = 4, +} OidOptions; + +/* global decls */ +static bool dosync = true; /* Issue fsync() to make dump durable on disk. */ + +static Oid g_last_builtin_oid; /* value of the last builtin oid */ + +/* The specified names/patterns should to match at least one entity */ +static int strict_names = 0; + +static pg_compress_algorithm compression_algorithm = PG_COMPRESSION_NONE; + +/* + * Object inclusion/exclusion lists + * + * The string lists record the patterns given by command-line switches, + * which we then convert to lists of OIDs of matching objects. + */ +static SimpleStringList schema_include_patterns = {NULL, NULL}; +static SimpleOidList schema_include_oids = {NULL, NULL}; +static SimpleStringList schema_exclude_patterns = {NULL, NULL}; +static SimpleOidList schema_exclude_oids = {NULL, NULL}; + +static SimpleStringList table_include_patterns = {NULL, NULL}; +static SimpleStringList table_include_patterns_and_children = {NULL, NULL}; +static SimpleOidList table_include_oids = {NULL, NULL}; +static SimpleStringList table_exclude_patterns = {NULL, NULL}; +static SimpleStringList table_exclude_patterns_and_children = {NULL, NULL}; +static SimpleOidList table_exclude_oids = {NULL, NULL}; +static SimpleStringList tabledata_exclude_patterns = {NULL, NULL}; +static SimpleStringList tabledata_exclude_patterns_and_children = {NULL, NULL}; +static SimpleOidList tabledata_exclude_oids = {NULL, NULL}; + +static SimpleStringList foreign_servers_include_patterns = {NULL, NULL}; +static SimpleOidList foreign_servers_include_oids = {NULL, NULL}; + +static SimpleStringList extension_include_patterns = {NULL, NULL}; +static SimpleOidList extension_include_oids = {NULL, NULL}; + +static SimpleStringList extension_exclude_patterns = {NULL, NULL}; +static SimpleOidList extension_exclude_oids = {NULL, NULL}; + +static const CatalogId nilCatalogId = {0, 0}; + +/* override for standard extra_float_digits setting */ +static bool have_extra_float_digits = false; +static int extra_float_digits; + +/* sorted table of role names */ +static RoleNameItem *rolenames = NULL; +static int nrolenames = 0; + +/* sorted table of comments */ +static CommentItem *comments = NULL; +static int ncomments = 0; + +/* sorted table of security labels */ +static SecLabelItem *seclabels = NULL; +static int nseclabels = 0; + +/* sorted table of pg_class information for binary upgrade */ +static BinaryUpgradeClassOidItem *binaryUpgradeClassOids = NULL; +static int nbinaryUpgradeClassOids = 0; + +/* sorted table of sequences */ +static SequenceItem *sequences = NULL; +static int nsequences = 0; + +/* Maximum number of relations to fetch in a fetchAttributeStats() call. */ +#define MAX_ATTR_STATS_RELS 64 + +/* + * The default number of rows per INSERT when + * --inserts is specified without --rows-per-insert + */ +#define DUMP_DEFAULT_ROWS_PER_INSERT 1 + +/* + * Maximum number of large objects to group into a single ArchiveEntry. + * At some point we might want to make this user-controllable, but for now + * a hard-wired setting will suffice. + */ +#define MAX_BLOBS_PER_ARCHIVE_ENTRY 1000 + +/* + * Macro for producing quoted, schema-qualified name of a dumpable object. + */ +#define fmtQualifiedDumpable(obj) \ + fmtQualifiedId((obj)->dobj.namespace->dobj.name, \ + (obj)->dobj.name) + +static void help(const char *progname); +static void setup_connection(Archive *AH, + const char *dumpencoding, const char *dumpsnapshot, + char *use_role); +static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode); +static void expand_schema_name_patterns(Archive *fout, + SimpleStringList *patterns, + SimpleOidList *oids, + bool strict_names); +static void expand_extension_name_patterns(Archive *fout, + SimpleStringList *patterns, + SimpleOidList *oids, + bool strict_names); +static void expand_foreign_server_name_patterns(Archive *fout, + SimpleStringList *patterns, + SimpleOidList *oids); +static void expand_table_name_patterns(Archive *fout, + SimpleStringList *patterns, + SimpleOidList *oids, + bool strict_names, + bool with_child_tables); +static void prohibit_crossdb_refs(PGconn *conn, const char *dbname, + const char *pattern); + +static NamespaceInfo *findNamespace(Oid nsoid); +static void dumpTableData(Archive *fout, const TableDataInfo *tdinfo); +static void refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo); +static const char *getRoleName(const char *roleoid_str); +static void collectRoleNames(Archive *fout); +static void getAdditionalACLs(Archive *fout); +static void dumpCommentExtended(Archive *fout, const char *type, + const char *name, const char *namespace, + const char *owner, CatalogId catalogId, + int subid, DumpId dumpId, + const char *initdb_comment); +static inline void dumpComment(Archive *fout, const char *type, + const char *name, const char *namespace, + const char *owner, CatalogId catalogId, + int subid, DumpId dumpId); +static int findComments(Oid classoid, Oid objoid, CommentItem **items); +static void collectComments(Archive *fout); +static void dumpSecLabel(Archive *fout, const char *type, const char *name, + const char *namespace, const char *owner, + CatalogId catalogId, int subid, DumpId dumpId); +static int findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items); +static void collectSecLabels(Archive *fout); +static void dumpDumpableObject(Archive *fout, DumpableObject *dobj); +static void dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo); +static void dumpExtension(Archive *fout, const ExtensionInfo *extinfo); +static void dumpType(Archive *fout, const TypeInfo *tyinfo); +static void dumpBaseType(Archive *fout, const TypeInfo *tyinfo); +static void dumpEnumType(Archive *fout, const TypeInfo *tyinfo); +static void dumpRangeType(Archive *fout, const TypeInfo *tyinfo); +static void dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo); +static void dumpDomain(Archive *fout, const TypeInfo *tyinfo); +static void dumpCompositeType(Archive *fout, const TypeInfo *tyinfo); +static void dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, + PGresult *res); +static void dumpShellType(Archive *fout, const ShellTypeInfo *stinfo); +static void dumpProcLang(Archive *fout, const ProcLangInfo *plang); +static void dumpFunc(Archive *fout, const FuncInfo *finfo); +static void dumpCast(Archive *fout, const CastInfo *cast); +static void dumpTransform(Archive *fout, const TransformInfo *transform); +static void dumpOpr(Archive *fout, const OprInfo *oprinfo); +static void dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo); +static void dumpOpclass(Archive *fout, const OpclassInfo *opcinfo); +static void dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo); +static void dumpCollation(Archive *fout, const CollInfo *collinfo); +static void dumpConversion(Archive *fout, const ConvInfo *convinfo); +static void dumpRule(Archive *fout, const RuleInfo *rinfo); +static void dumpAgg(Archive *fout, const AggInfo *agginfo); +static void dumpTrigger(Archive *fout, const TriggerInfo *tginfo); +static void dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo); +static void dumpTable(Archive *fout, const TableInfo *tbinfo); +static void dumpTableSchema(Archive *fout, const TableInfo *tbinfo); +static void dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo); +static void dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo); +static void collectSequences(Archive *fout); +static void dumpSequence(Archive *fout, const TableInfo *tbinfo); +static void dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo); +static void dumpIndex(Archive *fout, const IndxInfo *indxinfo); +static void dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo); +static void dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo); +static void dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo); +static void dumpConstraint(Archive *fout, const ConstraintInfo *coninfo); +static void dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo); +static void dumpTSParser(Archive *fout, const TSParserInfo *prsinfo); +static void dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo); +static void dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo); +static void dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo); +static void dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo); +static void dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo); +static void dumpUserMappings(Archive *fout, + const char *servername, const char *namespace, + const char *owner, CatalogId catalogId, DumpId dumpId); +static void dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo); + +static DumpId dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, + const char *type, const char *name, const char *subname, + const char *nspname, const char *tag, const char *owner, + const DumpableAcl *dacl); + +static void getDependencies(Archive *fout); +static void BuildArchiveDependencies(Archive *fout); +static void findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, + DumpId **dependencies, int *nDeps, int *allocDeps); + +static DumpableObject *createBoundaryObjects(void); +static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs, + DumpableObject *boundaryObjs); + +static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx); +static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo); +static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind); +static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo); +static void buildMatViewRefreshDependencies(Archive *fout); +static void getTableDataFKConstraints(void); +static void determineNotNullFlags(Archive *fout, PGresult *res, int r, + TableInfo *tbinfo, int j, + int i_notnull_name, + int i_notnull_comment, + int i_notnull_invalidoid, + int i_notnull_noinherit, + int i_notnull_islocal, + PQExpBuffer *invalidnotnulloids); +static char *format_function_arguments(const FuncInfo *finfo, const char *funcargs, + bool is_agg); +static char *format_function_signature(Archive *fout, + const FuncInfo *finfo, bool honor_quotes); +static char *convertRegProcReference(const char *proc); +static char *getFormattedOperatorName(const char *oproid); +static char *convertTSFunction(Archive *fout, Oid funcOid); +static const char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts); +static void getLOs(Archive *fout); +static void dumpLO(Archive *fout, const LoInfo *loinfo); +static int dumpLOs(Archive *fout, const void *arg); +static void dumpPolicy(Archive *fout, const PolicyInfo *polinfo); +static void dumpPublication(Archive *fout, const PublicationInfo *pubinfo); +static void dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo); +static void dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo); +static void dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo); +static void dumpDatabase(Archive *fout); +static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf, + const char *dbname, Oid dboid); +static void dumpEncoding(Archive *AH); +static void dumpStdStrings(Archive *AH); +static void dumpSearchPath(Archive *AH); +static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, + PQExpBuffer upgrade_buffer, + Oid pg_type_oid, + bool force_array_type, + bool include_multirange_type); +static void binary_upgrade_set_type_oids_by_rel(Archive *fout, + PQExpBuffer upgrade_buffer, + const TableInfo *tbinfo); +static void collectBinaryUpgradeClassOids(Archive *fout); +static void binary_upgrade_set_pg_class_oids(Archive *fout, + PQExpBuffer upgrade_buffer, + Oid pg_class_oid); +static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, + const DumpableObject *dobj, + const char *objtype, + const char *objname, + const char *objnamespace); +static const char *getAttrName(int attrnum, const TableInfo *tblInfo); +static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer); +static bool nonemptyReloptions(const char *reloptions); +static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, + const char *prefix, Archive *fout); +static char *get_synchronized_snapshot(Archive *fout); +static void set_restrict_relation_kind(Archive *AH, const char *value); +static void setupDumpWorker(Archive *AH); +static TableInfo *getRootTableInfo(const TableInfo *tbinfo); +static bool forcePartitionRootLoad(const TableInfo *tbinfo); +static void read_dump_filters(const char *filename, DumpOptions *dopt); + + +int +main(int argc, char **argv) +{ + int c; + const char *filename = NULL; + const char *format = "p"; + TableInfo *tblinfo; + int numTables; + DumpableObject **dobjs; + int numObjs; + DumpableObject *boundaryObjs; + int i; + int optindex; + RestoreOptions *ropt; + Archive *fout; /* the script file */ + bool g_verbose = false; + const char *dumpencoding = NULL; + const char *dumpsnapshot = NULL; + char *use_role = NULL; + int numWorkers = 1; + int plainText = 0; + ArchiveFormat archiveFormat = archUnknown; + ArchiveMode archiveMode; + pg_compress_specification compression_spec = {0}; + char *compression_detail = NULL; + char *compression_algorithm_str = "none"; + char *error_detail = NULL; + bool user_compression_defined = false; + DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC; + bool data_only = false; + bool schema_only = false; + bool statistics_only = false; + bool with_statistics = false; + bool no_data = false; + bool no_schema = false; + bool no_statistics = false; + + static DumpOptions dopt; + + static struct option long_options[] = { + {"data-only", no_argument, NULL, 'a'}, + {"blobs", no_argument, NULL, 'b'}, + {"large-objects", no_argument, NULL, 'b'}, + {"no-blobs", no_argument, NULL, 'B'}, + {"no-large-objects", no_argument, NULL, 'B'}, + {"clean", no_argument, NULL, 'c'}, + {"create", no_argument, NULL, 'C'}, + {"dbname", required_argument, NULL, 'd'}, + {"extension", required_argument, NULL, 'e'}, + {"file", required_argument, NULL, 'f'}, + {"format", required_argument, NULL, 'F'}, + {"host", required_argument, NULL, 'h'}, + {"jobs", 1, NULL, 'j'}, + {"no-reconnect", no_argument, NULL, 'R'}, + {"no-owner", no_argument, NULL, 'O'}, + {"port", required_argument, NULL, 'p'}, + {"schema", required_argument, NULL, 'n'}, + {"exclude-schema", required_argument, NULL, 'N'}, + {"schema-only", no_argument, NULL, 's'}, + {"superuser", required_argument, NULL, 'S'}, + {"table", required_argument, NULL, 't'}, + {"exclude-table", required_argument, NULL, 'T'}, + {"no-password", no_argument, NULL, 'w'}, + {"password", no_argument, NULL, 'W'}, + {"username", required_argument, NULL, 'U'}, + {"verbose", no_argument, NULL, 'v'}, + {"no-privileges", no_argument, NULL, 'x'}, + {"no-acl", no_argument, NULL, 'x'}, + {"compress", required_argument, NULL, 'Z'}, + {"encoding", required_argument, NULL, 'E'}, + {"help", no_argument, NULL, '?'}, + {"version", no_argument, NULL, 'V'}, + + /* + * the following options don't have an equivalent short option letter + */ + {"attribute-inserts", no_argument, &dopt.column_inserts, 1}, + {"binary-upgrade", no_argument, &dopt.binary_upgrade, 1}, + {"pgactive-init-node", no_argument, &dopt.pgactive_init_node, 1}, + {"column-inserts", no_argument, &dopt.column_inserts, 1}, + {"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1}, + {"disable-triggers", no_argument, &dopt.disable_triggers, 1}, + {"enable-row-security", no_argument, &dopt.enable_row_security, 1}, + {"exclude-table-data", required_argument, NULL, 4}, + {"extra-float-digits", required_argument, NULL, 8}, + {"if-exists", no_argument, &dopt.if_exists, 1}, + {"inserts", no_argument, NULL, 9}, + {"lock-wait-timeout", required_argument, NULL, 2}, + {"no-table-access-method", no_argument, &dopt.outputNoTableAm, 1}, + {"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1}, + {"quote-all-identifiers", no_argument, "e_all_identifiers, 1}, + {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1}, + {"role", required_argument, NULL, 3}, + {"section", required_argument, NULL, 5}, + {"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1}, + {"snapshot", required_argument, NULL, 6}, + {"statistics", no_argument, NULL, 22}, + {"statistics-only", no_argument, NULL, 18}, + {"strict-names", no_argument, &strict_names, 1}, + {"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1}, + {"no-comments", no_argument, &dopt.no_comments, 1}, + {"no-data", no_argument, NULL, 19}, + {"no-policies", no_argument, &dopt.no_policies, 1}, + {"no-publications", no_argument, &dopt.no_publications, 1}, + {"no-schema", no_argument, NULL, 20}, + {"no-security-labels", no_argument, &dopt.no_security_labels, 1}, + {"no-statistics", no_argument, NULL, 21}, + {"no-subscriptions", no_argument, &dopt.no_subscriptions, 1}, + {"no-toast-compression", no_argument, &dopt.no_toast_compression, 1}, + {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1}, + {"no-sync", no_argument, NULL, 7}, + {"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1}, + {"rows-per-insert", required_argument, NULL, 10}, + {"include-foreign-data", required_argument, NULL, 11}, + {"table-and-children", required_argument, NULL, 12}, + {"exclude-table-and-children", required_argument, NULL, 13}, + {"exclude-table-data-and-children", required_argument, NULL, 14}, + {"sync-method", required_argument, NULL, 15}, + {"filter", required_argument, NULL, 16}, + {"exclude-extension", required_argument, NULL, 17}, + {"sequence-data", no_argument, &dopt.sequence_data, 1}, + {"restrict-key", required_argument, NULL, 25}, + + {NULL, 0, NULL, 0} + }; + + pg_logging_init(argv[0]); + pg_logging_set_level(PG_LOG_WARNING); + set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump")); + + /* + * Initialize what we need for parallel execution, especially for thread + * support on Windows. + */ + init_parallel_dump_utils(); + + progname = get_progname(argv[0]); + + if (argc > 1) + { + if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) + { + help(progname); + exit_nicely(0); + } + if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) + { + puts("pg_dump (PostgreSQL) " PG_VERSION); + exit_nicely(0); + } + } + + InitDumpOptions(&dopt); + + while ((c = getopt_long(argc, argv, "abBcCd:e:E:f:F:h:j:n:N:Op:RsS:t:T:U:vwWxXZ:", + long_options, &optindex)) != -1) + { + switch (c) + { + case 'a': /* Dump data only */ + data_only = true; + break; + + case 'b': /* Dump LOs */ + dopt.outputLOs = true; + break; + + case 'B': /* Don't dump LOs */ + dopt.dontOutputLOs = true; + break; + + case 'c': /* clean (i.e., drop) schema prior to create */ + dopt.outputClean = 1; + break; + + case 'C': /* Create DB */ + dopt.outputCreateDB = 1; + break; + + case 'd': /* database name */ + dopt.cparams.dbname = pg_strdup(optarg); + break; + + case 'e': /* include extension(s) */ + simple_string_list_append(&extension_include_patterns, optarg); + dopt.include_everything = false; + break; + + case 'E': /* Dump encoding */ + dumpencoding = pg_strdup(optarg); + break; + + case 'f': + filename = pg_strdup(optarg); + break; + + case 'F': + format = pg_strdup(optarg); + break; + + case 'h': /* server host */ + dopt.cparams.pghost = pg_strdup(optarg); + break; + + case 'j': /* number of dump jobs */ + if (!option_parse_int(optarg, "-j/--jobs", 1, + PG_MAX_JOBS, + &numWorkers)) + exit_nicely(1); + break; + + case 'n': /* include schema(s) */ + simple_string_list_append(&schema_include_patterns, optarg); + dopt.include_everything = false; + break; + + case 'N': /* exclude schema(s) */ + simple_string_list_append(&schema_exclude_patterns, optarg); + break; + + case 'O': /* Don't reconnect to match owner */ + dopt.outputNoOwner = 1; + break; + + case 'p': /* server port */ + dopt.cparams.pgport = pg_strdup(optarg); + break; + + case 'R': + /* no-op, still accepted for backwards compatibility */ + break; + + case 's': /* dump schema only */ + schema_only = true; + break; + + case 'S': /* Username for superuser in plain text output */ + dopt.outputSuperuser = pg_strdup(optarg); + break; + + case 't': /* include table(s) */ + simple_string_list_append(&table_include_patterns, optarg); + dopt.include_everything = false; + break; + + case 'T': /* exclude table(s) */ + simple_string_list_append(&table_exclude_patterns, optarg); + break; + + case 'U': + dopt.cparams.username = pg_strdup(optarg); + break; + + case 'v': /* verbose */ + g_verbose = true; + pg_logging_increase_verbosity(); + break; + + case 'w': + dopt.cparams.promptPassword = TRI_NO; + break; + + case 'W': + dopt.cparams.promptPassword = TRI_YES; + break; + + case 'x': /* skip ACL dump */ + dopt.aclsSkip = true; + break; + + case 'Z': /* Compression */ + parse_compress_options(optarg, &compression_algorithm_str, + &compression_detail); + user_compression_defined = true; + break; + + case 0: + /* This covers the long options. */ + break; + + case 2: /* lock-wait-timeout */ + dopt.lockWaitTimeout = pg_strdup(optarg); + break; + + case 3: /* SET ROLE */ + use_role = pg_strdup(optarg); + break; + + case 4: /* exclude table(s) data */ + simple_string_list_append(&tabledata_exclude_patterns, optarg); + break; + + case 5: /* section */ + set_dump_section(optarg, &dopt.dumpSections); + break; + + case 6: /* snapshot */ + dumpsnapshot = pg_strdup(optarg); + break; + + case 7: /* no-sync */ + dosync = false; + break; + + case 8: + have_extra_float_digits = true; + if (!option_parse_int(optarg, "--extra-float-digits", -15, 3, + &extra_float_digits)) + exit_nicely(1); + break; + + case 9: /* inserts */ + + /* + * dump_inserts also stores --rows-per-insert, careful not to + * overwrite that. + */ + if (dopt.dump_inserts == 0) + dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT; + break; + + case 10: /* rows per insert */ + if (!option_parse_int(optarg, "--rows-per-insert", 1, INT_MAX, + &dopt.dump_inserts)) + exit_nicely(1); + break; + + case 11: /* include foreign data */ + simple_string_list_append(&foreign_servers_include_patterns, + optarg); + break; + + case 12: /* include table(s) and their children */ + simple_string_list_append(&table_include_patterns_and_children, + optarg); + dopt.include_everything = false; + break; + + case 13: /* exclude table(s) and their children */ + simple_string_list_append(&table_exclude_patterns_and_children, + optarg); + break; + + case 14: /* exclude data of table(s) and children */ + simple_string_list_append(&tabledata_exclude_patterns_and_children, + optarg); + break; + + case 15: + if (!parse_sync_method(optarg, &sync_method)) + exit_nicely(1); + break; + + case 16: /* read object filters from file */ + read_dump_filters(optarg, &dopt); + break; + + case 17: /* exclude extension(s) */ + simple_string_list_append(&extension_exclude_patterns, + optarg); + break; + + case 18: + statistics_only = true; + break; + + case 19: + no_data = true; + break; + + case 20: + no_schema = true; + break; + + case 21: + no_statistics = true; + break; + + case 22: + with_statistics = true; + break; + + case 25: + dopt.restrict_key = pg_strdup(optarg); + break; + + default: + /* getopt_long already emitted a complaint */ + pg_log_error_hint("Try \"%s --help\" for more information.", progname); + exit_nicely(1); + } + } + + /* + * Non-option argument specifies database name as long as it wasn't + * already specified with -d / --dbname + */ + if (optind < argc && dopt.cparams.dbname == NULL) + dopt.cparams.dbname = argv[optind++]; + + /* Complain if any arguments remain */ + if (optind < argc) + { + pg_log_error("too many command-line arguments (first is \"%s\")", + argv[optind]); + pg_log_error_hint("Try \"%s --help\" for more information.", progname); + exit_nicely(1); + } + + /* --column-inserts implies --inserts */ + if (dopt.column_inserts && dopt.dump_inserts == 0) + dopt.dump_inserts = DUMP_DEFAULT_ROWS_PER_INSERT; + + /* *-only options are incompatible with each other */ + check_mut_excl_opts(data_only, "-a/--data-only", + schema_only, "-s/--schema-only", + statistics_only, "--statistics-only"); + + /* --no-* and *-only for same thing are incompatible */ + check_mut_excl_opts(data_only, "-a/--data-only", + no_data, "--no-data"); + check_mut_excl_opts(schema_only, "-s/--schema-only", + no_schema, "--no-schema"); + check_mut_excl_opts(statistics_only, "--statistics-only", + no_statistics, "--no-statistics"); + + /* --statistics and --no-statistics are incompatible */ + check_mut_excl_opts(with_statistics, "--statistics", + no_statistics, "--no-statistics"); + + /* --statistics is incompatible with *-only (except --statistics-only) */ + check_mut_excl_opts(with_statistics, "--statistics", + data_only, "-a/--data-only", + schema_only, "-s/--schema-only"); + + /* --include-foreign-data is incompatible with --schema-only */ + check_mut_excl_opts(foreign_servers_include_patterns.head, "--include-foreign-data", + schema_only, "-s/--schema-only"); + + if (numWorkers > 1 && foreign_servers_include_patterns.head != NULL) + pg_fatal("option %s is not supported with parallel backup", + "--include-foreign-data"); + + /* --clean is incompatible with --data-only */ + check_mut_excl_opts(dopt.outputClean, "-c/--clean", + data_only, "-a/--data-only"); + + if (dopt.if_exists && !dopt.outputClean) + pg_fatal("option %s requires option %s", + "--if-exists", "-c/--clean"); + + /* + * Set derivative flags. Ambiguous or nonsensical combinations, e.g. + * "--schema-only --no-schema", will have already caused an error in one + * of the checks above. + */ + dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) || + data_only) && !no_data; + dopt.dumpSchema = ((dopt.dumpSchema && !data_only && !statistics_only) || + schema_only) && !no_schema; + dopt.dumpStatistics = ((dopt.dumpStatistics && !schema_only && !data_only) || + (statistics_only || with_statistics)) && !no_statistics; + + + /* + * --inserts are already implied above if --column-inserts or + * --rows-per-insert were specified. + */ + if (dopt.do_nothing && dopt.dump_inserts == 0) + pg_fatal("option %s requires option %s, %s, or %s", + "--on-conflict-do-nothing", + "--inserts", "--rows-per-insert", "--column-inserts"); + + /* Identify archive format to emit */ + archiveFormat = parseArchiveFormat(format, &archiveMode); + + /* archiveFormat specific setup */ + if (archiveFormat == archNull) + { + plainText = 1; + + /* + * If you don't provide a restrict key, one will be appointed for you. + */ + if (!dopt.restrict_key) + dopt.restrict_key = generate_restrict_key(); + if (!dopt.restrict_key) + pg_fatal("could not generate restrict key"); + if (!valid_restrict_key(dopt.restrict_key)) + pg_fatal("invalid restrict key"); + } + else if (dopt.restrict_key) + pg_fatal("option %s can only be used with %s", + "--restrict-key", "--format=plain"); + + /* + * Custom and directory formats are compressed by default with gzip when + * available, not the others. If gzip is not available, no compression is + * done by default. + */ + if ((archiveFormat == archCustom || archiveFormat == archDirectory) && + !user_compression_defined) + { +#ifdef HAVE_LIBZ + compression_algorithm_str = "gzip"; +#else + compression_algorithm_str = "none"; +#endif + } + + /* + * Compression options + */ + if (!parse_compress_algorithm(compression_algorithm_str, + &compression_algorithm)) + pg_fatal("unrecognized compression algorithm: \"%s\"", + compression_algorithm_str); + + parse_compress_specification(compression_algorithm, compression_detail, + &compression_spec); + error_detail = validate_compress_specification(&compression_spec); + if (error_detail != NULL) + pg_fatal("invalid compression specification: %s", + error_detail); + + error_detail = supports_compression(compression_spec); + if (error_detail != NULL) + pg_fatal("%s", error_detail); + + /* + * Disable support for zstd workers for now - these are based on + * threading, and it's unclear how it interacts with parallel dumps on + * platforms where that relies on threads too (e.g. Windows). + */ + if (compression_spec.options & PG_COMPRESSION_OPTION_WORKERS) + pg_log_warning("compression option \"%s\" is not currently supported by pg_dump", + "workers"); + + /* + * If emitting an archive format, we always want to emit a DATABASE item, + * in case --create is specified at pg_restore time. + */ + if (!plainText) + dopt.outputCreateDB = 1; + + /* Parallel backup only in the directory archive format so far */ + if (archiveFormat != archDirectory && numWorkers > 1) + pg_fatal("parallel backup only supported by the directory format"); + + /* Open the output file */ + fout = CreateArchive(filename, archiveFormat, compression_spec, + dosync, archiveMode, setupDumpWorker, sync_method); + + /* Make dump options accessible right away */ + SetArchiveOptions(fout, &dopt, NULL); + + /* Register the cleanup hook */ + on_exit_close_archive(fout); + + /* Let the archiver know how noisy to be */ + fout->verbose = g_verbose; + + + /* + * We allow the server to be back to 9.2, and up to any minor release of + * our own major version. (See also version check in pg_dumpall.c.) + */ + fout->minRemoteVersion = 90200; + fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99; + + fout->numWorkers = numWorkers; + + /* + * Open the database using the Archiver, so it knows about it. Errors mean + * death. + */ + ConnectDatabaseAhx(fout, &dopt.cparams, false); + setup_connection(fout, dumpencoding, dumpsnapshot, use_role); + + /* + * On hot standbys, never try to dump unlogged table data, since it will + * just throw an error. + */ + if (fout->isStandby) + dopt.no_unlogged_table_data = true; + + /* + * Find the last built-in OID, if needed (prior to 8.1) + * + * With 8.1 and above, we can just use FirstNormalObjectId - 1. + */ + g_last_builtin_oid = FirstNormalObjectId - 1; + + pg_log_info("last built-in OID is %u", g_last_builtin_oid); + + /* Expand schema selection patterns into OID lists */ + if (schema_include_patterns.head != NULL) + { + expand_schema_name_patterns(fout, &schema_include_patterns, + &schema_include_oids, + strict_names); + if (schema_include_oids.head == NULL) + pg_fatal("no matching schemas were found"); + } + expand_schema_name_patterns(fout, &schema_exclude_patterns, + &schema_exclude_oids, + false); + /* non-matching exclusion patterns aren't an error */ + + /* Expand table selection patterns into OID lists */ + expand_table_name_patterns(fout, &table_include_patterns, + &table_include_oids, + strict_names, false); + expand_table_name_patterns(fout, &table_include_patterns_and_children, + &table_include_oids, + strict_names, true); + if ((table_include_patterns.head != NULL || + table_include_patterns_and_children.head != NULL) && + table_include_oids.head == NULL) + pg_fatal("no matching tables were found"); + + expand_table_name_patterns(fout, &table_exclude_patterns, + &table_exclude_oids, + false, false); + expand_table_name_patterns(fout, &table_exclude_patterns_and_children, + &table_exclude_oids, + false, true); + + expand_table_name_patterns(fout, &tabledata_exclude_patterns, + &tabledata_exclude_oids, + false, false); + expand_table_name_patterns(fout, &tabledata_exclude_patterns_and_children, + &tabledata_exclude_oids, + false, true); + + expand_foreign_server_name_patterns(fout, &foreign_servers_include_patterns, + &foreign_servers_include_oids); + + /* non-matching exclusion patterns aren't an error */ + + /* Expand extension selection patterns into OID lists */ + if (extension_include_patterns.head != NULL) + { + expand_extension_name_patterns(fout, &extension_include_patterns, + &extension_include_oids, + strict_names); + if (extension_include_oids.head == NULL) + pg_fatal("no matching extensions were found"); + } + expand_extension_name_patterns(fout, &extension_exclude_patterns, + &extension_exclude_oids, + false); + /* non-matching exclusion patterns aren't an error */ + + /* + * Dumping LOs is the default for dumps where an inclusion switch is not + * used (an "include everything" dump). -B can be used to exclude LOs + * from those dumps. -b can be used to include LOs even when an inclusion + * switch is used. + * + * -s means "schema only" and LOs are data, not schema, so we never + * include LOs when -s is used. + */ + if (dopt.include_everything && dopt.dumpData && !dopt.dontOutputLOs) + dopt.outputLOs = true; + + /* + * Collect role names so we can map object owner OIDs to names. + */ + collectRoleNames(fout); + + /* + * Now scan the database and create DumpableObject structs for all the + * objects we intend to dump. + */ + tblinfo = getSchemaData(fout, &numTables); + + if (dopt.dumpData) + { + getTableData(&dopt, tblinfo, numTables, 0); + buildMatViewRefreshDependencies(fout); + if (!dopt.dumpSchema) + getTableDataFKConstraints(); + } + + if (!dopt.dumpData && dopt.sequence_data) + getTableData(&dopt, tblinfo, numTables, RELKIND_SEQUENCE); + + /* + * For binary upgrade mode, dump the pg_shdepend rows for large objects + * and maybe even pg_largeobject_metadata (see comment below for details). + * This is faster to restore than the equivalent set of large object + * commands. + */ + if (dopt.binary_upgrade) + { + TableInfo *shdepend; + + shdepend = findTableByOid(SharedDependRelationId); + makeTableDataInfo(&dopt, shdepend); + + /* + * Only dump large object shdepend rows for this database. + */ + shdepend->dataObj->filtercond = "WHERE classid = 'pg_largeobject'::regclass " + "AND dbid = (SELECT oid FROM pg_database " + " WHERE datname = current_database())"; + + /* + * For binary upgrades from v16 and newer versions, we can copy + * pg_largeobject_metadata's files from the old cluster, so we don't + * need to dump its contents. pg_upgrade can't copy/link the files + * from older versions because aclitem (needed by + * pg_largeobject_metadata.lomacl) changed its storage format in v16. + */ + if (fout->remoteVersion < 160000) + { + TableInfo *lo_metadata; + + lo_metadata = findTableByOid(LargeObjectMetadataRelationId); + makeTableDataInfo(&dopt, lo_metadata); + } + } + + /* + * In binary-upgrade mode, we do not have to worry about the actual LO + * data or the associated metadata that resides in the pg_largeobject and + * pg_largeobject_metadata tables, respectively. + * + * However, we do need to collect LO information as there may be comments + * or other information on LOs that we do need to dump out. + */ + if (dopt.outputLOs || dopt.binary_upgrade) + getLOs(fout); + + /* + * Collect dependency data to assist in ordering the objects. + */ + getDependencies(fout); + + /* + * Collect ACLs, comments, and security labels, if wanted. + */ + if (!dopt.aclsSkip) + getAdditionalACLs(fout); + if (!dopt.no_comments) + collectComments(fout); + if (!dopt.no_security_labels) + collectSecLabels(fout); + + /* For binary upgrade mode, collect required pg_class information. */ + if (dopt.binary_upgrade) + collectBinaryUpgradeClassOids(fout); + + /* Collect sequence information. */ + collectSequences(fout); + + /* Lastly, create dummy objects to represent the section boundaries */ + boundaryObjs = createBoundaryObjects(); + + /* Get pointers to all the known DumpableObjects */ + getDumpableObjects(&dobjs, &numObjs); + + /* + * Add dummy dependencies to enforce the dump section ordering. + */ + addBoundaryDependencies(dobjs, numObjs, boundaryObjs); + + /* + * Sort the objects into a safe dump order (no forward references). + * + * We rely on dependency information to help us determine a safe order, so + * the initial sort is mostly for cosmetic purposes: we sort by name to + * ensure that logically identical schemas will dump identically. + */ + sortDumpableObjectsByTypeName(dobjs, numObjs); + + sortDumpableObjects(dobjs, numObjs, + boundaryObjs[0].dumpId, boundaryObjs[1].dumpId); + + /* + * Create archive TOC entries for all the objects to be dumped, in a safe + * order. + */ + + /* + * First the special entries for ENCODING, STDSTRINGS, and SEARCHPATH. + */ + dumpEncoding(fout); + dumpStdStrings(fout); + dumpSearchPath(fout); + + /* The database items are always next, unless we don't want them at all */ + if (dopt.outputCreateDB) + dumpDatabase(fout); + + /* Now the rearrangeable objects. */ + for (i = 0; i < numObjs; i++) + dumpDumpableObject(fout, dobjs[i]); + + /* + * Set up options info to ensure we dump what we want. + */ + ropt = NewRestoreOptions(); + ropt->filename = filename; + + /* if you change this list, see dumpOptionsFromRestoreOptions */ + ropt->cparams.dbname = dopt.cparams.dbname ? pg_strdup(dopt.cparams.dbname) : NULL; + ropt->cparams.pgport = dopt.cparams.pgport ? pg_strdup(dopt.cparams.pgport) : NULL; + ropt->cparams.pghost = dopt.cparams.pghost ? pg_strdup(dopt.cparams.pghost) : NULL; + ropt->cparams.username = dopt.cparams.username ? pg_strdup(dopt.cparams.username) : NULL; + ropt->cparams.promptPassword = dopt.cparams.promptPassword; + ropt->dropSchema = dopt.outputClean; + ropt->dumpData = dopt.dumpData; + ropt->dumpSchema = dopt.dumpSchema; + ropt->dumpStatistics = dopt.dumpStatistics; + ropt->if_exists = dopt.if_exists; + ropt->column_inserts = dopt.column_inserts; + ropt->dumpSections = dopt.dumpSections; + ropt->aclsSkip = dopt.aclsSkip; + ropt->superuser = dopt.outputSuperuser; + ropt->createDB = dopt.outputCreateDB; + ropt->noOwner = dopt.outputNoOwner; + ropt->noTableAm = dopt.outputNoTableAm; + ropt->noTablespace = dopt.outputNoTablespaces; + ropt->disable_triggers = dopt.disable_triggers; + ropt->use_setsessauth = dopt.use_setsessauth; + ropt->disable_dollar_quoting = dopt.disable_dollar_quoting; + ropt->dump_inserts = dopt.dump_inserts; + ropt->no_comments = dopt.no_comments; + ropt->no_policies = dopt.no_policies; + ropt->no_publications = dopt.no_publications; + ropt->no_security_labels = dopt.no_security_labels; + ropt->no_subscriptions = dopt.no_subscriptions; + ropt->lockWaitTimeout = dopt.lockWaitTimeout; + ropt->include_everything = dopt.include_everything; + ropt->enable_row_security = dopt.enable_row_security; + ropt->sequence_data = dopt.sequence_data; + ropt->binary_upgrade = dopt.binary_upgrade; + ropt->restrict_key = dopt.restrict_key ? pg_strdup(dopt.restrict_key) : NULL; + + ropt->compression_spec = compression_spec; + + ropt->suppressDumpWarnings = true; /* We've already shown them */ + + SetArchiveOptions(fout, &dopt, ropt); + + /* Mark which entries should be output */ + ProcessArchiveRestoreOptions(fout); + + /* + * The archive's TOC entries are now marked as to which ones will actually + * be output, so we can set up their dependency lists properly. This isn't + * necessary for plain-text output, though. + */ + if (!plainText) + BuildArchiveDependencies(fout); + + /* + * And finally we can do the actual output. + * + * Note: for non-plain-text output formats, the output file is written + * inside CloseArchive(). This is, um, bizarre; but not worth changing + * right now. + */ + if (plainText) + RestoreArchive(fout, false); + + CloseArchive(fout); + + exit_nicely(0); +} + + +static void +help(const char *progname) +{ + printf(_("%s exports a PostgreSQL database as an SQL script or to other formats.\n\n"), progname); + printf(_("Usage:\n")); + printf(_(" %s [OPTION]... [DBNAME]\n"), progname); + + printf(_("\nGeneral options:\n")); + printf(_(" -f, --file=FILENAME output file or directory name\n")); + printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n" + " plain text (default))\n")); + printf(_(" -j, --jobs=NUM use this many parallel jobs to dump\n")); + printf(_(" -v, --verbose verbose mode\n")); + printf(_(" -V, --version output version information, then exit\n")); + printf(_(" -Z, --compress=METHOD[:DETAIL]\n" + " compress as specified\n")); + printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n")); + printf(_(" --no-sync do not wait for changes to be written safely to disk\n")); + printf(_(" --sync-method=METHOD set method for syncing files to disk\n")); + printf(_(" -?, --help show this help, then exit\n")); + + printf(_("\nOptions controlling the output content:\n")); + printf(_(" -a, --data-only dump only the data, not the schema or statistics\n")); + printf(_(" -b, --large-objects include large objects in dump\n")); + printf(_(" --blobs (same as --large-objects, deprecated)\n")); + printf(_(" -B, --no-large-objects exclude large objects in dump\n")); + printf(_(" --no-blobs (same as --no-large-objects, deprecated)\n")); + printf(_(" -c, --clean clean (drop) database objects before recreating\n")); + printf(_(" -C, --create include commands to create database in dump\n")); + printf(_(" -e, --extension=PATTERN dump the specified extension(s) only\n")); + printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n")); + printf(_(" -n, --schema=PATTERN dump the specified schema(s) only\n")); + printf(_(" -N, --exclude-schema=PATTERN do NOT dump the specified schema(s)\n")); + printf(_(" -O, --no-owner skip restoration of object ownership in\n" + " plain-text format\n")); + printf(_(" -s, --schema-only dump only the schema, no data or statistics\n")); + printf(_(" -S, --superuser=NAME superuser user name to use in plain-text format\n")); + printf(_(" -t, --table=PATTERN dump only the specified table(s)\n")); + printf(_(" -T, --exclude-table=PATTERN do NOT dump the specified table(s)\n")); + printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n")); + printf(_(" --binary-upgrade for use by upgrade utilities only\n")); + printf(_(" --column-inserts dump data as INSERT commands with column names\n")); + printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n")); + printf(_(" --disable-triggers disable triggers during data-only restore\n")); + printf(_(" --enable-row-security enable row security (dump only content user has\n" + " access to)\n")); + printf(_(" --exclude-extension=PATTERN do NOT dump the specified extension(s)\n")); + printf(_(" --exclude-table-and-children=PATTERN\n" + " do NOT dump the specified table(s), including\n" + " child and partition tables\n")); + printf(_(" --exclude-table-data=PATTERN do NOT dump data for the specified table(s)\n")); + printf(_(" --exclude-table-data-and-children=PATTERN\n" + " do NOT dump data for the specified table(s),\n" + " including child and partition tables\n")); + printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n")); + printf(_(" --filter=FILENAME include or exclude objects and data from dump\n" + " based on expressions in FILENAME\n")); + printf(_(" --if-exists use IF EXISTS when dropping objects\n")); + printf(_(" --include-foreign-data=PATTERN\n" + " include data of foreign tables on foreign\n" + " servers matching PATTERN\n")); + printf(_(" --inserts dump data as INSERT commands, rather than COPY\n")); + printf(_(" --load-via-partition-root load partitions via the root table\n")); + printf(_(" --no-comments do not dump comment commands\n")); + printf(_(" --no-data do not dump data\n")); + printf(_(" --no-policies do not dump row security policies\n")); + printf(_(" --no-publications do not dump publications\n")); + printf(_(" --no-schema do not dump schema\n")); + printf(_(" --no-security-labels do not dump security label assignments\n")); + printf(_(" --no-statistics do not dump statistics\n")); + printf(_(" --no-subscriptions do not dump subscriptions\n")); + printf(_(" --no-table-access-method do not dump table access methods\n")); + printf(_(" --no-tablespaces do not dump tablespace assignments\n")); + printf(_(" --no-toast-compression do not dump TOAST compression methods\n")); + printf(_(" --no-unlogged-table-data do not dump unlogged table data\n")); + printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n")); + printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n")); + printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n")); + printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n")); + printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n")); + printf(_(" --sequence-data include sequence data in dump\n")); + printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n")); + printf(_(" --snapshot=SNAPSHOT use given snapshot for the dump\n")); + printf(_(" --statistics dump the statistics\n")); + printf(_(" --statistics-only dump only the statistics, not schema or data\n")); + printf(_(" --strict-names require table and/or schema include patterns to\n" + " match at least one entity each\n")); + printf(_(" --table-and-children=PATTERN dump only the specified table(s), including\n" + " child and partition tables\n")); + printf(_(" --use-set-session-authorization\n" + " use SET SESSION AUTHORIZATION commands instead of\n" + " ALTER OWNER commands to set ownership\n")); + + printf(_("\nConnection options:\n")); + printf(_(" -d, --dbname=DBNAME database to dump\n")); + printf(_(" -h, --host=HOSTNAME database server host or socket directory\n")); + printf(_(" -p, --port=PORT database server port number\n")); + printf(_(" -U, --username=NAME connect as specified database user\n")); + printf(_(" -w, --no-password never prompt for password\n")); + printf(_(" -W, --password force password prompt (should happen automatically)\n")); + printf(_(" --role=ROLENAME do SET ROLE before dump\n")); + + printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n" + "variable value is used.\n\n")); + printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT); + printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL); +} + +static void +setup_connection(Archive *AH, const char *dumpencoding, + const char *dumpsnapshot, char *use_role) +{ + DumpOptions *dopt = AH->dopt; + PGconn *conn = GetConnection(AH); + + PQclear(ExecuteSqlQueryForSingleRow(AH, ALWAYS_SECURE_SEARCH_PATH_SQL)); + + /* + * Set the client encoding if requested. + */ + if (dumpencoding) + { + if (PQsetClientEncoding(conn, dumpencoding) < 0) + pg_fatal("invalid client encoding \"%s\" specified", + dumpencoding); + } + + /* + * Force standard_conforming_strings on, just in case we are dumping from + * an old server that has it disabled. Without this, literals in views, + * expressions, etc, would be incorrect for modern servers. + */ + ExecuteSqlStatement(AH, "SET standard_conforming_strings = on"); + + /* + * And reflect that to AH->std_strings. You might think that we should + * just delete that variable and the code that checks it, but that would + * be problematic for pg_restore, which at least for now should still cope + * with archives containing the other setting (cf. processStdStringsEntry + * in pg_backup_archiver.c). + */ + AH->std_strings = true; + + /* + * Get the active encoding, so we know how to escape strings. + */ + AH->encoding = PQclientEncoding(conn); + setFmtEncoding(AH->encoding); + + /* + * Set the role if requested. In a parallel dump worker, we'll be passed + * use_role == NULL, but AH->use_role is already set (if user specified it + * originally) and we should use that. + */ + if (!use_role && AH->use_role) + use_role = AH->use_role; + + /* Set the role if requested */ + if (use_role) + { + PQExpBuffer query = createPQExpBuffer(); + + appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role)); + ExecuteSqlStatement(AH, query->data); + destroyPQExpBuffer(query); + + /* save it for possible later use by parallel workers */ + if (!AH->use_role) + AH->use_role = pg_strdup(use_role); + } + + /* Set the datestyle to ISO to ensure the dump's portability */ + ExecuteSqlStatement(AH, "SET DATESTYLE = ISO"); + + /* Likewise, avoid using sql_standard intervalstyle */ + ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES"); + + /* + * Use an explicitly specified extra_float_digits if it has been provided. + * Otherwise, set extra_float_digits so that we can dump float data + * exactly (given correctly implemented float I/O code, anyway). + */ + if (have_extra_float_digits) + { + PQExpBuffer q = createPQExpBuffer(); + + appendPQExpBuffer(q, "SET extra_float_digits TO %d", + extra_float_digits); + ExecuteSqlStatement(AH, q->data); + destroyPQExpBuffer(q); + } + else + ExecuteSqlStatement(AH, "SET extra_float_digits TO 3"); + + /* + * Disable synchronized scanning, to prevent unpredictable changes in row + * ordering across a dump and reload. + */ + ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off"); + + /* + * Disable timeouts if supported. + */ + ExecuteSqlStatement(AH, "SET statement_timeout = 0"); + if (AH->remoteVersion >= 90300) + ExecuteSqlStatement(AH, "SET lock_timeout = 0"); + if (AH->remoteVersion >= 90600) + ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0"); + if (AH->remoteVersion >= 170000) + ExecuteSqlStatement(AH, "SET transaction_timeout = 0"); + + /* + * Quote all identifiers, if requested. + */ + if (quote_all_identifiers) + ExecuteSqlStatement(AH, "SET quote_all_identifiers = true"); + + /* + * Adjust row-security mode, if supported. + */ + if (AH->remoteVersion >= 90500) + { + if (dopt->enable_row_security) + ExecuteSqlStatement(AH, "SET row_security = on"); + else + ExecuteSqlStatement(AH, "SET row_security = off"); + } + + /* + * For security reasons, we restrict the expansion of non-system views and + * access to foreign tables during the pg_dump process. This restriction + * is adjusted when dumping foreign table data. + */ + set_restrict_relation_kind(AH, "view, foreign-table"); + + /* + * Initialize prepared-query state to "nothing prepared". We do this here + * so that a parallel dump worker will have its own state. + */ + AH->is_prepared = pg_malloc0_array(bool, NUM_PREP_QUERIES); + + /* + * Start transaction-snapshot mode transaction to dump consistent data. + */ + ExecuteSqlStatement(AH, "BEGIN"); + + /* + * To support the combination of serializable_deferrable with the jobs + * option we use REPEATABLE READ for the worker connections that are + * passed a snapshot. As long as the snapshot is acquired in a + * SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a + * REPEATABLE READ transaction provides the appropriate integrity + * guarantees. This is a kluge, but safe for back-patching. + */ + if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL) + ExecuteSqlStatement(AH, + "SET TRANSACTION ISOLATION LEVEL " + "SERIALIZABLE, READ ONLY, DEFERRABLE"); + else + ExecuteSqlStatement(AH, + "SET TRANSACTION ISOLATION LEVEL " + "REPEATABLE READ, READ ONLY"); + + /* + * If user specified a snapshot to use, select that. In a parallel dump + * worker, we'll be passed dumpsnapshot == NULL, but AH->sync_snapshot_id + * is already set (if the server can handle it) and we should use that. + */ + if (dumpsnapshot) + AH->sync_snapshot_id = pg_strdup(dumpsnapshot); + + if (AH->sync_snapshot_id) + { + PQExpBuffer query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "SET TRANSACTION SNAPSHOT "); + appendStringLiteralConn(query, AH->sync_snapshot_id, conn); + ExecuteSqlStatement(AH, query->data); + destroyPQExpBuffer(query); + } + else if (AH->numWorkers > 1) + { + if (AH->isStandby && AH->remoteVersion < 100000) + pg_fatal("parallel dumps from standby servers are not supported by this server version"); + AH->sync_snapshot_id = get_synchronized_snapshot(AH); + } +} + +/* Set up connection for a parallel worker process */ +static void +setupDumpWorker(Archive *AH) +{ + /* + * We want to re-select all the same values the leader connection is + * using. We'll have inherited directly-usable values in + * AH->sync_snapshot_id and AH->use_role, but we need to translate the + * inherited encoding value back to a string to pass to setup_connection. + */ + setup_connection(AH, + pg_encoding_to_char(AH->encoding), + NULL, + NULL); +} + +static char * +get_synchronized_snapshot(Archive *fout) +{ + char *query = "SELECT pg_catalog.pg_export_snapshot()"; + char *result; + PGresult *res; + + res = ExecuteSqlQueryForSingleRow(fout, query); + result = pg_strdup(PQgetvalue(res, 0, 0)); + PQclear(res); + + return result; +} + +static ArchiveFormat +parseArchiveFormat(const char *format, ArchiveMode *mode) +{ + ArchiveFormat archiveFormat; + + *mode = archModeWrite; + + if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0) + { + /* This is used by pg_dumpall, and is not documented */ + archiveFormat = archNull; + *mode = archModeAppend; + } + else if (pg_strcasecmp(format, "c") == 0) + archiveFormat = archCustom; + else if (pg_strcasecmp(format, "custom") == 0) + archiveFormat = archCustom; + else if (pg_strcasecmp(format, "d") == 0) + archiveFormat = archDirectory; + else if (pg_strcasecmp(format, "directory") == 0) + archiveFormat = archDirectory; + else if (pg_strcasecmp(format, "p") == 0) + archiveFormat = archNull; + else if (pg_strcasecmp(format, "plain") == 0) + archiveFormat = archNull; + else if (pg_strcasecmp(format, "t") == 0) + archiveFormat = archTar; + else if (pg_strcasecmp(format, "tar") == 0) + archiveFormat = archTar; + else + pg_fatal("invalid output format \"%s\" specified", format); + return archiveFormat; +} + +/* + * Find the OIDs of all schemas matching the given list of patterns, + * and append them to the given OID list. + */ +static void +expand_schema_name_patterns(Archive *fout, + SimpleStringList *patterns, + SimpleOidList *oids, + bool strict_names) +{ + PQExpBuffer query; + PGresult *res; + SimpleStringListCell *cell; + int i; + + if (patterns->head == NULL) + return; /* nothing to do */ + + query = createPQExpBuffer(); + + /* + * The loop below runs multiple SELECTs might sometimes result in + * duplicate entries in the OID list, but we don't care. + */ + + for (cell = patterns->head; cell; cell = cell->next) + { + PQExpBufferData dbbuf; + int dotcnt; + + appendPQExpBufferStr(query, + "SELECT oid FROM pg_catalog.pg_namespace n\n"); + initPQExpBuffer(&dbbuf); + processSQLNamePattern(GetConnection(fout), query, cell->val, false, + false, NULL, "n.nspname", NULL, NULL, &dbbuf, + &dotcnt); + if (dotcnt > 1) + pg_fatal("improper qualified name (too many dotted names): %s", + cell->val); + else if (dotcnt == 1) + prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val); + termPQExpBuffer(&dbbuf); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + if (strict_names && PQntuples(res) == 0) + pg_fatal("no matching schemas were found for pattern \"%s\"", cell->val); + + for (i = 0; i < PQntuples(res); i++) + { + simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); + } + + PQclear(res); + resetPQExpBuffer(query); + } + + destroyPQExpBuffer(query); +} + +/* + * Find the OIDs of all extensions matching the given list of patterns, + * and append them to the given OID list. + */ +static void +expand_extension_name_patterns(Archive *fout, + SimpleStringList *patterns, + SimpleOidList *oids, + bool strict_names) +{ + PQExpBuffer query; + PGresult *res; + SimpleStringListCell *cell; + int i; + + if (patterns->head == NULL) + return; /* nothing to do */ + + query = createPQExpBuffer(); + + /* + * The loop below runs multiple SELECTs might sometimes result in + * duplicate entries in the OID list, but we don't care. + */ + for (cell = patterns->head; cell; cell = cell->next) + { + int dotcnt; + + appendPQExpBufferStr(query, + "SELECT oid FROM pg_catalog.pg_extension e\n"); + processSQLNamePattern(GetConnection(fout), query, cell->val, false, + false, NULL, "e.extname", NULL, NULL, NULL, + &dotcnt); + if (dotcnt > 0) + pg_fatal("improper qualified name (too many dotted names): %s", + cell->val); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + if (strict_names && PQntuples(res) == 0) + pg_fatal("no matching extensions were found for pattern \"%s\"", cell->val); + + for (i = 0; i < PQntuples(res); i++) + { + simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); + } + + PQclear(res); + resetPQExpBuffer(query); + } + + destroyPQExpBuffer(query); +} + +/* + * Find the OIDs of all foreign servers matching the given list of patterns, + * and append them to the given OID list. + */ +static void +expand_foreign_server_name_patterns(Archive *fout, + SimpleStringList *patterns, + SimpleOidList *oids) +{ + PQExpBuffer query; + PGresult *res; + SimpleStringListCell *cell; + int i; + + if (patterns->head == NULL) + return; /* nothing to do */ + + query = createPQExpBuffer(); + + /* + * The loop below runs multiple SELECTs might sometimes result in + * duplicate entries in the OID list, but we don't care. + */ + + for (cell = patterns->head; cell; cell = cell->next) + { + int dotcnt; + + appendPQExpBufferStr(query, + "SELECT oid FROM pg_catalog.pg_foreign_server s\n"); + processSQLNamePattern(GetConnection(fout), query, cell->val, false, + false, NULL, "s.srvname", NULL, NULL, NULL, + &dotcnt); + if (dotcnt > 0) + pg_fatal("improper qualified name (too many dotted names): %s", + cell->val); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + if (PQntuples(res) == 0) + pg_fatal("no matching foreign servers were found for pattern \"%s\"", cell->val); + + for (i = 0; i < PQntuples(res); i++) + simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); + + PQclear(res); + resetPQExpBuffer(query); + } + + destroyPQExpBuffer(query); +} + +/* + * Find the OIDs of all tables matching the given list of patterns, + * and append them to the given OID list. See also expand_dbname_patterns() + * in pg_dumpall.c + */ +static void +expand_table_name_patterns(Archive *fout, + SimpleStringList *patterns, SimpleOidList *oids, + bool strict_names, bool with_child_tables) +{ + PQExpBuffer query; + PGresult *res; + SimpleStringListCell *cell; + int i; + + if (patterns->head == NULL) + return; /* nothing to do */ + + query = createPQExpBuffer(); + + /* + * this might sometimes result in duplicate entries in the OID list, but + * we don't care. + */ + + for (cell = patterns->head; cell; cell = cell->next) + { + PQExpBufferData dbbuf; + int dotcnt; + + /* + * Query must remain ABSOLUTELY devoid of unqualified names. This + * would be unnecessary given a pg_table_is_visible() variant taking a + * search_path argument. + * + * For with_child_tables, we start with the basic query's results and + * recursively search the inheritance tree to add child tables. + */ + if (with_child_tables) + { + appendPQExpBufferStr(query, "WITH RECURSIVE partition_tree (relid) AS (\n"); + } + + appendPQExpBuffer(query, + "SELECT c.oid" + "\nFROM pg_catalog.pg_class c" + "\n LEFT JOIN pg_catalog.pg_namespace n" + "\n ON n.oid OPERATOR(pg_catalog.=) c.relnamespace" + "\nWHERE c.relkind OPERATOR(pg_catalog.=) ANY" + "\n (array['%c', '%c', '%c', '%c', '%c', '%c', '%c'])\n", + RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW, + RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE, + RELKIND_PARTITIONED_TABLE, RELKIND_PROPGRAPH); + initPQExpBuffer(&dbbuf); + processSQLNamePattern(GetConnection(fout), query, cell->val, true, + false, "n.nspname", "c.relname", NULL, + "pg_catalog.pg_table_is_visible(c.oid)", &dbbuf, + &dotcnt); + if (dotcnt > 2) + pg_fatal("improper relation name (too many dotted names): %s", + cell->val); + else if (dotcnt == 2) + prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val); + termPQExpBuffer(&dbbuf); + + if (with_child_tables) + { + appendPQExpBufferStr(query, "UNION" + "\nSELECT i.inhrelid" + "\nFROM partition_tree p" + "\n JOIN pg_catalog.pg_inherits i" + "\n ON p.relid OPERATOR(pg_catalog.=) i.inhparent" + "\n)" + "\nSELECT relid FROM partition_tree"); + } + + ExecuteSqlStatement(fout, "RESET search_path"); + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + PQclear(ExecuteSqlQueryForSingleRow(fout, + ALWAYS_SECURE_SEARCH_PATH_SQL)); + if (strict_names && PQntuples(res) == 0) + pg_fatal("no matching tables were found for pattern \"%s\"", cell->val); + + for (i = 0; i < PQntuples(res); i++) + { + simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); + } + + PQclear(res); + resetPQExpBuffer(query); + } + + destroyPQExpBuffer(query); +} + +/* + * Verifies that the connected database name matches the given database name, + * and if not, dies with an error about the given pattern. + * + * The 'dbname' argument should be a literal name parsed from 'pattern'. + */ +static void +prohibit_crossdb_refs(PGconn *conn, const char *dbname, const char *pattern) +{ + const char *db; + + db = PQdb(conn); + if (db == NULL) + pg_fatal("You are currently not connected to a database."); + + if (strcmp(db, dbname) != 0) + pg_fatal("cross-database references are not implemented: %s", + pattern); +} + +/* + * checkExtensionMembership + * Determine whether object is an extension member, and if so, + * record an appropriate dependency and set the object's dump flag. + * + * It's important to call this for each object that could be an extension + * member. Generally, we integrate this with determining the object's + * to-be-dumped-ness, since extension membership overrides other rules for that. + * + * Returns true if object is an extension member, else false. + */ +static bool +checkExtensionMembership(DumpableObject *dobj, Archive *fout) +{ + ExtensionInfo *ext = findOwningExtension(dobj->catId); + + if (ext == NULL) + return false; + + dobj->ext_member = true; + + /* Record dependency so that getDependencies needn't deal with that */ + addObjectDependency(dobj, ext->dobj.dumpId); + + /* + * In 9.6 and above, mark the member object to have any non-initial ACLs + * dumped. (Any initial ACLs will be removed later, using data from + * pg_init_privs, so that we'll dump only the delta from the extension's + * initial setup.) + * + * Prior to 9.6, we do not include any extension member components. + * + * In binary upgrades, we still dump all components of the members + * individually, since the idea is to exactly reproduce the database + * contents rather than replace the extension contents with something + * different. + * + * Note: it might be interesting someday to implement storage and delta + * dumping of extension members' RLS policies and/or security labels. + * However there is a pitfall for RLS policies: trying to dump them + * requires getting a lock on their tables, and the calling user might not + * have privileges for that. We need no lock to examine a table's ACLs, + * so the current feature doesn't have a problem of that sort. + */ + if (fout->dopt->binary_upgrade) + dobj->dump = ext->dobj.dump; + else + { + if (fout->remoteVersion < 90600) + dobj->dump = DUMP_COMPONENT_NONE; + else + dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL); + } + + return true; +} + +/* + * selectDumpableNamespace: policy-setting subroutine + * Mark a namespace as to be dumped or not + */ +static void +selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout) +{ + /* + * DUMP_COMPONENT_DEFINITION typically implies a CREATE SCHEMA statement + * and (for --clean) a DROP SCHEMA statement. (In the absence of + * DUMP_COMPONENT_DEFINITION, this value is irrelevant.) + */ + nsinfo->create = true; + + /* + * If specific tables are being dumped, do not dump any complete + * namespaces. If specific namespaces are being dumped, dump just those + * namespaces. Otherwise, dump all non-system namespaces. + */ + if (table_include_oids.head != NULL) + nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE; + else if (schema_include_oids.head != NULL) + nsinfo->dobj.dump_contains = nsinfo->dobj.dump = + simple_oid_list_member(&schema_include_oids, + nsinfo->dobj.catId.oid) ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; + else if (fout->remoteVersion >= 90600 && + strcmp(nsinfo->dobj.name, "pg_catalog") == 0) + { + /* + * In 9.6 and above, we dump out any ACLs defined in pg_catalog, if + * they are interesting (and not the original ACLs which were set at + * initdb time, see pg_init_privs). + */ + nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL; + } + else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 || + strcmp(nsinfo->dobj.name, "information_schema") == 0) + { + /* Other system schemas don't get dumped */ + nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE; + } + else if (strcmp(nsinfo->dobj.name, "public") == 0) + { + /* + * The public schema is a strange beast that sits in a sort of + * no-mans-land between being a system object and a user object. + * CREATE SCHEMA would fail, so its DUMP_COMPONENT_DEFINITION is just + * a comment and an indication of ownership. If the owner is the + * default, omit that superfluous DUMP_COMPONENT_DEFINITION. Before + * v15, the default owner was BOOTSTRAP_SUPERUSERID. + */ + nsinfo->create = false; + nsinfo->dobj.dump = DUMP_COMPONENT_ALL; + if (nsinfo->nspowner == ROLE_PG_DATABASE_OWNER) + nsinfo->dobj.dump &= ~DUMP_COMPONENT_DEFINITION; + nsinfo->dobj.dump_contains = DUMP_COMPONENT_ALL; + + /* + * Also, make like it has a comment even if it doesn't; this is so + * that we'll emit a command to drop the comment, if appropriate. + * (Without this, we'd not call dumpCommentExtended for it.) + */ + nsinfo->dobj.components |= DUMP_COMPONENT_COMMENT; + } + else + nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ALL; + + /* + * In any case, a namespace can be excluded by an exclusion switch + */ + if (nsinfo->dobj.dump_contains && + simple_oid_list_member(&schema_exclude_oids, + nsinfo->dobj.catId.oid)) + nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE; + + /* + * If the schema belongs to an extension, allow extension membership to + * override the dump decision for the schema itself. However, this does + * not change dump_contains, so this won't change what we do with objects + * within the schema. (If they belong to the extension, they'll get + * suppressed by it, otherwise not.) + */ + (void) checkExtensionMembership(&nsinfo->dobj, fout); +} + +/* + * selectDumpableTable: policy-setting subroutine + * Mark a table as to be dumped or not + */ +static void +selectDumpableTable(TableInfo *tbinfo, Archive *fout) +{ + if (checkExtensionMembership(&tbinfo->dobj, fout)) + return; /* extension membership overrides all else */ + + /* + * If specific tables are being dumped, dump just those tables; else, dump + * according to the parent namespace's dump flag. + */ + if (table_include_oids.head != NULL) + tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids, + tbinfo->dobj.catId.oid) ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; + else + tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump_contains; + + /* + * In any case, a table can be excluded by an exclusion switch + */ + if (tbinfo->dobj.dump && + simple_oid_list_member(&table_exclude_oids, + tbinfo->dobj.catId.oid)) + tbinfo->dobj.dump = DUMP_COMPONENT_NONE; +} + +/* + * selectDumpableType: policy-setting subroutine + * Mark a type as to be dumped or not + * + * If it's a table's rowtype or an autogenerated array type, we also apply a + * special type code to facilitate sorting into the desired order. (We don't + * want to consider those to be ordinary types because that would bring tables + * up into the datatype part of the dump order.) We still set the object's + * dump flag; that's not going to cause the dummy type to be dumped, but we + * need it so that casts involving such types will be dumped correctly -- see + * dumpCast. This means the flag should be set the same as for the underlying + * object (the table or base type). + */ +static void +selectDumpableType(TypeInfo *tyinfo, Archive *fout) +{ + /* skip complex types, except for standalone composite types */ + if (OidIsValid(tyinfo->typrelid) && + tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE) + { + TableInfo *tytable = findTableByOid(tyinfo->typrelid); + + tyinfo->dobj.objType = DO_DUMMY_TYPE; + if (tytable != NULL) + tyinfo->dobj.dump = tytable->dobj.dump; + else + tyinfo->dobj.dump = DUMP_COMPONENT_NONE; + return; + } + + /* skip auto-generated array and multirange types */ + if (tyinfo->isArray || tyinfo->isMultirange) + { + tyinfo->dobj.objType = DO_DUMMY_TYPE; + + /* + * Fall through to set the dump flag; we assume that the subsequent + * rules will do the same thing as they would for the array's base + * type or multirange's range type. (We cannot reliably look up the + * base type here, since getTypes may not have processed it yet.) + */ + } + + if (checkExtensionMembership(&tyinfo->dobj, fout)) + return; /* extension membership overrides all else */ + + /* Dump based on if the contents of the namespace are being dumped */ + tyinfo->dobj.dump = tyinfo->dobj.namespace->dobj.dump_contains; +} + +/* + * selectDumpableDefaultACL: policy-setting subroutine + * Mark a default ACL as to be dumped or not + * + * For per-schema default ACLs, dump if the schema is to be dumped. + * Otherwise dump if we are dumping "everything". Note that dumpSchema + * and aclsSkip are checked separately. + */ +static void +selectDumpableDefaultACL(DefaultACLInfo *dinfo, DumpOptions *dopt) +{ + /* Default ACLs can't be extension members */ + + if (dinfo->dobj.namespace) + /* default ACLs are considered part of the namespace */ + dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump_contains; + else + dinfo->dobj.dump = dopt->include_everything ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; +} + +/* + * selectDumpableCast: policy-setting subroutine + * Mark a cast as to be dumped or not + * + * Casts do not belong to any particular namespace (since they haven't got + * names), nor do they have identifiable owners. To distinguish user-defined + * casts from built-in ones, we must resort to checking whether the cast's + * OID is in the range reserved for initdb. + */ +static void +selectDumpableCast(CastInfo *cast, Archive *fout) +{ + if (checkExtensionMembership(&cast->dobj, fout)) + return; /* extension membership overrides all else */ + + /* + * This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not + * support ACLs currently. + */ + if (cast->dobj.catId.oid <= g_last_builtin_oid) + cast->dobj.dump = DUMP_COMPONENT_NONE; + else + cast->dobj.dump = fout->dopt->include_everything ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; +} + +/* + * selectDumpableProcLang: policy-setting subroutine + * Mark a procedural language as to be dumped or not + * + * Procedural languages do not belong to any particular namespace. To + * identify built-in languages, we must resort to checking whether the + * language's OID is in the range reserved for initdb. + */ +static void +selectDumpableProcLang(ProcLangInfo *plang, Archive *fout) +{ + if (checkExtensionMembership(&plang->dobj, fout)) + return; /* extension membership overrides all else */ + + /* + * Only include procedural languages when we are dumping everything. + * + * For from-initdb procedural languages, only include ACLs, as we do for + * the pg_catalog namespace. We need this because procedural languages do + * not live in any namespace. + */ + if (!fout->dopt->include_everything) + plang->dobj.dump = DUMP_COMPONENT_NONE; + else + { + if (plang->dobj.catId.oid <= g_last_builtin_oid) + plang->dobj.dump = fout->remoteVersion < 90600 ? + DUMP_COMPONENT_NONE : DUMP_COMPONENT_ACL; + else + plang->dobj.dump = DUMP_COMPONENT_ALL; + } +} + +/* + * selectDumpableAccessMethod: policy-setting subroutine + * Mark an access method as to be dumped or not + * + * Access methods do not belong to any particular namespace. To identify + * built-in access methods, we must resort to checking whether the + * method's OID is in the range reserved for initdb. + */ +static void +selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout) +{ + /* see getAccessMethods() comment about v9.6. */ + if (fout->remoteVersion < 90600) + { + method->dobj.dump = DUMP_COMPONENT_NONE; + return; + } + + if (checkExtensionMembership(&method->dobj, fout)) + return; /* extension membership overrides all else */ + + /* + * This would be DUMP_COMPONENT_ACL for from-initdb access methods, but + * they do not support ACLs currently. + */ + if (method->dobj.catId.oid <= g_last_builtin_oid) + method->dobj.dump = DUMP_COMPONENT_NONE; + else + method->dobj.dump = fout->dopt->include_everything ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; +} + +/* + * selectDumpableExtension: policy-setting subroutine + * Mark an extension as to be dumped or not + * + * Built-in extensions should be skipped except for checking ACLs, since we + * assume those will already be installed in the target database. We identify + * such extensions by their having OIDs in the range reserved for initdb. + * We dump all user-added extensions by default. No extensions are dumped + * if include_everything is false (i.e., a --schema or --table switch was + * given), except if --extension specifies a list of extensions to dump. + */ +static void +selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt) +{ + /* + * Use DUMP_COMPONENT_ACL for built-in extensions, to allow users to + * change permissions on their member objects, if they wish to, and have + * those changes preserved. + */ + if (extinfo->dobj.catId.oid <= g_last_builtin_oid) + extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL; + else + { + /* check if there is a list of extensions to dump */ + if (extension_include_oids.head != NULL) + extinfo->dobj.dump = extinfo->dobj.dump_contains = + simple_oid_list_member(&extension_include_oids, + extinfo->dobj.catId.oid) ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; + else + extinfo->dobj.dump = extinfo->dobj.dump_contains = + dopt->include_everything ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; + + /* check that the extension is not explicitly excluded */ + if (extinfo->dobj.dump && + simple_oid_list_member(&extension_exclude_oids, + extinfo->dobj.catId.oid)) + extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_NONE; + } +} + +/* + * selectDumpablePublicationObject: policy-setting subroutine + * Mark a publication object as to be dumped or not + * + * A publication can have schemas and tables which have schemas, but those are + * ignored in decision making, because publications are only dumped when we are + * dumping everything. + */ +static void +selectDumpablePublicationObject(DumpableObject *dobj, Archive *fout) +{ + if (checkExtensionMembership(dobj, fout)) + return; /* extension membership overrides all else */ + + dobj->dump = fout->dopt->include_everything ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; +} + +/* + * selectDumpableStatisticsObject: policy-setting subroutine + * Mark an extended statistics object as to be dumped or not + * + * We dump an extended statistics object if the schema it's in and the table + * it's for are being dumped. (This'll need more thought if statistics + * objects ever support cross-table stats.) + */ +static void +selectDumpableStatisticsObject(StatsExtInfo *sobj, Archive *fout) +{ + if (checkExtensionMembership(&sobj->dobj, fout)) + return; /* extension membership overrides all else */ + + sobj->dobj.dump = sobj->dobj.namespace->dobj.dump_contains; + if (sobj->stattable == NULL || + !(sobj->stattable->dobj.dump & DUMP_COMPONENT_DEFINITION)) + sobj->dobj.dump = DUMP_COMPONENT_NONE; +} + +/* + * selectDumpableObject: policy-setting subroutine + * Mark a generic dumpable object as to be dumped or not + * + * Use this only for object types without a special-case routine above. + */ +static void +selectDumpableObject(DumpableObject *dobj, Archive *fout) +{ + if (checkExtensionMembership(dobj, fout)) + return; /* extension membership overrides all else */ + + /* + * Default policy is to dump if parent namespace is dumpable, or for + * non-namespace-associated items, dump if we're dumping "everything". + */ + if (dobj->namespace) + dobj->dump = dobj->namespace->dobj.dump_contains; + else + dobj->dump = fout->dopt->include_everything ? + DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE; +} + +/* + * Dump a table's contents for loading using the COPY command + * - this routine is called by the Archiver when it wants the table + * to be dumped. + */ +static int +dumpTableData_copy(Archive *fout, const void *dcontext) +{ + const TableDataInfo *tdinfo = dcontext; + const TableInfo *tbinfo = tdinfo->tdtable; + const char *classname = tbinfo->dobj.name; + PQExpBuffer q = createPQExpBuffer(); + + /* + * Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId + * which uses it already. + */ + PQExpBuffer clistBuf = createPQExpBuffer(); + PGconn *conn = GetConnection(fout); + PGresult *res; + int ret; + char *copybuf; + const char *column_list; + + pg_log_info("dumping contents of table \"%s.%s\"", + tbinfo->dobj.namespace->dobj.name, classname); + + /* + * Specify the column list explicitly so that we have no possibility of + * retrieving data in the wrong column order. (The default column + * ordering of COPY will not be what we want in certain corner cases + * involving ADD COLUMN and inheritance.) + */ + column_list = fmtCopyColumnList(tbinfo, clistBuf); + + /* + * Use COPY (SELECT ...) TO when dumping a foreign table's data, when a + * filter condition was specified, and when in binary upgrade mode and + * dumping an old pg_largeobject_metadata defined WITH OIDS. For other + * cases a simple COPY suffices. + */ + if (tdinfo->filtercond || tbinfo->relkind == RELKIND_FOREIGN_TABLE || + (fout->dopt->binary_upgrade && fout->remoteVersion < 120000 && + tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId)) + { + /* Temporary allows to access to foreign tables to dump data */ + if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) + set_restrict_relation_kind(fout, "view"); + + appendPQExpBufferStr(q, "COPY (SELECT "); + /* klugery to get rid of parens in column list */ + if (strlen(column_list) > 2) + { + appendPQExpBufferStr(q, column_list + 1); + q->data[q->len - 1] = ' '; + } + else + appendPQExpBufferStr(q, "* "); + + appendPQExpBuffer(q, "FROM %s %s) TO stdout;", + fmtQualifiedDumpable(tbinfo), + tdinfo->filtercond ? tdinfo->filtercond : ""); + } + else + { + appendPQExpBuffer(q, "COPY %s %s TO stdout;", + fmtQualifiedDumpable(tbinfo), + column_list); + } + res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT); + PQclear(res); + destroyPQExpBuffer(clistBuf); + + for (;;) + { + ret = PQgetCopyData(conn, ©buf, 0); + + if (ret < 0) + break; /* done or error */ + + if (copybuf) + { + WriteData(fout, copybuf, ret); + PQfreemem(copybuf); + } + + /* ---------- + * THROTTLE: + * + * There was considerable discussion in late July, 2000 regarding + * slowing down pg_dump when backing up large tables. Users with both + * slow & fast (multi-processor) machines experienced performance + * degradation when doing a backup. + * + * Initial attempts based on sleeping for a number of ms for each ms + * of work were deemed too complex, then a simple 'sleep in each loop' + * implementation was suggested. The latter failed because the loop + * was too tight. Finally, the following was implemented: + * + * If throttle is non-zero, then + * See how long since the last sleep. + * Work out how long to sleep (based on ratio). + * If sleep is more than 100ms, then + * sleep + * reset timer + * EndIf + * EndIf + * + * where the throttle value was the number of ms to sleep per ms of + * work. The calculation was done in each loop. + * + * Most of the hard work is done in the backend, and this solution + * still did not work particularly well: on slow machines, the ratio + * was 50:1, and on medium paced machines, 1:1, and on fast + * multi-processor machines, it had little or no effect, for reasons + * that were unclear. + * + * Further discussion ensued, and the proposal was dropped. + * + * For those people who want this feature, it can be implemented using + * gettimeofday in each loop, calculating the time since last sleep, + * multiplying that by the sleep ratio, then if the result is more + * than a preset 'minimum sleep time' (say 100ms), call the 'select' + * function to sleep for a subsecond period ie. + * + * select(0, NULL, NULL, NULL, &tvi); + * + * This will return after the interval specified in the structure tvi. + * Finally, call gettimeofday again to save the 'last sleep time'. + * ---------- + */ + } + archprintf(fout, "\\.\n\n\n"); + + if (ret == -2) + { + /* copy data transfer failed */ + pg_log_error("Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.", classname); + pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn)); + pg_log_error_detail("Command was: %s", q->data); + exit_nicely(1); + } + + /* Check command status and return to normal libpq state */ + res = PQgetResult(conn); + if (PQresultStatus(res) != PGRES_COMMAND_OK) + { + pg_log_error("Dumping the contents of table \"%s\" failed: PQgetResult() failed.", classname); + pg_log_error_detail("Error message from server: %s", PQerrorMessage(conn)); + pg_log_error_detail("Command was: %s", q->data); + exit_nicely(1); + } + PQclear(res); + + /* Do this to ensure we've pumped libpq back to idle state */ + if (PQgetResult(conn) != NULL) + pg_log_warning("unexpected extra results during COPY of table \"%s\"", + classname); + + destroyPQExpBuffer(q); + + /* Revert back the setting */ + if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) + set_restrict_relation_kind(fout, "view, foreign-table"); + + return 1; +} + +/* + * Dump table data using INSERT commands. + * + * Caution: when we restore from an archive file direct to database, the + * INSERT commands emitted by this function have to be parsed by + * pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments, + * E'' strings, or dollar-quoted strings. So don't emit anything like that. + */ +static int +dumpTableData_insert(Archive *fout, const void *dcontext) +{ + const TableDataInfo *tdinfo = dcontext; + const TableInfo *tbinfo = tdinfo->tdtable; + DumpOptions *dopt = fout->dopt; + PQExpBuffer q = createPQExpBuffer(); + PQExpBuffer insertStmt = NULL; + char *attgenerated; + PGresult *res; + int nfields, + i; + int rows_per_statement = dopt->dump_inserts; + int rows_this_statement = 0; + + /* Temporary allows to access to foreign tables to dump data */ + if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) + set_restrict_relation_kind(fout, "view"); + + /* + * If we're going to emit INSERTs with column names, the most efficient + * way to deal with generated columns is to exclude them entirely. For + * INSERTs without column names, we have to emit DEFAULT rather than the + * actual column value --- but we can save a few cycles by fetching nulls + * rather than the uninteresting-to-us value. + */ + attgenerated = pg_malloc_array(char, tbinfo->numatts); + appendPQExpBufferStr(q, "DECLARE _pg_dump_cursor CURSOR FOR SELECT "); + nfields = 0; + for (i = 0; i < tbinfo->numatts; i++) + { + if (tbinfo->attisdropped[i]) + continue; + if (tbinfo->attgenerated[i] && dopt->column_inserts) + continue; + if (nfields > 0) + appendPQExpBufferStr(q, ", "); + if (tbinfo->attgenerated[i]) + appendPQExpBufferStr(q, "NULL"); + else + appendPQExpBufferStr(q, fmtId(tbinfo->attnames[i])); + attgenerated[nfields] = tbinfo->attgenerated[i]; + nfields++; + } + /* Servers before 9.4 will complain about zero-column SELECT */ + if (nfields == 0) + appendPQExpBufferStr(q, "NULL"); + appendPQExpBuffer(q, " FROM ONLY %s", + fmtQualifiedDumpable(tbinfo)); + if (tdinfo->filtercond) + appendPQExpBuffer(q, " %s", tdinfo->filtercond); + + ExecuteSqlStatement(fout, q->data); + + while (1) + { + res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor", + PGRES_TUPLES_OK); + + /* cross-check field count, allowing for dummy NULL if any */ + if (nfields != PQnfields(res) && + !(nfields == 0 && PQnfields(res) == 1)) + pg_fatal("wrong number of fields retrieved from table \"%s\"", + tbinfo->dobj.name); + + /* + * First time through, we build as much of the INSERT statement as + * possible in "insertStmt", which we can then just print for each + * statement. If the table happens to have zero dumpable columns then + * this will be a complete statement, otherwise it will end in + * "VALUES" and be ready to have the row's column values printed. + */ + if (insertStmt == NULL) + { + const TableInfo *targettab; + + insertStmt = createPQExpBuffer(); + + /* + * When load-via-partition-root is set or forced, get the root + * table name for the partition table, so that we can reload data + * through the root table. + */ + if (tbinfo->ispartition && + (dopt->load_via_partition_root || + forcePartitionRootLoad(tbinfo))) + targettab = getRootTableInfo(tbinfo); + else + targettab = tbinfo; + + appendPQExpBuffer(insertStmt, "INSERT INTO %s ", + fmtQualifiedDumpable(targettab)); + + /* corner case for zero-column table */ + if (nfields == 0) + { + appendPQExpBufferStr(insertStmt, "DEFAULT VALUES;\n"); + } + else + { + /* append the list of column names if required */ + if (dopt->column_inserts) + { + appendPQExpBufferChar(insertStmt, '('); + for (int field = 0; field < nfields; field++) + { + if (field > 0) + appendPQExpBufferStr(insertStmt, ", "); + appendPQExpBufferStr(insertStmt, + fmtId(PQfname(res, field))); + } + appendPQExpBufferStr(insertStmt, ") "); + } + + if (tbinfo->needs_override) + appendPQExpBufferStr(insertStmt, "OVERRIDING SYSTEM VALUE "); + + appendPQExpBufferStr(insertStmt, "VALUES"); + } + } + + for (int tuple = 0; tuple < PQntuples(res); tuple++) + { + /* Write the INSERT if not in the middle of a multi-row INSERT. */ + if (rows_this_statement == 0) + archputs(insertStmt->data, fout); + + /* + * If it is zero-column table then we've already written the + * complete statement, which will mean we've disobeyed + * --rows-per-insert when it's set greater than 1. We do support + * a way to make this multi-row with: SELECT UNION ALL SELECT + * UNION ALL ... but that's non-standard so we should avoid it + * given that using INSERTs is mostly only ever needed for + * cross-database exports. + */ + if (nfields == 0) + continue; + + /* Emit a row heading */ + if (rows_per_statement == 1) + archputs(" (", fout); + else if (rows_this_statement > 0) + archputs(",\n\t(", fout); + else + archputs("\n\t(", fout); + + for (int field = 0; field < nfields; field++) + { + if (field > 0) + archputs(", ", fout); + if (attgenerated[field]) + { + archputs("DEFAULT", fout); + continue; + } + if (PQgetisnull(res, tuple, field)) + { + archputs("NULL", fout); + continue; + } + + /* XXX This code is partially duplicated in ruleutils.c */ + switch (PQftype(res, field)) + { + case INT2OID: + case INT4OID: + case INT8OID: + case OIDOID: + case FLOAT4OID: + case FLOAT8OID: + case NUMERICOID: + { + /* + * These types are printed without quotes unless + * they contain values that aren't accepted by the + * scanner unquoted (e.g., 'NaN'). Note that + * strtod() and friends might accept NaN, so we + * can't use that to test. + * + * In reality we only need to defend against + * infinity and NaN, so we need not get too crazy + * about pattern matching here. + */ + const char *s = PQgetvalue(res, tuple, field); + + if (strspn(s, "0123456789 +-eE.") == strlen(s)) + archputs(s, fout); + else + archprintf(fout, "'%s'", s); + } + break; + + case BITOID: + case VARBITOID: + archprintf(fout, "B'%s'", + PQgetvalue(res, tuple, field)); + break; + + case BOOLOID: + if (strcmp(PQgetvalue(res, tuple, field), "t") == 0) + archputs("true", fout); + else + archputs("false", fout); + break; + + default: + /* All other types are printed as string literals. */ + resetPQExpBuffer(q); + appendStringLiteralAH(q, + PQgetvalue(res, tuple, field), + fout); + archputs(q->data, fout); + break; + } + } + + /* Terminate the row ... */ + archputs(")", fout); + + /* ... and the statement, if the target no. of rows is reached */ + if (++rows_this_statement >= rows_per_statement) + { + if (dopt->do_nothing) + archputs(" ON CONFLICT DO NOTHING;\n", fout); + else + archputs(";\n", fout); + /* Reset the row counter */ + rows_this_statement = 0; + } + } + + if (PQntuples(res) <= 0) + { + PQclear(res); + break; + } + PQclear(res); + } + + /* Terminate any statements that didn't make the row count. */ + if (rows_this_statement > 0) + { + if (dopt->do_nothing) + archputs(" ON CONFLICT DO NOTHING;\n", fout); + else + archputs(";\n", fout); + } + + archputs("\n\n", fout); + + ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor"); + + destroyPQExpBuffer(q); + if (insertStmt != NULL) + destroyPQExpBuffer(insertStmt); + free(attgenerated); + + /* Revert back the setting */ + if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) + set_restrict_relation_kind(fout, "view, foreign-table"); + + return 1; +} + +/* + * getRootTableInfo: + * get the root TableInfo for the given partition table. + */ +static TableInfo * +getRootTableInfo(const TableInfo *tbinfo) +{ + TableInfo *parentTbinfo; + + Assert(tbinfo->ispartition); + Assert(tbinfo->numParents == 1); + + parentTbinfo = tbinfo->parents[0]; + while (parentTbinfo->ispartition) + { + Assert(parentTbinfo->numParents == 1); + parentTbinfo = parentTbinfo->parents[0]; + } + + return parentTbinfo; +} + +/* + * forcePartitionRootLoad + * Check if we must force load_via_partition_root for this partition. + * + * This is required if any level of ancestral partitioned table has an + * unsafe partitioning scheme. + */ +static bool +forcePartitionRootLoad(const TableInfo *tbinfo) +{ + TableInfo *parentTbinfo; + + Assert(tbinfo->ispartition); + Assert(tbinfo->numParents == 1); + + parentTbinfo = tbinfo->parents[0]; + if (parentTbinfo->unsafe_partitions) + return true; + while (parentTbinfo->ispartition) + { + Assert(parentTbinfo->numParents == 1); + parentTbinfo = parentTbinfo->parents[0]; + if (parentTbinfo->unsafe_partitions) + return true; + } + + return false; +} + +/* + * dumpTableData - + * dump the contents of a single table + * + * Actually, this just makes an ArchiveEntry for the table contents. + */ +static void +dumpTableData(Archive *fout, const TableDataInfo *tdinfo) +{ + DumpOptions *dopt = fout->dopt; + const TableInfo *tbinfo = tdinfo->tdtable; + PQExpBuffer copyBuf = createPQExpBuffer(); + PQExpBuffer clistBuf = createPQExpBuffer(); + DataDumperPtr dumpFn; + char *tdDefn = NULL; + char *copyStmt; + const char *copyFrom; + + /* We had better have loaded per-column details about this table */ + Assert(tbinfo->interesting); + + /* + * When load-via-partition-root is set or forced, get the root table name + * for the partition table, so that we can reload data through the root + * table. Then construct a comment to be inserted into the TOC entry's + * defn field, so that such cases can be identified reliably. + */ + if (tbinfo->ispartition && + (dopt->load_via_partition_root || + forcePartitionRootLoad(tbinfo))) + { + const TableInfo *parentTbinfo; + char *sanitized; + + parentTbinfo = getRootTableInfo(tbinfo); + copyFrom = fmtQualifiedDumpable(parentTbinfo); + sanitized = sanitize_line(copyFrom, true); + printfPQExpBuffer(copyBuf, "-- load via partition root %s", + sanitized); + free(sanitized); + tdDefn = pg_strdup(copyBuf->data); + } + else + copyFrom = fmtQualifiedDumpable(tbinfo); + + if (dopt->dump_inserts == 0) + { + /* Dump/restore using COPY */ + dumpFn = dumpTableData_copy; + /* must use 2 steps here 'cause fmtId is nonreentrant */ + printfPQExpBuffer(copyBuf, "COPY %s ", + copyFrom); + appendPQExpBuffer(copyBuf, "%s FROM stdin;\n", + fmtCopyColumnList(tbinfo, clistBuf)); + copyStmt = copyBuf->data; + } + else + { + /* Restore using INSERT */ + dumpFn = dumpTableData_insert; + copyStmt = NULL; + } + + /* + * Note: although the TableDataInfo is a full DumpableObject, we treat its + * dependency on its table as "special" and pass it to ArchiveEntry now. + * See comments for BuildArchiveDependencies. + */ + if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA) + { + TocEntry *te; + + te = ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tbinfo->dobj.name, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "TABLE DATA", + .section = SECTION_DATA, + .createStmt = tdDefn, + .copyStmt = copyStmt, + .deps = &(tbinfo->dobj.dumpId), + .nDeps = 1, + .dumpFn = dumpFn, + .dumpArg = tdinfo)); + + /* + * Set the TocEntry's dataLength in case we are doing a parallel dump + * and want to order dump jobs by table size. We choose to measure + * dataLength in table pages (including TOAST pages) during dump, so + * no scaling is needed. + * + * However, relpages is declared as "integer" in pg_class, and hence + * also in TableInfo, but it's really BlockNumber a/k/a unsigned int. + * Cast so that we get the right interpretation of table sizes + * exceeding INT_MAX pages. + */ + te->dataLength = (BlockNumber) tbinfo->relpages; + te->dataLength += (BlockNumber) tbinfo->toastpages; + + /* + * If pgoff_t is only 32 bits wide, the above refinement is useless, + * and instead we'd better worry about integer overflow. Clamp to + * INT_MAX if the correct result exceeds that. + */ + if (sizeof(te->dataLength) == 4 && + (tbinfo->relpages < 0 || tbinfo->toastpages < 0 || + te->dataLength < 0)) + te->dataLength = INT_MAX; + } + + destroyPQExpBuffer(copyBuf); + destroyPQExpBuffer(clistBuf); +} + +/* + * refreshMatViewData - + * load or refresh the contents of a single materialized view + * + * Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW + * statement. + */ +static void +refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo) +{ + TableInfo *tbinfo = tdinfo->tdtable; + PQExpBuffer q; + + /* If the materialized view is not flagged as populated, skip this. */ + if (!tbinfo->relispopulated) + return; + + q = createPQExpBuffer(); + + appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n", + fmtQualifiedDumpable(tbinfo)); + + if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA) + ArchiveEntry(fout, + tdinfo->dobj.catId, /* catalog ID */ + tdinfo->dobj.dumpId, /* dump ID */ + ARCHIVE_OPTS(.tag = tbinfo->dobj.name, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "MATERIALIZED VIEW DATA", + .section = SECTION_POST_DATA, + .createStmt = q->data, + .deps = tdinfo->dobj.dependencies, + .nDeps = tdinfo->dobj.nDeps)); + + destroyPQExpBuffer(q); +} + +/* + * getTableData - + * set up dumpable objects representing the contents of tables + */ +static void +getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind) +{ + int i; + + for (i = 0; i < numTables; i++) + { + if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA && + (!relkind || tblinfo[i].relkind == relkind)) + makeTableDataInfo(dopt, &(tblinfo[i])); + } +} + +/* + * Make a dumpable object for the data of this specific table + * + * Note: we make a TableDataInfo if and only if we are going to dump the + * table data; the "dump" field in such objects isn't very interesting. + */ +static void +makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo) +{ + TableDataInfo *tdinfo; + + /* + * Nothing to do if we already decided to dump the table. This will + * happen for "config" tables. + */ + if (tbinfo->dataObj != NULL) + return; + + /* Skip property graphs (no data to dump) */ + if (tbinfo->relkind == RELKIND_PROPGRAPH) + return; + /* Skip VIEWs (no data to dump) */ + if (tbinfo->relkind == RELKIND_VIEW) + return; + /* Skip FOREIGN TABLEs (no data to dump) unless requested explicitly */ + if (tbinfo->relkind == RELKIND_FOREIGN_TABLE && + (foreign_servers_include_oids.head == NULL || + !simple_oid_list_member(&foreign_servers_include_oids, + tbinfo->foreign_server))) + return; + /* Skip partitioned tables (data in partitions) */ + if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE) + return; + + /* Don't dump data in unlogged tables, if so requested */ + if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED && + dopt->no_unlogged_table_data) + return; + + /* Check that the data is not explicitly excluded */ + if (simple_oid_list_member(&tabledata_exclude_oids, + tbinfo->dobj.catId.oid)) + return; + + /* OK, let's dump it */ + tdinfo = pg_malloc_object(TableDataInfo); + + if (tbinfo->relkind == RELKIND_MATVIEW) + tdinfo->dobj.objType = DO_REFRESH_MATVIEW; + else if (tbinfo->relkind == RELKIND_SEQUENCE) + tdinfo->dobj.objType = DO_SEQUENCE_SET; + else + tdinfo->dobj.objType = DO_TABLE_DATA; + + /* + * Note: use tableoid 0 so that this object won't be mistaken for + * something that pg_depend entries apply to. + */ + tdinfo->dobj.catId.tableoid = 0; + tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid; + AssignDumpId(&tdinfo->dobj); + tdinfo->dobj.name = tbinfo->dobj.name; + tdinfo->dobj.namespace = tbinfo->dobj.namespace; + tdinfo->tdtable = tbinfo; + tdinfo->filtercond = NULL; /* might get set later */ + addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId); + + /* A TableDataInfo contains data, of course */ + tdinfo->dobj.components |= DUMP_COMPONENT_DATA; + + tbinfo->dataObj = tdinfo; + + /* + * Materialized view statistics must be restored after the data, because + * REFRESH MATERIALIZED VIEW replaces the storage and resets the stats. + * + * The dependency is added here because the statistics objects are created + * first. + */ + if (tbinfo->relkind == RELKIND_MATVIEW && tbinfo->stats != NULL) + { + tbinfo->stats->section = SECTION_POST_DATA; + addObjectDependency(&tbinfo->stats->dobj, tdinfo->dobj.dumpId); + } + + /* Make sure that we'll collect per-column info for this table. */ + tbinfo->interesting = true; +} + +/* + * The refresh for a materialized view must be dependent on the refresh for + * any materialized view that this one is dependent on. + * + * This must be called after all the objects are created, but before they are + * sorted. + */ +static void +buildMatViewRefreshDependencies(Archive *fout) +{ + PQExpBuffer query; + PGresult *res; + int ntups, + i; + int i_classid, + i_objid, + i_refobjid; + + /* No Mat Views before 9.3. */ + if (fout->remoteVersion < 90300) + return; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "WITH RECURSIVE w AS " + "( " + "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind " + "FROM pg_depend d1 " + "JOIN pg_class c1 ON c1.oid = d1.objid " + "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW) + " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid " + "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass " + "AND d2.objid = r1.oid " + "AND d2.refobjid <> d1.objid " + "JOIN pg_class c2 ON c2.oid = d2.refobjid " + "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) "," + CppAsString2(RELKIND_VIEW) ") " + "WHERE d1.classid = 'pg_class'::regclass " + "UNION " + "SELECT w.objid, d3.refobjid, c3.relkind " + "FROM w " + "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid " + "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass " + "AND d3.objid = r3.oid " + "AND d3.refobjid <> w.refobjid " + "JOIN pg_class c3 ON c3.oid = d3.refobjid " + "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) "," + CppAsString2(RELKIND_VIEW) ") " + ") " + "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid " + "FROM w " + "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW)); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_classid = PQfnumber(res, "classid"); + i_objid = PQfnumber(res, "objid"); + i_refobjid = PQfnumber(res, "refobjid"); + + for (i = 0; i < ntups; i++) + { + CatalogId objId; + CatalogId refobjId; + DumpableObject *dobj; + DumpableObject *refdobj; + TableInfo *tbinfo; + TableInfo *reftbinfo; + + objId.tableoid = atooid(PQgetvalue(res, i, i_classid)); + objId.oid = atooid(PQgetvalue(res, i, i_objid)); + refobjId.tableoid = objId.tableoid; + refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid)); + + dobj = findObjectByCatalogId(objId); + if (dobj == NULL) + continue; + + Assert(dobj->objType == DO_TABLE); + tbinfo = (TableInfo *) dobj; + Assert(tbinfo->relkind == RELKIND_MATVIEW); + dobj = (DumpableObject *) tbinfo->dataObj; + if (dobj == NULL) + continue; + Assert(dobj->objType == DO_REFRESH_MATVIEW); + + refdobj = findObjectByCatalogId(refobjId); + if (refdobj == NULL) + continue; + + Assert(refdobj->objType == DO_TABLE); + reftbinfo = (TableInfo *) refdobj; + Assert(reftbinfo->relkind == RELKIND_MATVIEW); + refdobj = (DumpableObject *) reftbinfo->dataObj; + if (refdobj == NULL) + continue; + Assert(refdobj->objType == DO_REFRESH_MATVIEW); + + addObjectDependency(dobj, refdobj->dumpId); + + if (!reftbinfo->relispopulated) + tbinfo->relispopulated = false; + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getTableDataFKConstraints - + * add dump-order dependencies reflecting foreign key constraints + * + * This code is executed only in a data-only dump --- in schema+data dumps + * we handle foreign key issues by not creating the FK constraints until + * after the data is loaded. In a data-only dump, however, we want to + * order the table data objects in such a way that a table's referenced + * tables are restored first. (In the presence of circular references or + * self-references this may be impossible; we'll detect and complain about + * that during the dependency sorting step.) + */ +static void +getTableDataFKConstraints(void) +{ + DumpableObject **dobjs; + int numObjs; + int i; + + /* Search through all the dumpable objects for FK constraints */ + getDumpableObjects(&dobjs, &numObjs); + for (i = 0; i < numObjs; i++) + { + if (dobjs[i]->objType == DO_FK_CONSTRAINT) + { + ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i]; + TableInfo *ftable; + + /* Not interesting unless both tables are to be dumped */ + if (cinfo->contable == NULL || + cinfo->contable->dataObj == NULL) + continue; + ftable = findTableByOid(cinfo->confrelid); + if (ftable == NULL || + ftable->dataObj == NULL) + continue; + + /* + * Okay, make referencing table's TABLE_DATA object depend on the + * referenced table's TABLE_DATA object. + */ + addObjectDependency(&cinfo->contable->dataObj->dobj, + ftable->dataObj->dobj.dumpId); + } + } + free(dobjs); +} + + +/* + * dumpDatabase: + * dump the database definition + */ +static void +dumpDatabase(Archive *fout) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer dbQry = createPQExpBuffer(); + PQExpBuffer delQry = createPQExpBuffer(); + PQExpBuffer creaQry = createPQExpBuffer(); + PQExpBuffer labelq = createPQExpBuffer(); + PGconn *conn = GetConnection(fout); + PGresult *res; + int i_tableoid, + i_oid, + i_datname, + i_datdba, + i_encoding, + i_datlocprovider, + i_collate, + i_ctype, + i_datlocale, + i_daticurules, + i_frozenxid, + i_minmxid, + i_datacl, + i_acldefault, + i_datistemplate, + i_datconnlimit, + i_datcollversion, + i_tablespace; + CatalogId dbCatId; + DumpId dbDumpId; + DumpableAcl dbdacl; + const char *datname, + *dba, + *encoding, + *datlocprovider, + *collate, + *ctype, + *locale, + *icurules, + *datistemplate, + *datconnlimit, + *tablespace; + uint32 frozenxid, + minmxid; + char *qdatname; + + pg_log_info("saving database definition"); + + /* + * Fetch the database-level properties for this database. + */ + appendPQExpBufferStr(dbQry, "SELECT tableoid, oid, datname, " + "datdba, " + "pg_encoding_to_char(encoding) AS encoding, " + "datcollate, datctype, datfrozenxid, " + "datacl, acldefault('d', datdba) AS acldefault, " + "datistemplate, datconnlimit, "); + if (fout->remoteVersion >= 90300) + appendPQExpBufferStr(dbQry, "datminmxid, "); + else + appendPQExpBufferStr(dbQry, "0 AS datminmxid, "); + if (fout->remoteVersion >= 170000) + appendPQExpBufferStr(dbQry, "datlocprovider, datlocale, datcollversion, "); + else if (fout->remoteVersion >= 150000) + appendPQExpBufferStr(dbQry, "datlocprovider, daticulocale AS datlocale, datcollversion, "); + else + appendPQExpBufferStr(dbQry, "'c' AS datlocprovider, NULL AS datlocale, NULL AS datcollversion, "); + if (fout->remoteVersion >= 160000) + appendPQExpBufferStr(dbQry, "daticurules, "); + else + appendPQExpBufferStr(dbQry, "NULL AS daticurules, "); + appendPQExpBufferStr(dbQry, + "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, " + "shobj_description(oid, 'pg_database') AS description " + "FROM pg_database " + "WHERE datname = current_database()"); + + res = ExecuteSqlQueryForSingleRow(fout, dbQry->data); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_datname = PQfnumber(res, "datname"); + i_datdba = PQfnumber(res, "datdba"); + i_encoding = PQfnumber(res, "encoding"); + i_datlocprovider = PQfnumber(res, "datlocprovider"); + i_collate = PQfnumber(res, "datcollate"); + i_ctype = PQfnumber(res, "datctype"); + i_datlocale = PQfnumber(res, "datlocale"); + i_daticurules = PQfnumber(res, "daticurules"); + i_frozenxid = PQfnumber(res, "datfrozenxid"); + i_minmxid = PQfnumber(res, "datminmxid"); + i_datacl = PQfnumber(res, "datacl"); + i_acldefault = PQfnumber(res, "acldefault"); + i_datistemplate = PQfnumber(res, "datistemplate"); + i_datconnlimit = PQfnumber(res, "datconnlimit"); + i_datcollversion = PQfnumber(res, "datcollversion"); + i_tablespace = PQfnumber(res, "tablespace"); + + dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid)); + dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid)); + datname = PQgetvalue(res, 0, i_datname); + dba = getRoleName(PQgetvalue(res, 0, i_datdba)); + encoding = PQgetvalue(res, 0, i_encoding); + datlocprovider = PQgetvalue(res, 0, i_datlocprovider); + collate = PQgetvalue(res, 0, i_collate); + ctype = PQgetvalue(res, 0, i_ctype); + if (!PQgetisnull(res, 0, i_datlocale)) + locale = PQgetvalue(res, 0, i_datlocale); + else + locale = NULL; + if (!PQgetisnull(res, 0, i_daticurules)) + icurules = PQgetvalue(res, 0, i_daticurules); + else + icurules = NULL; + frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid)); + minmxid = atooid(PQgetvalue(res, 0, i_minmxid)); + dbdacl.acl = PQgetvalue(res, 0, i_datacl); + dbdacl.acldefault = PQgetvalue(res, 0, i_acldefault); + datistemplate = PQgetvalue(res, 0, i_datistemplate); + datconnlimit = PQgetvalue(res, 0, i_datconnlimit); + tablespace = PQgetvalue(res, 0, i_tablespace); + + qdatname = pg_strdup(fmtId(datname)); + + /* + * Prepare the CREATE DATABASE command. We must specify OID (if we want + * to preserve that), as well as the encoding, locale, and tablespace + * since those can't be altered later. Other DB properties are left to + * the DATABASE PROPERTIES entry, so that they can be applied after + * reconnecting to the target DB. + * + * For binary upgrade, we use the FILE_COPY strategy because testing has + * shown it to be faster. When the server is in binary upgrade mode, it + * will also skip the checkpoints this strategy ordinarily performs. + */ + if (dopt->binary_upgrade) + { + appendPQExpBuffer(creaQry, + "CREATE DATABASE %s WITH TEMPLATE = template0 " + "OID = %u STRATEGY = FILE_COPY", + qdatname, dbCatId.oid); + } + else + { + appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0", + qdatname); + } + if (strlen(encoding) > 0) + { + appendPQExpBufferStr(creaQry, " ENCODING = "); + appendStringLiteralAH(creaQry, encoding, fout); + } + + appendPQExpBufferStr(creaQry, " LOCALE_PROVIDER = "); + if (datlocprovider[0] == 'b') + appendPQExpBufferStr(creaQry, "builtin"); + else if (datlocprovider[0] == 'c') + appendPQExpBufferStr(creaQry, "libc"); + else if (datlocprovider[0] == 'i') + appendPQExpBufferStr(creaQry, "icu"); + else + pg_fatal("unrecognized locale provider: %s", + datlocprovider); + + if (strlen(collate) > 0 && strcmp(collate, ctype) == 0) + { + appendPQExpBufferStr(creaQry, " LOCALE = "); + appendStringLiteralAH(creaQry, collate, fout); + } + else + { + if (strlen(collate) > 0) + { + appendPQExpBufferStr(creaQry, " LC_COLLATE = "); + appendStringLiteralAH(creaQry, collate, fout); + } + if (strlen(ctype) > 0) + { + appendPQExpBufferStr(creaQry, " LC_CTYPE = "); + appendStringLiteralAH(creaQry, ctype, fout); + } + } + if (locale) + { + if (datlocprovider[0] == 'b') + appendPQExpBufferStr(creaQry, " BUILTIN_LOCALE = "); + else + appendPQExpBufferStr(creaQry, " ICU_LOCALE = "); + + appendStringLiteralAH(creaQry, locale, fout); + } + + if (icurules) + { + appendPQExpBufferStr(creaQry, " ICU_RULES = "); + appendStringLiteralAH(creaQry, icurules, fout); + } + + /* + * For binary upgrade, carry over the collation version. For normal + * dump/restore, omit the version, so that it is computed upon restore. + */ + if (dopt->binary_upgrade) + { + if (!PQgetisnull(res, 0, i_datcollversion)) + { + appendPQExpBufferStr(creaQry, " COLLATION_VERSION = "); + appendStringLiteralAH(creaQry, + PQgetvalue(res, 0, i_datcollversion), + fout); + } + } + + /* + * Note: looking at dopt->outputNoTablespaces here is completely the wrong + * thing; the decision whether to specify a tablespace should be left till + * pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd + * label the DATABASE entry with the tablespace and let the normal + * tablespace selection logic work ... but CREATE DATABASE doesn't pay + * attention to default_tablespace, so that won't work. + */ + if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 && + !dopt->outputNoTablespaces) + appendPQExpBuffer(creaQry, " TABLESPACE = %s", + fmtId(tablespace)); + appendPQExpBufferStr(creaQry, ";\n"); + + appendPQExpBuffer(delQry, "DROP DATABASE %s;\n", + qdatname); + + dbDumpId = createDumpId(); + + ArchiveEntry(fout, + dbCatId, /* catalog ID */ + dbDumpId, /* dump ID */ + ARCHIVE_OPTS(.tag = datname, + .owner = dba, + .description = "DATABASE", + .section = SECTION_PRE_DATA, + .createStmt = creaQry->data, + .dropStmt = delQry->data)); + + /* Compute correct tag for archive entry */ + appendPQExpBuffer(labelq, "DATABASE %s", qdatname); + + /* Dump DB comment if any */ + { + /* + * 8.2 and up keep comments on shared objects in a shared table, so we + * cannot use the dumpComment() code used for other database objects. + * Be careful that the ArchiveEntry parameters match that function. + */ + char *comment = PQgetvalue(res, 0, PQfnumber(res, "description")); + + if (comment && *comment && !dopt->no_comments) + { + resetPQExpBuffer(dbQry); + + /* + * Generates warning when loaded into a differently-named + * database. + */ + appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname); + appendStringLiteralAH(dbQry, comment, fout); + appendPQExpBufferStr(dbQry, ";\n"); + + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = labelq->data, + .owner = dba, + .description = "COMMENT", + .section = SECTION_NONE, + .createStmt = dbQry->data, + .deps = &dbDumpId, + .nDeps = 1)); + } + } + + /* Dump DB security label, if enabled */ + if (!dopt->no_security_labels) + { + PGresult *shres; + PQExpBuffer seclabelQry; + + seclabelQry = createPQExpBuffer(); + + buildShSecLabelQuery("pg_database", dbCatId.oid, seclabelQry); + shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK); + resetPQExpBuffer(seclabelQry); + emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname); + if (seclabelQry->len > 0) + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = labelq->data, + .owner = dba, + .description = "SECURITY LABEL", + .section = SECTION_NONE, + .createStmt = seclabelQry->data, + .deps = &dbDumpId, + .nDeps = 1)); + destroyPQExpBuffer(seclabelQry); + PQclear(shres); + } + + /* + * Dump ACL if any. Note that we do not support initial privileges + * (pg_init_privs) on databases. + */ + dbdacl.privtype = 0; + dbdacl.initprivs = NULL; + + dumpACL(fout, dbDumpId, InvalidDumpId, "DATABASE", + qdatname, NULL, NULL, + NULL, dba, &dbdacl); + + /* + * Now construct a DATABASE PROPERTIES archive entry to restore any + * non-default database-level properties. (The reason this must be + * separate is that we cannot put any additional commands into the TOC + * entry that has CREATE DATABASE. pg_restore would execute such a group + * in an implicit transaction block, and the backend won't allow CREATE + * DATABASE in that context.) + */ + resetPQExpBuffer(creaQry); + resetPQExpBuffer(delQry); + + if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0) + appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n", + qdatname, datconnlimit); + + if (strcmp(datistemplate, "t") == 0) + { + appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n", + qdatname); + + /* + * The backend won't accept DROP DATABASE on a template database. We + * can deal with that by removing the template marking before the DROP + * gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but + * since no such command is currently supported, fake it with a direct + * UPDATE on pg_database. + */ + appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database " + "SET datistemplate = false WHERE datname = "); + appendStringLiteralAH(delQry, datname, fout); + appendPQExpBufferStr(delQry, ";\n"); + } + + /* + * We do not restore pg_database.dathasloginevt because it is set + * automatically on login event trigger creation. + */ + + /* Add database-specific SET options */ + dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid); + + /* + * We stick this binary-upgrade query into the DATABASE PROPERTIES archive + * entry, too, for lack of a better place. + */ + if (dopt->binary_upgrade) + { + appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n"); + appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n" + "SET datfrozenxid = '%u', datminmxid = '%u'\n" + "WHERE datname = ", + frozenxid, minmxid); + appendStringLiteralAH(creaQry, datname, fout); + appendPQExpBufferStr(creaQry, ";\n"); + } + + if (creaQry->len > 0) + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = datname, + .owner = dba, + .description = "DATABASE PROPERTIES", + .section = SECTION_PRE_DATA, + .createStmt = creaQry->data, + .dropStmt = delQry->data, + .deps = &dbDumpId)); + + /* + * pg_largeobject comes from the old system intact, so set its + * relfrozenxids, relminmxids and relfilenode. + * + * pg_largeobject_metadata also comes from the old system intact for + * upgrades from v16 and newer, so set its relfrozenxids, relminmxids, and + * relfilenode, too. pg_upgrade can't copy/link the files from older + * versions because aclitem (needed by pg_largeobject_metadata.lomacl) + * changed its storage format in v16. + */ + if (dopt->binary_upgrade) + { + PGresult *lo_res; + PQExpBuffer loFrozenQry = createPQExpBuffer(); + PQExpBuffer loOutQry = createPQExpBuffer(); + PQExpBuffer lomOutQry = createPQExpBuffer(); + PQExpBuffer loHorizonQry = createPQExpBuffer(); + PQExpBuffer lomHorizonQry = createPQExpBuffer(); + int ii_relfrozenxid, + ii_relfilenode, + ii_oid, + ii_relminmxid; + + if (fout->remoteVersion >= 90300) + appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid, relfilenode, oid\n" + "FROM pg_catalog.pg_class\n" + "WHERE oid IN (%u, %u, %u, %u);\n", + LargeObjectRelationId, LargeObjectLOidPNIndexId, + LargeObjectMetadataRelationId, LargeObjectMetadataOidIndexId); + else + appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid, relfilenode, oid\n" + "FROM pg_catalog.pg_class\n" + "WHERE oid IN (%u, %u);\n", + LargeObjectRelationId, LargeObjectLOidPNIndexId); + + lo_res = ExecuteSqlQuery(fout, loFrozenQry->data, PGRES_TUPLES_OK); + + ii_relfrozenxid = PQfnumber(lo_res, "relfrozenxid"); + ii_relminmxid = PQfnumber(lo_res, "relminmxid"); + ii_relfilenode = PQfnumber(lo_res, "relfilenode"); + ii_oid = PQfnumber(lo_res, "oid"); + + appendPQExpBufferStr(loHorizonQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n"); + appendPQExpBufferStr(lomHorizonQry, "\n-- For binary upgrade, set pg_largeobject_metadata relfrozenxid and relminmxid\n"); + appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, preserve pg_largeobject and index relfilenodes\n"); + appendPQExpBufferStr(lomOutQry, "\n-- For binary upgrade, preserve pg_largeobject_metadata and index relfilenodes\n"); + for (int i = 0; i < PQntuples(lo_res); ++i) + { + Oid oid; + RelFileNumber relfilenumber; + PQExpBuffer horizonQry; + PQExpBuffer outQry; + + oid = atooid(PQgetvalue(lo_res, i, ii_oid)); + relfilenumber = atooid(PQgetvalue(lo_res, i, ii_relfilenode)); + + if (oid == LargeObjectRelationId || + oid == LargeObjectLOidPNIndexId) + { + horizonQry = loHorizonQry; + outQry = loOutQry; + } + else + { + horizonQry = lomHorizonQry; + outQry = lomOutQry; + } + + appendPQExpBuffer(horizonQry, "UPDATE pg_catalog.pg_class\n" + "SET relfrozenxid = '%u', relminmxid = '%u'\n" + "WHERE oid = %u;\n", + atooid(PQgetvalue(lo_res, i, ii_relfrozenxid)), + atooid(PQgetvalue(lo_res, i, ii_relminmxid)), + atooid(PQgetvalue(lo_res, i, ii_oid))); + + if (oid == LargeObjectRelationId || + oid == LargeObjectMetadataRelationId) + appendPQExpBuffer(outQry, + "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n", + relfilenumber); + else if (oid == LargeObjectLOidPNIndexId || + oid == LargeObjectMetadataOidIndexId) + appendPQExpBuffer(outQry, + "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n", + relfilenumber); + } + + appendPQExpBufferStr(loOutQry, + "TRUNCATE pg_catalog.pg_largeobject;\n"); + appendPQExpBufferStr(lomOutQry, + "TRUNCATE pg_catalog.pg_largeobject_metadata;\n"); + + appendPQExpBufferStr(loOutQry, loHorizonQry->data); + appendPQExpBufferStr(lomOutQry, lomHorizonQry->data); + + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = "pg_largeobject", + .description = "pg_largeobject", + .section = SECTION_PRE_DATA, + .createStmt = loOutQry->data)); + + if (fout->remoteVersion >= 160000) + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = "pg_largeobject_metadata", + .description = "pg_largeobject_metadata", + .section = SECTION_PRE_DATA, + .createStmt = lomOutQry->data)); + + PQclear(lo_res); + + destroyPQExpBuffer(loFrozenQry); + destroyPQExpBuffer(loHorizonQry); + destroyPQExpBuffer(lomHorizonQry); + destroyPQExpBuffer(loOutQry); + destroyPQExpBuffer(lomOutQry); + } + + PQclear(res); + + free(qdatname); + destroyPQExpBuffer(dbQry); + destroyPQExpBuffer(delQry); + destroyPQExpBuffer(creaQry); + destroyPQExpBuffer(labelq); +} + +/* + * Collect any database-specific or role-and-database-specific SET options + * for this database, and append them to outbuf. + */ +static void +dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf, + const char *dbname, Oid dboid) +{ + PGconn *conn = GetConnection(AH); + PQExpBuffer buf = createPQExpBuffer(); + PGresult *res; + + /* First collect database-specific options */ + printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting " + "WHERE setrole = 0 AND setdatabase = '%u'::oid", + dboid); + + res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK); + + for (int i = 0; i < PQntuples(res); i++) + makeAlterConfigCommand(conn, PQgetvalue(res, i, 0), + "DATABASE", dbname, NULL, NULL, + outbuf); + + PQclear(res); + + /* Now look for role-and-database-specific options */ + printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) " + "FROM pg_db_role_setting s, pg_roles r " + "WHERE setrole = r.oid AND setdatabase = '%u'::oid", + dboid); + + res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK); + + for (int i = 0; i < PQntuples(res); i++) + makeAlterConfigCommand(conn, PQgetvalue(res, i, 1), + "ROLE", PQgetvalue(res, i, 0), + "DATABASE", dbname, + outbuf); + + PQclear(res); + + destroyPQExpBuffer(buf); +} + +/* + * dumpEncoding: put the correct encoding into the archive + */ +static void +dumpEncoding(Archive *AH) +{ + const char *encname = pg_encoding_to_char(AH->encoding); + PQExpBuffer qry = createPQExpBuffer(); + + pg_log_info("saving encoding = %s", encname); + + appendPQExpBufferStr(qry, "SET client_encoding = "); + appendStringLiteralAH(qry, encname, AH); + appendPQExpBufferStr(qry, ";\n"); + + ArchiveEntry(AH, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = "ENCODING", + .description = "ENCODING", + .section = SECTION_PRE_DATA, + .createStmt = qry->data)); + + destroyPQExpBuffer(qry); +} + + +/* + * dumpStdStrings: put the correct escape string behavior into the archive + */ +static void +dumpStdStrings(Archive *AH) +{ + const char *stdstrings = AH->std_strings ? "on" : "off"; + PQExpBuffer qry = createPQExpBuffer(); + + pg_log_info("saving \"standard_conforming_strings = %s\"", + stdstrings); + + appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n", + stdstrings); + + ArchiveEntry(AH, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = "STDSTRINGS", + .description = "STDSTRINGS", + .section = SECTION_PRE_DATA, + .createStmt = qry->data)); + + destroyPQExpBuffer(qry); +} + +/* + * dumpSearchPath: record the active search_path in the archive + */ +static void +dumpSearchPath(Archive *AH) +{ + PQExpBuffer qry = createPQExpBuffer(); + PQExpBuffer path = createPQExpBuffer(); + PGresult *res; + char **schemanames = NULL; + int nschemanames = 0; + int i; + + /* + * We use the result of current_schemas(), not the search_path GUC, + * because that might contain wildcards such as "$user", which won't + * necessarily have the same value during restore. Also, this way avoids + * listing schemas that may appear in search_path but not actually exist, + * which seems like a prudent exclusion. + */ + res = ExecuteSqlQueryForSingleRow(AH, + "SELECT pg_catalog.current_schemas(false)"); + + if (!parsePGArray(PQgetvalue(res, 0, 0), &schemanames, &nschemanames)) + pg_fatal("could not parse result of current_schemas()"); + + /* + * We use set_config(), not a simple "SET search_path" command, because + * the latter has less-clean behavior if the search path is empty. While + * that's likely to get fixed at some point, it seems like a good idea to + * be as backwards-compatible as possible in what we put into archives. + */ + for (i = 0; i < nschemanames; i++) + { + if (i > 0) + appendPQExpBufferStr(path, ", "); + appendPQExpBufferStr(path, fmtId(schemanames[i])); + } + + appendPQExpBufferStr(qry, "SELECT pg_catalog.set_config('search_path', "); + appendStringLiteralAH(qry, path->data, AH); + appendPQExpBufferStr(qry, ", false);\n"); + + pg_log_info("saving \"search_path = %s\"", path->data); + + ArchiveEntry(AH, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = "SEARCHPATH", + .description = "SEARCHPATH", + .section = SECTION_PRE_DATA, + .createStmt = qry->data)); + + /* Also save it in AH->searchpath, in case we're doing plain text dump */ + AH->searchpath = pg_strdup(qry->data); + + free(schemanames); + PQclear(res); + destroyPQExpBuffer(qry); + destroyPQExpBuffer(path); +} + + +/* + * getLOs: + * Collect schema-level data about large objects + */ +static void +getLOs(Archive *fout) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer loQry = createPQExpBuffer(); + PGresult *res; + int ntups; + int i; + int n; + int i_oid; + int i_lomowner; + int i_lomacl; + int i_acldefault; + + pg_log_info("reading large objects"); + + /* + * Fetch LO OIDs and owner/ACL data. Order the data so that all the blobs + * with the same owner/ACL appear together. + */ + appendPQExpBufferStr(loQry, + "SELECT oid, lomowner, lomacl, " + "acldefault('L', lomowner) AS acldefault " + "FROM pg_largeobject_metadata "); + + /* + * For binary upgrades, we transfer pg_largeobject_metadata via COPY or by + * copying/linking its files from the old cluster. On such upgrades, we + * only need to consider large objects that have comments or security + * labels, since we still restore those objects via COMMENT/SECURITY LABEL + * commands. + */ + if (dopt->binary_upgrade) + appendPQExpBufferStr(loQry, + "WHERE oid IN " + "(SELECT objoid FROM pg_description " + "WHERE classoid = " CppAsString2(LargeObjectRelationId) " " + "UNION SELECT objoid FROM pg_seclabel " + "WHERE classoid = " CppAsString2(LargeObjectRelationId) ") "); + + appendPQExpBufferStr(loQry, + "ORDER BY lomowner, lomacl::pg_catalog.text, oid"); + + res = ExecuteSqlQuery(fout, loQry->data, PGRES_TUPLES_OK); + + i_oid = PQfnumber(res, "oid"); + i_lomowner = PQfnumber(res, "lomowner"); + i_lomacl = PQfnumber(res, "lomacl"); + i_acldefault = PQfnumber(res, "acldefault"); + + ntups = PQntuples(res); + + /* + * Group the blobs into suitably-sized groups that have the same owner and + * ACL setting, and build a metadata and a data DumpableObject for each + * group. (If we supported initprivs for blobs, we'd have to insist that + * groups also share initprivs settings, since the DumpableObject only has + * room for one.) i is the index of the first tuple in the current group, + * and n is the number of tuples we include in the group. + */ + for (i = 0; i < ntups; i += n) + { + Oid thisoid = atooid(PQgetvalue(res, i, i_oid)); + char *thisowner = PQgetvalue(res, i, i_lomowner); + char *thisacl = PQgetvalue(res, i, i_lomacl); + LoInfo *loinfo; + DumpableObject *lodata; + char namebuf[64]; + + /* Scan to find first tuple not to be included in group */ + n = 1; + while (n < MAX_BLOBS_PER_ARCHIVE_ENTRY && i + n < ntups) + { + if (strcmp(thisowner, PQgetvalue(res, i + n, i_lomowner)) != 0 || + strcmp(thisacl, PQgetvalue(res, i + n, i_lomacl)) != 0) + break; + n++; + } + + /* Build the metadata DumpableObject */ + loinfo = (LoInfo *) pg_malloc(offsetof(LoInfo, looids) + n * sizeof(Oid)); + + loinfo->dobj.objType = DO_LARGE_OBJECT; + loinfo->dobj.catId.tableoid = LargeObjectRelationId; + loinfo->dobj.catId.oid = thisoid; + AssignDumpId(&loinfo->dobj); + + if (n > 1) + snprintf(namebuf, sizeof(namebuf), "%u..%u", thisoid, + atooid(PQgetvalue(res, i + n - 1, i_oid))); + else + snprintf(namebuf, sizeof(namebuf), "%u", thisoid); + loinfo->dobj.name = pg_strdup(namebuf); + loinfo->dacl.acl = pg_strdup(thisacl); + loinfo->dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + loinfo->dacl.privtype = 0; + loinfo->dacl.initprivs = NULL; + loinfo->rolname = getRoleName(thisowner); + loinfo->numlos = n; + loinfo->looids[0] = thisoid; + /* Collect OIDs of the remaining blobs in this group */ + for (int k = 1; k < n; k++) + { + CatalogId extraID; + + loinfo->looids[k] = atooid(PQgetvalue(res, i + k, i_oid)); + + /* Make sure we can look up loinfo by any of the blobs' OIDs */ + extraID.tableoid = LargeObjectRelationId; + extraID.oid = loinfo->looids[k]; + recordAdditionalCatalogID(extraID, &loinfo->dobj); + } + + /* LOs have data */ + loinfo->dobj.components |= DUMP_COMPONENT_DATA; + + /* Mark whether LO group has a non-empty ACL */ + if (!PQgetisnull(res, i, i_lomacl)) + loinfo->dobj.components |= DUMP_COMPONENT_ACL; + + /* + * In binary upgrade mode, pg_largeobject and pg_largeobject_metadata + * are transferred via COPY or by copying/linking the files from the + * old cluster. Thus, we do not need to dump LO data, definitions, or + * ACLs. + */ + if (dopt->binary_upgrade) + loinfo->dobj.dump &= ~(DUMP_COMPONENT_DATA | DUMP_COMPONENT_ACL | DUMP_COMPONENT_DEFINITION); + + /* + * Create a "BLOBS" data item for the group, too. This is just a + * placeholder for sorting; it carries no data now. + */ + lodata = pg_malloc_object(DumpableObject); + lodata->objType = DO_LARGE_OBJECT_DATA; + lodata->catId = nilCatalogId; + AssignDumpId(lodata); + lodata->name = pg_strdup(namebuf); + lodata->components |= DUMP_COMPONENT_DATA; + /* Set up explicit dependency from data to metadata */ + lodata->dependencies = pg_malloc_object(DumpId); + lodata->dependencies[0] = loinfo->dobj.dumpId; + lodata->nDeps = lodata->allocDeps = 1; + } + + PQclear(res); + destroyPQExpBuffer(loQry); +} + +/* + * dumpLO + * + * dump the definition (metadata) of the given large object group + */ +static void +dumpLO(Archive *fout, const LoInfo *loinfo) +{ + PQExpBuffer cquery = createPQExpBuffer(); + + /* + * The "definition" is just a newline-separated list of OIDs. We need to + * put something into the dropStmt too, but it can just be a comment. + */ + for (int i = 0; i < loinfo->numlos; i++) + appendPQExpBuffer(cquery, "%u\n", loinfo->looids[i]); + + if (loinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, loinfo->dobj.catId, loinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = loinfo->dobj.name, + .owner = loinfo->rolname, + .description = "BLOB METADATA", + .section = SECTION_DATA, + .createStmt = cquery->data, + .dropStmt = "-- dummy")); + + /* + * Dump per-blob comments and seclabels if any. We assume these are rare + * enough that it's okay to generate retail TOC entries for them. + */ + if (loinfo->dobj.dump & (DUMP_COMPONENT_COMMENT | + DUMP_COMPONENT_SECLABEL)) + { + for (int i = 0; i < loinfo->numlos; i++) + { + CatalogId catId; + char namebuf[32]; + + /* Build identifying info for this blob */ + catId.tableoid = loinfo->dobj.catId.tableoid; + catId.oid = loinfo->looids[i]; + snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[i]); + + if (loinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "LARGE OBJECT", namebuf, + NULL, loinfo->rolname, + catId, 0, loinfo->dobj.dumpId); + + if (loinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "LARGE OBJECT", namebuf, + NULL, loinfo->rolname, + catId, 0, loinfo->dobj.dumpId); + } + } + + /* + * Dump the ACLs if any (remember that all blobs in the group will have + * the same ACL). If there's just one blob, dump a simple ACL entry; if + * there's more, make a "LARGE OBJECTS" entry that really contains only + * the ACL for the first blob. _printTocEntry() will be cued by the tag + * string to emit a mutated version for each blob. + */ + if (loinfo->dobj.dump & DUMP_COMPONENT_ACL) + { + char namebuf[32]; + + /* Build identifying info for the first blob */ + snprintf(namebuf, sizeof(namebuf), "%u", loinfo->looids[0]); + + if (loinfo->numlos > 1) + { + char tagbuf[64]; + + snprintf(tagbuf, sizeof(tagbuf), "LARGE OBJECTS %u..%u", + loinfo->looids[0], loinfo->looids[loinfo->numlos - 1]); + + dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId, + "LARGE OBJECT", namebuf, NULL, NULL, + tagbuf, loinfo->rolname, &loinfo->dacl); + } + else + { + dumpACL(fout, loinfo->dobj.dumpId, InvalidDumpId, + "LARGE OBJECT", namebuf, NULL, NULL, + NULL, loinfo->rolname, &loinfo->dacl); + } + } + + destroyPQExpBuffer(cquery); +} + +/* + * dumpLOs: + * dump the data contents of the large objects in the given group + */ +static int +dumpLOs(Archive *fout, const void *arg) +{ + const LoInfo *loinfo = (const LoInfo *) arg; + PGconn *conn = GetConnection(fout); + char buf[LOBBUFSIZE]; + + pg_log_info("saving large objects \"%s\"", loinfo->dobj.name); + + for (int i = 0; i < loinfo->numlos; i++) + { + Oid loOid = loinfo->looids[i]; + int loFd; + int cnt; + + /* Open the LO */ + loFd = lo_open(conn, loOid, INV_READ); + if (loFd == -1) + pg_fatal("could not open large object %u: %s", + loOid, PQerrorMessage(conn)); + + StartLO(fout, loOid); + + /* Now read it in chunks, sending data to archive */ + do + { + cnt = lo_read(conn, loFd, buf, LOBBUFSIZE); + if (cnt < 0) + pg_fatal("error reading large object %u: %s", + loOid, PQerrorMessage(conn)); + + WriteData(fout, buf, cnt); + } while (cnt > 0); + + lo_close(conn, loFd); + + EndLO(fout, loOid); + } + + return 1; +} + +/* + * getPolicies + * get information about all RLS policies on dumpable tables. + */ +void +getPolicies(Archive *fout, TableInfo tblinfo[], int numTables) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PQExpBuffer tbloids; + PGresult *res; + PolicyInfo *polinfo; + int i_oid; + int i_tableoid; + int i_polrelid; + int i_polname; + int i_polcmd; + int i_polpermissive; + int i_polroles; + int i_polqual; + int i_polwithcheck; + int i, + j, + ntups; + + /* No policies before 9.5 */ + if (fout->remoteVersion < 90500) + return; + + /* Skip if --no-policies was specified */ + if (dopt->no_policies) + return; + + query = createPQExpBuffer(); + tbloids = createPQExpBuffer(); + + /* + * Identify tables of interest, and check which ones have RLS enabled. + */ + appendPQExpBufferChar(tbloids, '{'); + for (i = 0; i < numTables; i++) + { + TableInfo *tbinfo = &tblinfo[i]; + + /* Ignore row security on tables not to be dumped */ + if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY)) + continue; + + /* It can't have RLS or policies if it's not a table */ + if (tbinfo->relkind != RELKIND_RELATION && + tbinfo->relkind != RELKIND_PARTITIONED_TABLE) + continue; + + /* Add it to the list of table OIDs to be probed below */ + if (tbloids->len > 1) /* do we have more than the '{'? */ + appendPQExpBufferChar(tbloids, ','); + appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); + + /* Is RLS enabled? (That's separate from whether it has policies) */ + if (tbinfo->rowsec) + { + tbinfo->dobj.components |= DUMP_COMPONENT_POLICY; + + /* + * We represent RLS being enabled on a table by creating a + * PolicyInfo object with null polname. + * + * Note: use tableoid 0 so that this object won't be mistaken for + * something that pg_depend entries apply to. + */ + polinfo = pg_malloc_object(PolicyInfo); + polinfo->dobj.objType = DO_POLICY; + polinfo->dobj.catId.tableoid = 0; + polinfo->dobj.catId.oid = tbinfo->dobj.catId.oid; + AssignDumpId(&polinfo->dobj); + polinfo->dobj.namespace = tbinfo->dobj.namespace; + polinfo->dobj.name = pg_strdup(tbinfo->dobj.name); + polinfo->poltable = tbinfo; + polinfo->polname = NULL; + polinfo->polcmd = '\0'; + polinfo->polpermissive = 0; + polinfo->polroles = NULL; + polinfo->polqual = NULL; + polinfo->polwithcheck = NULL; + } + } + appendPQExpBufferChar(tbloids, '}'); + + /* + * Now, read all RLS policies belonging to the tables of interest, and + * create PolicyInfo objects for them. (Note that we must filter the + * results server-side not locally, because we dare not apply pg_get_expr + * to tables we don't have lock on.) + */ + pg_log_info("reading row-level security policies"); + + printfPQExpBuffer(query, + "SELECT pol.oid, pol.tableoid, pol.polrelid, pol.polname, pol.polcmd, "); + if (fout->remoteVersion >= 100000) + appendPQExpBufferStr(query, "pol.polpermissive, "); + else + appendPQExpBufferStr(query, "'t' as polpermissive, "); + appendPQExpBuffer(query, + "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE " + " pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, " + "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, " + "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck " + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_policy pol ON (src.tbloid = pol.polrelid)", + tbloids->data); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + if (ntups > 0) + { + i_oid = PQfnumber(res, "oid"); + i_tableoid = PQfnumber(res, "tableoid"); + i_polrelid = PQfnumber(res, "polrelid"); + i_polname = PQfnumber(res, "polname"); + i_polcmd = PQfnumber(res, "polcmd"); + i_polpermissive = PQfnumber(res, "polpermissive"); + i_polroles = PQfnumber(res, "polroles"); + i_polqual = PQfnumber(res, "polqual"); + i_polwithcheck = PQfnumber(res, "polwithcheck"); + + polinfo = pg_malloc_array(PolicyInfo, ntups); + + for (j = 0; j < ntups; j++) + { + Oid polrelid = atooid(PQgetvalue(res, j, i_polrelid)); + TableInfo *tbinfo = findTableByOid(polrelid); + + tbinfo->dobj.components |= DUMP_COMPONENT_POLICY; + + polinfo[j].dobj.objType = DO_POLICY; + polinfo[j].dobj.catId.tableoid = + atooid(PQgetvalue(res, j, i_tableoid)); + polinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); + AssignDumpId(&polinfo[j].dobj); + polinfo[j].dobj.namespace = tbinfo->dobj.namespace; + polinfo[j].poltable = tbinfo; + polinfo[j].polname = pg_strdup(PQgetvalue(res, j, i_polname)); + polinfo[j].dobj.name = pg_strdup(polinfo[j].polname); + + polinfo[j].polcmd = *(PQgetvalue(res, j, i_polcmd)); + polinfo[j].polpermissive = *(PQgetvalue(res, j, i_polpermissive)) == 't'; + + if (PQgetisnull(res, j, i_polroles)) + polinfo[j].polroles = NULL; + else + polinfo[j].polroles = pg_strdup(PQgetvalue(res, j, i_polroles)); + + if (PQgetisnull(res, j, i_polqual)) + polinfo[j].polqual = NULL; + else + polinfo[j].polqual = pg_strdup(PQgetvalue(res, j, i_polqual)); + + if (PQgetisnull(res, j, i_polwithcheck)) + polinfo[j].polwithcheck = NULL; + else + polinfo[j].polwithcheck + = pg_strdup(PQgetvalue(res, j, i_polwithcheck)); + } + } + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(tbloids); +} + +/* + * dumpPolicy + * dump the definition of the given policy + */ +static void +dumpPolicy(Archive *fout, const PolicyInfo *polinfo) +{ + DumpOptions *dopt = fout->dopt; + TableInfo *tbinfo = polinfo->poltable; + PQExpBuffer query; + PQExpBuffer delqry; + PQExpBuffer polprefix; + char *qtabname; + const char *cmd; + char *tag; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + /* + * If polname is NULL, then this record is just indicating that ROW LEVEL + * SECURITY is enabled for the table. Dump as ALTER TABLE ENABLE + * ROW LEVEL SECURITY. + */ + if (polinfo->polname == NULL) + { + query = createPQExpBuffer(); + + appendPQExpBuffer(query, "ALTER TABLE %s ENABLE ROW LEVEL SECURITY;", + fmtQualifiedDumpable(tbinfo)); + + /* + * We must emit the ROW SECURITY object's dependency on its table + * explicitly, because it will not match anything in pg_depend (unlike + * the case for other PolicyInfo objects). + */ + if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = polinfo->dobj.name, + .namespace = polinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "ROW SECURITY", + .section = SECTION_POST_DATA, + .createStmt = query->data, + .deps = &(tbinfo->dobj.dumpId), + .nDeps = 1)); + + destroyPQExpBuffer(query); + return; + } + + if (polinfo->polcmd == '*') + cmd = ""; + else if (polinfo->polcmd == 'r') + cmd = " FOR SELECT"; + else if (polinfo->polcmd == 'a') + cmd = " FOR INSERT"; + else if (polinfo->polcmd == 'w') + cmd = " FOR UPDATE"; + else if (polinfo->polcmd == 'd') + cmd = " FOR DELETE"; + else + pg_fatal("unexpected policy command type: %c", + polinfo->polcmd); + + query = createPQExpBuffer(); + delqry = createPQExpBuffer(); + polprefix = createPQExpBuffer(); + + qtabname = pg_strdup(fmtId(tbinfo->dobj.name)); + + appendPQExpBuffer(query, "CREATE POLICY %s", fmtId(polinfo->polname)); + + appendPQExpBuffer(query, " ON %s%s%s", fmtQualifiedDumpable(tbinfo), + !polinfo->polpermissive ? " AS RESTRICTIVE" : "", cmd); + + if (polinfo->polroles != NULL) + appendPQExpBuffer(query, " TO %s", polinfo->polroles); + + if (polinfo->polqual != NULL) + appendPQExpBuffer(query, " USING (%s)", polinfo->polqual); + + if (polinfo->polwithcheck != NULL) + appendPQExpBuffer(query, " WITH CHECK (%s)", polinfo->polwithcheck); + + appendPQExpBufferStr(query, ";\n"); + + appendPQExpBuffer(delqry, "DROP POLICY %s", fmtId(polinfo->polname)); + appendPQExpBuffer(delqry, " ON %s;\n", fmtQualifiedDumpable(tbinfo)); + + appendPQExpBuffer(polprefix, "POLICY %s ON", + fmtId(polinfo->polname)); + + tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name); + + if (polinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = polinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "POLICY", + .section = SECTION_POST_DATA, + .createStmt = query->data, + .dropStmt = delqry->data)); + + if (polinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, polprefix->data, qtabname, + tbinfo->dobj.namespace->dobj.name, tbinfo->rolname, + polinfo->dobj.catId, 0, polinfo->dobj.dumpId); + + free(tag); + destroyPQExpBuffer(query); + destroyPQExpBuffer(delqry); + destroyPQExpBuffer(polprefix); + free(qtabname); +} + +/* + * getPublications + * get information about publications + */ +void +getPublications(Archive *fout) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PGresult *res; + PublicationInfo *pubinfo; + int i_tableoid; + int i_oid; + int i_pubname; + int i_pubowner; + int i_puballtables; + int i_puballsequences; + int i_pubinsert; + int i_pubupdate; + int i_pubdelete; + int i_pubtruncate; + int i_pubviaroot; + int i_pubgencols; + int i, + ntups; + + if (dopt->no_publications || fout->remoteVersion < 100000) + return; + + query = createPQExpBuffer(); + + /* Get the publications. */ + appendPQExpBufferStr(query, "SELECT p.tableoid, p.oid, p.pubname, " + "p.pubowner, p.puballtables, p.pubinsert, " + "p.pubupdate, p.pubdelete, "); + + if (fout->remoteVersion >= 110000) + appendPQExpBufferStr(query, "p.pubtruncate, "); + else + appendPQExpBufferStr(query, "false AS pubtruncate, "); + + if (fout->remoteVersion >= 130000) + appendPQExpBufferStr(query, "p.pubviaroot, "); + else + appendPQExpBufferStr(query, "false AS pubviaroot, "); + + if (fout->remoteVersion >= 180000) + appendPQExpBufferStr(query, "p.pubgencols, "); + else + appendPQExpBuffer(query, "'%c' AS pubgencols, ", PUBLISH_GENCOLS_NONE); + + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, "p.puballsequences "); + else + appendPQExpBufferStr(query, "false AS puballsequences "); + + appendPQExpBufferStr(query, "FROM pg_publication p"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + if (ntups == 0) + goto cleanup; + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_pubname = PQfnumber(res, "pubname"); + i_pubowner = PQfnumber(res, "pubowner"); + i_puballtables = PQfnumber(res, "puballtables"); + i_puballsequences = PQfnumber(res, "puballsequences"); + i_pubinsert = PQfnumber(res, "pubinsert"); + i_pubupdate = PQfnumber(res, "pubupdate"); + i_pubdelete = PQfnumber(res, "pubdelete"); + i_pubtruncate = PQfnumber(res, "pubtruncate"); + i_pubviaroot = PQfnumber(res, "pubviaroot"); + i_pubgencols = PQfnumber(res, "pubgencols"); + + pubinfo = pg_malloc_array(PublicationInfo, ntups); + + for (i = 0; i < ntups; i++) + { + pubinfo[i].dobj.objType = DO_PUBLICATION; + pubinfo[i].dobj.catId.tableoid = + atooid(PQgetvalue(res, i, i_tableoid)); + pubinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&pubinfo[i].dobj); + pubinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_pubname)); + pubinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_pubowner)); + pubinfo[i].puballtables = + (strcmp(PQgetvalue(res, i, i_puballtables), "t") == 0); + pubinfo[i].puballsequences = + (strcmp(PQgetvalue(res, i, i_puballsequences), "t") == 0); + pubinfo[i].pubinsert = + (strcmp(PQgetvalue(res, i, i_pubinsert), "t") == 0); + pubinfo[i].pubupdate = + (strcmp(PQgetvalue(res, i, i_pubupdate), "t") == 0); + pubinfo[i].pubdelete = + (strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0); + pubinfo[i].pubtruncate = + (strcmp(PQgetvalue(res, i, i_pubtruncate), "t") == 0); + pubinfo[i].pubviaroot = + (strcmp(PQgetvalue(res, i, i_pubviaroot), "t") == 0); + pubinfo[i].pubgencols_type = + *(PQgetvalue(res, i, i_pubgencols)); + pubinfo[i].except_tables = (SimplePtrList) + { + NULL, NULL + }; + + /* Decide whether we want to dump it */ + selectDumpableObject(&(pubinfo[i].dobj), fout); + + /* + * Get the list of tables for publications specified in the EXCEPT + * TABLE clause. + * + * Although individual table entries in EXCEPT list could be stored in + * PublicationRelInfo, dumpPublicationTable cannot be used to emit + * them, because there is no ALTER PUBLICATION ... ADD command to add + * individual table entries to the EXCEPT list. + * + * Therefore, the approach is to dump the complete EXCEPT list in a + * single CREATE PUBLICATION statement. PublicationInfo is used to + * collect this information, which is then emitted by + * dumpPublication(). + */ + if (fout->remoteVersion >= 190000) + { + int ntbls; + PGresult *res_tbls; + + resetPQExpBuffer(query); + appendPQExpBuffer(query, + "SELECT prrelid\n" + "FROM pg_catalog.pg_publication_rel\n" + "WHERE prpubid = %u AND prexcept", + pubinfo[i].dobj.catId.oid); + + res_tbls = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntbls = PQntuples(res_tbls); + + for (int j = 0; j < ntbls; j++) + { + Oid prrelid; + TableInfo *tbinfo; + + prrelid = atooid(PQgetvalue(res_tbls, j, 0)); + + tbinfo = findTableByOid(prrelid); + + if (tbinfo != NULL) + simple_ptr_list_append(&pubinfo[i].except_tables, tbinfo); + } + + PQclear(res_tbls); + } + } + +cleanup: + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * dumpPublication + * dump the definition of the given publication + */ +static void +dumpPublication(Archive *fout, const PublicationInfo *pubinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer delq; + PQExpBuffer query; + char *qpubname; + bool first = true; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + delq = createPQExpBuffer(); + query = createPQExpBuffer(); + + qpubname = pg_strdup(fmtId(pubinfo->dobj.name)); + + appendPQExpBuffer(delq, "DROP PUBLICATION %s;\n", + qpubname); + + appendPQExpBuffer(query, "CREATE PUBLICATION %s", + qpubname); + + if (pubinfo->puballtables) + { + int n_except = 0; + + appendPQExpBufferStr(query, " FOR ALL TABLES"); + + /* Include EXCEPT (TABLE) clause if there are except_tables. */ + for (SimplePtrListCell *cell = pubinfo->except_tables.head; cell; cell = cell->next) + { + TableInfo *tbinfo = (TableInfo *) cell->ptr; + + if (++n_except == 1) + appendPQExpBufferStr(query, " EXCEPT ("); + else + appendPQExpBufferStr(query, ", "); + appendPQExpBuffer(query, "TABLE ONLY %s", fmtQualifiedDumpable(tbinfo)); + } + if (n_except > 0) + appendPQExpBufferChar(query, ')'); + + if (pubinfo->puballsequences) + appendPQExpBufferStr(query, ", ALL SEQUENCES"); + } + else if (pubinfo->puballsequences) + appendPQExpBufferStr(query, " FOR ALL SEQUENCES"); + + appendPQExpBufferStr(query, " WITH (publish = '"); + if (pubinfo->pubinsert) + { + appendPQExpBufferStr(query, "insert"); + first = false; + } + + if (pubinfo->pubupdate) + { + if (!first) + appendPQExpBufferStr(query, ", "); + + appendPQExpBufferStr(query, "update"); + first = false; + } + + if (pubinfo->pubdelete) + { + if (!first) + appendPQExpBufferStr(query, ", "); + + appendPQExpBufferStr(query, "delete"); + first = false; + } + + if (pubinfo->pubtruncate) + { + if (!first) + appendPQExpBufferStr(query, ", "); + + appendPQExpBufferStr(query, "truncate"); + first = false; + } + + appendPQExpBufferChar(query, '\''); + + if (pubinfo->pubviaroot) + appendPQExpBufferStr(query, ", publish_via_partition_root = true"); + + if (pubinfo->pubgencols_type == PUBLISH_GENCOLS_STORED) + appendPQExpBufferStr(query, ", publish_generated_columns = stored"); + + appendPQExpBufferStr(query, ");\n"); + + if (pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = pubinfo->dobj.name, + .owner = pubinfo->rolname, + .description = "PUBLICATION", + .section = SECTION_POST_DATA, + .createStmt = query->data, + .dropStmt = delq->data)); + + if (pubinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "PUBLICATION", qpubname, + NULL, pubinfo->rolname, + pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId); + + if (pubinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "PUBLICATION", qpubname, + NULL, pubinfo->rolname, + pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId); + + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qpubname); +} + +/* + * getPublicationNamespaces + * get information about publication membership for dumpable schemas. + */ +void +getPublicationNamespaces(Archive *fout) +{ + PQExpBuffer query; + PGresult *res; + PublicationSchemaInfo *pubsinfo; + DumpOptions *dopt = fout->dopt; + int i_tableoid; + int i_oid; + int i_pnpubid; + int i_pnnspid; + int i, + j, + ntups; + + if (dopt->no_publications || fout->remoteVersion < 150000) + return; + + query = createPQExpBuffer(); + + /* Collect all publication membership info. */ + appendPQExpBufferStr(query, + "SELECT tableoid, oid, pnpubid, pnnspid " + "FROM pg_catalog.pg_publication_namespace"); + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_pnpubid = PQfnumber(res, "pnpubid"); + i_pnnspid = PQfnumber(res, "pnnspid"); + + /* this allocation may be more than we need */ + pubsinfo = pg_malloc_array(PublicationSchemaInfo, ntups); + j = 0; + + for (i = 0; i < ntups; i++) + { + Oid pnpubid = atooid(PQgetvalue(res, i, i_pnpubid)); + Oid pnnspid = atooid(PQgetvalue(res, i, i_pnnspid)); + PublicationInfo *pubinfo; + NamespaceInfo *nspinfo; + + /* + * Ignore any entries for which we aren't interested in either the + * publication or the rel. + */ + pubinfo = findPublicationByOid(pnpubid); + if (pubinfo == NULL) + continue; + nspinfo = findNamespaceByOid(pnnspid); + if (nspinfo == NULL) + continue; + + /* OK, make a DumpableObject for this relationship */ + pubsinfo[j].dobj.objType = DO_PUBLICATION_TABLE_IN_SCHEMA; + pubsinfo[j].dobj.catId.tableoid = + atooid(PQgetvalue(res, i, i_tableoid)); + pubsinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&pubsinfo[j].dobj); + pubsinfo[j].dobj.namespace = nspinfo->dobj.namespace; + pubsinfo[j].dobj.name = nspinfo->dobj.name; + pubsinfo[j].publication = pubinfo; + pubsinfo[j].pubschema = nspinfo; + + /* Decide whether we want to dump it */ + selectDumpablePublicationObject(&(pubsinfo[j].dobj), fout); + + j++; + } + + PQclear(res); + destroyPQExpBuffer(query); +} + +/* + * getPublicationTables + * get information about publication membership for dumpable tables. + */ +void +getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables) +{ + PQExpBuffer query; + PGresult *res; + PublicationRelInfo *pubrinfo; + DumpOptions *dopt = fout->dopt; + int i_tableoid; + int i_oid; + int i_prpubid; + int i_prrelid; + int i_prrelqual; + int i_prattrs; + int i, + j, + ntups; + + if (dopt->no_publications || fout->remoteVersion < 100000) + return; + + query = createPQExpBuffer(); + + /* Collect all publication membership info. */ + if (fout->remoteVersion >= 150000) + { + appendPQExpBufferStr(query, + "SELECT tableoid, oid, prpubid, prrelid, " + "pg_catalog.pg_get_expr(prqual, prrelid) AS prrelqual, " + "(CASE\n" + " WHEN pr.prattrs IS NOT NULL THEN\n" + " (SELECT array_agg(attname)\n" + " FROM\n" + " pg_catalog.generate_series(0, pg_catalog.array_upper(pr.prattrs::pg_catalog.int2[], 1)) s,\n" + " pg_catalog.pg_attribute\n" + " WHERE attrelid = pr.prrelid AND attnum = prattrs[s])\n" + " ELSE NULL END) prattrs " + "FROM pg_catalog.pg_publication_rel pr"); + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, " WHERE NOT pr.prexcept"); + } + else + appendPQExpBufferStr(query, + "SELECT tableoid, oid, prpubid, prrelid, " + "NULL AS prrelqual, NULL AS prattrs " + "FROM pg_catalog.pg_publication_rel"); + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_prpubid = PQfnumber(res, "prpubid"); + i_prrelid = PQfnumber(res, "prrelid"); + i_prrelqual = PQfnumber(res, "prrelqual"); + i_prattrs = PQfnumber(res, "prattrs"); + + /* this allocation may be more than we need */ + pubrinfo = pg_malloc_array(PublicationRelInfo, ntups); + j = 0; + + for (i = 0; i < ntups; i++) + { + Oid prpubid = atooid(PQgetvalue(res, i, i_prpubid)); + Oid prrelid = atooid(PQgetvalue(res, i, i_prrelid)); + PublicationInfo *pubinfo; + TableInfo *tbinfo; + + /* + * Ignore any entries for which we aren't interested in either the + * publication or the rel. + */ + pubinfo = findPublicationByOid(prpubid); + if (pubinfo == NULL) + continue; + tbinfo = findTableByOid(prrelid); + if (tbinfo == NULL) + continue; + + /* OK, make a DumpableObject for this relationship */ + pubrinfo[j].dobj.objType = DO_PUBLICATION_REL; + pubrinfo[j].dobj.catId.tableoid = + atooid(PQgetvalue(res, i, i_tableoid)); + pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&pubrinfo[j].dobj); + pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace; + pubrinfo[j].dobj.name = tbinfo->dobj.name; + pubrinfo[j].publication = pubinfo; + pubrinfo[j].pubtable = tbinfo; + if (PQgetisnull(res, i, i_prrelqual)) + pubrinfo[j].pubrelqual = NULL; + else + pubrinfo[j].pubrelqual = pg_strdup(PQgetvalue(res, i, i_prrelqual)); + + if (!PQgetisnull(res, i, i_prattrs)) + { + char **attnames; + int nattnames; + PQExpBuffer attribs; + + if (!parsePGArray(PQgetvalue(res, i, i_prattrs), + &attnames, &nattnames)) + pg_fatal("could not parse %s array", "prattrs"); + attribs = createPQExpBuffer(); + for (int k = 0; k < nattnames; k++) + { + if (k > 0) + appendPQExpBufferStr(attribs, ", "); + + appendPQExpBufferStr(attribs, fmtId(attnames[k])); + } + pubrinfo[j].pubrattrs = attribs->data; + free(attribs); /* but not attribs->data */ + free(attnames); + } + else + pubrinfo[j].pubrattrs = NULL; + + /* Decide whether we want to dump it */ + selectDumpablePublicationObject(&(pubrinfo[j].dobj), fout); + + j++; + } + + PQclear(res); + destroyPQExpBuffer(query); +} + +/* + * dumpPublicationNamespace + * dump the definition of the given publication schema mapping. + */ +static void +dumpPublicationNamespace(Archive *fout, const PublicationSchemaInfo *pubsinfo) +{ + DumpOptions *dopt = fout->dopt; + NamespaceInfo *schemainfo = pubsinfo->pubschema; + PublicationInfo *pubinfo = pubsinfo->publication; + PQExpBuffer query; + char *tag; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + tag = psprintf("%s %s", pubinfo->dobj.name, schemainfo->dobj.name); + + query = createPQExpBuffer(); + + appendPQExpBuffer(query, "ALTER PUBLICATION %s ", fmtId(pubinfo->dobj.name)); + appendPQExpBuffer(query, "ADD TABLES IN SCHEMA %s;\n", fmtId(schemainfo->dobj.name)); + + /* + * There is no point in creating drop query as the drop is done by schema + * drop. + */ + if (pubsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, pubsinfo->dobj.catId, pubsinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = schemainfo->dobj.name, + .owner = pubinfo->rolname, + .description = "PUBLICATION TABLES IN SCHEMA", + .section = SECTION_POST_DATA, + .createStmt = query->data)); + + /* These objects can't currently have comments or seclabels */ + + free(tag); + destroyPQExpBuffer(query); +} + +/* + * dumpPublicationTable + * dump the definition of the given publication table mapping + */ +static void +dumpPublicationTable(Archive *fout, const PublicationRelInfo *pubrinfo) +{ + DumpOptions *dopt = fout->dopt; + PublicationInfo *pubinfo = pubrinfo->publication; + TableInfo *tbinfo = pubrinfo->pubtable; + PQExpBuffer query; + char *tag; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + tag = psprintf("%s %s", pubinfo->dobj.name, tbinfo->dobj.name); + + query = createPQExpBuffer(); + + appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY", + fmtId(pubinfo->dobj.name)); + appendPQExpBuffer(query, " %s", + fmtQualifiedDumpable(tbinfo)); + + if (pubrinfo->pubrattrs) + appendPQExpBuffer(query, " (%s)", pubrinfo->pubrattrs); + + if (pubrinfo->pubrelqual) + { + /* + * It's necessary to add parentheses around the expression because + * pg_get_expr won't supply the parentheses for things like WHERE + * TRUE. + */ + appendPQExpBuffer(query, " WHERE (%s)", pubrinfo->pubrelqual); + } + appendPQExpBufferStr(query, ";\n"); + + /* + * There is no point in creating a drop query as the drop is done by table + * drop. (If you think to change this, see also _printTocEntry().) + * Although this object doesn't really have ownership as such, set the + * owner field anyway to ensure that the command is run by the correct + * role at restore time. + */ + if (pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = pubinfo->rolname, + .description = "PUBLICATION TABLE", + .section = SECTION_POST_DATA, + .createStmt = query->data)); + + /* These objects can't currently have comments or seclabels */ + + free(tag); + destroyPQExpBuffer(query); +} + +/* + * Is the currently connected user a superuser? + */ +static bool +is_superuser(Archive *fout) +{ + ArchiveHandle *AH = (ArchiveHandle *) fout; + const char *val; + + val = PQparameterStatus(AH->connection, "is_superuser"); + + if (val && strcmp(val, "on") == 0) + return true; + + return false; +} + +/* + * Set the given value to restrict_nonsystem_relation_kind value. Since + * restrict_nonsystem_relation_kind is introduced in minor version releases, + * the setting query is effective only where available. + */ +static void +set_restrict_relation_kind(Archive *AH, const char *value) +{ + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + + appendPQExpBuffer(query, + "SELECT set_config(name, '%s', false) " + "FROM pg_settings " + "WHERE name = 'restrict_nonsystem_relation_kind'", + value); + res = ExecuteSqlQuery(AH, query->data, PGRES_TUPLES_OK); + + PQclear(res); + destroyPQExpBuffer(query); +} + +/* + * getSubscriptions + * get information about subscriptions + */ +void +getSubscriptions(Archive *fout) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PGresult *res; + SubscriptionInfo *subinfo; + int i_tableoid; + int i_oid; + int i_subname; + int i_subowner; + int i_subbinary; + int i_substream; + int i_subtwophasestate; + int i_subdisableonerr; + int i_subpasswordrequired; + int i_subrunasowner; + int i_subservername; + int i_subconninfo; + int i_subslotname; + int i_subsynccommit; + int i_subwalrcvtimeout; + int i_subpublications; + int i_suborigin; + int i_suboriginremotelsn; + int i_subenabled; + int i_subfailover; + int i_subretaindeadtuples; + int i_submaxretention; + int i, + ntups; + + if (dopt->no_subscriptions || fout->remoteVersion < 100000) + return; + + if (!is_superuser(fout)) + { + int n; + + res = ExecuteSqlQuery(fout, + "SELECT count(*) FROM pg_subscription " + "WHERE subdbid = (SELECT oid FROM pg_database" + " WHERE datname = current_database())", + PGRES_TUPLES_OK); + n = atoi(PQgetvalue(res, 0, 0)); + if (n > 0) + pg_log_warning("subscriptions not dumped because current user is not a superuser"); + PQclear(res); + return; + } + + query = createPQExpBuffer(); + + /* Get the subscriptions in current database. */ + appendPQExpBufferStr(query, + "SELECT s.tableoid, s.oid, s.subname,\n" + " s.subowner,\n" + " s.subconninfo, s.subslotname, s.subsynccommit,\n" + " s.subpublications,\n"); + + if (fout->remoteVersion >= 140000) + appendPQExpBufferStr(query, " s.subbinary,\n"); + else + appendPQExpBufferStr(query, " false AS subbinary,\n"); + + if (fout->remoteVersion >= 140000) + appendPQExpBufferStr(query, " s.substream,\n"); + else + appendPQExpBufferStr(query, " 'f' AS substream,\n"); + + if (fout->remoteVersion >= 150000) + appendPQExpBufferStr(query, + " s.subtwophasestate,\n" + " s.subdisableonerr,\n"); + else + appendPQExpBuffer(query, + " '%c' AS subtwophasestate,\n" + " false AS subdisableonerr,\n", + LOGICALREP_TWOPHASE_STATE_DISABLED); + + if (fout->remoteVersion >= 160000) + appendPQExpBufferStr(query, + " s.subpasswordrequired,\n" + " s.subrunasowner,\n" + " s.suborigin,\n"); + else + appendPQExpBuffer(query, + " 't' AS subpasswordrequired,\n" + " 't' AS subrunasowner,\n" + " '%s' AS suborigin,\n", + LOGICALREP_ORIGIN_ANY); + + if (dopt->binary_upgrade && fout->remoteVersion >= 170000) + appendPQExpBufferStr(query, " o.remote_lsn AS suboriginremotelsn,\n" + " s.subenabled,\n"); + else + appendPQExpBufferStr(query, " NULL AS suboriginremotelsn,\n" + " false AS subenabled,\n"); + + if (fout->remoteVersion >= 170000) + appendPQExpBufferStr(query, + " s.subfailover,\n"); + else + appendPQExpBufferStr(query, + " false AS subfailover,\n"); + + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, + " s.subretaindeadtuples,\n"); + else + appendPQExpBufferStr(query, + " false AS subretaindeadtuples,\n"); + + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, + " s.submaxretention,\n"); + else + appendPQExpBufferStr(query, " 0 AS submaxretention,\n"); + + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, + " s.subwalrcvtimeout,\n"); + else + appendPQExpBufferStr(query, + " '-1' AS subwalrcvtimeout,\n"); + + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, " fs.srvname AS subservername\n"); + else + appendPQExpBufferStr(query, " NULL AS subservername\n"); + + appendPQExpBufferStr(query, + "FROM pg_subscription s\n"); + + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, + "LEFT JOIN pg_catalog.pg_foreign_server fs \n" + " ON fs.oid = s.subserver \n"); + + if (dopt->binary_upgrade && fout->remoteVersion >= 170000) + appendPQExpBufferStr(query, + "LEFT JOIN pg_catalog.pg_replication_origin_status o \n" + " ON o.external_id = 'pg_' || s.oid::text \n"); + + appendPQExpBufferStr(query, + "WHERE s.subdbid = (SELECT oid FROM pg_database\n" + " WHERE datname = current_database())"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + /* + * Get subscription fields. We don't include subskiplsn in the dump as + * after restoring the dump this value may no longer be relevant. + */ + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_subname = PQfnumber(res, "subname"); + i_subowner = PQfnumber(res, "subowner"); + i_subenabled = PQfnumber(res, "subenabled"); + i_subbinary = PQfnumber(res, "subbinary"); + i_substream = PQfnumber(res, "substream"); + i_subtwophasestate = PQfnumber(res, "subtwophasestate"); + i_subdisableonerr = PQfnumber(res, "subdisableonerr"); + i_subpasswordrequired = PQfnumber(res, "subpasswordrequired"); + i_subrunasowner = PQfnumber(res, "subrunasowner"); + i_subfailover = PQfnumber(res, "subfailover"); + i_subretaindeadtuples = PQfnumber(res, "subretaindeadtuples"); + i_submaxretention = PQfnumber(res, "submaxretention"); + i_subservername = PQfnumber(res, "subservername"); + i_subconninfo = PQfnumber(res, "subconninfo"); + i_subslotname = PQfnumber(res, "subslotname"); + i_subsynccommit = PQfnumber(res, "subsynccommit"); + i_subwalrcvtimeout = PQfnumber(res, "subwalrcvtimeout"); + i_subpublications = PQfnumber(res, "subpublications"); + i_suborigin = PQfnumber(res, "suborigin"); + i_suboriginremotelsn = PQfnumber(res, "suboriginremotelsn"); + + subinfo = pg_malloc_array(SubscriptionInfo, ntups); + + for (i = 0; i < ntups; i++) + { + subinfo[i].dobj.objType = DO_SUBSCRIPTION; + subinfo[i].dobj.catId.tableoid = + atooid(PQgetvalue(res, i, i_tableoid)); + subinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&subinfo[i].dobj); + subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname)); + subinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_subowner)); + + subinfo[i].subenabled = + (strcmp(PQgetvalue(res, i, i_subenabled), "t") == 0); + if (PQgetisnull(res, i, i_subservername)) + subinfo[i].subservername = NULL; + else + subinfo[i].subservername = pg_strdup(PQgetvalue(res, i, i_subservername)); + subinfo[i].subbinary = + (strcmp(PQgetvalue(res, i, i_subbinary), "t") == 0); + subinfo[i].substream = *(PQgetvalue(res, i, i_substream)); + subinfo[i].subtwophasestate = *(PQgetvalue(res, i, i_subtwophasestate)); + subinfo[i].subdisableonerr = + (strcmp(PQgetvalue(res, i, i_subdisableonerr), "t") == 0); + subinfo[i].subpasswordrequired = + (strcmp(PQgetvalue(res, i, i_subpasswordrequired), "t") == 0); + subinfo[i].subrunasowner = + (strcmp(PQgetvalue(res, i, i_subrunasowner), "t") == 0); + subinfo[i].subfailover = + (strcmp(PQgetvalue(res, i, i_subfailover), "t") == 0); + subinfo[i].subretaindeadtuples = + (strcmp(PQgetvalue(res, i, i_subretaindeadtuples), "t") == 0); + subinfo[i].submaxretention = + atoi(PQgetvalue(res, i, i_submaxretention)); + if (PQgetisnull(res, i, i_subconninfo)) + subinfo[i].subconninfo = NULL; + else + subinfo[i].subconninfo = + pg_strdup(PQgetvalue(res, i, i_subconninfo)); + if (PQgetisnull(res, i, i_subslotname)) + subinfo[i].subslotname = NULL; + else + subinfo[i].subslotname = + pg_strdup(PQgetvalue(res, i, i_subslotname)); + subinfo[i].subsynccommit = + pg_strdup(PQgetvalue(res, i, i_subsynccommit)); + subinfo[i].subwalrcvtimeout = + pg_strdup(PQgetvalue(res, i, i_subwalrcvtimeout)); + subinfo[i].subpublications = + pg_strdup(PQgetvalue(res, i, i_subpublications)); + subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, i_suborigin)); + if (PQgetisnull(res, i, i_suboriginremotelsn)) + subinfo[i].suboriginremotelsn = NULL; + else + subinfo[i].suboriginremotelsn = + pg_strdup(PQgetvalue(res, i, i_suboriginremotelsn)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(subinfo[i].dobj), fout); + } + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getSubscriptionRelations + * Get information about subscription membership for dumpable relations. This + * will be used only in binary-upgrade mode for PG17 or later versions. + */ +void +getSubscriptionRelations(Archive *fout) +{ + DumpOptions *dopt = fout->dopt; + SubscriptionInfo *subinfo = NULL; + SubRelInfo *subrinfo; + PGresult *res; + int i_srsubid; + int i_srrelid; + int i_srsubstate; + int i_srsublsn; + int ntups; + Oid last_srsubid = InvalidOid; + + if (dopt->no_subscriptions || !dopt->binary_upgrade || + fout->remoteVersion < 170000) + return; + + res = ExecuteSqlQuery(fout, + "SELECT srsubid, srrelid, srsubstate, srsublsn " + "FROM pg_catalog.pg_subscription_rel " + "ORDER BY srsubid", + PGRES_TUPLES_OK); + ntups = PQntuples(res); + if (ntups == 0) + goto cleanup; + + /* Get pg_subscription_rel attributes */ + i_srsubid = PQfnumber(res, "srsubid"); + i_srrelid = PQfnumber(res, "srrelid"); + i_srsubstate = PQfnumber(res, "srsubstate"); + i_srsublsn = PQfnumber(res, "srsublsn"); + + subrinfo = pg_malloc_array(SubRelInfo, ntups); + for (int i = 0; i < ntups; i++) + { + Oid cur_srsubid = atooid(PQgetvalue(res, i, i_srsubid)); + Oid relid = atooid(PQgetvalue(res, i, i_srrelid)); + TableInfo *tblinfo; + + /* + * If we switched to a new subscription, check if the subscription + * exists. + */ + if (cur_srsubid != last_srsubid) + { + subinfo = findSubscriptionByOid(cur_srsubid); + if (subinfo == NULL) + pg_fatal("subscription with OID %u does not exist", cur_srsubid); + + last_srsubid = cur_srsubid; + } + + tblinfo = findTableByOid(relid); + if (tblinfo == NULL) + pg_fatal("failed sanity check, relation with OID %u not found", + relid); + + /* OK, make a DumpableObject for this relationship */ + subrinfo[i].dobj.objType = DO_SUBSCRIPTION_REL; + subrinfo[i].dobj.catId.tableoid = relid; + subrinfo[i].dobj.catId.oid = cur_srsubid; + AssignDumpId(&subrinfo[i].dobj); + subrinfo[i].dobj.namespace = tblinfo->dobj.namespace; + subrinfo[i].dobj.name = tblinfo->dobj.name; + subrinfo[i].subinfo = subinfo; + subrinfo[i].tblinfo = tblinfo; + subrinfo[i].srsubstate = PQgetvalue(res, i, i_srsubstate)[0]; + if (PQgetisnull(res, i, i_srsublsn)) + subrinfo[i].srsublsn = NULL; + else + subrinfo[i].srsublsn = pg_strdup(PQgetvalue(res, i, i_srsublsn)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(subrinfo[i].dobj), fout); + } + +cleanup: + PQclear(res); +} + +/* + * dumpSubscriptionTable + * Dump the definition of the given subscription table mapping. This will be + * used only in binary-upgrade mode for PG17 or later versions. + */ +static void +dumpSubscriptionTable(Archive *fout, const SubRelInfo *subrinfo) +{ + DumpOptions *dopt = fout->dopt; + SubscriptionInfo *subinfo = subrinfo->subinfo; + PQExpBuffer query; + char *tag; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + Assert(fout->dopt->binary_upgrade && fout->remoteVersion >= 170000); + + tag = psprintf("%s %s", subinfo->dobj.name, subrinfo->tblinfo->dobj.name); + + query = createPQExpBuffer(); + + if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + { + /* + * binary_upgrade_add_sub_rel_state will add the subscription relation + * to pg_subscription_rel table. This will be used only in + * binary-upgrade mode. + */ + appendPQExpBufferStr(query, + "\n-- For binary upgrade, must preserve the subscriber table.\n"); + appendPQExpBufferStr(query, + "SELECT pg_catalog.binary_upgrade_add_sub_rel_state("); + appendStringLiteralAH(query, subinfo->dobj.name, fout); + appendPQExpBuffer(query, + ", %u, '%c'", + subrinfo->tblinfo->dobj.catId.oid, + subrinfo->srsubstate); + + if (subrinfo->srsublsn && subrinfo->srsublsn[0] != '\0') + appendPQExpBuffer(query, ", '%s'", subrinfo->srsublsn); + else + appendPQExpBufferStr(query, ", NULL"); + + appendPQExpBufferStr(query, ");\n"); + } + + /* + * There is no point in creating a drop query as the drop is done by table + * drop. (If you think to change this, see also _printTocEntry().) + * Although this object doesn't really have ownership as such, set the + * owner field anyway to ensure that the command is run by the correct + * role at restore time. + */ + if (subrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, subrinfo->dobj.catId, subrinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = subrinfo->tblinfo->dobj.namespace->dobj.name, + .owner = subinfo->rolname, + .description = "SUBSCRIPTION TABLE", + .section = SECTION_POST_DATA, + .createStmt = query->data)); + + /* These objects can't currently have comments or seclabels */ + + free(tag); + destroyPQExpBuffer(query); +} + +/* + * dumpSubscription + * dump the definition of the given subscription + */ +static void +dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer delq; + PQExpBuffer query; + PQExpBuffer publications; + char *qsubname; + char **pubnames = NULL; + int npubnames = 0; + int i; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + delq = createPQExpBuffer(); + query = createPQExpBuffer(); + + qsubname = pg_strdup(fmtId(subinfo->dobj.name)); + + appendPQExpBuffer(delq, "DROP SUBSCRIPTION %s;\n", + qsubname); + + appendPQExpBuffer(query, "CREATE SUBSCRIPTION %s ", + qsubname); + if (subinfo->subservername) + { + appendPQExpBuffer(query, "SERVER %s", fmtId(subinfo->subservername)); + } + else + { + appendPQExpBufferStr(query, "CONNECTION "); + appendStringLiteralAH(query, subinfo->subconninfo, fout); + } + + /* Build list of quoted publications and append them to query. */ + if (!parsePGArray(subinfo->subpublications, &pubnames, &npubnames)) + pg_fatal("could not parse %s array", "subpublications"); + + publications = createPQExpBuffer(); + for (i = 0; i < npubnames; i++) + { + if (i > 0) + appendPQExpBufferStr(publications, ", "); + + appendPQExpBufferStr(publications, fmtId(pubnames[i])); + } + + appendPQExpBuffer(query, " PUBLICATION %s WITH (connect = false, slot_name = ", publications->data); + if (subinfo->subslotname) + appendStringLiteralAH(query, subinfo->subslotname, fout); + else + appendPQExpBufferStr(query, "NONE"); + + if (subinfo->subbinary) + appendPQExpBufferStr(query, ", binary = true"); + + if (subinfo->substream == LOGICALREP_STREAM_ON) + appendPQExpBufferStr(query, ", streaming = on"); + else if (subinfo->substream == LOGICALREP_STREAM_PARALLEL) + appendPQExpBufferStr(query, ", streaming = parallel"); + else + appendPQExpBufferStr(query, ", streaming = off"); + + if (subinfo->subtwophasestate != LOGICALREP_TWOPHASE_STATE_DISABLED) + appendPQExpBufferStr(query, ", two_phase = on"); + + if (subinfo->subdisableonerr) + appendPQExpBufferStr(query, ", disable_on_error = true"); + + if (!subinfo->subpasswordrequired) + appendPQExpBufferStr(query, ", password_required = false"); + + if (subinfo->subrunasowner) + appendPQExpBufferStr(query, ", run_as_owner = true"); + + if (subinfo->subfailover) + appendPQExpBufferStr(query, ", failover = true"); + + if (subinfo->subretaindeadtuples) + appendPQExpBufferStr(query, ", retain_dead_tuples = true"); + + if (subinfo->submaxretention) + appendPQExpBuffer(query, ", max_retention_duration = %d", subinfo->submaxretention); + + if (strcmp(subinfo->subsynccommit, "off") != 0) + appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit)); + + if (strcmp(subinfo->subwalrcvtimeout, "-1") != 0) + appendPQExpBuffer(query, ", wal_receiver_timeout = %s", fmtId(subinfo->subwalrcvtimeout)); + + if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0) + appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin); + + appendPQExpBufferStr(query, ");\n"); + + /* + * In binary-upgrade mode, we allow the replication to continue after the + * upgrade. + */ + if (dopt->binary_upgrade && fout->remoteVersion >= 170000) + { + if (subinfo->suboriginremotelsn) + { + /* + * Preserve the remote_lsn for the subscriber's replication + * origin. This value is required to start the replication from + * the position before the upgrade. This value will be stale if + * the publisher gets upgraded before the subscriber node. + * However, this shouldn't be a problem as the upgrade of the + * publisher ensures that all the transactions were replicated + * before upgrading it. + */ + appendPQExpBufferStr(query, + "\n-- For binary upgrade, must preserve the remote_lsn for the subscriber's replication origin.\n"); + appendPQExpBufferStr(query, + "SELECT pg_catalog.binary_upgrade_replorigin_advance("); + appendStringLiteralAH(query, subinfo->dobj.name, fout); + appendPQExpBuffer(query, ", '%s');\n", subinfo->suboriginremotelsn); + } + + if (subinfo->subenabled) + { + /* + * Enable the subscription to allow the replication to continue + * after the upgrade. + */ + appendPQExpBufferStr(query, + "\n-- For binary upgrade, must preserve the subscriber's running state.\n"); + appendPQExpBuffer(query, "ALTER SUBSCRIPTION %s ENABLE;\n", qsubname); + } + } + + if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = subinfo->dobj.name, + .owner = subinfo->rolname, + .description = "SUBSCRIPTION", + .section = SECTION_POST_DATA, + .createStmt = query->data, + .dropStmt = delq->data)); + + if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "SUBSCRIPTION", qsubname, + NULL, subinfo->rolname, + subinfo->dobj.catId, 0, subinfo->dobj.dumpId); + + if (subinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "SUBSCRIPTION", qsubname, + NULL, subinfo->rolname, + subinfo->dobj.catId, 0, subinfo->dobj.dumpId); + + destroyPQExpBuffer(publications); + free(pubnames); + + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qsubname); +} + +/* + * Given a "create query", append as many ALTER ... DEPENDS ON EXTENSION as + * the object needs. + */ +static void +append_depends_on_extension(Archive *fout, + PQExpBuffer create, + const DumpableObject *dobj, + const char *catalog, + const char *keyword, + const char *objname) +{ + if (dobj->depends_on_ext) + { + char *nm; + PGresult *res; + PQExpBuffer query; + int ntups; + int i_extname; + int i; + + /* dodge fmtId() non-reentrancy */ + nm = pg_strdup(objname); + + query = createPQExpBuffer(); + appendPQExpBuffer(query, + "SELECT e.extname " + "FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e " + "WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass " + "AND objid = '%u'::pg_catalog.oid AND deptype = 'x' " + "AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass", + catalog, + dobj->catId.oid); + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + ntups = PQntuples(res); + i_extname = PQfnumber(res, "extname"); + for (i = 0; i < ntups; i++) + { + appendPQExpBuffer(create, "\nALTER %s %s DEPENDS ON EXTENSION %s;", + keyword, nm, + fmtId(PQgetvalue(res, i, i_extname))); + } + + PQclear(res); + destroyPQExpBuffer(query); + pg_free(nm); + } +} + +static Oid +get_next_possible_free_pg_type_oid(Archive *fout, PQExpBuffer upgrade_query) +{ + /* + * If the old version didn't assign an array type, but the new version + * does, we must select an unused type OID to assign. This currently only + * happens for domains, when upgrading pre-v11 to v11 and up. + * + * Note: local state here is kind of ugly, but we must have some, since we + * mustn't choose the same unused OID more than once. + */ + static Oid next_possible_free_oid = FirstNormalObjectId; + PGresult *res; + bool is_dup; + + do + { + ++next_possible_free_oid; + printfPQExpBuffer(upgrade_query, + "SELECT EXISTS(SELECT 1 " + "FROM pg_catalog.pg_type " + "WHERE oid = '%u'::pg_catalog.oid);", + next_possible_free_oid); + res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data); + is_dup = (PQgetvalue(res, 0, 0)[0] == 't'); + PQclear(res); + } while (is_dup); + + return next_possible_free_oid; +} + +static void +binary_upgrade_set_type_oids_by_type_oid(Archive *fout, + PQExpBuffer upgrade_buffer, + Oid pg_type_oid, + bool force_array_type, + bool include_multirange_type) +{ + PQExpBuffer upgrade_query = createPQExpBuffer(); + PGresult *res; + Oid pg_type_array_oid; + Oid pg_type_multirange_oid; + Oid pg_type_multirange_array_oid; + TypeInfo *tinfo; + + appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n"); + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n", + pg_type_oid); + + tinfo = findTypeByOid(pg_type_oid); + if (tinfo) + pg_type_array_oid = tinfo->typarray; + else + pg_type_array_oid = InvalidOid; + + if (!OidIsValid(pg_type_array_oid) && force_array_type) + pg_type_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query); + + if (OidIsValid(pg_type_array_oid)) + { + appendPQExpBufferStr(upgrade_buffer, + "\n-- For binary upgrade, must preserve pg_type array oid\n"); + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n", + pg_type_array_oid); + } + + /* + * Pre-set the multirange type oid and its own array type oid. + */ + if (include_multirange_type) + { + if (fout->remoteVersion >= 140000) + { + printfPQExpBuffer(upgrade_query, + "SELECT t.oid, t.typarray " + "FROM pg_catalog.pg_type t " + "JOIN pg_catalog.pg_range r " + "ON t.oid = r.rngmultitypid " + "WHERE r.rngtypid = '%u'::pg_catalog.oid;", + pg_type_oid); + + res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data); + + pg_type_multirange_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid"))); + pg_type_multirange_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray"))); + + PQclear(res); + } + else + { + pg_type_multirange_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query); + pg_type_multirange_array_oid = get_next_possible_free_pg_type_oid(fout, upgrade_query); + } + + appendPQExpBufferStr(upgrade_buffer, + "\n-- For binary upgrade, must preserve multirange pg_type oid\n"); + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_multirange_pg_type_oid('%u'::pg_catalog.oid);\n\n", + pg_type_multirange_oid); + appendPQExpBufferStr(upgrade_buffer, + "\n-- For binary upgrade, must preserve multirange pg_type array oid\n"); + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_multirange_array_pg_type_oid('%u'::pg_catalog.oid);\n\n", + pg_type_multirange_array_oid); + } + + destroyPQExpBuffer(upgrade_query); +} + +static void +binary_upgrade_set_type_oids_by_rel(Archive *fout, + PQExpBuffer upgrade_buffer, + const TableInfo *tbinfo) +{ + Oid pg_type_oid = tbinfo->reltype; + + if (OidIsValid(pg_type_oid)) + binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer, + pg_type_oid, false, false); +} + +/* + * bsearch() comparator for BinaryUpgradeClassOidItem + */ +static int +BinaryUpgradeClassOidItemCmp(const void *p1, const void *p2) +{ + BinaryUpgradeClassOidItem v1 = *((const BinaryUpgradeClassOidItem *) p1); + BinaryUpgradeClassOidItem v2 = *((const BinaryUpgradeClassOidItem *) p2); + + return pg_cmp_u32(v1.oid, v2.oid); +} + +/* + * collectBinaryUpgradeClassOids + * + * Construct a table of pg_class information required for + * binary_upgrade_set_pg_class_oids(). The table is sorted by OID for speed in + * lookup. + */ +static void +collectBinaryUpgradeClassOids(Archive *fout) +{ + PGresult *res; + const char *query; + + query = "SELECT c.oid, c.relkind, c.relfilenode, c.reltoastrelid, " + "ct.relfilenode, i.indexrelid, cti.relfilenode " + "FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index i " + "ON (c.reltoastrelid = i.indrelid AND i.indisvalid) " + "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) " + "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) " + "ORDER BY c.oid;"; + + res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK); + + nbinaryUpgradeClassOids = PQntuples(res); + binaryUpgradeClassOids = + pg_malloc_array(BinaryUpgradeClassOidItem, nbinaryUpgradeClassOids); + + for (int i = 0; i < nbinaryUpgradeClassOids; i++) + { + binaryUpgradeClassOids[i].oid = atooid(PQgetvalue(res, i, 0)); + binaryUpgradeClassOids[i].relkind = *PQgetvalue(res, i, 1); + binaryUpgradeClassOids[i].relfilenumber = atooid(PQgetvalue(res, i, 2)); + binaryUpgradeClassOids[i].toast_oid = atooid(PQgetvalue(res, i, 3)); + binaryUpgradeClassOids[i].toast_relfilenumber = atooid(PQgetvalue(res, i, 4)); + binaryUpgradeClassOids[i].toast_index_oid = atooid(PQgetvalue(res, i, 5)); + binaryUpgradeClassOids[i].toast_index_relfilenumber = atooid(PQgetvalue(res, i, 6)); + } + + PQclear(res); +} + +static void +binary_upgrade_set_pg_class_oids(Archive *fout, + PQExpBuffer upgrade_buffer, Oid pg_class_oid) +{ + BinaryUpgradeClassOidItem key = {0}; + BinaryUpgradeClassOidItem *entry; + + Assert(binaryUpgradeClassOids); + + /* + * Preserve the OID and relfilenumber of the table, table's index, table's + * toast table and toast table's index if any. + * + * One complexity is that the current table definition might not require + * the creation of a TOAST table, but the old database might have a TOAST + * table that was created earlier, before some wide columns were dropped. + * By setting the TOAST oid we force creation of the TOAST heap and index + * by the new backend, so we can copy the files during binary upgrade + * without worrying about this case. + */ + key.oid = pg_class_oid; + entry = bsearch(&key, binaryUpgradeClassOids, nbinaryUpgradeClassOids, + sizeof(BinaryUpgradeClassOidItem), + BinaryUpgradeClassOidItemCmp); + + appendPQExpBufferStr(upgrade_buffer, + "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n"); + + if (entry->relkind != RELKIND_INDEX && + entry->relkind != RELKIND_PARTITIONED_INDEX) + { + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n", + pg_class_oid); + + /* + * Not every relation has storage. Also, in a pre-v12 database, + * partitioned tables have a relfilenumber, which should not be + * preserved when upgrading. + */ + if (RelFileNumberIsValid(entry->relfilenumber) && + entry->relkind != RELKIND_PARTITIONED_TABLE) + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n", + entry->relfilenumber); + + /* + * In a pre-v12 database, partitioned tables might be marked as having + * toast tables, but we should ignore them if so. + */ + if (OidIsValid(entry->toast_oid) && + entry->relkind != RELKIND_PARTITIONED_TABLE) + { + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n", + entry->toast_oid); + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n", + entry->toast_relfilenumber); + + /* every toast table has an index */ + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n", + entry->toast_index_oid); + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n", + entry->toast_index_relfilenumber); + } + } + else + { + /* Preserve the OID and relfilenumber of the index */ + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n", + pg_class_oid); + appendPQExpBuffer(upgrade_buffer, + "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n", + entry->relfilenumber); + } + + appendPQExpBufferChar(upgrade_buffer, '\n'); +} + +/* + * If the DumpableObject is a member of an extension, add a suitable + * ALTER EXTENSION ADD command to the creation commands in upgrade_buffer. + * + * For somewhat historical reasons, objname should already be quoted, + * but not objnamespace (if any). + */ +static void +binary_upgrade_extension_member(PQExpBuffer upgrade_buffer, + const DumpableObject *dobj, + const char *objtype, + const char *objname, + const char *objnamespace) +{ + DumpableObject *extobj = NULL; + int i; + + if (!dobj->ext_member) + return; + + /* + * Find the parent extension. We could avoid this search if we wanted to + * add a link field to DumpableObject, but the space costs of that would + * be considerable. We assume that member objects could only have a + * direct dependency on their own extension, not any others. + */ + for (i = 0; i < dobj->nDeps; i++) + { + extobj = findObjectByDumpId(dobj->dependencies[i]); + if (extobj && extobj->objType == DO_EXTENSION) + break; + extobj = NULL; + } + if (extobj == NULL) + pg_fatal("could not find parent extension for %s %s", + objtype, objname); + + appendPQExpBufferStr(upgrade_buffer, + "\n-- For binary upgrade, handle extension membership the hard way\n"); + appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ", + fmtId(extobj->name), + objtype); + if (objnamespace && *objnamespace) + appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace)); + appendPQExpBuffer(upgrade_buffer, "%s;\n", objname); +} + +/* + * getNamespaces: + * get information about all namespaces in the system catalogs + */ +void +getNamespaces(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + NamespaceInfo *nsinfo; + int i_tableoid; + int i_oid; + int i_nspname; + int i_nspowner; + int i_nspacl; + int i_acldefault; + + query = createPQExpBuffer(); + + /* + * we fetch all namespaces including system ones, so that every object we + * read in can be linked to a containing namespace. + */ + appendPQExpBufferStr(query, "SELECT n.tableoid, n.oid, n.nspname, " + "n.nspowner, " + "n.nspacl, " + "acldefault('n', n.nspowner) AS acldefault " + "FROM pg_namespace n"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + nsinfo = pg_malloc_array(NamespaceInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_nspname = PQfnumber(res, "nspname"); + i_nspowner = PQfnumber(res, "nspowner"); + i_nspacl = PQfnumber(res, "nspacl"); + i_acldefault = PQfnumber(res, "acldefault"); + + for (i = 0; i < ntups; i++) + { + const char *nspowner; + + nsinfo[i].dobj.objType = DO_NAMESPACE; + nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&nsinfo[i].dobj); + nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname)); + nsinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_nspacl)); + nsinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + nsinfo[i].dacl.privtype = 0; + nsinfo[i].dacl.initprivs = NULL; + nspowner = PQgetvalue(res, i, i_nspowner); + nsinfo[i].nspowner = atooid(nspowner); + nsinfo[i].rolname = getRoleName(nspowner); + + /* Decide whether to dump this namespace */ + selectDumpableNamespace(&nsinfo[i], fout); + + /* Mark whether namespace has an ACL */ + if (!PQgetisnull(res, i, i_nspacl)) + nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL; + + /* + * We ignore any pg_init_privs.initprivs entry for the public schema + * and assume a predetermined default, for several reasons. First, + * dropping and recreating the schema removes its pg_init_privs entry, + * but an empty destination database starts with this ACL nonetheless. + * Second, we support dump/reload of public schema ownership changes. + * ALTER SCHEMA OWNER filters nspacl through aclnewowner(), but + * initprivs continues to reflect the initial owner. Hence, + * synthesize the value that nspacl will have after the restore's + * ALTER SCHEMA OWNER. Third, this makes the destination database + * match the source's ACL, even if the latter was an initdb-default + * ACL, which changed in v15. An upgrade pulls in changes to most + * system object ACLs that the DBA had not customized. We've made the + * public schema depart from that, because changing its ACL so easily + * breaks applications. + */ + if (strcmp(nsinfo[i].dobj.name, "public") == 0) + { + PQExpBuffer aclarray = createPQExpBuffer(); + PQExpBuffer aclitem = createPQExpBuffer(); + + /* Standard ACL as of v15 is {owner=UC/owner,=U/owner} */ + appendPQExpBufferChar(aclarray, '{'); + quoteAclUserName(aclitem, nsinfo[i].rolname); + appendPQExpBufferStr(aclitem, "=UC/"); + quoteAclUserName(aclitem, nsinfo[i].rolname); + appendPGArray(aclarray, aclitem->data); + resetPQExpBuffer(aclitem); + appendPQExpBufferStr(aclitem, "=U/"); + quoteAclUserName(aclitem, nsinfo[i].rolname); + appendPGArray(aclarray, aclitem->data); + appendPQExpBufferChar(aclarray, '}'); + + nsinfo[i].dacl.privtype = 'i'; + nsinfo[i].dacl.initprivs = pstrdup(aclarray->data); + nsinfo[i].dobj.components |= DUMP_COMPONENT_ACL; + + destroyPQExpBuffer(aclarray); + destroyPQExpBuffer(aclitem); + } + } + + PQclear(res); + destroyPQExpBuffer(query); +} + +/* + * findNamespace: + * given a namespace OID, look up the info read by getNamespaces + */ +static NamespaceInfo * +findNamespace(Oid nsoid) +{ + NamespaceInfo *nsinfo; + + nsinfo = findNamespaceByOid(nsoid); + if (nsinfo == NULL) + pg_fatal("schema with OID %u does not exist", nsoid); + return nsinfo; +} + +/* + * getExtensions: + * read all extensions in the system catalogs and return them in the + * ExtensionInfo* structure + * + * numExtensions is set to the number of extensions read in + */ +ExtensionInfo * +getExtensions(Archive *fout, int *numExtensions) +{ + DumpOptions *dopt = fout->dopt; + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + ExtensionInfo *extinfo = NULL; + int i_tableoid; + int i_oid; + int i_extname; + int i_nspname; + int i_extrelocatable; + int i_extversion; + int i_extconfig; + int i_extcondition; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "SELECT x.tableoid, x.oid, " + "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition " + "FROM pg_extension x " + "JOIN pg_namespace n ON n.oid = x.extnamespace"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + if (ntups == 0) + goto cleanup; + + extinfo = pg_malloc_array(ExtensionInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_extname = PQfnumber(res, "extname"); + i_nspname = PQfnumber(res, "nspname"); + i_extrelocatable = PQfnumber(res, "extrelocatable"); + i_extversion = PQfnumber(res, "extversion"); + i_extconfig = PQfnumber(res, "extconfig"); + i_extcondition = PQfnumber(res, "extcondition"); + + for (i = 0; i < ntups; i++) + { + extinfo[i].dobj.objType = DO_EXTENSION; + extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&extinfo[i].dobj); + extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname)); + extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname)); + extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't'; + extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion)); + extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig)); + extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition)); + + /* Decide whether we want to dump it */ + selectDumpableExtension(&(extinfo[i]), dopt); + } + +cleanup: + PQclear(res); + destroyPQExpBuffer(query); + + *numExtensions = ntups; + + return extinfo; +} + +/* + * getTypes: + * get information about all types in the system catalogs + * + * NB: this must run after getFuncs() because we assume we can do + * findFuncByOid(). + */ +void +getTypes(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + TypeInfo *tyinfo; + ShellTypeInfo *stinfo; + int i_tableoid; + int i_oid; + int i_typname; + int i_typnamespace; + int i_typacl; + int i_acldefault; + int i_typowner; + int i_typelem; + int i_typrelid; + int i_typrelkind; + int i_typtype; + int i_typisdefined; + int i_isarray; + int i_typarray; + + /* + * we include even the built-in types because those may be used as array + * elements by user-defined types + * + * we filter out the built-in types when we dump out the types + * + * same approach for undefined (shell) types and array types + * + * Note: as of 8.3 we can reliably detect whether a type is an + * auto-generated array type by checking the element type's typarray. + * (Before that the test is capable of generating false positives.) We + * still check for name beginning with '_', though, so as to avoid the + * cost of the subselect probe for all standard types. This would have to + * be revisited if the backend ever allows renaming of array types. + */ + appendPQExpBufferStr(query, "SELECT tableoid, oid, typname, " + "typnamespace, typacl, " + "acldefault('T', typowner) AS acldefault, " + "typowner, " + "typelem, typrelid, typarray, " + "CASE WHEN typrelid = 0 THEN ' '::\"char\" " + "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, " + "typtype, typisdefined, " + "typname[0] = '_' AND typelem != 0 AND " + "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray " + "FROM pg_type"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + tyinfo = pg_malloc_array(TypeInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_typname = PQfnumber(res, "typname"); + i_typnamespace = PQfnumber(res, "typnamespace"); + i_typacl = PQfnumber(res, "typacl"); + i_acldefault = PQfnumber(res, "acldefault"); + i_typowner = PQfnumber(res, "typowner"); + i_typelem = PQfnumber(res, "typelem"); + i_typrelid = PQfnumber(res, "typrelid"); + i_typrelkind = PQfnumber(res, "typrelkind"); + i_typtype = PQfnumber(res, "typtype"); + i_typisdefined = PQfnumber(res, "typisdefined"); + i_isarray = PQfnumber(res, "isarray"); + i_typarray = PQfnumber(res, "typarray"); + + for (i = 0; i < ntups; i++) + { + tyinfo[i].dobj.objType = DO_TYPE; + tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&tyinfo[i].dobj); + tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname)); + tyinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_typnamespace))); + tyinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_typacl)); + tyinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + tyinfo[i].dacl.privtype = 0; + tyinfo[i].dacl.initprivs = NULL; + tyinfo[i].ftypname = NULL; /* may get filled later */ + tyinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_typowner)); + tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem)); + tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid)); + tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind); + tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype); + tyinfo[i].shellType = NULL; + + if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0) + tyinfo[i].isDefined = true; + else + tyinfo[i].isDefined = false; + + if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0) + tyinfo[i].isArray = true; + else + tyinfo[i].isArray = false; + + tyinfo[i].typarray = atooid(PQgetvalue(res, i, i_typarray)); + + if (tyinfo[i].typtype == TYPTYPE_MULTIRANGE) + tyinfo[i].isMultirange = true; + else + tyinfo[i].isMultirange = false; + + /* Decide whether we want to dump it */ + selectDumpableType(&tyinfo[i], fout); + + /* Mark whether type has an ACL */ + if (!PQgetisnull(res, i, i_typacl)) + tyinfo[i].dobj.components |= DUMP_COMPONENT_ACL; + + /* + * If it's a domain, fetch info about its constraints, if any + */ + tyinfo[i].nDomChecks = 0; + tyinfo[i].domChecks = NULL; + tyinfo[i].notnull = NULL; + if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) && + tyinfo[i].typtype == TYPTYPE_DOMAIN) + getDomainConstraints(fout, &(tyinfo[i])); + + /* + * If it's a base type, make a DumpableObject representing a shell + * definition of the type. We will need to dump that ahead of the I/O + * functions for the type. Similarly, range types need a shell + * definition in case they have a canonicalize function. + * + * Note: the shell type doesn't have a catId. You might think it + * should copy the base type's catId, but then it might capture the + * pg_depend entries for the type, which we don't want. + */ + if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) && + (tyinfo[i].typtype == TYPTYPE_BASE || + tyinfo[i].typtype == TYPTYPE_RANGE)) + { + stinfo = pg_malloc_object(ShellTypeInfo); + stinfo->dobj.objType = DO_SHELL_TYPE; + stinfo->dobj.catId = nilCatalogId; + AssignDumpId(&stinfo->dobj); + stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name); + stinfo->dobj.namespace = tyinfo[i].dobj.namespace; + stinfo->baseType = &(tyinfo[i]); + tyinfo[i].shellType = stinfo; + + /* + * Initially mark the shell type as not to be dumped. We'll only + * dump it if the I/O or canonicalize functions need to be dumped; + * this is taken care of while sorting dependencies. + */ + stinfo->dobj.dump = DUMP_COMPONENT_NONE; + } + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getOperators: + * get information about all operators in the system catalogs + */ +void +getOperators(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + OprInfo *oprinfo; + int i_tableoid; + int i_oid; + int i_oprname; + int i_oprnamespace; + int i_oprowner; + int i_oprkind; + int i_oprleft; + int i_oprright; + int i_oprcode; + + /* + * find all operators, including builtin operators; we filter out + * system-defined operators at dump-out time. + */ + + appendPQExpBufferStr(query, "SELECT tableoid, oid, oprname, " + "oprnamespace, " + "oprowner, " + "oprkind, " + "oprleft, " + "oprright, " + "oprcode::oid AS oprcode " + "FROM pg_operator"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + oprinfo = pg_malloc_array(OprInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_oprname = PQfnumber(res, "oprname"); + i_oprnamespace = PQfnumber(res, "oprnamespace"); + i_oprowner = PQfnumber(res, "oprowner"); + i_oprkind = PQfnumber(res, "oprkind"); + i_oprleft = PQfnumber(res, "oprleft"); + i_oprright = PQfnumber(res, "oprright"); + i_oprcode = PQfnumber(res, "oprcode"); + + for (i = 0; i < ntups; i++) + { + oprinfo[i].dobj.objType = DO_OPERATOR; + oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&oprinfo[i].dobj); + oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname)); + oprinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace))); + oprinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_oprowner)); + oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0]; + oprinfo[i].oprleft = atooid(PQgetvalue(res, i, i_oprleft)); + oprinfo[i].oprright = atooid(PQgetvalue(res, i, i_oprright)); + oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(oprinfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getCollations: + * get information about all collations in the system catalogs + */ +void +getCollations(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + CollInfo *collinfo; + int i_tableoid; + int i_oid; + int i_collname; + int i_collnamespace; + int i_collowner; + int i_collencoding; + + query = createPQExpBuffer(); + + /* + * find all collations, including builtin collations; we filter out + * system-defined collations at dump-out time. + */ + + appendPQExpBufferStr(query, "SELECT tableoid, oid, collname, " + "collnamespace, " + "collowner, " + "collencoding " + "FROM pg_collation"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + collinfo = pg_malloc_array(CollInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_collname = PQfnumber(res, "collname"); + i_collnamespace = PQfnumber(res, "collnamespace"); + i_collowner = PQfnumber(res, "collowner"); + i_collencoding = PQfnumber(res, "collencoding"); + + for (i = 0; i < ntups; i++) + { + collinfo[i].dobj.objType = DO_COLLATION; + collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&collinfo[i].dobj); + collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname)); + collinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_collnamespace))); + collinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_collowner)); + collinfo[i].collencoding = atoi(PQgetvalue(res, i, i_collencoding)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(collinfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getConversions: + * get information about all conversions in the system catalogs + */ +void +getConversions(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + ConvInfo *convinfo; + int i_tableoid; + int i_oid; + int i_conname; + int i_connamespace; + int i_conowner; + + query = createPQExpBuffer(); + + /* + * find all conversions, including builtin conversions; we filter out + * system-defined conversions at dump-out time. + */ + + appendPQExpBufferStr(query, "SELECT tableoid, oid, conname, " + "connamespace, " + "conowner " + "FROM pg_conversion"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + convinfo = pg_malloc_array(ConvInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_conname = PQfnumber(res, "conname"); + i_connamespace = PQfnumber(res, "connamespace"); + i_conowner = PQfnumber(res, "conowner"); + + for (i = 0; i < ntups; i++) + { + convinfo[i].dobj.objType = DO_CONVERSION; + convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&convinfo[i].dobj); + convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname)); + convinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_connamespace))); + convinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_conowner)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(convinfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getAccessMethods: + * get information about all user-defined access methods + */ +void +getAccessMethods(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + AccessMethodInfo *aminfo; + int i_tableoid; + int i_oid; + int i_amname; + int i_amhandler; + int i_amtype; + + query = createPQExpBuffer(); + + /* + * Select all access methods from pg_am table. v9.6 introduced CREATE + * ACCESS METHOD, so earlier versions usually have only built-in access + * methods. v9.6 also changed the access method API, replacing dozens of + * pg_am columns with amhandler. Even if a user created an access method + * by "INSERT INTO pg_am", we have no way to translate pre-v9.6 pg_am + * columns to a v9.6+ CREATE ACCESS METHOD. Hence, before v9.6, read + * pg_am just to facilitate findAccessMethodByOid() providing the + * OID-to-name mapping. + */ + appendPQExpBufferStr(query, "SELECT tableoid, oid, amname, "); + if (fout->remoteVersion >= 90600) + appendPQExpBufferStr(query, + "amtype, " + "amhandler::pg_catalog.regproc AS amhandler "); + else + appendPQExpBufferStr(query, + "'i'::pg_catalog.\"char\" AS amtype, " + "'-'::pg_catalog.regproc AS amhandler "); + appendPQExpBufferStr(query, "FROM pg_am"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + aminfo = pg_malloc_array(AccessMethodInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_amname = PQfnumber(res, "amname"); + i_amhandler = PQfnumber(res, "amhandler"); + i_amtype = PQfnumber(res, "amtype"); + + for (i = 0; i < ntups; i++) + { + aminfo[i].dobj.objType = DO_ACCESS_METHOD; + aminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + aminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&aminfo[i].dobj); + aminfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_amname)); + aminfo[i].dobj.namespace = NULL; + aminfo[i].amhandler = pg_strdup(PQgetvalue(res, i, i_amhandler)); + aminfo[i].amtype = *(PQgetvalue(res, i, i_amtype)); + + /* Decide whether we want to dump it */ + selectDumpableAccessMethod(&(aminfo[i]), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + + +/* + * getOpclasses: + * get information about all opclasses in the system catalogs + */ +void +getOpclasses(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + OpclassInfo *opcinfo; + int i_tableoid; + int i_oid; + int i_opcmethod; + int i_opcname; + int i_opcnamespace; + int i_opcowner; + + /* + * find all opclasses, including builtin opclasses; we filter out + * system-defined opclasses at dump-out time. + */ + + appendPQExpBufferStr(query, "SELECT tableoid, oid, opcmethod, opcname, " + "opcnamespace, " + "opcowner " + "FROM pg_opclass"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + opcinfo = pg_malloc_array(OpclassInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_opcmethod = PQfnumber(res, "opcmethod"); + i_opcname = PQfnumber(res, "opcname"); + i_opcnamespace = PQfnumber(res, "opcnamespace"); + i_opcowner = PQfnumber(res, "opcowner"); + + for (i = 0; i < ntups; i++) + { + opcinfo[i].dobj.objType = DO_OPCLASS; + opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&opcinfo[i].dobj); + opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname)); + opcinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_opcnamespace))); + opcinfo[i].opcmethod = atooid(PQgetvalue(res, i, i_opcmethod)); + opcinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opcowner)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(opcinfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getOpfamilies: + * get information about all opfamilies in the system catalogs + */ +void +getOpfamilies(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + OpfamilyInfo *opfinfo; + int i_tableoid; + int i_oid; + int i_opfmethod; + int i_opfname; + int i_opfnamespace; + int i_opfowner; + + query = createPQExpBuffer(); + + /* + * find all opfamilies, including builtin opfamilies; we filter out + * system-defined opfamilies at dump-out time. + */ + + appendPQExpBufferStr(query, "SELECT tableoid, oid, opfmethod, opfname, " + "opfnamespace, " + "opfowner " + "FROM pg_opfamily"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + opfinfo = pg_malloc_array(OpfamilyInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_opfname = PQfnumber(res, "opfname"); + i_opfmethod = PQfnumber(res, "opfmethod"); + i_opfnamespace = PQfnumber(res, "opfnamespace"); + i_opfowner = PQfnumber(res, "opfowner"); + + for (i = 0; i < ntups; i++) + { + opfinfo[i].dobj.objType = DO_OPFAMILY; + opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&opfinfo[i].dobj); + opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname)); + opfinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_opfnamespace))); + opfinfo[i].opfmethod = atooid(PQgetvalue(res, i, i_opfmethod)); + opfinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_opfowner)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(opfinfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getAggregates: + * get information about all user-defined aggregates in the system catalogs + */ +void +getAggregates(Archive *fout) +{ + DumpOptions *dopt = fout->dopt; + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + AggInfo *agginfo; + int i_tableoid; + int i_oid; + int i_aggname; + int i_aggnamespace; + int i_pronargs; + int i_proargtypes; + int i_proowner; + int i_aggacl; + int i_acldefault; + + /* + * Find all interesting aggregates. See comment in getFuncs() for the + * rationale behind the filtering logic. + */ + if (fout->remoteVersion >= 90600) + { + const char *agg_check; + + agg_check = (fout->remoteVersion >= 110000 ? "p.prokind = 'a'" + : "p.proisagg"); + + appendPQExpBuffer(query, "SELECT p.tableoid, p.oid, " + "p.proname AS aggname, " + "p.pronamespace AS aggnamespace, " + "p.pronargs, p.proargtypes, " + "p.proowner, " + "p.proacl AS aggacl, " + "acldefault('f', p.proowner) AS acldefault " + "FROM pg_proc p " + "LEFT JOIN pg_init_privs pip ON " + "(p.oid = pip.objoid " + "AND pip.classoid = 'pg_proc'::regclass " + "AND pip.objsubid = 0) " + "WHERE %s AND (" + "p.pronamespace != " + "(SELECT oid FROM pg_namespace " + "WHERE nspname = 'pg_catalog') OR " + "p.proacl IS DISTINCT FROM pip.initprivs", + agg_check); + if (dopt->binary_upgrade) + appendPQExpBufferStr(query, + " OR EXISTS(SELECT 1 FROM pg_depend WHERE " + "classid = 'pg_proc'::regclass AND " + "objid = p.oid AND " + "refclassid = 'pg_extension'::regclass AND " + "deptype = 'e')"); + appendPQExpBufferChar(query, ')'); + } + else + { + appendPQExpBufferStr(query, "SELECT tableoid, oid, proname AS aggname, " + "pronamespace AS aggnamespace, " + "pronargs, proargtypes, " + "proowner, " + "proacl AS aggacl, " + "acldefault('f', proowner) AS acldefault " + "FROM pg_proc p " + "WHERE proisagg AND (" + "pronamespace != " + "(SELECT oid FROM pg_namespace " + "WHERE nspname = 'pg_catalog')"); + if (dopt->binary_upgrade) + appendPQExpBufferStr(query, + " OR EXISTS(SELECT 1 FROM pg_depend WHERE " + "classid = 'pg_proc'::regclass AND " + "objid = p.oid AND " + "refclassid = 'pg_extension'::regclass AND " + "deptype = 'e')"); + appendPQExpBufferChar(query, ')'); + } + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + agginfo = pg_malloc_array(AggInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_aggname = PQfnumber(res, "aggname"); + i_aggnamespace = PQfnumber(res, "aggnamespace"); + i_pronargs = PQfnumber(res, "pronargs"); + i_proargtypes = PQfnumber(res, "proargtypes"); + i_proowner = PQfnumber(res, "proowner"); + i_aggacl = PQfnumber(res, "aggacl"); + i_acldefault = PQfnumber(res, "acldefault"); + + for (i = 0; i < ntups; i++) + { + agginfo[i].aggfn.dobj.objType = DO_AGG; + agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&agginfo[i].aggfn.dobj); + agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname)); + agginfo[i].aggfn.dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_aggnamespace))); + agginfo[i].aggfn.dacl.acl = pg_strdup(PQgetvalue(res, i, i_aggacl)); + agginfo[i].aggfn.dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + agginfo[i].aggfn.dacl.privtype = 0; + agginfo[i].aggfn.dacl.initprivs = NULL; + agginfo[i].aggfn.rolname = getRoleName(PQgetvalue(res, i, i_proowner)); + agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */ + agginfo[i].aggfn.prorettype = InvalidOid; /* not saved */ + agginfo[i].aggfn.nargs = atoi(PQgetvalue(res, i, i_pronargs)); + if (agginfo[i].aggfn.nargs == 0) + agginfo[i].aggfn.argtypes = NULL; + else + { + agginfo[i].aggfn.argtypes = pg_malloc_array(Oid, agginfo[i].aggfn.nargs); + parseOidArray(PQgetvalue(res, i, i_proargtypes), + agginfo[i].aggfn.argtypes, + agginfo[i].aggfn.nargs); + } + agginfo[i].aggfn.postponed_def = false; /* might get set during sort */ + + /* Decide whether we want to dump it */ + selectDumpableObject(&(agginfo[i].aggfn.dobj), fout); + + /* Mark whether aggregate has an ACL */ + if (!PQgetisnull(res, i, i_aggacl)) + agginfo[i].aggfn.dobj.components |= DUMP_COMPONENT_ACL; + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getFuncs: + * get information about all user-defined functions in the system catalogs + */ +void +getFuncs(Archive *fout) +{ + DumpOptions *dopt = fout->dopt; + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + FuncInfo *finfo; + int i_tableoid; + int i_oid; + int i_proname; + int i_pronamespace; + int i_proowner; + int i_prolang; + int i_pronargs; + int i_proargtypes; + int i_prorettype; + int i_proacl; + int i_acldefault; + + /* + * Find all interesting functions. This is a bit complicated: + * + * 1. Always exclude aggregates; those are handled elsewhere. + * + * 2. Always exclude functions that are internally dependent on something + * else, since presumably those will be created as a result of creating + * the something else. This currently acts only to suppress constructor + * functions for range types. Note this is OK only because the + * constructors don't have any dependencies the range type doesn't have; + * otherwise we might not get creation ordering correct. + * + * 3. Otherwise, we normally exclude functions in pg_catalog. However, if + * they're members of extensions and we are in binary-upgrade mode then + * include them, since we want to dump extension members individually in + * that mode. Also, if they are used by casts or transforms then we need + * to gather the information about them, though they won't be dumped if + * they are built-in. Also, in 9.6 and up, include functions in + * pg_catalog if they have an ACL different from what's shown in + * pg_init_privs (so we have to join to pg_init_privs; annoying). + */ + if (fout->remoteVersion >= 90600) + { + const char *not_agg_check; + + not_agg_check = (fout->remoteVersion >= 110000 ? "p.prokind <> 'a'" + : "NOT p.proisagg"); + + appendPQExpBuffer(query, + "SELECT p.tableoid, p.oid, p.proname, p.prolang, " + "p.pronargs, p.proargtypes, p.prorettype, " + "p.proacl, " + "acldefault('f', p.proowner) AS acldefault, " + "p.pronamespace, " + "p.proowner " + "FROM pg_proc p " + "LEFT JOIN pg_init_privs pip ON " + "(p.oid = pip.objoid " + "AND pip.classoid = 'pg_proc'::regclass " + "AND pip.objsubid = 0) " + "WHERE %s" + "\n AND NOT EXISTS (SELECT 1 FROM pg_depend " + "WHERE classid = 'pg_proc'::regclass AND " + "objid = p.oid AND deptype = 'i')" + "\n AND (" + "\n pronamespace != " + "(SELECT oid FROM pg_namespace " + "WHERE nspname = 'pg_catalog')" + "\n OR EXISTS (SELECT 1 FROM pg_cast" + "\n WHERE pg_cast.oid > %u " + "\n AND p.oid = pg_cast.castfunc)" + "\n OR EXISTS (SELECT 1 FROM pg_transform" + "\n WHERE pg_transform.oid > %u AND " + "\n (p.oid = pg_transform.trffromsql" + "\n OR p.oid = pg_transform.trftosql))", + not_agg_check, + g_last_builtin_oid, + g_last_builtin_oid); + if (dopt->binary_upgrade) + appendPQExpBufferStr(query, + "\n OR EXISTS(SELECT 1 FROM pg_depend WHERE " + "classid = 'pg_proc'::regclass AND " + "objid = p.oid AND " + "refclassid = 'pg_extension'::regclass AND " + "deptype = 'e')"); + appendPQExpBufferStr(query, + "\n OR p.proacl IS DISTINCT FROM pip.initprivs"); + appendPQExpBufferChar(query, ')'); + } + else + { + appendPQExpBuffer(query, + "SELECT tableoid, oid, proname, prolang, " + "pronargs, proargtypes, prorettype, proacl, " + "acldefault('f', proowner) AS acldefault, " + "pronamespace, " + "proowner " + "FROM pg_proc p " + "WHERE NOT proisagg" + "\n AND NOT EXISTS (SELECT 1 FROM pg_depend " + "WHERE classid = 'pg_proc'::regclass AND " + "objid = p.oid AND deptype = 'i')" + "\n AND (" + "\n pronamespace != " + "(SELECT oid FROM pg_namespace " + "WHERE nspname = 'pg_catalog')" + "\n OR EXISTS (SELECT 1 FROM pg_cast" + "\n WHERE pg_cast.oid > '%u'::oid" + "\n AND p.oid = pg_cast.castfunc)", + g_last_builtin_oid); + + if (fout->remoteVersion >= 90500) + appendPQExpBuffer(query, + "\n OR EXISTS (SELECT 1 FROM pg_transform" + "\n WHERE pg_transform.oid > '%u'::oid" + "\n AND (p.oid = pg_transform.trffromsql" + "\n OR p.oid = pg_transform.trftosql))", + g_last_builtin_oid); + + if (dopt->binary_upgrade) + appendPQExpBufferStr(query, + "\n OR EXISTS(SELECT 1 FROM pg_depend WHERE " + "classid = 'pg_proc'::regclass AND " + "objid = p.oid AND " + "refclassid = 'pg_extension'::regclass AND " + "deptype = 'e')"); + appendPQExpBufferChar(query, ')'); + } + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + finfo = pg_malloc0_array(FuncInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_proname = PQfnumber(res, "proname"); + i_pronamespace = PQfnumber(res, "pronamespace"); + i_proowner = PQfnumber(res, "proowner"); + i_prolang = PQfnumber(res, "prolang"); + i_pronargs = PQfnumber(res, "pronargs"); + i_proargtypes = PQfnumber(res, "proargtypes"); + i_prorettype = PQfnumber(res, "prorettype"); + i_proacl = PQfnumber(res, "proacl"); + i_acldefault = PQfnumber(res, "acldefault"); + + for (i = 0; i < ntups; i++) + { + finfo[i].dobj.objType = DO_FUNC; + finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&finfo[i].dobj); + finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname)); + finfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_pronamespace))); + finfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_proacl)); + finfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + finfo[i].dacl.privtype = 0; + finfo[i].dacl.initprivs = NULL; + finfo[i].rolname = getRoleName(PQgetvalue(res, i, i_proowner)); + finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang)); + finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype)); + finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs)); + if (finfo[i].nargs == 0) + finfo[i].argtypes = NULL; + else + { + finfo[i].argtypes = pg_malloc_array(Oid, finfo[i].nargs); + parseOidArray(PQgetvalue(res, i, i_proargtypes), + finfo[i].argtypes, finfo[i].nargs); + } + finfo[i].postponed_def = false; /* might get set during sort */ + + /* Decide whether we want to dump it */ + selectDumpableObject(&(finfo[i].dobj), fout); + + /* Mark whether function has an ACL */ + if (!PQgetisnull(res, i, i_proacl)) + finfo[i].dobj.components |= DUMP_COMPONENT_ACL; + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getRelationStatistics + * register the statistics object as a dependent of the relation. + * + * reltuples is passed as a string to avoid complexities in converting from/to + * floating point. + */ +static RelStatsInfo * +getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages, + char *reltuples, int32 relallvisible, + int32 relallfrozen, char relkind, + char **indAttNames, int nindAttNames) +{ + if (!fout->dopt->dumpStatistics) + return NULL; + + if ((relkind == RELKIND_RELATION) || + (relkind == RELKIND_PARTITIONED_TABLE) || + (relkind == RELKIND_INDEX) || + (relkind == RELKIND_PARTITIONED_INDEX) || + (relkind == RELKIND_MATVIEW || + relkind == RELKIND_FOREIGN_TABLE)) + { + RelStatsInfo *info = pg_malloc0_object(RelStatsInfo); + DumpableObject *dobj = &info->dobj; + + dobj->objType = DO_REL_STATS; + dobj->catId.tableoid = 0; + dobj->catId.oid = 0; + AssignDumpId(dobj); + dobj->dependencies = pg_malloc_object(DumpId); + dobj->dependencies[0] = rel->dumpId; + dobj->nDeps = 1; + dobj->allocDeps = 1; + dobj->components |= DUMP_COMPONENT_STATISTICS; + dobj->name = pg_strdup(rel->name); + dobj->namespace = rel->namespace; + info->relid = rel->catId.oid; + info->relpages = relpages; + info->reltuples = pstrdup(reltuples); + info->relallvisible = relallvisible; + info->relallfrozen = relallfrozen; + info->relkind = relkind; + info->indAttNames = indAttNames; + info->nindAttNames = nindAttNames; + + /* + * Ordinarily, stats go in SECTION_DATA for tables and + * SECTION_POST_DATA for indexes. + * + * However, the section may be updated later for materialized view + * stats. REFRESH MATERIALIZED VIEW replaces the storage and resets + * the stats, so the stats must be restored after the data. Also, the + * materialized view definition may be postponed to SECTION_POST_DATA + * (see repairMatViewBoundaryMultiLoop()). + */ + switch (info->relkind) + { + case RELKIND_RELATION: + case RELKIND_PARTITIONED_TABLE: + case RELKIND_MATVIEW: + case RELKIND_FOREIGN_TABLE: + info->section = SECTION_DATA; + break; + case RELKIND_INDEX: + case RELKIND_PARTITIONED_INDEX: + info->section = SECTION_POST_DATA; + break; + default: + pg_fatal("cannot dump statistics for relation kind \"%c\"", + info->relkind); + } + + return info; + } + return NULL; +} + +/* + * getTables + * read all the tables (no indexes) in the system catalogs, + * and return them as an array of TableInfo structures + * + * *numTables is set to the number of tables read in + */ +TableInfo * +getTables(Archive *fout, int *numTables) +{ + DumpOptions *dopt = fout->dopt; + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + TableInfo *tblinfo; + int i_reltableoid; + int i_reloid; + int i_relname; + int i_relnamespace; + int i_relkind; + int i_reltype; + int i_relowner; + int i_relchecks; + int i_relhasindex; + int i_relhasrules; + int i_relpages; + int i_reltuples; + int i_relallvisible; + int i_relallfrozen; + int i_toastpages; + int i_owning_tab; + int i_owning_col; + int i_reltablespace; + int i_relhasoids; + int i_relhastriggers; + int i_relpersistence; + int i_relispopulated; + int i_relreplident; + int i_relrowsec; + int i_relforcerowsec; + int i_relfrozenxid; + int i_toastfrozenxid; + int i_toastoid; + int i_relminmxid; + int i_toastminmxid; + int i_reloptions; + int i_checkoption; + int i_toastreloptions; + int i_reloftype; + int i_foreignserver; + int i_amname; + int i_is_identity_sequence; + int i_relacl; + int i_acldefault; + int i_ispartition; + + /* + * Find all the tables and table-like objects. + * + * We must fetch all tables in this phase because otherwise we cannot + * correctly identify inherited columns, owned sequences, etc. + * + * We include system catalogs, so that we can work if a user table is + * defined to inherit from a system catalog (pretty weird, but...) + * + * Note: in this phase we should collect only a minimal amount of + * information about each table, basically just enough to decide if it is + * interesting. In particular, since we do not yet have lock on any user + * table, we MUST NOT invoke any server-side data collection functions + * (for instance, pg_get_partkeydef()). Those are likely to fail or give + * wrong answers if any concurrent DDL is happening. + */ + + appendPQExpBufferStr(query, + "SELECT c.tableoid, c.oid, c.relname, " + "c.relnamespace, c.relkind, c.reltype, " + "c.relowner, " + "c.relchecks, " + "c.relhasindex, c.relhasrules, c.relpages, " + "c.reltuples, c.relallvisible, "); + + if (fout->remoteVersion >= 180000) + appendPQExpBufferStr(query, "c.relallfrozen, "); + else + appendPQExpBufferStr(query, "0 AS relallfrozen, "); + + appendPQExpBufferStr(query, + "c.relhastriggers, c.relpersistence, " + "c.reloftype, " + "c.relacl, " + "acldefault(CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE) + " THEN 's'::\"char\" ELSE 'r'::\"char\" END, c.relowner) AS acldefault, " + "CASE WHEN c.relkind = " CppAsString2(RELKIND_FOREIGN_TABLE) " THEN " + "(SELECT ftserver FROM pg_catalog.pg_foreign_table WHERE ftrelid = c.oid) " + "ELSE 0 END AS foreignserver, " + "c.relfrozenxid, tc.relfrozenxid AS tfrozenxid, " + "tc.oid AS toid, " + "tc.relpages AS toastpages, " + "tc.reloptions AS toast_reloptions, " + "d.refobjid AS owning_tab, " + "d.refobjsubid AS owning_col, " + "tsp.spcname AS reltablespace, "); + + if (fout->remoteVersion >= 120000) + appendPQExpBufferStr(query, + "false AS relhasoids, "); + else + appendPQExpBufferStr(query, + "c.relhasoids, "); + + if (fout->remoteVersion >= 90300) + appendPQExpBufferStr(query, + "c.relispopulated, "); + else + appendPQExpBufferStr(query, + "'t' as relispopulated, "); + + if (fout->remoteVersion >= 90400) + appendPQExpBufferStr(query, + "c.relreplident, "); + else + appendPQExpBufferStr(query, + "'d' AS relreplident, "); + + if (fout->remoteVersion >= 90500) + appendPQExpBufferStr(query, + "c.relrowsecurity, c.relforcerowsecurity, "); + else + appendPQExpBufferStr(query, + "false AS relrowsecurity, " + "false AS relforcerowsecurity, "); + + if (fout->remoteVersion >= 90300) + appendPQExpBufferStr(query, + "c.relminmxid, tc.relminmxid AS tminmxid, "); + else + appendPQExpBufferStr(query, + "0 AS relminmxid, 0 AS tminmxid, "); + + if (fout->remoteVersion >= 90300) + appendPQExpBufferStr(query, + "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, " + "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text " + "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "); + else + appendPQExpBufferStr(query, + "c.reloptions, NULL AS checkoption, "); + + if (fout->remoteVersion >= 90600) + appendPQExpBufferStr(query, + "am.amname, "); + else + appendPQExpBufferStr(query, + "NULL AS amname, "); + + if (fout->remoteVersion >= 90600) + appendPQExpBufferStr(query, + "(d.deptype = 'i') IS TRUE AS is_identity_sequence, "); + else + appendPQExpBufferStr(query, + "false AS is_identity_sequence, "); + + if (fout->remoteVersion >= 100000) + appendPQExpBufferStr(query, + "c.relispartition AS ispartition "); + else + appendPQExpBufferStr(query, + "false AS ispartition "); + + /* + * Left join to pg_depend to pick up dependency info linking sequences to + * their owning column, if any (note this dependency is AUTO except for + * identity sequences, where it's INTERNAL). Also join to pg_tablespace to + * collect the spcname. + */ + appendPQExpBufferStr(query, + "\nFROM pg_class c\n" + "LEFT JOIN pg_depend d ON " + "(c.relkind = " CppAsString2(RELKIND_SEQUENCE) " AND " + "d.classid = 'pg_class'::regclass AND d.objid = c.oid AND " + "d.objsubid = 0 AND " + "d.refclassid = 'pg_class'::regclass AND d.deptype IN ('a', 'i'))\n" + "LEFT JOIN pg_tablespace tsp ON (tsp.oid = c.reltablespace)\n"); + + /* + * In 9.6 and up, left join to pg_am to pick up the amname. + */ + if (fout->remoteVersion >= 90600) + appendPQExpBufferStr(query, + "LEFT JOIN pg_am am ON (c.relam = am.oid)\n"); + + /* + * We purposefully ignore toast OIDs for partitioned tables; the reason is + * that versions 10 and 11 have them, but later versions do not, so + * emitting them causes the upgrade to fail. + */ + appendPQExpBufferStr(query, + "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid" + " AND tc.relkind = " CppAsString2(RELKIND_TOASTVALUE) + " AND c.relkind <> " CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n"); + + /* + * Restrict to interesting relkinds (in particular, not indexes). Not all + * relkinds are possible in older servers, but it's not worth the trouble + * to emit a version-dependent list. + * + * Composite-type table entries won't be dumped as such, but we have to + * make a DumpableObject for them so that we can track dependencies of the + * composite type (pg_depend entries for columns of the composite type + * link to the pg_class entry not the pg_type entry). + */ + appendPQExpBufferStr(query, + "WHERE c.relkind IN (" + CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_SEQUENCE) ", " + CppAsString2(RELKIND_VIEW) ", " + CppAsString2(RELKIND_COMPOSITE_TYPE) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ", " + CppAsString2(RELKIND_PROPGRAPH) ")\n" + "ORDER BY c.oid"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + *numTables = ntups; + + /* + * Extract data from result and lock dumpable tables. We do the locking + * before anything else, to minimize the window wherein a table could + * disappear under us. + * + * Note that we have to save info about all tables here, even when dumping + * only one, because we don't yet know which tables might be inheritance + * ancestors of the target table. + */ + tblinfo = pg_malloc0_array(TableInfo, ntups); + + i_reltableoid = PQfnumber(res, "tableoid"); + i_reloid = PQfnumber(res, "oid"); + i_relname = PQfnumber(res, "relname"); + i_relnamespace = PQfnumber(res, "relnamespace"); + i_relkind = PQfnumber(res, "relkind"); + i_reltype = PQfnumber(res, "reltype"); + i_relowner = PQfnumber(res, "relowner"); + i_relchecks = PQfnumber(res, "relchecks"); + i_relhasindex = PQfnumber(res, "relhasindex"); + i_relhasrules = PQfnumber(res, "relhasrules"); + i_relpages = PQfnumber(res, "relpages"); + i_reltuples = PQfnumber(res, "reltuples"); + i_relallvisible = PQfnumber(res, "relallvisible"); + i_relallfrozen = PQfnumber(res, "relallfrozen"); + i_toastpages = PQfnumber(res, "toastpages"); + i_owning_tab = PQfnumber(res, "owning_tab"); + i_owning_col = PQfnumber(res, "owning_col"); + i_reltablespace = PQfnumber(res, "reltablespace"); + i_relhasoids = PQfnumber(res, "relhasoids"); + i_relhastriggers = PQfnumber(res, "relhastriggers"); + i_relpersistence = PQfnumber(res, "relpersistence"); + i_relispopulated = PQfnumber(res, "relispopulated"); + i_relreplident = PQfnumber(res, "relreplident"); + i_relrowsec = PQfnumber(res, "relrowsecurity"); + i_relforcerowsec = PQfnumber(res, "relforcerowsecurity"); + i_relfrozenxid = PQfnumber(res, "relfrozenxid"); + i_toastfrozenxid = PQfnumber(res, "tfrozenxid"); + i_toastoid = PQfnumber(res, "toid"); + i_relminmxid = PQfnumber(res, "relminmxid"); + i_toastminmxid = PQfnumber(res, "tminmxid"); + i_reloptions = PQfnumber(res, "reloptions"); + i_checkoption = PQfnumber(res, "checkoption"); + i_toastreloptions = PQfnumber(res, "toast_reloptions"); + i_reloftype = PQfnumber(res, "reloftype"); + i_foreignserver = PQfnumber(res, "foreignserver"); + i_amname = PQfnumber(res, "amname"); + i_is_identity_sequence = PQfnumber(res, "is_identity_sequence"); + i_relacl = PQfnumber(res, "relacl"); + i_acldefault = PQfnumber(res, "acldefault"); + i_ispartition = PQfnumber(res, "ispartition"); + + if (dopt->lockWaitTimeout) + { + /* + * Arrange to fail instead of waiting forever for a table lock. + * + * NB: this coding assumes that the only queries issued within the + * following loop are LOCK TABLEs; else the timeout may be undesirably + * applied to other things too. + */ + resetPQExpBuffer(query); + appendPQExpBufferStr(query, "SET statement_timeout = "); + appendStringLiteralConn(query, dopt->lockWaitTimeout, GetConnection(fout)); + ExecuteSqlStatement(fout, query->data); + } + + resetPQExpBuffer(query); + + for (i = 0; i < ntups; i++) + { + int32 relallvisible = atoi(PQgetvalue(res, i, i_relallvisible)); + int32 relallfrozen = atoi(PQgetvalue(res, i, i_relallfrozen)); + + tblinfo[i].dobj.objType = DO_TABLE; + tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid)); + tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid)); + AssignDumpId(&tblinfo[i].dobj); + tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname)); + tblinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_relnamespace))); + tblinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_relacl)); + tblinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + tblinfo[i].dacl.privtype = 0; + tblinfo[i].dacl.initprivs = NULL; + tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind)); + tblinfo[i].reltype = atooid(PQgetvalue(res, i, i_reltype)); + tblinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_relowner)); + tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks)); + tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0); + tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0); + tblinfo[i].relpages = atoi(PQgetvalue(res, i, i_relpages)); + if (PQgetisnull(res, i, i_toastpages)) + tblinfo[i].toastpages = 0; + else + tblinfo[i].toastpages = atoi(PQgetvalue(res, i, i_toastpages)); + if (PQgetisnull(res, i, i_owning_tab)) + { + tblinfo[i].owning_tab = InvalidOid; + tblinfo[i].owning_col = 0; + } + else + { + tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab)); + tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col)); + } + tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace)); + tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0); + tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0); + tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence)); + tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0); + tblinfo[i].relreplident = *(PQgetvalue(res, i, i_relreplident)); + tblinfo[i].rowsec = (strcmp(PQgetvalue(res, i, i_relrowsec), "t") == 0); + tblinfo[i].forcerowsec = (strcmp(PQgetvalue(res, i, i_relforcerowsec), "t") == 0); + tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid)); + tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid)); + tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid)); + tblinfo[i].minmxid = atooid(PQgetvalue(res, i, i_relminmxid)); + tblinfo[i].toast_minmxid = atooid(PQgetvalue(res, i, i_toastminmxid)); + tblinfo[i].reloptions = pg_strdup(PQgetvalue(res, i, i_reloptions)); + if (PQgetisnull(res, i, i_checkoption)) + tblinfo[i].checkoption = NULL; + else + tblinfo[i].checkoption = pg_strdup(PQgetvalue(res, i, i_checkoption)); + tblinfo[i].toast_reloptions = pg_strdup(PQgetvalue(res, i, i_toastreloptions)); + tblinfo[i].reloftype = atooid(PQgetvalue(res, i, i_reloftype)); + tblinfo[i].foreign_server = atooid(PQgetvalue(res, i, i_foreignserver)); + if (PQgetisnull(res, i, i_amname)) + tblinfo[i].amname = NULL; + else + tblinfo[i].amname = pg_strdup(PQgetvalue(res, i, i_amname)); + tblinfo[i].is_identity_sequence = (strcmp(PQgetvalue(res, i, i_is_identity_sequence), "t") == 0); + tblinfo[i].ispartition = (strcmp(PQgetvalue(res, i, i_ispartition), "t") == 0); + + /* other fields were zeroed above */ + + /* + * Decide whether we want to dump this table. + */ + if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE) + tblinfo[i].dobj.dump = DUMP_COMPONENT_NONE; + else + selectDumpableTable(&tblinfo[i], fout); + + /* + * Now, consider the table "interesting" if we need to dump its + * definition, data or its statistics. Later on, we'll skip a lot of + * data collection for uninteresting tables. + * + * Note: the "interesting" flag will also be set by flagInhTables for + * parents of interesting tables, so that we collect necessary + * inheritance info even when the parents are not themselves being + * dumped. This is the main reason why we need an "interesting" flag + * that's separate from the components-to-dump bitmask. + */ + tblinfo[i].interesting = (tblinfo[i].dobj.dump & + (DUMP_COMPONENT_DEFINITION | + DUMP_COMPONENT_DATA | + DUMP_COMPONENT_STATISTICS)) != 0; + + tblinfo[i].dummy_view = false; /* might get set during sort */ + tblinfo[i].postponed_def = false; /* might get set during sort */ + + /* Tables have data */ + tblinfo[i].dobj.components |= DUMP_COMPONENT_DATA; + + /* Mark whether table has an ACL */ + if (!PQgetisnull(res, i, i_relacl)) + tblinfo[i].dobj.components |= DUMP_COMPONENT_ACL; + tblinfo[i].hascolumnACLs = false; /* may get set later */ + + /* Add statistics */ + if (tblinfo[i].interesting) + { + RelStatsInfo *stats; + + stats = getRelationStatistics(fout, &tblinfo[i].dobj, + tblinfo[i].relpages, + PQgetvalue(res, i, i_reltuples), + relallvisible, relallfrozen, + tblinfo[i].relkind, NULL, 0); + if (tblinfo[i].relkind == RELKIND_MATVIEW) + tblinfo[i].stats = stats; + } + + /* + * Read-lock target tables to make sure they aren't DROPPED or altered + * in schema before we get around to dumping them. + * + * Note that we don't explicitly lock parents of the target tables; we + * assume our lock on the child is enough to prevent schema + * alterations to parent tables. + * + * NOTE: it'd be kinda nice to lock other relations too, not only + * plain or partitioned tables, but the backend doesn't presently + * allow that. + * + * We only need to lock the table for certain components; see + * pg_dump.h + */ + if ((tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK) && + (tblinfo[i].relkind == RELKIND_RELATION || + tblinfo[i].relkind == RELKIND_PARTITIONED_TABLE)) + { + /* + * Tables are locked in batches. When dumping from a remote + * server this can save a significant amount of time by reducing + * the number of round trips. + */ + if (query->len == 0) + appendPQExpBuffer(query, "LOCK TABLE %s", + fmtQualifiedDumpable(&tblinfo[i])); + else + { + appendPQExpBuffer(query, ", %s", + fmtQualifiedDumpable(&tblinfo[i])); + + /* Arbitrarily end a batch when query length reaches 100K. */ + if (query->len >= 100000) + { + /* Lock another batch of tables. */ + appendPQExpBufferStr(query, " IN ACCESS SHARE MODE"); + ExecuteSqlStatement(fout, query->data); + resetPQExpBuffer(query); + } + } + } + } + + if (query->len != 0) + { + /* Lock the tables in the last batch. */ + appendPQExpBufferStr(query, " IN ACCESS SHARE MODE"); + ExecuteSqlStatement(fout, query->data); + } + + if (dopt->lockWaitTimeout) + { + ExecuteSqlStatement(fout, "SET statement_timeout = 0"); + } + + PQclear(res); + + destroyPQExpBuffer(query); + + return tblinfo; +} + +/* + * getOwnedSeqs + * identify owned sequences and mark them as dumpable if owning table is + * + * We used to do this in getTables(), but it's better to do it after the + * index used by findTableByOid() has been set up. + */ +void +getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables) +{ + int i; + + /* + * Force sequences that are "owned" by table columns to be dumped whenever + * their owning table is being dumped. + */ + for (i = 0; i < numTables; i++) + { + TableInfo *seqinfo = &tblinfo[i]; + TableInfo *owning_tab; + + if (!OidIsValid(seqinfo->owning_tab)) + continue; /* not an owned sequence */ + + owning_tab = findTableByOid(seqinfo->owning_tab); + if (owning_tab == NULL) + pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found", + seqinfo->owning_tab, seqinfo->dobj.catId.oid); + + /* + * For an identity sequence, dump exactly the same components for the + * sequence as for the owning table. This is important because we + * treat the identity sequence as an integral part of the table. For + * example, there is not any DDL command that allows creation of such + * a sequence independently of the table. + * + * For other owned sequences such as serial sequences, we need to dump + * the components that are being dumped for the table and any + * components that the sequence is explicitly marked with. + * + * We can't simply use the set of components which are being dumped + * for the table as the table might be in an extension (and only the + * non-extension components, eg: ACLs if changed, security labels, and + * policies, are being dumped) while the sequence is not (and + * therefore the definition and other components should also be + * dumped). + * + * If the sequence is part of the extension then it should be properly + * marked by checkExtensionMembership() and this will be a no-op as + * the table will be equivalently marked. + */ + if (seqinfo->is_identity_sequence) + seqinfo->dobj.dump = owning_tab->dobj.dump; + else + seqinfo->dobj.dump |= owning_tab->dobj.dump; + + /* Make sure that necessary data is available if we're dumping it */ + if (seqinfo->dobj.dump != DUMP_COMPONENT_NONE) + { + seqinfo->interesting = true; + owning_tab->interesting = true; + } + } +} + +/* + * getInherits + * read all the inheritance information + * from the system catalogs return them in the InhInfo* structure + * + * numInherits is set to the number of pairs read in + */ +InhInfo * +getInherits(Archive *fout, int *numInherits) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + InhInfo *inhinfo; + + int i_inhrelid; + int i_inhparent; + + /* find all the inheritance information */ + appendPQExpBufferStr(query, "SELECT inhrelid, inhparent FROM pg_inherits"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + *numInherits = ntups; + + inhinfo = pg_malloc_array(InhInfo, ntups); + + i_inhrelid = PQfnumber(res, "inhrelid"); + i_inhparent = PQfnumber(res, "inhparent"); + + for (i = 0; i < ntups; i++) + { + inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid)); + inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent)); + } + + PQclear(res); + + destroyPQExpBuffer(query); + + return inhinfo; +} + +/* + * getPartitioningInfo + * get information about partitioning + * + * For the most part, we only collect partitioning info about tables we + * intend to dump. However, this function has to consider all partitioned + * tables in the database, because we need to know about parents of partitions + * we are going to dump even if the parents themselves won't be dumped. + * + * Specifically, what we need to know is whether each partitioned table + * has an "unsafe" partitioning scheme that requires us to force + * load-via-partition-root mode for its children. Currently the only case + * for which we force that is hash partitioning on enum columns, since the + * hash codes depend on enum value OIDs which won't be replicated across + * dump-and-reload. There are other cases in which load-via-partition-root + * might be necessary, but we expect users to cope with them. + */ +void +getPartitioningInfo(Archive *fout) +{ + PQExpBuffer query; + PGresult *res; + int ntups; + + /* hash partitioning didn't exist before v11 */ + if (fout->remoteVersion < 110000) + return; + /* needn't bother if not dumping data */ + if (!fout->dopt->dumpData) + return; + + query = createPQExpBuffer(); + + /* + * Unsafe partitioning schemes are exactly those for which hash enum_ops + * appears among the partition opclasses. We needn't check partstrat. + * + * Note that this query may well retrieve info about tables we aren't + * going to dump and hence have no lock on. That's okay since we need not + * invoke any unsafe server-side functions. + */ + appendPQExpBufferStr(query, + "SELECT partrelid FROM pg_partitioned_table WHERE\n" + "(SELECT c.oid FROM pg_opclass c JOIN pg_am a " + "ON c.opcmethod = a.oid\n" + "WHERE opcname = 'enum_ops' " + "AND opcnamespace = 'pg_catalog'::regnamespace " + "AND amname = 'hash') = ANY(partclass)"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + for (int i = 0; i < ntups; i++) + { + Oid tabrelid = atooid(PQgetvalue(res, i, 0)); + TableInfo *tbinfo; + + tbinfo = findTableByOid(tabrelid); + if (tbinfo == NULL) + pg_fatal("failed sanity check, table OID %u appearing in pg_partitioned_table not found", + tabrelid); + tbinfo->unsafe_partitions = true; + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getIndexes + * get information about every index on a dumpable table + * + * Note: index data is not returned directly to the caller, but it + * does get entered into the DumpableObject tables. + */ +void +getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) +{ + PQExpBuffer query = createPQExpBuffer(); + PQExpBuffer tbloids = createPQExpBuffer(); + PGresult *res; + int ntups; + int curtblindx; + IndxInfo *indxinfo; + int i_tableoid, + i_oid, + i_indrelid, + i_indexname, + i_relpages, + i_reltuples, + i_relallvisible, + i_relallfrozen, + i_parentidx, + i_indexdef, + i_indnkeyatts, + i_indnatts, + i_indkey, + i_indisclustered, + i_indisreplident, + i_indnullsnotdistinct, + i_contype, + i_conname, + i_condeferrable, + i_condeferred, + i_conperiod, + i_contableoid, + i_conoid, + i_condef, + i_indattnames, + i_tablespace, + i_indreloptions, + i_indstatcols, + i_indstatvals; + + /* + * We want to perform just one query against pg_index. However, we + * mustn't try to select every row of the catalog and then sort it out on + * the client side, because some of the server-side functions we need + * would be unsafe to apply to tables we don't have lock on. Hence, we + * build an array of the OIDs of tables we care about (and now have lock + * on!), and use a WHERE clause to constrain which rows are selected. + */ + appendPQExpBufferChar(tbloids, '{'); + for (int i = 0; i < numTables; i++) + { + TableInfo *tbinfo = &tblinfo[i]; + + if (!tbinfo->hasindex) + continue; + + /* + * We can ignore indexes of uninteresting tables. + */ + if (!tbinfo->interesting) + continue; + + /* OK, we need info for this table */ + if (tbloids->len > 1) /* do we have more than the '{'? */ + appendPQExpBufferChar(tbloids, ','); + appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); + } + appendPQExpBufferChar(tbloids, '}'); + + appendPQExpBufferStr(query, + "SELECT t.tableoid, t.oid, i.indrelid, " + "t.relname AS indexname, " + "t.relpages, t.reltuples, t.relallvisible, "); + + if (fout->remoteVersion >= 180000) + appendPQExpBufferStr(query, "t.relallfrozen, "); + else + appendPQExpBufferStr(query, "0 AS relallfrozen, "); + + appendPQExpBufferStr(query, + "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, " + "i.indkey, i.indisclustered, " + "c.contype, c.conname, " + "c.condeferrable, c.condeferred, " + "c.tableoid AS contableoid, " + "c.oid AS conoid, " + "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, " + "CASE WHEN i.indexprs IS NOT NULL THEN " + "(SELECT pg_catalog.array_agg(attname ORDER BY attnum)" + " FROM pg_catalog.pg_attribute " + " WHERE attrelid = i.indexrelid) " + "ELSE NULL END AS indattnames, " + "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, " + "t.reloptions AS indreloptions, "); + + + if (fout->remoteVersion >= 90400) + appendPQExpBufferStr(query, + "i.indisreplident, "); + else + appendPQExpBufferStr(query, + "false AS indisreplident, "); + + if (fout->remoteVersion >= 110000) + appendPQExpBufferStr(query, + "inh.inhparent AS parentidx, " + "i.indnkeyatts AS indnkeyatts, " + "i.indnatts AS indnatts, " + "(SELECT pg_catalog.array_agg(attnum ORDER BY attnum) " + " FROM pg_catalog.pg_attribute " + " WHERE attrelid = i.indexrelid AND " + " attstattarget >= 0) AS indstatcols, " + "(SELECT pg_catalog.array_agg(attstattarget ORDER BY attnum) " + " FROM pg_catalog.pg_attribute " + " WHERE attrelid = i.indexrelid AND " + " attstattarget >= 0) AS indstatvals, "); + else + appendPQExpBufferStr(query, + "0 AS parentidx, " + "i.indnatts AS indnkeyatts, " + "i.indnatts AS indnatts, " + "'' AS indstatcols, " + "'' AS indstatvals, "); + + if (fout->remoteVersion >= 150000) + appendPQExpBufferStr(query, + "i.indnullsnotdistinct, "); + else + appendPQExpBufferStr(query, + "false AS indnullsnotdistinct, "); + + if (fout->remoteVersion >= 180000) + appendPQExpBufferStr(query, + "c.conperiod "); + else + appendPQExpBufferStr(query, + "NULL AS conperiod "); + + /* + * The point of the messy-looking outer join is to find a constraint that + * is related by an internal dependency link to the index. If we find one, + * create a CONSTRAINT entry linked to the INDEX entry. We assume an + * index won't have more than one internal dependency. + * + * Note: the check on conrelid is redundant, but useful because that + * column is indexed while conindid is not. + */ + if (fout->remoteVersion >= 110000) + { + appendPQExpBuffer(query, + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) " + "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " + "JOIN pg_catalog.pg_class t2 ON (t2.oid = i.indrelid) " + "LEFT JOIN pg_catalog.pg_constraint c " + "ON (i.indrelid = c.conrelid AND " + "i.indexrelid = c.conindid AND " + "c.contype IN ('p','u','x')) " + "LEFT JOIN pg_catalog.pg_inherits inh " + "ON (inh.inhrelid = indexrelid) " + "WHERE (i.indisvalid OR t2.relkind = 'p') " + "AND i.indisready " + "ORDER BY i.indrelid, indexname", + tbloids->data); + } + else + { + /* + * the test on indisready is necessary in 9.2, and harmless in + * earlier/later versions + */ + appendPQExpBuffer(query, + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_index i ON (src.tbloid = i.indrelid) " + "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) " + "LEFT JOIN pg_catalog.pg_constraint c " + "ON (i.indrelid = c.conrelid AND " + "i.indexrelid = c.conindid AND " + "c.contype IN ('p','u','x')) " + "WHERE i.indisvalid AND i.indisready " + "ORDER BY i.indrelid, indexname", + tbloids->data); + } + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_indrelid = PQfnumber(res, "indrelid"); + i_indexname = PQfnumber(res, "indexname"); + i_relpages = PQfnumber(res, "relpages"); + i_reltuples = PQfnumber(res, "reltuples"); + i_relallvisible = PQfnumber(res, "relallvisible"); + i_relallfrozen = PQfnumber(res, "relallfrozen"); + i_parentidx = PQfnumber(res, "parentidx"); + i_indexdef = PQfnumber(res, "indexdef"); + i_indnkeyatts = PQfnumber(res, "indnkeyatts"); + i_indnatts = PQfnumber(res, "indnatts"); + i_indkey = PQfnumber(res, "indkey"); + i_indisclustered = PQfnumber(res, "indisclustered"); + i_indisreplident = PQfnumber(res, "indisreplident"); + i_indnullsnotdistinct = PQfnumber(res, "indnullsnotdistinct"); + i_contype = PQfnumber(res, "contype"); + i_conname = PQfnumber(res, "conname"); + i_condeferrable = PQfnumber(res, "condeferrable"); + i_condeferred = PQfnumber(res, "condeferred"); + i_conperiod = PQfnumber(res, "conperiod"); + i_contableoid = PQfnumber(res, "contableoid"); + i_conoid = PQfnumber(res, "conoid"); + i_condef = PQfnumber(res, "condef"); + i_indattnames = PQfnumber(res, "indattnames"); + i_tablespace = PQfnumber(res, "tablespace"); + i_indreloptions = PQfnumber(res, "indreloptions"); + i_indstatcols = PQfnumber(res, "indstatcols"); + i_indstatvals = PQfnumber(res, "indstatvals"); + + indxinfo = pg_malloc_array(IndxInfo, ntups); + + /* + * Outer loop iterates once per table, not once per row. Incrementing of + * j is handled by the inner loop. + */ + curtblindx = -1; + for (int j = 0; j < ntups;) + { + Oid indrelid = atooid(PQgetvalue(res, j, i_indrelid)); + TableInfo *tbinfo = NULL; + char **indAttNames = NULL; + int nindAttNames = 0; + int numinds; + + /* Count rows for this table */ + for (numinds = 1; numinds < ntups - j; numinds++) + if (atooid(PQgetvalue(res, j + numinds, i_indrelid)) != indrelid) + break; + + /* + * Locate the associated TableInfo; we rely on tblinfo[] being in OID + * order. + */ + while (++curtblindx < numTables) + { + tbinfo = &tblinfo[curtblindx]; + if (tbinfo->dobj.catId.oid == indrelid) + break; + } + if (curtblindx >= numTables) + pg_fatal("unrecognized table OID %u", indrelid); + /* cross-check that we only got requested tables */ + if (!tbinfo->hasindex || + !tbinfo->interesting) + pg_fatal("unexpected index data for table \"%s\"", + tbinfo->dobj.name); + + /* Save data for this table */ + tbinfo->indexes = indxinfo + j; + tbinfo->numIndexes = numinds; + + for (int c = 0; c < numinds; c++, j++) + { + char contype; + char indexkind; + RelStatsInfo *relstats; + int32 relpages = atoi(PQgetvalue(res, j, i_relpages)); + int32 relallvisible = atoi(PQgetvalue(res, j, i_relallvisible)); + int32 relallfrozen = atoi(PQgetvalue(res, j, i_relallfrozen)); + + indxinfo[j].dobj.objType = DO_INDEX; + indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid)); + indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); + AssignDumpId(&indxinfo[j].dobj); + indxinfo[j].dobj.dump = tbinfo->dobj.dump; + indxinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_indexname)); + indxinfo[j].dobj.namespace = tbinfo->dobj.namespace; + indxinfo[j].indextable = tbinfo; + indxinfo[j].indexdef = pg_strdup(PQgetvalue(res, j, i_indexdef)); + indxinfo[j].indnkeyattrs = atoi(PQgetvalue(res, j, i_indnkeyatts)); + indxinfo[j].indnattrs = atoi(PQgetvalue(res, j, i_indnatts)); + indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace)); + indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions)); + indxinfo[j].indstatcols = pg_strdup(PQgetvalue(res, j, i_indstatcols)); + indxinfo[j].indstatvals = pg_strdup(PQgetvalue(res, j, i_indstatvals)); + indxinfo[j].indkeys = pg_malloc_array(Oid, indxinfo[j].indnattrs); + parseOidArray(PQgetvalue(res, j, i_indkey), + indxinfo[j].indkeys, indxinfo[j].indnattrs); + indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't'); + indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't'); + indxinfo[j].indnullsnotdistinct = (PQgetvalue(res, j, i_indnullsnotdistinct)[0] == 't'); + indxinfo[j].parentidx = atooid(PQgetvalue(res, j, i_parentidx)); + indxinfo[j].partattaches = (SimplePtrList) + { + NULL, NULL + }; + + if (indxinfo[j].parentidx == 0) + indexkind = RELKIND_INDEX; + else + indexkind = RELKIND_PARTITIONED_INDEX; + + if (!PQgetisnull(res, j, i_indattnames)) + { + if (!parsePGArray(PQgetvalue(res, j, i_indattnames), + &indAttNames, &nindAttNames)) + pg_fatal("could not parse %s array", "indattnames"); + } + + relstats = getRelationStatistics(fout, &indxinfo[j].dobj, relpages, + PQgetvalue(res, j, i_reltuples), + relallvisible, relallfrozen, indexkind, + indAttNames, nindAttNames); + + contype = *(PQgetvalue(res, j, i_contype)); + if (contype == 'p' || contype == 'u' || contype == 'x') + { + /* + * If we found a constraint matching the index, create an + * entry for it. + */ + ConstraintInfo *constrinfo; + + constrinfo = pg_malloc_object(ConstraintInfo); + constrinfo->dobj.objType = DO_CONSTRAINT; + constrinfo->dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid)); + constrinfo->dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid)); + AssignDumpId(&constrinfo->dobj); + constrinfo->dobj.dump = tbinfo->dobj.dump; + constrinfo->dobj.name = pg_strdup(PQgetvalue(res, j, i_conname)); + constrinfo->dobj.namespace = tbinfo->dobj.namespace; + constrinfo->contable = tbinfo; + constrinfo->condomain = NULL; + constrinfo->contype = contype; + if (contype == 'x') + constrinfo->condef = pg_strdup(PQgetvalue(res, j, i_condef)); + else + constrinfo->condef = NULL; + constrinfo->confrelid = InvalidOid; + constrinfo->conindex = indxinfo[j].dobj.dumpId; + constrinfo->condeferrable = *(PQgetvalue(res, j, i_condeferrable)) == 't'; + constrinfo->condeferred = *(PQgetvalue(res, j, i_condeferred)) == 't'; + constrinfo->conperiod = *(PQgetvalue(res, j, i_conperiod)) == 't'; + constrinfo->conislocal = true; + constrinfo->separate = true; + + indxinfo[j].indexconstraint = constrinfo->dobj.dumpId; + if (relstats != NULL) + addObjectDependency(&relstats->dobj, constrinfo->dobj.dumpId); + } + else + { + /* Plain secondary index */ + indxinfo[j].indexconstraint = 0; + } + } + } + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(tbloids); +} + +/* + * getExtendedStatistics + * get information about extended-statistics objects. + * + * Note: extended statistics data is not returned directly to the caller, but + * it does get entered into the DumpableObject tables. + */ +void +getExtendedStatistics(Archive *fout) +{ + PQExpBuffer query; + PGresult *res; + StatsExtInfo *statsextinfo; + int ntups; + int i_tableoid; + int i_oid; + int i_stxname; + int i_stxnamespace; + int i_stxowner; + int i_stxrelid; + int i_stattarget; + int i; + + /* Extended statistics were new in v10 */ + if (fout->remoteVersion < 100000) + return; + + query = createPQExpBuffer(); + + if (fout->remoteVersion < 130000) + appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, " + "stxnamespace, stxowner, stxrelid, NULL AS stxstattarget " + "FROM pg_catalog.pg_statistic_ext"); + else + appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, " + "stxnamespace, stxowner, stxrelid, stxstattarget " + "FROM pg_catalog.pg_statistic_ext"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_stxname = PQfnumber(res, "stxname"); + i_stxnamespace = PQfnumber(res, "stxnamespace"); + i_stxowner = PQfnumber(res, "stxowner"); + i_stxrelid = PQfnumber(res, "stxrelid"); + i_stattarget = PQfnumber(res, "stxstattarget"); + + statsextinfo = pg_malloc_array(StatsExtInfo, ntups); + + for (i = 0; i < ntups; i++) + { + statsextinfo[i].dobj.objType = DO_STATSEXT; + statsextinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + statsextinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&statsextinfo[i].dobj); + statsextinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_stxname)); + statsextinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_stxnamespace))); + statsextinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_stxowner)); + statsextinfo[i].stattable = + findTableByOid(atooid(PQgetvalue(res, i, i_stxrelid))); + if (PQgetisnull(res, i, i_stattarget)) + statsextinfo[i].stattarget = -1; + else + statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget)); + + /* Decide whether we want to dump it */ + selectDumpableStatisticsObject(&(statsextinfo[i]), fout); + + if (fout->dopt->dumpStatistics) + statsextinfo[i].dobj.components |= DUMP_COMPONENT_STATISTICS; + } + + PQclear(res); + destroyPQExpBuffer(query); +} + +/* + * getConstraints + * + * Get info about constraints on dumpable tables. + * + * Currently handles foreign keys only. + * Unique and primary key constraints are handled with indexes, + * while check constraints are processed in getTableAttrs(). + */ +void +getConstraints(Archive *fout, TableInfo tblinfo[], int numTables) +{ + PQExpBuffer query = createPQExpBuffer(); + PQExpBuffer tbloids = createPQExpBuffer(); + PGresult *res; + int ntups; + int curtblindx; + TableInfo *tbinfo = NULL; + ConstraintInfo *constrinfo; + int i_contableoid, + i_conoid, + i_conrelid, + i_conname, + i_confrelid, + i_conindid, + i_condef; + + /* + * We want to perform just one query against pg_constraint. However, we + * mustn't try to select every row of the catalog and then sort it out on + * the client side, because some of the server-side functions we need + * would be unsafe to apply to tables we don't have lock on. Hence, we + * build an array of the OIDs of tables we care about (and now have lock + * on!), and use a WHERE clause to constrain which rows are selected. + */ + appendPQExpBufferChar(tbloids, '{'); + for (int i = 0; i < numTables; i++) + { + TableInfo *tinfo = &tblinfo[i]; + + if (!(tinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)) + continue; + + /* OK, we need info for this table */ + if (tbloids->len > 1) /* do we have more than the '{'? */ + appendPQExpBufferChar(tbloids, ','); + appendPQExpBuffer(tbloids, "%u", tinfo->dobj.catId.oid); + } + appendPQExpBufferChar(tbloids, '}'); + + appendPQExpBufferStr(query, + "SELECT c.tableoid, c.oid, " + "conrelid, conname, confrelid, "); + if (fout->remoteVersion >= 110000) + appendPQExpBufferStr(query, "conindid, "); + else + appendPQExpBufferStr(query, "0 AS conindid, "); + appendPQExpBuffer(query, + "pg_catalog.pg_get_constraintdef(c.oid) AS condef\n" + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n" + "WHERE contype = 'f' ", + tbloids->data); + if (fout->remoteVersion >= 110000) + appendPQExpBufferStr(query, + "AND conparentid = 0 "); + appendPQExpBufferStr(query, + "ORDER BY conrelid, conname"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_contableoid = PQfnumber(res, "tableoid"); + i_conoid = PQfnumber(res, "oid"); + i_conrelid = PQfnumber(res, "conrelid"); + i_conname = PQfnumber(res, "conname"); + i_confrelid = PQfnumber(res, "confrelid"); + i_conindid = PQfnumber(res, "conindid"); + i_condef = PQfnumber(res, "condef"); + + constrinfo = pg_malloc_array(ConstraintInfo, ntups); + + curtblindx = -1; + for (int j = 0; j < ntups; j++) + { + Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid)); + TableInfo *reftable; + + /* + * Locate the associated TableInfo; we rely on tblinfo[] being in OID + * order. + */ + if (tbinfo == NULL || tbinfo->dobj.catId.oid != conrelid) + { + while (++curtblindx < numTables) + { + tbinfo = &tblinfo[curtblindx]; + if (tbinfo->dobj.catId.oid == conrelid) + break; + } + if (curtblindx >= numTables) + pg_fatal("unrecognized table OID %u", conrelid); + } + + constrinfo[j].dobj.objType = DO_FK_CONSTRAINT; + constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid)); + constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid)); + AssignDumpId(&constrinfo[j].dobj); + constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname)); + constrinfo[j].dobj.namespace = tbinfo->dobj.namespace; + constrinfo[j].contable = tbinfo; + constrinfo[j].condomain = NULL; + constrinfo[j].contype = 'f'; + constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef)); + constrinfo[j].confrelid = atooid(PQgetvalue(res, j, i_confrelid)); + constrinfo[j].conindex = 0; + constrinfo[j].condeferrable = false; + constrinfo[j].condeferred = false; + constrinfo[j].conislocal = true; + constrinfo[j].separate = true; + + /* + * Restoring an FK that points to a partitioned table requires that + * all partition indexes have been attached beforehand. Ensure that + * happens by making the constraint depend on each index partition + * attach object. + */ + reftable = findTableByOid(constrinfo[j].confrelid); + if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE) + { + Oid indexOid = atooid(PQgetvalue(res, j, i_conindid)); + + if (indexOid != InvalidOid) + { + for (int k = 0; k < reftable->numIndexes; k++) + { + IndxInfo *refidx; + + /* not our index? */ + if (reftable->indexes[k].dobj.catId.oid != indexOid) + continue; + + refidx = &reftable->indexes[k]; + addConstrChildIdxDeps(&constrinfo[j].dobj, refidx); + break; + } + } + } + } + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(tbloids); +} + +/* + * addConstrChildIdxDeps + * + * Recursive subroutine for getConstraints + * + * Given an object representing a foreign key constraint and an index on the + * partitioned table it references, mark the constraint object as dependent + * on the DO_INDEX_ATTACH object of each index partition, recursively + * drilling down to their partitions if any. This ensures that the FK is not + * restored until the index is fully marked valid. + */ +static void +addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx) +{ + SimplePtrListCell *cell; + + Assert(dobj->objType == DO_FK_CONSTRAINT); + + for (cell = refidx->partattaches.head; cell; cell = cell->next) + { + IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr; + + addObjectDependency(dobj, attach->dobj.dumpId); + + if (attach->partitionIdx->partattaches.head != NULL) + addConstrChildIdxDeps(dobj, attach->partitionIdx); + } +} + +/* + * getDomainConstraints + * + * Get info about constraints on a domain. + */ +static void +getDomainConstraints(Archive *fout, TypeInfo *tyinfo) +{ + ConstraintInfo *constrinfo; + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + int i_tableoid, + i_oid, + i_conname, + i_consrc, + i_convalidated, + i_contype; + int ntups; + + if (!fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS]) + { + /* + * Set up query for constraint-specific details. For servers 17 and + * up, domains have constraints of type 'n' as well as 'c', otherwise + * just the latter. + */ + appendPQExpBuffer(query, + "PREPARE getDomainConstraints(pg_catalog.oid) AS\n" + "SELECT tableoid, oid, conname, " + "pg_catalog.pg_get_constraintdef(oid) AS consrc, " + "convalidated, contype " + "FROM pg_catalog.pg_constraint " + "WHERE contypid = $1 AND contype IN (%s) " + "ORDER BY conname", + fout->remoteVersion < 170000 ? "'c'" : "'c', 'n'"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_GETDOMAINCONSTRAINTS] = true; + } + + printfPQExpBuffer(query, + "EXECUTE getDomainConstraints('%u')", + tyinfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_conname = PQfnumber(res, "conname"); + i_consrc = PQfnumber(res, "consrc"); + i_convalidated = PQfnumber(res, "convalidated"); + i_contype = PQfnumber(res, "contype"); + + constrinfo = pg_malloc_array(ConstraintInfo, ntups); + tyinfo->domChecks = constrinfo; + + /* 'i' tracks result rows; 'j' counts CHECK constraints */ + for (int i = 0, j = 0; i < ntups; i++) + { + bool validated = PQgetvalue(res, i, i_convalidated)[0] == 't'; + char contype = (PQgetvalue(res, i, i_contype))[0]; + ConstraintInfo *constraint; + + if (contype == CONSTRAINT_CHECK) + { + constraint = &constrinfo[j++]; + tyinfo->nDomChecks++; + } + else + { + Assert(contype == CONSTRAINT_NOTNULL); + Assert(tyinfo->notnull == NULL); + /* use last item in array for the not-null constraint */ + tyinfo->notnull = &(constrinfo[ntups - 1]); + constraint = tyinfo->notnull; + } + + constraint->dobj.objType = DO_CONSTRAINT; + constraint->dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + constraint->dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&(constraint->dobj)); + constraint->dobj.name = pg_strdup(PQgetvalue(res, i, i_conname)); + constraint->dobj.namespace = tyinfo->dobj.namespace; + constraint->contable = NULL; + constraint->condomain = tyinfo; + constraint->contype = contype; + constraint->condef = pg_strdup(PQgetvalue(res, i, i_consrc)); + constraint->confrelid = InvalidOid; + constraint->conindex = 0; + constraint->condeferrable = false; + constraint->condeferred = false; + constraint->conislocal = true; + + constraint->separate = !validated; + + /* + * Make the domain depend on the constraint, ensuring it won't be + * output till any constraint dependencies are OK. If the constraint + * has not been validated, it's going to be dumped after the domain + * anyway, so this doesn't matter. + */ + if (validated) + addObjectDependency(&tyinfo->dobj, constraint->dobj.dumpId); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getRules + * get basic information about every rule in the system + */ +void +getRules(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + RuleInfo *ruleinfo; + int i_tableoid; + int i_oid; + int i_rulename; + int i_ruletable; + int i_ev_type; + int i_is_instead; + int i_ev_enabled; + + appendPQExpBufferStr(query, "SELECT " + "tableoid, oid, rulename, " + "ev_class AS ruletable, ev_type, is_instead, " + "ev_enabled " + "FROM pg_rewrite " + "ORDER BY oid"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + ruleinfo = pg_malloc_array(RuleInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_rulename = PQfnumber(res, "rulename"); + i_ruletable = PQfnumber(res, "ruletable"); + i_ev_type = PQfnumber(res, "ev_type"); + i_is_instead = PQfnumber(res, "is_instead"); + i_ev_enabled = PQfnumber(res, "ev_enabled"); + + for (i = 0; i < ntups; i++) + { + Oid ruletableoid; + + ruleinfo[i].dobj.objType = DO_RULE; + ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&ruleinfo[i].dobj); + ruleinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_rulename)); + ruletableoid = atooid(PQgetvalue(res, i, i_ruletable)); + ruleinfo[i].ruletable = findTableByOid(ruletableoid); + if (ruleinfo[i].ruletable == NULL) + pg_fatal("failed sanity check, parent table with OID %u of pg_rewrite entry with OID %u not found", + ruletableoid, ruleinfo[i].dobj.catId.oid); + ruleinfo[i].dobj.namespace = ruleinfo[i].ruletable->dobj.namespace; + ruleinfo[i].dobj.dump = ruleinfo[i].ruletable->dobj.dump; + ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type)); + ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't'; + ruleinfo[i].ev_enabled = *(PQgetvalue(res, i, i_ev_enabled)); + if (ruleinfo[i].ruletable) + { + /* + * If the table is a view or materialized view, force its ON + * SELECT rule to be sorted before the view itself --- this + * ensures that any dependencies for the rule affect the table's + * positioning. Other rules are forced to appear after their + * table. + */ + if ((ruleinfo[i].ruletable->relkind == RELKIND_VIEW || + ruleinfo[i].ruletable->relkind == RELKIND_MATVIEW) && + ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead) + { + addObjectDependency(&ruleinfo[i].ruletable->dobj, + ruleinfo[i].dobj.dumpId); + /* We'll merge the rule into CREATE VIEW, if possible */ + ruleinfo[i].separate = false; + } + else + { + addObjectDependency(&ruleinfo[i].dobj, + ruleinfo[i].ruletable->dobj.dumpId); + ruleinfo[i].separate = true; + } + } + else + ruleinfo[i].separate = true; + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getTriggers + * get information about every trigger on a dumpable table + * + * Note: trigger data is not returned directly to the caller, but it + * does get entered into the DumpableObject tables. + */ +void +getTriggers(Archive *fout, TableInfo tblinfo[], int numTables) +{ + PQExpBuffer query = createPQExpBuffer(); + PQExpBuffer tbloids = createPQExpBuffer(); + PGresult *res; + int ntups; + int curtblindx; + TriggerInfo *tginfo; + int i_tableoid, + i_oid, + i_tgrelid, + i_tgname, + i_tgenabled, + i_tgispartition, + i_tgdef; + + /* + * We want to perform just one query against pg_trigger. However, we + * mustn't try to select every row of the catalog and then sort it out on + * the client side, because some of the server-side functions we need + * would be unsafe to apply to tables we don't have lock on. Hence, we + * build an array of the OIDs of tables we care about (and now have lock + * on!), and use a WHERE clause to constrain which rows are selected. + */ + appendPQExpBufferChar(tbloids, '{'); + for (int i = 0; i < numTables; i++) + { + TableInfo *tbinfo = &tblinfo[i]; + + if (!tbinfo->hastriggers || + !(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)) + continue; + + /* OK, we need info for this table */ + if (tbloids->len > 1) /* do we have more than the '{'? */ + appendPQExpBufferChar(tbloids, ','); + appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); + } + appendPQExpBufferChar(tbloids, '}'); + + if (fout->remoteVersion >= 150000) + { + /* + * NB: think not to use pretty=true in pg_get_triggerdef. It could + * result in non-forward-compatible dumps of WHEN clauses due to + * under-parenthesization. + * + * NB: We need to see partition triggers in case the tgenabled flag + * has been changed from the parent. + */ + appendPQExpBuffer(query, + "SELECT t.tgrelid, t.tgname, " + "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, " + "t.tgenabled, t.tableoid, t.oid, " + "t.tgparentid <> 0 AS tgispartition\n" + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) " + "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) " + "WHERE ((NOT t.tgisinternal AND t.tgparentid = 0) " + "OR t.tgenabled != u.tgenabled) " + "ORDER BY t.tgrelid, t.tgname", + tbloids->data); + } + else if (fout->remoteVersion >= 130000) + { + /* + * NB: think not to use pretty=true in pg_get_triggerdef. It could + * result in non-forward-compatible dumps of WHEN clauses due to + * under-parenthesization. + * + * NB: We need to see tgisinternal triggers in partitions, in case the + * tgenabled flag has been changed from the parent. + */ + appendPQExpBuffer(query, + "SELECT t.tgrelid, t.tgname, " + "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, " + "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition\n" + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) " + "LEFT JOIN pg_catalog.pg_trigger u ON (u.oid = t.tgparentid) " + "WHERE (NOT t.tgisinternal OR t.tgenabled != u.tgenabled) " + "ORDER BY t.tgrelid, t.tgname", + tbloids->data); + } + else if (fout->remoteVersion >= 110000) + { + /* + * NB: We need to see tgisinternal triggers in partitions, in case the + * tgenabled flag has been changed from the parent. No tgparentid in + * version 11-12, so we have to match them via pg_depend. + * + * See above about pretty=true in pg_get_triggerdef. + */ + appendPQExpBuffer(query, + "SELECT t.tgrelid, t.tgname, " + "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, " + "t.tgenabled, t.tableoid, t.oid, t.tgisinternal as tgispartition " + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) " + "LEFT JOIN pg_catalog.pg_depend AS d ON " + " d.classid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND " + " d.refclassid = 'pg_catalog.pg_trigger'::pg_catalog.regclass AND " + " d.objid = t.oid " + "LEFT JOIN pg_catalog.pg_trigger AS pt ON pt.oid = refobjid " + "WHERE (NOT t.tgisinternal OR t.tgenabled != pt.tgenabled) " + "ORDER BY t.tgrelid, t.tgname", + tbloids->data); + } + else + { + /* See above about pretty=true in pg_get_triggerdef */ + appendPQExpBuffer(query, + "SELECT t.tgrelid, t.tgname, " + "pg_catalog.pg_get_triggerdef(t.oid, false) AS tgdef, " + "t.tgenabled, false as tgispartition, " + "t.tableoid, t.oid " + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_trigger t ON (src.tbloid = t.tgrelid) " + "WHERE NOT tgisinternal " + "ORDER BY t.tgrelid, t.tgname", + tbloids->data); + } + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_tgrelid = PQfnumber(res, "tgrelid"); + i_tgname = PQfnumber(res, "tgname"); + i_tgenabled = PQfnumber(res, "tgenabled"); + i_tgispartition = PQfnumber(res, "tgispartition"); + i_tgdef = PQfnumber(res, "tgdef"); + + tginfo = pg_malloc_array(TriggerInfo, ntups); + + /* + * Outer loop iterates once per table, not once per row. Incrementing of + * j is handled by the inner loop. + */ + curtblindx = -1; + for (int j = 0; j < ntups;) + { + Oid tgrelid = atooid(PQgetvalue(res, j, i_tgrelid)); + TableInfo *tbinfo = NULL; + int numtrigs; + + /* Count rows for this table */ + for (numtrigs = 1; numtrigs < ntups - j; numtrigs++) + if (atooid(PQgetvalue(res, j + numtrigs, i_tgrelid)) != tgrelid) + break; + + /* + * Locate the associated TableInfo; we rely on tblinfo[] being in OID + * order. + */ + while (++curtblindx < numTables) + { + tbinfo = &tblinfo[curtblindx]; + if (tbinfo->dobj.catId.oid == tgrelid) + break; + } + if (curtblindx >= numTables) + pg_fatal("unrecognized table OID %u", tgrelid); + + /* Save data for this table */ + tbinfo->triggers = tginfo + j; + tbinfo->numTriggers = numtrigs; + + for (int c = 0; c < numtrigs; c++, j++) + { + tginfo[j].dobj.objType = DO_TRIGGER; + tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid)); + tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); + AssignDumpId(&tginfo[j].dobj); + tginfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_tgname)); + tginfo[j].dobj.namespace = tbinfo->dobj.namespace; + tginfo[j].tgtable = tbinfo; + tginfo[j].tgenabled = *(PQgetvalue(res, j, i_tgenabled)); + tginfo[j].tgispartition = *(PQgetvalue(res, j, i_tgispartition)) == 't'; + tginfo[j].tgdef = pg_strdup(PQgetvalue(res, j, i_tgdef)); + } + } + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(tbloids); +} + +/* + * getEventTriggers + * get information about event triggers + */ +void +getEventTriggers(Archive *fout) +{ + int i; + PQExpBuffer query; + PGresult *res; + EventTriggerInfo *evtinfo; + int i_tableoid, + i_oid, + i_evtname, + i_evtevent, + i_evtowner, + i_evttags, + i_evtfname, + i_evtenabled; + int ntups; + + /* Before 9.3, there are no event triggers */ + if (fout->remoteVersion < 90300) + return; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, + "SELECT e.tableoid, e.oid, evtname, evtenabled, " + "evtevent, evtowner, " + "array_to_string(array(" + "select quote_literal(x) " + " from unnest(evttags) as t(x)), ', ') as evttags, " + "e.evtfoid::regproc as evtfname " + "FROM pg_event_trigger e " + "ORDER BY e.oid"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + evtinfo = pg_malloc_array(EventTriggerInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_evtname = PQfnumber(res, "evtname"); + i_evtevent = PQfnumber(res, "evtevent"); + i_evtowner = PQfnumber(res, "evtowner"); + i_evttags = PQfnumber(res, "evttags"); + i_evtfname = PQfnumber(res, "evtfname"); + i_evtenabled = PQfnumber(res, "evtenabled"); + + for (i = 0; i < ntups; i++) + { + evtinfo[i].dobj.objType = DO_EVENT_TRIGGER; + evtinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + evtinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&evtinfo[i].dobj); + evtinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_evtname)); + evtinfo[i].evtname = pg_strdup(PQgetvalue(res, i, i_evtname)); + evtinfo[i].evtevent = pg_strdup(PQgetvalue(res, i, i_evtevent)); + evtinfo[i].evtowner = getRoleName(PQgetvalue(res, i, i_evtowner)); + evtinfo[i].evttags = pg_strdup(PQgetvalue(res, i, i_evttags)); + evtinfo[i].evtfname = pg_strdup(PQgetvalue(res, i, i_evtfname)); + evtinfo[i].evtenabled = *(PQgetvalue(res, i, i_evtenabled)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(evtinfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getProcLangs + * get basic information about every procedural language in the system + * + * NB: this must run after getFuncs() because we assume we can do + * findFuncByOid(). + */ +void +getProcLangs(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + ProcLangInfo *planginfo; + int i_tableoid; + int i_oid; + int i_lanname; + int i_lanpltrusted; + int i_lanplcallfoid; + int i_laninline; + int i_lanvalidator; + int i_lanacl; + int i_acldefault; + int i_lanowner; + + appendPQExpBufferStr(query, "SELECT tableoid, oid, " + "lanname, lanpltrusted, lanplcallfoid, " + "laninline, lanvalidator, " + "lanacl, " + "acldefault('l', lanowner) AS acldefault, " + "lanowner " + "FROM pg_language " + "WHERE lanispl " + "ORDER BY oid"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + planginfo = pg_malloc_array(ProcLangInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_lanname = PQfnumber(res, "lanname"); + i_lanpltrusted = PQfnumber(res, "lanpltrusted"); + i_lanplcallfoid = PQfnumber(res, "lanplcallfoid"); + i_laninline = PQfnumber(res, "laninline"); + i_lanvalidator = PQfnumber(res, "lanvalidator"); + i_lanacl = PQfnumber(res, "lanacl"); + i_acldefault = PQfnumber(res, "acldefault"); + i_lanowner = PQfnumber(res, "lanowner"); + + for (i = 0; i < ntups; i++) + { + planginfo[i].dobj.objType = DO_PROCLANG; + planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&planginfo[i].dobj); + + planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname)); + planginfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_lanacl)); + planginfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + planginfo[i].dacl.privtype = 0; + planginfo[i].dacl.initprivs = NULL; + planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't'; + planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid)); + planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline)); + planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator)); + planginfo[i].lanowner = getRoleName(PQgetvalue(res, i, i_lanowner)); + + /* Decide whether we want to dump it */ + selectDumpableProcLang(&(planginfo[i]), fout); + + /* Mark whether language has an ACL */ + if (!PQgetisnull(res, i, i_lanacl)) + planginfo[i].dobj.components |= DUMP_COMPONENT_ACL; + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getCasts + * get basic information about most casts in the system + * + * Skip casts from a range to its multirange, since we'll create those + * automatically. + */ +void +getCasts(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query = createPQExpBuffer(); + CastInfo *castinfo; + int i_tableoid; + int i_oid; + int i_castsource; + int i_casttarget; + int i_castfunc; + int i_castcontext; + int i_castmethod; + + if (fout->remoteVersion >= 140000) + { + appendPQExpBufferStr(query, "SELECT tableoid, oid, " + "castsource, casttarget, castfunc, castcontext, " + "castmethod " + "FROM pg_cast c " + "WHERE NOT EXISTS ( " + "SELECT 1 FROM pg_range r " + "WHERE c.castsource = r.rngtypid " + "AND c.casttarget = r.rngmultitypid " + ") " + "ORDER BY 3,4"); + } + else + { + appendPQExpBufferStr(query, "SELECT tableoid, oid, " + "castsource, casttarget, castfunc, castcontext, " + "castmethod " + "FROM pg_cast ORDER BY 3,4"); + } + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + castinfo = pg_malloc_array(CastInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_castsource = PQfnumber(res, "castsource"); + i_casttarget = PQfnumber(res, "casttarget"); + i_castfunc = PQfnumber(res, "castfunc"); + i_castcontext = PQfnumber(res, "castcontext"); + i_castmethod = PQfnumber(res, "castmethod"); + + for (i = 0; i < ntups; i++) + { + PQExpBufferData namebuf; + TypeInfo *sTypeInfo; + TypeInfo *tTypeInfo; + + castinfo[i].dobj.objType = DO_CAST; + castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&castinfo[i].dobj); + castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource)); + castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget)); + castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc)); + castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext)); + castinfo[i].castmethod = *(PQgetvalue(res, i, i_castmethod)); + + /* + * Try to name cast as concatenation of typnames. This is only used + * for purposes of sorting. If we fail to find either type, the name + * will be an empty string. + */ + initPQExpBuffer(&namebuf); + sTypeInfo = findTypeByOid(castinfo[i].castsource); + tTypeInfo = findTypeByOid(castinfo[i].casttarget); + if (sTypeInfo && tTypeInfo) + appendPQExpBuffer(&namebuf, "%s %s", + sTypeInfo->dobj.name, tTypeInfo->dobj.name); + castinfo[i].dobj.name = namebuf.data; + + /* Decide whether we want to dump it */ + selectDumpableCast(&(castinfo[i]), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +static char * +get_language_name(Archive *fout, Oid langid) +{ + PQExpBuffer query; + PGresult *res; + char *lanname; + + query = createPQExpBuffer(); + appendPQExpBuffer(query, "SELECT lanname FROM pg_language WHERE oid = %u", langid); + res = ExecuteSqlQueryForSingleRow(fout, query->data); + lanname = pg_strdup(fmtId(PQgetvalue(res, 0, 0))); + destroyPQExpBuffer(query); + PQclear(res); + + return lanname; +} + +/* + * getTransforms + * get basic information about every transform in the system + */ +void +getTransforms(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + TransformInfo *transforminfo; + int i_tableoid; + int i_oid; + int i_trftype; + int i_trflang; + int i_trffromsql; + int i_trftosql; + + /* Transforms didn't exist pre-9.5 */ + if (fout->remoteVersion < 90500) + return; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "SELECT tableoid, oid, " + "trftype, trflang, trffromsql::oid, trftosql::oid " + "FROM pg_transform " + "ORDER BY 3,4"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + transforminfo = pg_malloc_array(TransformInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_trftype = PQfnumber(res, "trftype"); + i_trflang = PQfnumber(res, "trflang"); + i_trffromsql = PQfnumber(res, "trffromsql"); + i_trftosql = PQfnumber(res, "trftosql"); + + for (i = 0; i < ntups; i++) + { + PQExpBufferData namebuf; + TypeInfo *typeInfo; + char *lanname; + + transforminfo[i].dobj.objType = DO_TRANSFORM; + transforminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + transforminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&transforminfo[i].dobj); + transforminfo[i].trftype = atooid(PQgetvalue(res, i, i_trftype)); + transforminfo[i].trflang = atooid(PQgetvalue(res, i, i_trflang)); + transforminfo[i].trffromsql = atooid(PQgetvalue(res, i, i_trffromsql)); + transforminfo[i].trftosql = atooid(PQgetvalue(res, i, i_trftosql)); + + /* + * Try to name transform as concatenation of type and language name. + * This is only used for purposes of sorting. If we fail to find + * either, the name will be an empty string. + */ + initPQExpBuffer(&namebuf); + typeInfo = findTypeByOid(transforminfo[i].trftype); + lanname = get_language_name(fout, transforminfo[i].trflang); + if (typeInfo && lanname) + appendPQExpBuffer(&namebuf, "%s %s", + typeInfo->dobj.name, lanname); + transforminfo[i].dobj.name = namebuf.data; + free(lanname); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(transforminfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getTableAttrs - + * for each interesting table, read info about its attributes + * (names, types, default values, CHECK constraints, etc) + * + * modifies tblinfo + */ +void +getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q = createPQExpBuffer(); + PQExpBuffer tbloids = createPQExpBuffer(); + PQExpBuffer checkoids = createPQExpBuffer(); + PQExpBuffer invalidnotnulloids = NULL; + PGresult *res; + int ntups; + int curtblindx; + int i_attrelid; + int i_attnum; + int i_attname; + int i_atttypname; + int i_attstattarget; + int i_attstorage; + int i_typstorage; + int i_attidentity; + int i_attgenerated; + int i_attisdropped; + int i_attlen; + int i_attalign; + int i_attislocal; + int i_notnull_name; + int i_notnull_comment; + int i_notnull_noinherit; + int i_notnull_islocal; + int i_notnull_invalidoid; + int i_attoptions; + int i_attcollation; + int i_attcompression; + int i_attfdwoptions; + int i_attmissingval; + int i_atthasdef; + + /* + * We want to perform just one query against pg_attribute, and then just + * one against pg_attrdef (for DEFAULTs) and two against pg_constraint + * (for CHECK constraints and for NOT NULL constraints). However, we + * mustn't try to select every row of those catalogs and then sort it out + * on the client side, because some of the server-side functions we need + * would be unsafe to apply to tables we don't have lock on. Hence, we + * build an array of the OIDs of tables we care about (and now have lock + * on!), and use a WHERE clause to constrain which rows are selected. + */ + appendPQExpBufferChar(tbloids, '{'); + appendPQExpBufferChar(checkoids, '{'); + for (int i = 0; i < numTables; i++) + { + TableInfo *tbinfo = &tblinfo[i]; + + /* Don't bother to collect info for sequences */ + if (tbinfo->relkind == RELKIND_SEQUENCE) + continue; + + /* + * Don't bother with uninteresting tables, either. For binary + * upgrades, this is bypassed for pg_largeobject_metadata and + * pg_shdepend so that the columns names are collected for the + * corresponding COPY commands. Restoring the data for those catalogs + * is faster than restoring the equivalent set of large object + * commands. + */ + if (!tbinfo->interesting && + !(fout->dopt->binary_upgrade && + (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId || + tbinfo->dobj.catId.oid == SharedDependRelationId))) + continue; + + /* OK, we need info for this table */ + if (tbloids->len > 1) /* do we have more than the '{'? */ + appendPQExpBufferChar(tbloids, ','); + appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); + + if (tbinfo->ncheck > 0) + { + /* Also make a list of the ones with check constraints */ + if (checkoids->len > 1) /* do we have more than the '{'? */ + appendPQExpBufferChar(checkoids, ','); + appendPQExpBuffer(checkoids, "%u", tbinfo->dobj.catId.oid); + } + } + appendPQExpBufferChar(tbloids, '}'); + appendPQExpBufferChar(checkoids, '}'); + + /* + * Find all the user attributes and their types. + * + * Since we only want to dump COLLATE clauses for attributes whose + * collation is different from their type's default, we use a CASE here to + * suppress uninteresting attcollations cheaply. + */ + appendPQExpBufferStr(q, + "SELECT\n" + "a.attrelid,\n" + "a.attnum,\n" + "a.attname,\n" + "a.attstattarget,\n" + "a.attstorage,\n" + "t.typstorage,\n" + "a.atthasdef,\n" + "a.attisdropped,\n" + "a.attlen,\n" + "a.attalign,\n" + "a.attislocal,\n" + "pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n" + "array_to_string(a.attoptions, ', ') AS attoptions,\n" + "CASE WHEN a.attcollation <> t.typcollation " + "THEN a.attcollation ELSE 0 END AS attcollation,\n" + "pg_catalog.array_to_string(ARRAY(" + "SELECT pg_catalog.quote_ident(option_name) || " + "' ' || pg_catalog.quote_literal(option_value) " + "FROM pg_catalog.pg_options_to_table(attfdwoptions) " + "ORDER BY option_name" + "), E',\n ') AS attfdwoptions,\n"); + + /* + * Find out any NOT NULL markings for each column. In 18 and up we read + * pg_constraint to obtain the constraint name, and for valid constraints + * also pg_description to obtain its comment. notnull_noinherit is set + * according to the NO INHERIT property. For versions prior to 18, we + * store an empty string as the name when a constraint is marked as + * attnotnull (this cues dumpTableSchema to print the NOT NULL clause + * without a name); also, such cases are never NO INHERIT. + * + * For invalid constraints, we need to store their OIDs for processing + * elsewhere, so we bring the pg_constraint.oid value when the constraint + * is invalid, and NULL otherwise. Their comments are handled not here + * but by collectComments, because they're their own dumpable object. + * + * We track in notnull_islocal whether the constraint was defined directly + * in this table or via an ancestor, for binary upgrade. flagInhAttrs + * might modify this later. + */ + if (fout->remoteVersion >= 180000) + appendPQExpBufferStr(q, + "co.conname AS notnull_name,\n" + "CASE WHEN co.convalidated THEN pt.description" + " ELSE NULL END AS notnull_comment,\n" + "CASE WHEN NOT co.convalidated THEN co.oid " + "ELSE NULL END AS notnull_invalidoid,\n" + "co.connoinherit AS notnull_noinherit,\n" + "co.conislocal AS notnull_islocal,\n"); + else + appendPQExpBufferStr(q, + "CASE WHEN a.attnotnull THEN '' ELSE NULL END AS notnull_name,\n" + "NULL AS notnull_comment,\n" + "NULL AS notnull_invalidoid,\n" + "false AS notnull_noinherit,\n" + "CASE WHEN a.attislocal THEN true\n" + " WHEN a.attnotnull AND NOT a.attislocal THEN true\n" + " ELSE false\n" + "END AS notnull_islocal,\n"); + + if (fout->remoteVersion >= 140000) + appendPQExpBufferStr(q, + "a.attcompression AS attcompression,\n"); + else + appendPQExpBufferStr(q, + "'' AS attcompression,\n"); + + if (fout->remoteVersion >= 100000) + appendPQExpBufferStr(q, + "a.attidentity,\n"); + else + appendPQExpBufferStr(q, + "'' AS attidentity,\n"); + + if (fout->remoteVersion >= 110000) + appendPQExpBufferStr(q, + "CASE WHEN a.atthasmissing AND NOT a.attisdropped " + "THEN a.attmissingval ELSE null END AS attmissingval,\n"); + else + appendPQExpBufferStr(q, + "NULL AS attmissingval,\n"); + + if (fout->remoteVersion >= 120000) + appendPQExpBufferStr(q, + "a.attgenerated\n"); + else + appendPQExpBufferStr(q, + "'' AS attgenerated\n"); + + /* need left join to pg_type to not fail on dropped columns ... */ + appendPQExpBuffer(q, + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_attribute a ON (src.tbloid = a.attrelid) " + "LEFT JOIN pg_catalog.pg_type t " + "ON (a.atttypid = t.oid)\n", + tbloids->data); + + /* + * In versions 18 and up, we need pg_constraint for explicit NOT NULL + * entries and pg_description to get their comments. + */ + if (fout->remoteVersion >= 180000) + appendPQExpBufferStr(q, + " LEFT JOIN pg_catalog.pg_constraint co ON " + "(a.attrelid = co.conrelid\n" + " AND co.contype = 'n' AND " + "co.conkey = array[a.attnum])\n" + " LEFT JOIN pg_catalog.pg_description pt ON " + "(pt.classoid = co.tableoid AND pt.objoid = co.oid)\n"); + + appendPQExpBufferStr(q, + "WHERE a.attnum > 0::pg_catalog.int2\n"); + + /* + * For binary upgrades from dopt->binary_upgrade && fout->remoteVersion < 120000) + appendPQExpBufferStr(q, + "OR (a.attnum = -2::pg_catalog.int2 AND src.tbloid = " + CppAsString2(LargeObjectMetadataRelationId) ")\n"); + + appendPQExpBufferStr(q, + "ORDER BY a.attrelid, a.attnum"); + + res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_attrelid = PQfnumber(res, "attrelid"); + i_attnum = PQfnumber(res, "attnum"); + i_attname = PQfnumber(res, "attname"); + i_atttypname = PQfnumber(res, "atttypname"); + i_attstattarget = PQfnumber(res, "attstattarget"); + i_attstorage = PQfnumber(res, "attstorage"); + i_typstorage = PQfnumber(res, "typstorage"); + i_attidentity = PQfnumber(res, "attidentity"); + i_attgenerated = PQfnumber(res, "attgenerated"); + i_attisdropped = PQfnumber(res, "attisdropped"); + i_attlen = PQfnumber(res, "attlen"); + i_attalign = PQfnumber(res, "attalign"); + i_attislocal = PQfnumber(res, "attislocal"); + i_notnull_name = PQfnumber(res, "notnull_name"); + i_notnull_comment = PQfnumber(res, "notnull_comment"); + i_notnull_invalidoid = PQfnumber(res, "notnull_invalidoid"); + i_notnull_noinherit = PQfnumber(res, "notnull_noinherit"); + i_notnull_islocal = PQfnumber(res, "notnull_islocal"); + i_attoptions = PQfnumber(res, "attoptions"); + i_attcollation = PQfnumber(res, "attcollation"); + i_attcompression = PQfnumber(res, "attcompression"); + i_attfdwoptions = PQfnumber(res, "attfdwoptions"); + i_attmissingval = PQfnumber(res, "attmissingval"); + i_atthasdef = PQfnumber(res, "atthasdef"); + + /* Within the next loop, we'll accumulate OIDs of tables with defaults */ + resetPQExpBuffer(tbloids); + appendPQExpBufferChar(tbloids, '{'); + + /* + * Outer loop iterates once per table, not once per row. Incrementing of + * r is handled by the inner loop. + */ + curtblindx = -1; + for (int r = 0; r < ntups;) + { + Oid attrelid = atooid(PQgetvalue(res, r, i_attrelid)); + TableInfo *tbinfo = NULL; + int numatts; + bool hasdefaults; + + /* Count rows for this table */ + for (numatts = 1; numatts < ntups - r; numatts++) + if (atooid(PQgetvalue(res, r + numatts, i_attrelid)) != attrelid) + break; + + /* + * Locate the associated TableInfo; we rely on tblinfo[] being in OID + * order. + */ + while (++curtblindx < numTables) + { + tbinfo = &tblinfo[curtblindx]; + if (tbinfo->dobj.catId.oid == attrelid) + break; + } + if (curtblindx >= numTables) + pg_fatal("unrecognized table OID %u", attrelid); + /* cross-check that we only got requested tables */ + if (tbinfo->relkind == RELKIND_SEQUENCE || + (!tbinfo->interesting && + !(fout->dopt->binary_upgrade && + (tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId || + tbinfo->dobj.catId.oid == SharedDependRelationId)))) + pg_fatal("unexpected column data for table \"%s\"", + tbinfo->dobj.name); + + /* Save data for this table */ + tbinfo->numatts = numatts; + tbinfo->attnames = pg_malloc_array(char *, numatts); + tbinfo->atttypnames = pg_malloc_array(char *, numatts); + tbinfo->attstattarget = pg_malloc_array(int, numatts); + tbinfo->attstorage = pg_malloc_array(char, numatts); + tbinfo->typstorage = pg_malloc_array(char, numatts); + tbinfo->attidentity = pg_malloc_array(char, numatts); + tbinfo->attgenerated = pg_malloc_array(char, numatts); + tbinfo->attisdropped = pg_malloc_array(bool, numatts); + tbinfo->attlen = pg_malloc_array(int, numatts); + tbinfo->attalign = pg_malloc_array(char, numatts); + tbinfo->attislocal = pg_malloc_array(bool, numatts); + tbinfo->attoptions = pg_malloc_array(char *, numatts); + tbinfo->attcollation = pg_malloc_array(Oid, numatts); + tbinfo->attcompression = pg_malloc_array(char, numatts); + tbinfo->attfdwoptions = pg_malloc_array(char *, numatts); + tbinfo->attmissingval = pg_malloc_array(char *, numatts); + tbinfo->notnull_constrs = pg_malloc_array(char *, numatts); + tbinfo->notnull_comment = pg_malloc_array(char *, numatts); + tbinfo->notnull_invalid = pg_malloc_array(bool, numatts); + tbinfo->notnull_noinh = pg_malloc_array(bool, numatts); + tbinfo->notnull_islocal = pg_malloc_array(bool, numatts); + tbinfo->attrdefs = pg_malloc_array(AttrDefInfo *, numatts); + hasdefaults = false; + + for (int j = 0; j < numatts; j++, r++) + { + if (j + 1 != atoi(PQgetvalue(res, r, i_attnum)) && + !(fout->dopt->binary_upgrade && fout->remoteVersion < 120000 && + tbinfo->dobj.catId.oid == LargeObjectMetadataRelationId)) + pg_fatal("invalid column numbering in table \"%s\"", + tbinfo->dobj.name); + tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, r, i_attname)); + tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, r, i_atttypname)); + if (PQgetisnull(res, r, i_attstattarget)) + tbinfo->attstattarget[j] = -1; + else + tbinfo->attstattarget[j] = atoi(PQgetvalue(res, r, i_attstattarget)); + tbinfo->attstorage[j] = *(PQgetvalue(res, r, i_attstorage)); + tbinfo->typstorage[j] = *(PQgetvalue(res, r, i_typstorage)); + tbinfo->attidentity[j] = *(PQgetvalue(res, r, i_attidentity)); + tbinfo->attgenerated[j] = *(PQgetvalue(res, r, i_attgenerated)); + tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS); + tbinfo->attisdropped[j] = (PQgetvalue(res, r, i_attisdropped)[0] == 't'); + tbinfo->attlen[j] = atoi(PQgetvalue(res, r, i_attlen)); + tbinfo->attalign[j] = *(PQgetvalue(res, r, i_attalign)); + tbinfo->attislocal[j] = (PQgetvalue(res, r, i_attislocal)[0] == 't'); + + /* Handle not-null constraint name and flags */ + determineNotNullFlags(fout, res, r, + tbinfo, j, + i_notnull_name, + i_notnull_comment, + i_notnull_invalidoid, + i_notnull_noinherit, + i_notnull_islocal, + &invalidnotnulloids); + + tbinfo->notnull_comment[j] = PQgetisnull(res, r, i_notnull_comment) ? + NULL : pg_strdup(PQgetvalue(res, r, i_notnull_comment)); + tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, r, i_attoptions)); + tbinfo->attcollation[j] = atooid(PQgetvalue(res, r, i_attcollation)); + tbinfo->attcompression[j] = *(PQgetvalue(res, r, i_attcompression)); + tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, r, i_attfdwoptions)); + tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, r, i_attmissingval)); + tbinfo->attrdefs[j] = NULL; /* fix below */ + if (PQgetvalue(res, r, i_atthasdef)[0] == 't') + hasdefaults = true; + } + + if (hasdefaults) + { + /* Collect OIDs of interesting tables that have defaults */ + if (tbloids->len > 1) /* do we have more than the '{'? */ + appendPQExpBufferChar(tbloids, ','); + appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); + } + } + + /* If invalidnotnulloids has any data, finalize it */ + if (invalidnotnulloids != NULL) + appendPQExpBufferChar(invalidnotnulloids, '}'); + + PQclear(res); + + /* + * Now get info about column defaults. This is skipped for a data-only + * dump, as it is only needed for table schemas. + */ + if (dopt->dumpSchema && tbloids->len > 1) + { + AttrDefInfo *attrdefs; + int numDefaults; + TableInfo *tbinfo = NULL; + + pg_log_info("finding table default expressions"); + + appendPQExpBufferChar(tbloids, '}'); + + printfPQExpBuffer(q, "SELECT a.tableoid, a.oid, adrelid, adnum, " + "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc\n" + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_attrdef a ON (src.tbloid = a.adrelid)\n" + "ORDER BY a.adrelid, a.adnum", + tbloids->data); + + res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); + + numDefaults = PQntuples(res); + attrdefs = pg_malloc_array(AttrDefInfo, numDefaults); + + curtblindx = -1; + for (int j = 0; j < numDefaults; j++) + { + Oid adtableoid = atooid(PQgetvalue(res, j, 0)); + Oid adoid = atooid(PQgetvalue(res, j, 1)); + Oid adrelid = atooid(PQgetvalue(res, j, 2)); + int adnum = atoi(PQgetvalue(res, j, 3)); + char *adsrc = PQgetvalue(res, j, 4); + + /* + * Locate the associated TableInfo; we rely on tblinfo[] being in + * OID order. + */ + if (tbinfo == NULL || tbinfo->dobj.catId.oid != adrelid) + { + while (++curtblindx < numTables) + { + tbinfo = &tblinfo[curtblindx]; + if (tbinfo->dobj.catId.oid == adrelid) + break; + } + if (curtblindx >= numTables) + pg_fatal("unrecognized table OID %u", adrelid); + } + + if (adnum <= 0 || adnum > tbinfo->numatts) + pg_fatal("invalid adnum value %d for table \"%s\"", + adnum, tbinfo->dobj.name); + + /* + * dropped columns shouldn't have defaults, but just in case, + * ignore 'em + */ + if (tbinfo->attisdropped[adnum - 1]) + continue; + + attrdefs[j].dobj.objType = DO_ATTRDEF; + attrdefs[j].dobj.catId.tableoid = adtableoid; + attrdefs[j].dobj.catId.oid = adoid; + AssignDumpId(&attrdefs[j].dobj); + attrdefs[j].adtable = tbinfo; + attrdefs[j].adnum = adnum; + attrdefs[j].adef_expr = pg_strdup(adsrc); + + attrdefs[j].dobj.name = pg_strdup(tbinfo->dobj.name); + attrdefs[j].dobj.namespace = tbinfo->dobj.namespace; + + attrdefs[j].dobj.dump = tbinfo->dobj.dump; + + /* + * Figure out whether the default/generation expression should be + * dumped as part of the main CREATE TABLE (or similar) command or + * as a separate ALTER TABLE (or similar) command. The preference + * is to put it into the CREATE command, but in some cases that's + * not possible. + */ + if (tbinfo->attgenerated[adnum - 1]) + { + /* + * Column generation expressions cannot be dumped separately, + * because there is no syntax for it. By setting separate to + * false here we prevent the "default" from being processed as + * its own dumpable object. Later, flagInhAttrs() will mark + * it as not to be dumped at all, if possible (that is, if it + * can be inherited from a parent). + */ + attrdefs[j].separate = false; + } + else if (tbinfo->relkind == RELKIND_VIEW) + { + /* + * Defaults on a VIEW must always be dumped as separate ALTER + * TABLE commands. + */ + attrdefs[j].separate = true; + } + else if (!shouldPrintColumn(dopt, tbinfo, adnum - 1)) + { + /* column will be suppressed, print default separately */ + attrdefs[j].separate = true; + } + else + { + attrdefs[j].separate = false; + } + + if (!attrdefs[j].separate) + { + /* + * Mark the default as needing to appear before the table, so + * that any dependencies it has must be emitted before the + * CREATE TABLE. If this is not possible, we'll change to + * "separate" mode while sorting dependencies. + */ + addObjectDependency(&tbinfo->dobj, + attrdefs[j].dobj.dumpId); + } + + tbinfo->attrdefs[adnum - 1] = &attrdefs[j]; + } + + PQclear(res); + } + + /* + * Get info about NOT NULL NOT VALID constraints. This is skipped for a + * data-only dump, as it is only needed for table schemas. + */ + if (dopt->dumpSchema && invalidnotnulloids) + { + ConstraintInfo *constrs; + int numConstrs; + int i_tableoid; + int i_oid; + int i_conrelid; + int i_conname; + int i_consrc; + int i_conislocal; + + pg_log_info("finding invalid not-null constraints"); + + resetPQExpBuffer(q); + appendPQExpBuffer(q, + "SELECT c.tableoid, c.oid, conrelid, conname, " + "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, " + "conislocal, convalidated " + "FROM unnest('%s'::pg_catalog.oid[]) AS src(conoid)\n" + "JOIN pg_catalog.pg_constraint c ON (src.conoid = c.oid)\n" + "ORDER BY c.conrelid, c.conname", + invalidnotnulloids->data); + + res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); + + numConstrs = PQntuples(res); + constrs = pg_malloc_array(ConstraintInfo, numConstrs); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_conrelid = PQfnumber(res, "conrelid"); + i_conname = PQfnumber(res, "conname"); + i_consrc = PQfnumber(res, "consrc"); + i_conislocal = PQfnumber(res, "conislocal"); + + /* As above, this loop iterates once per table, not once per row */ + curtblindx = -1; + for (int j = 0; j < numConstrs;) + { + Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid)); + TableInfo *tbinfo = NULL; + int numcons; + + /* Count rows for this table */ + for (numcons = 1; numcons < numConstrs - j; numcons++) + if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid) + break; + + /* + * Locate the associated TableInfo; we rely on tblinfo[] being in + * OID order. + */ + while (++curtblindx < numTables) + { + tbinfo = &tblinfo[curtblindx]; + if (tbinfo->dobj.catId.oid == conrelid) + break; + } + if (curtblindx >= numTables) + pg_fatal("unrecognized table OID %u", conrelid); + + for (int c = 0; c < numcons; c++, j++) + { + constrs[j].dobj.objType = DO_CONSTRAINT; + constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid)); + constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); + AssignDumpId(&constrs[j].dobj); + constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname)); + constrs[j].dobj.namespace = tbinfo->dobj.namespace; + constrs[j].contable = tbinfo; + constrs[j].condomain = NULL; + constrs[j].contype = 'n'; + constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc)); + constrs[j].confrelid = InvalidOid; + constrs[j].conindex = 0; + constrs[j].condeferrable = false; + constrs[j].condeferred = false; + constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't'); + + /* + * All invalid not-null constraints must be dumped separately, + * because CREATE TABLE would not create them as invalid, and + * also because they must be created after potentially + * violating data has been loaded. + */ + constrs[j].separate = true; + + constrs[j].dobj.dump = tbinfo->dobj.dump; + } + } + PQclear(res); + } + + /* + * Get info about table CHECK constraints. This is skipped for a + * data-only dump, as it is only needed for table schemas. + */ + if (dopt->dumpSchema && checkoids->len > 2) + { + ConstraintInfo *constrs; + int numConstrs; + int i_tableoid; + int i_oid; + int i_conrelid; + int i_conname; + int i_consrc; + int i_conislocal; + int i_convalidated; + + pg_log_info("finding table check constraints"); + + resetPQExpBuffer(q); + appendPQExpBuffer(q, + "SELECT c.tableoid, c.oid, conrelid, conname, " + "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, " + "conislocal, convalidated " + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n" + "WHERE contype = 'c' " + "ORDER BY c.conrelid, c.conname", + checkoids->data); + + res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); + + numConstrs = PQntuples(res); + constrs = pg_malloc_array(ConstraintInfo, numConstrs); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_conrelid = PQfnumber(res, "conrelid"); + i_conname = PQfnumber(res, "conname"); + i_consrc = PQfnumber(res, "consrc"); + i_conislocal = PQfnumber(res, "conislocal"); + i_convalidated = PQfnumber(res, "convalidated"); + + /* As above, this loop iterates once per table, not once per row */ + curtblindx = -1; + for (int j = 0; j < numConstrs;) + { + Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid)); + TableInfo *tbinfo = NULL; + int numcons; + + /* Count rows for this table */ + for (numcons = 1; numcons < numConstrs - j; numcons++) + if (atooid(PQgetvalue(res, j + numcons, i_conrelid)) != conrelid) + break; + + /* + * Locate the associated TableInfo; we rely on tblinfo[] being in + * OID order. + */ + while (++curtblindx < numTables) + { + tbinfo = &tblinfo[curtblindx]; + if (tbinfo->dobj.catId.oid == conrelid) + break; + } + if (curtblindx >= numTables) + pg_fatal("unrecognized table OID %u", conrelid); + + if (numcons != tbinfo->ncheck) + { + pg_log_error(ngettext("expected %d check constraint on table \"%s\" but found %d", + "expected %d check constraints on table \"%s\" but found %d", + tbinfo->ncheck), + tbinfo->ncheck, tbinfo->dobj.name, numcons); + pg_log_error_hint("The system catalogs might be corrupted."); + exit_nicely(1); + } + + tbinfo->checkexprs = constrs + j; + + for (int c = 0; c < numcons; c++, j++) + { + bool validated = PQgetvalue(res, j, i_convalidated)[0] == 't'; + + constrs[j].dobj.objType = DO_CONSTRAINT; + constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid)); + constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid)); + AssignDumpId(&constrs[j].dobj); + constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname)); + constrs[j].dobj.namespace = tbinfo->dobj.namespace; + constrs[j].contable = tbinfo; + constrs[j].condomain = NULL; + constrs[j].contype = 'c'; + constrs[j].condef = pg_strdup(PQgetvalue(res, j, i_consrc)); + constrs[j].confrelid = InvalidOid; + constrs[j].conindex = 0; + constrs[j].condeferrable = false; + constrs[j].condeferred = false; + constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't'); + + /* + * An unvalidated constraint needs to be dumped separately, so + * that potentially-violating existing data is loaded before + * the constraint. + */ + constrs[j].separate = !validated; + + constrs[j].dobj.dump = tbinfo->dobj.dump; + + /* + * Mark the constraint as needing to appear before the table + * --- this is so that any other dependencies of the + * constraint will be emitted before we try to create the + * table. If the constraint is to be dumped separately, it + * will be dumped after data is loaded anyway, so don't do it. + * (There's an automatic dependency in the opposite direction + * anyway, so don't need to add one manually here.) + */ + if (!constrs[j].separate) + addObjectDependency(&tbinfo->dobj, + constrs[j].dobj.dumpId); + + /* + * We will detect later whether the constraint must be split + * out from the table definition. + */ + } + } + + PQclear(res); + } + + destroyPQExpBuffer(q); + destroyPQExpBuffer(tbloids); + destroyPQExpBuffer(checkoids); +} + +/* + * Based on the getTableAttrs query's row corresponding to one column, set + * the name and flags to handle a not-null constraint for that column in + * the tbinfo struct. + * + * Result row 'r' is for tbinfo's attribute 'j'. + * + * There are four possibilities: + * 1) the column has no not-null constraints. In that case, ->notnull_constrs + * (the constraint name) remains NULL. + * 2) The column has a constraint with no name (this is the case when + * constraints come from pre-18 servers). In this case, ->notnull_constrs + * is set to the empty string; dumpTableSchema will print just "NOT NULL". + * 3) The column has an invalid not-null constraint. This must be treated + * as a separate object (because it must be created after the table data + * is loaded). So we add its OID to invalidnotnulloids for processing + * elsewhere and do nothing further with it here. We distinguish this + * case because the "notnull_invalidoid" column has been set to a non-NULL + * value, which is the constraint OID. Valid constraints have a null OID. + * 4) The column has a constraint with a known name; in that case + * notnull_constrs carries that name and dumpTableSchema will print + * "CONSTRAINT the_name NOT NULL". However, if the name is the default + * (table_column_not_null) and there's no comment on the constraint, + * there's no need to print that name in the dump, so notnull_constrs + * is set to the empty string and it behaves as case 2. + * + * In a child table that inherits from a parent already containing NOT NULL + * constraints and the columns in the child don't have their own NOT NULL + * declarations, we suppress printing constraints in the child: the + * constraints are acquired at the point where the child is attached to the + * parent. This is tracked in ->notnull_islocal; for servers pre-18 this is + * set not here but in flagInhAttrs. That flag is also used when the + * constraint was validated in a child but all its parent have it as NOT + * VALID. + * + * Any of these constraints might have the NO INHERIT bit. If so we set + * ->notnull_noinh and NO INHERIT will be printed by dumpTableSchema. + * + * In case 4 above, the name comparison is a bit of a hack; it actually fails + * to do the right thing in all but the trivial case. However, the downside + * of getting it wrong is simply that the name is printed rather than + * suppressed, so it's not a big deal. + * + * invalidnotnulloids is expected to be given as NULL; if any invalid not-null + * constraints are found, it is initialized and filled with the array of + * OIDs of such constraints, for later processing. + */ +static void +determineNotNullFlags(Archive *fout, PGresult *res, int r, + TableInfo *tbinfo, int j, + int i_notnull_name, + int i_notnull_comment, + int i_notnull_invalidoid, + int i_notnull_noinherit, + int i_notnull_islocal, + PQExpBuffer *invalidnotnulloids) +{ + DumpOptions *dopt = fout->dopt; + + /* + * If this not-null constraint is not valid, list its OID in + * invalidnotnulloids and do nothing further. It'll be processed + * elsewhere later. + * + * Because invalid not-null constraints are rare, we don't want to malloc + * invalidnotnulloids until we're sure we're going it need it, which + * happens here. + */ + if (!PQgetisnull(res, r, i_notnull_invalidoid)) + { + char *constroid = PQgetvalue(res, r, i_notnull_invalidoid); + + if (*invalidnotnulloids == NULL) + { + *invalidnotnulloids = createPQExpBuffer(); + appendPQExpBufferChar(*invalidnotnulloids, '{'); + appendPQExpBufferStr(*invalidnotnulloids, constroid); + } + else + appendPQExpBuffer(*invalidnotnulloids, ",%s", constroid); + + /* + * Track when a parent constraint is invalid for the cases where a + * child constraint has been validated independenly. + */ + tbinfo->notnull_invalid[j] = true; + + /* nothing else to do */ + tbinfo->notnull_constrs[j] = NULL; + return; + } + + /* + * notnull_noinh is straight from the query result. notnull_islocal also, + * though flagInhAttrs may change that one later. + */ + tbinfo->notnull_noinh[j] = PQgetvalue(res, r, i_notnull_noinherit)[0] == 't'; + tbinfo->notnull_islocal[j] = PQgetvalue(res, r, i_notnull_islocal)[0] == 't'; + tbinfo->notnull_invalid[j] = false; + + /* + * Determine a constraint name to use. If the column is not marked not- + * null, we set NULL which cues ... to do nothing. An empty string says + * to print an unnamed NOT NULL, and anything else is a constraint name to + * use. + */ + if (fout->remoteVersion < 180000) + { + /* + * < 18 doesn't have not-null names, so an unnamed constraint is + * sufficient. + */ + if (PQgetisnull(res, r, i_notnull_name)) + tbinfo->notnull_constrs[j] = NULL; + else + tbinfo->notnull_constrs[j] = ""; + } + else + { + if (PQgetisnull(res, r, i_notnull_name)) + tbinfo->notnull_constrs[j] = NULL; + else + { + /* + * In binary upgrade of inheritance child tables, must have a + * constraint name that we can UPDATE later; same if there's a + * comment on the constraint. + */ + if ((dopt->binary_upgrade && + !tbinfo->ispartition && + !tbinfo->notnull_islocal[j]) || + !PQgetisnull(res, r, i_notnull_comment)) + { + tbinfo->notnull_constrs[j] = + pstrdup(PQgetvalue(res, r, i_notnull_name)); + } + else + { + char *default_name; + + /* XXX should match ChooseConstraintName better */ + default_name = psprintf("%s_%s_not_null", tbinfo->dobj.name, + tbinfo->attnames[j]); + if (strcmp(default_name, + PQgetvalue(res, r, i_notnull_name)) == 0) + tbinfo->notnull_constrs[j] = ""; + else + { + tbinfo->notnull_constrs[j] = + pstrdup(PQgetvalue(res, r, i_notnull_name)); + } + free(default_name); + } + } + } +} + +/* + * Test whether a column should be printed as part of table's CREATE TABLE. + * Column number is zero-based. + * + * Normally this is always true, but it's false for dropped columns, as well + * as those that were inherited without any local definition. (If we print + * such a column it will mistakenly get pg_attribute.attislocal set to true.) + * For partitions, it's always true, because we want the partitions to be + * created independently and ATTACH PARTITION used afterwards. + * + * In binary_upgrade mode, we must print all columns and fix the attislocal/ + * attisdropped state later, so as to keep control of the physical column + * order. + * + * This function exists because there are scattered nonobvious places that + * must be kept in sync with this decision. + */ +bool +shouldPrintColumn(const DumpOptions *dopt, const TableInfo *tbinfo, int colno) +{ + if (dopt->binary_upgrade) + return true; + if (dopt->pgactive_init_node) + return true; + if (tbinfo->attisdropped[colno]) + return false; + return (tbinfo->attislocal[colno] || tbinfo->ispartition); +} + + +/* + * getTSParsers: + * get information about all text search parsers in the system catalogs + */ +void +getTSParsers(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + TSParserInfo *prsinfo; + int i_tableoid; + int i_oid; + int i_prsname; + int i_prsnamespace; + int i_prsstart; + int i_prstoken; + int i_prsend; + int i_prsheadline; + int i_prslextype; + + query = createPQExpBuffer(); + + /* + * find all text search objects, including builtin ones; we filter out + * system-defined objects at dump-out time. + */ + + appendPQExpBufferStr(query, "SELECT tableoid, oid, prsname, prsnamespace, " + "prsstart::oid, prstoken::oid, " + "prsend::oid, prsheadline::oid, prslextype::oid " + "FROM pg_ts_parser"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + prsinfo = pg_malloc_array(TSParserInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_prsname = PQfnumber(res, "prsname"); + i_prsnamespace = PQfnumber(res, "prsnamespace"); + i_prsstart = PQfnumber(res, "prsstart"); + i_prstoken = PQfnumber(res, "prstoken"); + i_prsend = PQfnumber(res, "prsend"); + i_prsheadline = PQfnumber(res, "prsheadline"); + i_prslextype = PQfnumber(res, "prslextype"); + + for (i = 0; i < ntups; i++) + { + prsinfo[i].dobj.objType = DO_TSPARSER; + prsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&prsinfo[i].dobj); + prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname)); + prsinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_prsnamespace))); + prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart)); + prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken)); + prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend)); + prsinfo[i].prsheadline = atooid(PQgetvalue(res, i, i_prsheadline)); + prsinfo[i].prslextype = atooid(PQgetvalue(res, i, i_prslextype)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(prsinfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getTSDictionaries: + * get information about all text search dictionaries in the system catalogs + */ +void +getTSDictionaries(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + TSDictInfo *dictinfo; + int i_tableoid; + int i_oid; + int i_dictname; + int i_dictnamespace; + int i_dictowner; + int i_dicttemplate; + int i_dictinitoption; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "SELECT tableoid, oid, dictname, " + "dictnamespace, dictowner, " + "dicttemplate, dictinitoption " + "FROM pg_ts_dict"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + dictinfo = pg_malloc_array(TSDictInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_dictname = PQfnumber(res, "dictname"); + i_dictnamespace = PQfnumber(res, "dictnamespace"); + i_dictowner = PQfnumber(res, "dictowner"); + i_dictinitoption = PQfnumber(res, "dictinitoption"); + i_dicttemplate = PQfnumber(res, "dicttemplate"); + + for (i = 0; i < ntups; i++) + { + dictinfo[i].dobj.objType = DO_TSDICT; + dictinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&dictinfo[i].dobj); + dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname)); + dictinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_dictnamespace))); + dictinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_dictowner)); + dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate)); + if (PQgetisnull(res, i, i_dictinitoption)) + dictinfo[i].dictinitoption = NULL; + else + dictinfo[i].dictinitoption = pg_strdup(PQgetvalue(res, i, i_dictinitoption)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(dictinfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getTSTemplates: + * get information about all text search templates in the system catalogs + */ +void +getTSTemplates(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + TSTemplateInfo *tmplinfo; + int i_tableoid; + int i_oid; + int i_tmplname; + int i_tmplnamespace; + int i_tmplinit; + int i_tmpllexize; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "SELECT tableoid, oid, tmplname, " + "tmplnamespace, tmplinit::oid, tmpllexize::oid " + "FROM pg_ts_template"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + tmplinfo = pg_malloc_array(TSTemplateInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_tmplname = PQfnumber(res, "tmplname"); + i_tmplnamespace = PQfnumber(res, "tmplnamespace"); + i_tmplinit = PQfnumber(res, "tmplinit"); + i_tmpllexize = PQfnumber(res, "tmpllexize"); + + for (i = 0; i < ntups; i++) + { + tmplinfo[i].dobj.objType = DO_TSTEMPLATE; + tmplinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&tmplinfo[i].dobj); + tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname)); + tmplinfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_tmplnamespace))); + tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit)); + tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(tmplinfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getTSConfigurations: + * get information about all text search configurations + */ +void +getTSConfigurations(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + TSConfigInfo *cfginfo; + int i_tableoid; + int i_oid; + int i_cfgname; + int i_cfgnamespace; + int i_cfgowner; + int i_cfgparser; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "SELECT tableoid, oid, cfgname, " + "cfgnamespace, cfgowner, cfgparser " + "FROM pg_ts_config"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + cfginfo = pg_malloc_array(TSConfigInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_cfgname = PQfnumber(res, "cfgname"); + i_cfgnamespace = PQfnumber(res, "cfgnamespace"); + i_cfgowner = PQfnumber(res, "cfgowner"); + i_cfgparser = PQfnumber(res, "cfgparser"); + + for (i = 0; i < ntups; i++) + { + cfginfo[i].dobj.objType = DO_TSCONFIG; + cfginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&cfginfo[i].dobj); + cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname)); + cfginfo[i].dobj.namespace = + findNamespace(atooid(PQgetvalue(res, i, i_cfgnamespace))); + cfginfo[i].rolname = getRoleName(PQgetvalue(res, i, i_cfgowner)); + cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(cfginfo[i].dobj), fout); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getForeignDataWrappers: + * get information about all foreign-data wrappers in the system catalogs + */ +void +getForeignDataWrappers(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + FdwInfo *fdwinfo; + int i_tableoid; + int i_oid; + int i_fdwname; + int i_fdwowner; + int i_fdwhandler; + int i_fdwvalidator; + int i_fdwconnection; + int i_fdwacl; + int i_acldefault; + int i_fdwoptions; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "SELECT tableoid, oid, fdwname, " + "fdwowner, " + "fdwhandler::pg_catalog.regproc, " + "fdwvalidator::pg_catalog.regproc, "); + + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, "fdwconnection::pg_catalog.regproc, "); + else + appendPQExpBufferStr(query, "'-' AS fdwconnection, "); + + appendPQExpBufferStr(query, + "fdwacl, " + "acldefault('F', fdwowner) AS acldefault, " + "array_to_string(ARRAY(" + "SELECT quote_ident(option_name) || ' ' || " + "quote_literal(option_value) " + "FROM pg_options_to_table(fdwoptions) " + "ORDER BY option_name" + "), E',\n ') AS fdwoptions " + "FROM pg_foreign_data_wrapper"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + fdwinfo = pg_malloc_array(FdwInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_fdwname = PQfnumber(res, "fdwname"); + i_fdwowner = PQfnumber(res, "fdwowner"); + i_fdwhandler = PQfnumber(res, "fdwhandler"); + i_fdwvalidator = PQfnumber(res, "fdwvalidator"); + i_fdwconnection = PQfnumber(res, "fdwconnection"); + i_fdwacl = PQfnumber(res, "fdwacl"); + i_acldefault = PQfnumber(res, "acldefault"); + i_fdwoptions = PQfnumber(res, "fdwoptions"); + + for (i = 0; i < ntups; i++) + { + fdwinfo[i].dobj.objType = DO_FDW; + fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&fdwinfo[i].dobj); + fdwinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_fdwname)); + fdwinfo[i].dobj.namespace = NULL; + fdwinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_fdwacl)); + fdwinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + fdwinfo[i].dacl.privtype = 0; + fdwinfo[i].dacl.initprivs = NULL; + fdwinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_fdwowner)); + fdwinfo[i].fdwhandler = pg_strdup(PQgetvalue(res, i, i_fdwhandler)); + fdwinfo[i].fdwvalidator = pg_strdup(PQgetvalue(res, i, i_fdwvalidator)); + fdwinfo[i].fdwconnection = pg_strdup(PQgetvalue(res, i, i_fdwconnection)); + fdwinfo[i].fdwoptions = pg_strdup(PQgetvalue(res, i, i_fdwoptions)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(fdwinfo[i].dobj), fout); + + /* Mark whether FDW has an ACL */ + if (!PQgetisnull(res, i, i_fdwacl)) + fdwinfo[i].dobj.components |= DUMP_COMPONENT_ACL; + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getForeignServers: + * get information about all foreign servers in the system catalogs + */ +void +getForeignServers(Archive *fout) +{ + PGresult *res; + int ntups; + int i; + PQExpBuffer query; + ForeignServerInfo *srvinfo; + int i_tableoid; + int i_oid; + int i_srvname; + int i_srvowner; + int i_srvfdw; + int i_srvtype; + int i_srvversion; + int i_srvacl; + int i_acldefault; + int i_srvoptions; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "SELECT tableoid, oid, srvname, " + "srvowner, " + "srvfdw, srvtype, srvversion, srvacl, " + "acldefault('S', srvowner) AS acldefault, " + "array_to_string(ARRAY(" + "SELECT quote_ident(option_name) || ' ' || " + "quote_literal(option_value) " + "FROM pg_options_to_table(srvoptions) " + "ORDER BY option_name" + "), E',\n ') AS srvoptions " + "FROM pg_foreign_server"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + srvinfo = pg_malloc_array(ForeignServerInfo, ntups); + + i_tableoid = PQfnumber(res, "tableoid"); + i_oid = PQfnumber(res, "oid"); + i_srvname = PQfnumber(res, "srvname"); + i_srvowner = PQfnumber(res, "srvowner"); + i_srvfdw = PQfnumber(res, "srvfdw"); + i_srvtype = PQfnumber(res, "srvtype"); + i_srvversion = PQfnumber(res, "srvversion"); + i_srvacl = PQfnumber(res, "srvacl"); + i_acldefault = PQfnumber(res, "acldefault"); + i_srvoptions = PQfnumber(res, "srvoptions"); + + for (i = 0; i < ntups; i++) + { + srvinfo[i].dobj.objType = DO_FOREIGN_SERVER; + srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&srvinfo[i].dobj); + srvinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_srvname)); + srvinfo[i].dobj.namespace = NULL; + srvinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_srvacl)); + srvinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + srvinfo[i].dacl.privtype = 0; + srvinfo[i].dacl.initprivs = NULL; + srvinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_srvowner)); + srvinfo[i].srvfdw = atooid(PQgetvalue(res, i, i_srvfdw)); + srvinfo[i].srvtype = pg_strdup(PQgetvalue(res, i, i_srvtype)); + srvinfo[i].srvversion = pg_strdup(PQgetvalue(res, i, i_srvversion)); + srvinfo[i].srvoptions = pg_strdup(PQgetvalue(res, i, i_srvoptions)); + + /* Decide whether we want to dump it */ + selectDumpableObject(&(srvinfo[i].dobj), fout); + + /* Servers have user mappings */ + srvinfo[i].dobj.components |= DUMP_COMPONENT_USERMAP; + + /* Mark whether server has an ACL */ + if (!PQgetisnull(res, i, i_srvacl)) + srvinfo[i].dobj.components |= DUMP_COMPONENT_ACL; + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getDefaultACLs: + * get information about all default ACL information in the system catalogs + */ +void +getDefaultACLs(Archive *fout) +{ + DumpOptions *dopt = fout->dopt; + DefaultACLInfo *daclinfo; + PQExpBuffer query; + PGresult *res; + int i_oid; + int i_tableoid; + int i_defaclrole; + int i_defaclnamespace; + int i_defaclobjtype; + int i_defaclacl; + int i_acldefault; + int i, + ntups; + + query = createPQExpBuffer(); + + /* + * Global entries (with defaclnamespace=0) replace the hard-wired default + * ACL for their object type. We should dump them as deltas from the + * default ACL, since that will be used as a starting point for + * interpreting the ALTER DEFAULT PRIVILEGES commands. On the other hand, + * non-global entries can only add privileges not revoke them. We must + * dump those as-is (i.e., as deltas from an empty ACL). + * + * We can use defaclobjtype as the object type for acldefault(), except + * for the case of 'S' (DEFACLOBJ_SEQUENCE) which must be converted to + * 's'. + */ + appendPQExpBufferStr(query, + "SELECT oid, tableoid, " + "defaclrole, " + "defaclnamespace, " + "defaclobjtype, " + "defaclacl, " + "CASE WHEN defaclnamespace = 0 THEN " + "acldefault(CASE WHEN defaclobjtype = 'S' " + "THEN 's'::\"char\" ELSE defaclobjtype END, " + "defaclrole) ELSE '{}' END AS acldefault " + "FROM pg_default_acl"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + daclinfo = pg_malloc_array(DefaultACLInfo, ntups); + + i_oid = PQfnumber(res, "oid"); + i_tableoid = PQfnumber(res, "tableoid"); + i_defaclrole = PQfnumber(res, "defaclrole"); + i_defaclnamespace = PQfnumber(res, "defaclnamespace"); + i_defaclobjtype = PQfnumber(res, "defaclobjtype"); + i_defaclacl = PQfnumber(res, "defaclacl"); + i_acldefault = PQfnumber(res, "acldefault"); + + for (i = 0; i < ntups; i++) + { + Oid nspid = atooid(PQgetvalue(res, i, i_defaclnamespace)); + + daclinfo[i].dobj.objType = DO_DEFAULT_ACL; + daclinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); + daclinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); + AssignDumpId(&daclinfo[i].dobj); + /* cheesy ... is it worth coming up with a better object name? */ + daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype)); + + if (nspid != InvalidOid) + daclinfo[i].dobj.namespace = findNamespace(nspid); + else + daclinfo[i].dobj.namespace = NULL; + + daclinfo[i].dacl.acl = pg_strdup(PQgetvalue(res, i, i_defaclacl)); + daclinfo[i].dacl.acldefault = pg_strdup(PQgetvalue(res, i, i_acldefault)); + daclinfo[i].dacl.privtype = 0; + daclinfo[i].dacl.initprivs = NULL; + daclinfo[i].defaclrole = getRoleName(PQgetvalue(res, i, i_defaclrole)); + daclinfo[i].defaclobjtype = *(PQgetvalue(res, i, i_defaclobjtype)); + + /* Default ACLs are ACLs, of course */ + daclinfo[i].dobj.components |= DUMP_COMPONENT_ACL; + + /* Decide whether we want to dump it */ + selectDumpableDefaultACL(&(daclinfo[i]), dopt); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * getRoleName -- look up the name of a role, given its OID + * + * In current usage, we don't expect failures, so error out for a bad OID. + */ +static const char * +getRoleName(const char *roleoid_str) +{ + Oid roleoid = atooid(roleoid_str); + + /* + * Do binary search to find the appropriate item. + */ + if (nrolenames > 0) + { + RoleNameItem *low = &rolenames[0]; + RoleNameItem *high = &rolenames[nrolenames - 1]; + + while (low <= high) + { + RoleNameItem *middle = low + (high - low) / 2; + + if (roleoid < middle->roleoid) + high = middle - 1; + else if (roleoid > middle->roleoid) + low = middle + 1; + else + return middle->rolename; /* found a match */ + } + } + + pg_fatal("role with OID %u does not exist", roleoid); + return NULL; /* keep compiler quiet */ +} + +/* + * collectRoleNames -- + * + * Construct a table of all known roles. + * The table is sorted by OID for speed in lookup. + */ +static void +collectRoleNames(Archive *fout) +{ + PGresult *res; + const char *query; + int i; + + query = "SELECT oid, rolname FROM pg_catalog.pg_roles ORDER BY 1"; + + res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK); + + nrolenames = PQntuples(res); + + rolenames = pg_malloc_array(RoleNameItem, nrolenames); + + for (i = 0; i < nrolenames; i++) + { + rolenames[i].roleoid = atooid(PQgetvalue(res, i, 0)); + rolenames[i].rolename = pg_strdup(PQgetvalue(res, i, 1)); + } + + PQclear(res); +} + +/* + * getAdditionalACLs + * + * We have now created all the DumpableObjects, and collected the ACL data + * that appears in the directly-associated catalog entries. However, there's + * more ACL-related info to collect. If any of a table's columns have ACLs, + * we must set the TableInfo's DUMP_COMPONENT_ACL components flag, as well as + * its hascolumnACLs flag (we won't store the ACLs themselves here, though). + * Also, in versions having the pg_init_privs catalog, read that and load the + * information into the relevant DumpableObjects. + */ +static void +getAdditionalACLs(Archive *fout) +{ + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + int ntups, + i; + + /* Check for per-column ACLs */ + appendPQExpBufferStr(query, + "SELECT DISTINCT attrelid FROM pg_attribute " + "WHERE attacl IS NOT NULL"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + for (i = 0; i < ntups; i++) + { + Oid relid = atooid(PQgetvalue(res, i, 0)); + TableInfo *tblinfo; + + tblinfo = findTableByOid(relid); + /* OK to ignore tables we haven't got a DumpableObject for */ + if (tblinfo) + { + tblinfo->dobj.components |= DUMP_COMPONENT_ACL; + tblinfo->hascolumnACLs = true; + } + } + PQclear(res); + + /* Fetch initial-privileges data */ + if (fout->remoteVersion >= 90600) + { + printfPQExpBuffer(query, + "SELECT objoid, classoid, objsubid, privtype, initprivs " + "FROM pg_init_privs"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + for (i = 0; i < ntups; i++) + { + Oid objoid = atooid(PQgetvalue(res, i, 0)); + Oid classoid = atooid(PQgetvalue(res, i, 1)); + int objsubid = atoi(PQgetvalue(res, i, 2)); + char privtype = *(PQgetvalue(res, i, 3)); + char *initprivs = PQgetvalue(res, i, 4); + CatalogId objId; + DumpableObject *dobj; + + objId.tableoid = classoid; + objId.oid = objoid; + dobj = findObjectByCatalogId(objId); + /* OK to ignore entries we haven't got a DumpableObject for */ + if (dobj) + { + /* Cope with sub-object initprivs */ + if (objsubid != 0) + { + if (dobj->objType == DO_TABLE) + { + /* For a column initprivs, set the table's ACL flags */ + dobj->components |= DUMP_COMPONENT_ACL; + ((TableInfo *) dobj)->hascolumnACLs = true; + } + else + pg_log_warning("unsupported pg_init_privs entry: %u %u %d", + classoid, objoid, objsubid); + continue; + } + + /* + * We ignore any pg_init_privs.initprivs entry for the public + * schema, as explained in getNamespaces(). + */ + if (dobj->objType == DO_NAMESPACE && + strcmp(dobj->name, "public") == 0) + continue; + + /* Else it had better be of a type we think has ACLs */ + if (dobj->objType == DO_NAMESPACE || + dobj->objType == DO_TYPE || + dobj->objType == DO_FUNC || + dobj->objType == DO_AGG || + dobj->objType == DO_TABLE || + dobj->objType == DO_PROCLANG || + dobj->objType == DO_FDW || + dobj->objType == DO_FOREIGN_SERVER) + { + DumpableObjectWithAcl *daobj = (DumpableObjectWithAcl *) dobj; + + daobj->dacl.privtype = privtype; + daobj->dacl.initprivs = pstrdup(initprivs); + } + else + pg_log_warning("unsupported pg_init_privs entry: %u %u %d", + classoid, objoid, objsubid); + } + } + PQclear(res); + } + + destroyPQExpBuffer(query); +} + +/* + * dumpCommentExtended -- + * + * This routine is used to dump any comments associated with the + * object handed to this routine. The routine takes the object type + * and object name (ready to print, except for schema decoration), plus + * the namespace and owner of the object (for labeling the ArchiveEntry), + * plus catalog ID and subid which are the lookup key for pg_description, + * plus the dump ID for the object (for setting a dependency). + * If a matching pg_description entry is found, it is dumped. + * + * Note: in some cases, such as comments for triggers and rules, the "type" + * string really looks like, e.g., "TRIGGER name ON". This is a bit of a hack + * but it doesn't seem worth complicating the API for all callers to make + * it cleaner. + * + * Note: although this routine takes a dumpId for dependency purposes, + * that purpose is just to mark the dependency in the emitted dump file + * for possible future use by pg_restore. We do NOT use it for determining + * ordering of the comment in the dump file, because this routine is called + * after dependency sorting occurs. This routine should be called just after + * calling ArchiveEntry() for the specified object. + */ +static void +dumpCommentExtended(Archive *fout, const char *type, + const char *name, const char *namespace, + const char *owner, CatalogId catalogId, + int subid, DumpId dumpId, + const char *initdb_comment) +{ + DumpOptions *dopt = fout->dopt; + CommentItem *comments; + int ncomments; + + /* do nothing, if --no-comments is supplied */ + if (dopt->no_comments) + return; + + /* Comments are schema not data ... except LO comments are data */ + if (strcmp(type, "LARGE OBJECT") != 0) + { + if (!dopt->dumpSchema) + return; + } + else + { + /* We do dump LO comments in binary-upgrade mode */ + if (!dopt->dumpData && !dopt->binary_upgrade) + return; + } + + /* Search for comments associated with catalogId, using table */ + ncomments = findComments(catalogId.tableoid, catalogId.oid, + &comments); + + /* Is there one matching the subid? */ + while (ncomments > 0) + { + if (comments->objsubid == subid) + break; + comments++; + ncomments--; + } + + if (initdb_comment != NULL) + { + static CommentItem empty_comment = {.descr = ""}; + + /* + * initdb creates this object with a comment. Skip dumping the + * initdb-provided comment, which would complicate matters for + * non-superuser use of pg_dump. When the DBA has removed initdb's + * comment, replicate that. + */ + if (ncomments == 0) + { + comments = &empty_comment; + ncomments = 1; + } + else if (strcmp(comments->descr, initdb_comment) == 0) + ncomments = 0; + } + + /* If a comment exists, build COMMENT ON statement */ + if (ncomments > 0) + { + PQExpBuffer query = createPQExpBuffer(); + PQExpBuffer tag = createPQExpBuffer(); + + appendPQExpBuffer(query, "COMMENT ON %s ", type); + if (namespace && *namespace) + appendPQExpBuffer(query, "%s.", fmtId(namespace)); + appendPQExpBuffer(query, "%s IS ", name); + appendStringLiteralAH(query, comments->descr, fout); + appendPQExpBufferStr(query, ";\n"); + + appendPQExpBuffer(tag, "%s %s", type, name); + + /* + * We mark comments as SECTION_NONE because they really belong in the + * same section as their parent, whether that is pre-data or + * post-data. + */ + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = tag->data, + .namespace = namespace, + .owner = owner, + .description = "COMMENT", + .section = SECTION_NONE, + .createStmt = query->data, + .deps = &dumpId, + .nDeps = 1)); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(tag); + } +} + +/* + * dumpComment -- + * + * Typical simplification of the above function. + */ +static inline void +dumpComment(Archive *fout, const char *type, + const char *name, const char *namespace, + const char *owner, CatalogId catalogId, + int subid, DumpId dumpId) +{ + dumpCommentExtended(fout, type, name, namespace, owner, + catalogId, subid, dumpId, NULL); +} + +/* + * appendNamedArgument -- + * + * Convenience routine for constructing parameters of the form: + * 'paraname', 'value'::type + */ +static void +appendNamedArgument(PQExpBuffer out, Archive *fout, const char *argname, + const char *argtype, const char *argval) +{ + appendPQExpBufferStr(out, ",\n\t"); + + appendStringLiteralAH(out, argname, fout); + appendPQExpBufferStr(out, ", "); + + appendStringLiteralAH(out, argval, fout); + appendPQExpBuffer(out, "::%s", argtype); +} + +/* + * fetchAttributeStats -- + * + * Fetch next batch of attribute statistics for dumpRelationStats_dumper(). + */ +static PGresult * +fetchAttributeStats(Archive *fout) +{ + ArchiveHandle *AH = (ArchiveHandle *) fout; + PQExpBuffer relids = createPQExpBuffer(); + PQExpBuffer nspnames = createPQExpBuffer(); + PQExpBuffer relnames = createPQExpBuffer(); + int count = 0; + PGresult *res = NULL; + static TocEntry *te; + static bool restarted; + int max_rels = MAX_ATTR_STATS_RELS; + + /* + * Our query for retrieving statistics for multiple relations uses WITH + * ORDINALITY and multi-argument UNNEST(), both of which were introduced + * in v9.4. For older versions, we resort to gathering statistics for a + * single relation at a time. + */ + if (fout->remoteVersion < 90400) + max_rels = 1; + + /* If we're just starting, set our TOC pointer. */ + if (!te) + te = AH->toc->next; + + /* + * We can't easily avoid a second TOC scan for the tar format because it + * writes restore.sql separately, which means we must execute the queries + * twice. This feels risky, but there is no known reason it should + * generate different output than the first pass. Even if it does, the + * worst-case scenario is that restore.sql might have different statistics + * data than the archive. + */ + if (!restarted && te == AH->toc && AH->format == archTar) + { + te = AH->toc->next; + restarted = true; + } + + appendPQExpBufferChar(relids, '{'); + appendPQExpBufferChar(nspnames, '{'); + appendPQExpBufferChar(relnames, '{'); + + /* + * Scan the TOC for the next set of relevant stats entries. We assume + * that statistics are dumped in the order they are listed in the TOC. + * This is perhaps not the sturdiest assumption, so we verify it matches + * reality in dumpRelationStats_dumper(). + */ + for (; te != AH->toc && count < max_rels; te = te->next) + { + if ((te->reqs & REQ_STATS) == 0 || + strcmp(te->desc, "STATISTICS DATA") != 0) + continue; + + if (fout->remoteVersion >= 190000) + { + const RelStatsInfo *rsinfo = (const RelStatsInfo *) te->defnDumperArg; + char relid[32]; + + sprintf(relid, "%u", rsinfo->relid); + appendPGArray(relids, relid); + } + else + { + appendPGArray(nspnames, te->namespace); + appendPGArray(relnames, te->tag); + } + + count++; + } + + appendPQExpBufferChar(relids, '}'); + appendPQExpBufferChar(nspnames, '}'); + appendPQExpBufferChar(relnames, '}'); + + /* Execute the query for the next batch of relations. */ + if (count > 0) + { + PQExpBuffer query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "EXECUTE getAttributeStats("); + + if (fout->remoteVersion >= 190000) + { + appendStringLiteralAH(query, relids->data, fout); + appendPQExpBufferStr(query, "::pg_catalog.oid[])"); + } + else + { + appendStringLiteralAH(query, nspnames->data, fout); + appendPQExpBufferStr(query, "::pg_catalog.name[],"); + appendStringLiteralAH(query, relnames->data, fout); + appendPQExpBufferStr(query, "::pg_catalog.name[])"); + } + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + destroyPQExpBuffer(query); + } + + destroyPQExpBuffer(relids); + destroyPQExpBuffer(nspnames); + destroyPQExpBuffer(relnames); + return res; +} + +/* + * dumpRelationStats_dumper -- + * + * Generate command to import stats into the relation on the new database. + * This routine is called by the Archiver when it wants the statistics to be + * dumped. + */ +static char * +dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te) +{ + const RelStatsInfo *rsinfo = userArg; + static PGresult *res; + static int rownum; + PQExpBuffer query; + PQExpBufferData out_data; + PQExpBuffer out = &out_data; + int i_schemaname; + int i_tablename; + int i_attname; + int i_inherited; + int i_null_frac; + int i_avg_width; + int i_n_distinct; + int i_most_common_vals; + int i_most_common_freqs; + int i_histogram_bounds; + int i_correlation; + int i_most_common_elems; + int i_most_common_elem_freqs; + int i_elem_count_histogram; + int i_range_length_histogram; + int i_range_empty_frac; + int i_range_bounds_histogram; + static TocEntry *expected_te; + + /* + * fetchAttributeStats() assumes that the statistics are dumped in the + * order they are listed in the TOC. We verify that here for safety. + */ + if (!expected_te) + expected_te = ((ArchiveHandle *) fout)->toc; + + expected_te = expected_te->next; + while ((expected_te->reqs & REQ_STATS) == 0 || + strcmp(expected_te->desc, "STATISTICS DATA") != 0) + expected_te = expected_te->next; + + if (te != expected_te) + pg_fatal("statistics dumped out of order (current: %d %s %s, expected: %d %s %s)", + te->dumpId, te->desc, te->tag, + expected_te->dumpId, expected_te->desc, expected_te->tag); + + query = createPQExpBuffer(); + if (!fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS]) + { + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, + "PREPARE getAttributeStats(pg_catalog.oid[]) AS\n"); + else + appendPQExpBufferStr(query, + "PREPARE getAttributeStats(pg_catalog.name[], pg_catalog.name[]) AS\n"); + + appendPQExpBufferStr(query, + "SELECT s.schemaname, s.tablename, s.attname, s.inherited, " + "s.null_frac, s.avg_width, s.n_distinct, " + "s.most_common_vals, s.most_common_freqs, " + "s.histogram_bounds, s.correlation, " + "s.most_common_elems, s.most_common_elem_freqs, " + "s.elem_count_histogram, "); + + if (fout->remoteVersion >= 170000) + appendPQExpBufferStr(query, + "s.range_length_histogram, " + "s.range_empty_frac, " + "s.range_bounds_histogram "); + else + appendPQExpBufferStr(query, + "NULL AS range_length_histogram," + "NULL AS range_empty_frac," + "NULL AS range_bounds_histogram "); + + /* + * The results must be in the order of the relations supplied in the + * parameters to ensure we remain in sync as we walk through the TOC. + * + * For v9.4 through v18, the redundant filter clause on s.tablename = + * ANY(...) seems sufficient to convince the planner to use + * pg_class_relname_nsp_index, which avoids a full scan of pg_stats. + * In newer versions, pg_stats returns the table OIDs, eliminating the + * need for that hack. + * + * Our query for retrieving statistics for multiple relations uses + * WITH ORDINALITY and multi-argument UNNEST(), both of which were + * introduced in v9.4. For older versions, we resort to gathering + * statistics for a single relation at a time. + */ + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, + "FROM pg_catalog.pg_stats s " + "JOIN unnest($1) WITH ORDINALITY AS u (tableid, ord) " + "ON s.tableid = u.tableid " + "ORDER BY u.ord, s.attname, s.inherited"); + else if (fout->remoteVersion >= 90400) + appendPQExpBufferStr(query, + "FROM pg_catalog.pg_stats s " + "JOIN unnest($1, $2) WITH ORDINALITY AS u (schemaname, tablename, ord) " + "ON s.schemaname = u.schemaname " + "AND s.tablename = u.tablename " + "WHERE s.tablename = ANY($2) " + "ORDER BY u.ord, s.attname, s.inherited"); + else + appendPQExpBufferStr(query, + "FROM pg_catalog.pg_stats s " + "WHERE s.schemaname = $1[1] " + "AND s.tablename = $2[1] " + "ORDER BY s.attname, s.inherited"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_GETATTRIBUTESTATS] = true; + resetPQExpBuffer(query); + } + + initPQExpBuffer(out); + + /* restore relation stats */ + appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_relation_stats(\n"); + appendPQExpBuffer(out, "\t'version', '%d'::integer,\n", + fout->remoteVersion); + appendPQExpBufferStr(out, "\t'schemaname', "); + appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout); + appendPQExpBufferStr(out, ",\n"); + appendPQExpBufferStr(out, "\t'relname', "); + appendStringLiteralAH(out, rsinfo->dobj.name, fout); + appendPQExpBufferStr(out, ",\n"); + appendPQExpBuffer(out, "\t'relpages', '%d'::integer,\n", rsinfo->relpages); + + /* + * Before v14, a reltuples value of 0 was ambiguous: it could either mean + * the relation is empty, or it could mean that it hadn't yet been + * vacuumed or analyzed. (Newer versions use -1 for the latter case.) + * This ambiguity allegedly can cause the planner to choose inefficient + * plans after restoring to v18 or newer. To deal with this, let's just + * set reltuples to -1 in that case. + */ + if (fout->remoteVersion < 140000 && strcmp("0", rsinfo->reltuples) == 0) + appendPQExpBufferStr(out, "\t'reltuples', '-1'::real,\n"); + else + appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", rsinfo->reltuples); + + appendPQExpBuffer(out, "\t'relallvisible', '%d'::integer", + rsinfo->relallvisible); + + if (fout->remoteVersion >= 180000) + appendPQExpBuffer(out, ",\n\t'relallfrozen', '%d'::integer", rsinfo->relallfrozen); + + appendPQExpBufferStr(out, "\n);\n"); + + /* Fetch the next batch of attribute statistics if needed. */ + if (rownum >= PQntuples(res)) + { + PQclear(res); + res = fetchAttributeStats(fout); + rownum = 0; + } + + i_schemaname = PQfnumber(res, "schemaname"); + i_tablename = PQfnumber(res, "tablename"); + i_attname = PQfnumber(res, "attname"); + i_inherited = PQfnumber(res, "inherited"); + i_null_frac = PQfnumber(res, "null_frac"); + i_avg_width = PQfnumber(res, "avg_width"); + i_n_distinct = PQfnumber(res, "n_distinct"); + i_most_common_vals = PQfnumber(res, "most_common_vals"); + i_most_common_freqs = PQfnumber(res, "most_common_freqs"); + i_histogram_bounds = PQfnumber(res, "histogram_bounds"); + i_correlation = PQfnumber(res, "correlation"); + i_most_common_elems = PQfnumber(res, "most_common_elems"); + i_most_common_elem_freqs = PQfnumber(res, "most_common_elem_freqs"); + i_elem_count_histogram = PQfnumber(res, "elem_count_histogram"); + i_range_length_histogram = PQfnumber(res, "range_length_histogram"); + i_range_empty_frac = PQfnumber(res, "range_empty_frac"); + i_range_bounds_histogram = PQfnumber(res, "range_bounds_histogram"); + + /* restore attribute stats */ + for (; rownum < PQntuples(res); rownum++) + { + const char *attname; + + /* Stop if the next stat row in our cache isn't for this relation. */ + if (strcmp(te->tag, PQgetvalue(res, rownum, i_tablename)) != 0 || + strcmp(te->namespace, PQgetvalue(res, rownum, i_schemaname)) != 0) + break; + + appendPQExpBufferStr(out, "SELECT * FROM pg_catalog.pg_restore_attribute_stats(\n"); + appendPQExpBuffer(out, "\t'version', '%d'::integer,\n", + fout->remoteVersion); + appendPQExpBufferStr(out, "\t'schemaname', "); + appendStringLiteralAH(out, rsinfo->dobj.namespace->dobj.name, fout); + appendPQExpBufferStr(out, ",\n\t'relname', "); + appendStringLiteralAH(out, rsinfo->dobj.name, fout); + + if (PQgetisnull(res, rownum, i_attname)) + pg_fatal("unexpected null attname"); + attname = PQgetvalue(res, rownum, i_attname); + + /* + * Indexes look up attname in indAttNames to derive attnum, all others + * use attname directly. We must specify attnum for indexes, since + * their attnames are not necessarily stable across dump/reload. + */ + if (rsinfo->nindAttNames == 0) + { + appendPQExpBufferStr(out, ",\n\t'attname', "); + appendStringLiteralAH(out, attname, fout); + } + else + { + bool found = false; + + for (int i = 0; i < rsinfo->nindAttNames; i++) + { + if (strcmp(attname, rsinfo->indAttNames[i]) == 0) + { + appendPQExpBuffer(out, ",\n\t'attnum', '%d'::smallint", + i + 1); + found = true; + break; + } + } + + if (!found) + pg_fatal("could not find index attname \"%s\"", attname); + } + + if (!PQgetisnull(res, rownum, i_inherited)) + appendNamedArgument(out, fout, "inherited", "boolean", + PQgetvalue(res, rownum, i_inherited)); + if (!PQgetisnull(res, rownum, i_null_frac)) + appendNamedArgument(out, fout, "null_frac", "real", + PQgetvalue(res, rownum, i_null_frac)); + if (!PQgetisnull(res, rownum, i_avg_width)) + appendNamedArgument(out, fout, "avg_width", "integer", + PQgetvalue(res, rownum, i_avg_width)); + if (!PQgetisnull(res, rownum, i_n_distinct)) + appendNamedArgument(out, fout, "n_distinct", "real", + PQgetvalue(res, rownum, i_n_distinct)); + if (!PQgetisnull(res, rownum, i_most_common_vals)) + appendNamedArgument(out, fout, "most_common_vals", "text", + PQgetvalue(res, rownum, i_most_common_vals)); + if (!PQgetisnull(res, rownum, i_most_common_freqs)) + appendNamedArgument(out, fout, "most_common_freqs", "real[]", + PQgetvalue(res, rownum, i_most_common_freqs)); + if (!PQgetisnull(res, rownum, i_histogram_bounds)) + appendNamedArgument(out, fout, "histogram_bounds", "text", + PQgetvalue(res, rownum, i_histogram_bounds)); + if (!PQgetisnull(res, rownum, i_correlation)) + appendNamedArgument(out, fout, "correlation", "real", + PQgetvalue(res, rownum, i_correlation)); + if (!PQgetisnull(res, rownum, i_most_common_elems)) + appendNamedArgument(out, fout, "most_common_elems", "text", + PQgetvalue(res, rownum, i_most_common_elems)); + if (!PQgetisnull(res, rownum, i_most_common_elem_freqs)) + appendNamedArgument(out, fout, "most_common_elem_freqs", "real[]", + PQgetvalue(res, rownum, i_most_common_elem_freqs)); + if (!PQgetisnull(res, rownum, i_elem_count_histogram)) + appendNamedArgument(out, fout, "elem_count_histogram", "real[]", + PQgetvalue(res, rownum, i_elem_count_histogram)); + if (fout->remoteVersion >= 170000) + { + if (!PQgetisnull(res, rownum, i_range_length_histogram)) + appendNamedArgument(out, fout, "range_length_histogram", "text", + PQgetvalue(res, rownum, i_range_length_histogram)); + if (!PQgetisnull(res, rownum, i_range_empty_frac)) + appendNamedArgument(out, fout, "range_empty_frac", "real", + PQgetvalue(res, rownum, i_range_empty_frac)); + if (!PQgetisnull(res, rownum, i_range_bounds_histogram)) + appendNamedArgument(out, fout, "range_bounds_histogram", "text", + PQgetvalue(res, rownum, i_range_bounds_histogram)); + } + appendPQExpBufferStr(out, "\n);\n"); + } + + destroyPQExpBuffer(query); + return out->data; +} + +/* + * dumpRelationStats -- + * + * Make an ArchiveEntry for the relation statistics. The Archiver will take + * care of gathering the statistics and generating the restore commands when + * they are needed. + */ +static void +dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo) +{ + const DumpableObject *dobj = &rsinfo->dobj; + + /* nothing to do if we are not dumping statistics */ + if (!fout->dopt->dumpStatistics) + return; + + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = dobj->name, + .namespace = dobj->namespace->dobj.name, + .description = "STATISTICS DATA", + .section = rsinfo->section, + .defnFn = dumpRelationStats_dumper, + .defnArg = rsinfo, + .deps = dobj->dependencies, + .nDeps = dobj->nDeps)); +} + +/* + * dumpTableComment -- + * + * As above, but dump comments for both the specified table (or view) + * and its columns. + */ +static void +dumpTableComment(Archive *fout, const TableInfo *tbinfo, + const char *reltypename) +{ + DumpOptions *dopt = fout->dopt; + CommentItem *comments; + int ncomments; + PQExpBuffer query; + PQExpBuffer tag; + + /* do nothing, if --no-comments is supplied */ + if (dopt->no_comments) + return; + + /* Comments are SCHEMA not data */ + if (!dopt->dumpSchema) + return; + + /* Search for comments associated with relation, using table */ + ncomments = findComments(tbinfo->dobj.catId.tableoid, + tbinfo->dobj.catId.oid, + &comments); + + /* If comments exist, build COMMENT ON statements */ + if (ncomments <= 0) + return; + + query = createPQExpBuffer(); + tag = createPQExpBuffer(); + + while (ncomments > 0) + { + const char *descr = comments->descr; + int objsubid = comments->objsubid; + + if (objsubid == 0) + { + resetPQExpBuffer(tag); + appendPQExpBuffer(tag, "%s %s", reltypename, + fmtId(tbinfo->dobj.name)); + + resetPQExpBuffer(query); + appendPQExpBuffer(query, "COMMENT ON %s %s IS ", reltypename, + fmtQualifiedDumpable(tbinfo)); + appendStringLiteralAH(query, descr, fout); + appendPQExpBufferStr(query, ";\n"); + + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = tag->data, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "COMMENT", + .section = SECTION_NONE, + .createStmt = query->data, + .deps = &(tbinfo->dobj.dumpId), + .nDeps = 1)); + } + else if (objsubid > 0 && objsubid <= tbinfo->numatts) + { + resetPQExpBuffer(tag); + appendPQExpBuffer(tag, "COLUMN %s.", + fmtId(tbinfo->dobj.name)); + appendPQExpBufferStr(tag, fmtId(tbinfo->attnames[objsubid - 1])); + + resetPQExpBuffer(query); + appendPQExpBuffer(query, "COMMENT ON COLUMN %s.", + fmtQualifiedDumpable(tbinfo)); + appendPQExpBuffer(query, "%s IS ", + fmtId(tbinfo->attnames[objsubid - 1])); + appendStringLiteralAH(query, descr, fout); + appendPQExpBufferStr(query, ";\n"); + + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = tag->data, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "COMMENT", + .section = SECTION_NONE, + .createStmt = query->data, + .deps = &(tbinfo->dobj.dumpId), + .nDeps = 1)); + } + + comments++; + ncomments--; + } + + destroyPQExpBuffer(query); + destroyPQExpBuffer(tag); +} + +/* + * findComments -- + * + * Find the comment(s), if any, associated with the given object. All the + * objsubid values associated with the given classoid/objoid are found with + * one search. + */ +static int +findComments(Oid classoid, Oid objoid, CommentItem **items) +{ + CommentItem *middle = NULL; + CommentItem *low; + CommentItem *high; + int nmatch; + + /* + * Do binary search to find some item matching the object. + */ + low = &comments[0]; + high = &comments[ncomments - 1]; + while (low <= high) + { + middle = low + (high - low) / 2; + + if (classoid < middle->classoid) + high = middle - 1; + else if (classoid > middle->classoid) + low = middle + 1; + else if (objoid < middle->objoid) + high = middle - 1; + else if (objoid > middle->objoid) + low = middle + 1; + else + break; /* found a match */ + } + + if (low > high) /* no matches */ + { + *items = NULL; + return 0; + } + + /* + * Now determine how many items match the object. The search loop + * invariant still holds: only items between low and high inclusive could + * match. + */ + nmatch = 1; + while (middle > low) + { + if (classoid != middle[-1].classoid || + objoid != middle[-1].objoid) + break; + middle--; + nmatch++; + } + + *items = middle; + + middle += nmatch; + while (middle <= high) + { + if (classoid != middle->classoid || + objoid != middle->objoid) + break; + middle++; + nmatch++; + } + + return nmatch; +} + +/* + * collectComments -- + * + * Construct a table of all comments available for database objects; + * also set the has-comment component flag for each relevant object. + * + * We used to do per-object queries for the comments, but it's much faster + * to pull them all over at once, and on most databases the memory cost + * isn't high. + * + * The table is sorted by classoid/objid/objsubid for speed in lookup. + */ +static void +collectComments(Archive *fout) +{ + PGresult *res; + PQExpBuffer query; + int i_description; + int i_classoid; + int i_objoid; + int i_objsubid; + int ntups; + int i; + DumpableObject *dobj; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid " + "FROM pg_catalog.pg_description " + "ORDER BY classoid, objoid, objsubid"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + /* Construct lookup table containing OIDs in numeric form */ + + i_description = PQfnumber(res, "description"); + i_classoid = PQfnumber(res, "classoid"); + i_objoid = PQfnumber(res, "objoid"); + i_objsubid = PQfnumber(res, "objsubid"); + + ntups = PQntuples(res); + + comments = pg_malloc_array(CommentItem, ntups); + ncomments = 0; + dobj = NULL; + + for (i = 0; i < ntups; i++) + { + CatalogId objId; + int subid; + + objId.tableoid = atooid(PQgetvalue(res, i, i_classoid)); + objId.oid = atooid(PQgetvalue(res, i, i_objoid)); + subid = atoi(PQgetvalue(res, i, i_objsubid)); + + /* We needn't remember comments that don't match any dumpable object */ + if (dobj == NULL || + dobj->catId.tableoid != objId.tableoid || + dobj->catId.oid != objId.oid) + dobj = findObjectByCatalogId(objId); + if (dobj == NULL) + continue; + + /* + * Comments on columns of composite types are linked to the type's + * pg_class entry, but we need to set the DUMP_COMPONENT_COMMENT flag + * in the type's own DumpableObject. + */ + if (subid != 0 && dobj->objType == DO_TABLE && + ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE) + { + TypeInfo *cTypeInfo; + + cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype); + if (cTypeInfo) + cTypeInfo->dobj.components |= DUMP_COMPONENT_COMMENT; + } + else + dobj->components |= DUMP_COMPONENT_COMMENT; + + comments[ncomments].descr = pg_strdup(PQgetvalue(res, i, i_description)); + comments[ncomments].classoid = objId.tableoid; + comments[ncomments].objoid = objId.oid; + comments[ncomments].objsubid = subid; + ncomments++; + } + + PQclear(res); + destroyPQExpBuffer(query); +} + +/* + * dumpDumpableObject + * + * This routine and its subsidiaries are responsible for creating + * ArchiveEntries (TOC objects) for each object to be dumped. + */ +static void +dumpDumpableObject(Archive *fout, DumpableObject *dobj) +{ + /* + * Clear any dump-request bits for components that don't exist for this + * object. (This makes it safe to initially use DUMP_COMPONENT_ALL as the + * request for every kind of object.) + */ + dobj->dump &= dobj->components; + + /* Now, short-circuit if there's nothing to be done here. */ + if (dobj->dump == 0) + return; + + switch (dobj->objType) + { + case DO_NAMESPACE: + dumpNamespace(fout, (const NamespaceInfo *) dobj); + break; + case DO_EXTENSION: + dumpExtension(fout, (const ExtensionInfo *) dobj); + break; + case DO_TYPE: + dumpType(fout, (const TypeInfo *) dobj); + break; + case DO_SHELL_TYPE: + dumpShellType(fout, (const ShellTypeInfo *) dobj); + break; + case DO_FUNC: + dumpFunc(fout, (const FuncInfo *) dobj); + break; + case DO_AGG: + dumpAgg(fout, (const AggInfo *) dobj); + break; + case DO_OPERATOR: + dumpOpr(fout, (const OprInfo *) dobj); + break; + case DO_ACCESS_METHOD: + dumpAccessMethod(fout, (const AccessMethodInfo *) dobj); + break; + case DO_OPCLASS: + dumpOpclass(fout, (const OpclassInfo *) dobj); + break; + case DO_OPFAMILY: + dumpOpfamily(fout, (const OpfamilyInfo *) dobj); + break; + case DO_COLLATION: + dumpCollation(fout, (const CollInfo *) dobj); + break; + case DO_CONVERSION: + dumpConversion(fout, (const ConvInfo *) dobj); + break; + case DO_TABLE: + dumpTable(fout, (const TableInfo *) dobj); + break; + case DO_TABLE_ATTACH: + dumpTableAttach(fout, (const TableAttachInfo *) dobj); + break; + case DO_ATTRDEF: + dumpAttrDef(fout, (const AttrDefInfo *) dobj); + break; + case DO_INDEX: + dumpIndex(fout, (const IndxInfo *) dobj); + break; + case DO_INDEX_ATTACH: + dumpIndexAttach(fout, (const IndexAttachInfo *) dobj); + break; + case DO_STATSEXT: + dumpStatisticsExt(fout, (const StatsExtInfo *) dobj); + dumpStatisticsExtStats(fout, (const StatsExtInfo *) dobj); + break; + case DO_REFRESH_MATVIEW: + refreshMatViewData(fout, (const TableDataInfo *) dobj); + break; + case DO_RULE: + dumpRule(fout, (const RuleInfo *) dobj); + break; + case DO_TRIGGER: + dumpTrigger(fout, (const TriggerInfo *) dobj); + break; + case DO_EVENT_TRIGGER: + dumpEventTrigger(fout, (const EventTriggerInfo *) dobj); + break; + case DO_CONSTRAINT: + dumpConstraint(fout, (const ConstraintInfo *) dobj); + break; + case DO_FK_CONSTRAINT: + dumpConstraint(fout, (const ConstraintInfo *) dobj); + break; + case DO_PROCLANG: + dumpProcLang(fout, (const ProcLangInfo *) dobj); + break; + case DO_CAST: + dumpCast(fout, (const CastInfo *) dobj); + break; + case DO_TRANSFORM: + dumpTransform(fout, (const TransformInfo *) dobj); + break; + case DO_SEQUENCE_SET: + dumpSequenceData(fout, (const TableDataInfo *) dobj); + break; + case DO_TABLE_DATA: + dumpTableData(fout, (const TableDataInfo *) dobj); + break; + case DO_DUMMY_TYPE: + /* table rowtypes and array types are never dumped separately */ + break; + case DO_TSPARSER: + dumpTSParser(fout, (const TSParserInfo *) dobj); + break; + case DO_TSDICT: + dumpTSDictionary(fout, (const TSDictInfo *) dobj); + break; + case DO_TSTEMPLATE: + dumpTSTemplate(fout, (const TSTemplateInfo *) dobj); + break; + case DO_TSCONFIG: + dumpTSConfig(fout, (const TSConfigInfo *) dobj); + break; + case DO_FDW: + dumpForeignDataWrapper(fout, (const FdwInfo *) dobj); + break; + case DO_FOREIGN_SERVER: + dumpForeignServer(fout, (const ForeignServerInfo *) dobj); + break; + case DO_DEFAULT_ACL: + dumpDefaultACL(fout, (const DefaultACLInfo *) dobj); + break; + case DO_LARGE_OBJECT: + dumpLO(fout, (const LoInfo *) dobj); + break; + case DO_LARGE_OBJECT_DATA: + if (dobj->dump & DUMP_COMPONENT_DATA) + { + LoInfo *loinfo; + TocEntry *te; + + loinfo = (LoInfo *) findObjectByDumpId(dobj->dependencies[0]); + if (loinfo == NULL) + pg_fatal("missing metadata for large objects \"%s\"", + dobj->name); + + te = ArchiveEntry(fout, dobj->catId, dobj->dumpId, + ARCHIVE_OPTS(.tag = dobj->name, + .owner = loinfo->rolname, + .description = "BLOBS", + .section = SECTION_DATA, + .deps = dobj->dependencies, + .nDeps = dobj->nDeps, + .dumpFn = dumpLOs, + .dumpArg = loinfo)); + + /* + * Set the TocEntry's dataLength in case we are doing a + * parallel dump and want to order dump jobs by table size. + * (We need some size estimate for every TocEntry with a + * DataDumper function.) We don't currently have any cheap + * way to estimate the size of LOs, but fortunately it doesn't + * matter too much as long as we get large batches of LOs + * processed reasonably early. Assume 8K per blob. + */ + te->dataLength = loinfo->numlos * (pgoff_t) 8192; + } + break; + case DO_POLICY: + dumpPolicy(fout, (const PolicyInfo *) dobj); + break; + case DO_PUBLICATION: + dumpPublication(fout, (const PublicationInfo *) dobj); + break; + case DO_PUBLICATION_REL: + dumpPublicationTable(fout, (const PublicationRelInfo *) dobj); + break; + case DO_PUBLICATION_TABLE_IN_SCHEMA: + dumpPublicationNamespace(fout, + (const PublicationSchemaInfo *) dobj); + break; + case DO_SUBSCRIPTION: + dumpSubscription(fout, (const SubscriptionInfo *) dobj); + break; + case DO_SUBSCRIPTION_REL: + dumpSubscriptionTable(fout, (const SubRelInfo *) dobj); + break; + case DO_REL_STATS: + dumpRelationStats(fout, (const RelStatsInfo *) dobj); + break; + case DO_PRE_DATA_BOUNDARY: + case DO_POST_DATA_BOUNDARY: + /* never dumped, nothing to do */ + break; + } +} + +/* + * dumpNamespace + * writes out to fout the queries to recreate a user-defined namespace + */ +static void +dumpNamespace(Archive *fout, const NamespaceInfo *nspinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + char *qnspname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qnspname = pg_strdup(fmtId(nspinfo->dobj.name)); + + if (nspinfo->create) + { + appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname); + appendPQExpBuffer(q, "CREATE SCHEMA %s;\n", qnspname); + } + else + { + /* see selectDumpableNamespace() */ + appendPQExpBufferStr(delq, + "-- *not* dropping schema, since initdb creates it\n"); + appendPQExpBufferStr(q, + "-- *not* creating schema, since initdb creates it\n"); + } + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &nspinfo->dobj, + "SCHEMA", qnspname, NULL); + + if (nspinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = nspinfo->dobj.name, + .owner = nspinfo->rolname, + .description = "SCHEMA", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Schema Comments and Security Labels */ + if (nspinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + { + const char *initdb_comment = NULL; + + if (!nspinfo->create && strcmp(qnspname, "public") == 0) + initdb_comment = "standard public schema"; + dumpCommentExtended(fout, "SCHEMA", qnspname, + NULL, nspinfo->rolname, + nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId, + initdb_comment); + } + + if (nspinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "SCHEMA", qnspname, + NULL, nspinfo->rolname, + nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId); + + if (nspinfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, nspinfo->dobj.dumpId, InvalidDumpId, "SCHEMA", + qnspname, NULL, NULL, + NULL, nspinfo->rolname, &nspinfo->dacl); + + free(qnspname); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); +} + +/* + * dumpExtension + * writes out to fout the queries to recreate an extension + */ +static void +dumpExtension(Archive *fout, const ExtensionInfo *extinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + char *qextname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qextname = pg_strdup(fmtId(extinfo->dobj.name)); + + appendPQExpBuffer(delq, "DROP EXTENSION %s;\n", qextname); + + if (!dopt->binary_upgrade) + { + /* + * In a regular dump, we simply create the extension, intentionally + * not specifying a version, so that the destination installation's + * default version is used. + * + * Use of IF NOT EXISTS here is unlike our behavior for other object + * types; but there are various scenarios in which it's convenient to + * manually create the desired extension before restoring, so we + * prefer to allow it to exist already. + */ + appendPQExpBuffer(q, "CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;\n", + qextname, fmtId(extinfo->namespace)); + } + else + { + /* + * In binary-upgrade mode, it's critical to reproduce the state of the + * database exactly, so our procedure is to create an empty extension, + * restore all the contained objects normally, and add them to the + * extension one by one. This function performs just the first of + * those steps. binary_upgrade_extension_member() takes care of + * adding member objects as they're created. + */ + int i; + int n; + + appendPQExpBufferStr(q, "-- For binary upgrade, create an empty extension and insert objects into it\n"); + + /* + * We unconditionally create the extension, so we must drop it if it + * exists. This could happen if the user deleted 'plpgsql' and then + * readded it, causing its oid to be greater than g_last_builtin_oid. + */ + appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname); + + appendPQExpBufferStr(q, + "SELECT pg_catalog.binary_upgrade_create_empty_extension("); + appendStringLiteralAH(q, extinfo->dobj.name, fout); + appendPQExpBufferStr(q, ", "); + appendStringLiteralAH(q, extinfo->namespace, fout); + appendPQExpBufferStr(q, ", "); + appendPQExpBuffer(q, "%s, ", extinfo->relocatable ? "true" : "false"); + appendStringLiteralAH(q, extinfo->extversion, fout); + appendPQExpBufferStr(q, ", "); + + /* + * Note that we're pushing extconfig (an OID array) back into + * pg_extension exactly as-is. This is OK because pg_class OIDs are + * preserved in binary upgrade. + */ + if (strlen(extinfo->extconfig) > 2) + appendStringLiteralAH(q, extinfo->extconfig, fout); + else + appendPQExpBufferStr(q, "NULL"); + appendPQExpBufferStr(q, ", "); + if (strlen(extinfo->extcondition) > 2) + appendStringLiteralAH(q, extinfo->extcondition, fout); + else + appendPQExpBufferStr(q, "NULL"); + appendPQExpBufferStr(q, ", "); + appendPQExpBufferStr(q, "ARRAY["); + n = 0; + for (i = 0; i < extinfo->dobj.nDeps; i++) + { + DumpableObject *extobj; + + extobj = findObjectByDumpId(extinfo->dobj.dependencies[i]); + if (extobj && extobj->objType == DO_EXTENSION) + { + if (n++ > 0) + appendPQExpBufferChar(q, ','); + appendStringLiteralAH(q, extobj->name, fout); + } + } + appendPQExpBufferStr(q, "]::pg_catalog.text[]"); + appendPQExpBufferStr(q, ");\n"); + } + + if (extinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, extinfo->dobj.catId, extinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = extinfo->dobj.name, + .description = "EXTENSION", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Extension Comments */ + if (extinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "EXTENSION", qextname, + NULL, "", + extinfo->dobj.catId, 0, extinfo->dobj.dumpId); + + free(qextname); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); +} + +/* + * dumpType + * writes out to fout the queries to recreate a user-defined type + */ +static void +dumpType(Archive *fout, const TypeInfo *tyinfo) +{ + DumpOptions *dopt = fout->dopt; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + /* Dump out in proper style */ + if (tyinfo->typtype == TYPTYPE_BASE) + dumpBaseType(fout, tyinfo); + else if (tyinfo->typtype == TYPTYPE_DOMAIN) + dumpDomain(fout, tyinfo); + else if (tyinfo->typtype == TYPTYPE_COMPOSITE) + dumpCompositeType(fout, tyinfo); + else if (tyinfo->typtype == TYPTYPE_ENUM) + dumpEnumType(fout, tyinfo); + else if (tyinfo->typtype == TYPTYPE_RANGE) + dumpRangeType(fout, tyinfo); + else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined) + dumpUndefinedType(fout, tyinfo); + else + pg_log_warning("typtype of data type \"%s\" appears to be invalid", + tyinfo->dobj.name); +} + +/* + * dumpEnumType + * writes out to fout the queries to recreate a user-defined enum type + */ +static void +dumpEnumType(Archive *fout, const TypeInfo *tyinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q = createPQExpBuffer(); + PQExpBuffer delq = createPQExpBuffer(); + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + int num, + i; + Oid enum_oid; + char *qtypname; + char *qualtypname; + char *label; + int i_enumlabel; + int i_oid; + + if (!fout->is_prepared[PREPQUERY_DUMPENUMTYPE]) + { + /* Set up query for enum-specific details */ + appendPQExpBufferStr(query, + "PREPARE dumpEnumType(pg_catalog.oid) AS\n" + "SELECT oid, enumlabel " + "FROM pg_catalog.pg_enum " + "WHERE enumtypid = $1 " + "ORDER BY enumsortorder"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_DUMPENUMTYPE] = true; + } + + printfPQExpBuffer(query, + "EXECUTE dumpEnumType('%u')", + tyinfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + num = PQntuples(res); + + qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); + qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); + + /* + * CASCADE shouldn't be required here as for normal types since the I/O + * functions are generic and do not get dropped. + */ + appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname); + + if (dopt->binary_upgrade) + binary_upgrade_set_type_oids_by_type_oid(fout, q, + tyinfo->dobj.catId.oid, + false, false); + + appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (", + qualtypname); + + if (!dopt->binary_upgrade) + { + i_enumlabel = PQfnumber(res, "enumlabel"); + + /* Labels with server-assigned oids */ + for (i = 0; i < num; i++) + { + label = PQgetvalue(res, i, i_enumlabel); + if (i > 0) + appendPQExpBufferChar(q, ','); + appendPQExpBufferStr(q, "\n "); + appendStringLiteralAH(q, label, fout); + } + } + + appendPQExpBufferStr(q, "\n);\n"); + + if (dopt->binary_upgrade) + { + i_oid = PQfnumber(res, "oid"); + i_enumlabel = PQfnumber(res, "enumlabel"); + + /* Labels with dump-assigned (preserved) oids */ + for (i = 0; i < num; i++) + { + enum_oid = atooid(PQgetvalue(res, i, i_oid)); + label = PQgetvalue(res, i, i_enumlabel); + + if (i == 0) + appendPQExpBufferStr(q, "\n-- For binary upgrade, must preserve pg_enum oids\n"); + appendPQExpBuffer(q, + "SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('%u'::pg_catalog.oid);\n", + enum_oid); + appendPQExpBuffer(q, "ALTER TYPE %s ADD VALUE ", qualtypname); + appendStringLiteralAH(q, label, fout); + appendPQExpBufferStr(q, ";\n\n"); + } + } + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &tyinfo->dobj, + "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tyinfo->dobj.name, + .namespace = tyinfo->dobj.namespace->dobj.name, + .owner = tyinfo->rolname, + .description = "TYPE", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Type Comments and Security Labels */ + if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", + qtypname, NULL, + tyinfo->dobj.namespace->dobj.name, + NULL, tyinfo->rolname, &tyinfo->dacl); + + PQclear(res); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qtypname); + free(qualtypname); +} + +/* + * dumpRangeType + * writes out to fout the queries to recreate a user-defined range type + */ +static void +dumpRangeType(Archive *fout, const TypeInfo *tyinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q = createPQExpBuffer(); + PQExpBuffer delq = createPQExpBuffer(); + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + Oid collationOid; + char *qtypname; + char *qualtypname; + char *procname; + + if (!fout->is_prepared[PREPQUERY_DUMPRANGETYPE]) + { + /* Set up query for range-specific details */ + appendPQExpBufferStr(query, + "PREPARE dumpRangeType(pg_catalog.oid) AS\n"); + + appendPQExpBufferStr(query, + "SELECT "); + + if (fout->remoteVersion >= 140000) + appendPQExpBufferStr(query, + "pg_catalog.format_type(rngmultitypid, NULL) AS rngmultitype, "); + else + appendPQExpBufferStr(query, + "NULL AS rngmultitype, "); + + appendPQExpBufferStr(query, + "pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, " + "opc.opcname AS opcname, " + "(SELECT nspname FROM pg_catalog.pg_namespace nsp " + " WHERE nsp.oid = opc.opcnamespace) AS opcnsp, " + "opc.opcdefault, " + "CASE WHEN rngcollation = st.typcollation THEN 0 " + " ELSE rngcollation END AS collation, " + "rngcanonical, rngsubdiff " + "FROM pg_catalog.pg_range r, pg_catalog.pg_type st, " + " pg_catalog.pg_opclass opc " + "WHERE st.oid = rngsubtype AND opc.oid = rngsubopc AND " + "rngtypid = $1"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_DUMPRANGETYPE] = true; + } + + printfPQExpBuffer(query, + "EXECUTE dumpRangeType('%u')", + tyinfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); + qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); + + /* + * CASCADE shouldn't be required here as for normal types since the I/O + * functions are generic and do not get dropped. + */ + appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname); + + if (dopt->binary_upgrade) + binary_upgrade_set_type_oids_by_type_oid(fout, q, + tyinfo->dobj.catId.oid, + false, true); + + appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (", + qualtypname); + + appendPQExpBuffer(q, "\n subtype = %s", + PQgetvalue(res, 0, PQfnumber(res, "rngsubtype"))); + + if (!PQgetisnull(res, 0, PQfnumber(res, "rngmultitype"))) + appendPQExpBuffer(q, ",\n multirange_type_name = %s", + PQgetvalue(res, 0, PQfnumber(res, "rngmultitype"))); + + /* print subtype_opclass only if not default for subtype */ + if (PQgetvalue(res, 0, PQfnumber(res, "opcdefault"))[0] != 't') + { + char *opcname = PQgetvalue(res, 0, PQfnumber(res, "opcname")); + char *nspname = PQgetvalue(res, 0, PQfnumber(res, "opcnsp")); + + appendPQExpBuffer(q, ",\n subtype_opclass = %s.", + fmtId(nspname)); + appendPQExpBufferStr(q, fmtId(opcname)); + } + + collationOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "collation"))); + if (OidIsValid(collationOid)) + { + CollInfo *coll = findCollationByOid(collationOid); + + if (coll) + appendPQExpBuffer(q, ",\n collation = %s", + fmtQualifiedDumpable(coll)); + } + + procname = PQgetvalue(res, 0, PQfnumber(res, "rngcanonical")); + if (strcmp(procname, "-") != 0) + appendPQExpBuffer(q, ",\n canonical = %s", procname); + + procname = PQgetvalue(res, 0, PQfnumber(res, "rngsubdiff")); + if (strcmp(procname, "-") != 0) + appendPQExpBuffer(q, ",\n subtype_diff = %s", procname); + + appendPQExpBufferStr(q, "\n);\n"); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &tyinfo->dobj, + "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tyinfo->dobj.name, + .namespace = tyinfo->dobj.namespace->dobj.name, + .owner = tyinfo->rolname, + .description = "TYPE", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Type Comments and Security Labels */ + if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", + qtypname, NULL, + tyinfo->dobj.namespace->dobj.name, + NULL, tyinfo->rolname, &tyinfo->dacl); + + PQclear(res); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qtypname); + free(qualtypname); +} + +/* + * dumpUndefinedType + * writes out to fout the queries to recreate a !typisdefined type + * + * This is a shell type, but we use different terminology to distinguish + * this case from where we have to emit a shell type definition to break + * circular dependencies. An undefined type shouldn't ever have anything + * depending on it. + */ +static void +dumpUndefinedType(Archive *fout, const TypeInfo *tyinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q = createPQExpBuffer(); + PQExpBuffer delq = createPQExpBuffer(); + char *qtypname; + char *qualtypname; + + qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); + qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); + + appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname); + + if (dopt->binary_upgrade) + binary_upgrade_set_type_oids_by_type_oid(fout, q, + tyinfo->dobj.catId.oid, + false, false); + + appendPQExpBuffer(q, "CREATE TYPE %s;\n", + qualtypname); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &tyinfo->dobj, + "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tyinfo->dobj.name, + .namespace = tyinfo->dobj.namespace->dobj.name, + .owner = tyinfo->rolname, + .description = "TYPE", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Type Comments and Security Labels */ + if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", + qtypname, NULL, + tyinfo->dobj.namespace->dobj.name, + NULL, tyinfo->rolname, &tyinfo->dacl); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + free(qtypname); + free(qualtypname); +} + +/* + * dumpBaseType + * writes out to fout the queries to recreate a user-defined base type + */ +static void +dumpBaseType(Archive *fout, const TypeInfo *tyinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q = createPQExpBuffer(); + PQExpBuffer delq = createPQExpBuffer(); + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + char *qtypname; + char *qualtypname; + char *typlen; + char *typinput; + char *typoutput; + char *typreceive; + char *typsend; + char *typmodin; + char *typmodout; + char *typanalyze; + char *typsubscript; + Oid typreceiveoid; + Oid typsendoid; + Oid typmodinoid; + Oid typmodoutoid; + Oid typanalyzeoid; + Oid typsubscriptoid; + char *typcategory; + char *typispreferred; + char *typdelim; + char *typbyval; + char *typalign; + char *typstorage; + char *typcollatable; + char *typdefault; + bool typdefault_is_literal = false; + + if (!fout->is_prepared[PREPQUERY_DUMPBASETYPE]) + { + /* Set up query for type-specific details */ + appendPQExpBufferStr(query, + "PREPARE dumpBaseType(pg_catalog.oid) AS\n" + "SELECT typlen, " + "typinput, typoutput, typreceive, typsend, " + "typreceive::pg_catalog.oid AS typreceiveoid, " + "typsend::pg_catalog.oid AS typsendoid, " + "typanalyze, " + "typanalyze::pg_catalog.oid AS typanalyzeoid, " + "typdelim, typbyval, typalign, typstorage, " + "typmodin, typmodout, " + "typmodin::pg_catalog.oid AS typmodinoid, " + "typmodout::pg_catalog.oid AS typmodoutoid, " + "typcategory, typispreferred, " + "(typcollation <> 0) AS typcollatable, " + "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault, "); + + if (fout->remoteVersion >= 140000) + appendPQExpBufferStr(query, + "typsubscript, " + "typsubscript::pg_catalog.oid AS typsubscriptoid "); + else + appendPQExpBufferStr(query, + "'-' AS typsubscript, 0 AS typsubscriptoid "); + + appendPQExpBufferStr(query, "FROM pg_catalog.pg_type " + "WHERE oid = $1"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_DUMPBASETYPE] = true; + } + + printfPQExpBuffer(query, + "EXECUTE dumpBaseType('%u')", + tyinfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen")); + typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput")); + typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput")); + typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive")); + typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend")); + typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin")); + typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout")); + typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze")); + typsubscript = PQgetvalue(res, 0, PQfnumber(res, "typsubscript")); + typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid"))); + typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid"))); + typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid"))); + typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid"))); + typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid"))); + typsubscriptoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsubscriptoid"))); + typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory")); + typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred")); + typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim")); + typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval")); + typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign")); + typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage")); + typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable")); + if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin"))) + typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin")); + else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault"))) + { + typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault")); + typdefault_is_literal = true; /* it needs quotes */ + } + else + typdefault = NULL; + + qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); + qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); + + /* + * The reason we include CASCADE is that the circular dependency between + * the type and its I/O functions makes it impossible to drop the type any + * other way. + */ + appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname); + + /* + * We might already have a shell type, but setting pg_type_oid is + * harmless, and in any case we'd better set the array type OID. + */ + if (dopt->binary_upgrade) + binary_upgrade_set_type_oids_by_type_oid(fout, q, + tyinfo->dobj.catId.oid, + false, false); + + appendPQExpBuffer(q, + "CREATE TYPE %s (\n" + " INTERNALLENGTH = %s", + qualtypname, + (strcmp(typlen, "-1") == 0) ? "variable" : typlen); + + /* regproc result is sufficiently quoted already */ + appendPQExpBuffer(q, ",\n INPUT = %s", typinput); + appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput); + if (OidIsValid(typreceiveoid)) + appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive); + if (OidIsValid(typsendoid)) + appendPQExpBuffer(q, ",\n SEND = %s", typsend); + if (OidIsValid(typmodinoid)) + appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin); + if (OidIsValid(typmodoutoid)) + appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout); + if (OidIsValid(typanalyzeoid)) + appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze); + + if (strcmp(typcollatable, "t") == 0) + appendPQExpBufferStr(q, ",\n COLLATABLE = true"); + + if (typdefault != NULL) + { + appendPQExpBufferStr(q, ",\n DEFAULT = "); + if (typdefault_is_literal) + appendStringLiteralAH(q, typdefault, fout); + else + appendPQExpBufferStr(q, typdefault); + } + + if (OidIsValid(typsubscriptoid)) + appendPQExpBuffer(q, ",\n SUBSCRIPT = %s", typsubscript); + + if (OidIsValid(tyinfo->typelem)) + appendPQExpBuffer(q, ",\n ELEMENT = %s", + getFormattedTypeName(fout, tyinfo->typelem, + zeroIsError)); + + if (strcmp(typcategory, "U") != 0) + { + appendPQExpBufferStr(q, ",\n CATEGORY = "); + appendStringLiteralAH(q, typcategory, fout); + } + + if (strcmp(typispreferred, "t") == 0) + appendPQExpBufferStr(q, ",\n PREFERRED = true"); + + if (typdelim && strcmp(typdelim, ",") != 0) + { + appendPQExpBufferStr(q, ",\n DELIMITER = "); + appendStringLiteralAH(q, typdelim, fout); + } + + if (*typalign == TYPALIGN_CHAR) + appendPQExpBufferStr(q, ",\n ALIGNMENT = char"); + else if (*typalign == TYPALIGN_SHORT) + appendPQExpBufferStr(q, ",\n ALIGNMENT = int2"); + else if (*typalign == TYPALIGN_INT) + appendPQExpBufferStr(q, ",\n ALIGNMENT = int4"); + else if (*typalign == TYPALIGN_DOUBLE) + appendPQExpBufferStr(q, ",\n ALIGNMENT = double"); + + if (*typstorage == TYPSTORAGE_PLAIN) + appendPQExpBufferStr(q, ",\n STORAGE = plain"); + else if (*typstorage == TYPSTORAGE_EXTERNAL) + appendPQExpBufferStr(q, ",\n STORAGE = external"); + else if (*typstorage == TYPSTORAGE_EXTENDED) + appendPQExpBufferStr(q, ",\n STORAGE = extended"); + else if (*typstorage == TYPSTORAGE_MAIN) + appendPQExpBufferStr(q, ",\n STORAGE = main"); + + if (strcmp(typbyval, "t") == 0) + appendPQExpBufferStr(q, ",\n PASSEDBYVALUE"); + + appendPQExpBufferStr(q, "\n);\n"); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &tyinfo->dobj, + "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tyinfo->dobj.name, + .namespace = tyinfo->dobj.namespace->dobj.name, + .owner = tyinfo->rolname, + .description = "TYPE", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Type Comments and Security Labels */ + if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", + qtypname, NULL, + tyinfo->dobj.namespace->dobj.name, + NULL, tyinfo->rolname, &tyinfo->dacl); + + PQclear(res); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qtypname); + free(qualtypname); +} + +/* + * dumpDomain + * writes out to fout the queries to recreate a user-defined domain + */ +static void +dumpDomain(Archive *fout, const TypeInfo *tyinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q = createPQExpBuffer(); + PQExpBuffer delq = createPQExpBuffer(); + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + int i; + char *qtypname; + char *qualtypname; + char *typnotnull; + char *typdefn; + char *typdefault; + Oid typcollation; + bool typdefault_is_literal = false; + + if (!fout->is_prepared[PREPQUERY_DUMPDOMAIN]) + { + /* Set up query for domain-specific details */ + appendPQExpBufferStr(query, + "PREPARE dumpDomain(pg_catalog.oid) AS\n"); + + appendPQExpBufferStr(query, "SELECT t.typnotnull, " + "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, " + "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, " + "t.typdefault, " + "CASE WHEN t.typcollation <> u.typcollation " + "THEN t.typcollation ELSE 0 END AS typcollation " + "FROM pg_catalog.pg_type t " + "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) " + "WHERE t.oid = $1"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_DUMPDOMAIN] = true; + } + + printfPQExpBuffer(query, + "EXECUTE dumpDomain('%u')", + tyinfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull")); + typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn")); + if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin"))) + typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin")); + else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault"))) + { + typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault")); + typdefault_is_literal = true; /* it needs quotes */ + } + else + typdefault = NULL; + typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation"))); + + if (dopt->binary_upgrade) + binary_upgrade_set_type_oids_by_type_oid(fout, q, + tyinfo->dobj.catId.oid, + true, /* force array type */ + false); /* force multirange type */ + + qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); + qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); + + appendPQExpBuffer(q, + "CREATE DOMAIN %s AS %s", + qualtypname, + typdefn); + + /* Print collation only if different from base type's collation */ + if (OidIsValid(typcollation)) + { + CollInfo *coll; + + coll = findCollationByOid(typcollation); + if (coll) + appendPQExpBuffer(q, " COLLATE %s", fmtQualifiedDumpable(coll)); + } + + /* + * Print a not-null constraint if there's one. In servers older than 17 + * these don't have names, so just print it unadorned; in newer ones they + * do, but most of the time it's going to be the standard generated one, + * so omit the name in that case also. + */ + if (typnotnull[0] == 't') + { + if (fout->remoteVersion < 170000 || tyinfo->notnull == NULL) + appendPQExpBufferStr(q, " NOT NULL"); + else + { + ConstraintInfo *notnull = tyinfo->notnull; + + if (!notnull->separate) + { + char *default_name; + + /* XXX should match ChooseConstraintName better */ + default_name = psprintf("%s_not_null", tyinfo->dobj.name); + + if (strcmp(default_name, notnull->dobj.name) == 0) + appendPQExpBufferStr(q, " NOT NULL"); + else + appendPQExpBuffer(q, " CONSTRAINT %s %s", + fmtId(notnull->dobj.name), notnull->condef); + free(default_name); + } + } + } + + if (typdefault != NULL) + { + appendPQExpBufferStr(q, " DEFAULT "); + if (typdefault_is_literal) + appendStringLiteralAH(q, typdefault, fout); + else + appendPQExpBufferStr(q, typdefault); + } + + PQclear(res); + + /* + * Add any CHECK constraints for the domain + */ + for (i = 0; i < tyinfo->nDomChecks; i++) + { + ConstraintInfo *domcheck = &(tyinfo->domChecks[i]); + + if (!domcheck->separate && domcheck->contype == 'c') + appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s", + fmtId(domcheck->dobj.name), domcheck->condef); + } + + appendPQExpBufferStr(q, ";\n"); + + appendPQExpBuffer(delq, "DROP DOMAIN %s;\n", qualtypname); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &tyinfo->dobj, + "DOMAIN", qtypname, + tyinfo->dobj.namespace->dobj.name); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tyinfo->dobj.name, + .namespace = tyinfo->dobj.namespace->dobj.name, + .owner = tyinfo->rolname, + .description = "DOMAIN", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Domain Comments and Security Labels */ + if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "DOMAIN", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "DOMAIN", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", + qtypname, NULL, + tyinfo->dobj.namespace->dobj.name, + NULL, tyinfo->rolname, &tyinfo->dacl); + + /* Dump any per-constraint comments */ + for (i = 0; i < tyinfo->nDomChecks; i++) + { + ConstraintInfo *domcheck = &(tyinfo->domChecks[i]); + PQExpBuffer conprefix; + + /* but only if the constraint itself was dumped here */ + if (domcheck->separate) + continue; + + conprefix = createPQExpBuffer(); + appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN", + fmtId(domcheck->dobj.name)); + + if (domcheck->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, conprefix->data, qtypname, + tyinfo->dobj.namespace->dobj.name, + tyinfo->rolname, + domcheck->dobj.catId, 0, tyinfo->dobj.dumpId); + + destroyPQExpBuffer(conprefix); + } + + /* + * And a comment on the not-null constraint, if there's one -- but only if + * the constraint itself was dumped here + */ + if (tyinfo->notnull != NULL && !tyinfo->notnull->separate) + { + PQExpBuffer conprefix = createPQExpBuffer(); + + appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN", + fmtId(tyinfo->notnull->dobj.name)); + + if (tyinfo->notnull->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, conprefix->data, qtypname, + tyinfo->dobj.namespace->dobj.name, + tyinfo->rolname, + tyinfo->notnull->dobj.catId, 0, tyinfo->dobj.dumpId); + destroyPQExpBuffer(conprefix); + } + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qtypname); + free(qualtypname); +} + +/* + * dumpCompositeType + * writes out to fout the queries to recreate a user-defined stand-alone + * composite type + */ +static void +dumpCompositeType(Archive *fout, const TypeInfo *tyinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q = createPQExpBuffer(); + PQExpBuffer dropped = createPQExpBuffer(); + PQExpBuffer delq = createPQExpBuffer(); + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + char *qtypname; + char *qualtypname; + int ntups; + int i_attname; + int i_atttypdefn; + int i_attlen; + int i_attalign; + int i_attisdropped; + int i_attcollation; + int i; + int actual_atts; + + if (!fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE]) + { + /* + * Set up query for type-specific details. + * + * Since we only want to dump COLLATE clauses for attributes whose + * collation is different from their type's default, we use a CASE + * here to suppress uninteresting attcollations cheaply. atttypid + * will be 0 for dropped columns; collation does not matter for those. + */ + appendPQExpBufferStr(query, + "PREPARE dumpCompositeType(pg_catalog.oid) AS\n" + "SELECT a.attname, a.attnum, " + "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, " + "a.attlen, a.attalign, a.attisdropped, " + "CASE WHEN a.attcollation <> at.typcollation " + "THEN a.attcollation ELSE 0 END AS attcollation " + "FROM pg_catalog.pg_type ct " + "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid " + "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid " + "WHERE ct.oid = $1 " + "ORDER BY a.attnum"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_DUMPCOMPOSITETYPE] = true; + } + + printfPQExpBuffer(query, + "EXECUTE dumpCompositeType('%u')", + tyinfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_attname = PQfnumber(res, "attname"); + i_atttypdefn = PQfnumber(res, "atttypdefn"); + i_attlen = PQfnumber(res, "attlen"); + i_attalign = PQfnumber(res, "attalign"); + i_attisdropped = PQfnumber(res, "attisdropped"); + i_attcollation = PQfnumber(res, "attcollation"); + + if (dopt->binary_upgrade) + { + binary_upgrade_set_type_oids_by_type_oid(fout, q, + tyinfo->dobj.catId.oid, + false, false); + binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid); + } + + qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); + qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo)); + + appendPQExpBuffer(q, "CREATE TYPE %s AS (", + qualtypname); + + actual_atts = 0; + for (i = 0; i < ntups; i++) + { + char *attname; + char *atttypdefn; + char *attlen; + char *attalign; + bool attisdropped; + Oid attcollation; + + attname = PQgetvalue(res, i, i_attname); + atttypdefn = PQgetvalue(res, i, i_atttypdefn); + attlen = PQgetvalue(res, i, i_attlen); + attalign = PQgetvalue(res, i, i_attalign); + attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't'); + attcollation = atooid(PQgetvalue(res, i, i_attcollation)); + + if (attisdropped && !dopt->binary_upgrade) + continue; + + /* Format properly if not first attr */ + if (actual_atts++ > 0) + appendPQExpBufferChar(q, ','); + appendPQExpBufferStr(q, "\n\t"); + + if (!attisdropped) + { + appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn); + + /* Add collation if not default for the column type */ + if (OidIsValid(attcollation)) + { + CollInfo *coll; + + coll = findCollationByOid(attcollation); + if (coll) + appendPQExpBuffer(q, " COLLATE %s", + fmtQualifiedDumpable(coll)); + } + } + else + { + /* + * This is a dropped attribute and we're in binary_upgrade mode. + * Insert a placeholder for it in the CREATE TYPE command, and set + * length and alignment with direct UPDATE to the catalogs + * afterwards. See similar code in dumpTableSchema(). + */ + appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname)); + + /* stash separately for insertion after the CREATE TYPE */ + appendPQExpBufferStr(dropped, + "\n-- For binary upgrade, recreate dropped column.\n"); + appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n" + "SET attlen = %s, " + "attalign = '%s', attbyval = false\n" + "WHERE attname = ", attlen, attalign); + appendStringLiteralAH(dropped, attname, fout); + appendPQExpBufferStr(dropped, "\n AND attrelid = "); + appendStringLiteralAH(dropped, qualtypname, fout); + appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n"); + + appendPQExpBuffer(dropped, "ALTER TYPE %s ", + qualtypname); + appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n", + fmtId(attname)); + } + } + appendPQExpBufferStr(q, "\n);\n"); + appendPQExpBufferStr(q, dropped->data); + + appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &tyinfo->dobj, + "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tyinfo->dobj.name, + .namespace = tyinfo->dobj.namespace->dobj.name, + .owner = tyinfo->rolname, + .description = "TYPE", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + + /* Dump Type Comments and Security Labels */ + if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "TYPE", qtypname, + tyinfo->dobj.namespace->dobj.name, tyinfo->rolname, + tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId); + + if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, tyinfo->dobj.dumpId, InvalidDumpId, "TYPE", + qtypname, NULL, + tyinfo->dobj.namespace->dobj.name, + NULL, tyinfo->rolname, &tyinfo->dacl); + + /* Dump any per-column comments */ + if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpCompositeTypeColComments(fout, tyinfo, res); + + PQclear(res); + destroyPQExpBuffer(q); + destroyPQExpBuffer(dropped); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qtypname); + free(qualtypname); +} + +/* + * dumpCompositeTypeColComments + * writes out to fout the queries to recreate comments on the columns of + * a user-defined stand-alone composite type. + * + * The caller has already made a query to collect the names and attnums + * of the type's columns, so we just pass that result into here rather + * than reading them again. + */ +static void +dumpCompositeTypeColComments(Archive *fout, const TypeInfo *tyinfo, + PGresult *res) +{ + CommentItem *comments; + int ncomments; + PQExpBuffer query; + PQExpBuffer target; + int i; + int ntups; + int i_attname; + int i_attnum; + int i_attisdropped; + + /* do nothing, if --no-comments is supplied */ + if (fout->dopt->no_comments) + return; + + /* Search for comments associated with type's pg_class OID */ + ncomments = findComments(RelationRelationId, tyinfo->typrelid, + &comments); + + /* If no comments exist, we're done */ + if (ncomments <= 0) + return; + + /* Build COMMENT ON statements */ + query = createPQExpBuffer(); + target = createPQExpBuffer(); + + ntups = PQntuples(res); + i_attnum = PQfnumber(res, "attnum"); + i_attname = PQfnumber(res, "attname"); + i_attisdropped = PQfnumber(res, "attisdropped"); + while (ncomments > 0) + { + const char *attname; + + attname = NULL; + for (i = 0; i < ntups; i++) + { + if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid && + PQgetvalue(res, i, i_attisdropped)[0] != 't') + { + attname = PQgetvalue(res, i, i_attname); + break; + } + } + if (attname) /* just in case we don't find it */ + { + const char *descr = comments->descr; + + resetPQExpBuffer(target); + appendPQExpBuffer(target, "COLUMN %s.", + fmtId(tyinfo->dobj.name)); + appendPQExpBufferStr(target, fmtId(attname)); + + resetPQExpBuffer(query); + appendPQExpBuffer(query, "COMMENT ON COLUMN %s.", + fmtQualifiedDumpable(tyinfo)); + appendPQExpBuffer(query, "%s IS ", fmtId(attname)); + appendStringLiteralAH(query, descr, fout); + appendPQExpBufferStr(query, ";\n"); + + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = target->data, + .namespace = tyinfo->dobj.namespace->dobj.name, + .owner = tyinfo->rolname, + .description = "COMMENT", + .section = SECTION_NONE, + .createStmt = query->data, + .deps = &(tyinfo->dobj.dumpId), + .nDeps = 1)); + } + + comments++; + ncomments--; + } + + destroyPQExpBuffer(query); + destroyPQExpBuffer(target); +} + +/* + * dumpShellType + * writes out to fout the queries to create a shell type + * + * We dump a shell definition in advance of the I/O functions for the type. + */ +static void +dumpShellType(Archive *fout, const ShellTypeInfo *stinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + + /* + * Note the lack of a DROP command for the shell type; any required DROP + * is driven off the base type entry, instead. This interacts with + * _printTocEntry()'s use of the presence of a DROP command to decide + * whether an entry needs an ALTER OWNER command. We don't want to alter + * the shell type's owner immediately on creation; that should happen only + * after it's filled in, otherwise the backend complains. + */ + + if (dopt->binary_upgrade) + binary_upgrade_set_type_oids_by_type_oid(fout, q, + stinfo->baseType->dobj.catId.oid, + false, false); + + appendPQExpBuffer(q, "CREATE TYPE %s;\n", + fmtQualifiedDumpable(stinfo)); + + if (stinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, stinfo->dobj.catId, stinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = stinfo->dobj.name, + .namespace = stinfo->dobj.namespace->dobj.name, + .owner = stinfo->baseType->rolname, + .description = "SHELL TYPE", + .section = SECTION_PRE_DATA, + .createStmt = q->data)); + + destroyPQExpBuffer(q); +} + +/* + * dumpProcLang + * writes out to fout the queries to recreate a user-defined + * procedural language + */ +static void +dumpProcLang(Archive *fout, const ProcLangInfo *plang) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer defqry; + PQExpBuffer delqry; + bool useParams; + char *qlanname; + FuncInfo *funcInfo; + FuncInfo *inlineInfo = NULL; + FuncInfo *validatorInfo = NULL; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + /* + * Try to find the support function(s). It is not an error if we don't + * find them --- if the functions are in the pg_catalog schema, as is + * standard in 8.1 and up, then we won't have loaded them. (In this case + * we will emit a parameterless CREATE LANGUAGE command, which will + * require PL template knowledge in the backend to reload.) + */ + + funcInfo = findFuncByOid(plang->lanplcallfoid); + if (funcInfo != NULL && !funcInfo->dobj.dump) + funcInfo = NULL; /* treat not-dumped same as not-found */ + + if (OidIsValid(plang->laninline)) + { + inlineInfo = findFuncByOid(plang->laninline); + if (inlineInfo != NULL && !inlineInfo->dobj.dump) + inlineInfo = NULL; + } + + if (OidIsValid(plang->lanvalidator)) + { + validatorInfo = findFuncByOid(plang->lanvalidator); + if (validatorInfo != NULL && !validatorInfo->dobj.dump) + validatorInfo = NULL; + } + + /* + * If the functions are dumpable then emit a complete CREATE LANGUAGE with + * parameters. Otherwise, we'll write a parameterless command, which will + * be interpreted as CREATE EXTENSION. + */ + useParams = (funcInfo != NULL && + (inlineInfo != NULL || !OidIsValid(plang->laninline)) && + (validatorInfo != NULL || !OidIsValid(plang->lanvalidator))); + + defqry = createPQExpBuffer(); + delqry = createPQExpBuffer(); + + qlanname = pg_strdup(fmtId(plang->dobj.name)); + + appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n", + qlanname); + + if (useParams) + { + appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s", + plang->lanpltrusted ? "TRUSTED " : "", + qlanname); + appendPQExpBuffer(defqry, " HANDLER %s", + fmtQualifiedDumpable(funcInfo)); + if (OidIsValid(plang->laninline)) + appendPQExpBuffer(defqry, " INLINE %s", + fmtQualifiedDumpable(inlineInfo)); + if (OidIsValid(plang->lanvalidator)) + appendPQExpBuffer(defqry, " VALIDATOR %s", + fmtQualifiedDumpable(validatorInfo)); + } + else + { + /* + * If not dumping parameters, then use CREATE OR REPLACE so that the + * command will not fail if the language is preinstalled in the target + * database. + * + * Modern servers will interpret this as CREATE EXTENSION IF NOT + * EXISTS; perhaps we should emit that instead? But it might just add + * confusion. + */ + appendPQExpBuffer(defqry, "CREATE OR REPLACE PROCEDURAL LANGUAGE %s", + qlanname); + } + appendPQExpBufferStr(defqry, ";\n"); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(defqry, &plang->dobj, + "LANGUAGE", qlanname, NULL); + + if (plang->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId, + ARCHIVE_OPTS(.tag = plang->dobj.name, + .owner = plang->lanowner, + .description = "PROCEDURAL LANGUAGE", + .section = SECTION_PRE_DATA, + .createStmt = defqry->data, + .dropStmt = delqry->data, + )); + + /* Dump Proc Lang Comments and Security Labels */ + if (plang->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "LANGUAGE", qlanname, + NULL, plang->lanowner, + plang->dobj.catId, 0, plang->dobj.dumpId); + + if (plang->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "LANGUAGE", qlanname, + NULL, plang->lanowner, + plang->dobj.catId, 0, plang->dobj.dumpId); + + if (plang->lanpltrusted && plang->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, plang->dobj.dumpId, InvalidDumpId, "LANGUAGE", + qlanname, NULL, NULL, + NULL, plang->lanowner, &plang->dacl); + + free(qlanname); + + destroyPQExpBuffer(defqry); + destroyPQExpBuffer(delqry); +} + +/* + * format_function_arguments: generate function name and argument list + * + * This is used when we can rely on pg_get_function_arguments to format + * the argument list. Note, however, that pg_get_function_arguments + * does not special-case zero-argument aggregates. + */ +static char * +format_function_arguments(const FuncInfo *finfo, const char *funcargs, bool is_agg) +{ + PQExpBufferData fn; + + initPQExpBuffer(&fn); + appendPQExpBufferStr(&fn, fmtId(finfo->dobj.name)); + if (is_agg && finfo->nargs == 0) + appendPQExpBufferStr(&fn, "(*)"); + else + appendPQExpBuffer(&fn, "(%s)", funcargs); + return fn.data; +} + +/* + * format_function_signature: generate function name and argument list + * + * Only a minimal list of input argument types is generated; this is + * sufficient to reference the function, but not to define it. + * + * If honor_quotes is false then the function name is never quoted. + * This is appropriate for use in TOC tags, but not in SQL commands. + */ +static char * +format_function_signature(Archive *fout, const FuncInfo *finfo, bool honor_quotes) +{ + PQExpBufferData fn; + int j; + + initPQExpBuffer(&fn); + if (honor_quotes) + appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name)); + else + appendPQExpBuffer(&fn, "%s(", finfo->dobj.name); + for (j = 0; j < finfo->nargs; j++) + { + if (j > 0) + appendPQExpBufferStr(&fn, ", "); + + appendPQExpBufferStr(&fn, + getFormattedTypeName(fout, finfo->argtypes[j], + zeroIsError)); + } + appendPQExpBufferChar(&fn, ')'); + return fn.data; +} + + +/* + * dumpFunc: + * dump out one function + */ +static void +dumpFunc(Archive *fout, const FuncInfo *finfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PQExpBuffer q; + PQExpBuffer delqry; + PQExpBuffer asPart; + PGresult *res; + char *funcsig; /* identity signature */ + char *funcfullsig = NULL; /* full signature */ + char *funcsig_tag; + char *qual_funcsig; + char *proretset; + char *prosrc; + char *probin; + char *prosqlbody; + char *funcargs; + char *funciargs; + char *funcresult; + char *protrftypes; + char *prokind; + char *provolatile; + char *proisstrict; + char *prosecdef; + char *proleakproof; + char *proconfig; + char *procost; + char *prorows; + char *prosupport; + char *proparallel; + char *lanname; + char **configitems = NULL; + int nconfigitems = 0; + const char *keyword; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + query = createPQExpBuffer(); + q = createPQExpBuffer(); + delqry = createPQExpBuffer(); + asPart = createPQExpBuffer(); + + if (!fout->is_prepared[PREPQUERY_DUMPFUNC]) + { + /* Set up query for function-specific details */ + appendPQExpBufferStr(query, + "PREPARE dumpFunc(pg_catalog.oid) AS\n"); + + appendPQExpBufferStr(query, + "SELECT\n" + "proretset,\n" + "prosrc,\n" + "probin,\n" + "provolatile,\n" + "proisstrict,\n" + "prosecdef,\n" + "lanname,\n" + "proconfig,\n" + "procost,\n" + "prorows,\n" + "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n" + "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n" + "pg_catalog.pg_get_function_result(p.oid) AS funcresult,\n" + "proleakproof,\n"); + + if (fout->remoteVersion >= 90500) + appendPQExpBufferStr(query, + "array_to_string(protrftypes, ' ') AS protrftypes,\n"); + else + appendPQExpBufferStr(query, + "NULL AS protrftypes,\n"); + + if (fout->remoteVersion >= 90600) + appendPQExpBufferStr(query, + "proparallel,\n"); + else + appendPQExpBufferStr(query, + "'u' AS proparallel,\n"); + + if (fout->remoteVersion >= 110000) + appendPQExpBufferStr(query, + "prokind,\n"); + else + appendPQExpBufferStr(query, + "CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind,\n"); + + if (fout->remoteVersion >= 120000) + appendPQExpBufferStr(query, + "prosupport,\n"); + else + appendPQExpBufferStr(query, + "'-' AS prosupport,\n"); + + if (fout->remoteVersion >= 140000) + appendPQExpBufferStr(query, + "pg_get_function_sqlbody(p.oid) AS prosqlbody\n"); + else + appendPQExpBufferStr(query, + "NULL AS prosqlbody\n"); + + appendPQExpBufferStr(query, + "FROM pg_catalog.pg_proc p, pg_catalog.pg_language l\n" + "WHERE p.oid = $1 " + "AND l.oid = p.prolang"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_DUMPFUNC] = true; + } + + printfPQExpBuffer(query, + "EXECUTE dumpFunc('%u')", + finfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset")); + if (PQgetisnull(res, 0, PQfnumber(res, "prosqlbody"))) + { + prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc")); + probin = PQgetvalue(res, 0, PQfnumber(res, "probin")); + prosqlbody = NULL; + } + else + { + prosrc = NULL; + probin = NULL; + prosqlbody = PQgetvalue(res, 0, PQfnumber(res, "prosqlbody")); + } + funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs")); + funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs")); + funcresult = PQgetvalue(res, 0, PQfnumber(res, "funcresult")); + protrftypes = PQgetvalue(res, 0, PQfnumber(res, "protrftypes")); + prokind = PQgetvalue(res, 0, PQfnumber(res, "prokind")); + provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile")); + proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict")); + prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef")); + proleakproof = PQgetvalue(res, 0, PQfnumber(res, "proleakproof")); + proconfig = PQgetvalue(res, 0, PQfnumber(res, "proconfig")); + procost = PQgetvalue(res, 0, PQfnumber(res, "procost")); + prorows = PQgetvalue(res, 0, PQfnumber(res, "prorows")); + prosupport = PQgetvalue(res, 0, PQfnumber(res, "prosupport")); + proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel")); + lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname")); + + /* + * See backend/commands/functioncmds.c for details of how the 'AS' clause + * is used. + */ + if (prosqlbody) + { + appendPQExpBufferStr(asPart, prosqlbody); + } + else if (probin[0] != '\0') + { + appendPQExpBufferStr(asPart, "AS "); + appendStringLiteralAH(asPart, probin, fout); + if (prosrc[0] != '\0') + { + appendPQExpBufferStr(asPart, ", "); + + /* + * where we have bin, use dollar quoting if allowed and src + * contains quote or backslash; else use regular quoting. + */ + if (dopt->disable_dollar_quoting || + (strchr(prosrc, '\'') == NULL && strchr(prosrc, '\\') == NULL)) + appendStringLiteralAH(asPart, prosrc, fout); + else + appendStringLiteralDQ(asPart, prosrc, NULL); + } + } + else + { + appendPQExpBufferStr(asPart, "AS "); + /* with no bin, dollar quote src unconditionally if allowed */ + if (dopt->disable_dollar_quoting) + appendStringLiteralAH(asPart, prosrc, fout); + else + appendStringLiteralDQ(asPart, prosrc, NULL); + } + + if (*proconfig) + { + if (!parsePGArray(proconfig, &configitems, &nconfigitems)) + pg_fatal("could not parse %s array", "proconfig"); + } + else + { + configitems = NULL; + nconfigitems = 0; + } + + funcfullsig = format_function_arguments(finfo, funcargs, false); + funcsig = format_function_arguments(finfo, funciargs, false); + + funcsig_tag = format_function_signature(fout, finfo, false); + + qual_funcsig = psprintf("%s.%s", + fmtId(finfo->dobj.namespace->dobj.name), + funcsig); + + if (prokind[0] == PROKIND_PROCEDURE) + keyword = "PROCEDURE"; + else + keyword = "FUNCTION"; /* works for window functions too */ + + appendPQExpBuffer(delqry, "DROP %s %s;\n", + keyword, qual_funcsig); + + appendPQExpBuffer(q, "CREATE %s %s.%s", + keyword, + fmtId(finfo->dobj.namespace->dobj.name), + funcfullsig ? funcfullsig : + funcsig); + + if (prokind[0] == PROKIND_PROCEDURE) + /* no result type to output */ ; + else if (funcresult) + appendPQExpBuffer(q, " RETURNS %s", funcresult); + else + appendPQExpBuffer(q, " RETURNS %s%s", + (proretset[0] == 't') ? "SETOF " : "", + getFormattedTypeName(fout, finfo->prorettype, + zeroIsError)); + + appendPQExpBuffer(q, "\n LANGUAGE %s", fmtId(lanname)); + + if (*protrftypes) + { + Oid *typeids = pg_malloc_array(Oid, FUNC_MAX_ARGS); + int i; + + appendPQExpBufferStr(q, " TRANSFORM "); + parseOidArray(protrftypes, typeids, FUNC_MAX_ARGS); + for (i = 0; typeids[i]; i++) + { + if (i != 0) + appendPQExpBufferStr(q, ", "); + appendPQExpBuffer(q, "FOR TYPE %s", + getFormattedTypeName(fout, typeids[i], zeroAsNone)); + } + + free(typeids); + } + + if (prokind[0] == PROKIND_WINDOW) + appendPQExpBufferStr(q, " WINDOW"); + + if (provolatile[0] != PROVOLATILE_VOLATILE) + { + if (provolatile[0] == PROVOLATILE_IMMUTABLE) + appendPQExpBufferStr(q, " IMMUTABLE"); + else if (provolatile[0] == PROVOLATILE_STABLE) + appendPQExpBufferStr(q, " STABLE"); + else if (provolatile[0] != PROVOLATILE_VOLATILE) + pg_fatal("unrecognized provolatile value for function \"%s\"", + finfo->dobj.name); + } + + if (proisstrict[0] == 't') + appendPQExpBufferStr(q, " STRICT"); + + if (prosecdef[0] == 't') + appendPQExpBufferStr(q, " SECURITY DEFINER"); + + if (proleakproof[0] == 't') + appendPQExpBufferStr(q, " LEAKPROOF"); + + /* + * COST and ROWS are emitted only if present and not default, so as not to + * break backwards-compatibility of the dump without need. Keep this code + * in sync with the defaults in functioncmds.c. + */ + if (strcmp(procost, "0") != 0) + { + if (strcmp(lanname, "internal") == 0 || strcmp(lanname, "c") == 0) + { + /* default cost is 1 */ + if (strcmp(procost, "1") != 0) + appendPQExpBuffer(q, " COST %s", procost); + } + else + { + /* default cost is 100 */ + if (strcmp(procost, "100") != 0) + appendPQExpBuffer(q, " COST %s", procost); + } + } + if (proretset[0] == 't' && + strcmp(prorows, "0") != 0 && strcmp(prorows, "1000") != 0) + appendPQExpBuffer(q, " ROWS %s", prorows); + + if (strcmp(prosupport, "-") != 0) + { + /* We rely on regprocout to provide quoting and qualification */ + appendPQExpBuffer(q, " SUPPORT %s", prosupport); + } + + if (proparallel[0] != PROPARALLEL_UNSAFE) + { + if (proparallel[0] == PROPARALLEL_SAFE) + appendPQExpBufferStr(q, " PARALLEL SAFE"); + else if (proparallel[0] == PROPARALLEL_RESTRICTED) + appendPQExpBufferStr(q, " PARALLEL RESTRICTED"); + else if (proparallel[0] != PROPARALLEL_UNSAFE) + pg_fatal("unrecognized proparallel value for function \"%s\"", + finfo->dobj.name); + } + + for (int i = 0; i < nconfigitems; i++) + { + /* we feel free to scribble on configitems[] here */ + char *configitem = configitems[i]; + char *pos; + + pos = strchr(configitem, '='); + if (pos == NULL) + continue; + *pos++ = '\0'; + appendPQExpBuffer(q, "\n SET %s TO ", fmtId(configitem)); + + /* + * Variables that are marked GUC_LIST_QUOTE were already fully quoted + * by flatten_set_variable_args() before they were put into the + * proconfig array. However, because the quoting rules used there + * aren't exactly like SQL's, we have to break the list value apart + * and then quote the elements as string literals. (The elements may + * be double-quoted as-is, but we can't just feed them to the SQL + * parser; it would do the wrong thing with elements that are + * zero-length or longer than NAMEDATALEN.) Also, we need a special + * case for empty lists. + * + * Variables that are not so marked should just be emitted as simple + * string literals. If the variable is not known to + * variable_is_guc_list_quote(), we'll do that; this makes it unsafe + * to use GUC_LIST_QUOTE for extension variables. + */ + if (variable_is_guc_list_quote(configitem)) + { + char **namelist; + char **nameptr; + + /* Parse string into list of identifiers */ + /* this shouldn't fail really */ + if (SplitGUCList(pos, ',', &namelist)) + { + /* Special case: represent an empty list as NULL */ + if (*namelist == NULL) + appendPQExpBufferStr(q, "NULL"); + for (nameptr = namelist; *nameptr; nameptr++) + { + if (nameptr != namelist) + appendPQExpBufferStr(q, ", "); + appendStringLiteralAH(q, *nameptr, fout); + } + } + pg_free(namelist); + } + else + appendStringLiteralAH(q, pos, fout); + } + + appendPQExpBuffer(q, "\n %s;\n", asPart->data); + + append_depends_on_extension(fout, q, &finfo->dobj, + "pg_catalog.pg_proc", keyword, + qual_funcsig); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &finfo->dobj, + keyword, funcsig, + finfo->dobj.namespace->dobj.name); + + if (finfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = funcsig_tag, + .namespace = finfo->dobj.namespace->dobj.name, + .owner = finfo->rolname, + .description = keyword, + .section = finfo->postponed_def ? + SECTION_POST_DATA : SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delqry->data)); + + /* Dump Function Comments and Security Labels */ + if (finfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, keyword, funcsig, + finfo->dobj.namespace->dobj.name, finfo->rolname, + finfo->dobj.catId, 0, finfo->dobj.dumpId); + + if (finfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, keyword, funcsig, + finfo->dobj.namespace->dobj.name, finfo->rolname, + finfo->dobj.catId, 0, finfo->dobj.dumpId); + + if (finfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, finfo->dobj.dumpId, InvalidDumpId, keyword, + funcsig, NULL, + finfo->dobj.namespace->dobj.name, + NULL, finfo->rolname, &finfo->dacl); + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delqry); + destroyPQExpBuffer(asPart); + free(funcsig); + free(funcfullsig); + free(funcsig_tag); + free(qual_funcsig); + free(configitems); +} + + +/* + * Dump a user-defined cast + */ +static void +dumpCast(Archive *fout, const CastInfo *cast) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer defqry; + PQExpBuffer delqry; + PQExpBuffer labelq; + PQExpBuffer castargs; + FuncInfo *funcInfo = NULL; + const char *sourceType; + const char *targetType; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + /* Cannot dump if we don't have the cast function's info */ + if (OidIsValid(cast->castfunc)) + { + funcInfo = findFuncByOid(cast->castfunc); + if (funcInfo == NULL) + pg_fatal("could not find function definition for function with OID %u", + cast->castfunc); + } + + defqry = createPQExpBuffer(); + delqry = createPQExpBuffer(); + labelq = createPQExpBuffer(); + castargs = createPQExpBuffer(); + + sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone); + targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone); + appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n", + sourceType, targetType); + + appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ", + sourceType, targetType); + + switch (cast->castmethod) + { + case COERCION_METHOD_BINARY: + appendPQExpBufferStr(defqry, "WITHOUT FUNCTION"); + break; + case COERCION_METHOD_INOUT: + appendPQExpBufferStr(defqry, "WITH INOUT"); + break; + case COERCION_METHOD_FUNCTION: + if (funcInfo) + { + char *fsig = format_function_signature(fout, funcInfo, true); + + /* + * Always qualify the function name (format_function_signature + * won't qualify it). + */ + appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s", + fmtId(funcInfo->dobj.namespace->dobj.name), fsig); + free(fsig); + } + else + pg_log_warning("bogus value in pg_cast.castfunc or pg_cast.castmethod field"); + break; + default: + pg_log_warning("bogus value in pg_cast.castmethod field"); + } + + if (cast->castcontext == 'a') + appendPQExpBufferStr(defqry, " AS ASSIGNMENT"); + else if (cast->castcontext == 'i') + appendPQExpBufferStr(defqry, " AS IMPLICIT"); + appendPQExpBufferStr(defqry, ";\n"); + + appendPQExpBuffer(labelq, "CAST (%s AS %s)", + sourceType, targetType); + + appendPQExpBuffer(castargs, "(%s AS %s)", + sourceType, targetType); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(defqry, &cast->dobj, + "CAST", castargs->data, NULL); + + if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId, + ARCHIVE_OPTS(.tag = labelq->data, + .description = "CAST", + .section = SECTION_PRE_DATA, + .createStmt = defqry->data, + .dropStmt = delqry->data)); + + /* Dump Cast Comments */ + if (cast->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "CAST", castargs->data, + NULL, "", + cast->dobj.catId, 0, cast->dobj.dumpId); + + destroyPQExpBuffer(defqry); + destroyPQExpBuffer(delqry); + destroyPQExpBuffer(labelq); + destroyPQExpBuffer(castargs); +} + +/* + * Dump a transform + */ +static void +dumpTransform(Archive *fout, const TransformInfo *transform) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer defqry; + PQExpBuffer delqry; + PQExpBuffer labelq; + PQExpBuffer transformargs; + FuncInfo *fromsqlFuncInfo = NULL; + FuncInfo *tosqlFuncInfo = NULL; + char *lanname; + const char *transformType; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + /* Cannot dump if we don't have the transform functions' info */ + if (OidIsValid(transform->trffromsql)) + { + fromsqlFuncInfo = findFuncByOid(transform->trffromsql); + if (fromsqlFuncInfo == NULL) + pg_fatal("could not find function definition for function with OID %u", + transform->trffromsql); + } + if (OidIsValid(transform->trftosql)) + { + tosqlFuncInfo = findFuncByOid(transform->trftosql); + if (tosqlFuncInfo == NULL) + pg_fatal("could not find function definition for function with OID %u", + transform->trftosql); + } + + defqry = createPQExpBuffer(); + delqry = createPQExpBuffer(); + labelq = createPQExpBuffer(); + transformargs = createPQExpBuffer(); + + lanname = get_language_name(fout, transform->trflang); + transformType = getFormattedTypeName(fout, transform->trftype, zeroAsNone); + + appendPQExpBuffer(delqry, "DROP TRANSFORM FOR %s LANGUAGE %s;\n", + transformType, lanname); + + appendPQExpBuffer(defqry, "CREATE TRANSFORM FOR %s LANGUAGE %s (", + transformType, lanname); + + if (!transform->trffromsql && !transform->trftosql) + pg_log_warning("bogus transform definition, at least one of trffromsql and trftosql should be nonzero"); + + if (transform->trffromsql) + { + if (fromsqlFuncInfo) + { + char *fsig = format_function_signature(fout, fromsqlFuncInfo, true); + + /* + * Always qualify the function name (format_function_signature + * won't qualify it). + */ + appendPQExpBuffer(defqry, "FROM SQL WITH FUNCTION %s.%s", + fmtId(fromsqlFuncInfo->dobj.namespace->dobj.name), fsig); + free(fsig); + } + else + pg_log_warning("bogus value in pg_transform.trffromsql field"); + } + + if (transform->trftosql) + { + if (transform->trffromsql) + appendPQExpBufferStr(defqry, ", "); + + if (tosqlFuncInfo) + { + char *fsig = format_function_signature(fout, tosqlFuncInfo, true); + + /* + * Always qualify the function name (format_function_signature + * won't qualify it). + */ + appendPQExpBuffer(defqry, "TO SQL WITH FUNCTION %s.%s", + fmtId(tosqlFuncInfo->dobj.namespace->dobj.name), fsig); + free(fsig); + } + else + pg_log_warning("bogus value in pg_transform.trftosql field"); + } + + appendPQExpBufferStr(defqry, ");\n"); + + appendPQExpBuffer(labelq, "TRANSFORM FOR %s LANGUAGE %s", + transformType, lanname); + + appendPQExpBuffer(transformargs, "FOR %s LANGUAGE %s", + transformType, lanname); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(defqry, &transform->dobj, + "TRANSFORM", transformargs->data, NULL); + + if (transform->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, transform->dobj.catId, transform->dobj.dumpId, + ARCHIVE_OPTS(.tag = labelq->data, + .description = "TRANSFORM", + .section = SECTION_PRE_DATA, + .createStmt = defqry->data, + .dropStmt = delqry->data, + .deps = transform->dobj.dependencies, + .nDeps = transform->dobj.nDeps)); + + /* Dump Transform Comments */ + if (transform->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TRANSFORM", transformargs->data, + NULL, "", + transform->dobj.catId, 0, transform->dobj.dumpId); + + free(lanname); + destroyPQExpBuffer(defqry); + destroyPQExpBuffer(delqry); + destroyPQExpBuffer(labelq); + destroyPQExpBuffer(transformargs); +} + + +/* + * dumpOpr + * write out a single operator definition + */ +static void +dumpOpr(Archive *fout, const OprInfo *oprinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PQExpBuffer q; + PQExpBuffer delq; + PQExpBuffer oprid; + PQExpBuffer details; + PGresult *res; + int i_oprkind; + int i_oprcode; + int i_oprleft; + int i_oprright; + int i_oprcom; + int i_oprnegate; + int i_oprrest; + int i_oprjoin; + int i_oprcanmerge; + int i_oprcanhash; + char *oprkind; + char *oprcode; + char *oprleft; + char *oprright; + char *oprcom; + char *oprnegate; + char *oprrest; + char *oprjoin; + char *oprcanmerge; + char *oprcanhash; + char *oprregproc; + char *oprref; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + /* + * some operators are invalid because they were the result of user + * defining operators before commutators exist + */ + if (!OidIsValid(oprinfo->oprcode)) + return; + + query = createPQExpBuffer(); + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + oprid = createPQExpBuffer(); + details = createPQExpBuffer(); + + if (!fout->is_prepared[PREPQUERY_DUMPOPR]) + { + /* Set up query for operator-specific details */ + appendPQExpBufferStr(query, + "PREPARE dumpOpr(pg_catalog.oid) AS\n" + "SELECT oprkind, " + "oprcode::pg_catalog.regprocedure, " + "oprleft::pg_catalog.regtype, " + "oprright::pg_catalog.regtype, " + "oprcom, " + "oprnegate, " + "oprrest::pg_catalog.regprocedure, " + "oprjoin::pg_catalog.regprocedure, " + "oprcanmerge, oprcanhash " + "FROM pg_catalog.pg_operator " + "WHERE oid = $1"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_DUMPOPR] = true; + } + + printfPQExpBuffer(query, + "EXECUTE dumpOpr('%u')", + oprinfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + i_oprkind = PQfnumber(res, "oprkind"); + i_oprcode = PQfnumber(res, "oprcode"); + i_oprleft = PQfnumber(res, "oprleft"); + i_oprright = PQfnumber(res, "oprright"); + i_oprcom = PQfnumber(res, "oprcom"); + i_oprnegate = PQfnumber(res, "oprnegate"); + i_oprrest = PQfnumber(res, "oprrest"); + i_oprjoin = PQfnumber(res, "oprjoin"); + i_oprcanmerge = PQfnumber(res, "oprcanmerge"); + i_oprcanhash = PQfnumber(res, "oprcanhash"); + + oprkind = PQgetvalue(res, 0, i_oprkind); + oprcode = PQgetvalue(res, 0, i_oprcode); + oprleft = PQgetvalue(res, 0, i_oprleft); + oprright = PQgetvalue(res, 0, i_oprright); + oprcom = PQgetvalue(res, 0, i_oprcom); + oprnegate = PQgetvalue(res, 0, i_oprnegate); + oprrest = PQgetvalue(res, 0, i_oprrest); + oprjoin = PQgetvalue(res, 0, i_oprjoin); + oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge); + oprcanhash = PQgetvalue(res, 0, i_oprcanhash); + + /* In PG14 upwards postfix operator support does not exist anymore. */ + if (strcmp(oprkind, "r") == 0) + pg_log_warning("postfix operators are not supported anymore (operator \"%s\")", + oprcode); + + oprregproc = convertRegProcReference(oprcode); + if (oprregproc) + { + appendPQExpBuffer(details, " FUNCTION = %s", oprregproc); + free(oprregproc); + } + + appendPQExpBuffer(oprid, "%s (", + oprinfo->dobj.name); + + /* + * right unary means there's a left arg and left unary means there's a + * right arg. (Although the "r" case is dead code for PG14 and later, + * continue to support it in case we're dumping from an old server.) + */ + if (strcmp(oprkind, "r") == 0 || + strcmp(oprkind, "b") == 0) + { + appendPQExpBuffer(details, ",\n LEFTARG = %s", oprleft); + appendPQExpBufferStr(oprid, oprleft); + } + else + appendPQExpBufferStr(oprid, "NONE"); + + if (strcmp(oprkind, "l") == 0 || + strcmp(oprkind, "b") == 0) + { + appendPQExpBuffer(details, ",\n RIGHTARG = %s", oprright); + appendPQExpBuffer(oprid, ", %s)", oprright); + } + else + appendPQExpBufferStr(oprid, ", NONE)"); + + oprref = getFormattedOperatorName(oprcom); + if (oprref) + { + appendPQExpBuffer(details, ",\n COMMUTATOR = %s", oprref); + free(oprref); + } + + oprref = getFormattedOperatorName(oprnegate); + if (oprref) + { + appendPQExpBuffer(details, ",\n NEGATOR = %s", oprref); + free(oprref); + } + + if (strcmp(oprcanmerge, "t") == 0) + appendPQExpBufferStr(details, ",\n MERGES"); + + if (strcmp(oprcanhash, "t") == 0) + appendPQExpBufferStr(details, ",\n HASHES"); + + oprregproc = convertRegProcReference(oprrest); + if (oprregproc) + { + appendPQExpBuffer(details, ",\n RESTRICT = %s", oprregproc); + free(oprregproc); + } + + oprregproc = convertRegProcReference(oprjoin); + if (oprregproc) + { + appendPQExpBuffer(details, ",\n JOIN = %s", oprregproc); + free(oprregproc); + } + + appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n", + fmtId(oprinfo->dobj.namespace->dobj.name), + oprid->data); + + appendPQExpBuffer(q, "CREATE OPERATOR %s.%s (\n%s\n);\n", + fmtId(oprinfo->dobj.namespace->dobj.name), + oprinfo->dobj.name, details->data); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &oprinfo->dobj, + "OPERATOR", oprid->data, + oprinfo->dobj.namespace->dobj.name); + + if (oprinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = oprinfo->dobj.name, + .namespace = oprinfo->dobj.namespace->dobj.name, + .owner = oprinfo->rolname, + .description = "OPERATOR", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Operator Comments */ + if (oprinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "OPERATOR", oprid->data, + oprinfo->dobj.namespace->dobj.name, oprinfo->rolname, + oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId); + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(oprid); + destroyPQExpBuffer(details); +} + +/* + * Convert a function reference obtained from pg_operator + * + * Returns allocated string of what to print, or NULL if function references + * is InvalidOid. Returned string is expected to be free'd by the caller. + * + * The input is a REGPROCEDURE display; we have to strip the argument-types + * part. + */ +static char * +convertRegProcReference(const char *proc) +{ + char *name; + char *paren; + bool inquote; + + /* In all cases "-" means a null reference */ + if (strcmp(proc, "-") == 0) + return NULL; + + name = pg_strdup(proc); + /* find non-double-quoted left paren */ + inquote = false; + for (paren = name; *paren; paren++) + { + if (*paren == '(' && !inquote) + { + *paren = '\0'; + break; + } + if (*paren == '"') + inquote = !inquote; + } + return name; +} + +/* + * getFormattedOperatorName - retrieve the operator name for the + * given operator OID (presented in string form). + * + * Returns an allocated string, or NULL if the given OID is invalid. + * Caller is responsible for free'ing result string. + * + * What we produce has the format "OPERATOR(schema.oprname)". This is only + * useful in commands where the operator's argument types can be inferred from + * context. We always schema-qualify the name, though. The predecessor to + * this code tried to skip the schema qualification if possible, but that led + * to wrong results in corner cases, such as if an operator and its negator + * are in different schemas. + */ +static char * +getFormattedOperatorName(const char *oproid) +{ + OprInfo *oprInfo; + + /* In all cases "0" means a null reference */ + if (strcmp(oproid, "0") == 0) + return NULL; + + oprInfo = findOprByOid(atooid(oproid)); + if (oprInfo == NULL) + { + pg_log_warning("could not find operator with OID %s", + oproid); + return NULL; + } + + return psprintf("OPERATOR(%s.%s)", + fmtId(oprInfo->dobj.namespace->dobj.name), + oprInfo->dobj.name); +} + +/* + * Convert a function OID obtained from pg_ts_parser or pg_ts_template + * + * It is sufficient to use REGPROC rather than REGPROCEDURE, since the + * argument lists of these functions are predetermined. Note that the + * caller should ensure we are in the proper schema, because the results + * are search path dependent! + */ +static char * +convertTSFunction(Archive *fout, Oid funcOid) +{ + char *result; + char query[128]; + PGresult *res; + + snprintf(query, sizeof(query), + "SELECT '%u'::pg_catalog.regproc", funcOid); + res = ExecuteSqlQueryForSingleRow(fout, query); + + result = pg_strdup(PQgetvalue(res, 0, 0)); + + PQclear(res); + + return result; +} + +/* + * dumpAccessMethod + * write out a single access method definition + */ +static void +dumpAccessMethod(Archive *fout, const AccessMethodInfo *aminfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + char *qamname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qamname = pg_strdup(fmtId(aminfo->dobj.name)); + + appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname); + + switch (aminfo->amtype) + { + case AMTYPE_INDEX: + appendPQExpBufferStr(q, "TYPE INDEX "); + break; + case AMTYPE_TABLE: + appendPQExpBufferStr(q, "TYPE TABLE "); + break; + default: + pg_log_warning("invalid type \"%c\" of access method \"%s\"", + aminfo->amtype, qamname); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + free(qamname); + return; + } + + appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler); + + appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n", + qamname); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &aminfo->dobj, + "ACCESS METHOD", qamname, NULL); + + if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = aminfo->dobj.name, + .description = "ACCESS METHOD", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Access Method Comments */ + if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "ACCESS METHOD", qamname, + NULL, "", + aminfo->dobj.catId, 0, aminfo->dobj.dumpId); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + free(qamname); +} + +/* + * dumpOpclass + * write out a single operator class definition + */ +static void +dumpOpclass(Archive *fout, const OpclassInfo *opcinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PQExpBuffer q; + PQExpBuffer delq; + PQExpBuffer nameusing; + PGresult *res; + int ntups; + int i_opcintype; + int i_opckeytype; + int i_opcdefault; + int i_opcfamily; + int i_opcfamilyname; + int i_opcfamilynsp; + int i_amname; + int i_amopstrategy; + int i_amopopr; + int i_sortfamily; + int i_sortfamilynsp; + int i_amprocnum; + int i_amproc; + int i_amproclefttype; + int i_amprocrighttype; + char *opcintype; + char *opckeytype; + char *opcdefault; + char *opcfamily; + char *opcfamilyname; + char *opcfamilynsp; + char *amname; + char *amopstrategy; + char *amopopr; + char *sortfamily; + char *sortfamilynsp; + char *amprocnum; + char *amproc; + char *amproclefttype; + char *amprocrighttype; + bool needComma; + int i; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + query = createPQExpBuffer(); + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + nameusing = createPQExpBuffer(); + + /* Get additional fields from the pg_opclass row */ + appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, " + "opckeytype::pg_catalog.regtype, " + "opcdefault, opcfamily, " + "opfname AS opcfamilyname, " + "nspname AS opcfamilynsp, " + "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname " + "FROM pg_catalog.pg_opclass c " + "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily " + "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace " + "WHERE c.oid = '%u'::pg_catalog.oid", + opcinfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + i_opcintype = PQfnumber(res, "opcintype"); + i_opckeytype = PQfnumber(res, "opckeytype"); + i_opcdefault = PQfnumber(res, "opcdefault"); + i_opcfamily = PQfnumber(res, "opcfamily"); + i_opcfamilyname = PQfnumber(res, "opcfamilyname"); + i_opcfamilynsp = PQfnumber(res, "opcfamilynsp"); + i_amname = PQfnumber(res, "amname"); + + /* opcintype may still be needed after we PQclear res */ + opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype)); + opckeytype = PQgetvalue(res, 0, i_opckeytype); + opcdefault = PQgetvalue(res, 0, i_opcdefault); + /* opcfamily will still be needed after we PQclear res */ + opcfamily = pg_strdup(PQgetvalue(res, 0, i_opcfamily)); + opcfamilyname = PQgetvalue(res, 0, i_opcfamilyname); + opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp); + /* amname will still be needed after we PQclear res */ + amname = pg_strdup(PQgetvalue(res, 0, i_amname)); + + appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s", + fmtQualifiedDumpable(opcinfo)); + appendPQExpBuffer(delq, " USING %s;\n", + fmtId(amname)); + + /* Build the fixed portion of the CREATE command */ + appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n ", + fmtQualifiedDumpable(opcinfo)); + if (strcmp(opcdefault, "t") == 0) + appendPQExpBufferStr(q, "DEFAULT "); + appendPQExpBuffer(q, "FOR TYPE %s USING %s", + opcintype, + fmtId(amname)); + if (strlen(opcfamilyname) > 0) + { + appendPQExpBufferStr(q, " FAMILY "); + appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp)); + appendPQExpBufferStr(q, fmtId(opcfamilyname)); + } + appendPQExpBufferStr(q, " AS\n "); + + needComma = false; + + if (strcmp(opckeytype, "-") != 0) + { + appendPQExpBuffer(q, "STORAGE %s", + opckeytype); + needComma = true; + } + + PQclear(res); + + /* + * Now fetch and print the OPERATOR entries (pg_amop rows). + * + * Print only those opfamily members that are tied to the opclass by + * pg_depend entries. + */ + resetPQExpBuffer(query); + appendPQExpBuffer(query, "SELECT amopstrategy, " + "amopopr::pg_catalog.regoperator, " + "opfname AS sortfamily, " + "nspname AS sortfamilynsp " + "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON " + "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) " + "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily " + "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace " + "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass " + "AND refobjid = '%u'::pg_catalog.oid " + "AND amopfamily = '%s'::pg_catalog.oid " + "ORDER BY amopstrategy", + opcinfo->dobj.catId.oid, + opcfamily); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_amopstrategy = PQfnumber(res, "amopstrategy"); + i_amopopr = PQfnumber(res, "amopopr"); + i_sortfamily = PQfnumber(res, "sortfamily"); + i_sortfamilynsp = PQfnumber(res, "sortfamilynsp"); + + for (i = 0; i < ntups; i++) + { + amopstrategy = PQgetvalue(res, i, i_amopstrategy); + amopopr = PQgetvalue(res, i, i_amopopr); + sortfamily = PQgetvalue(res, i, i_sortfamily); + sortfamilynsp = PQgetvalue(res, i, i_sortfamilynsp); + + if (needComma) + appendPQExpBufferStr(q, " ,\n "); + + appendPQExpBuffer(q, "OPERATOR %s %s", + amopstrategy, amopopr); + + if (strlen(sortfamily) > 0) + { + appendPQExpBufferStr(q, " FOR ORDER BY "); + appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp)); + appendPQExpBufferStr(q, fmtId(sortfamily)); + } + + needComma = true; + } + + PQclear(res); + + /* + * Now fetch and print the FUNCTION entries (pg_amproc rows). + * + * Print only those opfamily members that are tied to the opclass by + * pg_depend entries. + * + * We print the amproclefttype/amprocrighttype even though in most cases + * the backend could deduce the right values, because of the corner case + * of a btree sort support function for a cross-type comparison. + */ + resetPQExpBuffer(query); + + appendPQExpBuffer(query, "SELECT amprocnum, " + "amproc::pg_catalog.regprocedure, " + "amproclefttype::pg_catalog.regtype, " + "amprocrighttype::pg_catalog.regtype " + "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend " + "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass " + "AND refobjid = '%u'::pg_catalog.oid " + "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass " + "AND objid = ap.oid " + "ORDER BY amprocnum", + opcinfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_amprocnum = PQfnumber(res, "amprocnum"); + i_amproc = PQfnumber(res, "amproc"); + i_amproclefttype = PQfnumber(res, "amproclefttype"); + i_amprocrighttype = PQfnumber(res, "amprocrighttype"); + + for (i = 0; i < ntups; i++) + { + amprocnum = PQgetvalue(res, i, i_amprocnum); + amproc = PQgetvalue(res, i, i_amproc); + amproclefttype = PQgetvalue(res, i, i_amproclefttype); + amprocrighttype = PQgetvalue(res, i, i_amprocrighttype); + + if (needComma) + appendPQExpBufferStr(q, " ,\n "); + + appendPQExpBuffer(q, "FUNCTION %s", amprocnum); + + if (*amproclefttype && *amprocrighttype) + appendPQExpBuffer(q, " (%s, %s)", amproclefttype, amprocrighttype); + + appendPQExpBuffer(q, " %s", amproc); + + needComma = true; + } + + PQclear(res); + + /* + * If needComma is still false it means we haven't added anything after + * the AS keyword. To avoid printing broken SQL, append a dummy STORAGE + * clause with the same datatype. This isn't sanctioned by the + * documentation, but actually DefineOpClass will treat it as a no-op. + */ + if (!needComma) + appendPQExpBuffer(q, "STORAGE %s", opcintype); + + appendPQExpBufferStr(q, ";\n"); + + appendPQExpBufferStr(nameusing, fmtId(opcinfo->dobj.name)); + appendPQExpBuffer(nameusing, " USING %s", + fmtId(amname)); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &opcinfo->dobj, + "OPERATOR CLASS", nameusing->data, + opcinfo->dobj.namespace->dobj.name); + + if (opcinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = opcinfo->dobj.name, + .namespace = opcinfo->dobj.namespace->dobj.name, + .owner = opcinfo->rolname, + .description = "OPERATOR CLASS", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Operator Class Comments */ + if (opcinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "OPERATOR CLASS", nameusing->data, + opcinfo->dobj.namespace->dobj.name, opcinfo->rolname, + opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId); + + free(opcintype); + free(opcfamily); + free(amname); + destroyPQExpBuffer(query); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(nameusing); +} + +/* + * dumpOpfamily + * write out a single operator family definition + * + * Note: this also dumps any "loose" operator members that aren't bound to a + * specific opclass within the opfamily. + */ +static void +dumpOpfamily(Archive *fout, const OpfamilyInfo *opfinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PQExpBuffer q; + PQExpBuffer delq; + PQExpBuffer nameusing; + PGresult *res; + PGresult *res_ops; + PGresult *res_procs; + int ntups; + int i_amname; + int i_amopstrategy; + int i_amopopr; + int i_sortfamily; + int i_sortfamilynsp; + int i_amprocnum; + int i_amproc; + int i_amproclefttype; + int i_amprocrighttype; + char *amname; + char *amopstrategy; + char *amopopr; + char *sortfamily; + char *sortfamilynsp; + char *amprocnum; + char *amproc; + char *amproclefttype; + char *amprocrighttype; + bool needComma; + int i; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + query = createPQExpBuffer(); + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + nameusing = createPQExpBuffer(); + + /* + * Fetch only those opfamily members that are tied directly to the + * opfamily by pg_depend entries. + */ + appendPQExpBuffer(query, "SELECT amopstrategy, " + "amopopr::pg_catalog.regoperator, " + "opfname AS sortfamily, " + "nspname AS sortfamilynsp " + "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON " + "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) " + "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily " + "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace " + "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass " + "AND refobjid = '%u'::pg_catalog.oid " + "AND amopfamily = '%u'::pg_catalog.oid " + "ORDER BY amopstrategy", + opfinfo->dobj.catId.oid, + opfinfo->dobj.catId.oid); + + res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + resetPQExpBuffer(query); + + appendPQExpBuffer(query, "SELECT amprocnum, " + "amproc::pg_catalog.regprocedure, " + "amproclefttype::pg_catalog.regtype, " + "amprocrighttype::pg_catalog.regtype " + "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend " + "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass " + "AND refobjid = '%u'::pg_catalog.oid " + "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass " + "AND objid = ap.oid " + "ORDER BY amprocnum", + opfinfo->dobj.catId.oid); + + res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + /* Get additional fields from the pg_opfamily row */ + resetPQExpBuffer(query); + + appendPQExpBuffer(query, "SELECT " + "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname " + "FROM pg_catalog.pg_opfamily " + "WHERE oid = '%u'::pg_catalog.oid", + opfinfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + i_amname = PQfnumber(res, "amname"); + + /* amname will still be needed after we PQclear res */ + amname = pg_strdup(PQgetvalue(res, 0, i_amname)); + + appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s", + fmtQualifiedDumpable(opfinfo)); + appendPQExpBuffer(delq, " USING %s;\n", + fmtId(amname)); + + /* Build the fixed portion of the CREATE command */ + appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s", + fmtQualifiedDumpable(opfinfo)); + appendPQExpBuffer(q, " USING %s;\n", + fmtId(amname)); + + PQclear(res); + + /* Do we need an ALTER to add loose members? */ + if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0) + { + appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s", + fmtQualifiedDumpable(opfinfo)); + appendPQExpBuffer(q, " USING %s ADD\n ", + fmtId(amname)); + + needComma = false; + + /* + * Now fetch and print the OPERATOR entries (pg_amop rows). + */ + ntups = PQntuples(res_ops); + + i_amopstrategy = PQfnumber(res_ops, "amopstrategy"); + i_amopopr = PQfnumber(res_ops, "amopopr"); + i_sortfamily = PQfnumber(res_ops, "sortfamily"); + i_sortfamilynsp = PQfnumber(res_ops, "sortfamilynsp"); + + for (i = 0; i < ntups; i++) + { + amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy); + amopopr = PQgetvalue(res_ops, i, i_amopopr); + sortfamily = PQgetvalue(res_ops, i, i_sortfamily); + sortfamilynsp = PQgetvalue(res_ops, i, i_sortfamilynsp); + + if (needComma) + appendPQExpBufferStr(q, " ,\n "); + + appendPQExpBuffer(q, "OPERATOR %s %s", + amopstrategy, amopopr); + + if (strlen(sortfamily) > 0) + { + appendPQExpBufferStr(q, " FOR ORDER BY "); + appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp)); + appendPQExpBufferStr(q, fmtId(sortfamily)); + } + + needComma = true; + } + + /* + * Now fetch and print the FUNCTION entries (pg_amproc rows). + */ + ntups = PQntuples(res_procs); + + i_amprocnum = PQfnumber(res_procs, "amprocnum"); + i_amproc = PQfnumber(res_procs, "amproc"); + i_amproclefttype = PQfnumber(res_procs, "amproclefttype"); + i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype"); + + for (i = 0; i < ntups; i++) + { + amprocnum = PQgetvalue(res_procs, i, i_amprocnum); + amproc = PQgetvalue(res_procs, i, i_amproc); + amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype); + amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype); + + if (needComma) + appendPQExpBufferStr(q, " ,\n "); + + appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s", + amprocnum, amproclefttype, amprocrighttype, + amproc); + + needComma = true; + } + + appendPQExpBufferStr(q, ";\n"); + } + + appendPQExpBufferStr(nameusing, fmtId(opfinfo->dobj.name)); + appendPQExpBuffer(nameusing, " USING %s", + fmtId(amname)); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &opfinfo->dobj, + "OPERATOR FAMILY", nameusing->data, + opfinfo->dobj.namespace->dobj.name); + + if (opfinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = opfinfo->dobj.name, + .namespace = opfinfo->dobj.namespace->dobj.name, + .owner = opfinfo->rolname, + .description = "OPERATOR FAMILY", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Operator Family Comments */ + if (opfinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "OPERATOR FAMILY", nameusing->data, + opfinfo->dobj.namespace->dobj.name, opfinfo->rolname, + opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId); + + free(amname); + PQclear(res_ops); + PQclear(res_procs); + destroyPQExpBuffer(query); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(nameusing); +} + +/* + * dumpCollation + * write out a single collation definition + */ +static void +dumpCollation(Archive *fout, const CollInfo *collinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PQExpBuffer q; + PQExpBuffer delq; + char *qcollname; + PGresult *res; + int i_collprovider; + int i_collisdeterministic; + int i_collcollate; + int i_collctype; + int i_colllocale; + int i_collicurules; + const char *collprovider; + const char *collcollate; + const char *collctype; + const char *colllocale; + const char *collicurules; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + query = createPQExpBuffer(); + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qcollname = pg_strdup(fmtId(collinfo->dobj.name)); + + /* Get collation-specific details */ + appendPQExpBufferStr(query, "SELECT "); + + if (fout->remoteVersion >= 100000) + appendPQExpBufferStr(query, + "collprovider, " + "collversion, "); + else + appendPQExpBufferStr(query, + "'c' AS collprovider, " + "NULL AS collversion, "); + + if (fout->remoteVersion >= 120000) + appendPQExpBufferStr(query, + "collisdeterministic, "); + else + appendPQExpBufferStr(query, + "true AS collisdeterministic, "); + + if (fout->remoteVersion >= 170000) + appendPQExpBufferStr(query, + "colllocale, "); + else if (fout->remoteVersion >= 150000) + appendPQExpBufferStr(query, + "colliculocale AS colllocale, "); + else + appendPQExpBufferStr(query, + "NULL AS colllocale, "); + + if (fout->remoteVersion >= 160000) + appendPQExpBufferStr(query, + "collicurules, "); + else + appendPQExpBufferStr(query, + "NULL AS collicurules, "); + + appendPQExpBuffer(query, + "collcollate, " + "collctype " + "FROM pg_catalog.pg_collation c " + "WHERE c.oid = '%u'::pg_catalog.oid", + collinfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + i_collprovider = PQfnumber(res, "collprovider"); + i_collisdeterministic = PQfnumber(res, "collisdeterministic"); + i_collcollate = PQfnumber(res, "collcollate"); + i_collctype = PQfnumber(res, "collctype"); + i_colllocale = PQfnumber(res, "colllocale"); + i_collicurules = PQfnumber(res, "collicurules"); + + collprovider = PQgetvalue(res, 0, i_collprovider); + + if (!PQgetisnull(res, 0, i_collcollate)) + collcollate = PQgetvalue(res, 0, i_collcollate); + else + collcollate = NULL; + + if (!PQgetisnull(res, 0, i_collctype)) + collctype = PQgetvalue(res, 0, i_collctype); + else + collctype = NULL; + + /* + * Before version 15, collcollate and collctype were of type NAME and + * non-nullable. Treat empty strings as NULL for consistency. + */ + if (fout->remoteVersion < 150000) + { + if (collcollate[0] == '\0') + collcollate = NULL; + if (collctype[0] == '\0') + collctype = NULL; + } + + if (!PQgetisnull(res, 0, i_colllocale)) + colllocale = PQgetvalue(res, 0, i_colllocale); + else + colllocale = NULL; + + if (!PQgetisnull(res, 0, i_collicurules)) + collicurules = PQgetvalue(res, 0, i_collicurules); + else + collicurules = NULL; + + appendPQExpBuffer(delq, "DROP COLLATION %s;\n", + fmtQualifiedDumpable(collinfo)); + + appendPQExpBuffer(q, "CREATE COLLATION %s (", + fmtQualifiedDumpable(collinfo)); + + appendPQExpBufferStr(q, "provider = "); + if (collprovider[0] == 'b') + appendPQExpBufferStr(q, "builtin"); + else if (collprovider[0] == 'c') + appendPQExpBufferStr(q, "libc"); + else if (collprovider[0] == 'i') + appendPQExpBufferStr(q, "icu"); + else if (collprovider[0] == 'd') + /* to allow dumping pg_catalog; not accepted on input */ + appendPQExpBufferStr(q, "default"); + else + pg_fatal("unrecognized collation provider: %s", + collprovider); + + if (strcmp(PQgetvalue(res, 0, i_collisdeterministic), "f") == 0) + appendPQExpBufferStr(q, ", deterministic = false"); + + if (collprovider[0] == 'd') + { + if (collcollate || collctype || colllocale || collicurules) + pg_log_warning("invalid collation \"%s\"", qcollname); + + /* no locale -- the default collation cannot be reloaded anyway */ + } + else if (collprovider[0] == 'b') + { + if (collcollate || collctype || !colllocale || collicurules) + pg_log_warning("invalid collation \"%s\"", qcollname); + + appendPQExpBufferStr(q, ", locale = "); + appendStringLiteralAH(q, colllocale ? colllocale : "", + fout); + } + else if (collprovider[0] == 'i') + { + if (fout->remoteVersion >= 150000) + { + if (collcollate || collctype || !colllocale) + pg_log_warning("invalid collation \"%s\"", qcollname); + + appendPQExpBufferStr(q, ", locale = "); + appendStringLiteralAH(q, colllocale ? colllocale : "", + fout); + } + else + { + if (!collcollate || !collctype || colllocale || + strcmp(collcollate, collctype) != 0) + pg_log_warning("invalid collation \"%s\"", qcollname); + + appendPQExpBufferStr(q, ", locale = "); + appendStringLiteralAH(q, collcollate ? collcollate : "", fout); + } + + if (collicurules) + { + appendPQExpBufferStr(q, ", rules = "); + appendStringLiteralAH(q, collicurules ? collicurules : "", fout); + } + } + else if (collprovider[0] == 'c') + { + if (colllocale || collicurules || !collcollate || !collctype) + pg_log_warning("invalid collation \"%s\"", qcollname); + + if (collcollate && collctype && strcmp(collcollate, collctype) == 0) + { + appendPQExpBufferStr(q, ", locale = "); + appendStringLiteralAH(q, collcollate ? collcollate : "", fout); + } + else + { + appendPQExpBufferStr(q, ", lc_collate = "); + appendStringLiteralAH(q, collcollate ? collcollate : "", fout); + appendPQExpBufferStr(q, ", lc_ctype = "); + appendStringLiteralAH(q, collctype ? collctype : "", fout); + } + } + else + pg_fatal("unrecognized collation provider: %s", collprovider); + + /* + * For binary upgrade, carry over the collation version. For normal + * dump/restore, omit the version, so that it is computed upon restore. + */ + if (dopt->binary_upgrade) + { + int i_collversion; + + i_collversion = PQfnumber(res, "collversion"); + if (!PQgetisnull(res, 0, i_collversion)) + { + appendPQExpBufferStr(q, ", version = "); + appendStringLiteralAH(q, + PQgetvalue(res, 0, i_collversion), + fout); + } + } + + appendPQExpBufferStr(q, ");\n"); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &collinfo->dobj, + "COLLATION", qcollname, + collinfo->dobj.namespace->dobj.name); + + if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = collinfo->dobj.name, + .namespace = collinfo->dobj.namespace->dobj.name, + .owner = collinfo->rolname, + .description = "COLLATION", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Collation Comments */ + if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "COLLATION", qcollname, + collinfo->dobj.namespace->dobj.name, collinfo->rolname, + collinfo->dobj.catId, 0, collinfo->dobj.dumpId); + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + free(qcollname); +} + +/* + * dumpConversion + * write out a single conversion definition + */ +static void +dumpConversion(Archive *fout, const ConvInfo *convinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PQExpBuffer q; + PQExpBuffer delq; + char *qconvname; + PGresult *res; + int i_conforencoding; + int i_contoencoding; + int i_conproc; + int i_condefault; + const char *conforencoding; + const char *contoencoding; + const char *conproc; + bool condefault; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + query = createPQExpBuffer(); + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qconvname = pg_strdup(fmtId(convinfo->dobj.name)); + + /* Get conversion-specific details */ + appendPQExpBuffer(query, "SELECT " + "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, " + "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, " + "conproc, condefault " + "FROM pg_catalog.pg_conversion c " + "WHERE c.oid = '%u'::pg_catalog.oid", + convinfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + i_conforencoding = PQfnumber(res, "conforencoding"); + i_contoencoding = PQfnumber(res, "contoencoding"); + i_conproc = PQfnumber(res, "conproc"); + i_condefault = PQfnumber(res, "condefault"); + + conforencoding = PQgetvalue(res, 0, i_conforencoding); + contoencoding = PQgetvalue(res, 0, i_contoencoding); + conproc = PQgetvalue(res, 0, i_conproc); + condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't'); + + appendPQExpBuffer(delq, "DROP CONVERSION %s;\n", + fmtQualifiedDumpable(convinfo)); + + appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ", + (condefault) ? "DEFAULT " : "", + fmtQualifiedDumpable(convinfo)); + appendStringLiteralAH(q, conforencoding, fout); + appendPQExpBufferStr(q, " TO "); + appendStringLiteralAH(q, contoencoding, fout); + /* regproc output is already sufficiently quoted */ + appendPQExpBuffer(q, " FROM %s;\n", conproc); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &convinfo->dobj, + "CONVERSION", qconvname, + convinfo->dobj.namespace->dobj.name); + + if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = convinfo->dobj.name, + .namespace = convinfo->dobj.namespace->dobj.name, + .owner = convinfo->rolname, + .description = "CONVERSION", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Conversion Comments */ + if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "CONVERSION", qconvname, + convinfo->dobj.namespace->dobj.name, convinfo->rolname, + convinfo->dobj.catId, 0, convinfo->dobj.dumpId); + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + free(qconvname); +} + +/* + * format_aggregate_signature: generate aggregate name and argument list + * + * The argument type names are qualified if needed. The aggregate name + * is never qualified. + */ +static char * +format_aggregate_signature(const AggInfo *agginfo, Archive *fout, bool honor_quotes) +{ + PQExpBufferData buf; + int j; + + initPQExpBuffer(&buf); + if (honor_quotes) + appendPQExpBufferStr(&buf, fmtId(agginfo->aggfn.dobj.name)); + else + appendPQExpBufferStr(&buf, agginfo->aggfn.dobj.name); + + if (agginfo->aggfn.nargs == 0) + appendPQExpBufferStr(&buf, "(*)"); + else + { + appendPQExpBufferChar(&buf, '('); + for (j = 0; j < agginfo->aggfn.nargs; j++) + appendPQExpBuffer(&buf, "%s%s", + (j > 0) ? ", " : "", + getFormattedTypeName(fout, + agginfo->aggfn.argtypes[j], + zeroIsError)); + appendPQExpBufferChar(&buf, ')'); + } + return buf.data; +} + +/* + * dumpAgg + * write out a single aggregate definition + */ +static void +dumpAgg(Archive *fout, const AggInfo *agginfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PQExpBuffer q; + PQExpBuffer delq; + PQExpBuffer details; + char *aggsig; /* identity signature */ + char *aggfullsig = NULL; /* full signature */ + char *aggsig_tag; + PGresult *res; + int i_agginitval; + int i_aggminitval; + const char *aggtransfn; + const char *aggfinalfn; + const char *aggcombinefn; + const char *aggserialfn; + const char *aggdeserialfn; + const char *aggmtransfn; + const char *aggminvtransfn; + const char *aggmfinalfn; + bool aggfinalextra; + bool aggmfinalextra; + char aggfinalmodify; + char aggmfinalmodify; + const char *aggsortop; + char *aggsortconvop; + char aggkind; + const char *aggtranstype; + const char *aggtransspace; + const char *aggmtranstype; + const char *aggmtransspace; + const char *agginitval; + const char *aggminitval; + const char *proparallel; + char defaultfinalmodify; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + query = createPQExpBuffer(); + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + details = createPQExpBuffer(); + + if (!fout->is_prepared[PREPQUERY_DUMPAGG]) + { + /* Set up query for aggregate-specific details */ + appendPQExpBufferStr(query, + "PREPARE dumpAgg(pg_catalog.oid) AS\n"); + + appendPQExpBufferStr(query, + "SELECT " + "aggtransfn,\n" + "aggfinalfn,\n" + "aggtranstype::pg_catalog.regtype,\n" + "agginitval,\n" + "aggsortop,\n" + "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs,\n" + "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs,\n"); + + if (fout->remoteVersion >= 90400) + appendPQExpBufferStr(query, + "aggkind,\n" + "aggmtransfn,\n" + "aggminvtransfn,\n" + "aggmfinalfn,\n" + "aggmtranstype::pg_catalog.regtype,\n" + "aggfinalextra,\n" + "aggmfinalextra,\n" + "aggtransspace,\n" + "aggmtransspace,\n" + "aggminitval,\n"); + else + appendPQExpBufferStr(query, + "'n' AS aggkind,\n" + "'-' AS aggmtransfn,\n" + "'-' AS aggminvtransfn,\n" + "'-' AS aggmfinalfn,\n" + "0 AS aggmtranstype,\n" + "false AS aggfinalextra,\n" + "false AS aggmfinalextra,\n" + "0 AS aggtransspace,\n" + "0 AS aggmtransspace,\n" + "NULL AS aggminitval,\n"); + + if (fout->remoteVersion >= 90600) + appendPQExpBufferStr(query, + "aggcombinefn,\n" + "aggserialfn,\n" + "aggdeserialfn,\n" + "proparallel,\n"); + else + appendPQExpBufferStr(query, + "'-' AS aggcombinefn,\n" + "'-' AS aggserialfn,\n" + "'-' AS aggdeserialfn,\n" + "'u' AS proparallel,\n"); + + if (fout->remoteVersion >= 110000) + appendPQExpBufferStr(query, + "aggfinalmodify,\n" + "aggmfinalmodify\n"); + else + appendPQExpBufferStr(query, + "'0' AS aggfinalmodify,\n" + "'0' AS aggmfinalmodify\n"); + + appendPQExpBufferStr(query, + "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p " + "WHERE a.aggfnoid = p.oid " + "AND p.oid = $1"); + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_DUMPAGG] = true; + } + + printfPQExpBuffer(query, + "EXECUTE dumpAgg('%u')", + agginfo->aggfn.dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + i_agginitval = PQfnumber(res, "agginitval"); + i_aggminitval = PQfnumber(res, "aggminitval"); + + aggtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggtransfn")); + aggfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggfinalfn")); + aggcombinefn = PQgetvalue(res, 0, PQfnumber(res, "aggcombinefn")); + aggserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggserialfn")); + aggdeserialfn = PQgetvalue(res, 0, PQfnumber(res, "aggdeserialfn")); + aggmtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggmtransfn")); + aggminvtransfn = PQgetvalue(res, 0, PQfnumber(res, "aggminvtransfn")); + aggmfinalfn = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalfn")); + aggfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggfinalextra"))[0] == 't'); + aggmfinalextra = (PQgetvalue(res, 0, PQfnumber(res, "aggmfinalextra"))[0] == 't'); + aggfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggfinalmodify"))[0]; + aggmfinalmodify = PQgetvalue(res, 0, PQfnumber(res, "aggmfinalmodify"))[0]; + aggsortop = PQgetvalue(res, 0, PQfnumber(res, "aggsortop")); + aggkind = PQgetvalue(res, 0, PQfnumber(res, "aggkind"))[0]; + aggtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggtranstype")); + aggtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggtransspace")); + aggmtranstype = PQgetvalue(res, 0, PQfnumber(res, "aggmtranstype")); + aggmtransspace = PQgetvalue(res, 0, PQfnumber(res, "aggmtransspace")); + agginitval = PQgetvalue(res, 0, i_agginitval); + aggminitval = PQgetvalue(res, 0, i_aggminitval); + proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel")); + + { + char *funcargs; + char *funciargs; + + funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs")); + funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs")); + aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true); + aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true); + } + + aggsig_tag = format_aggregate_signature(agginfo, fout, false); + + /* identify default modify flag for aggkind (must match DefineAggregate) */ + defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE; + /* replace omitted flags for old versions */ + if (aggfinalmodify == '0') + aggfinalmodify = defaultfinalmodify; + if (aggmfinalmodify == '0') + aggmfinalmodify = defaultfinalmodify; + + /* regproc and regtype output is already sufficiently quoted */ + appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s", + aggtransfn, aggtranstype); + + if (strcmp(aggtransspace, "0") != 0) + { + appendPQExpBuffer(details, ",\n SSPACE = %s", + aggtransspace); + } + + if (!PQgetisnull(res, 0, i_agginitval)) + { + appendPQExpBufferStr(details, ",\n INITCOND = "); + appendStringLiteralAH(details, agginitval, fout); + } + + if (strcmp(aggfinalfn, "-") != 0) + { + appendPQExpBuffer(details, ",\n FINALFUNC = %s", + aggfinalfn); + if (aggfinalextra) + appendPQExpBufferStr(details, ",\n FINALFUNC_EXTRA"); + if (aggfinalmodify != defaultfinalmodify) + { + switch (aggfinalmodify) + { + case AGGMODIFY_READ_ONLY: + appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_ONLY"); + break; + case AGGMODIFY_SHAREABLE: + appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = SHAREABLE"); + break; + case AGGMODIFY_READ_WRITE: + appendPQExpBufferStr(details, ",\n FINALFUNC_MODIFY = READ_WRITE"); + break; + default: + pg_fatal("unrecognized aggfinalmodify value for aggregate \"%s\"", + agginfo->aggfn.dobj.name); + break; + } + } + } + + if (strcmp(aggcombinefn, "-") != 0) + appendPQExpBuffer(details, ",\n COMBINEFUNC = %s", aggcombinefn); + + if (strcmp(aggserialfn, "-") != 0) + appendPQExpBuffer(details, ",\n SERIALFUNC = %s", aggserialfn); + + if (strcmp(aggdeserialfn, "-") != 0) + appendPQExpBuffer(details, ",\n DESERIALFUNC = %s", aggdeserialfn); + + if (strcmp(aggmtransfn, "-") != 0) + { + appendPQExpBuffer(details, ",\n MSFUNC = %s,\n MINVFUNC = %s,\n MSTYPE = %s", + aggmtransfn, + aggminvtransfn, + aggmtranstype); + } + + if (strcmp(aggmtransspace, "0") != 0) + { + appendPQExpBuffer(details, ",\n MSSPACE = %s", + aggmtransspace); + } + + if (!PQgetisnull(res, 0, i_aggminitval)) + { + appendPQExpBufferStr(details, ",\n MINITCOND = "); + appendStringLiteralAH(details, aggminitval, fout); + } + + if (strcmp(aggmfinalfn, "-") != 0) + { + appendPQExpBuffer(details, ",\n MFINALFUNC = %s", + aggmfinalfn); + if (aggmfinalextra) + appendPQExpBufferStr(details, ",\n MFINALFUNC_EXTRA"); + if (aggmfinalmodify != defaultfinalmodify) + { + switch (aggmfinalmodify) + { + case AGGMODIFY_READ_ONLY: + appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_ONLY"); + break; + case AGGMODIFY_SHAREABLE: + appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = SHAREABLE"); + break; + case AGGMODIFY_READ_WRITE: + appendPQExpBufferStr(details, ",\n MFINALFUNC_MODIFY = READ_WRITE"); + break; + default: + pg_fatal("unrecognized aggmfinalmodify value for aggregate \"%s\"", + agginfo->aggfn.dobj.name); + break; + } + } + } + + aggsortconvop = getFormattedOperatorName(aggsortop); + if (aggsortconvop) + { + appendPQExpBuffer(details, ",\n SORTOP = %s", + aggsortconvop); + free(aggsortconvop); + } + + if (aggkind == AGGKIND_HYPOTHETICAL) + appendPQExpBufferStr(details, ",\n HYPOTHETICAL"); + + if (proparallel[0] != PROPARALLEL_UNSAFE) + { + if (proparallel[0] == PROPARALLEL_SAFE) + appendPQExpBufferStr(details, ",\n PARALLEL = safe"); + else if (proparallel[0] == PROPARALLEL_RESTRICTED) + appendPQExpBufferStr(details, ",\n PARALLEL = restricted"); + else if (proparallel[0] != PROPARALLEL_UNSAFE) + pg_fatal("unrecognized proparallel value for function \"%s\"", + agginfo->aggfn.dobj.name); + } + + appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n", + fmtId(agginfo->aggfn.dobj.namespace->dobj.name), + aggsig); + + appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n", + fmtId(agginfo->aggfn.dobj.namespace->dobj.name), + aggfullsig ? aggfullsig : aggsig, details->data); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &agginfo->aggfn.dobj, + "AGGREGATE", aggsig, + agginfo->aggfn.dobj.namespace->dobj.name); + + if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, agginfo->aggfn.dobj.catId, + agginfo->aggfn.dobj.dumpId, + ARCHIVE_OPTS(.tag = aggsig_tag, + .namespace = agginfo->aggfn.dobj.namespace->dobj.name, + .owner = agginfo->aggfn.rolname, + .description = "AGGREGATE", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Aggregate Comments */ + if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "AGGREGATE", aggsig, + agginfo->aggfn.dobj.namespace->dobj.name, + agginfo->aggfn.rolname, + agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId); + + if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "AGGREGATE", aggsig, + agginfo->aggfn.dobj.namespace->dobj.name, + agginfo->aggfn.rolname, + agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId); + + /* + * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL + * command look like a function's GRANT; in particular this affects the + * syntax for zero-argument aggregates and ordered-set aggregates. + */ + free(aggsig); + + aggsig = format_function_signature(fout, &agginfo->aggfn, true); + + if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, agginfo->aggfn.dobj.dumpId, InvalidDumpId, + "FUNCTION", aggsig, NULL, + agginfo->aggfn.dobj.namespace->dobj.name, + NULL, agginfo->aggfn.rolname, &agginfo->aggfn.dacl); + + free(aggsig); + free(aggfullsig); + free(aggsig_tag); + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(details); +} + +/* + * dumpTSParser + * write out a single text search parser + */ +static void +dumpTSParser(Archive *fout, const TSParserInfo *prsinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + char *qprsname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qprsname = pg_strdup(fmtId(prsinfo->dobj.name)); + + appendPQExpBuffer(q, "CREATE TEXT SEARCH PARSER %s (\n", + fmtQualifiedDumpable(prsinfo)); + + appendPQExpBuffer(q, " START = %s,\n", + convertTSFunction(fout, prsinfo->prsstart)); + appendPQExpBuffer(q, " GETTOKEN = %s,\n", + convertTSFunction(fout, prsinfo->prstoken)); + appendPQExpBuffer(q, " END = %s,\n", + convertTSFunction(fout, prsinfo->prsend)); + if (prsinfo->prsheadline != InvalidOid) + appendPQExpBuffer(q, " HEADLINE = %s,\n", + convertTSFunction(fout, prsinfo->prsheadline)); + appendPQExpBuffer(q, " LEXTYPES = %s );\n", + convertTSFunction(fout, prsinfo->prslextype)); + + appendPQExpBuffer(delq, "DROP TEXT SEARCH PARSER %s;\n", + fmtQualifiedDumpable(prsinfo)); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &prsinfo->dobj, + "TEXT SEARCH PARSER", qprsname, + prsinfo->dobj.namespace->dobj.name); + + if (prsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, prsinfo->dobj.catId, prsinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = prsinfo->dobj.name, + .namespace = prsinfo->dobj.namespace->dobj.name, + .description = "TEXT SEARCH PARSER", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Parser Comments */ + if (prsinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TEXT SEARCH PARSER", qprsname, + prsinfo->dobj.namespace->dobj.name, "", + prsinfo->dobj.catId, 0, prsinfo->dobj.dumpId); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + free(qprsname); +} + +/* + * dumpTSDictionary + * write out a single text search dictionary + */ +static void +dumpTSDictionary(Archive *fout, const TSDictInfo *dictinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + PQExpBuffer query; + char *qdictname; + PGresult *res; + char *nspname; + char *tmplname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + query = createPQExpBuffer(); + + qdictname = pg_strdup(fmtId(dictinfo->dobj.name)); + + /* Fetch name and namespace of the dictionary's template */ + appendPQExpBuffer(query, "SELECT nspname, tmplname " + "FROM pg_ts_template p, pg_namespace n " + "WHERE p.oid = '%u' AND n.oid = tmplnamespace", + dictinfo->dicttemplate); + res = ExecuteSqlQueryForSingleRow(fout, query->data); + nspname = PQgetvalue(res, 0, 0); + tmplname = PQgetvalue(res, 0, 1); + + appendPQExpBuffer(q, "CREATE TEXT SEARCH DICTIONARY %s (\n", + fmtQualifiedDumpable(dictinfo)); + + appendPQExpBufferStr(q, " TEMPLATE = "); + appendPQExpBuffer(q, "%s.", fmtId(nspname)); + appendPQExpBufferStr(q, fmtId(tmplname)); + + PQclear(res); + + /* the dictinitoption can be dumped straight into the command */ + if (dictinfo->dictinitoption) + appendPQExpBuffer(q, ",\n %s", dictinfo->dictinitoption); + + appendPQExpBufferStr(q, " );\n"); + + appendPQExpBuffer(delq, "DROP TEXT SEARCH DICTIONARY %s;\n", + fmtQualifiedDumpable(dictinfo)); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &dictinfo->dobj, + "TEXT SEARCH DICTIONARY", qdictname, + dictinfo->dobj.namespace->dobj.name); + + if (dictinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, dictinfo->dobj.catId, dictinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = dictinfo->dobj.name, + .namespace = dictinfo->dobj.namespace->dobj.name, + .owner = dictinfo->rolname, + .description = "TEXT SEARCH DICTIONARY", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Dictionary Comments */ + if (dictinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TEXT SEARCH DICTIONARY", qdictname, + dictinfo->dobj.namespace->dobj.name, dictinfo->rolname, + dictinfo->dobj.catId, 0, dictinfo->dobj.dumpId); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qdictname); +} + +/* + * dumpTSTemplate + * write out a single text search template + */ +static void +dumpTSTemplate(Archive *fout, const TSTemplateInfo *tmplinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + char *qtmplname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qtmplname = pg_strdup(fmtId(tmplinfo->dobj.name)); + + appendPQExpBuffer(q, "CREATE TEXT SEARCH TEMPLATE %s (\n", + fmtQualifiedDumpable(tmplinfo)); + + if (tmplinfo->tmplinit != InvalidOid) + appendPQExpBuffer(q, " INIT = %s,\n", + convertTSFunction(fout, tmplinfo->tmplinit)); + appendPQExpBuffer(q, " LEXIZE = %s );\n", + convertTSFunction(fout, tmplinfo->tmpllexize)); + + appendPQExpBuffer(delq, "DROP TEXT SEARCH TEMPLATE %s;\n", + fmtQualifiedDumpable(tmplinfo)); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &tmplinfo->dobj, + "TEXT SEARCH TEMPLATE", qtmplname, + tmplinfo->dobj.namespace->dobj.name); + + if (tmplinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, tmplinfo->dobj.catId, tmplinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tmplinfo->dobj.name, + .namespace = tmplinfo->dobj.namespace->dobj.name, + .description = "TEXT SEARCH TEMPLATE", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Template Comments */ + if (tmplinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TEXT SEARCH TEMPLATE", qtmplname, + tmplinfo->dobj.namespace->dobj.name, "", + tmplinfo->dobj.catId, 0, tmplinfo->dobj.dumpId); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + free(qtmplname); +} + +/* + * dumpTSConfig + * write out a single text search configuration + */ +static void +dumpTSConfig(Archive *fout, const TSConfigInfo *cfginfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + PQExpBuffer query; + char *qcfgname; + PGresult *res; + char *nspname; + char *prsname; + int ntups, + i; + int i_tokenname; + int i_dictname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + query = createPQExpBuffer(); + + qcfgname = pg_strdup(fmtId(cfginfo->dobj.name)); + + /* Fetch name and namespace of the config's parser */ + appendPQExpBuffer(query, "SELECT nspname, prsname " + "FROM pg_ts_parser p, pg_namespace n " + "WHERE p.oid = '%u' AND n.oid = prsnamespace", + cfginfo->cfgparser); + res = ExecuteSqlQueryForSingleRow(fout, query->data); + nspname = PQgetvalue(res, 0, 0); + prsname = PQgetvalue(res, 0, 1); + + appendPQExpBuffer(q, "CREATE TEXT SEARCH CONFIGURATION %s (\n", + fmtQualifiedDumpable(cfginfo)); + + appendPQExpBuffer(q, " PARSER = %s.", fmtId(nspname)); + appendPQExpBuffer(q, "%s );\n", fmtId(prsname)); + + PQclear(res); + + resetPQExpBuffer(query); + appendPQExpBuffer(query, + "SELECT\n" + " ( SELECT alias FROM pg_catalog.ts_token_type('%u'::pg_catalog.oid) AS t\n" + " WHERE t.tokid = m.maptokentype ) AS tokenname,\n" + " m.mapdict::pg_catalog.regdictionary AS dictname\n" + "FROM pg_catalog.pg_ts_config_map AS m\n" + "WHERE m.mapcfg = '%u'\n" + "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno", + cfginfo->cfgparser, cfginfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + ntups = PQntuples(res); + + i_tokenname = PQfnumber(res, "tokenname"); + i_dictname = PQfnumber(res, "dictname"); + + for (i = 0; i < ntups; i++) + { + char *tokenname = PQgetvalue(res, i, i_tokenname); + char *dictname = PQgetvalue(res, i, i_dictname); + + if (i == 0 || + strcmp(tokenname, PQgetvalue(res, i - 1, i_tokenname)) != 0) + { + /* starting a new token type, so start a new command */ + if (i > 0) + appendPQExpBufferStr(q, ";\n"); + appendPQExpBuffer(q, "\nALTER TEXT SEARCH CONFIGURATION %s\n", + fmtQualifiedDumpable(cfginfo)); + /* tokenname needs quoting, dictname does NOT */ + appendPQExpBuffer(q, " ADD MAPPING FOR %s WITH %s", + fmtId(tokenname), dictname); + } + else + appendPQExpBuffer(q, ", %s", dictname); + } + + if (ntups > 0) + appendPQExpBufferStr(q, ";\n"); + + PQclear(res); + + appendPQExpBuffer(delq, "DROP TEXT SEARCH CONFIGURATION %s;\n", + fmtQualifiedDumpable(cfginfo)); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &cfginfo->dobj, + "TEXT SEARCH CONFIGURATION", qcfgname, + cfginfo->dobj.namespace->dobj.name); + + if (cfginfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, cfginfo->dobj.catId, cfginfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = cfginfo->dobj.name, + .namespace = cfginfo->dobj.namespace->dobj.name, + .owner = cfginfo->rolname, + .description = "TEXT SEARCH CONFIGURATION", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Configuration Comments */ + if (cfginfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "TEXT SEARCH CONFIGURATION", qcfgname, + cfginfo->dobj.namespace->dobj.name, cfginfo->rolname, + cfginfo->dobj.catId, 0, cfginfo->dobj.dumpId); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qcfgname); +} + +/* + * dumpForeignDataWrapper + * write out a single foreign-data wrapper definition + */ +static void +dumpForeignDataWrapper(Archive *fout, const FdwInfo *fdwinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + char *qfdwname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qfdwname = pg_strdup(fmtId(fdwinfo->dobj.name)); + + appendPQExpBuffer(q, "CREATE FOREIGN DATA WRAPPER %s", + qfdwname); + + if (strcmp(fdwinfo->fdwhandler, "-") != 0) + appendPQExpBuffer(q, " HANDLER %s", fdwinfo->fdwhandler); + + if (strcmp(fdwinfo->fdwvalidator, "-") != 0) + appendPQExpBuffer(q, " VALIDATOR %s", fdwinfo->fdwvalidator); + + if (strcmp(fdwinfo->fdwconnection, "-") != 0) + appendPQExpBuffer(q, " CONNECTION %s", fdwinfo->fdwconnection); + + if (strlen(fdwinfo->fdwoptions) > 0) + appendPQExpBuffer(q, " OPTIONS (\n %s\n)", fdwinfo->fdwoptions); + + appendPQExpBufferStr(q, ";\n"); + + appendPQExpBuffer(delq, "DROP FOREIGN DATA WRAPPER %s;\n", + qfdwname); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &fdwinfo->dobj, + "FOREIGN DATA WRAPPER", qfdwname, + NULL); + + if (fdwinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = fdwinfo->dobj.name, + .owner = fdwinfo->rolname, + .description = "FOREIGN DATA WRAPPER", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Foreign Data Wrapper Comments */ + if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "FOREIGN DATA WRAPPER", qfdwname, + NULL, fdwinfo->rolname, + fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId); + + /* Handle the ACL */ + if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, fdwinfo->dobj.dumpId, InvalidDumpId, + "FOREIGN DATA WRAPPER", qfdwname, NULL, NULL, + NULL, fdwinfo->rolname, &fdwinfo->dacl); + + free(qfdwname); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); +} + +/* + * dumpForeignServer + * write out a foreign server definition + */ +static void +dumpForeignServer(Archive *fout, const ForeignServerInfo *srvinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + PQExpBuffer query; + PGresult *res; + char *qsrvname; + char *fdwname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + query = createPQExpBuffer(); + + qsrvname = pg_strdup(fmtId(srvinfo->dobj.name)); + + /* look up the foreign-data wrapper */ + appendPQExpBuffer(query, "SELECT fdwname " + "FROM pg_foreign_data_wrapper w " + "WHERE w.oid = '%u'", + srvinfo->srvfdw); + res = ExecuteSqlQueryForSingleRow(fout, query->data); + fdwname = PQgetvalue(res, 0, 0); + + appendPQExpBuffer(q, "CREATE SERVER IF NOT EXISTS %s", qsrvname); + if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0) + { + appendPQExpBufferStr(q, " TYPE "); + appendStringLiteralAH(q, srvinfo->srvtype, fout); + } + if (srvinfo->srvversion && strlen(srvinfo->srvversion) > 0) + { + appendPQExpBufferStr(q, " VERSION "); + appendStringLiteralAH(q, srvinfo->srvversion, fout); + } + + appendPQExpBufferStr(q, " FOREIGN DATA WRAPPER "); + appendPQExpBufferStr(q, fmtId(fdwname)); + + if (srvinfo->srvoptions && strlen(srvinfo->srvoptions) > 0) + appendPQExpBuffer(q, " OPTIONS (\n %s\n)", srvinfo->srvoptions); + + appendPQExpBufferStr(q, ";\n"); + + appendPQExpBuffer(delq, "DROP SERVER %s;\n", + qsrvname); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &srvinfo->dobj, + "SERVER", qsrvname, NULL); + + if (srvinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = srvinfo->dobj.name, + .owner = srvinfo->rolname, + .description = "SERVER", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Foreign Server Comments */ + if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "SERVER", qsrvname, + NULL, srvinfo->rolname, + srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId); + + /* Handle the ACL */ + if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL) + dumpACL(fout, srvinfo->dobj.dumpId, InvalidDumpId, + "FOREIGN SERVER", qsrvname, NULL, NULL, + NULL, srvinfo->rolname, &srvinfo->dacl); + + /* Dump user mappings */ + if (srvinfo->dobj.dump & DUMP_COMPONENT_USERMAP) + dumpUserMappings(fout, + srvinfo->dobj.name, NULL, + srvinfo->rolname, + srvinfo->dobj.catId, srvinfo->dobj.dumpId); + + PQclear(res); + + free(qsrvname); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); +} + +/* + * dumpUserMappings + * + * This routine is used to dump any user mappings associated with the + * server handed to this routine. Should be called after ArchiveEntry() + * for the server. + */ +static void +dumpUserMappings(Archive *fout, + const char *servername, const char *namespace, + const char *owner, + CatalogId catalogId, DumpId dumpId) +{ + PQExpBuffer q; + PQExpBuffer delq; + PQExpBuffer query; + PQExpBuffer tag; + PGresult *res; + int ntups; + int i_usename; + int i_umoptions; + int i; + + q = createPQExpBuffer(); + tag = createPQExpBuffer(); + delq = createPQExpBuffer(); + query = createPQExpBuffer(); + + /* + * We read from the publicly accessible view pg_user_mappings, so as not + * to fail if run by a non-superuser. Note that the view will show + * umoptions as null if the user hasn't got privileges for the associated + * server; this means that pg_dump will dump such a mapping, but with no + * OPTIONS clause. A possible alternative is to skip such mappings + * altogether, but it's not clear that that's an improvement. + */ + appendPQExpBuffer(query, + "SELECT usename, " + "array_to_string(ARRAY(" + "SELECT quote_ident(option_name) || ' ' || " + "quote_literal(option_value) " + "FROM pg_options_to_table(umoptions) " + "ORDER BY option_name" + "), E',\n ') AS umoptions " + "FROM pg_user_mappings " + "WHERE srvid = '%u' " + "ORDER BY usename", + catalogId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + i_usename = PQfnumber(res, "usename"); + i_umoptions = PQfnumber(res, "umoptions"); + + for (i = 0; i < ntups; i++) + { + char *usename; + char *umoptions; + + usename = PQgetvalue(res, i, i_usename); + umoptions = PQgetvalue(res, i, i_umoptions); + + resetPQExpBuffer(q); + appendPQExpBuffer(q, "CREATE USER MAPPING IF NOT EXISTS FOR %s", fmtId(usename)); + appendPQExpBuffer(q, " SERVER %s", fmtId(servername)); + + if (umoptions && strlen(umoptions) > 0) + appendPQExpBuffer(q, " OPTIONS (\n %s\n)", umoptions); + + appendPQExpBufferStr(q, ";\n"); + + resetPQExpBuffer(delq); + appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename)); + appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername)); + + resetPQExpBuffer(tag); + appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s", + usename, servername); + + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = tag->data, + .namespace = namespace, + .owner = owner, + .description = "USER MAPPING", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + } + + PQclear(res); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(tag); + destroyPQExpBuffer(q); +} + +/* + * Write out default privileges information + */ +static void +dumpDefaultACL(Archive *fout, const DefaultACLInfo *daclinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer tag; + const char *type; + + /* Do nothing if not dumping schema, or if we're skipping ACLs */ + if (!dopt->dumpSchema || dopt->aclsSkip) + return; + + q = createPQExpBuffer(); + tag = createPQExpBuffer(); + + switch (daclinfo->defaclobjtype) + { + case DEFACLOBJ_RELATION: + type = "TABLES"; + break; + case DEFACLOBJ_SEQUENCE: + type = "SEQUENCES"; + break; + case DEFACLOBJ_FUNCTION: + type = "FUNCTIONS"; + break; + case DEFACLOBJ_TYPE: + type = "TYPES"; + break; + case DEFACLOBJ_NAMESPACE: + type = "SCHEMAS"; + break; + case DEFACLOBJ_LARGEOBJECT: + type = "LARGE OBJECTS"; + break; + default: + /* shouldn't get here */ + pg_fatal("unrecognized object type in default privileges: %d", + (int) daclinfo->defaclobjtype); + type = ""; /* keep compiler quiet */ + } + + appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type); + + /* build the actual command(s) for this tuple */ + if (!buildDefaultACLCommands(type, + daclinfo->dobj.namespace != NULL ? + daclinfo->dobj.namespace->dobj.name : NULL, + daclinfo->dacl.acl, + daclinfo->dacl.acldefault, + daclinfo->defaclrole, + fout->remoteVersion, + q)) + pg_fatal("could not parse default ACL list (%s)", + daclinfo->dacl.acl); + + if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL) + ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag->data, + .namespace = daclinfo->dobj.namespace ? + daclinfo->dobj.namespace->dobj.name : NULL, + .owner = daclinfo->defaclrole, + .description = "DEFAULT ACL", + .section = SECTION_POST_DATA, + .createStmt = q->data)); + + destroyPQExpBuffer(tag); + destroyPQExpBuffer(q); +} + +/*---------- + * Write out grant/revoke information + * + * 'objDumpId' is the dump ID of the underlying object. + * 'altDumpId' can be a second dumpId that the ACL entry must also depend on, + * or InvalidDumpId if there is no need for a second dependency. + * 'type' must be one of + * TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE, + * FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT. + * 'name' is the formatted name of the object. Must be quoted etc. already. + * 'subname' is the formatted name of the sub-object, if any. Must be quoted. + * (Currently we assume that subname is only provided for table columns.) + * 'nspname' is the namespace the object is in (NULL if none). + * 'tag' is the tag to use for the ACL TOC entry; typically, this is NULL + * to use the default for the object type. + * 'owner' is the owner, NULL if there is no owner (for languages). + * 'dacl' is the DumpableAcl struct for the object. + * + * Returns the dump ID assigned to the ACL TocEntry, or InvalidDumpId if + * no ACL entry was created. + *---------- + */ +static DumpId +dumpACL(Archive *fout, DumpId objDumpId, DumpId altDumpId, + const char *type, const char *name, const char *subname, + const char *nspname, const char *tag, const char *owner, + const DumpableAcl *dacl) +{ + DumpId aclDumpId = InvalidDumpId; + DumpOptions *dopt = fout->dopt; + const char *acls = dacl->acl; + const char *acldefault = dacl->acldefault; + char privtype = dacl->privtype; + const char *initprivs = dacl->initprivs; + const char *baseacls; + PQExpBuffer sql; + + /* Do nothing if ACL dump is not enabled */ + if (dopt->aclsSkip) + return InvalidDumpId; + + /* --data-only skips ACLs *except* large object ACLs */ + if (!dopt->dumpSchema && strcmp(type, "LARGE OBJECT") != 0) + return InvalidDumpId; + + sql = createPQExpBuffer(); + + /* + * In binary upgrade mode, we don't run an extension's script but instead + * dump out the objects independently and then recreate them. To preserve + * any initial privileges which were set on extension objects, we need to + * compute the set of GRANT and REVOKE commands necessary to get from the + * default privileges of an object to its initial privileges as recorded + * in pg_init_privs. + * + * At restore time, we apply these commands after having called + * binary_upgrade_set_record_init_privs(true). That tells the backend to + * copy the results into pg_init_privs. This is how we preserve the + * contents of that catalog across binary upgrades. + */ + if (dopt->binary_upgrade && privtype == 'e' && + initprivs && *initprivs != '\0') + { + appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n"); + if (!buildACLCommands(name, subname, nspname, type, + initprivs, acldefault, owner, + "", fout->remoteVersion, sql)) + pg_fatal("could not parse initial ACL list (%s) or default (%s) for object \"%s\" (%s)", + initprivs, acldefault, name, type); + appendPQExpBufferStr(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n"); + } + + /* + * Now figure the GRANT and REVOKE commands needed to get to the object's + * actual current ACL, starting from the initprivs if given, else from the + * object-type-specific default. Also, while buildACLCommands will assume + * that a NULL/empty acls string means it needn't do anything, what that + * actually represents is the object-type-specific default; so we need to + * substitute the acldefault string to get the right results in that case. + */ + if (initprivs && *initprivs != '\0') + { + baseacls = initprivs; + if (acls == NULL || *acls == '\0') + acls = acldefault; + } + else + baseacls = acldefault; + + if (!buildACLCommands(name, subname, nspname, type, + acls, baseacls, owner, + "", fout->remoteVersion, sql)) + pg_fatal("could not parse ACL list (%s) or default (%s) for object \"%s\" (%s)", + acls, baseacls, name, type); + + if (sql->len > 0) + { + PQExpBuffer tagbuf = createPQExpBuffer(); + DumpId aclDeps[2]; + int nDeps = 0; + + if (tag) + appendPQExpBufferStr(tagbuf, tag); + else if (subname) + appendPQExpBuffer(tagbuf, "COLUMN %s.%s", name, subname); + else + appendPQExpBuffer(tagbuf, "%s %s", type, name); + + aclDeps[nDeps++] = objDumpId; + if (altDumpId != InvalidDumpId) + aclDeps[nDeps++] = altDumpId; + + aclDumpId = createDumpId(); + + ArchiveEntry(fout, nilCatalogId, aclDumpId, + ARCHIVE_OPTS(.tag = tagbuf->data, + .namespace = nspname, + .owner = owner, + .description = "ACL", + .section = SECTION_NONE, + .createStmt = sql->data, + .deps = aclDeps, + .nDeps = nDeps)); + + destroyPQExpBuffer(tagbuf); + } + + destroyPQExpBuffer(sql); + + return aclDumpId; +} + +/* + * dumpSecLabel + * + * This routine is used to dump any security labels associated with the + * object handed to this routine. The routine takes the object type + * and object name (ready to print, except for schema decoration), plus + * the namespace and owner of the object (for labeling the ArchiveEntry), + * plus catalog ID and subid which are the lookup key for pg_seclabel, + * plus the dump ID for the object (for setting a dependency). + * If a matching pg_seclabel entry is found, it is dumped. + * + * Note: although this routine takes a dumpId for dependency purposes, + * that purpose is just to mark the dependency in the emitted dump file + * for possible future use by pg_restore. We do NOT use it for determining + * ordering of the label in the dump file, because this routine is called + * after dependency sorting occurs. This routine should be called just after + * calling ArchiveEntry() for the specified object. + */ +static void +dumpSecLabel(Archive *fout, const char *type, const char *name, + const char *namespace, const char *owner, + CatalogId catalogId, int subid, DumpId dumpId) +{ + DumpOptions *dopt = fout->dopt; + SecLabelItem *labels; + int nlabels; + int i; + PQExpBuffer query; + + /* do nothing, if --no-security-labels is supplied */ + if (dopt->no_security_labels) + return; + + /* + * Security labels are schema not data ... except large object labels are + * data + */ + if (strcmp(type, "LARGE OBJECT") != 0) + { + if (!dopt->dumpSchema) + return; + } + else + { + /* We do dump large object security labels in binary-upgrade mode */ + if (!dopt->dumpData && !dopt->binary_upgrade) + return; + } + + /* Search for security labels associated with catalogId, using table */ + nlabels = findSecLabels(catalogId.tableoid, catalogId.oid, &labels); + + query = createPQExpBuffer(); + + for (i = 0; i < nlabels; i++) + { + /* + * Ignore label entries for which the subid doesn't match. + */ + if (labels[i].objsubid != subid) + continue; + + appendPQExpBuffer(query, + "SECURITY LABEL FOR %s ON %s ", + fmtId(labels[i].provider), type); + if (namespace && *namespace) + appendPQExpBuffer(query, "%s.", fmtId(namespace)); + appendPQExpBuffer(query, "%s IS ", name); + appendStringLiteralAH(query, labels[i].label, fout); + appendPQExpBufferStr(query, ";\n"); + } + + if (query->len > 0) + { + PQExpBuffer tag = createPQExpBuffer(); + + appendPQExpBuffer(tag, "%s %s", type, name); + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = tag->data, + .namespace = namespace, + .owner = owner, + .description = "SECURITY LABEL", + .section = SECTION_NONE, + .createStmt = query->data, + .deps = &dumpId, + .nDeps = 1)); + destroyPQExpBuffer(tag); + } + + destroyPQExpBuffer(query); +} + +/* + * dumpTableSecLabel + * + * As above, but dump security label for both the specified table (or view) + * and its columns. + */ +static void +dumpTableSecLabel(Archive *fout, const TableInfo *tbinfo, const char *reltypename) +{ + DumpOptions *dopt = fout->dopt; + SecLabelItem *labels; + int nlabels; + int i; + PQExpBuffer query; + PQExpBuffer target; + + /* do nothing, if --no-security-labels is supplied */ + if (dopt->no_security_labels) + return; + + /* SecLabel are SCHEMA not data */ + if (!dopt->dumpSchema) + return; + + /* Search for comments associated with relation, using table */ + nlabels = findSecLabels(tbinfo->dobj.catId.tableoid, + tbinfo->dobj.catId.oid, + &labels); + + /* If security labels exist, build SECURITY LABEL statements */ + if (nlabels <= 0) + return; + + query = createPQExpBuffer(); + target = createPQExpBuffer(); + + for (i = 0; i < nlabels; i++) + { + const char *colname; + const char *provider = labels[i].provider; + const char *label = labels[i].label; + int objsubid = labels[i].objsubid; + + resetPQExpBuffer(target); + if (objsubid == 0) + { + appendPQExpBuffer(target, "%s %s", reltypename, + fmtQualifiedDumpable(tbinfo)); + } + else + { + colname = getAttrName(objsubid, tbinfo); + /* first fmtXXX result must be consumed before calling again */ + appendPQExpBuffer(target, "COLUMN %s", + fmtQualifiedDumpable(tbinfo)); + appendPQExpBuffer(target, ".%s", fmtId(colname)); + } + appendPQExpBuffer(query, "SECURITY LABEL FOR %s ON %s IS ", + fmtId(provider), target->data); + appendStringLiteralAH(query, label, fout); + appendPQExpBufferStr(query, ";\n"); + } + if (query->len > 0) + { + resetPQExpBuffer(target); + appendPQExpBuffer(target, "%s %s", reltypename, + fmtId(tbinfo->dobj.name)); + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = target->data, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "SECURITY LABEL", + .section = SECTION_NONE, + .createStmt = query->data, + .deps = &(tbinfo->dobj.dumpId), + .nDeps = 1)); + } + destroyPQExpBuffer(query); + destroyPQExpBuffer(target); +} + +/* + * findSecLabels + * + * Find the security label(s), if any, associated with the given object. + * All the objsubid values associated with the given classoid/objoid are + * found with one search. + */ +static int +findSecLabels(Oid classoid, Oid objoid, SecLabelItem **items) +{ + SecLabelItem *middle = NULL; + SecLabelItem *low; + SecLabelItem *high; + int nmatch; + + if (nseclabels <= 0) /* no labels, so no match is possible */ + { + *items = NULL; + return 0; + } + + /* + * Do binary search to find some item matching the object. + */ + low = &seclabels[0]; + high = &seclabels[nseclabels - 1]; + while (low <= high) + { + middle = low + (high - low) / 2; + + if (classoid < middle->classoid) + high = middle - 1; + else if (classoid > middle->classoid) + low = middle + 1; + else if (objoid < middle->objoid) + high = middle - 1; + else if (objoid > middle->objoid) + low = middle + 1; + else + break; /* found a match */ + } + + if (low > high) /* no matches */ + { + *items = NULL; + return 0; + } + + /* + * Now determine how many items match the object. The search loop + * invariant still holds: only items between low and high inclusive could + * match. + */ + nmatch = 1; + while (middle > low) + { + if (classoid != middle[-1].classoid || + objoid != middle[-1].objoid) + break; + middle--; + nmatch++; + } + + *items = middle; + + middle += nmatch; + while (middle <= high) + { + if (classoid != middle->classoid || + objoid != middle->objoid) + break; + middle++; + nmatch++; + } + + return nmatch; +} + +/* + * collectSecLabels + * + * Construct a table of all security labels available for database objects; + * also set the has-seclabel component flag for each relevant object. + * + * The table is sorted by classoid/objid/objsubid for speed in lookup. + */ +static void +collectSecLabels(Archive *fout) +{ + PGresult *res; + PQExpBuffer query; + int i_label; + int i_provider; + int i_classoid; + int i_objoid; + int i_objsubid; + int ntups; + int i; + DumpableObject *dobj; + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, + "SELECT label, provider, classoid, objoid, objsubid " + "FROM pg_catalog.pg_seclabels " + "ORDER BY classoid, objoid, objsubid"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + /* Construct lookup table containing OIDs in numeric form */ + i_label = PQfnumber(res, "label"); + i_provider = PQfnumber(res, "provider"); + i_classoid = PQfnumber(res, "classoid"); + i_objoid = PQfnumber(res, "objoid"); + i_objsubid = PQfnumber(res, "objsubid"); + + ntups = PQntuples(res); + + seclabels = pg_malloc_array(SecLabelItem, ntups); + nseclabels = 0; + dobj = NULL; + + for (i = 0; i < ntups; i++) + { + CatalogId objId; + int subid; + + objId.tableoid = atooid(PQgetvalue(res, i, i_classoid)); + objId.oid = atooid(PQgetvalue(res, i, i_objoid)); + subid = atoi(PQgetvalue(res, i, i_objsubid)); + + /* We needn't remember labels that don't match any dumpable object */ + if (dobj == NULL || + dobj->catId.tableoid != objId.tableoid || + dobj->catId.oid != objId.oid) + dobj = findObjectByCatalogId(objId); + if (dobj == NULL) + continue; + + /* + * Labels on columns of composite types are linked to the type's + * pg_class entry, but we need to set the DUMP_COMPONENT_SECLABEL flag + * in the type's own DumpableObject. + */ + if (subid != 0 && dobj->objType == DO_TABLE && + ((TableInfo *) dobj)->relkind == RELKIND_COMPOSITE_TYPE) + { + TypeInfo *cTypeInfo; + + cTypeInfo = findTypeByOid(((TableInfo *) dobj)->reltype); + if (cTypeInfo) + cTypeInfo->dobj.components |= DUMP_COMPONENT_SECLABEL; + } + else + dobj->components |= DUMP_COMPONENT_SECLABEL; + + seclabels[nseclabels].label = pg_strdup(PQgetvalue(res, i, i_label)); + seclabels[nseclabels].provider = pg_strdup(PQgetvalue(res, i, i_provider)); + seclabels[nseclabels].classoid = objId.tableoid; + seclabels[nseclabels].objoid = objId.oid; + seclabels[nseclabels].objsubid = subid; + nseclabels++; + } + + PQclear(res); + destroyPQExpBuffer(query); +} + +/* + * dumpTable + * write out to fout the declarations (not data) of a user-defined table + */ +static void +dumpTable(Archive *fout, const TableInfo *tbinfo) +{ + DumpOptions *dopt = fout->dopt; + DumpId tableAclDumpId = InvalidDumpId; + char *namecopy; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + { + if (tbinfo->relkind == RELKIND_SEQUENCE) + dumpSequence(fout, tbinfo); + else + dumpTableSchema(fout, tbinfo); + } + + /* Handle the ACL here */ + namecopy = pg_strdup(fmtId(tbinfo->dobj.name)); + if (tbinfo->dobj.dump & DUMP_COMPONENT_ACL) + { + const char *objtype; + + switch (tbinfo->relkind) + { + case RELKIND_SEQUENCE: + objtype = "SEQUENCE"; + break; + case RELKIND_PROPGRAPH: + objtype = "PROPERTY GRAPH"; + break; + default: + objtype = "TABLE"; + break; + } + + tableAclDumpId = + dumpACL(fout, tbinfo->dobj.dumpId, InvalidDumpId, + objtype, namecopy, NULL, + tbinfo->dobj.namespace->dobj.name, + NULL, tbinfo->rolname, &tbinfo->dacl); + } + + /* + * Handle column ACLs, if any. Note: we pull these with a separate query + * rather than trying to fetch them during getTableAttrs, so that we won't + * miss ACLs on system columns. Doing it this way also allows us to dump + * ACLs for catalogs that we didn't mark "interesting" back in getTables. + */ + if ((tbinfo->dobj.dump & DUMP_COMPONENT_ACL) && tbinfo->hascolumnACLs) + { + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + int i; + + if (!fout->is_prepared[PREPQUERY_GETCOLUMNACLS]) + { + /* Set up query for column ACLs */ + appendPQExpBufferStr(query, + "PREPARE getColumnACLs(pg_catalog.oid) AS\n"); + + if (fout->remoteVersion >= 90600) + { + /* + * In principle we should call acldefault('c', relowner) to + * get the default ACL for a column. However, we don't + * currently store the numeric OID of the relowner in + * TableInfo. We could convert the owner name using regrole, + * but that creates a risk of failure due to concurrent role + * renames. Given that the default ACL for columns is empty + * and is likely to stay that way, it's not worth extra cycles + * and risk to avoid hard-wiring that knowledge here. + */ + appendPQExpBufferStr(query, + "SELECT at.attname, " + "at.attacl, " + "'{}' AS acldefault, " + "pip.privtype, pip.initprivs " + "FROM pg_catalog.pg_attribute at " + "LEFT JOIN pg_catalog.pg_init_privs pip ON " + "(at.attrelid = pip.objoid " + "AND pip.classoid = 'pg_catalog.pg_class'::pg_catalog.regclass " + "AND at.attnum = pip.objsubid) " + "WHERE at.attrelid = $1 AND " + "NOT at.attisdropped " + "AND (at.attacl IS NOT NULL OR pip.initprivs IS NOT NULL) " + "ORDER BY at.attnum"); + } + else + { + appendPQExpBufferStr(query, + "SELECT attname, attacl, '{}' AS acldefault, " + "NULL AS privtype, NULL AS initprivs " + "FROM pg_catalog.pg_attribute " + "WHERE attrelid = $1 AND NOT attisdropped " + "AND attacl IS NOT NULL " + "ORDER BY attnum"); + } + + ExecuteSqlStatement(fout, query->data); + + fout->is_prepared[PREPQUERY_GETCOLUMNACLS] = true; + } + + printfPQExpBuffer(query, + "EXECUTE getColumnACLs('%u')", + tbinfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + for (i = 0; i < PQntuples(res); i++) + { + char *attname = PQgetvalue(res, i, 0); + char *attacl = PQgetvalue(res, i, 1); + char *acldefault = PQgetvalue(res, i, 2); + char privtype = *(PQgetvalue(res, i, 3)); + char *initprivs = PQgetvalue(res, i, 4); + DumpableAcl coldacl; + char *attnamecopy; + + coldacl.acl = attacl; + coldacl.acldefault = acldefault; + coldacl.privtype = privtype; + coldacl.initprivs = initprivs; + attnamecopy = pg_strdup(fmtId(attname)); + + /* + * Column's GRANT type is always TABLE. Each column ACL depends + * on the table-level ACL, since we can restore column ACLs in + * parallel but the table-level ACL has to be done first. + */ + dumpACL(fout, tbinfo->dobj.dumpId, tableAclDumpId, + "TABLE", namecopy, attnamecopy, + tbinfo->dobj.namespace->dobj.name, + NULL, tbinfo->rolname, &coldacl); + free(attnamecopy); + } + PQclear(res); + destroyPQExpBuffer(query); + } + + free(namecopy); +} + +/* + * Create the AS clause for a view or materialized view. The semicolon is + * stripped because a materialized view must add a WITH NO DATA clause. + * + * This returns a new buffer which must be freed by the caller. + */ +static PQExpBuffer +createViewAsClause(Archive *fout, const TableInfo *tbinfo) +{ + PQExpBuffer query = createPQExpBuffer(); + PQExpBuffer result = createPQExpBuffer(); + PGresult *res; + int len; + + /* Fetch the view definition */ + appendPQExpBuffer(query, + "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef", + tbinfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + if (PQntuples(res) != 1) + { + if (PQntuples(res) < 1) + pg_fatal("query to obtain definition of view \"%s\" returned no data", + tbinfo->dobj.name); + else + pg_fatal("query to obtain definition of view \"%s\" returned more than one definition", + tbinfo->dobj.name); + } + + len = PQgetlength(res, 0, 0); + + if (len == 0) + pg_fatal("definition of view \"%s\" appears to be empty (length zero)", + tbinfo->dobj.name); + + /* Strip off the trailing semicolon so that other things may follow. */ + Assert(PQgetvalue(res, 0, 0)[len - 1] == ';'); + appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1); + + PQclear(res); + destroyPQExpBuffer(query); + + return result; +} + +/* + * Create a dummy AS clause for a view. This is used when the real view + * definition has to be postponed because of circular dependencies. + * We must duplicate the view's external properties -- column names and types + * (including collation) -- so that it works for subsequent references. + * + * This returns a new buffer which must be freed by the caller. + */ +static PQExpBuffer +createDummyViewAsClause(Archive *fout, const TableInfo *tbinfo) +{ + PQExpBuffer result = createPQExpBuffer(); + int j; + + appendPQExpBufferStr(result, "SELECT"); + + for (j = 0; j < tbinfo->numatts; j++) + { + if (j > 0) + appendPQExpBufferChar(result, ','); + appendPQExpBufferStr(result, "\n "); + + appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]); + + /* + * Must add collation if not default for the type, because CREATE OR + * REPLACE VIEW won't change it + */ + if (OidIsValid(tbinfo->attcollation[j])) + { + CollInfo *coll; + + coll = findCollationByOid(tbinfo->attcollation[j]); + if (coll) + appendPQExpBuffer(result, " COLLATE %s", + fmtQualifiedDumpable(coll)); + } + + appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j])); + } + + return result; +} + +/* + * dumpTableSchema + * write the declaration (not data) of one user-defined table or view + */ +static void +dumpTableSchema(Archive *fout, const TableInfo *tbinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q = createPQExpBuffer(); + PQExpBuffer delq = createPQExpBuffer(); + PQExpBuffer extra = createPQExpBuffer(); + char *qrelname; + char *qualrelname; + int numParents; + TableInfo **parents; + int actual_atts; /* number of attrs in this CREATE statement */ + const char *reltypename; + char *storage; + int j, + k; + + /* We had better have loaded per-column details about this table */ + Assert(tbinfo->interesting); + + qrelname = pg_strdup(fmtId(tbinfo->dobj.name)); + qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo)); + + if (tbinfo->hasoids) + pg_log_warning("WITH OIDS is not supported anymore (table \"%s\")", + qrelname); + + if (dopt->binary_upgrade) + binary_upgrade_set_type_oids_by_rel(fout, q, tbinfo); + + /* Is it a table or a view? */ + if (tbinfo->relkind == RELKIND_VIEW) + { + PQExpBuffer result; + + /* + * Note: keep this code in sync with the is_view case in dumpRule() + */ + + reltypename = "VIEW"; + + if (dopt->binary_upgrade) + binary_upgrade_set_pg_class_oids(fout, q, + tbinfo->dobj.catId.oid); + + appendPQExpBuffer(q, "CREATE VIEW %s", qualrelname); + + if (tbinfo->dummy_view) + result = createDummyViewAsClause(fout, tbinfo); + else + { + if (nonemptyReloptions(tbinfo->reloptions)) + { + appendPQExpBufferStr(q, " WITH ("); + appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout); + appendPQExpBufferChar(q, ')'); + } + result = createViewAsClause(fout, tbinfo); + } + appendPQExpBuffer(q, " AS\n%s", result->data); + destroyPQExpBuffer(result); + + if (tbinfo->checkoption != NULL && !tbinfo->dummy_view) + appendPQExpBuffer(q, "\n WITH %s CHECK OPTION", tbinfo->checkoption); + appendPQExpBufferStr(q, ";\n"); + } + else if (tbinfo->relkind == RELKIND_PROPGRAPH) + { + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + int len; + + reltypename = "PROPERTY GRAPH"; + + if (dopt->binary_upgrade) + binary_upgrade_set_pg_class_oids(fout, q, + tbinfo->dobj.catId.oid); + + appendPQExpBuffer(query, + "SELECT pg_catalog.pg_get_propgraphdef('%u'::pg_catalog.oid) AS pgdef", + tbinfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + if (PQntuples(res) != 1) + { + if (PQntuples(res) < 1) + pg_fatal("query to obtain definition of property graph \"%s\" returned no data", + tbinfo->dobj.name); + else + pg_fatal("query to obtain definition of property graph \"%s\" returned more than one definition", + tbinfo->dobj.name); + } + + len = PQgetlength(res, 0, 0); + + if (len == 0) + pg_fatal("definition of property graph \"%s\" appears to be empty (length zero)", + tbinfo->dobj.name); + + appendPQExpBufferStr(q, PQgetvalue(res, 0, 0)); + + PQclear(res); + destroyPQExpBuffer(query); + + appendPQExpBufferStr(q, ";\n"); + } + else + { + char *partkeydef = NULL; + char *ftoptions = NULL; + char *srvname = NULL; + const char *foreign = ""; + + /* + * Set reltypename, and collect any relkind-specific data that we + * didn't fetch during getTables(). + */ + switch (tbinfo->relkind) + { + case RELKIND_PARTITIONED_TABLE: + { + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + + reltypename = "TABLE"; + + /* retrieve partition key definition */ + appendPQExpBuffer(query, + "SELECT pg_get_partkeydef('%u')", + tbinfo->dobj.catId.oid); + res = ExecuteSqlQueryForSingleRow(fout, query->data); + partkeydef = pg_strdup(PQgetvalue(res, 0, 0)); + PQclear(res); + destroyPQExpBuffer(query); + break; + } + case RELKIND_FOREIGN_TABLE: + { + PQExpBuffer query = createPQExpBuffer(); + PGresult *res; + int i_srvname; + int i_ftoptions; + + reltypename = "FOREIGN TABLE"; + + /* retrieve name of foreign server and generic options */ + appendPQExpBuffer(query, + "SELECT fs.srvname, " + "pg_catalog.array_to_string(ARRAY(" + "SELECT pg_catalog.quote_ident(option_name) || " + "' ' || pg_catalog.quote_literal(option_value) " + "FROM pg_catalog.pg_options_to_table(ftoptions) " + "ORDER BY option_name" + "), E',\n ') AS ftoptions " + "FROM pg_catalog.pg_foreign_table ft " + "JOIN pg_catalog.pg_foreign_server fs " + "ON (fs.oid = ft.ftserver) " + "WHERE ft.ftrelid = '%u'", + tbinfo->dobj.catId.oid); + res = ExecuteSqlQueryForSingleRow(fout, query->data); + i_srvname = PQfnumber(res, "srvname"); + i_ftoptions = PQfnumber(res, "ftoptions"); + srvname = pg_strdup(PQgetvalue(res, 0, i_srvname)); + ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions)); + PQclear(res); + destroyPQExpBuffer(query); + + foreign = "FOREIGN "; + break; + } + case RELKIND_MATVIEW: + reltypename = "MATERIALIZED VIEW"; + break; + default: + reltypename = "TABLE"; + break; + } + + numParents = tbinfo->numParents; + parents = tbinfo->parents; + + if (dopt->binary_upgrade) + binary_upgrade_set_pg_class_oids(fout, q, + tbinfo->dobj.catId.oid); + + /* + * PostgreSQL 18 has disabled UNLOGGED for partitioned tables, so + * ignore it when dumping if it was set in this case. + */ + appendPQExpBuffer(q, "CREATE %s%s %s", + (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED && + tbinfo->relkind != RELKIND_PARTITIONED_TABLE) ? + "UNLOGGED " : "", + reltypename, + qualrelname); + + /* + * Attach to type, if reloftype; except in case of a binary upgrade, + * we dump the table normally and attach it to the type afterward. + */ + if (OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade) + appendPQExpBuffer(q, " OF %s", + getFormattedTypeName(fout, tbinfo->reloftype, + zeroIsError)); + + if (tbinfo->relkind != RELKIND_MATVIEW) + { + /* Dump the attributes */ + actual_atts = 0; + for (j = 0; j < tbinfo->numatts; j++) + { + /* + * Normally, dump if it's locally defined in this table, and + * not dropped. But for binary upgrade, we'll dump all the + * columns, and then fix up the dropped and nonlocal cases + * below. + */ + if (shouldPrintColumn(dopt, tbinfo, j)) + { + bool print_default; + bool print_notnull; + + /* + * Default value --- suppress if to be printed separately + * or not at all. + */ + print_default = (tbinfo->attrdefs[j] != NULL && + tbinfo->attrdefs[j]->dobj.dump && + !tbinfo->attrdefs[j]->separate); + + /* + * Not Null constraint --- print it if it is locally + * defined, or if binary upgrade. (In the latter case, we + * reset conislocal below.) + */ + print_notnull = (tbinfo->notnull_constrs[j] != NULL && + (tbinfo->notnull_islocal[j] || + dopt->binary_upgrade || + tbinfo->ispartition)); + + /* + * Skip column if fully defined by reloftype, except in + * binary upgrade + */ + if (OidIsValid(tbinfo->reloftype) && + !print_default && !print_notnull && + !dopt->binary_upgrade) + continue; + + /* Format properly if not first attr */ + if (actual_atts == 0) + appendPQExpBufferStr(q, " ("); + else + appendPQExpBufferChar(q, ','); + appendPQExpBufferStr(q, "\n "); + actual_atts++; + + /* Attribute name */ + appendPQExpBufferStr(q, fmtId(tbinfo->attnames[j])); + + if (tbinfo->attisdropped[j]) + { + /* + * ALTER TABLE DROP COLUMN clears + * pg_attribute.atttypid, so we will not have gotten a + * valid type name; insert INTEGER as a stopgap. We'll + * clean things up later. + */ + appendPQExpBufferStr(q, " INTEGER /* dummy */"); + /* and skip to the next column */ + continue; + } + + /* + * Attribute type; print it except when creating a typed + * table ('OF type_name'), but in binary-upgrade mode, + * print it in that case too. + */ + if (dopt->binary_upgrade || !OidIsValid(tbinfo->reloftype)) + { + appendPQExpBuffer(q, " %s", + tbinfo->atttypnames[j]); + } + + if (print_default) + { + if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_STORED) + appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s) STORED", + tbinfo->attrdefs[j]->adef_expr); + else if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_VIRTUAL) + appendPQExpBuffer(q, " GENERATED ALWAYS AS (%s)", + tbinfo->attrdefs[j]->adef_expr); + else + appendPQExpBuffer(q, " DEFAULT %s", + tbinfo->attrdefs[j]->adef_expr); + } + + if (print_notnull) + { + if (tbinfo->notnull_constrs[j][0] == '\0') + appendPQExpBufferStr(q, " NOT NULL"); + else + appendPQExpBuffer(q, " CONSTRAINT %s NOT NULL", + fmtId(tbinfo->notnull_constrs[j])); + + if (tbinfo->notnull_noinh[j]) + appendPQExpBufferStr(q, " NO INHERIT"); + } + + /* Add collation if not default for the type */ + if (OidIsValid(tbinfo->attcollation[j])) + { + CollInfo *coll; + + coll = findCollationByOid(tbinfo->attcollation[j]); + if (coll) + appendPQExpBuffer(q, " COLLATE %s", + fmtQualifiedDumpable(coll)); + } + } + + /* + * On the other hand, if we choose not to print a column + * (likely because it is created by inheritance), but the + * column has a locally-defined not-null constraint, we need + * to dump the constraint as a standalone object. + * + * This syntax isn't SQL-conforming, but if you wanted + * standard output you wouldn't be creating non-standard + * objects to begin with. + */ + if (!shouldPrintColumn(dopt, tbinfo, j) && + !tbinfo->attisdropped[j] && + tbinfo->notnull_constrs[j] != NULL && + tbinfo->notnull_islocal[j]) + { + /* Format properly if not first attr */ + if (actual_atts == 0) + appendPQExpBufferStr(q, " ("); + else + appendPQExpBufferChar(q, ','); + appendPQExpBufferStr(q, "\n "); + actual_atts++; + + if (tbinfo->notnull_constrs[j][0] == '\0') + appendPQExpBuffer(q, "NOT NULL %s", + fmtId(tbinfo->attnames[j])); + else + appendPQExpBuffer(q, "CONSTRAINT %s NOT NULL %s", + tbinfo->notnull_constrs[j], + fmtId(tbinfo->attnames[j])); + + if (tbinfo->notnull_noinh[j]) + appendPQExpBufferStr(q, " NO INHERIT"); + } + } + + /* + * Add non-inherited CHECK constraints, if any. + * + * For partitions, we need to include check constraints even if + * they're not defined locally, because the ALTER TABLE ATTACH + * PARTITION that we'll emit later expects the constraint to be + * there. (No need to fix conislocal: ATTACH PARTITION does that) + */ + for (j = 0; j < tbinfo->ncheck; j++) + { + ConstraintInfo *constr = &(tbinfo->checkexprs[j]); + + if (constr->separate || + (!constr->conislocal && !tbinfo->ispartition)) + continue; + + if (actual_atts == 0) + appendPQExpBufferStr(q, " (\n "); + else + appendPQExpBufferStr(q, ",\n "); + + appendPQExpBuffer(q, "CONSTRAINT %s ", + fmtId(constr->dobj.name)); + appendPQExpBufferStr(q, constr->condef); + + actual_atts++; + } + + if (actual_atts) + appendPQExpBufferStr(q, "\n)"); + else if (!(OidIsValid(tbinfo->reloftype) && !dopt->binary_upgrade)) + { + /* + * No attributes? we must have a parenthesized attribute list, + * even though empty, when not using the OF TYPE syntax. + */ + appendPQExpBufferStr(q, " (\n)"); + } + + /* + * Emit the INHERITS clause (not for partitions), except in + * binary-upgrade mode. + */ + if (numParents > 0 && !tbinfo->ispartition && + !dopt->binary_upgrade) + { + appendPQExpBufferStr(q, "\nINHERITS ("); + for (k = 0; k < numParents; k++) + { + TableInfo *parentRel = parents[k]; + + if (k > 0) + appendPQExpBufferStr(q, ", "); + appendPQExpBufferStr(q, fmtQualifiedDumpable(parentRel)); + } + appendPQExpBufferChar(q, ')'); + } + + if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE) + appendPQExpBuffer(q, "\nPARTITION BY %s", partkeydef); + + if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) + appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname)); + } + + if (nonemptyReloptions(tbinfo->reloptions) || + nonemptyReloptions(tbinfo->toast_reloptions)) + { + bool addcomma = false; + + appendPQExpBufferStr(q, "\nWITH ("); + if (nonemptyReloptions(tbinfo->reloptions)) + { + addcomma = true; + appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout); + } + if (nonemptyReloptions(tbinfo->toast_reloptions)) + { + if (addcomma) + appendPQExpBufferStr(q, ", "); + appendReloptionsArrayAH(q, tbinfo->toast_reloptions, "toast.", + fout); + } + appendPQExpBufferChar(q, ')'); + } + + /* Dump generic options if any */ + if (ftoptions && ftoptions[0]) + appendPQExpBuffer(q, "\nOPTIONS (\n %s\n)", ftoptions); + + /* + * For materialized views, create the AS clause just like a view. At + * this point, we always mark the view as not populated. + */ + if (tbinfo->relkind == RELKIND_MATVIEW) + { + PQExpBuffer result; + + result = createViewAsClause(fout, tbinfo); + appendPQExpBuffer(q, " AS\n%s\n WITH NO DATA;\n", + result->data); + destroyPQExpBuffer(result); + } + else + appendPQExpBufferStr(q, ";\n"); + + /* Materialized views can depend on extensions */ + if (tbinfo->relkind == RELKIND_MATVIEW) + append_depends_on_extension(fout, q, &tbinfo->dobj, + "pg_catalog.pg_class", + "MATERIALIZED VIEW", + qualrelname); + + /* + * in binary upgrade mode, update the catalog with any missing values + * that might be present. + */ + if (dopt->binary_upgrade) + { + for (j = 0; j < tbinfo->numatts; j++) + { + if (tbinfo->attmissingval[j][0] != '\0') + { + appendPQExpBufferStr(q, "\n-- set missing value.\n"); + appendPQExpBufferStr(q, + "SELECT pg_catalog.binary_upgrade_set_missing_value("); + appendStringLiteralAH(q, qualrelname, fout); + appendPQExpBufferStr(q, "::pg_catalog.regclass,"); + appendStringLiteralAH(q, tbinfo->attnames[j], fout); + appendPQExpBufferChar(q, ','); + appendStringLiteralAH(q, tbinfo->attmissingval[j], fout); + appendPQExpBufferStr(q, ");\n\n"); + } + } + } + + /* + * To create binary-compatible heap files, we have to ensure the same + * physical column order, including dropped columns, as in the + * original. Therefore, we create dropped columns above and drop them + * here, also updating their attlen/attalign values so that the + * dropped column can be skipped properly. (We do not bother with + * restoring the original attbyval setting.) Also, inheritance + * relationships are set up by doing ALTER TABLE INHERIT rather than + * using an INHERITS clause --- the latter would possibly mess up the + * column order. That also means we have to take care about setting + * attislocal correctly, plus fix up any inherited CHECK constraints. + * Analogously, we set up typed tables using ALTER TABLE / OF here. + * + * We process foreign and partitioned tables here, even though they + * lack heap storage, because they can participate in inheritance + * relationships and we want this stuff to be consistent across the + * inheritance tree. We can exclude indexes, toast tables, sequences + * and matviews, even though they have storage, because we don't + * support altering or dropping columns in them, nor can they be part + * of inheritance trees. + */ + if (dopt->binary_upgrade && + (tbinfo->relkind == RELKIND_RELATION || + tbinfo->relkind == RELKIND_FOREIGN_TABLE || + tbinfo->relkind == RELKIND_PARTITIONED_TABLE)) + { + bool firstitem; + bool firstitem_extra; + + /* + * Drop any dropped columns. Merge the pg_attribute manipulations + * into a single SQL command, so that we don't cause repeated + * relcache flushes on the target table. Otherwise we risk O(N^2) + * relcache bloat while dropping N columns. + */ + resetPQExpBuffer(extra); + firstitem = true; + for (j = 0; j < tbinfo->numatts; j++) + { + if (tbinfo->attisdropped[j]) + { + if (firstitem) + { + appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate dropped columns.\n" + "UPDATE pg_catalog.pg_attribute\n" + "SET attlen = v.dlen, " + "attalign = v.dalign, " + "attbyval = false\n" + "FROM (VALUES "); + firstitem = false; + } + else + appendPQExpBufferStr(q, ",\n "); + appendPQExpBufferChar(q, '('); + appendStringLiteralAH(q, tbinfo->attnames[j], fout); + appendPQExpBuffer(q, ", %d, '%c')", + tbinfo->attlen[j], + tbinfo->attalign[j]); + /* The ALTER ... DROP COLUMN commands must come after */ + appendPQExpBuffer(extra, "ALTER %sTABLE ONLY %s ", + foreign, qualrelname); + appendPQExpBuffer(extra, "DROP COLUMN %s;\n", + fmtId(tbinfo->attnames[j])); + } + } + if (!firstitem) + { + appendPQExpBufferStr(q, ") v(dname, dlen, dalign)\n" + "WHERE attrelid = "); + appendStringLiteralAH(q, qualrelname, fout); + appendPQExpBufferStr(q, "::pg_catalog.regclass\n" + " AND attname = v.dname;\n"); + /* Now we can issue the actual DROP COLUMN commands */ + appendBinaryPQExpBuffer(q, extra->data, extra->len); + } + + /* + * Fix up inherited columns. As above, do the pg_attribute + * manipulations in a single SQL command. + */ + firstitem = true; + for (j = 0; j < tbinfo->numatts; j++) + { + if (!tbinfo->attisdropped[j] && + !tbinfo->attislocal[j]) + { + if (firstitem) + { + appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate inherited columns.\n"); + appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_attribute\n" + "SET attislocal = false\n" + "WHERE attrelid = "); + appendStringLiteralAH(q, qualrelname, fout); + appendPQExpBufferStr(q, "::pg_catalog.regclass\n" + " AND attname IN ("); + firstitem = false; + } + else + appendPQExpBufferStr(q, ", "); + appendStringLiteralAH(q, tbinfo->attnames[j], fout); + } + } + if (!firstitem) + appendPQExpBufferStr(q, ");\n"); + + /* + * Fix up not-null constraints that come from inheritance. As + * above, do the pg_constraint manipulations in a single SQL + * command. (Actually, two in special cases, if we're doing an + * upgrade from < 18). + */ + firstitem = true; + firstitem_extra = true; + resetPQExpBuffer(extra); + for (j = 0; j < tbinfo->numatts; j++) + { + /* + * If a not-null constraint comes from inheritance, reset + * conislocal. The inhcount is fixed by ALTER TABLE INHERIT, + * below. Special hack: in versions < 18, columns with no + * local definition need their constraint to be matched by + * column number in conkeys instead of by constraint name, + * because the latter is not available. (We distinguish the + * case because the constraint name is the empty string.) + */ + if (tbinfo->notnull_constrs[j] != NULL && + !tbinfo->notnull_islocal[j]) + { + if (tbinfo->notnull_constrs[j][0] != '\0') + { + if (firstitem) + { + appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n" + "SET conislocal = false\n" + "WHERE contype = 'n' AND conrelid = "); + appendStringLiteralAH(q, qualrelname, fout); + appendPQExpBufferStr(q, "::pg_catalog.regclass AND\n" + "conname IN ("); + firstitem = false; + } + else + appendPQExpBufferStr(q, ", "); + appendStringLiteralAH(q, tbinfo->notnull_constrs[j], fout); + } + else + { + if (firstitem_extra) + { + appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n" + "SET conislocal = false\n" + "WHERE contype = 'n' AND conrelid = "); + appendStringLiteralAH(extra, qualrelname, fout); + appendPQExpBufferStr(extra, "::pg_catalog.regclass AND\n" + "conkey IN ("); + firstitem_extra = false; + } + else + appendPQExpBufferStr(extra, ", "); + appendPQExpBuffer(extra, "'{%d}'", j + 1); + } + } + } + if (!firstitem) + appendPQExpBufferStr(q, ");\n"); + if (!firstitem_extra) + appendPQExpBufferStr(extra, ");\n"); + + if (extra->len > 0) + appendBinaryPQExpBuffer(q, extra->data, extra->len); + + /* + * Add inherited CHECK constraints, if any. + * + * For partitions, they were already dumped, and conislocal + * doesn't need fixing. + * + * As above, issue only one direct manipulation of pg_constraint. + * Although it is tempting to merge the ALTER ADD CONSTRAINT + * commands into one as well, refrain for now due to concern about + * possible backend memory bloat if there are many such + * constraints. + */ + resetPQExpBuffer(extra); + firstitem = true; + for (k = 0; k < tbinfo->ncheck; k++) + { + ConstraintInfo *constr = &(tbinfo->checkexprs[k]); + + if (constr->separate || constr->conislocal || tbinfo->ispartition) + continue; + + if (firstitem) + appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraints.\n"); + appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ADD CONSTRAINT %s %s;\n", + foreign, qualrelname, + fmtId(constr->dobj.name), + constr->condef); + /* Update pg_constraint after all the ALTER TABLEs */ + if (firstitem) + { + appendPQExpBufferStr(extra, "UPDATE pg_catalog.pg_constraint\n" + "SET conislocal = false\n" + "WHERE contype = 'c' AND conrelid = "); + appendStringLiteralAH(extra, qualrelname, fout); + appendPQExpBufferStr(extra, "::pg_catalog.regclass\n"); + appendPQExpBufferStr(extra, " AND conname IN ("); + firstitem = false; + } + else + appendPQExpBufferStr(extra, ", "); + appendStringLiteralAH(extra, constr->dobj.name, fout); + } + if (!firstitem) + { + appendPQExpBufferStr(extra, ");\n"); + appendBinaryPQExpBuffer(q, extra->data, extra->len); + } + + if (numParents > 0 && !tbinfo->ispartition) + { + appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance this way.\n"); + for (k = 0; k < numParents; k++) + { + TableInfo *parentRel = parents[k]; + + appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s INHERIT %s;\n", foreign, + qualrelname, + fmtQualifiedDumpable(parentRel)); + } + } + + if (OidIsValid(tbinfo->reloftype)) + { + appendPQExpBufferStr(q, "\n-- For binary upgrade, set up typed tables this way.\n"); + appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n", + qualrelname, + getFormattedTypeName(fout, tbinfo->reloftype, + zeroIsError)); + } + } + + /* Some of the binary compatibility is needed for pgactive as well. */ + if (dopt->pgactive_init_node && tbinfo->relkind == RELKIND_RELATION) + { + for (j = 0; j < tbinfo->numatts; j++) + { + if (!tbinfo->attisdropped[j]) + continue; + + appendPQExpBufferStr(q, "\n-- For pgactive init, recreate dropped column.\n"); + appendPQExpBuffer(q, "UPDATE pg_catalog.pg_attribute\n" + "SET attlen = %d, " + "attalign = '%c', attbyval = false\n" + "WHERE attname = ", + tbinfo->attlen[j], + tbinfo->attalign[j]); + appendStringLiteralAH(q, tbinfo->attnames[j], fout); + appendPQExpBufferStr(q, "\n AND attrelid = "); + appendStringLiteralAH(q, qualrelname, fout); + appendPQExpBufferStr(q, "::pg_catalog.regclass;\n"); + + if (tbinfo->relkind == RELKIND_RELATION || + tbinfo->relkind == RELKIND_PARTITIONED_TABLE) + appendPQExpBuffer(q, "ALTER TABLE ONLY %s ", qualrelname); + else + appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ", qualrelname); + + appendPQExpBuffer(q, "DROP COLUMN %s;\n", fmtId(tbinfo->attnames[j])); + } + } + /* + * In binary_upgrade mode, arrange to restore the old relfrozenxid and + * relminmxid of all vacuumable relations. (While vacuum.c processes + * TOAST tables semi-independently, here we see them only as children + * of other relations; so this "if" lacks RELKIND_TOASTVALUE, and the + * child toast table is handled below.) + */ + if (dopt->binary_upgrade && + (tbinfo->relkind == RELKIND_RELATION || + tbinfo->relkind == RELKIND_MATVIEW)) + { + appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n"); + appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n" + "SET relfrozenxid = '%u', relminmxid = '%u'\n" + "WHERE oid = ", + tbinfo->frozenxid, tbinfo->minmxid); + appendStringLiteralAH(q, qualrelname, fout); + appendPQExpBufferStr(q, "::pg_catalog.regclass;\n"); + + if (tbinfo->toast_oid) + { + /* + * The toast table will have the same OID at restore, so we + * can safely target it by OID. + */ + appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n"); + appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n" + "SET relfrozenxid = '%u', relminmxid = '%u'\n" + "WHERE oid = '%u';\n", + tbinfo->toast_frozenxid, + tbinfo->toast_minmxid, tbinfo->toast_oid); + } + } + + /* + * In binary_upgrade mode, restore matviews' populated status by + * poking pg_class directly. This is pretty ugly, but we can't use + * REFRESH MATERIALIZED VIEW since it's possible that some underlying + * matview is not populated even though this matview is; in any case, + * we want to transfer the matview's heap storage, not run REFRESH. + */ + if (dopt->binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW && + tbinfo->relispopulated) + { + appendPQExpBufferStr(q, "\n-- For binary upgrade, mark materialized view as populated\n"); + appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_class\n" + "SET relispopulated = 't'\n" + "WHERE oid = "); + appendStringLiteralAH(q, qualrelname, fout); + appendPQExpBufferStr(q, "::pg_catalog.regclass;\n"); + } + + /* + * Dump additional per-column properties that we can't handle in the + * main CREATE TABLE command. + */ + for (j = 0; j < tbinfo->numatts; j++) + { + /* None of this applies to dropped columns */ + if (tbinfo->attisdropped[j]) + continue; + + /* + * Dump per-column statistics information. We only issue an ALTER + * TABLE statement if the attstattarget entry for this column is + * not the default value. + */ + if (tbinfo->attstattarget[j] >= 0) + appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STATISTICS %d;\n", + foreign, qualrelname, + fmtId(tbinfo->attnames[j]), + tbinfo->attstattarget[j]); + + /* + * Dump per-column storage information. The statement is only + * dumped if the storage has been changed from the type's default. + */ + if (tbinfo->attstorage[j] != tbinfo->typstorage[j]) + { + switch (tbinfo->attstorage[j]) + { + case TYPSTORAGE_PLAIN: + storage = "PLAIN"; + break; + case TYPSTORAGE_EXTERNAL: + storage = "EXTERNAL"; + break; + case TYPSTORAGE_EXTENDED: + storage = "EXTENDED"; + break; + case TYPSTORAGE_MAIN: + storage = "MAIN"; + break; + default: + storage = NULL; + } + + /* + * Only dump the statement if it's a storage type we recognize + */ + if (storage != NULL) + appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET STORAGE %s;\n", + foreign, qualrelname, + fmtId(tbinfo->attnames[j]), + storage); + } + + /* + * Dump per-column compression, if it's been set. + */ + if (!dopt->no_toast_compression) + { + const char *cmname; + + switch (tbinfo->attcompression[j]) + { + case 'p': + cmname = "pglz"; + break; + case 'l': + cmname = "lz4"; + break; + default: + cmname = NULL; + break; + } + + if (cmname != NULL) + appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET COMPRESSION %s;\n", + foreign, qualrelname, + fmtId(tbinfo->attnames[j]), + cmname); + } + + /* + * Dump per-column attributes. + */ + if (tbinfo->attoptions[j][0] != '\0') + appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET (%s);\n", + foreign, qualrelname, + fmtId(tbinfo->attnames[j]), + tbinfo->attoptions[j]); + + /* + * Dump per-column fdw options. + */ + if (tbinfo->relkind == RELKIND_FOREIGN_TABLE && + tbinfo->attfdwoptions[j][0] != '\0') + appendPQExpBuffer(q, + "ALTER FOREIGN TABLE ONLY %s ALTER COLUMN %s OPTIONS (\n" + " %s\n" + ");\n", + qualrelname, + fmtId(tbinfo->attnames[j]), + tbinfo->attfdwoptions[j]); + } /* end loop over columns */ + + free(partkeydef); + free(ftoptions); + free(srvname); + } + + /* + * dump properties we only have ALTER TABLE syntax for + */ + if ((tbinfo->relkind == RELKIND_RELATION || + tbinfo->relkind == RELKIND_PARTITIONED_TABLE || + tbinfo->relkind == RELKIND_MATVIEW) && + tbinfo->relreplident != REPLICA_IDENTITY_DEFAULT) + { + if (tbinfo->relreplident == REPLICA_IDENTITY_INDEX) + { + /* nothing to do, will be set when the index is dumped */ + } + else if (tbinfo->relreplident == REPLICA_IDENTITY_NOTHING) + { + appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY NOTHING;\n", + qualrelname); + } + else if (tbinfo->relreplident == REPLICA_IDENTITY_FULL) + { + appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY FULL;\n", + qualrelname); + } + } + + if (tbinfo->forcerowsec) + appendPQExpBuffer(q, "\nALTER TABLE ONLY %s FORCE ROW LEVEL SECURITY;\n", + qualrelname); + + appendPQExpBuffer(delq, "DROP %s %s;\n", reltypename, qualrelname); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(q, &tbinfo->dobj, + reltypename, qrelname, + tbinfo->dobj.namespace->dobj.name); + + if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + { + char *tablespace = NULL; + char *tableam = NULL; + + /* + * _selectTablespace() relies on tablespace-enabled objects in the + * default tablespace to have a tablespace of "" (empty string) versus + * non-tablespace-enabled objects to have a tablespace of NULL. + * getTables() sets tbinfo->reltablespace to "" for the default + * tablespace (not NULL). + */ + if (RELKIND_HAS_TABLESPACE(tbinfo->relkind)) + tablespace = tbinfo->reltablespace; + + if (RELKIND_HAS_TABLE_AM(tbinfo->relkind) || + tbinfo->relkind == RELKIND_PARTITIONED_TABLE) + tableam = tbinfo->amname; + + ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tbinfo->dobj.name, + .namespace = tbinfo->dobj.namespace->dobj.name, + .tablespace = tablespace, + .tableam = tableam, + .relkind = tbinfo->relkind, + .owner = tbinfo->rolname, + .description = reltypename, + .section = tbinfo->postponed_def ? + SECTION_POST_DATA : SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + } + + /* Dump Table Comments */ + if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpTableComment(fout, tbinfo, reltypename); + + /* Dump Table Security Labels */ + if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpTableSecLabel(fout, tbinfo, reltypename); + + /* + * Dump comments for not-null constraints that aren't to be dumped + * separately (those are processed by collectComments/dumpComment). + */ + if (!fout->dopt->no_comments && dopt->dumpSchema && + fout->remoteVersion >= 180000) + { + PQExpBuffer comment = NULL; + PQExpBuffer tag = NULL; + + for (j = 0; j < tbinfo->numatts; j++) + { + if (tbinfo->notnull_constrs[j] != NULL && + tbinfo->notnull_comment[j] != NULL) + { + if (comment == NULL) + { + comment = createPQExpBuffer(); + tag = createPQExpBuffer(); + } + else + { + resetPQExpBuffer(comment); + resetPQExpBuffer(tag); + } + + appendPQExpBuffer(comment, "COMMENT ON CONSTRAINT %s ON %s IS ", + fmtId(tbinfo->notnull_constrs[j]), qualrelname); + appendStringLiteralAH(comment, tbinfo->notnull_comment[j], fout); + appendPQExpBufferStr(comment, ";\n"); + + appendPQExpBuffer(tag, "CONSTRAINT %s ON %s", + fmtId(tbinfo->notnull_constrs[j]), qrelname); + + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = tag->data, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "COMMENT", + .section = SECTION_NONE, + .createStmt = comment->data, + .deps = &(tbinfo->dobj.dumpId), + .nDeps = 1)); + } + } + + destroyPQExpBuffer(comment); + destroyPQExpBuffer(tag); + } + + /* Dump comments on inlined table constraints */ + for (j = 0; j < tbinfo->ncheck; j++) + { + ConstraintInfo *constr = &(tbinfo->checkexprs[j]); + + if (constr->separate || !constr->conislocal) + continue; + + if (constr->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpTableConstraintComment(fout, constr); + } + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(extra); + free(qrelname); + free(qualrelname); +} + +/* + * dumpTableAttach + * write to fout the commands to attach a child partition + * + * Child partitions are always made by creating them separately + * and then using ATTACH PARTITION, rather than using + * CREATE TABLE ... PARTITION OF. This is important for preserving + * any possible discrepancy in column layout, to allow assigning the + * correct tablespace if different, and so that it's possible to restore + * a partition without restoring its parent. (You'll get an error from + * the ATTACH PARTITION command, but that can be ignored, or skipped + * using "pg_restore -L" if you prefer.) The last point motivates + * treating ATTACH PARTITION as a completely separate ArchiveEntry + * rather than emitting it within the child partition's ArchiveEntry. + */ +static void +dumpTableAttach(Archive *fout, const TableAttachInfo *attachinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PGresult *res; + char *partbound; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + + if (!fout->is_prepared[PREPQUERY_DUMPTABLEATTACH]) + { + /* Set up query for partbound details */ + appendPQExpBufferStr(q, + "PREPARE dumpTableAttach(pg_catalog.oid) AS\n"); + + appendPQExpBufferStr(q, + "SELECT pg_get_expr(c.relpartbound, c.oid) " + "FROM pg_class c " + "WHERE c.oid = $1"); + + ExecuteSqlStatement(fout, q->data); + + fout->is_prepared[PREPQUERY_DUMPTABLEATTACH] = true; + } + + printfPQExpBuffer(q, + "EXECUTE dumpTableAttach('%u')", + attachinfo->partitionTbl->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, q->data); + partbound = PQgetvalue(res, 0, 0); + + /* Perform ALTER TABLE on the parent */ + printfPQExpBuffer(q, + "ALTER TABLE ONLY %s ", + fmtQualifiedDumpable(attachinfo->parentTbl)); + appendPQExpBuffer(q, + "ATTACH PARTITION %s %s;\n", + fmtQualifiedDumpable(attachinfo->partitionTbl), + partbound); + + /* + * There is no point in creating a drop query as the drop is done by table + * drop. (If you think to change this, see also _printTocEntry().) + * Although this object doesn't really have ownership as such, set the + * owner field anyway to ensure that the command is run by the correct + * role at restore time. + */ + ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = attachinfo->dobj.name, + .namespace = attachinfo->dobj.namespace->dobj.name, + .owner = attachinfo->partitionTbl->rolname, + .description = "TABLE ATTACH", + .section = SECTION_PRE_DATA, + .createStmt = q->data)); + + PQclear(res); + destroyPQExpBuffer(q); +} + +/* + * dumpAttrDef --- dump an attribute's default-value declaration + */ +static void +dumpAttrDef(Archive *fout, const AttrDefInfo *adinfo) +{ + DumpOptions *dopt = fout->dopt; + TableInfo *tbinfo = adinfo->adtable; + int adnum = adinfo->adnum; + PQExpBuffer q; + PQExpBuffer delq; + char *qualrelname; + char *tag; + char *foreign; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + /* Skip if not "separate"; it was dumped in the table's definition */ + if (!adinfo->separate) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo)); + + foreign = tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : ""; + + appendPQExpBuffer(q, + "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET DEFAULT %s;\n", + foreign, qualrelname, fmtId(tbinfo->attnames[adnum - 1]), + adinfo->adef_expr); + + appendPQExpBuffer(delq, "ALTER %sTABLE %s ALTER COLUMN %s DROP DEFAULT;\n", + foreign, qualrelname, + fmtId(tbinfo->attnames[adnum - 1])); + + tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]); + + if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "DEFAULT", + .section = SECTION_PRE_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + free(tag); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + free(qualrelname); +} + +/* + * getAttrName: extract the correct name for an attribute + * + * The array tblInfo->attnames[] only provides names of user attributes; + * if a system attribute number is supplied, we have to fake it. + * We also do a little bit of bounds checking for safety's sake. + */ +static const char * +getAttrName(int attrnum, const TableInfo *tblInfo) +{ + if (attrnum > 0 && attrnum <= tblInfo->numatts) + return tblInfo->attnames[attrnum - 1]; + switch (attrnum) + { + case SelfItemPointerAttributeNumber: + return "ctid"; + case MinTransactionIdAttributeNumber: + return "xmin"; + case MinCommandIdAttributeNumber: + return "cmin"; + case MaxTransactionIdAttributeNumber: + return "xmax"; + case MaxCommandIdAttributeNumber: + return "cmax"; + case TableOidAttributeNumber: + return "tableoid"; + } + pg_fatal("invalid column number %d for table \"%s\"", + attrnum, tblInfo->dobj.name); + return NULL; /* keep compiler quiet */ +} + +/* + * dumpIndex + * write out to fout a user-defined index + */ +static void +dumpIndex(Archive *fout, const IndxInfo *indxinfo) +{ + DumpOptions *dopt = fout->dopt; + TableInfo *tbinfo = indxinfo->indextable; + bool is_constraint = (indxinfo->indexconstraint != 0); + PQExpBuffer q; + PQExpBuffer delq; + char *qindxname; + char *qqindxname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + qindxname = pg_strdup(fmtId(indxinfo->dobj.name)); + qqindxname = pg_strdup(fmtQualifiedDumpable(indxinfo)); + + /* + * If there's an associated constraint, don't dump the index per se, but + * do dump any comment for it. (This is safe because dependency ordering + * will have ensured the constraint is emitted first.) Note that the + * emitted comment has to be shown as depending on the constraint, not the + * index, in such cases. + */ + if (!is_constraint) + { + char *indstatcols = indxinfo->indstatcols; + char *indstatvals = indxinfo->indstatvals; + char **indstatcolsarray = NULL; + char **indstatvalsarray = NULL; + int nstatcols = 0; + int nstatvals = 0; + + if (dopt->binary_upgrade) + binary_upgrade_set_pg_class_oids(fout, q, + indxinfo->dobj.catId.oid); + + /* Plain secondary index */ + appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef); + + /* + * Append ALTER TABLE commands as needed to set properties that we + * only have ALTER TABLE syntax for. Keep this in sync with the + * similar code in dumpConstraint! + */ + + /* If the index is clustered, we need to record that. */ + if (indxinfo->indisclustered) + { + appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER", + fmtQualifiedDumpable(tbinfo)); + /* index name is not qualified in this syntax */ + appendPQExpBuffer(q, " ON %s;\n", + qindxname); + } + + /* + * If the index has any statistics on some of its columns, generate + * the associated ALTER INDEX queries. + */ + if (strlen(indstatcols) != 0 || strlen(indstatvals) != 0) + { + int j; + + if (!parsePGArray(indstatcols, &indstatcolsarray, &nstatcols)) + pg_fatal("could not parse index statistic columns"); + if (!parsePGArray(indstatvals, &indstatvalsarray, &nstatvals)) + pg_fatal("could not parse index statistic values"); + if (nstatcols != nstatvals) + pg_fatal("mismatched number of columns and values for index statistics"); + + for (j = 0; j < nstatcols; j++) + { + appendPQExpBuffer(q, "ALTER INDEX %s ", qqindxname); + + /* + * Note that this is a column number, so no quotes should be + * used. + */ + appendPQExpBuffer(q, "ALTER COLUMN %s ", + indstatcolsarray[j]); + appendPQExpBuffer(q, "SET STATISTICS %s;\n", + indstatvalsarray[j]); + } + } + + /* Indexes can depend on extensions */ + append_depends_on_extension(fout, q, &indxinfo->dobj, + "pg_catalog.pg_class", + "INDEX", qqindxname); + + /* If the index defines identity, we need to record that. */ + if (indxinfo->indisreplident) + { + appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING", + fmtQualifiedDumpable(tbinfo)); + /* index name is not qualified in this syntax */ + appendPQExpBuffer(q, " INDEX %s;\n", + qindxname); + } + + /* + * If this index is a member of a partitioned index, the backend will + * not allow us to drop it separately, so don't try. It will go away + * automatically when we drop either the index's table or the + * partitioned index. (If, in a selective restore with --clean, we + * drop neither of those, then this index will not be dropped either. + * But that's fine, and even if you think it's not, the backend won't + * let us do differently.) + */ + if (indxinfo->parentidx == 0) + appendPQExpBuffer(delq, "DROP INDEX %s;\n", qqindxname); + + if (indxinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = indxinfo->dobj.name, + .namespace = tbinfo->dobj.namespace->dobj.name, + .tablespace = indxinfo->tablespace, + .owner = tbinfo->rolname, + .description = "INDEX", + .section = SECTION_POST_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + free(indstatcolsarray); + free(indstatvalsarray); + } + + /* Dump Index Comments */ + if (indxinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "INDEX", qindxname, + tbinfo->dobj.namespace->dobj.name, + tbinfo->rolname, + indxinfo->dobj.catId, 0, + is_constraint ? indxinfo->indexconstraint : + indxinfo->dobj.dumpId); + + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + free(qindxname); + free(qqindxname); +} + +/* + * dumpIndexAttach + * write out to fout a partitioned-index attachment clause + */ +static void +dumpIndexAttach(Archive *fout, const IndexAttachInfo *attachinfo) +{ + /* Do nothing if not dumping schema */ + if (!fout->dopt->dumpSchema) + return; + + if (attachinfo->partitionIdx->dobj.dump & DUMP_COMPONENT_DEFINITION) + { + PQExpBuffer q = createPQExpBuffer(); + + appendPQExpBuffer(q, "ALTER INDEX %s ", + fmtQualifiedDumpable(attachinfo->parentIdx)); + appendPQExpBuffer(q, "ATTACH PARTITION %s;\n", + fmtQualifiedDumpable(attachinfo->partitionIdx)); + + /* + * There is no need for a dropStmt since the drop is done implicitly + * when we drop either the index's table or the partitioned index. + * Moreover, since there's no ALTER INDEX DETACH PARTITION command, + * there's no way to do it anyway. (If you think to change this, + * consider also what to do with --if-exists.) + * + * Although this object doesn't really have ownership as such, set the + * owner field anyway to ensure that the command is run by the correct + * role at restore time. + */ + ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = attachinfo->dobj.name, + .namespace = attachinfo->dobj.namespace->dobj.name, + .owner = attachinfo->parentIdx->indextable->rolname, + .description = "INDEX ATTACH", + .section = SECTION_POST_DATA, + .createStmt = q->data)); + + destroyPQExpBuffer(q); + } +} + +/* + * dumpStatisticsExt + * write out to fout an extended statistics object + */ +static void +dumpStatisticsExt(Archive *fout, const StatsExtInfo *statsextinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer q; + PQExpBuffer delq; + PQExpBuffer query; + char *qstatsextname; + PGresult *res; + char *stxdef; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + query = createPQExpBuffer(); + + qstatsextname = pg_strdup(fmtId(statsextinfo->dobj.name)); + + appendPQExpBuffer(query, "SELECT " + "pg_catalog.pg_get_statisticsobjdef('%u'::pg_catalog.oid)", + statsextinfo->dobj.catId.oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + stxdef = PQgetvalue(res, 0, 0); + + /* Result of pg_get_statisticsobjdef is complete except for semicolon */ + appendPQExpBuffer(q, "%s;\n", stxdef); + + /* + * We only issue an ALTER STATISTICS statement if the stxstattarget entry + * for this statistics object is not the default value. + */ + if (statsextinfo->stattarget >= 0) + { + appendPQExpBuffer(q, "ALTER STATISTICS %s ", + fmtQualifiedDumpable(statsextinfo)); + appendPQExpBuffer(q, "SET STATISTICS %d;\n", + statsextinfo->stattarget); + } + + appendPQExpBuffer(delq, "DROP STATISTICS %s;\n", + fmtQualifiedDumpable(statsextinfo)); + + if (statsextinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, statsextinfo->dobj.catId, + statsextinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = statsextinfo->dobj.name, + .namespace = statsextinfo->dobj.namespace->dobj.name, + .owner = statsextinfo->rolname, + .description = "STATISTICS", + .section = SECTION_POST_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + /* Dump Statistics Comments */ + if (statsextinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "STATISTICS", qstatsextname, + statsextinfo->dobj.namespace->dobj.name, + statsextinfo->rolname, + statsextinfo->dobj.catId, 0, + statsextinfo->dobj.dumpId); + + PQclear(res); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); + destroyPQExpBuffer(query); + free(qstatsextname); +} + +/* + * dumpStatisticsExtStats + * write out to fout the stats for an extended statistics object + */ +static void +dumpStatisticsExtStats(Archive *fout, const StatsExtInfo *statsextinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PGresult *res; + int nstats; + + /* Do nothing if not dumping statistics */ + if (!dopt->dumpStatistics) + return; + + if (!fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS]) + { + PQExpBuffer pq = createPQExpBuffer(); + + /*--------- + * Set up query for details about extended statistics objects. + * + * The query depends on the backend version: + * - In v19 and newer versions, query directly the pg_stats_ext* + * catalogs. + * - In v18 and older versions, ndistinct and dependencies have a + * different format that needs translation. + * - In v14 and older versions, inherited does not exist. + * - In v11 and older versions, there is no pg_stats_ext, hence + * the logic joins pg_statistic_ext and pg_namespace. + *--------- + */ + + appendPQExpBufferStr(pq, + "PREPARE getExtStatsStats(pg_catalog.name, pg_catalog.name) AS\n" + "SELECT "); + + /* + * Versions 15 and newer have inherited stats. + * + * Create this column in all versions because we need to order by it + * later. + */ + if (fout->remoteVersion >= 150000) + appendPQExpBufferStr(pq, "e.inherited, "); + else + appendPQExpBufferStr(pq, "false AS inherited, "); + + /*-------- + * The ndistinct and dependencies formats changed in v19, so + * everything before that needs to be translated. + * + * The ndistinct translation converts this kind of data: + * {"3, 4": 11, "3, 6": 11, "4, 6": 11, "3, 4, 6": 11} + * + * to this: + * [ {"attributes": [3,4], "ndistinct": 11}, + * {"attributes": [3,6], "ndistinct": 11}, + * {"attributes": [4,6], "ndistinct": 11}, + * {"attributes": [3,4,6], "ndistinct": 11} ] + * + * The dependencies translation converts this kind of data: + * {"3 => 4": 1.000000, "3 => 6": 1.000000, + * "4 => 6": 1.000000, "3, 4 => 6": 1.000000, + * "3, 6 => 4": 1.000000} + * + * to this: + * [ {"attributes": [3], "dependency": 4, "degree": 1.000000}, + * {"attributes": [3], "dependency": 6, "degree": 1.000000}, + * {"attributes": [4], "dependency": 6, "degree": 1.000000}, + * {"attributes": [3,4], "dependency": 6, "degree": 1.000000}, + * {"attributes": [3,6], "dependency": 4, "degree": 1.000000} ] + *-------- + */ + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(pq, "e.n_distinct, e.dependencies, "); + else + appendPQExpBufferStr(pq, + "( " + "SELECT json_agg( " + " json_build_object( " + " '" PG_NDISTINCT_KEY_ATTRIBUTES "', " + " string_to_array(kv.key, ', ')::integer[], " + " '" PG_NDISTINCT_KEY_NDISTINCT "', " + " kv.value::bigint )) " + "FROM json_each_text(e.n_distinct::text::json) AS kv" + ") AS n_distinct, " + "( " + "SELECT json_agg( " + " json_build_object( " + " '" PG_DEPENDENCIES_KEY_ATTRIBUTES "', " + " string_to_array( " + " split_part(kv.key, ' => ', 1), " + " ', ')::integer[], " + " '" PG_DEPENDENCIES_KEY_DEPENDENCY "', " + " split_part(kv.key, ' => ', 2)::integer, " + " '" PG_DEPENDENCIES_KEY_DEGREE "', " + " kv.value::double precision )) " + "FROM json_each_text(e.dependencies::text::json) AS kv " + ") AS dependencies, "); + + /* MCV was introduced v13 */ + if (fout->remoteVersion >= 130000) + appendPQExpBufferStr(pq, + "e.most_common_vals, e.most_common_freqs, " + "e.most_common_base_freqs, "); + else + appendPQExpBufferStr(pq, + "NULL AS most_common_vals, NULL AS most_common_freqs, " + "NULL AS most_common_base_freqs, "); + + /* Expressions were introduced in v14 */ + if (fout->remoteVersion >= 140000) + { + /* + * There is no ordering column in pg_stats_ext_exprs. However, we + * can rely on the unnesting of pg_statistic_ext_data.stxdexpr to + * maintain the desired order of expression elements. + */ + appendPQExpBufferStr(pq, + "( " + "SELECT jsonb_pretty(jsonb_agg(" + "nullif(j.obj, '{}'::jsonb))) " + "FROM pg_stats_ext_exprs AS ee " + "CROSS JOIN LATERAL jsonb_strip_nulls(" + " jsonb_build_object( " + " 'null_frac', ee.null_frac::text, " + " 'avg_width', ee.avg_width::text, " + " 'n_distinct', ee.n_distinct::text, " + " 'most_common_vals', ee.most_common_vals::text, " + " 'most_common_freqs', ee.most_common_freqs::text, " + " 'histogram_bounds', ee.histogram_bounds::text, " + " 'correlation', ee.correlation::text, " + " 'most_common_elems', ee.most_common_elems::text, " + " 'most_common_elem_freqs', ee.most_common_elem_freqs::text, " + " 'elem_count_histogram', ee.elem_count_histogram::text"); + + /* These three have been added to pg_stats_ext_exprs in v19. */ + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(pq, + ", " + " 'range_length_histogram', ee.range_length_histogram::text, " + " 'range_empty_frac', ee.range_empty_frac::text, " + " 'range_bounds_histogram', ee.range_bounds_histogram::text"); + + appendPQExpBufferStr(pq, + " )) AS j(obj)" + "WHERE ee.statistics_schemaname = $1 " + "AND ee.statistics_name = $2 "); + /* Inherited expressions introduced in v15 */ + if (fout->remoteVersion >= 150000) + appendPQExpBufferStr(pq, "AND ee.inherited = e.inherited"); + + appendPQExpBufferStr(pq, ") AS exprs "); + } + else + appendPQExpBufferStr(pq, "NULL AS exprs "); + + /* pg_stats_ext introduced in v12 */ + if (fout->remoteVersion >= 120000) + appendPQExpBufferStr(pq, + "FROM pg_catalog.pg_stats_ext AS e " + "WHERE e.statistics_schemaname = $1 " + "AND e.statistics_name = $2 "); + else + appendPQExpBufferStr(pq, + "FROM ( " + "SELECT s.stxndistinct AS n_distinct, " + " s.stxdependencies AS dependencies " + "FROM pg_catalog.pg_statistic_ext AS s " + "JOIN pg_catalog.pg_namespace AS n " + "ON n.oid = s.stxnamespace " + "WHERE n.nspname = $1 " + "AND s.stxname = $2 " + ") AS e "); + + /* we always have an inherited column, but it may be a constant */ + appendPQExpBufferStr(pq, "ORDER BY inherited"); + + ExecuteSqlStatement(fout, pq->data); + + fout->is_prepared[PREPQUERY_DUMPEXTSTATSOBJSTATS] = true; + + destroyPQExpBuffer(pq); + } + + query = createPQExpBuffer(); + + appendPQExpBufferStr(query, "EXECUTE getExtStatsStats("); + appendStringLiteralAH(query, statsextinfo->dobj.namespace->dobj.name, fout); + appendPQExpBufferStr(query, "::pg_catalog.name, "); + appendStringLiteralAH(query, statsextinfo->dobj.name, fout); + appendPQExpBufferStr(query, "::pg_catalog.name)"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + destroyPQExpBuffer(query); + + nstats = PQntuples(res); + + if (nstats > 0) + { + PQExpBuffer out = createPQExpBuffer(); + + int i_inherited = PQfnumber(res, "inherited"); + int i_ndistinct = PQfnumber(res, "n_distinct"); + int i_dependencies = PQfnumber(res, "dependencies"); + int i_mcv = PQfnumber(res, "most_common_vals"); + int i_mcf = PQfnumber(res, "most_common_freqs"); + int i_mcbf = PQfnumber(res, "most_common_base_freqs"); + int i_exprs = PQfnumber(res, "exprs"); + + for (int i = 0; i < nstats; i++) + { + TableInfo *tbinfo = statsextinfo->stattable; + + if (PQgetisnull(res, i, i_inherited)) + pg_fatal("inherited cannot be NULL"); + + appendPQExpBufferStr(out, + "SELECT * FROM pg_catalog.pg_restore_extended_stats(\n"); + appendPQExpBuffer(out, "\t'version', '%d'::integer,\n", + fout->remoteVersion); + + /* Relation information */ + appendPQExpBufferStr(out, "\t'schemaname', "); + appendStringLiteralAH(out, tbinfo->dobj.namespace->dobj.name, fout); + appendPQExpBufferStr(out, ",\n\t'relname', "); + appendStringLiteralAH(out, tbinfo->dobj.name, fout); + + /* Extended statistics information */ + appendPQExpBufferStr(out, ",\n\t'statistics_schemaname', "); + appendStringLiteralAH(out, statsextinfo->dobj.namespace->dobj.name, fout); + appendPQExpBufferStr(out, ",\n\t'statistics_name', "); + appendStringLiteralAH(out, statsextinfo->dobj.name, fout); + appendNamedArgument(out, fout, "inherited", "boolean", + PQgetvalue(res, i, i_inherited)); + + if (!PQgetisnull(res, i, i_ndistinct)) + appendNamedArgument(out, fout, "n_distinct", "pg_ndistinct", + PQgetvalue(res, i, i_ndistinct)); + + if (!PQgetisnull(res, i, i_dependencies)) + appendNamedArgument(out, fout, "dependencies", "pg_dependencies", + PQgetvalue(res, i, i_dependencies)); + + if (!PQgetisnull(res, i, i_mcv)) + appendNamedArgument(out, fout, "most_common_vals", "text[]", + PQgetvalue(res, i, i_mcv)); + + if (!PQgetisnull(res, i, i_mcf)) + appendNamedArgument(out, fout, "most_common_freqs", "double precision[]", + PQgetvalue(res, i, i_mcf)); + + if (!PQgetisnull(res, i, i_mcbf)) + appendNamedArgument(out, fout, "most_common_base_freqs", "double precision[]", + PQgetvalue(res, i, i_mcbf)); + + if (!PQgetisnull(res, i, i_exprs)) + appendNamedArgument(out, fout, "exprs", "jsonb", + PQgetvalue(res, i, i_exprs)); + + appendPQExpBufferStr(out, "\n);\n"); + } + + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = statsextinfo->dobj.name, + .namespace = statsextinfo->dobj.namespace->dobj.name, + .owner = statsextinfo->rolname, + .description = "EXTENDED STATISTICS DATA", + .section = SECTION_POST_DATA, + .createStmt = out->data, + .deps = &statsextinfo->dobj.dumpId, + .nDeps = 1)); + destroyPQExpBuffer(out); + } + PQclear(res); +} + +/* + * dumpConstraint + * write out to fout a user-defined constraint + */ +static void +dumpConstraint(Archive *fout, const ConstraintInfo *coninfo) +{ + DumpOptions *dopt = fout->dopt; + TableInfo *tbinfo = coninfo->contable; + PQExpBuffer q; + PQExpBuffer delq; + char *tag = NULL; + char *foreign; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + q = createPQExpBuffer(); + delq = createPQExpBuffer(); + + foreign = tbinfo && + tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : ""; + + if (coninfo->contype == 'p' || + coninfo->contype == 'u' || + coninfo->contype == 'x') + { + /* Index-related constraint */ + IndxInfo *indxinfo; + int k; + + indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex); + + if (indxinfo == NULL) + pg_fatal("missing index for constraint \"%s\"", + coninfo->dobj.name); + + if (dopt->binary_upgrade) + binary_upgrade_set_pg_class_oids(fout, q, + indxinfo->dobj.catId.oid); + + appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s\n", foreign, + fmtQualifiedDumpable(tbinfo)); + appendPQExpBuffer(q, " ADD CONSTRAINT %s ", + fmtId(coninfo->dobj.name)); + + if (coninfo->condef) + { + /* pg_get_constraintdef should have provided everything */ + appendPQExpBuffer(q, "%s;\n", coninfo->condef); + } + else + { + appendPQExpBufferStr(q, + coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE"); + + /* + * PRIMARY KEY constraints should not be using NULLS NOT DISTINCT + * indexes. Being able to create this was fixed, but we need to + * make the index distinct in order to be able to restore the + * dump. + */ + if (indxinfo->indnullsnotdistinct && coninfo->contype != 'p') + appendPQExpBufferStr(q, " NULLS NOT DISTINCT"); + appendPQExpBufferStr(q, " ("); + for (k = 0; k < indxinfo->indnkeyattrs; k++) + { + int indkey = (int) indxinfo->indkeys[k]; + const char *attname; + + if (indkey == InvalidAttrNumber) + break; + attname = getAttrName(indkey, tbinfo); + + appendPQExpBuffer(q, "%s%s", + (k == 0) ? "" : ", ", + fmtId(attname)); + } + if (coninfo->conperiod) + appendPQExpBufferStr(q, " WITHOUT OVERLAPS"); + + if (indxinfo->indnkeyattrs < indxinfo->indnattrs) + appendPQExpBufferStr(q, ") INCLUDE ("); + + for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++) + { + int indkey = (int) indxinfo->indkeys[k]; + const char *attname; + + if (indkey == InvalidAttrNumber) + break; + attname = getAttrName(indkey, tbinfo); + + appendPQExpBuffer(q, "%s%s", + (k == indxinfo->indnkeyattrs) ? "" : ", ", + fmtId(attname)); + } + + appendPQExpBufferChar(q, ')'); + + if (nonemptyReloptions(indxinfo->indreloptions)) + { + appendPQExpBufferStr(q, " WITH ("); + appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout); + appendPQExpBufferChar(q, ')'); + } + + if (coninfo->condeferrable) + { + appendPQExpBufferStr(q, " DEFERRABLE"); + if (coninfo->condeferred) + appendPQExpBufferStr(q, " INITIALLY DEFERRED"); + } + + appendPQExpBufferStr(q, ";\n"); + } + + /* + * Append ALTER TABLE commands as needed to set properties that we + * only have ALTER TABLE syntax for. Keep this in sync with the + * similar code in dumpIndex! + */ + + /* If the index is clustered, we need to record that. */ + if (indxinfo->indisclustered) + { + appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER", + fmtQualifiedDumpable(tbinfo)); + /* index name is not qualified in this syntax */ + appendPQExpBuffer(q, " ON %s;\n", + fmtId(indxinfo->dobj.name)); + } + + /* If the index defines identity, we need to record that. */ + if (indxinfo->indisreplident) + { + appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING", + fmtQualifiedDumpable(tbinfo)); + /* index name is not qualified in this syntax */ + appendPQExpBuffer(q, " INDEX %s;\n", + fmtId(indxinfo->dobj.name)); + } + + /* Indexes can depend on extensions */ + append_depends_on_extension(fout, q, &indxinfo->dobj, + "pg_catalog.pg_class", "INDEX", + fmtQualifiedDumpable(indxinfo)); + + appendPQExpBuffer(delq, "ALTER %sTABLE ONLY %s ", foreign, + fmtQualifiedDumpable(tbinfo)); + appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", + fmtId(coninfo->dobj.name)); + + tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name); + + if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = tbinfo->dobj.namespace->dobj.name, + .tablespace = indxinfo->tablespace, + .owner = tbinfo->rolname, + .description = "CONSTRAINT", + .section = SECTION_POST_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + } + else if (coninfo->contype == 'f') + { + char *only; + + /* + * Foreign keys on partitioned tables are always declared as + * inheriting to partitions; for all other cases, emit them as + * applying ONLY directly to the named table, because that's how they + * work for regular inherited tables. + */ + only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY "; + + /* + * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the + * current table data is not processed + */ + appendPQExpBuffer(q, "ALTER %sTABLE %s%s\n", foreign, + only, fmtQualifiedDumpable(tbinfo)); + appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n", + fmtId(coninfo->dobj.name), + coninfo->condef); + + appendPQExpBuffer(delq, "ALTER %sTABLE %s%s ", foreign, + only, fmtQualifiedDumpable(tbinfo)); + appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", + fmtId(coninfo->dobj.name)); + + tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name); + + if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "FK CONSTRAINT", + .section = SECTION_POST_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + } + else if ((coninfo->contype == 'c' || coninfo->contype == 'n') && tbinfo) + { + /* CHECK or invalid not-null constraint on a table */ + + /* Ignore if not to be dumped separately, or if it was inherited */ + if (coninfo->separate && coninfo->conislocal) + { + const char *keyword; + + if (coninfo->contype == 'c') + keyword = "CHECK CONSTRAINT"; + else + keyword = "CONSTRAINT"; + + /* not ONLY since we want it to propagate to children */ + appendPQExpBuffer(q, "ALTER %sTABLE %s\n", foreign, + fmtQualifiedDumpable(tbinfo)); + appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n", + fmtId(coninfo->dobj.name), + coninfo->condef); + + appendPQExpBuffer(delq, "ALTER %sTABLE %s ", foreign, + fmtQualifiedDumpable(tbinfo)); + appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", + fmtId(coninfo->dobj.name)); + + tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name); + + if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = keyword, + .section = SECTION_POST_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + } + } + else if (tbinfo == NULL) + { + /* CHECK, NOT NULL constraint on a domain */ + TypeInfo *tyinfo = coninfo->condomain; + + Assert(coninfo->contype == 'c' || coninfo->contype == 'n'); + + /* Ignore if not to be dumped separately */ + if (coninfo->separate) + { + const char *keyword; + + if (coninfo->contype == 'c') + keyword = "CHECK CONSTRAINT"; + else + keyword = "CONSTRAINT"; + + appendPQExpBuffer(q, "ALTER DOMAIN %s\n", + fmtQualifiedDumpable(tyinfo)); + appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n", + fmtId(coninfo->dobj.name), + coninfo->condef); + + appendPQExpBuffer(delq, "ALTER DOMAIN %s ", + fmtQualifiedDumpable(tyinfo)); + appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n", + fmtId(coninfo->dobj.name)); + + tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name); + + if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = tyinfo->dobj.namespace->dobj.name, + .owner = tyinfo->rolname, + .description = keyword, + .section = SECTION_POST_DATA, + .createStmt = q->data, + .dropStmt = delq->data)); + + if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT) + { + PQExpBuffer conprefix = createPQExpBuffer(); + char *qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); + + appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN", + fmtId(coninfo->dobj.name)); + + dumpComment(fout, conprefix->data, qtypname, + tyinfo->dobj.namespace->dobj.name, + tyinfo->rolname, + coninfo->dobj.catId, 0, coninfo->dobj.dumpId); + destroyPQExpBuffer(conprefix); + free(qtypname); + } + } + } + else + { + pg_fatal("unrecognized constraint type: %c", + coninfo->contype); + } + + /* Dump Constraint Comments --- only works for table constraints */ + if (tbinfo && coninfo->separate && + coninfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpTableConstraintComment(fout, coninfo); + + free(tag); + destroyPQExpBuffer(q); + destroyPQExpBuffer(delq); +} + +/* + * dumpTableConstraintComment --- dump a constraint's comment if any + * + * This is split out because we need the function in two different places + * depending on whether the constraint is dumped as part of CREATE TABLE + * or as a separate ALTER command. + */ +static void +dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo) +{ + TableInfo *tbinfo = coninfo->contable; + PQExpBuffer conprefix = createPQExpBuffer(); + char *qtabname; + + qtabname = pg_strdup(fmtId(tbinfo->dobj.name)); + + appendPQExpBuffer(conprefix, "CONSTRAINT %s ON", + fmtId(coninfo->dobj.name)); + + if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, conprefix->data, qtabname, + tbinfo->dobj.namespace->dobj.name, + tbinfo->rolname, + coninfo->dobj.catId, 0, + coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId); + + destroyPQExpBuffer(conprefix); + free(qtabname); +} + +static inline SeqType +parse_sequence_type(const char *name) +{ + for (int i = 0; i < lengthof(SeqTypeNames); i++) + { + if (strcmp(SeqTypeNames[i], name) == 0) + return (SeqType) i; + } + + pg_fatal("unrecognized sequence type: %s", name); + return (SeqType) 0; /* keep compiler quiet */ +} + +/* + * bsearch() comparator for SequenceItem + */ +static int +SequenceItemCmp(const void *p1, const void *p2) +{ + SequenceItem v1 = *((const SequenceItem *) p1); + SequenceItem v2 = *((const SequenceItem *) p2); + + return pg_cmp_u32(v1.oid, v2.oid); +} + +/* + * collectSequences + * + * Construct a table of sequence information. This table is sorted by OID for + * speed in lookup. + */ +static void +collectSequences(Archive *fout) +{ + PGresult *res; + const char *query; + + /* + * Before Postgres 10, sequence metadata is in the sequence itself. With + * some extra effort, we might be able to use the sorted table for those + * versions, but for now it seems unlikely to be worth it. + * + * Since version 18, we can gather the sequence data in this query with + * pg_get_sequence_data(), but we only do so for non-schema-only dumps. + */ + if (fout->remoteVersion < 100000) + return; + else if (fout->remoteVersion < 180000 || + (!fout->dopt->dumpData && !fout->dopt->sequence_data)) + query = "SELECT seqrelid, format_type(seqtypid, NULL), " + "seqstart, seqincrement, " + "seqmax, seqmin, " + "seqcache, seqcycle, " + "NULL, 'f' " + "FROM pg_catalog.pg_sequence " + "ORDER BY seqrelid"; + else + query = "SELECT seqrelid, format_type(seqtypid, NULL), " + "seqstart, seqincrement, " + "seqmax, seqmin, " + "seqcache, seqcycle, " + "last_value, is_called " + "FROM pg_catalog.pg_sequence, " + "pg_get_sequence_data(seqrelid) " + "ORDER BY seqrelid;"; + + res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK); + + nsequences = PQntuples(res); + sequences = pg_malloc_array(SequenceItem, nsequences); + + for (int i = 0; i < nsequences; i++) + { + sequences[i].oid = atooid(PQgetvalue(res, i, 0)); + sequences[i].seqtype = parse_sequence_type(PQgetvalue(res, i, 1)); + sequences[i].startv = strtoi64(PQgetvalue(res, i, 2), NULL, 10); + sequences[i].incby = strtoi64(PQgetvalue(res, i, 3), NULL, 10); + sequences[i].maxv = strtoi64(PQgetvalue(res, i, 4), NULL, 10); + sequences[i].minv = strtoi64(PQgetvalue(res, i, 5), NULL, 10); + sequences[i].cache = strtoi64(PQgetvalue(res, i, 6), NULL, 10); + sequences[i].cycled = (strcmp(PQgetvalue(res, i, 7), "t") == 0); + sequences[i].last_value = strtoi64(PQgetvalue(res, i, 8), NULL, 10); + sequences[i].is_called = (strcmp(PQgetvalue(res, i, 9), "t") == 0); + sequences[i].null_seqtuple = (PQgetisnull(res, i, 8) || PQgetisnull(res, i, 9)); + } + + PQclear(res); +} + +/* + * dumpSequence + * write the declaration (not data) of one user-defined sequence + */ +static void +dumpSequence(Archive *fout, const TableInfo *tbinfo) +{ + DumpOptions *dopt = fout->dopt; + SequenceItem *seq; + bool is_ascending; + int64 default_minv, + default_maxv; + PQExpBuffer query = createPQExpBuffer(); + PQExpBuffer delqry = createPQExpBuffer(); + char *qseqname; + TableInfo *owning_tab = NULL; + + qseqname = pg_strdup(fmtId(tbinfo->dobj.name)); + + /* + * For versions >= 10, the sequence information is gathered in a sorted + * table before any calls to dumpSequence(). See collectSequences() for + * more information. + */ + if (fout->remoteVersion >= 100000) + { + SequenceItem key = {0}; + + Assert(sequences); + + key.oid = tbinfo->dobj.catId.oid; + seq = bsearch(&key, sequences, nsequences, + sizeof(SequenceItem), SequenceItemCmp); + } + else + { + PGresult *res; + + /* + * Before PostgreSQL 10, sequence metadata is in the sequence itself. + * + * Note: it might seem that 'bigint' potentially needs to be + * schema-qualified, but actually that's a keyword. + */ + appendPQExpBuffer(query, + "SELECT 'bigint' AS sequence_type, " + "start_value, increment_by, max_value, min_value, " + "cache_value, is_cycled FROM %s", + fmtQualifiedDumpable(tbinfo)); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + if (PQntuples(res) != 1) + pg_fatal(ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)", + "query to get data of sequence \"%s\" returned %d rows (expected 1)", + PQntuples(res)), + tbinfo->dobj.name, PQntuples(res)); + + seq = pg_malloc0_object(SequenceItem); + seq->seqtype = parse_sequence_type(PQgetvalue(res, 0, 0)); + seq->startv = strtoi64(PQgetvalue(res, 0, 1), NULL, 10); + seq->incby = strtoi64(PQgetvalue(res, 0, 2), NULL, 10); + seq->maxv = strtoi64(PQgetvalue(res, 0, 3), NULL, 10); + seq->minv = strtoi64(PQgetvalue(res, 0, 4), NULL, 10); + seq->cache = strtoi64(PQgetvalue(res, 0, 5), NULL, 10); + seq->cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0); + + PQclear(res); + } + + /* Calculate default limits for a sequence of this type */ + is_ascending = (seq->incby >= 0); + if (seq->seqtype == SEQTYPE_SMALLINT) + { + default_minv = is_ascending ? 1 : PG_INT16_MIN; + default_maxv = is_ascending ? PG_INT16_MAX : -1; + } + else if (seq->seqtype == SEQTYPE_INTEGER) + { + default_minv = is_ascending ? 1 : PG_INT32_MIN; + default_maxv = is_ascending ? PG_INT32_MAX : -1; + } + else if (seq->seqtype == SEQTYPE_BIGINT) + { + default_minv = is_ascending ? 1 : PG_INT64_MIN; + default_maxv = is_ascending ? PG_INT64_MAX : -1; + } + else + { + pg_fatal("unrecognized sequence type: %d", seq->seqtype); + default_minv = default_maxv = 0; /* keep compiler quiet */ + } + + /* + * Identity sequences are not to be dropped separately. + */ + if (!tbinfo->is_identity_sequence) + { + appendPQExpBuffer(delqry, "DROP SEQUENCE %s;\n", + fmtQualifiedDumpable(tbinfo)); + } + + resetPQExpBuffer(query); + + if (dopt->binary_upgrade) + { + binary_upgrade_set_pg_class_oids(fout, query, + tbinfo->dobj.catId.oid); + + /* + * In older PG versions a sequence will have a pg_type entry, but v14 + * and up don't use that, so don't attempt to preserve the type OID. + */ + } + + if (tbinfo->is_identity_sequence) + { + owning_tab = findTableByOid(tbinfo->owning_tab); + + appendPQExpBuffer(query, + "ALTER TABLE %s ", + fmtQualifiedDumpable(owning_tab)); + appendPQExpBuffer(query, + "ALTER COLUMN %s ADD GENERATED ", + fmtId(owning_tab->attnames[tbinfo->owning_col - 1])); + if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_ALWAYS) + appendPQExpBufferStr(query, "ALWAYS"); + else if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_BY_DEFAULT) + appendPQExpBufferStr(query, "BY DEFAULT"); + appendPQExpBuffer(query, " AS IDENTITY (\n SEQUENCE NAME %s\n", + fmtQualifiedDumpable(tbinfo)); + + /* + * Emit persistence option only if it's different from the owning + * table's. This avoids using this new syntax unnecessarily. + */ + if (tbinfo->relpersistence != owning_tab->relpersistence) + appendPQExpBuffer(query, " %s\n", + tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ? + "UNLOGGED" : "LOGGED"); + } + else + { + appendPQExpBuffer(query, + "CREATE %sSEQUENCE %s\n", + tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ? + "UNLOGGED " : "", + fmtQualifiedDumpable(tbinfo)); + + if (seq->seqtype != SEQTYPE_BIGINT) + appendPQExpBuffer(query, " AS %s\n", SeqTypeNames[seq->seqtype]); + } + + appendPQExpBuffer(query, " START WITH " INT64_FORMAT "\n", seq->startv); + + appendPQExpBuffer(query, " INCREMENT BY " INT64_FORMAT "\n", seq->incby); + + if (seq->minv != default_minv) + appendPQExpBuffer(query, " MINVALUE " INT64_FORMAT "\n", seq->minv); + else + appendPQExpBufferStr(query, " NO MINVALUE\n"); + + if (seq->maxv != default_maxv) + appendPQExpBuffer(query, " MAXVALUE " INT64_FORMAT "\n", seq->maxv); + else + appendPQExpBufferStr(query, " NO MAXVALUE\n"); + + appendPQExpBuffer(query, + " CACHE " INT64_FORMAT "%s", + seq->cache, (seq->cycled ? "\n CYCLE" : "")); + + if (tbinfo->is_identity_sequence) + appendPQExpBufferStr(query, "\n);\n"); + else + appendPQExpBufferStr(query, ";\n"); + + /* binary_upgrade: no need to clear TOAST table oid */ + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(query, &tbinfo->dobj, + "SEQUENCE", qseqname, + tbinfo->dobj.namespace->dobj.name); + + if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tbinfo->dobj.name, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "SEQUENCE", + .section = SECTION_PRE_DATA, + .createStmt = query->data, + .dropStmt = delqry->data)); + + /* + * If the sequence is owned by a table column, emit the ALTER for it as a + * separate TOC entry immediately following the sequence's own entry. It's + * OK to do this rather than using full sorting logic, because the + * dependency that tells us it's owned will have forced the table to be + * created first. We can't just include the ALTER in the TOC entry + * because it will fail if we haven't reassigned the sequence owner to + * match the table's owner. + * + * We need not schema-qualify the table reference because both sequence + * and table must be in the same schema. + */ + if (OidIsValid(tbinfo->owning_tab) && !tbinfo->is_identity_sequence) + { + owning_tab = findTableByOid(tbinfo->owning_tab); + + if (owning_tab == NULL) + pg_fatal("failed sanity check, parent table with OID %u of sequence with OID %u not found", + tbinfo->owning_tab, tbinfo->dobj.catId.oid); + + if (owning_tab->dobj.dump & DUMP_COMPONENT_DEFINITION) + { + resetPQExpBuffer(query); + appendPQExpBuffer(query, "ALTER SEQUENCE %s", + fmtQualifiedDumpable(tbinfo)); + appendPQExpBuffer(query, " OWNED BY %s", + fmtQualifiedDumpable(owning_tab)); + appendPQExpBuffer(query, ".%s;\n", + fmtId(owning_tab->attnames[tbinfo->owning_col - 1])); + + if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = tbinfo->dobj.name, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "SEQUENCE OWNED BY", + .section = SECTION_PRE_DATA, + .createStmt = query->data, + .deps = &(tbinfo->dobj.dumpId), + .nDeps = 1)); + } + } + + /* Dump Sequence Comments and Security Labels */ + if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "SEQUENCE", qseqname, + tbinfo->dobj.namespace->dobj.name, tbinfo->rolname, + tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId); + + if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "SEQUENCE", qseqname, + tbinfo->dobj.namespace->dobj.name, tbinfo->rolname, + tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId); + + if (fout->remoteVersion < 100000) + pg_free(seq); + destroyPQExpBuffer(query); + destroyPQExpBuffer(delqry); + free(qseqname); +} + +/* + * dumpSequenceData + * write the data of one user-defined sequence + */ +static void +dumpSequenceData(Archive *fout, const TableDataInfo *tdinfo) +{ + TableInfo *tbinfo = tdinfo->tdtable; + int64 last; + bool called; + PQExpBuffer query; + + /* needn't bother if not dumping sequence data */ + if (!fout->dopt->dumpData && !fout->dopt->sequence_data) + return; + + query = createPQExpBuffer(); + + /* + * For versions >= 18, the sequence information is gathered in the sorted + * array before any calls to dumpSequenceData(). See collectSequences() + * for more information. + * + * For older versions, we have to query the sequence relations + * individually. + */ + if (fout->remoteVersion < 180000) + { + PGresult *res; + + appendPQExpBuffer(query, + "SELECT last_value, is_called FROM %s", + fmtQualifiedDumpable(tbinfo)); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + if (PQntuples(res) != 1) + pg_fatal(ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)", + "query to get data of sequence \"%s\" returned %d rows (expected 1)", + PQntuples(res)), + tbinfo->dobj.name, PQntuples(res)); + + last = strtoi64(PQgetvalue(res, 0, 0), NULL, 10); + called = (strcmp(PQgetvalue(res, 0, 1), "t") == 0); + + PQclear(res); + } + else + { + SequenceItem key = {0}; + SequenceItem *entry; + + Assert(sequences); + Assert(tbinfo->dobj.catId.oid); + + key.oid = tbinfo->dobj.catId.oid; + entry = bsearch(&key, sequences, nsequences, + sizeof(SequenceItem), SequenceItemCmp); + + if (entry->null_seqtuple) + pg_fatal("failed to get data for sequence \"%s\"; user may lack " + "SELECT privilege on the sequence or the sequence may " + "have been concurrently dropped", + tbinfo->dobj.name); + + last = entry->last_value; + called = entry->is_called; + } + + resetPQExpBuffer(query); + appendPQExpBufferStr(query, "SELECT pg_catalog.setval("); + appendStringLiteralAH(query, fmtQualifiedDumpable(tbinfo), fout); + appendPQExpBuffer(query, ", " INT64_FORMAT ", %s);\n", + last, (called ? "true" : "false")); + + if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA) + ArchiveEntry(fout, nilCatalogId, createDumpId(), + ARCHIVE_OPTS(.tag = tbinfo->dobj.name, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "SEQUENCE SET", + .section = SECTION_DATA, + .createStmt = query->data, + .deps = &(tbinfo->dobj.dumpId), + .nDeps = 1)); + + destroyPQExpBuffer(query); +} + +/* + * dumpTrigger + * write the declaration of one user-defined table trigger + */ +static void +dumpTrigger(Archive *fout, const TriggerInfo *tginfo) +{ + DumpOptions *dopt = fout->dopt; + TableInfo *tbinfo = tginfo->tgtable; + PQExpBuffer query; + PQExpBuffer delqry; + PQExpBuffer trigprefix; + PQExpBuffer trigidentity; + char *qtabname; + char *tag; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + query = createPQExpBuffer(); + delqry = createPQExpBuffer(); + trigprefix = createPQExpBuffer(); + trigidentity = createPQExpBuffer(); + + qtabname = pg_strdup(fmtId(tbinfo->dobj.name)); + + appendPQExpBuffer(trigidentity, "%s ", fmtId(tginfo->dobj.name)); + appendPQExpBuffer(trigidentity, "ON %s", fmtQualifiedDumpable(tbinfo)); + + appendPQExpBuffer(query, "%s;\n", tginfo->tgdef); + appendPQExpBuffer(delqry, "DROP TRIGGER %s;\n", trigidentity->data); + + /* Triggers can depend on extensions */ + append_depends_on_extension(fout, query, &tginfo->dobj, + "pg_catalog.pg_trigger", "TRIGGER", + trigidentity->data); + + if (tginfo->tgispartition) + { + Assert(tbinfo->ispartition); + + /* + * Partition triggers only appear here because their 'tgenabled' flag + * differs from its parent's. The trigger is created already, so + * remove the CREATE and replace it with an ALTER. (Clear out the + * DROP query too, so that pg_dump --create does not cause errors.) + */ + resetPQExpBuffer(query); + resetPQExpBuffer(delqry); + appendPQExpBuffer(query, "\nALTER %sTABLE %s ", + tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "", + fmtQualifiedDumpable(tbinfo)); + switch (tginfo->tgenabled) + { + case 'f': + case 'D': + appendPQExpBufferStr(query, "DISABLE"); + break; + case 't': + case 'O': + appendPQExpBufferStr(query, "ENABLE"); + break; + case 'R': + appendPQExpBufferStr(query, "ENABLE REPLICA"); + break; + case 'A': + appendPQExpBufferStr(query, "ENABLE ALWAYS"); + break; + } + appendPQExpBuffer(query, " TRIGGER %s;\n", + fmtId(tginfo->dobj.name)); + } + else if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O') + { + appendPQExpBuffer(query, "\nALTER %sTABLE %s ", + tbinfo->relkind == RELKIND_FOREIGN_TABLE ? "FOREIGN " : "", + fmtQualifiedDumpable(tbinfo)); + switch (tginfo->tgenabled) + { + case 'D': + case 'f': + appendPQExpBufferStr(query, "DISABLE"); + break; + case 'A': + appendPQExpBufferStr(query, "ENABLE ALWAYS"); + break; + case 'R': + appendPQExpBufferStr(query, "ENABLE REPLICA"); + break; + default: + appendPQExpBufferStr(query, "ENABLE"); + break; + } + appendPQExpBuffer(query, " TRIGGER %s;\n", + fmtId(tginfo->dobj.name)); + } + + appendPQExpBuffer(trigprefix, "TRIGGER %s ON", + fmtId(tginfo->dobj.name)); + + tag = psprintf("%s %s", tbinfo->dobj.name, tginfo->dobj.name); + + if (tginfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "TRIGGER", + .section = SECTION_POST_DATA, + .createStmt = query->data, + .dropStmt = delqry->data)); + + if (tginfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, trigprefix->data, qtabname, + tbinfo->dobj.namespace->dobj.name, tbinfo->rolname, + tginfo->dobj.catId, 0, tginfo->dobj.dumpId); + + free(tag); + destroyPQExpBuffer(query); + destroyPQExpBuffer(delqry); + destroyPQExpBuffer(trigprefix); + destroyPQExpBuffer(trigidentity); + free(qtabname); +} + +/* + * dumpEventTrigger + * write the declaration of one user-defined event trigger + */ +static void +dumpEventTrigger(Archive *fout, const EventTriggerInfo *evtinfo) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PQExpBuffer delqry; + char *qevtname; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + query = createPQExpBuffer(); + delqry = createPQExpBuffer(); + + qevtname = pg_strdup(fmtId(evtinfo->dobj.name)); + + appendPQExpBufferStr(query, "CREATE EVENT TRIGGER "); + appendPQExpBufferStr(query, qevtname); + appendPQExpBufferStr(query, " ON "); + appendPQExpBufferStr(query, fmtId(evtinfo->evtevent)); + + if (strcmp("", evtinfo->evttags) != 0) + { + appendPQExpBufferStr(query, "\n WHEN TAG IN ("); + appendPQExpBufferStr(query, evtinfo->evttags); + appendPQExpBufferChar(query, ')'); + } + + appendPQExpBufferStr(query, "\n EXECUTE FUNCTION "); + appendPQExpBufferStr(query, evtinfo->evtfname); + appendPQExpBufferStr(query, "();\n"); + + if (evtinfo->evtenabled != 'O') + { + appendPQExpBuffer(query, "\nALTER EVENT TRIGGER %s ", + qevtname); + switch (evtinfo->evtenabled) + { + case 'D': + appendPQExpBufferStr(query, "DISABLE"); + break; + case 'A': + appendPQExpBufferStr(query, "ENABLE ALWAYS"); + break; + case 'R': + appendPQExpBufferStr(query, "ENABLE REPLICA"); + break; + default: + appendPQExpBufferStr(query, "ENABLE"); + break; + } + appendPQExpBufferStr(query, ";\n"); + } + + appendPQExpBuffer(delqry, "DROP EVENT TRIGGER %s;\n", + qevtname); + + if (dopt->binary_upgrade) + binary_upgrade_extension_member(query, &evtinfo->dobj, + "EVENT TRIGGER", qevtname, NULL); + + if (evtinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, evtinfo->dobj.catId, evtinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = evtinfo->dobj.name, + .owner = evtinfo->evtowner, + .description = "EVENT TRIGGER", + .section = SECTION_POST_DATA, + .createStmt = query->data, + .dropStmt = delqry->data)); + + if (evtinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, "EVENT TRIGGER", qevtname, + NULL, evtinfo->evtowner, + evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId); + + if (evtinfo->dobj.dump & DUMP_COMPONENT_SECLABEL) + dumpSecLabel(fout, "EVENT TRIGGER", qevtname, + NULL, evtinfo->evtowner, + evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId); + + destroyPQExpBuffer(query); + destroyPQExpBuffer(delqry); + free(qevtname); +} + +/* + * dumpRule + * Dump a rule + */ +static void +dumpRule(Archive *fout, const RuleInfo *rinfo) +{ + DumpOptions *dopt = fout->dopt; + TableInfo *tbinfo = rinfo->ruletable; + bool is_view; + PQExpBuffer query; + PQExpBuffer cmd; + PQExpBuffer delcmd; + PQExpBuffer ruleprefix; + char *qtabname; + PGresult *res; + char *tag; + + /* Do nothing if not dumping schema */ + if (!dopt->dumpSchema) + return; + + /* + * If it is an ON SELECT rule that is created implicitly by CREATE VIEW, + * we do not want to dump it as a separate object. + */ + if (!rinfo->separate) + return; + + /* + * If it's an ON SELECT rule, we want to print it as a view definition, + * instead of a rule. + */ + is_view = (rinfo->ev_type == '1' && rinfo->is_instead); + + query = createPQExpBuffer(); + cmd = createPQExpBuffer(); + delcmd = createPQExpBuffer(); + ruleprefix = createPQExpBuffer(); + + qtabname = pg_strdup(fmtId(tbinfo->dobj.name)); + + if (is_view) + { + PQExpBuffer result; + + /* + * We need OR REPLACE here because we'll be replacing a dummy view. + * Otherwise this should look largely like the regular view dump code. + */ + appendPQExpBuffer(cmd, "CREATE OR REPLACE VIEW %s", + fmtQualifiedDumpable(tbinfo)); + if (nonemptyReloptions(tbinfo->reloptions)) + { + appendPQExpBufferStr(cmd, " WITH ("); + appendReloptionsArrayAH(cmd, tbinfo->reloptions, "", fout); + appendPQExpBufferChar(cmd, ')'); + } + result = createViewAsClause(fout, tbinfo); + appendPQExpBuffer(cmd, " AS\n%s", result->data); + destroyPQExpBuffer(result); + if (tbinfo->checkoption != NULL) + appendPQExpBuffer(cmd, "\n WITH %s CHECK OPTION", + tbinfo->checkoption); + appendPQExpBufferStr(cmd, ";\n"); + } + else + { + /* In the rule case, just print pg_get_ruledef's result verbatim */ + appendPQExpBuffer(query, + "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)", + rinfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + if (PQntuples(res) != 1) + pg_fatal("query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned", + rinfo->dobj.name, tbinfo->dobj.name); + + printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0)); + + PQclear(res); + } + + /* + * Add the command to alter the rules replication firing semantics if it + * differs from the default. + */ + if (rinfo->ev_enabled != 'O') + { + appendPQExpBuffer(cmd, "ALTER TABLE %s ", fmtQualifiedDumpable(tbinfo)); + switch (rinfo->ev_enabled) + { + case 'A': + appendPQExpBuffer(cmd, "ENABLE ALWAYS RULE %s;\n", + fmtId(rinfo->dobj.name)); + break; + case 'R': + appendPQExpBuffer(cmd, "ENABLE REPLICA RULE %s;\n", + fmtId(rinfo->dobj.name)); + break; + case 'D': + appendPQExpBuffer(cmd, "DISABLE RULE %s;\n", + fmtId(rinfo->dobj.name)); + break; + } + } + + if (is_view) + { + /* + * We can't DROP a view's ON SELECT rule. Instead, use CREATE OR + * REPLACE VIEW to replace the rule with something with minimal + * dependencies. + */ + PQExpBuffer result; + + appendPQExpBuffer(delcmd, "CREATE OR REPLACE VIEW %s", + fmtQualifiedDumpable(tbinfo)); + result = createDummyViewAsClause(fout, tbinfo); + appendPQExpBuffer(delcmd, " AS\n%s;\n", result->data); + destroyPQExpBuffer(result); + } + else + { + appendPQExpBuffer(delcmd, "DROP RULE %s ", + fmtId(rinfo->dobj.name)); + appendPQExpBuffer(delcmd, "ON %s;\n", + fmtQualifiedDumpable(tbinfo)); + } + + appendPQExpBuffer(ruleprefix, "RULE %s ON", + fmtId(rinfo->dobj.name)); + + tag = psprintf("%s %s", tbinfo->dobj.name, rinfo->dobj.name); + + if (rinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) + ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId, + ARCHIVE_OPTS(.tag = tag, + .namespace = tbinfo->dobj.namespace->dobj.name, + .owner = tbinfo->rolname, + .description = "RULE", + .section = SECTION_POST_DATA, + .createStmt = cmd->data, + .dropStmt = delcmd->data)); + + /* Dump rule comments */ + if (rinfo->dobj.dump & DUMP_COMPONENT_COMMENT) + dumpComment(fout, ruleprefix->data, qtabname, + tbinfo->dobj.namespace->dobj.name, + tbinfo->rolname, + rinfo->dobj.catId, 0, rinfo->dobj.dumpId); + + free(tag); + destroyPQExpBuffer(query); + destroyPQExpBuffer(cmd); + destroyPQExpBuffer(delcmd); + destroyPQExpBuffer(ruleprefix); + free(qtabname); +} + +/* + * getExtensionMembership --- obtain extension membership data + * + * We need to identify objects that are extension members as soon as they're + * loaded, so that we can correctly determine whether they need to be dumped. + * Generally speaking, extension member objects will get marked as *not* to + * be dumped, as they will be recreated by the single CREATE EXTENSION + * command. However, in binary upgrade mode we still need to dump the members + * individually. + */ +void +getExtensionMembership(Archive *fout, ExtensionInfo extinfo[], + int numExtensions) +{ + PQExpBuffer query; + PGresult *res; + int ntups, + i; + int i_classid, + i_objid, + i_refobjid; + ExtensionInfo *ext; + + /* Nothing to do if no extensions */ + if (numExtensions == 0) + return; + + query = createPQExpBuffer(); + + /* refclassid constraint is redundant but may speed the search */ + appendPQExpBufferStr(query, "SELECT " + "classid, objid, refobjid " + "FROM pg_depend " + "WHERE refclassid = 'pg_extension'::regclass " + "AND deptype = 'e' " + "ORDER BY 3"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_classid = PQfnumber(res, "classid"); + i_objid = PQfnumber(res, "objid"); + i_refobjid = PQfnumber(res, "refobjid"); + + /* + * Since we ordered the SELECT by referenced ID, we can expect that + * multiple entries for the same extension will appear together; this + * saves on searches. + */ + ext = NULL; + + for (i = 0; i < ntups; i++) + { + CatalogId objId; + Oid extId; + + objId.tableoid = atooid(PQgetvalue(res, i, i_classid)); + objId.oid = atooid(PQgetvalue(res, i, i_objid)); + extId = atooid(PQgetvalue(res, i, i_refobjid)); + + if (ext == NULL || + ext->dobj.catId.oid != extId) + ext = findExtensionByOid(extId); + + if (ext == NULL) + { + /* shouldn't happen */ + pg_log_warning("could not find referenced extension %u", extId); + continue; + } + + recordExtensionMembership(objId, ext); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + +/* + * processExtensionTables --- deal with extension configuration tables + * + * There are two parts to this process: + * + * 1. Identify and create dump records for extension configuration tables. + * + * Extensions can mark tables as "configuration", which means that the user + * is able and expected to modify those tables after the extension has been + * loaded. For these tables, we dump out only the data- the structure is + * expected to be handled at CREATE EXTENSION time, including any indexes or + * foreign keys, which brings us to- + * + * 2. Record FK dependencies between configuration tables. + * + * Due to the FKs being created at CREATE EXTENSION time and therefore before + * the data is loaded, we have to work out what the best order for reloading + * the data is, to avoid FK violations when the tables are restored. This is + * not perfect- we can't handle circular dependencies and if any exist they + * will cause an invalid dump to be produced (though at least all of the data + * is included for a user to manually restore). This is currently documented + * but perhaps we can provide a better solution in the future. + */ +void +processExtensionTables(Archive *fout, ExtensionInfo extinfo[], + int numExtensions) +{ + DumpOptions *dopt = fout->dopt; + PQExpBuffer query; + PGresult *res; + int ntups, + i; + int i_conrelid, + i_confrelid; + + /* Nothing to do if no extensions */ + if (numExtensions == 0) + return; + + /* + * Identify extension configuration tables and create TableDataInfo + * objects for them, ensuring their data will be dumped even though the + * tables themselves won't be. + * + * Note that we create TableDataInfo objects even in schema-only mode, ie, + * user data in a configuration table is treated like schema data. This + * seems appropriate since system data in a config table would get + * reloaded by CREATE EXTENSION. If the extension is not listed in the + * list of extensions to be included, none of its data is dumped. + */ + for (i = 0; i < numExtensions; i++) + { + ExtensionInfo *curext = &(extinfo[i]); + char *extconfig = curext->extconfig; + char *extcondition = curext->extcondition; + char **extconfigarray = NULL; + char **extconditionarray = NULL; + int nconfigitems = 0; + int nconditionitems = 0; + + /* + * Check if this extension is listed as to include in the dump. If + * not, any table data associated with it is discarded. + */ + if (extension_include_oids.head != NULL && + !simple_oid_list_member(&extension_include_oids, + curext->dobj.catId.oid)) + continue; + + /* + * Check if this extension is listed as to exclude in the dump. If + * yes, any table data associated with it is discarded. + */ + if (extension_exclude_oids.head != NULL && + simple_oid_list_member(&extension_exclude_oids, + curext->dobj.catId.oid)) + continue; + + if (strlen(extconfig) != 0 || strlen(extcondition) != 0) + { + int j; + + if (!parsePGArray(extconfig, &extconfigarray, &nconfigitems)) + pg_fatal("could not parse %s array", "extconfig"); + if (!parsePGArray(extcondition, &extconditionarray, &nconditionitems)) + pg_fatal("could not parse %s array", "extcondition"); + if (nconfigitems != nconditionitems) + pg_fatal("mismatched number of configurations and conditions for extension"); + + for (j = 0; j < nconfigitems; j++) + { + TableInfo *configtbl; + Oid configtbloid = atooid(extconfigarray[j]); + bool dumpobj = + curext->dobj.dump & DUMP_COMPONENT_DEFINITION; + + configtbl = findTableByOid(configtbloid); + if (configtbl == NULL) + continue; + + /* + * Tables of not-to-be-dumped extensions shouldn't be dumped + * unless the table or its schema is explicitly included + */ + if (!(curext->dobj.dump & DUMP_COMPONENT_DEFINITION)) + { + /* check table explicitly requested */ + if (table_include_oids.head != NULL && + simple_oid_list_member(&table_include_oids, + configtbloid)) + dumpobj = true; + + /* check table's schema explicitly requested */ + if (configtbl->dobj.namespace->dobj.dump & + DUMP_COMPONENT_DATA) + dumpobj = true; + } + + /* check table excluded by an exclusion switch */ + if (table_exclude_oids.head != NULL && + simple_oid_list_member(&table_exclude_oids, + configtbloid)) + dumpobj = false; + + /* check schema excluded by an exclusion switch */ + if (simple_oid_list_member(&schema_exclude_oids, + configtbl->dobj.namespace->dobj.catId.oid)) + dumpobj = false; + + if (dumpobj) + { + makeTableDataInfo(dopt, configtbl); + if (configtbl->dataObj != NULL) + { + if (strlen(extconditionarray[j]) > 0) + configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]); + } + } + } + } + if (extconfigarray) + free(extconfigarray); + if (extconditionarray) + free(extconditionarray); + } + + /* + * Now that all the TableDataInfo objects have been created for all the + * extensions, check their FK dependencies and register them to try and + * dump the data out in an order that they can be restored in. + * + * Note that this is not a problem for user tables as their FKs are + * recreated after the data has been loaded. + */ + + query = createPQExpBuffer(); + + printfPQExpBuffer(query, + "SELECT conrelid, confrelid " + "FROM pg_constraint " + "JOIN pg_depend ON (objid = confrelid) " + "WHERE contype = 'f' " + "AND refclassid = 'pg_extension'::regclass " + "AND classid = 'pg_class'::regclass;"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + ntups = PQntuples(res); + + i_conrelid = PQfnumber(res, "conrelid"); + i_confrelid = PQfnumber(res, "confrelid"); + + /* Now get the dependencies and register them */ + for (i = 0; i < ntups; i++) + { + Oid conrelid, + confrelid; + TableInfo *reftable, + *contable; + + conrelid = atooid(PQgetvalue(res, i, i_conrelid)); + confrelid = atooid(PQgetvalue(res, i, i_confrelid)); + contable = findTableByOid(conrelid); + reftable = findTableByOid(confrelid); + + if (reftable == NULL || + reftable->dataObj == NULL || + contable == NULL || + contable->dataObj == NULL) + continue; + + /* + * Make referencing TABLE_DATA object depend on the referenced table's + * TABLE_DATA object. + */ + addObjectDependency(&contable->dataObj->dobj, + reftable->dataObj->dobj.dumpId); + } + PQclear(res); + destroyPQExpBuffer(query); +} + +/* + * getDependencies --- obtain available dependency data + */ +static void +getDependencies(Archive *fout) +{ + PQExpBuffer query; + PGresult *res; + int ntups, + i; + int i_classid, + i_objid, + i_refclassid, + i_refobjid, + i_deptype; + DumpableObject *dobj, + *refdobj; + + pg_log_info("reading dependency data"); + + query = createPQExpBuffer(); + + /* + * Messy query to collect the dependency data we need. Note that we + * ignore the sub-object column, so that dependencies of or on a column + * look the same as dependencies of or on a whole table. + * + * PIN dependencies aren't interesting, and EXTENSION dependencies were + * already processed by getExtensionMembership. + */ + appendPQExpBufferStr(query, "SELECT " + "classid, objid, refclassid, refobjid, deptype " + "FROM pg_depend " + "WHERE deptype != 'p' AND deptype != 'e'\n"); + + /* + * Since we don't treat pg_amop entries as separate DumpableObjects, we + * have to translate their dependencies into dependencies of their parent + * opfamily. Ignore internal dependencies though, as those will point to + * their parent opclass, which we needn't consider here (and if we did, + * it'd just result in circular dependencies). Also, "loose" opfamily + * entries will have dependencies on their parent opfamily, which we + * should drop since they'd likewise become useless self-dependencies. + * (But be sure to keep deps on *other* opfamilies; see amopsortfamily.) + */ + appendPQExpBufferStr(query, "UNION ALL\n" + "SELECT 'pg_opfamily'::regclass AS classid, amopfamily AS objid, refclassid, refobjid, deptype " + "FROM pg_depend d, pg_amop o " + "WHERE deptype NOT IN ('p', 'e', 'i') AND " + "classid = 'pg_amop'::regclass AND objid = o.oid " + "AND NOT (refclassid = 'pg_opfamily'::regclass AND amopfamily = refobjid)\n"); + + /* Likewise for pg_amproc entries */ + appendPQExpBufferStr(query, "UNION ALL\n" + "SELECT 'pg_opfamily'::regclass AS classid, amprocfamily AS objid, refclassid, refobjid, deptype " + "FROM pg_depend d, pg_amproc p " + "WHERE deptype NOT IN ('p', 'e', 'i') AND " + "classid = 'pg_amproc'::regclass AND objid = p.oid " + "AND NOT (refclassid = 'pg_opfamily'::regclass AND amprocfamily = refobjid)\n"); + + /* + * Translate dependencies of pg_propgraph_element entries into + * dependencies of their parent pg_class entry. + */ + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, "UNION ALL\n" + "SELECT 'pg_class'::regclass AS classid, pgepgid AS objid, refclassid, refobjid, deptype " + "FROM pg_depend d, pg_propgraph_element pge " + "WHERE deptype NOT IN ('p', 'e', 'i') AND " + "classid = 'pg_propgraph_element'::regclass AND objid = pge.oid\n"); + + /* Sort the output for efficiency below */ + appendPQExpBufferStr(query, "ORDER BY 1,2"); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + ntups = PQntuples(res); + + i_classid = PQfnumber(res, "classid"); + i_objid = PQfnumber(res, "objid"); + i_refclassid = PQfnumber(res, "refclassid"); + i_refobjid = PQfnumber(res, "refobjid"); + i_deptype = PQfnumber(res, "deptype"); + + /* + * Since we ordered the SELECT by referencing ID, we can expect that + * multiple entries for the same object will appear together; this saves + * on searches. + */ + dobj = NULL; + + for (i = 0; i < ntups; i++) + { + CatalogId objId; + CatalogId refobjId; + char deptype; + + objId.tableoid = atooid(PQgetvalue(res, i, i_classid)); + objId.oid = atooid(PQgetvalue(res, i, i_objid)); + refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid)); + refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid)); + deptype = *(PQgetvalue(res, i, i_deptype)); + + if (dobj == NULL || + dobj->catId.tableoid != objId.tableoid || + dobj->catId.oid != objId.oid) + dobj = findObjectByCatalogId(objId); + + /* + * Failure to find objects mentioned in pg_depend is not unexpected, + * since for example we don't collect info about TOAST tables. + */ + if (dobj == NULL) + { +#ifdef NOT_USED + pg_log_warning("no referencing object %u %u", + objId.tableoid, objId.oid); +#endif + continue; + } + + refdobj = findObjectByCatalogId(refobjId); + + if (refdobj == NULL) + { +#ifdef NOT_USED + pg_log_warning("no referenced object %u %u", + refobjId.tableoid, refobjId.oid); +#endif + continue; + } + + /* + * For 'x' dependencies, mark the object for later; we still add the + * normal dependency, for possible ordering purposes. Currently + * pg_dump_sort.c knows to put extensions ahead of all object types + * that could possibly depend on them, but this is safer. + */ + if (deptype == 'x') + dobj->depends_on_ext = true; + + /* + * Ordinarily, table rowtypes have implicit dependencies on their + * tables. However, for a composite type the implicit dependency goes + * the other way in pg_depend; which is the right thing for DROP but + * it doesn't produce the dependency ordering we need. So in that one + * case, we reverse the direction of the dependency. + */ + if (deptype == 'i' && + dobj->objType == DO_TABLE && + refdobj->objType == DO_TYPE) + addObjectDependency(refdobj, dobj->dumpId); + else + /* normal case */ + addObjectDependency(dobj, refdobj->dumpId); + } + + PQclear(res); + + destroyPQExpBuffer(query); +} + + +/* + * createBoundaryObjects - create dummy DumpableObjects to represent + * dump section boundaries. + */ +static DumpableObject * +createBoundaryObjects(void) +{ + DumpableObject *dobjs; + + dobjs = pg_malloc_array(DumpableObject, 2); + + dobjs[0].objType = DO_PRE_DATA_BOUNDARY; + dobjs[0].catId = nilCatalogId; + AssignDumpId(dobjs + 0); + dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY"); + + dobjs[1].objType = DO_POST_DATA_BOUNDARY; + dobjs[1].catId = nilCatalogId; + AssignDumpId(dobjs + 1); + dobjs[1].name = pg_strdup("POST-DATA BOUNDARY"); + + return dobjs; +} + +/* + * addBoundaryDependencies - add dependencies as needed to enforce the dump + * section boundaries. + */ +static void +addBoundaryDependencies(DumpableObject **dobjs, int numObjs, + DumpableObject *boundaryObjs) +{ + DumpableObject *preDataBound = boundaryObjs + 0; + DumpableObject *postDataBound = boundaryObjs + 1; + int i; + + for (i = 0; i < numObjs; i++) + { + DumpableObject *dobj = dobjs[i]; + + /* + * The classification of object types here must match the SECTION_xxx + * values assigned during subsequent ArchiveEntry calls! + */ + switch (dobj->objType) + { + case DO_NAMESPACE: + case DO_EXTENSION: + case DO_TYPE: + case DO_SHELL_TYPE: + case DO_FUNC: + case DO_AGG: + case DO_OPERATOR: + case DO_ACCESS_METHOD: + case DO_OPCLASS: + case DO_OPFAMILY: + case DO_COLLATION: + case DO_CONVERSION: + case DO_TABLE: + case DO_TABLE_ATTACH: + case DO_ATTRDEF: + case DO_PROCLANG: + case DO_CAST: + case DO_DUMMY_TYPE: + case DO_TSPARSER: + case DO_TSDICT: + case DO_TSTEMPLATE: + case DO_TSCONFIG: + case DO_FDW: + case DO_FOREIGN_SERVER: + case DO_TRANSFORM: + /* Pre-data objects: must come before the pre-data boundary */ + addObjectDependency(preDataBound, dobj->dumpId); + break; + case DO_TABLE_DATA: + case DO_SEQUENCE_SET: + case DO_LARGE_OBJECT: + case DO_LARGE_OBJECT_DATA: + /* Data objects: must come between the boundaries */ + addObjectDependency(dobj, preDataBound->dumpId); + addObjectDependency(postDataBound, dobj->dumpId); + break; + case DO_INDEX: + case DO_INDEX_ATTACH: + case DO_STATSEXT: + case DO_REFRESH_MATVIEW: + case DO_TRIGGER: + case DO_EVENT_TRIGGER: + case DO_DEFAULT_ACL: + case DO_POLICY: + case DO_PUBLICATION: + case DO_PUBLICATION_REL: + case DO_PUBLICATION_TABLE_IN_SCHEMA: + case DO_SUBSCRIPTION: + case DO_SUBSCRIPTION_REL: + /* Post-data objects: must come after the post-data boundary */ + addObjectDependency(dobj, postDataBound->dumpId); + break; + case DO_RULE: + /* Rules are post-data, but only if dumped separately */ + if (((RuleInfo *) dobj)->separate) + addObjectDependency(dobj, postDataBound->dumpId); + break; + case DO_CONSTRAINT: + case DO_FK_CONSTRAINT: + /* Constraints are post-data, but only if dumped separately */ + if (((ConstraintInfo *) dobj)->separate) + addObjectDependency(dobj, postDataBound->dumpId); + break; + case DO_PRE_DATA_BOUNDARY: + /* nothing to do */ + break; + case DO_POST_DATA_BOUNDARY: + /* must come after the pre-data boundary */ + addObjectDependency(dobj, preDataBound->dumpId); + break; + case DO_REL_STATS: + /* stats section varies by parent object type, DATA or POST */ + if (((RelStatsInfo *) dobj)->section == SECTION_DATA) + { + addObjectDependency(dobj, preDataBound->dumpId); + addObjectDependency(postDataBound, dobj->dumpId); + } + else + addObjectDependency(dobj, postDataBound->dumpId); + break; + } + } +} + + +/* + * BuildArchiveDependencies - create dependency data for archive TOC entries + * + * The raw dependency data obtained by getDependencies() is not terribly + * useful in an archive dump, because in many cases there are dependency + * chains linking through objects that don't appear explicitly in the dump. + * For example, a view will depend on its _RETURN rule while the _RETURN rule + * will depend on other objects --- but the rule will not appear as a separate + * object in the dump. We need to adjust the view's dependencies to include + * whatever the rule depends on that is included in the dump. + * + * Just to make things more complicated, there are also "special" dependencies + * such as the dependency of a TABLE DATA item on its TABLE, which we must + * not rearrange because pg_restore knows that TABLE DATA only depends on + * its table. In these cases we must leave the dependencies strictly as-is + * even if they refer to not-to-be-dumped objects. + * + * To handle this, the convention is that "special" dependencies are created + * during ArchiveEntry calls, and an archive TOC item that has any such + * entries will not be touched here. Otherwise, we recursively search the + * DumpableObject data structures to build the correct dependencies for each + * archive TOC item. + */ +static void +BuildArchiveDependencies(Archive *fout) +{ + ArchiveHandle *AH = (ArchiveHandle *) fout; + TocEntry *te; + + /* Scan all TOC entries in the archive */ + for (te = AH->toc->next; te != AH->toc; te = te->next) + { + DumpableObject *dobj; + DumpId *dependencies; + int nDeps; + int allocDeps; + + /* No need to process entries that will not be dumped */ + if (te->reqs == 0) + continue; + /* Ignore entries that already have "special" dependencies */ + if (te->nDeps > 0) + continue; + /* Otherwise, look up the item's original DumpableObject, if any */ + dobj = findObjectByDumpId(te->dumpId); + if (dobj == NULL) + continue; + /* No work if it has no dependencies */ + if (dobj->nDeps <= 0) + continue; + /* Set up work array */ + allocDeps = 64; + dependencies = pg_malloc_array(DumpId, allocDeps); + nDeps = 0; + /* Recursively find all dumpable dependencies */ + findDumpableDependencies(AH, dobj, + &dependencies, &nDeps, &allocDeps); + /* And save 'em ... */ + if (nDeps > 0) + { + dependencies = pg_realloc_array(dependencies, DumpId, nDeps); + te->dependencies = dependencies; + te->nDeps = nDeps; + } + else + free(dependencies); + } +} + +/* Recursive search subroutine for BuildArchiveDependencies */ +static void +findDumpableDependencies(ArchiveHandle *AH, const DumpableObject *dobj, + DumpId **dependencies, int *nDeps, int *allocDeps) +{ + int i; + + /* + * Ignore section boundary objects: if we search through them, we'll + * report lots of bogus dependencies. + */ + if (dobj->objType == DO_PRE_DATA_BOUNDARY || + dobj->objType == DO_POST_DATA_BOUNDARY) + return; + + for (i = 0; i < dobj->nDeps; i++) + { + DumpId depid = dobj->dependencies[i]; + + if (TocIDRequired(AH, depid) != 0) + { + /* Object will be dumped, so just reference it as a dependency */ + if (*nDeps >= *allocDeps) + { + *allocDeps *= 2; + *dependencies = pg_realloc_array(*dependencies, DumpId, *allocDeps); + } + (*dependencies)[*nDeps] = depid; + (*nDeps)++; + } + else + { + /* + * Object will not be dumped, so recursively consider its deps. We + * rely on the assumption that sortDumpableObjects already broke + * any dependency loops, else we might recurse infinitely. + */ + DumpableObject *otherdobj = findObjectByDumpId(depid); + + if (otherdobj) + findDumpableDependencies(AH, otherdobj, + dependencies, nDeps, allocDeps); + } + } +} + + +/* + * getFormattedTypeName - retrieve a nicely-formatted type name for the + * given type OID. + * + * This does not guarantee to schema-qualify the output, so it should not + * be used to create the target object name for CREATE or ALTER commands. + * + * Note that the result is cached and must not be freed by the caller. + */ +static const char * +getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts) +{ + TypeInfo *typeInfo; + char *result; + PQExpBuffer query; + PGresult *res; + + if (oid == 0) + { + if ((opts & zeroAsStar) != 0) + return "*"; + else if ((opts & zeroAsNone) != 0) + return "NONE"; + } + + /* see if we have the result cached in the type's TypeInfo record */ + typeInfo = findTypeByOid(oid); + if (typeInfo && typeInfo->ftypname) + return typeInfo->ftypname; + + query = createPQExpBuffer(); + appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)", + oid); + + res = ExecuteSqlQueryForSingleRow(fout, query->data); + + /* result of format_type is already quoted */ + result = pg_strdup(PQgetvalue(res, 0, 0)); + + PQclear(res); + destroyPQExpBuffer(query); + + /* + * Cache the result for re-use in later requests, if possible. If we + * don't have a TypeInfo for the type, the string will be leaked once the + * caller is done with it ... but that case really should not happen, so + * leaking if it does seems acceptable. + */ + if (typeInfo) + typeInfo->ftypname = result; + + return result; +} + +/* + * Return a column list clause for the given relation. + * + * Special case: if there are no undropped columns in the relation, return + * "", not an invalid "()" column list. + */ +static const char * +fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer) +{ + int numatts = ti->numatts; + char **attnames = ti->attnames; + bool *attisdropped = ti->attisdropped; + char *attgenerated = ti->attgenerated; + bool needComma; + int i; + + appendPQExpBufferChar(buffer, '('); + needComma = false; + for (i = 0; i < numatts; i++) + { + if (attisdropped[i]) + continue; + if (attgenerated[i]) + continue; + if (needComma) + appendPQExpBufferStr(buffer, ", "); + appendPQExpBufferStr(buffer, fmtId(attnames[i])); + needComma = true; + } + + if (!needComma) + return ""; /* no undropped columns */ + + appendPQExpBufferChar(buffer, ')'); + return buffer->data; +} + +/* + * Check if a reloptions array is nonempty. + */ +static bool +nonemptyReloptions(const char *reloptions) +{ + /* Don't want to print it if it's just "{}" */ + return (reloptions != NULL && strlen(reloptions) > 2); +} + +/* + * Format a reloptions array and append it to the given buffer. + * + * "prefix" is prepended to the option names; typically it's "" or "toast.". + */ +static void +appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions, + const char *prefix, Archive *fout) +{ + bool res; + + res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding, + fout->std_strings); + if (!res) + pg_log_warning("could not parse %s array", "reloptions"); +} + +/* + * read_dump_filters - retrieve object identifier patterns from file + * + * Parse the specified filter file for include and exclude patterns, and add + * them to the relevant lists. If the filename is "-" then filters will be + * read from STDIN rather than a file. + */ +static void +read_dump_filters(const char *filename, DumpOptions *dopt) +{ + FilterStateData fstate; + char *objname; + FilterCommandType comtype; + FilterObjectType objtype; + + filter_init(&fstate, filename, exit_nicely); + + while (filter_read_item(&fstate, &objname, &comtype, &objtype)) + { + if (comtype == FILTER_COMMAND_TYPE_INCLUDE) + { + switch (objtype) + { + case FILTER_OBJECT_TYPE_NONE: + break; + case FILTER_OBJECT_TYPE_DATABASE: + case FILTER_OBJECT_TYPE_FUNCTION: + case FILTER_OBJECT_TYPE_INDEX: + case FILTER_OBJECT_TYPE_TABLE_DATA: + case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN: + case FILTER_OBJECT_TYPE_TRIGGER: + pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"), + "include", + filter_object_type_name(objtype)); + exit_nicely(1); + break; /* unreachable */ + + case FILTER_OBJECT_TYPE_EXTENSION: + simple_string_list_append(&extension_include_patterns, objname); + break; + case FILTER_OBJECT_TYPE_FOREIGN_DATA: + simple_string_list_append(&foreign_servers_include_patterns, objname); + break; + case FILTER_OBJECT_TYPE_SCHEMA: + simple_string_list_append(&schema_include_patterns, objname); + dopt->include_everything = false; + break; + case FILTER_OBJECT_TYPE_TABLE: + simple_string_list_append(&table_include_patterns, objname); + dopt->include_everything = false; + break; + case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN: + simple_string_list_append(&table_include_patterns_and_children, + objname); + dopt->include_everything = false; + break; + } + } + else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE) + { + switch (objtype) + { + case FILTER_OBJECT_TYPE_NONE: + break; + case FILTER_OBJECT_TYPE_DATABASE: + case FILTER_OBJECT_TYPE_FUNCTION: + case FILTER_OBJECT_TYPE_INDEX: + case FILTER_OBJECT_TYPE_TRIGGER: + case FILTER_OBJECT_TYPE_FOREIGN_DATA: + pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"), + "exclude", + filter_object_type_name(objtype)); + exit_nicely(1); + break; + + case FILTER_OBJECT_TYPE_EXTENSION: + simple_string_list_append(&extension_exclude_patterns, objname); + break; + case FILTER_OBJECT_TYPE_TABLE_DATA: + simple_string_list_append(&tabledata_exclude_patterns, + objname); + break; + case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN: + simple_string_list_append(&tabledata_exclude_patterns_and_children, + objname); + break; + case FILTER_OBJECT_TYPE_SCHEMA: + simple_string_list_append(&schema_exclude_patterns, objname); + break; + case FILTER_OBJECT_TYPE_TABLE: + simple_string_list_append(&table_exclude_patterns, objname); + break; + case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN: + simple_string_list_append(&table_exclude_patterns_and_children, + objname); + break; + } + } + else + { + Assert(comtype == FILTER_COMMAND_TYPE_NONE); + Assert(objtype == FILTER_OBJECT_TYPE_NONE); + } + + if (objname) + free(objname); + } + + filter_free(&fstate); +} diff --git a/src/compat/19/pg_dump/pg_dump.h b/src/compat/19/pg_dump/pg_dump.h new file mode 100644 index 00000000..5a6726d8 --- /dev/null +++ b/src/compat/19/pg_dump/pg_dump.h @@ -0,0 +1,839 @@ +/*------------------------------------------------------------------------- + * + * pg_dump.h + * Common header file for the pg_dump utility + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/bin/pg_dump/pg_dump.h + * + *------------------------------------------------------------------------- + */ + +#ifndef PG_DUMP_H +#define PG_DUMP_H + +#include "pg_backup.h" +#include "catalog/pg_publication_d.h" + + +#define oidcmp(x,y) ( ((x) < (y) ? -1 : ((x) > (y)) ? 1 : 0) ) + +/* + * The data structures used to store system catalog information. Every + * dumpable object is a subclass of DumpableObject. + * + * NOTE: the structures described here live for the entire pg_dump run; + * and in most cases we make a struct for every object we can find in the + * catalogs, not only those we are actually going to dump. Hence, it's + * best to store a minimal amount of per-object info in these structs, + * and retrieve additional per-object info when and if we dump a specific + * object. In particular, try to avoid retrieving expensive-to-compute + * information until it's known to be needed. We do, however, have to + * store enough info to determine whether an object should be dumped and + * what order to dump in. + */ + +typedef enum +{ + /* When modifying this enum, update priority tables in pg_dump_sort.c! */ + DO_NAMESPACE, + DO_EXTENSION, + DO_TYPE, + DO_SHELL_TYPE, + DO_FUNC, + DO_AGG, + DO_OPERATOR, + DO_ACCESS_METHOD, + DO_OPCLASS, + DO_OPFAMILY, + DO_COLLATION, + DO_CONVERSION, + DO_TABLE, + DO_TABLE_ATTACH, + DO_ATTRDEF, + DO_INDEX, + DO_INDEX_ATTACH, + DO_STATSEXT, + DO_RULE, + DO_TRIGGER, + DO_CONSTRAINT, + DO_FK_CONSTRAINT, /* see note for ConstraintInfo */ + DO_PROCLANG, + DO_CAST, + DO_TABLE_DATA, + DO_SEQUENCE_SET, + DO_DUMMY_TYPE, + DO_TSPARSER, + DO_TSDICT, + DO_TSTEMPLATE, + DO_TSCONFIG, + DO_FDW, + DO_FOREIGN_SERVER, + DO_DEFAULT_ACL, + DO_TRANSFORM, + DO_LARGE_OBJECT, + DO_LARGE_OBJECT_DATA, + DO_PRE_DATA_BOUNDARY, + DO_POST_DATA_BOUNDARY, + DO_EVENT_TRIGGER, + DO_REFRESH_MATVIEW, + DO_POLICY, + DO_PUBLICATION, + DO_PUBLICATION_REL, + DO_PUBLICATION_TABLE_IN_SCHEMA, + DO_REL_STATS, + DO_SUBSCRIPTION, + DO_SUBSCRIPTION_REL, /* see note for SubRelInfo */ +} DumpableObjectType; + +#define NUM_DUMPABLE_OBJECT_TYPES (DO_SUBSCRIPTION_REL + 1) + +/* + * DumpComponents is a bitmask of the potentially dumpable components of + * a database object: its core definition, plus optional attributes such + * as ACL, comments, etc. + * + * The NONE and ALL symbols are convenient shorthands for assigning values, + * but be careful about using them in tests. For example, a test like + * "if (dobj->dump == DUMP_COMPONENT_NONE)" is probably wrong; you likely want + * "if (!(dobj->dump & DUMP_COMPONENT_DEFINITION))" instead. This is because + * we aren't too careful about the values of irrelevant bits, as indeed can be + * seen in the definition of DUMP_COMPONENT_ALL. It's also possible that an + * object has only subsidiary bits such as DUMP_COMPONENT_ACL set, leading to + * unexpected behavior of a test against NONE. + */ +typedef uint32 DumpComponents; +#define DUMP_COMPONENT_NONE (0) +#define DUMP_COMPONENT_DEFINITION (1 << 0) +#define DUMP_COMPONENT_DATA (1 << 1) +#define DUMP_COMPONENT_COMMENT (1 << 2) +#define DUMP_COMPONENT_SECLABEL (1 << 3) +#define DUMP_COMPONENT_ACL (1 << 4) +#define DUMP_COMPONENT_POLICY (1 << 5) +#define DUMP_COMPONENT_USERMAP (1 << 6) +#define DUMP_COMPONENT_STATISTICS (1 << 7) +#define DUMP_COMPONENT_ALL (0xFFFF) + +/* + * component types which require us to obtain a lock on the table + * + * Note that some components only require looking at the information + * in the pg_catalog tables and, for those components, we do not need + * to lock the table. Be careful here though- some components use + * server-side functions which pull the latest information from + * SysCache and in those cases we *do* need to lock the table. + * + * We do not need locks for the COMMENT and SECLABEL components as + * those simply query their associated tables without using any + * server-side functions. We do not need locks for the ACL component + * as we pull that information from pg_class without using any + * server-side functions that use SysCache. The USERMAP component + * is only relevant for FOREIGN SERVERs and not tables, so no sense + * locking a table for that either (that can happen if we are going + * to dump "ALL" components for a table). + * + * We DO need locks for DEFINITION, due to various server-side + * functions that are used and POLICY due to pg_get_expr(). We set + * this up to grab the lock except in the cases we know to be safe. + */ +#define DUMP_COMPONENTS_REQUIRING_LOCK (\ + DUMP_COMPONENT_DEFINITION |\ + DUMP_COMPONENT_DATA |\ + DUMP_COMPONENT_STATISTICS |\ + DUMP_COMPONENT_POLICY) + +typedef struct _dumpableObject +{ + DumpableObjectType objType; + CatalogId catId; /* zero if not a cataloged object */ + DumpId dumpId; /* assigned by AssignDumpId() */ + char *name; /* object name (should never be NULL) */ + struct _namespaceInfo *namespace; /* containing namespace, or NULL */ + DumpComponents dump; /* bitmask of components requested to dump */ + DumpComponents dump_contains; /* as above, but for contained objects */ + DumpComponents components; /* bitmask of components available to dump */ + bool ext_member; /* true if object is member of extension */ + bool depends_on_ext; /* true if object depends on an extension */ + DumpId *dependencies; /* dumpIds of objects this one depends on */ + int nDeps; /* number of valid dependencies */ + int allocDeps; /* allocated size of dependencies[] */ +} DumpableObject; + +/* + * Object types that have ACLs must store them in a DumpableAcl sub-struct, + * which must immediately follow the DumpableObject base struct. + */ +typedef struct _dumpableAcl +{ + char *acl; /* the object's actual ACL string */ + char *acldefault; /* default ACL for the object's type & owner */ + /* these fields come from the object's pg_init_privs entry, if any: */ + char privtype; /* entry type, 'i' or 'e'; 0 if no entry */ + char *initprivs; /* the object's initial ACL string, or NULL */ +} DumpableAcl; + +/* Generic struct that can be used to access any object type having an ACL */ +typedef struct _dumpableObjectWithAcl +{ + DumpableObject dobj; + DumpableAcl dacl; +} DumpableObjectWithAcl; + +typedef struct _namespaceInfo +{ + DumpableObject dobj; + DumpableAcl dacl; + bool create; /* CREATE SCHEMA, or just set owner? */ + Oid nspowner; /* OID of owner */ + const char *rolname; /* name of owner */ +} NamespaceInfo; + +typedef struct _extensionInfo +{ + DumpableObject dobj; + char *namespace; /* schema containing extension's objects */ + bool relocatable; + char *extversion; + char *extconfig; /* info about configuration tables */ + char *extcondition; +} ExtensionInfo; + +typedef struct _typeInfo +{ + DumpableObject dobj; + DumpableAcl dacl; + + /* + * Note: dobj.name is the raw pg_type.typname entry. ftypname is the + * result of format_type(), which will be quoted if needed, and might be + * schema-qualified too. + */ + char *ftypname; + const char *rolname; + Oid typelem; + Oid typrelid; + Oid typarray; + char typrelkind; /* 'r', 'v', 'c', etc */ + char typtype; /* 'b', 'c', etc */ + bool isArray; /* true if auto-generated array type */ + bool isMultirange; /* true if auto-generated multirange type */ + bool isDefined; /* true if typisdefined */ + /* If needed, we'll create a "shell type" entry for it; link that here: */ + struct _shellTypeInfo *shellType; /* shell-type entry, or NULL */ + /* If it's a domain, its not-null constraint is here: */ + struct _constraintInfo *notnull; + /* If it's a domain, we store links to its CHECK constraints here: */ + int nDomChecks; + struct _constraintInfo *domChecks; +} TypeInfo; + +typedef struct _shellTypeInfo +{ + DumpableObject dobj; + + TypeInfo *baseType; /* back link to associated base type */ +} ShellTypeInfo; + +typedef struct _funcInfo +{ + DumpableObject dobj; + DumpableAcl dacl; + const char *rolname; + Oid lang; + int nargs; + Oid *argtypes; + Oid prorettype; + bool postponed_def; /* function must be postponed into post-data */ +} FuncInfo; + +/* AggInfo is a superset of FuncInfo */ +typedef struct _aggInfo +{ + FuncInfo aggfn; + /* we don't require any other fields at the moment */ +} AggInfo; + +typedef struct _oprInfo +{ + DumpableObject dobj; + const char *rolname; + char oprkind; + Oid oprleft; + Oid oprright; + Oid oprcode; +} OprInfo; + +typedef struct _accessMethodInfo +{ + DumpableObject dobj; + char amtype; + char *amhandler; +} AccessMethodInfo; + +typedef struct _opclassInfo +{ + DumpableObject dobj; + Oid opcmethod; + const char *rolname; +} OpclassInfo; + +typedef struct _opfamilyInfo +{ + DumpableObject dobj; + Oid opfmethod; + const char *rolname; +} OpfamilyInfo; + +typedef struct _collInfo +{ + DumpableObject dobj; + const char *rolname; + int collencoding; +} CollInfo; + +typedef struct _convInfo +{ + DumpableObject dobj; + const char *rolname; +} ConvInfo; + +typedef struct _tableInfo +{ + /* + * These fields are collected for every table in the database. + */ + DumpableObject dobj; + DumpableAcl dacl; + const char *rolname; + char relkind; + char relpersistence; /* relation persistence */ + bool relispopulated; /* relation is populated */ + char relreplident; /* replica identifier */ + char *reltablespace; /* relation tablespace */ + char *reloptions; /* options specified by WITH (...) */ + char *checkoption; /* WITH CHECK OPTION, if any */ + char *toast_reloptions; /* WITH options for the TOAST table */ + bool hasindex; /* does it have any indexes? */ + bool hasrules; /* does it have any rules? */ + bool hastriggers; /* does it have any triggers? */ + bool hascolumnACLs; /* do any columns have non-default ACLs? */ + bool rowsec; /* is row security enabled? */ + bool forcerowsec; /* is row security forced? */ + bool hasoids; /* does it have OIDs? */ + uint32 frozenxid; /* table's relfrozenxid */ + uint32 minmxid; /* table's relminmxid */ + Oid toast_oid; /* toast table's OID, or 0 if none */ + uint32 toast_frozenxid; /* toast table's relfrozenxid, if any */ + uint32 toast_minmxid; /* toast table's relminmxid */ + int ncheck; /* # of CHECK expressions */ + Oid reltype; /* OID of table's composite type, if any */ + Oid reloftype; /* underlying type for typed table */ + Oid foreign_server; /* foreign server oid, if applicable */ + /* these two are set only if table is a sequence owned by a column: */ + Oid owning_tab; /* OID of table owning sequence */ + int owning_col; /* attr # of column owning sequence */ + bool is_identity_sequence; + int32 relpages; /* table's size in pages (from pg_class) */ + int toastpages; /* toast table's size in pages, if any */ + + bool interesting; /* true if need to collect more data */ + bool dummy_view; /* view's real definition must be postponed */ + bool postponed_def; /* matview must be postponed into post-data */ + bool ispartition; /* is table a partition? */ + bool unsafe_partitions; /* is it an unsafe partitioned table? */ + + int numParents; /* number of (immediate) parent tables */ + struct _tableInfo **parents; /* TableInfos of immediate parents */ + + /* + * These fields are computed only if we decide the table is interesting + * (it's either a table to dump, or a direct parent of a dumpable table). + */ + int numatts; /* number of attributes */ + char **attnames; /* the attribute names */ + char **atttypnames; /* attribute type names */ + int *attstattarget; /* attribute statistics targets */ + char *attstorage; /* attribute storage scheme */ + char *typstorage; /* type storage scheme */ + bool *attisdropped; /* true if attr is dropped; don't dump it */ + char *attidentity; + char *attgenerated; + int *attlen; /* attribute length, used by binary_upgrade */ + char *attalign; /* attribute align, used by binary_upgrade */ + bool *attislocal; /* true if attr has local definition */ + char **attoptions; /* per-attribute options */ + Oid *attcollation; /* per-attribute collation selection */ + char *attcompression; /* per-attribute compression method */ + char **attfdwoptions; /* per-attribute fdw options */ + char **attmissingval; /* per attribute missing value */ + char **notnull_constrs; /* NOT NULL constraint names. If null, + * there isn't one on this column. If + * empty string, unnamed constraint + * (pre-v17) */ + char **notnull_comment; /* comment thereof */ + bool *notnull_invalid; /* true for NOT NULL NOT VALID */ + bool *notnull_noinh; /* NOT NULL is NO INHERIT */ + bool *notnull_islocal; /* true if NOT NULL has local definition */ + struct _attrDefInfo **attrdefs; /* DEFAULT expressions */ + struct _constraintInfo *checkexprs; /* CHECK constraints */ + struct _relStatsInfo *stats; /* only set for matviews */ + bool needs_override; /* has GENERATED ALWAYS AS IDENTITY */ + char *amname; /* relation access method */ + + /* + * Stuff computed only for dumpable tables. + */ + int numIndexes; /* number of indexes */ + struct _indxInfo *indexes; /* indexes */ + struct _tableDataInfo *dataObj; /* TableDataInfo, if dumping its data */ + int numTriggers; /* number of triggers for table */ + struct _triggerInfo *triggers; /* array of TriggerInfo structs */ +} TableInfo; + +typedef struct _tableAttachInfo +{ + DumpableObject dobj; + TableInfo *parentTbl; /* link to partitioned table */ + TableInfo *partitionTbl; /* link to partition */ +} TableAttachInfo; + +typedef struct _attrDefInfo +{ + DumpableObject dobj; /* note: dobj.name is name of table */ + TableInfo *adtable; /* link to table of attribute */ + int adnum; + char *adef_expr; /* decompiled DEFAULT expression */ + bool separate; /* true if must dump as separate item */ +} AttrDefInfo; + +typedef struct _tableDataInfo +{ + DumpableObject dobj; + TableInfo *tdtable; /* link to table to dump */ + char *filtercond; /* WHERE condition to limit rows dumped */ +} TableDataInfo; + +typedef struct _indxInfo +{ + DumpableObject dobj; + TableInfo *indextable; /* link to table the index is for */ + char *indexdef; + char *tablespace; /* tablespace in which index is stored */ + char *indreloptions; /* options specified by WITH (...) */ + char *indstatcols; /* column numbers with statistics */ + char *indstatvals; /* statistic values for columns */ + int indnkeyattrs; /* number of index key attributes */ + int indnattrs; /* total number of index attributes */ + Oid *indkeys; /* In spite of the name 'indkeys' this field + * contains both key and nonkey attributes */ + bool indisclustered; + bool indisreplident; + bool indnullsnotdistinct; + Oid parentidx; /* if a partition, parent index OID */ + SimplePtrList partattaches; /* if partitioned, partition attach objects */ + + /* if there is an associated constraint object, its dumpId: */ + DumpId indexconstraint; +} IndxInfo; + +typedef struct _indexAttachInfo +{ + DumpableObject dobj; + IndxInfo *parentIdx; /* link to index on partitioned table */ + IndxInfo *partitionIdx; /* link to index on partition */ +} IndexAttachInfo; + +typedef struct _relStatsInfo +{ + DumpableObject dobj; + Oid relid; + int32 relpages; + char *reltuples; + int32 relallvisible; + int32 relallfrozen; + char relkind; /* 'r', 'm', 'i', etc */ + + /* + * indAttNames/nindAttNames are populated only if the relation is an index + * with at least one expression column; we don't need them otherwise. + */ + char **indAttNames; /* attnames of the index, in order */ + int32 nindAttNames; /* number of attnames stored (can be 0) */ + teSection section; /* stats may appear in data or post-data */ +} RelStatsInfo; + +typedef struct _statsExtInfo +{ + DumpableObject dobj; + const char *rolname; /* owner */ + TableInfo *stattable; /* link to table the stats are for */ + int stattarget; /* statistics target */ +} StatsExtInfo; + +typedef struct _ruleInfo +{ + DumpableObject dobj; + TableInfo *ruletable; /* link to table the rule is for */ + char ev_type; + bool is_instead; + char ev_enabled; + bool separate; /* true if must dump as separate item */ + /* separate is always true for non-ON SELECT rules */ +} RuleInfo; + +typedef struct _triggerInfo +{ + DumpableObject dobj; + TableInfo *tgtable; /* link to table the trigger is for */ + char tgenabled; + bool tgispartition; + char *tgdef; +} TriggerInfo; + +typedef struct _evttriggerInfo +{ + DumpableObject dobj; + char *evtname; + char *evtevent; + const char *evtowner; + char *evttags; + char *evtfname; + char evtenabled; +} EventTriggerInfo; + +/* + * struct ConstraintInfo is used for all constraint types. However we + * use a different objType for foreign key constraints, to make it easier + * to sort them the way we want. + * + * Not-null constraints don't need this, unless they are NOT VALID. + * + * Note: condeferrable and condeferred are currently only valid for + * unique/primary-key constraints. Otherwise that info is in condef. + */ +typedef struct _constraintInfo +{ + DumpableObject dobj; + TableInfo *contable; /* NULL if domain constraint */ + TypeInfo *condomain; /* NULL if table constraint */ + char contype; + char *condef; /* definition, if CHECK or FOREIGN KEY */ + Oid confrelid; /* referenced table, if FOREIGN KEY */ + DumpId conindex; /* identifies associated index if any */ + bool condeferrable; /* true if constraint is DEFERRABLE */ + bool condeferred; /* true if constraint is INITIALLY DEFERRED */ + bool conperiod; /* true if the constraint is WITHOUT OVERLAPS */ + bool conislocal; /* true if constraint has local definition */ + bool separate; /* true if must dump as separate item */ +} ConstraintInfo; + +typedef struct _procLangInfo +{ + DumpableObject dobj; + DumpableAcl dacl; + bool lanpltrusted; + Oid lanplcallfoid; + Oid laninline; + Oid lanvalidator; + const char *lanowner; +} ProcLangInfo; + +typedef struct _castInfo +{ + DumpableObject dobj; + Oid castsource; + Oid casttarget; + Oid castfunc; + char castcontext; + char castmethod; +} CastInfo; + +typedef struct _transformInfo +{ + DumpableObject dobj; + Oid trftype; + Oid trflang; + Oid trffromsql; + Oid trftosql; +} TransformInfo; + +/* InhInfo isn't a DumpableObject, just temporary state */ +typedef struct _inhInfo +{ + Oid inhrelid; /* OID of a child table */ + Oid inhparent; /* OID of its parent */ +} InhInfo; + +typedef struct _prsInfo +{ + DumpableObject dobj; + Oid prsstart; + Oid prstoken; + Oid prsend; + Oid prsheadline; + Oid prslextype; +} TSParserInfo; + +typedef struct _dictInfo +{ + DumpableObject dobj; + const char *rolname; + Oid dicttemplate; + char *dictinitoption; +} TSDictInfo; + +typedef struct _tmplInfo +{ + DumpableObject dobj; + Oid tmplinit; + Oid tmpllexize; +} TSTemplateInfo; + +typedef struct _cfgInfo +{ + DumpableObject dobj; + const char *rolname; + Oid cfgparser; +} TSConfigInfo; + +typedef struct _fdwInfo +{ + DumpableObject dobj; + DumpableAcl dacl; + const char *rolname; + char *fdwhandler; + char *fdwvalidator; + char *fdwconnection; + char *fdwoptions; +} FdwInfo; + +typedef struct _foreignServerInfo +{ + DumpableObject dobj; + DumpableAcl dacl; + const char *rolname; + Oid srvfdw; + char *srvtype; + char *srvversion; + char *srvoptions; +} ForeignServerInfo; + +typedef struct _defaultACLInfo +{ + DumpableObject dobj; + DumpableAcl dacl; + const char *defaclrole; + char defaclobjtype; +} DefaultACLInfo; + +/* + * LoInfo represents a group of large objects (blobs) that share the same + * owner and ACL setting. dobj.components has the DUMP_COMPONENT_COMMENT bit + * set if any blob in the group has a comment; similarly for sec labels. + * If there are many blobs with the same owner/ACL, we can divide them into + * multiple LoInfo groups, which will each spawn a BLOB METADATA and a BLOBS + * (data) TOC entry. This allows more parallelism during restore. + */ +typedef struct _loInfo +{ + DumpableObject dobj; + DumpableAcl dacl; + const char *rolname; + int numlos; + Oid looids[FLEXIBLE_ARRAY_MEMBER]; +} LoInfo; + +/* + * The PolicyInfo struct is used to represent policies on a table and + * to indicate if a table has RLS enabled (ENABLE ROW SECURITY). If + * polname is NULL, then the record indicates ENABLE ROW SECURITY, while if + * it's non-NULL then this is a regular policy definition. + */ +typedef struct _policyInfo +{ + DumpableObject dobj; + TableInfo *poltable; + char *polname; /* null indicates RLS is enabled on rel */ + char polcmd; + bool polpermissive; + char *polroles; + char *polqual; + char *polwithcheck; +} PolicyInfo; + +/* + * The PublicationInfo struct is used to represent publications. + */ +typedef struct _PublicationInfo +{ + DumpableObject dobj; + const char *rolname; + bool puballtables; + bool puballsequences; + bool pubinsert; + bool pubupdate; + bool pubdelete; + bool pubtruncate; + bool pubviaroot; + PublishGencolsType pubgencols_type; + SimplePtrList except_tables; +} PublicationInfo; + +/* + * The PublicationRelInfo struct is used to represent publication table + * mapping. + */ +typedef struct _PublicationRelInfo +{ + DumpableObject dobj; + PublicationInfo *publication; + TableInfo *pubtable; + char *pubrelqual; + char *pubrattrs; +} PublicationRelInfo; + +/* + * The PublicationSchemaInfo struct is used to represent publication schema + * mapping. + */ +typedef struct _PublicationSchemaInfo +{ + DumpableObject dobj; + PublicationInfo *publication; + NamespaceInfo *pubschema; +} PublicationSchemaInfo; + +/* + * The SubscriptionInfo struct is used to represent subscription. + */ +typedef struct _SubscriptionInfo +{ + DumpableObject dobj; + const char *rolname; + bool subenabled; + bool subbinary; + char substream; + char subtwophasestate; + bool subdisableonerr; + bool subpasswordrequired; + bool subrunasowner; + bool subfailover; + bool subretaindeadtuples; + int submaxretention; + char *subservername; + char *subconninfo; + char *subslotname; + char *subsynccommit; + char *subwalrcvtimeout; + char *subpublications; + char *suborigin; + char *suboriginremotelsn; +} SubscriptionInfo; + +/* + * The SubRelInfo struct is used to represent a subscription relation. + * + * XXX Currently, the subscription tables are added to the subscription after + * enabling the subscription in binary-upgrade mode. As the apply workers will + * not be started in binary_upgrade mode the ordering of enable subscription + * does not matter. The order of adding the subscription tables to the + * subscription and enabling the subscription should be taken care of if this + * feature will be supported in a non-binary-upgrade mode in the future. + */ +typedef struct _SubRelInfo +{ + DumpableObject dobj; + SubscriptionInfo *subinfo; + TableInfo *tblinfo; + char srsubstate; + char *srsublsn; +} SubRelInfo; + +/* + * common utility functions + */ + +extern TableInfo *getSchemaData(Archive *fout, int *numTablesPtr); + +extern void AssignDumpId(DumpableObject *dobj); +extern void recordAdditionalCatalogID(CatalogId catId, DumpableObject *dobj); +extern DumpId createDumpId(void); +extern DumpId getMaxDumpId(void); +extern DumpableObject *findObjectByDumpId(DumpId dumpId); +extern DumpableObject *findObjectByCatalogId(CatalogId catalogId); +extern void getDumpableObjects(DumpableObject ***objs, int *numObjs); + +extern void addObjectDependency(DumpableObject *dobj, DumpId refId); +extern void removeObjectDependency(DumpableObject *dobj, DumpId refId); + +extern TableInfo *findTableByOid(Oid oid); +extern TypeInfo *findTypeByOid(Oid oid); +extern FuncInfo *findFuncByOid(Oid oid); +extern OprInfo *findOprByOid(Oid oid); +extern AccessMethodInfo *findAccessMethodByOid(Oid oid); +extern CollInfo *findCollationByOid(Oid oid); +extern NamespaceInfo *findNamespaceByOid(Oid oid); +extern ExtensionInfo *findExtensionByOid(Oid oid); +extern PublicationInfo *findPublicationByOid(Oid oid); +extern SubscriptionInfo *findSubscriptionByOid(Oid oid); + +extern void recordExtensionMembership(CatalogId catId, ExtensionInfo *ext); +extern ExtensionInfo *findOwningExtension(CatalogId catalogId); + +extern void parseOidArray(const char *str, Oid *array, int arraysize); + +extern void sortDumpableObjects(DumpableObject **objs, int numObjs, + DumpId preBoundaryId, DumpId postBoundaryId); +extern void sortDumpableObjectsByTypeName(DumpableObject **objs, int numObjs); + +/* + * version specific routines + */ +extern void getNamespaces(Archive *fout); +extern ExtensionInfo *getExtensions(Archive *fout, int *numExtensions); +extern void getTypes(Archive *fout); +extern void getFuncs(Archive *fout); +extern void getAggregates(Archive *fout); +extern void getOperators(Archive *fout); +extern void getAccessMethods(Archive *fout); +extern void getOpclasses(Archive *fout); +extern void getOpfamilies(Archive *fout); +extern void getCollations(Archive *fout); +extern void getConversions(Archive *fout); +extern TableInfo *getTables(Archive *fout, int *numTables); +extern void getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables); +extern InhInfo *getInherits(Archive *fout, int *numInherits); +extern void getPartitioningInfo(Archive *fout); +extern void getIndexes(Archive *fout, TableInfo tblinfo[], int numTables); +extern void getExtendedStatistics(Archive *fout); +extern void getConstraints(Archive *fout, TableInfo tblinfo[], int numTables); +extern void getRules(Archive *fout); +extern void getTriggers(Archive *fout, TableInfo tblinfo[], int numTables); +extern void getProcLangs(Archive *fout); +extern void getCasts(Archive *fout); +extern void getTransforms(Archive *fout); +extern void getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables); +extern bool shouldPrintColumn(const DumpOptions *dopt, const TableInfo *tbinfo, int colno); +extern void getTSParsers(Archive *fout); +extern void getTSDictionaries(Archive *fout); +extern void getTSTemplates(Archive *fout); +extern void getTSConfigurations(Archive *fout); +extern void getForeignDataWrappers(Archive *fout); +extern void getForeignServers(Archive *fout); +extern void getDefaultACLs(Archive *fout); +extern void getExtensionMembership(Archive *fout, ExtensionInfo extinfo[], + int numExtensions); +extern void processExtensionTables(Archive *fout, ExtensionInfo extinfo[], + int numExtensions); +extern void getEventTriggers(Archive *fout); +extern void getPolicies(Archive *fout, TableInfo tblinfo[], int numTables); +extern void getPublications(Archive *fout); +extern void getPublicationNamespaces(Archive *fout); +extern void getPublicationTables(Archive *fout, TableInfo tblinfo[], + int numTables); +extern void getSubscriptions(Archive *fout); +extern void getSubscriptionRelations(Archive *fout); + +#endif /* PG_DUMP_H */ diff --git a/src/compat/19/pg_dump/pg_dump_sort.c b/src/compat/19/pg_dump/pg_dump_sort.c new file mode 100644 index 00000000..03e5c1c1 --- /dev/null +++ b/src/compat/19/pg_dump/pg_dump_sort.c @@ -0,0 +1,1780 @@ +/*------------------------------------------------------------------------- + * + * pg_dump_sort.c + * Sort the items of a dump into a safe order for dumping + * + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/bin/pg_dump/pg_dump_sort.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include "catalog/pg_class_d.h" +#include "common/int.h" +#include "lib/binaryheap.h" +#include "pg_backup_utils.h" +#include "pg_dump.h" + +/* + * Sort priority for database object types. + * Objects are sorted by type, and within a type by name. + * + * Triggers, event triggers, and materialized views are intentionally sorted + * late. Triggers must be restored after all data modifications, so that + * they don't interfere with loading data. Event triggers are restored + * next-to-last so that they don't interfere with object creations of any + * kind. Matview refreshes are last because they should execute in the + * database's normal state (e.g., they must come after all ACLs are restored; + * also, if they choose to look at system catalogs, they should see the final + * restore state). If you think to change this, see also the RestorePass + * mechanism in pg_backup_archiver.c. + * + * On the other hand, casts are intentionally sorted earlier than you might + * expect; logically they should come after functions, since they usually + * depend on those. This works around the backend's habit of recording + * views that use casts as dependent on the cast's underlying function. + * We initially sort casts first, and then any functions used by casts + * will be hoisted above the casts, and in turn views that those functions + * depend on will be hoisted above the functions. But views not used that + * way won't be hoisted. + * + * NOTE: object-type priorities must match the section assignments made in + * pg_dump.c; that is, PRE_DATA objects must sort before DO_PRE_DATA_BOUNDARY, + * POST_DATA objects must sort after DO_POST_DATA_BOUNDARY, and DATA objects + * must sort between them. + */ + +/* This enum lists the priority levels in order */ +enum dbObjectTypePriorities +{ + PRIO_NAMESPACE = 1, + PRIO_PROCLANG, + PRIO_COLLATION, + PRIO_TRANSFORM, + PRIO_EXTENSION, + PRIO_TYPE, /* used for DO_TYPE and DO_SHELL_TYPE */ + PRIO_CAST, + PRIO_FUNC, + PRIO_AGG, + PRIO_ACCESS_METHOD, + PRIO_OPERATOR, + PRIO_OPFAMILY, /* used for DO_OPFAMILY and DO_OPCLASS */ + PRIO_CONVERSION, + PRIO_TSPARSER, + PRIO_TSTEMPLATE, + PRIO_TSDICT, + PRIO_TSCONFIG, + PRIO_FDW, + PRIO_FOREIGN_SERVER, + PRIO_TABLE, + PRIO_TABLE_ATTACH, + PRIO_DUMMY_TYPE, + PRIO_ATTRDEF, + PRIO_PRE_DATA_BOUNDARY, /* boundary! */ + PRIO_TABLE_DATA, + PRIO_SEQUENCE_SET, + PRIO_LARGE_OBJECT, + PRIO_LARGE_OBJECT_DATA, + PRIO_STATISTICS_DATA_DATA, + PRIO_POST_DATA_BOUNDARY, /* boundary! */ + PRIO_CONSTRAINT, + PRIO_INDEX, + PRIO_INDEX_ATTACH, + PRIO_STATSEXT, + PRIO_RULE, + PRIO_TRIGGER, + PRIO_FK_CONSTRAINT, + PRIO_POLICY, + PRIO_PUBLICATION, + PRIO_PUBLICATION_REL, + PRIO_PUBLICATION_TABLE_IN_SCHEMA, + PRIO_SUBSCRIPTION, + PRIO_SUBSCRIPTION_REL, + PRIO_DEFAULT_ACL, /* done in ACL pass */ + PRIO_EVENT_TRIGGER, /* must be next to last! */ + PRIO_REFRESH_MATVIEW /* must be last! */ +}; + +/* This table is indexed by enum DumpableObjectType */ +static const int dbObjectTypePriority[] = +{ + [DO_NAMESPACE] = PRIO_NAMESPACE, + [DO_EXTENSION] = PRIO_EXTENSION, + [DO_TYPE] = PRIO_TYPE, + [DO_SHELL_TYPE] = PRIO_TYPE, + [DO_FUNC] = PRIO_FUNC, + [DO_AGG] = PRIO_AGG, + [DO_OPERATOR] = PRIO_OPERATOR, + [DO_ACCESS_METHOD] = PRIO_ACCESS_METHOD, + [DO_OPCLASS] = PRIO_OPFAMILY, + [DO_OPFAMILY] = PRIO_OPFAMILY, + [DO_COLLATION] = PRIO_COLLATION, + [DO_CONVERSION] = PRIO_CONVERSION, + [DO_TABLE] = PRIO_TABLE, + [DO_TABLE_ATTACH] = PRIO_TABLE_ATTACH, + [DO_ATTRDEF] = PRIO_ATTRDEF, + [DO_INDEX] = PRIO_INDEX, + [DO_INDEX_ATTACH] = PRIO_INDEX_ATTACH, + [DO_STATSEXT] = PRIO_STATSEXT, + [DO_RULE] = PRIO_RULE, + [DO_TRIGGER] = PRIO_TRIGGER, + [DO_CONSTRAINT] = PRIO_CONSTRAINT, + [DO_FK_CONSTRAINT] = PRIO_FK_CONSTRAINT, + [DO_PROCLANG] = PRIO_PROCLANG, + [DO_CAST] = PRIO_CAST, + [DO_TABLE_DATA] = PRIO_TABLE_DATA, + [DO_SEQUENCE_SET] = PRIO_SEQUENCE_SET, + [DO_DUMMY_TYPE] = PRIO_DUMMY_TYPE, + [DO_TSPARSER] = PRIO_TSPARSER, + [DO_TSDICT] = PRIO_TSDICT, + [DO_TSTEMPLATE] = PRIO_TSTEMPLATE, + [DO_TSCONFIG] = PRIO_TSCONFIG, + [DO_FDW] = PRIO_FDW, + [DO_FOREIGN_SERVER] = PRIO_FOREIGN_SERVER, + [DO_DEFAULT_ACL] = PRIO_DEFAULT_ACL, + [DO_TRANSFORM] = PRIO_TRANSFORM, + [DO_LARGE_OBJECT] = PRIO_LARGE_OBJECT, + [DO_LARGE_OBJECT_DATA] = PRIO_LARGE_OBJECT_DATA, + [DO_PRE_DATA_BOUNDARY] = PRIO_PRE_DATA_BOUNDARY, + [DO_POST_DATA_BOUNDARY] = PRIO_POST_DATA_BOUNDARY, + [DO_EVENT_TRIGGER] = PRIO_EVENT_TRIGGER, + [DO_REFRESH_MATVIEW] = PRIO_REFRESH_MATVIEW, + [DO_POLICY] = PRIO_POLICY, + [DO_PUBLICATION] = PRIO_PUBLICATION, + [DO_PUBLICATION_REL] = PRIO_PUBLICATION_REL, + [DO_PUBLICATION_TABLE_IN_SCHEMA] = PRIO_PUBLICATION_TABLE_IN_SCHEMA, + [DO_REL_STATS] = PRIO_STATISTICS_DATA_DATA, + [DO_SUBSCRIPTION] = PRIO_SUBSCRIPTION, + [DO_SUBSCRIPTION_REL] = PRIO_SUBSCRIPTION_REL, +}; + +StaticAssertDecl(lengthof(dbObjectTypePriority) == NUM_DUMPABLE_OBJECT_TYPES, + "array length mismatch"); + +static DumpId preDataBoundId; +static DumpId postDataBoundId; + + +static int DOTypeNameCompare(const void *p1, const void *p2); +static int pgTypeNameCompare(Oid typid1, Oid typid2); +static int accessMethodNameCompare(Oid am1, Oid am2); +static bool TopoSort(DumpableObject **objs, + int numObjs, + DumpableObject **ordering, + int *nOrdering); +static void findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs); +static int findLoop(DumpableObject *obj, + DumpId startPoint, + bool *processed, + DumpId *searchFailed, + DumpableObject **workspace, + int depth); +static void repairDependencyLoop(DumpableObject **loop, + int nLoop); +static void describeDumpableObject(DumpableObject *obj, + char *buf, int bufsize); +static int int_cmp(void *a, void *b, void *arg); + + +/* + * Sort the given objects into a type/name-based ordering + * + * Normally this is just the starting point for the dependency-based + * ordering. + */ +void +sortDumpableObjectsByTypeName(DumpableObject **objs, int numObjs) +{ + if (numObjs > 1) + qsort(objs, numObjs, sizeof(DumpableObject *), + DOTypeNameCompare); +} + +static int +DOTypeNameCompare(const void *p1, const void *p2) +{ + DumpableObject *obj1 = *(DumpableObject *const *) p1; + DumpableObject *obj2 = *(DumpableObject *const *) p2; + int cmpval; + + /* Sort by type's priority */ + cmpval = dbObjectTypePriority[obj1->objType] - + dbObjectTypePriority[obj2->objType]; + + if (cmpval != 0) + return cmpval; + + /* + * Sort by namespace. Typically, all objects of the same priority would + * either have or not have a namespace link, but there are exceptions. + * Sort NULL namespace after non-NULL in such cases. + */ + if (obj1->namespace) + { + if (obj2->namespace) + { + cmpval = strcmp(obj1->namespace->dobj.name, + obj2->namespace->dobj.name); + if (cmpval != 0) + return cmpval; + } + else + return -1; + } + else if (obj2->namespace) + return 1; + + /* + * Sort by name. With a few exceptions, names here are single catalog + * columns. To get a fuller picture, grep pg_dump.c for "dobj.name = ". + * Names here don't match "Name:" in plain format output, which is a + * _tocEntry.tag. For example, DumpableObject.name of a constraint is + * pg_constraint.conname, but _tocEntry.tag of a constraint is relname and + * conname joined with a space. + */ + cmpval = strcmp(obj1->name, obj2->name); + if (cmpval != 0) + return cmpval; + + /* + * Sort by type. This helps types that share a type priority without + * sharing a unique name constraint, e.g. opclass and opfamily. + */ + cmpval = obj1->objType - obj2->objType; + if (cmpval != 0) + return cmpval; + + /* + * To have a stable sort order, break ties for some object types. Most + * catalogs have a natural key, e.g. pg_proc_proname_args_nsp_index. Where + * the above "namespace" and "name" comparisons don't cover all natural + * key columns, compare the rest here. + * + * The natural key usually refers to other catalogs by surrogate keys. + * Hence, this translates each of those references to the natural key of + * the referenced catalog. That may descend through multiple levels of + * catalog references. For example, to sort by pg_proc.proargtypes, + * descend to each pg_type and then further to its pg_namespace, for an + * overall sort by (nspname, typname). + */ + if (obj1->objType == DO_FUNC || obj1->objType == DO_AGG) + { + FuncInfo *fobj1 = *(FuncInfo *const *) p1; + FuncInfo *fobj2 = *(FuncInfo *const *) p2; + int i; + + /* Sort by number of arguments, then argument type names */ + cmpval = fobj1->nargs - fobj2->nargs; + if (cmpval != 0) + return cmpval; + for (i = 0; i < fobj1->nargs; i++) + { + cmpval = pgTypeNameCompare(fobj1->argtypes[i], + fobj2->argtypes[i]); + if (cmpval != 0) + return cmpval; + } + } + else if (obj1->objType == DO_OPERATOR) + { + OprInfo *oobj1 = *(OprInfo *const *) p1; + OprInfo *oobj2 = *(OprInfo *const *) p2; + + /* oprkind is 'l', 'r', or 'b'; this sorts prefix, postfix, infix */ + cmpval = (oobj2->oprkind - oobj1->oprkind); + if (cmpval != 0) + return cmpval; + /* Within an oprkind, sort by argument type names */ + cmpval = pgTypeNameCompare(oobj1->oprleft, oobj2->oprleft); + if (cmpval != 0) + return cmpval; + cmpval = pgTypeNameCompare(oobj1->oprright, oobj2->oprright); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_OPCLASS) + { + OpclassInfo *opcobj1 = *(OpclassInfo *const *) p1; + OpclassInfo *opcobj2 = *(OpclassInfo *const *) p2; + + /* Sort by access method name, per pg_opclass_am_name_nsp_index */ + cmpval = accessMethodNameCompare(opcobj1->opcmethod, + opcobj2->opcmethod); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_OPFAMILY) + { + OpfamilyInfo *opfobj1 = *(OpfamilyInfo *const *) p1; + OpfamilyInfo *opfobj2 = *(OpfamilyInfo *const *) p2; + + /* Sort by access method name, per pg_opfamily_am_name_nsp_index */ + cmpval = accessMethodNameCompare(opfobj1->opfmethod, + opfobj2->opfmethod); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_COLLATION) + { + CollInfo *cobj1 = *(CollInfo *const *) p1; + CollInfo *cobj2 = *(CollInfo *const *) p2; + + /* + * Sort by encoding, per pg_collation_name_enc_nsp_index. Technically, + * this is not necessary, because wherever this changes dump order, + * restoring the dump fails anyway. CREATE COLLATION can't create a + * tie for this to break, because it imposes restrictions to make + * (nspname, collname) uniquely identify a collation within a given + * DatabaseEncoding. While pg_import_system_collations() can create a + * tie, pg_dump+restore fails after + * pg_import_system_collations('my_schema') does so. However, there's + * little to gain by ignoring one natural key column on the basis of + * those limitations elsewhere, so respect the full natural key like + * we do for other object types. + */ + cmpval = cobj1->collencoding - cobj2->collencoding; + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_ATTRDEF) + { + AttrDefInfo *adobj1 = *(AttrDefInfo *const *) p1; + AttrDefInfo *adobj2 = *(AttrDefInfo *const *) p2; + + /* Sort by attribute number */ + cmpval = (adobj1->adnum - adobj2->adnum); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_POLICY) + { + PolicyInfo *pobj1 = *(PolicyInfo *const *) p1; + PolicyInfo *pobj2 = *(PolicyInfo *const *) p2; + + /* Sort by table name (table namespace was considered already) */ + cmpval = strcmp(pobj1->poltable->dobj.name, + pobj2->poltable->dobj.name); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_RULE) + { + RuleInfo *robj1 = *(RuleInfo *const *) p1; + RuleInfo *robj2 = *(RuleInfo *const *) p2; + + /* Sort by table name (table namespace was considered already) */ + cmpval = strcmp(robj1->ruletable->dobj.name, + robj2->ruletable->dobj.name); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_TRIGGER) + { + TriggerInfo *tobj1 = *(TriggerInfo *const *) p1; + TriggerInfo *tobj2 = *(TriggerInfo *const *) p2; + + /* Sort by table name (table namespace was considered already) */ + cmpval = strcmp(tobj1->tgtable->dobj.name, + tobj2->tgtable->dobj.name); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_CONSTRAINT || + obj1->objType == DO_FK_CONSTRAINT) + { + ConstraintInfo *robj1 = *(ConstraintInfo *const *) p1; + ConstraintInfo *robj2 = *(ConstraintInfo *const *) p2; + + /* + * Sort domain constraints before table constraints, for consistency + * with our decision to sort CREATE DOMAIN before CREATE TABLE. + */ + if (robj1->condomain) + { + if (robj2->condomain) + { + /* Sort by domain name (domain namespace was considered) */ + cmpval = strcmp(robj1->condomain->dobj.name, + robj2->condomain->dobj.name); + if (cmpval != 0) + return cmpval; + } + else + return PRIO_TYPE - PRIO_TABLE; + } + else if (robj2->condomain) + return PRIO_TABLE - PRIO_TYPE; + else + { + /* Sort by table name (table namespace was considered already) */ + cmpval = strcmp(robj1->contable->dobj.name, + robj2->contable->dobj.name); + if (cmpval != 0) + return cmpval; + } + } + else if (obj1->objType == DO_DEFAULT_ACL) + { + DefaultACLInfo *daclobj1 = *(DefaultACLInfo *const *) p1; + DefaultACLInfo *daclobj2 = *(DefaultACLInfo *const *) p2; + + /* + * Sort by defaclrole, per pg_default_acl_role_nsp_obj_index. The + * (namespace, name) match (defaclnamespace, defaclobjtype). + */ + cmpval = strcmp(daclobj1->defaclrole, daclobj2->defaclrole); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_PUBLICATION_REL) + { + PublicationRelInfo *probj1 = *(PublicationRelInfo *const *) p1; + PublicationRelInfo *probj2 = *(PublicationRelInfo *const *) p2; + + /* Sort by publication name, since (namespace, name) match the rel */ + cmpval = strcmp(probj1->publication->dobj.name, + probj2->publication->dobj.name); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_PUBLICATION_TABLE_IN_SCHEMA) + { + PublicationSchemaInfo *psobj1 = *(PublicationSchemaInfo *const *) p1; + PublicationSchemaInfo *psobj2 = *(PublicationSchemaInfo *const *) p2; + + /* Sort by publication name, since ->name is just nspname */ + cmpval = strcmp(psobj1->publication->dobj.name, + psobj2->publication->dobj.name); + if (cmpval != 0) + return cmpval; + } + else if (obj1->objType == DO_SUBSCRIPTION_REL) + { + SubRelInfo *srobj1 = *(SubRelInfo *const *) p1; + SubRelInfo *srobj2 = *(SubRelInfo *const *) p2; + + /* Sort by subscription name, since (namespace, name) match the rel */ + cmpval = strcmp(srobj1->subinfo->dobj.name, + srobj2->subinfo->dobj.name); + if (cmpval != 0) + return cmpval; + } + + /* + * Shouldn't get here except after catalog corruption, but if we do, sort + * by OID. This may make logically-identical databases differ in the + * order of objects in dump output. Users will get spurious schema diffs. + * Expect flaky failures of 002_pg_upgrade.pl test 'dump outputs from + * original and restored regression databases match' if the regression + * database contains objects allowing that test to reach here. That's a + * consequence of the test using "pg_restore -j", which doesn't fully + * constrain OID assignment order. + */ + Assert(false); + return oidcmp(obj1->catId.oid, obj2->catId.oid); +} + +/* Compare two OID-identified pg_type values by nspname, then by typname. */ +static int +pgTypeNameCompare(Oid typid1, Oid typid2) +{ + TypeInfo *typobj1; + TypeInfo *typobj2; + int cmpval; + + if (typid1 == typid2) + return 0; + + typobj1 = findTypeByOid(typid1); + typobj2 = findTypeByOid(typid2); + + if (!typobj1 || !typobj2) + { + /* + * getTypes() didn't find some OID. Assume catalog corruption, e.g. + * an oprright value without the corresponding OID in a pg_type row. + * Report as "equal", so the caller uses the next available basis for + * comparison, e.g. the next function argument. + * + * Unary operators have InvalidOid in oprleft (if oprkind='r') or in + * oprright (if oprkind='l'). Caller already sorted by oprkind, + * calling us only for like-kind operators. Hence, "typid1 == typid2" + * took care of InvalidOid. (v14 removed postfix operator support. + * Hence, when dumping from v14+, only oprleft can be InvalidOid.) + */ + Assert(false); + return 0; + } + + if (!typobj1->dobj.namespace || !typobj2->dobj.namespace) + Assert(false); /* catalog corruption */ + else + { + cmpval = strcmp(typobj1->dobj.namespace->dobj.name, + typobj2->dobj.namespace->dobj.name); + if (cmpval != 0) + return cmpval; + } + return strcmp(typobj1->dobj.name, typobj2->dobj.name); +} + +/* Compare two OID-identified pg_am values by amname. */ +static int +accessMethodNameCompare(Oid am1, Oid am2) +{ + AccessMethodInfo *amobj1; + AccessMethodInfo *amobj2; + + if (am1 == am2) + return 0; + + amobj1 = findAccessMethodByOid(am1); + amobj2 = findAccessMethodByOid(am2); + + if (!amobj1 || !amobj2) + { + /* catalog corruption: handle like pgTypeNameCompare() does */ + Assert(false); + return 0; + } + + return strcmp(amobj1->dobj.name, amobj2->dobj.name); +} + + +/* + * Sort the given objects into a safe dump order using dependency + * information (to the extent we have it available). + * + * The DumpIds of the PRE_DATA_BOUNDARY and POST_DATA_BOUNDARY objects are + * passed in separately, in case we need them during dependency loop repair. + */ +void +sortDumpableObjects(DumpableObject **objs, int numObjs, + DumpId preBoundaryId, DumpId postBoundaryId) +{ + DumpableObject **ordering; + int nOrdering; + + if (numObjs <= 0) /* can't happen anymore ... */ + return; + + /* + * Saving the boundary IDs in static variables is a bit grotty, but seems + * better than adding them to parameter lists of subsidiary functions. + */ + preDataBoundId = preBoundaryId; + postDataBoundId = postBoundaryId; + + ordering = pg_malloc_array(DumpableObject *, numObjs); + while (!TopoSort(objs, numObjs, ordering, &nOrdering)) + findDependencyLoops(ordering, nOrdering, numObjs); + + memcpy(objs, ordering, numObjs * sizeof(DumpableObject *)); + + free(ordering); +} + +/* + * TopoSort -- topological sort of a dump list + * + * Generate a re-ordering of the dump list that satisfies all the dependency + * constraints shown in the dump list. (Each such constraint is a fact of a + * partial ordering.) Minimize rearrangement of the list not needed to + * achieve the partial ordering. + * + * The input is the list of numObjs objects in objs[]. This list is not + * modified. + * + * Returns true if able to build an ordering that satisfies all the + * constraints, false if not (there are contradictory constraints). + * + * On success (true result), ordering[] is filled with a sorted array of + * DumpableObject pointers, of length equal to the input list length. + * + * On failure (false result), ordering[] is filled with an unsorted array of + * DumpableObject pointers of length *nOrdering, listing the objects that + * prevented the sort from being completed. In general, these objects either + * participate directly in a dependency cycle, or are depended on by objects + * that are in a cycle. (The latter objects are not actually problematic, + * but it takes further analysis to identify which are which.) + * + * The caller is responsible for allocating sufficient space at *ordering. + */ +static bool +TopoSort(DumpableObject **objs, + int numObjs, + DumpableObject **ordering, /* output argument */ + int *nOrdering) /* output argument */ +{ + DumpId maxDumpId = getMaxDumpId(); + binaryheap *pendingHeap; + int *beforeConstraints; + int *idMap; + DumpableObject *obj; + int i, + j, + k; + + /* + * This is basically the same algorithm shown for topological sorting in + * Knuth's Volume 1. However, we would like to minimize unnecessary + * rearrangement of the input ordering; that is, when we have a choice of + * which item to output next, we always want to take the one highest in + * the original list. Therefore, instead of maintaining an unordered + * linked list of items-ready-to-output as Knuth does, we maintain a heap + * of their item numbers, which we can use as a priority queue. This + * turns the algorithm from O(N) to O(N log N) because each insertion or + * removal of a heap item takes O(log N) time. However, that's still + * plenty fast enough for this application. + */ + + *nOrdering = numObjs; /* for success return */ + + /* Eliminate the null case */ + if (numObjs <= 0) + return true; + + /* Create workspace for the above-described heap */ + pendingHeap = binaryheap_allocate(numObjs, int_cmp, NULL); + + /* + * Scan the constraints, and for each item in the input, generate a count + * of the number of constraints that say it must be before something else. + * The count for the item with dumpId j is stored in beforeConstraints[j]. + * We also make a map showing the input-order index of the item with + * dumpId j. + */ + beforeConstraints = pg_malloc0_array(int, (maxDumpId + 1)); + idMap = pg_malloc_array(int, (maxDumpId + 1)); + for (i = 0; i < numObjs; i++) + { + obj = objs[i]; + j = obj->dumpId; + if (j <= 0 || j > maxDumpId) + pg_fatal("invalid dumpId %d", j); + idMap[j] = i; + for (j = 0; j < obj->nDeps; j++) + { + k = obj->dependencies[j]; + if (k <= 0 || k > maxDumpId) + pg_fatal("invalid dependency %d", k); + beforeConstraints[k]++; + } + } + + /* + * Now initialize the heap of items-ready-to-output by filling it with the + * indexes of items that already have beforeConstraints[id] == 0. + * + * We enter the indexes into pendingHeap in decreasing order so that the + * heap invariant is satisfied at the completion of this loop. This + * reduces the amount of work that binaryheap_build() must do. + */ + for (i = numObjs; --i >= 0;) + { + if (beforeConstraints[objs[i]->dumpId] == 0) + binaryheap_add_unordered(pendingHeap, (void *) (intptr_t) i); + } + binaryheap_build(pendingHeap); + + /*-------------------- + * Now emit objects, working backwards in the output list. At each step, + * we use the priority heap to select the last item that has no remaining + * before-constraints. We remove that item from the heap, output it to + * ordering[], and decrease the beforeConstraints count of each of the + * items it was constrained against. Whenever an item's beforeConstraints + * count is thereby decreased to zero, we insert it into the priority heap + * to show that it is a candidate to output. We are done when the heap + * becomes empty; if we have output every element then we succeeded, + * otherwise we failed. + * i = number of ordering[] entries left to output + * j = objs[] index of item we are outputting + * k = temp for scanning constraint list for item j + *-------------------- + */ + i = numObjs; + while (!binaryheap_empty(pendingHeap)) + { + /* Select object to output by removing largest heap member */ + j = (int) (intptr_t) binaryheap_remove_first(pendingHeap); + obj = objs[j]; + /* Output candidate to ordering[] */ + ordering[--i] = obj; + /* Update beforeConstraints counts of its predecessors */ + for (k = 0; k < obj->nDeps; k++) + { + int id = obj->dependencies[k]; + + if ((--beforeConstraints[id]) == 0) + binaryheap_add(pendingHeap, (void *) (intptr_t) idMap[id]); + } + } + + /* + * If we failed, report the objects that couldn't be output; these are the + * ones with beforeConstraints[] still nonzero. + */ + if (i != 0) + { + k = 0; + for (j = 1; j <= maxDumpId; j++) + { + if (beforeConstraints[j] != 0) + ordering[k++] = objs[idMap[j]]; + } + *nOrdering = k; + } + + /* Done */ + binaryheap_free(pendingHeap); + free(beforeConstraints); + free(idMap); + + return (i == 0); +} + +/* + * findDependencyLoops - identify loops in TopoSort's failure output, + * and pass each such loop to repairDependencyLoop() for action + * + * In general there may be many loops in the set of objects returned by + * TopoSort; for speed we should try to repair as many loops as we can + * before trying TopoSort again. We can safely repair loops that are + * disjoint (have no members in common); if we find overlapping loops + * then we repair only the first one found, because the action taken to + * repair the first might have repaired the other as well. (If not, + * we'll fix it on the next go-round.) + * + * objs[] lists the objects TopoSort couldn't sort + * nObjs is the number of such objects + * totObjs is the total number of objects in the universe + */ +static void +findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs) +{ + /* + * We use three data structures here: + * + * processed[] is a bool array indexed by dump ID, marking the objects + * already processed during this invocation of findDependencyLoops(). + * + * searchFailed[] is another array indexed by dump ID. searchFailed[j] is + * set to dump ID k if we have proven that there is no dependency path + * leading from object j back to start point k. This allows us to skip + * useless searching when there are multiple dependency paths from k to j, + * which is a common situation. We could use a simple bool array for + * this, but then we'd need to re-zero it for each start point, resulting + * in O(N^2) zeroing work. Using the start point's dump ID as the "true" + * value lets us skip clearing the array before we consider the next start + * point. + * + * workspace[] is an array of DumpableObject pointers, in which we try to + * build lists of objects constituting loops. We make workspace[] large + * enough to hold all the objects in TopoSort's output, which is huge + * overkill in most cases but could theoretically be necessary if there is + * a single dependency chain linking all the objects. + */ + bool *processed; + DumpId *searchFailed; + DumpableObject **workspace; + bool fixedloop; + int i; + + processed = pg_malloc0_array(bool, (getMaxDumpId() + 1)); + searchFailed = pg_malloc0_array(DumpId, (getMaxDumpId() + 1)); + workspace = pg_malloc_array(DumpableObject *, totObjs); + fixedloop = false; + + for (i = 0; i < nObjs; i++) + { + DumpableObject *obj = objs[i]; + int looplen; + int j; + + looplen = findLoop(obj, + obj->dumpId, + processed, + searchFailed, + workspace, + 0); + + if (looplen > 0) + { + /* Found a loop, repair it */ + repairDependencyLoop(workspace, looplen); + fixedloop = true; + /* Mark loop members as processed */ + for (j = 0; j < looplen; j++) + processed[workspace[j]->dumpId] = true; + } + else + { + /* + * There's no loop starting at this object, but mark it processed + * anyway. This is not necessary for correctness, but saves later + * invocations of findLoop() from uselessly chasing references to + * such an object. + */ + processed[obj->dumpId] = true; + } + } + + /* We'd better have fixed at least one loop */ + if (!fixedloop) + pg_fatal("could not identify dependency loop"); + + free(workspace); + free(searchFailed); + free(processed); +} + +/* + * Recursively search for a circular dependency loop that doesn't include + * any already-processed objects. + * + * obj: object we are examining now + * startPoint: dumpId of starting object for the hoped-for circular loop + * processed[]: flag array marking already-processed objects + * searchFailed[]: flag array marking already-unsuccessfully-visited objects + * workspace[]: work array in which we are building list of loop members + * depth: number of valid entries in workspace[] at call + * + * On success, the length of the loop is returned, and workspace[] is filled + * with pointers to the members of the loop. On failure, we return 0. + * + * Note: it is possible that the given starting object is a member of more + * than one cycle; if so, we will find an arbitrary one of the cycles. + */ +static int +findLoop(DumpableObject *obj, + DumpId startPoint, + bool *processed, + DumpId *searchFailed, + DumpableObject **workspace, + int depth) +{ + int i; + + /* + * Reject if obj is already processed. This test prevents us from finding + * loops that overlap previously-processed loops. + */ + if (processed[obj->dumpId]) + return 0; + + /* + * If we've already proven there is no path from this object back to the + * startPoint, forget it. + */ + if (searchFailed[obj->dumpId] == startPoint) + return 0; + + /* + * Reject if obj is already present in workspace. This test prevents us + * from going into infinite recursion if we are given a startPoint object + * that links to a cycle it's not a member of, and it guarantees that we + * can't overflow the allocated size of workspace[]. + */ + for (i = 0; i < depth; i++) + { + if (workspace[i] == obj) + return 0; + } + + /* + * Okay, tentatively add obj to workspace + */ + workspace[depth++] = obj; + + /* + * See if we've found a loop back to the desired startPoint; if so, done + */ + for (i = 0; i < obj->nDeps; i++) + { + if (obj->dependencies[i] == startPoint) + return depth; + } + + /* + * Recurse down each outgoing branch + */ + for (i = 0; i < obj->nDeps; i++) + { + DumpableObject *nextobj = findObjectByDumpId(obj->dependencies[i]); + int newDepth; + + if (!nextobj) + continue; /* ignore dependencies on undumped objects */ + newDepth = findLoop(nextobj, + startPoint, + processed, + searchFailed, + workspace, + depth); + if (newDepth > 0) + return newDepth; + } + + /* + * Remember there is no path from here back to startPoint + */ + searchFailed[obj->dumpId] = startPoint; + + return 0; +} + +/* + * A user-defined datatype will have a dependency loop with each of its + * I/O functions (since those have the datatype as input or output). + * Similarly, a range type will have a loop with its canonicalize function, + * if any. Break the loop by making the function depend on the associated + * shell type, instead. + */ +static void +repairTypeFuncLoop(DumpableObject *typeobj, DumpableObject *funcobj) +{ + TypeInfo *typeInfo = (TypeInfo *) typeobj; + + /* remove function's dependency on type */ + removeObjectDependency(funcobj, typeobj->dumpId); + + /* add function's dependency on shell type, instead */ + if (typeInfo->shellType) + { + addObjectDependency(funcobj, typeInfo->shellType->dobj.dumpId); + + /* + * Mark shell type (always including the definition, as we need the + * shell type defined to identify the function fully) as to be dumped + * if any such function is + */ + if (funcobj->dump) + typeInfo->shellType->dobj.dump = funcobj->dump | + DUMP_COMPONENT_DEFINITION; + } +} + +/* + * Because we force a view to depend on its ON SELECT rule, while there + * will be an implicit dependency in the other direction, we need to break + * the loop. If there are no other objects in the loop then we can remove + * the implicit dependency and leave the ON SELECT rule non-separate. + * This applies to matviews, as well. + */ +static void +repairViewRuleLoop(DumpableObject *viewobj, + DumpableObject *ruleobj) +{ + /* remove rule's dependency on view */ + removeObjectDependency(ruleobj, viewobj->dumpId); + /* flags on the two objects are already set correctly for this case */ +} + +/* + * However, if there are other objects in the loop, we must break the loop + * by making the ON SELECT rule a separately-dumped object. + * + * Because findLoop() finds shorter cycles before longer ones, it's likely + * that we will have previously fired repairViewRuleLoop() and removed the + * rule's dependency on the view. Put it back to ensure the rule won't be + * emitted before the view. + * + * Note: this approach does *not* work for matviews, at the moment. + */ +static void +repairViewRuleMultiLoop(DumpableObject *viewobj, + DumpableObject *ruleobj) +{ + TableInfo *viewinfo = (TableInfo *) viewobj; + RuleInfo *ruleinfo = (RuleInfo *) ruleobj; + + /* remove view's dependency on rule */ + removeObjectDependency(viewobj, ruleobj->dumpId); + /* mark view to be printed with a dummy definition */ + viewinfo->dummy_view = true; + /* mark rule as needing its own dump */ + ruleinfo->separate = true; + /* put back rule's dependency on view */ + addObjectDependency(ruleobj, viewobj->dumpId); + /* now that rule is separate, it must be post-data */ + addObjectDependency(ruleobj, postDataBoundId); +} + +/* + * If a matview is involved in a multi-object loop, we can't currently fix + * that by splitting off the rule. As a stopgap, we try to fix it by + * dropping the constraint that the matview be dumped in the pre-data section. + * This is sufficient to handle cases where a matview depends on some unique + * index, as can happen if it has a GROUP BY for example. + * + * Note that the "next object" is not necessarily the matview itself; + * it could be the matview's rowtype, for example. We may come through here + * several times while removing all the pre-data linkages. In particular, + * if there are other matviews that depend on the one with the circularity + * problem, we'll come through here for each such matview and mark them all + * as postponed. (This works because all MVs have pre-data dependencies + * to begin with, so each of them will get visited.) + */ +static void +repairMatViewBoundaryMultiLoop(DumpableObject *boundaryobj, + DumpableObject *nextobj) +{ + /* remove boundary's dependency on object after it in loop */ + removeObjectDependency(boundaryobj, nextobj->dumpId); + + /* + * If that object is a matview or matview stats, mark it as postponed into + * post-data. + */ + if (nextobj->objType == DO_TABLE) + { + TableInfo *nextinfo = (TableInfo *) nextobj; + + if (nextinfo->relkind == RELKIND_MATVIEW) + nextinfo->postponed_def = true; + } + else if (nextobj->objType == DO_REL_STATS) + { + RelStatsInfo *nextinfo = (RelStatsInfo *) nextobj; + + if (nextinfo->relkind == RELKIND_MATVIEW) + nextinfo->section = SECTION_POST_DATA; + } +} + +/* + * If a function is involved in a multi-object loop, we can't currently fix + * that by splitting it into two DumpableObjects. As a stopgap, we try to fix + * it by dropping the constraint that the function be dumped in the pre-data + * section. This is sufficient to handle cases where a function depends on + * some unique index, as can happen if it has a GROUP BY for example. + */ +static void +repairFunctionBoundaryMultiLoop(DumpableObject *boundaryobj, + DumpableObject *nextobj) +{ + /* remove boundary's dependency on object after it in loop */ + removeObjectDependency(boundaryobj, nextobj->dumpId); + /* if that object is a function, mark it as postponed into post-data */ + if (nextobj->objType == DO_FUNC) + { + FuncInfo *nextinfo = (FuncInfo *) nextobj; + + nextinfo->postponed_def = true; + } +} + +/* + * Because we make tables depend on their CHECK constraints, while there + * will be an automatic dependency in the other direction, we need to break + * the loop. If there are no other objects in the loop then we can remove + * the automatic dependency and leave the CHECK constraint non-separate. + */ +static void +repairTableConstraintLoop(DumpableObject *tableobj, + DumpableObject *constraintobj) +{ + /* remove constraint's dependency on table */ + removeObjectDependency(constraintobj, tableobj->dumpId); +} + +/* + * However, if there are other objects in the loop, we must break the loop + * by making the CHECK constraint a separately-dumped object. + * + * Because findLoop() finds shorter cycles before longer ones, it's likely + * that we will have previously fired repairTableConstraintLoop() and + * removed the constraint's dependency on the table. Put it back to ensure + * the constraint won't be emitted before the table... + */ +static void +repairTableConstraintMultiLoop(DumpableObject *tableobj, + DumpableObject *constraintobj) +{ + /* remove table's dependency on constraint */ + removeObjectDependency(tableobj, constraintobj->dumpId); + /* mark constraint as needing its own dump */ + ((ConstraintInfo *) constraintobj)->separate = true; + /* put back constraint's dependency on table */ + addObjectDependency(constraintobj, tableobj->dumpId); + /* now that constraint is separate, it must be post-data */ + addObjectDependency(constraintobj, postDataBoundId); +} + +/* + * Attribute defaults behave exactly the same as CHECK constraints... + */ +static void +repairTableAttrDefLoop(DumpableObject *tableobj, + DumpableObject *attrdefobj) +{ + /* remove attrdef's dependency on table */ + removeObjectDependency(attrdefobj, tableobj->dumpId); +} + +static void +repairTableAttrDefMultiLoop(DumpableObject *tableobj, + DumpableObject *attrdefobj) +{ + /* remove table's dependency on attrdef */ + removeObjectDependency(tableobj, attrdefobj->dumpId); + /* mark attrdef as needing its own dump */ + ((AttrDefInfo *) attrdefobj)->separate = true; + /* put back attrdef's dependency on table */ + addObjectDependency(attrdefobj, tableobj->dumpId); +} + +/* + * CHECK, NOT NULL constraints on domains work just like those on tables ... + */ +static void +repairDomainConstraintLoop(DumpableObject *domainobj, + DumpableObject *constraintobj) +{ + /* remove constraint's dependency on domain */ + removeObjectDependency(constraintobj, domainobj->dumpId); +} + +static void +repairDomainConstraintMultiLoop(DumpableObject *domainobj, + DumpableObject *constraintobj) +{ + /* remove domain's dependency on constraint */ + removeObjectDependency(domainobj, constraintobj->dumpId); + /* mark constraint as needing its own dump */ + ((ConstraintInfo *) constraintobj)->separate = true; + /* put back constraint's dependency on domain */ + addObjectDependency(constraintobj, domainobj->dumpId); + /* now that constraint is separate, it must be post-data */ + addObjectDependency(constraintobj, postDataBoundId); +} + +static void +repairIndexLoop(DumpableObject *partedindex, + DumpableObject *partindex) +{ + removeObjectDependency(partedindex, partindex->dumpId); +} + +/* + * Fix a dependency loop, or die trying ... + * + * This routine is mainly concerned with reducing the multiple ways that + * a loop might appear to common cases, which it passes off to the + * "fixer" routines above. + */ +static void +repairDependencyLoop(DumpableObject **loop, + int nLoop) +{ + int i, + j; + + /* Datatype and one of its I/O or canonicalize functions */ + if (nLoop == 2 && + loop[0]->objType == DO_TYPE && + loop[1]->objType == DO_FUNC) + { + repairTypeFuncLoop(loop[0], loop[1]); + return; + } + if (nLoop == 2 && + loop[1]->objType == DO_TYPE && + loop[0]->objType == DO_FUNC) + { + repairTypeFuncLoop(loop[1], loop[0]); + return; + } + + /* View (including matview) and its ON SELECT rule */ + if (nLoop == 2 && + loop[0]->objType == DO_TABLE && + loop[1]->objType == DO_RULE && + (((TableInfo *) loop[0])->relkind == RELKIND_VIEW || + ((TableInfo *) loop[0])->relkind == RELKIND_MATVIEW) && + ((RuleInfo *) loop[1])->ev_type == '1' && + ((RuleInfo *) loop[1])->is_instead && + ((RuleInfo *) loop[1])->ruletable == (TableInfo *) loop[0]) + { + repairViewRuleLoop(loop[0], loop[1]); + return; + } + if (nLoop == 2 && + loop[1]->objType == DO_TABLE && + loop[0]->objType == DO_RULE && + (((TableInfo *) loop[1])->relkind == RELKIND_VIEW || + ((TableInfo *) loop[1])->relkind == RELKIND_MATVIEW) && + ((RuleInfo *) loop[0])->ev_type == '1' && + ((RuleInfo *) loop[0])->is_instead && + ((RuleInfo *) loop[0])->ruletable == (TableInfo *) loop[1]) + { + repairViewRuleLoop(loop[1], loop[0]); + return; + } + + /* Indirect loop involving view (but not matview) and ON SELECT rule */ + if (nLoop > 2) + { + for (i = 0; i < nLoop; i++) + { + if (loop[i]->objType == DO_TABLE && + ((TableInfo *) loop[i])->relkind == RELKIND_VIEW) + { + for (j = 0; j < nLoop; j++) + { + if (loop[j]->objType == DO_RULE && + ((RuleInfo *) loop[j])->ev_type == '1' && + ((RuleInfo *) loop[j])->is_instead && + ((RuleInfo *) loop[j])->ruletable == (TableInfo *) loop[i]) + { + repairViewRuleMultiLoop(loop[i], loop[j]); + return; + } + } + } + } + } + + /* Indirect loop involving matview and data boundary */ + if (nLoop > 2) + { + for (i = 0; i < nLoop; i++) + { + if (loop[i]->objType == DO_TABLE && + ((TableInfo *) loop[i])->relkind == RELKIND_MATVIEW) + { + for (j = 0; j < nLoop; j++) + { + if (loop[j]->objType == DO_PRE_DATA_BOUNDARY) + { + DumpableObject *nextobj; + + nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0]; + repairMatViewBoundaryMultiLoop(loop[j], nextobj); + return; + } + } + } + else if (loop[i]->objType == DO_REL_STATS && + ((RelStatsInfo *) loop[i])->relkind == RELKIND_MATVIEW) + { + for (j = 0; j < nLoop; j++) + { + if (loop[j]->objType == DO_POST_DATA_BOUNDARY) + { + DumpableObject *nextobj; + + nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0]; + repairMatViewBoundaryMultiLoop(loop[j], nextobj); + return; + } + } + } + } + } + + /* Indirect loop involving function and data boundary */ + if (nLoop > 2) + { + for (i = 0; i < nLoop; i++) + { + if (loop[i]->objType == DO_FUNC) + { + for (j = 0; j < nLoop; j++) + { + if (loop[j]->objType == DO_PRE_DATA_BOUNDARY) + { + DumpableObject *nextobj; + + nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0]; + repairFunctionBoundaryMultiLoop(loop[j], nextobj); + return; + } + } + } + } + } + + /* Table and CHECK constraint */ + if (nLoop == 2 && + loop[0]->objType == DO_TABLE && + loop[1]->objType == DO_CONSTRAINT && + ((ConstraintInfo *) loop[1])->contype == 'c' && + ((ConstraintInfo *) loop[1])->contable == (TableInfo *) loop[0]) + { + repairTableConstraintLoop(loop[0], loop[1]); + return; + } + if (nLoop == 2 && + loop[1]->objType == DO_TABLE && + loop[0]->objType == DO_CONSTRAINT && + ((ConstraintInfo *) loop[0])->contype == 'c' && + ((ConstraintInfo *) loop[0])->contable == (TableInfo *) loop[1]) + { + repairTableConstraintLoop(loop[1], loop[0]); + return; + } + + /* Indirect loop involving table and CHECK constraint */ + if (nLoop > 2) + { + for (i = 0; i < nLoop; i++) + { + if (loop[i]->objType == DO_TABLE) + { + for (j = 0; j < nLoop; j++) + { + if (loop[j]->objType == DO_CONSTRAINT && + ((ConstraintInfo *) loop[j])->contype == 'c' && + ((ConstraintInfo *) loop[j])->contable == (TableInfo *) loop[i]) + { + repairTableConstraintMultiLoop(loop[i], loop[j]); + return; + } + } + } + } + } + + /* Table and attribute default */ + if (nLoop == 2 && + loop[0]->objType == DO_TABLE && + loop[1]->objType == DO_ATTRDEF && + ((AttrDefInfo *) loop[1])->adtable == (TableInfo *) loop[0]) + { + repairTableAttrDefLoop(loop[0], loop[1]); + return; + } + if (nLoop == 2 && + loop[1]->objType == DO_TABLE && + loop[0]->objType == DO_ATTRDEF && + ((AttrDefInfo *) loop[0])->adtable == (TableInfo *) loop[1]) + { + repairTableAttrDefLoop(loop[1], loop[0]); + return; + } + + /* index on partitioned table and corresponding index on partition */ + if (nLoop == 2 && + loop[0]->objType == DO_INDEX && + loop[1]->objType == DO_INDEX) + { + if (((IndxInfo *) loop[0])->parentidx == loop[1]->catId.oid) + { + repairIndexLoop(loop[0], loop[1]); + return; + } + else if (((IndxInfo *) loop[1])->parentidx == loop[0]->catId.oid) + { + repairIndexLoop(loop[1], loop[0]); + return; + } + } + + /* Indirect loop involving table and attribute default */ + if (nLoop > 2) + { + for (i = 0; i < nLoop; i++) + { + if (loop[i]->objType == DO_TABLE) + { + for (j = 0; j < nLoop; j++) + { + if (loop[j]->objType == DO_ATTRDEF && + ((AttrDefInfo *) loop[j])->adtable == (TableInfo *) loop[i]) + { + repairTableAttrDefMultiLoop(loop[i], loop[j]); + return; + } + } + } + } + } + + /* Domain and CHECK or NOT NULL constraint */ + if (nLoop == 2 && + loop[0]->objType == DO_TYPE && + loop[1]->objType == DO_CONSTRAINT && + (((ConstraintInfo *) loop[1])->contype == 'c' || + ((ConstraintInfo *) loop[1])->contype == 'n') && + ((ConstraintInfo *) loop[1])->condomain == (TypeInfo *) loop[0]) + { + repairDomainConstraintLoop(loop[0], loop[1]); + return; + } + if (nLoop == 2 && + loop[1]->objType == DO_TYPE && + loop[0]->objType == DO_CONSTRAINT && + (((ConstraintInfo *) loop[0])->contype == 'c' || + ((ConstraintInfo *) loop[0])->contype == 'n') && + ((ConstraintInfo *) loop[0])->condomain == (TypeInfo *) loop[1]) + { + repairDomainConstraintLoop(loop[1], loop[0]); + return; + } + + /* Indirect loop involving domain and CHECK or NOT NULL constraint */ + if (nLoop > 2) + { + for (i = 0; i < nLoop; i++) + { + if (loop[i]->objType == DO_TYPE) + { + for (j = 0; j < nLoop; j++) + { + if (loop[j]->objType == DO_CONSTRAINT && + (((ConstraintInfo *) loop[j])->contype == 'c' || + ((ConstraintInfo *) loop[j])->contype == 'n') && + ((ConstraintInfo *) loop[j])->condomain == (TypeInfo *) loop[i]) + { + repairDomainConstraintMultiLoop(loop[i], loop[j]); + return; + } + } + } + } + } + + /* + * Loop of table with itself --- just ignore it. + * + * (Actually, what this arises from is a dependency of a table column on + * another column, which happened with generated columns before v15; or a + * dependency of a table column on the whole table, which happens with + * partitioning. But we didn't pay attention to sub-object IDs while + * collecting the dependency data, so we can't see that here.) + */ + if (nLoop == 1) + { + if (loop[0]->objType == DO_TABLE) + { + removeObjectDependency(loop[0], loop[0]->dumpId); + return; + } + } + + /* + * If all the objects are TABLE_DATA items, what we must have is a + * circular set of foreign key constraints (or a single self-referential + * table). Print an appropriate complaint and break the loop arbitrarily. + */ + for (i = 0; i < nLoop; i++) + { + if (loop[i]->objType != DO_TABLE_DATA) + break; + } + if (i >= nLoop) + { + pg_log_warning(ngettext("there are circular foreign-key constraints on this table:", + "there are circular foreign-key constraints among these tables:", + nLoop)); + for (i = 0; i < nLoop; i++) + pg_log_warning_detail("%s", loop[i]->name); + pg_log_warning_hint("You might not be able to restore the dump without using --disable-triggers or temporarily dropping the constraints."); + pg_log_warning_hint("Consider using a full dump instead of a --data-only dump to avoid this problem."); + if (nLoop > 1) + removeObjectDependency(loop[0], loop[1]->dumpId); + else /* must be a self-dependency */ + removeObjectDependency(loop[0], loop[0]->dumpId); + return; + } + + /* + * If we can't find a principled way to break the loop, complain and break + * it in an arbitrary fashion. + */ + pg_log_warning("could not resolve dependency loop among these items:"); + for (i = 0; i < nLoop; i++) + { + char buf[1024]; + + describeDumpableObject(loop[i], buf, sizeof(buf)); + pg_log_warning_detail("%s", buf); + } + + if (nLoop > 1) + removeObjectDependency(loop[0], loop[1]->dumpId); + else /* must be a self-dependency */ + removeObjectDependency(loop[0], loop[0]->dumpId); +} + +/* + * Describe a dumpable object usefully for errors + * + * This should probably go somewhere else... + */ +static void +describeDumpableObject(DumpableObject *obj, char *buf, int bufsize) +{ + switch (obj->objType) + { + case DO_NAMESPACE: + snprintf(buf, bufsize, + "SCHEMA %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_EXTENSION: + snprintf(buf, bufsize, + "EXTENSION %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_TYPE: + snprintf(buf, bufsize, + "TYPE %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_SHELL_TYPE: + snprintf(buf, bufsize, + "SHELL TYPE %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_FUNC: + snprintf(buf, bufsize, + "FUNCTION %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_AGG: + snprintf(buf, bufsize, + "AGGREGATE %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_OPERATOR: + snprintf(buf, bufsize, + "OPERATOR %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_ACCESS_METHOD: + snprintf(buf, bufsize, + "ACCESS METHOD %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_OPCLASS: + snprintf(buf, bufsize, + "OPERATOR CLASS %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_OPFAMILY: + snprintf(buf, bufsize, + "OPERATOR FAMILY %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_COLLATION: + snprintf(buf, bufsize, + "COLLATION %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_CONVERSION: + snprintf(buf, bufsize, + "CONVERSION %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_TABLE: + snprintf(buf, bufsize, + "TABLE %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_TABLE_ATTACH: + snprintf(buf, bufsize, + "TABLE ATTACH %s (ID %d)", + obj->name, obj->dumpId); + return; + case DO_ATTRDEF: + snprintf(buf, bufsize, + "ATTRDEF %s.%s (ID %d OID %u)", + ((AttrDefInfo *) obj)->adtable->dobj.name, + ((AttrDefInfo *) obj)->adtable->attnames[((AttrDefInfo *) obj)->adnum - 1], + obj->dumpId, obj->catId.oid); + return; + case DO_INDEX: + snprintf(buf, bufsize, + "INDEX %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_INDEX_ATTACH: + snprintf(buf, bufsize, + "INDEX ATTACH %s (ID %d)", + obj->name, obj->dumpId); + return; + case DO_STATSEXT: + snprintf(buf, bufsize, + "STATISTICS %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_REFRESH_MATVIEW: + snprintf(buf, bufsize, + "REFRESH MATERIALIZED VIEW %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_RULE: + snprintf(buf, bufsize, + "RULE %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_TRIGGER: + snprintf(buf, bufsize, + "TRIGGER %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_EVENT_TRIGGER: + snprintf(buf, bufsize, + "EVENT TRIGGER %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_CONSTRAINT: + snprintf(buf, bufsize, + "CONSTRAINT %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_FK_CONSTRAINT: + snprintf(buf, bufsize, + "FK CONSTRAINT %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_PROCLANG: + snprintf(buf, bufsize, + "PROCEDURAL LANGUAGE %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_CAST: + snprintf(buf, bufsize, + "CAST %u to %u (ID %d OID %u)", + ((CastInfo *) obj)->castsource, + ((CastInfo *) obj)->casttarget, + obj->dumpId, obj->catId.oid); + return; + case DO_TRANSFORM: + snprintf(buf, bufsize, + "TRANSFORM %u lang %u (ID %d OID %u)", + ((TransformInfo *) obj)->trftype, + ((TransformInfo *) obj)->trflang, + obj->dumpId, obj->catId.oid); + return; + case DO_TABLE_DATA: + snprintf(buf, bufsize, + "TABLE DATA %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_SEQUENCE_SET: + snprintf(buf, bufsize, + "SEQUENCE SET %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_DUMMY_TYPE: + snprintf(buf, bufsize, + "DUMMY TYPE %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_TSPARSER: + snprintf(buf, bufsize, + "TEXT SEARCH PARSER %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_TSDICT: + snprintf(buf, bufsize, + "TEXT SEARCH DICTIONARY %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_TSTEMPLATE: + snprintf(buf, bufsize, + "TEXT SEARCH TEMPLATE %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_TSCONFIG: + snprintf(buf, bufsize, + "TEXT SEARCH CONFIGURATION %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_FDW: + snprintf(buf, bufsize, + "FOREIGN DATA WRAPPER %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_FOREIGN_SERVER: + snprintf(buf, bufsize, + "FOREIGN SERVER %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_DEFAULT_ACL: + snprintf(buf, bufsize, + "DEFAULT ACL %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + case DO_LARGE_OBJECT: + snprintf(buf, bufsize, + "LARGE OBJECT (ID %d OID %u)", + obj->dumpId, obj->catId.oid); + return; + case DO_LARGE_OBJECT_DATA: + snprintf(buf, bufsize, + "LARGE OBJECT DATA (ID %d)", + obj->dumpId); + return; + case DO_POLICY: + snprintf(buf, bufsize, + "POLICY (ID %d OID %u)", + obj->dumpId, obj->catId.oid); + return; + case DO_PUBLICATION: + snprintf(buf, bufsize, + "PUBLICATION (ID %d OID %u)", + obj->dumpId, obj->catId.oid); + return; + case DO_PUBLICATION_REL: + snprintf(buf, bufsize, + "PUBLICATION TABLE (ID %d OID %u)", + obj->dumpId, obj->catId.oid); + return; + case DO_PUBLICATION_TABLE_IN_SCHEMA: + snprintf(buf, bufsize, + "PUBLICATION TABLES IN SCHEMA (ID %d OID %u)", + obj->dumpId, obj->catId.oid); + return; + case DO_SUBSCRIPTION: + snprintf(buf, bufsize, + "SUBSCRIPTION (ID %d OID %u)", + obj->dumpId, obj->catId.oid); + return; + case DO_SUBSCRIPTION_REL: + snprintf(buf, bufsize, + "SUBSCRIPTION TABLE (ID %d OID %u)", + obj->dumpId, obj->catId.oid); + return; + case DO_PRE_DATA_BOUNDARY: + snprintf(buf, bufsize, + "PRE-DATA BOUNDARY (ID %d)", + obj->dumpId); + return; + case DO_POST_DATA_BOUNDARY: + snprintf(buf, bufsize, + "POST-DATA BOUNDARY (ID %d)", + obj->dumpId); + return; + case DO_REL_STATS: + snprintf(buf, bufsize, + "RELATION STATISTICS FOR %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; + } + /* shouldn't get here */ + snprintf(buf, bufsize, + "object type %d (ID %d OID %u)", + (int) obj->objType, + obj->dumpId, obj->catId.oid); +} + +/* binaryheap comparator that compares "a" and "b" as integers */ +static int +int_cmp(void *a, void *b, void *arg) +{ + int ai = (int) (intptr_t) a; + int bi = (int) (intptr_t) b; + + return pg_cmp_s32(ai, bi); +} diff --git a/src/compat/19/pg_dump/string_utils.c b/src/compat/19/pg_dump/string_utils.c new file mode 100644 index 00000000..38fffbd0 --- /dev/null +++ b/src/compat/19/pg_dump/string_utils.c @@ -0,0 +1,1387 @@ +/*------------------------------------------------------------------------- + * + * String-processing utility routines for frontend code + * + * Assorted utility functions that are useful in constructing SQL queries + * and interpreting backend output. + * + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/fe_utils/string_utils.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include + +#include "common/keywords.h" +#include "fe_utils/string_utils.h" +#include "mb/pg_wchar.h" + +static PQExpBuffer defaultGetLocalPQExpBuffer(void); + +/* Globals exported by this file */ +int quote_all_identifiers = 0; +PQExpBuffer (*getLocalPQExpBuffer) (void) = defaultGetLocalPQExpBuffer; + +static int fmtIdEncoding = -1; + + +/* + * Returns a temporary PQExpBuffer, valid until the next call to the function. + * This is used by fmtId and fmtQualifiedId. + * + * Non-reentrant and non-thread-safe but reduces memory leakage. You can + * replace this with a custom version by setting the getLocalPQExpBuffer + * function pointer. + */ +static PQExpBuffer +defaultGetLocalPQExpBuffer(void) +{ + static PQExpBuffer id_return = NULL; + + if (id_return) /* first time through? */ + { + /* same buffer, just wipe contents */ + resetPQExpBuffer(id_return); + } + else + { + /* new buffer */ + id_return = createPQExpBuffer(); + } + + return id_return; +} + +/* + * Set the encoding that fmtId() and fmtQualifiedId() use. + * + * This is not safe against multiple connections having different encodings, + * but there is no real other way to address the need to know the encoding for + * fmtId()/fmtQualifiedId() input for safe escaping. Eventually we should get + * rid of fmtId(). + */ +void +setFmtEncoding(int encoding) +{ + fmtIdEncoding = encoding; +} + +/* + * Return the currently configured encoding for fmtId() and fmtQualifiedId(). + */ +static int +getFmtEncoding(void) +{ + if (fmtIdEncoding != -1) + return fmtIdEncoding; + + /* + * In assertion builds it seems best to fail hard if the encoding was not + * set, to make it easier to find places with missing calls. But in + * production builds that seems like a bad idea, thus we instead just + * default to UTF-8. + */ + Assert(fmtIdEncoding != -1); + + return PG_UTF8; +} + +/* + * Quotes input string if it's not a legitimate SQL identifier as-is. + * + * Note that the returned string must be used before calling fmtIdEnc again, + * since we re-use the same return buffer each time. + */ +const char * +fmtIdEnc(const char *rawid, int encoding) +{ + PQExpBuffer id_return = getLocalPQExpBuffer(); + + const char *cp; + bool need_quotes = false; + size_t remaining = strlen(rawid); + + /* + * These checks need to match the identifier production in scan.l. Don't + * use islower() etc. + */ + if (quote_all_identifiers) + need_quotes = true; + /* slightly different rules for first character */ + else if (!((rawid[0] >= 'a' && rawid[0] <= 'z') || rawid[0] == '_')) + need_quotes = true; + else + { + /* otherwise check the entire string */ + cp = rawid; + for (size_t i = 0; i < remaining; i++, cp++) + { + if (!((*cp >= 'a' && *cp <= 'z') + || (*cp >= '0' && *cp <= '9') + || (*cp == '_'))) + { + need_quotes = true; + break; + } + } + } + + if (!need_quotes) + { + /* + * Check for keyword. We quote keywords except for unreserved ones. + * (In some cases we could avoid quoting a col_name or type_func_name + * keyword, but it seems much harder than it's worth to tell that.) + * + * Note: ScanKeywordLookup() does case-insensitive comparison, but + * that's fine, since we already know we have all-lower-case. + */ + int kwnum = ScanKeywordLookup(rawid, &ScanKeywords); + + if (kwnum >= 0 && ScanKeywordCategories[kwnum] != UNRESERVED_KEYWORD) + need_quotes = true; + } + + if (!need_quotes) + { + /* no quoting needed */ + appendPQExpBufferStr(id_return, rawid); + } + else + { + appendPQExpBufferChar(id_return, '"'); + + cp = &rawid[0]; + while (remaining > 0) + { + int charlen; + + /* Fast path for plain ASCII */ + if (!IS_HIGHBIT_SET(*cp)) + { + /* + * Did we find a double-quote in the string? Then make this a + * double double-quote per SQL99. Before, we put in a + * backslash/double-quote pair. - thomas 2000-08-05 + */ + if (*cp == '"') + appendPQExpBufferChar(id_return, '"'); + appendPQExpBufferChar(id_return, *cp); + remaining--; + cp++; + continue; + } + + /* Slow path for possible multibyte characters */ + charlen = pg_encoding_mblen(encoding, cp); + + if (remaining < charlen || + pg_encoding_verifymbchar(encoding, cp, charlen) == -1) + { + /* + * Multibyte character is invalid. It's important to verify + * that as invalid multibyte characters could e.g. be used to + * "skip" over quote characters, e.g. when parsing + * character-by-character. + * + * Replace the character's first byte with an invalid + * sequence. The invalid sequence ensures that the escaped + * string will trigger an error on the server-side, even if we + * can't directly report an error here. + * + * It would be a bit faster to verify the whole string the + * first time we encounter a set highbit, but this way we can + * replace just the invalid data, which probably makes it + * easier for users to find the invalidly encoded portion of a + * larger string. + */ + if (enlargePQExpBuffer(id_return, 2)) + { + pg_encoding_set_invalid(encoding, + id_return->data + id_return->len); + id_return->len += 2; + id_return->data[id_return->len] = '\0'; + } + + /* + * Handle the following bytes as if this byte didn't exist. + * That's safer in case the subsequent bytes contain + * characters that are significant for the caller (e.g. '>' in + * html). + */ + remaining--; + cp++; + } + else + { + for (int i = 0; i < charlen; i++) + { + appendPQExpBufferChar(id_return, *cp); + remaining--; + cp++; + } + } + } + + appendPQExpBufferChar(id_return, '"'); + } + + return id_return->data; +} + +/* + * Quotes input string if it's not a legitimate SQL identifier as-is. + * + * Note that the returned string must be used before calling fmtId again, + * since we re-use the same return buffer each time. + * + * NB: This assumes setFmtEncoding() previously has been called to configure + * the encoding of rawid. It is preferable to use fmtIdEnc() with an + * explicit encoding. + */ +const char * +fmtId(const char *rawid) +{ + return fmtIdEnc(rawid, getFmtEncoding()); +} + +/* + * fmtQualifiedIdEnc - construct a schema-qualified name, with quoting as + * needed. + * + * Like fmtId, use the result before calling again. + * + * Since we call fmtId and it also uses getLocalPQExpBuffer() we cannot + * use that buffer until we're finished with calling fmtId(). + */ +const char * +fmtQualifiedIdEnc(const char *schema, const char *id, int encoding) +{ + PQExpBuffer id_return; + PQExpBuffer lcl_pqexp = createPQExpBuffer(); + + /* Some callers might fail to provide a schema name */ + if (schema && *schema) + { + appendPQExpBuffer(lcl_pqexp, "%s.", fmtIdEnc(schema, encoding)); + } + appendPQExpBufferStr(lcl_pqexp, fmtIdEnc(id, encoding)); + + id_return = getLocalPQExpBuffer(); + + appendPQExpBufferStr(id_return, lcl_pqexp->data); + destroyPQExpBuffer(lcl_pqexp); + + return id_return->data; +} + +/* + * fmtQualifiedId - construct a schema-qualified name, with quoting as needed. + * + * Like fmtId, use the result before calling again. + * + * Since we call fmtId and it also uses getLocalPQExpBuffer() we cannot + * use that buffer until we're finished with calling fmtId(). + * + * NB: This assumes setFmtEncoding() previously has been called to configure + * the encoding of schema/id. It is preferable to use fmtQualifiedIdEnc() + * with an explicit encoding. + */ +const char * +fmtQualifiedId(const char *schema, const char *id) +{ + return fmtQualifiedIdEnc(schema, id, getFmtEncoding()); +} + + +/* + * Format a Postgres version number (in the PG_VERSION_NUM integer format + * returned by PQserverVersion()) as a string. This exists mainly to + * encapsulate knowledge about two-part vs. three-part version numbers. + * + * For reentrancy, caller must supply the buffer the string is put in. + * Recommended size of the buffer is 32 bytes. + * + * Returns address of 'buf', as a notational convenience. + */ +char * +formatPGVersionNumber(int version_number, bool include_minor, + char *buf, size_t buflen) +{ + if (version_number >= 100000) + { + /* New two-part style */ + if (include_minor) + snprintf(buf, buflen, "%d.%d", version_number / 10000, + version_number % 10000); + else + snprintf(buf, buflen, "%d", version_number / 10000); + } + else + { + /* Old three-part style */ + if (include_minor) + snprintf(buf, buflen, "%d.%d.%d", version_number / 10000, + (version_number / 100) % 100, + version_number % 100); + else + snprintf(buf, buflen, "%d.%d", version_number / 10000, + (version_number / 100) % 100); + } + return buf; +} + + +/* + * Convert a string value to an SQL string literal and append it to + * the given buffer. We assume the specified client_encoding and + * standard_conforming_strings settings. + * + * This is essentially equivalent to libpq's PQescapeStringInternal, + * except for the output buffer structure. We need it in situations + * where we do not have a PGconn available. Where we do, + * appendStringLiteralConn is a better choice. + */ +void +appendStringLiteral(PQExpBuffer buf, const char *str, + int encoding, bool std_strings) +{ + size_t length = strlen(str); + const char *source = str; + char *target; + size_t remaining = length; + + if (!enlargePQExpBuffer(buf, 2 * length + 2)) + return; + + target = buf->data + buf->len; + *target++ = '\''; + + while (remaining > 0) + { + char c = *source; + int charlen; + int i; + + /* Fast path for plain ASCII */ + if (!IS_HIGHBIT_SET(c)) + { + /* Apply quoting if needed */ + if (SQL_STR_DOUBLE(c, !std_strings)) + *target++ = c; + /* Copy the character */ + *target++ = c; + source++; + remaining--; + continue; + } + + /* Slow path for possible multibyte characters */ + charlen = PQmblen(source, encoding); + + if (remaining < charlen || + pg_encoding_verifymbchar(encoding, source, charlen) == -1) + { + /* + * Multibyte character is invalid. It's important to verify that + * as invalid multibyte characters could e.g. be used to "skip" + * over quote characters, e.g. when parsing + * character-by-character. + * + * Replace the character's first byte with an invalid sequence. + * The invalid sequence ensures that the escaped string will + * trigger an error on the server-side, even if we can't directly + * report an error here. + * + * We know there's enough space for the invalid sequence because + * the "target" buffer is 2 * length + 2 long, and at worst we're + * replacing a single input byte with two invalid bytes. + * + * It would be a bit faster to verify the whole string the first + * time we encounter a set highbit, but this way we can replace + * just the invalid data, which probably makes it easier for users + * to find the invalidly encoded portion of a larger string. + */ + pg_encoding_set_invalid(encoding, target); + target += 2; + + /* + * Handle the following bytes as if this byte didn't exist. That's + * safer in case the subsequent bytes contain important characters + * for the caller (e.g. '>' in html). + */ + source++; + remaining--; + } + else + { + /* Copy the character */ + for (i = 0; i < charlen; i++) + { + *target++ = *source++; + remaining--; + } + } + } + + /* Write the terminating quote and NUL character. */ + *target++ = '\''; + *target = '\0'; + + buf->len = target - buf->data; +} + + +/* + * Convert a string value to an SQL string literal and append it to + * the given buffer. Encoding and string syntax rules are as indicated + * by current settings of the PGconn. + */ +void +appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn) +{ + size_t length = strlen(str); + + /* + * XXX This is a kluge to silence escape_string_warning in our utility + * programs. It can go away once pre-v19 servers are out of support. + */ + if (strchr(str, '\\') != NULL && PQserverVersion(conn) < 190000) + { + /* ensure we are not adjacent to an identifier */ + if (buf->len > 0 && buf->data[buf->len - 1] != ' ') + appendPQExpBufferChar(buf, ' '); + appendPQExpBufferChar(buf, ESCAPE_STRING_SYNTAX); + appendStringLiteral(buf, str, PQclientEncoding(conn), false); + return; + } + /* XXX end kluge */ + + if (!enlargePQExpBuffer(buf, 2 * length + 2)) + return; + appendPQExpBufferChar(buf, '\''); + buf->len += PQescapeStringConn(conn, buf->data + buf->len, + str, length, NULL); + appendPQExpBufferChar(buf, '\''); +} + + +/* + * Convert a string value to a dollar quoted literal and append it to + * the given buffer. If the dqprefix parameter is not NULL then the + * dollar quote delimiter will begin with that (after the opening $). + * + * No escaping is done at all on str, in compliance with the rules + * for parsing dollar quoted strings. Also, we need not worry about + * encoding issues. + */ +void +appendStringLiteralDQ(PQExpBuffer buf, const char *str, const char *dqprefix) +{ + static const char suffixes[] = "_XXXXXXX"; + int nextchar = 0; + PQExpBuffer delimBuf = createPQExpBuffer(); + + /* start with $ + dqprefix if not NULL */ + appendPQExpBufferChar(delimBuf, '$'); + if (dqprefix) + appendPQExpBufferStr(delimBuf, dqprefix); + + /* + * Make sure we choose a delimiter which (without the trailing $) is not + * present in the string being quoted. We don't check with the trailing $ + * because a string ending in $foo must not be quoted with $foo$. + */ + while (strstr(str, delimBuf->data) != NULL) + { + appendPQExpBufferChar(delimBuf, suffixes[nextchar++]); + nextchar %= sizeof(suffixes) - 1; + } + + /* add trailing $ */ + appendPQExpBufferChar(delimBuf, '$'); + + /* quote it and we are all done */ + appendPQExpBufferStr(buf, delimBuf->data); + appendPQExpBufferStr(buf, str); + appendPQExpBufferStr(buf, delimBuf->data); + + destroyPQExpBuffer(delimBuf); +} + + +/* + * Convert a bytea value (presented as raw bytes) to an SQL string literal + * and append it to the given buffer. We assume the specified + * standard_conforming_strings setting. + * + * This is needed in situations where we do not have a PGconn available. + * Where we do, PQescapeByteaConn is a better choice. + */ +void +appendByteaLiteral(PQExpBuffer buf, const unsigned char *str, size_t length, + bool std_strings) +{ + const unsigned char *source = str; + char *target; + + static const char hextbl[] = "0123456789abcdef"; + + /* + * This implementation is hard-wired to produce hex-format output. We do + * not know the server version the output will be loaded into, so making + * an intelligent format choice is impossible. It might be better to + * always use the old escaped format. + */ + if (!enlargePQExpBuffer(buf, 2 * length + 5)) + return; + + target = buf->data + buf->len; + *target++ = '\''; + if (!std_strings) + *target++ = '\\'; + *target++ = '\\'; + *target++ = 'x'; + + while (length-- > 0) + { + unsigned char c = *source++; + + *target++ = hextbl[(c >> 4) & 0xF]; + *target++ = hextbl[c & 0xF]; + } + + /* Write the terminating quote and NUL character. */ + *target++ = '\''; + *target = '\0'; + + buf->len = target - buf->data; +} + + +/* + * Append the given string to the shell command being built in the buffer, + * with shell-style quoting as needed to create exactly one argument. + * + * appendShellString() simply prints an error and dies if LF or CR appears. + * appendShellStringNoError() omits those characters from the result, and + * returns false if there were any. + */ +void +appendShellString(PQExpBuffer buf, const char *str) +{ + if (!appendShellStringNoError(buf, str)) + { + fprintf(stderr, + _("shell command argument contains a newline or carriage return: \"%s\"\n"), + str); + exit(EXIT_FAILURE); + } +} + +bool +appendShellStringNoError(PQExpBuffer buf, const char *str) +{ +#ifdef WIN32 + int backslash_run_length = 0; +#endif + bool ok = true; + const char *p; + + /* + * Don't bother with adding quotes if the string is nonempty and clearly + * contains only safe characters. + */ + if (*str != '\0' && + strspn(str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_./:") == strlen(str)) + { + appendPQExpBufferStr(buf, str); + return ok; + } + +#ifndef WIN32 + appendPQExpBufferChar(buf, '\''); + for (p = str; *p; p++) + { + if (*p == '\n' || *p == '\r') + { + ok = false; + continue; + } + + if (*p == '\'') + appendPQExpBufferStr(buf, "'\"'\"'"); + else + appendPQExpBufferChar(buf, *p); + } + appendPQExpBufferChar(buf, '\''); +#else /* WIN32 */ + + /* + * A Windows system() argument experiences two layers of interpretation. + * First, cmd.exe interprets the string. Its behavior is undocumented, + * but a caret escapes any byte except LF or CR that would otherwise have + * special meaning. Handling of a caret before LF or CR differs between + * "cmd.exe /c" and other modes, and it is unusable here. + * + * Second, the new process parses its command line to construct argv (see + * https://msdn.microsoft.com/en-us/library/17w5ykft.aspx). This treats + * backslash-double quote sequences specially. + */ + appendPQExpBufferStr(buf, "^\""); + for (p = str; *p; p++) + { + if (*p == '\n' || *p == '\r') + { + ok = false; + continue; + } + + /* Change N backslashes before a double quote to 2N+1 backslashes. */ + if (*p == '"') + { + while (backslash_run_length) + { + appendPQExpBufferStr(buf, "^\\"); + backslash_run_length--; + } + appendPQExpBufferStr(buf, "^\\"); + } + else if (*p == '\\') + backslash_run_length++; + else + backslash_run_length = 0; + + /* + * Decline to caret-escape the most mundane characters, to ease + * debugging and lest we approach the command length limit. + */ + if (!((*p >= 'a' && *p <= 'z') || + (*p >= 'A' && *p <= 'Z') || + (*p >= '0' && *p <= '9'))) + appendPQExpBufferChar(buf, '^'); + appendPQExpBufferChar(buf, *p); + } + + /* + * Change N backslashes at end of argument to 2N backslashes, because they + * precede the double quote that terminates the argument. + */ + while (backslash_run_length) + { + appendPQExpBufferStr(buf, "^\\"); + backslash_run_length--; + } + appendPQExpBufferStr(buf, "^\""); +#endif /* WIN32 */ + + return ok; +} + + +/* + * Append the given string to the buffer, with suitable quoting for passing + * the string as a value in a keyword/value pair in a libpq connection string. + */ +void +appendConnStrVal(PQExpBuffer buf, const char *str) +{ + const char *s; + bool needquotes; + + /* + * If the string is one or more plain ASCII characters, no need to quote + * it. This is quite conservative, but better safe than sorry. + */ + needquotes = true; + for (s = str; *s; s++) + { + if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || + (*s >= '0' && *s <= '9') || *s == '_' || *s == '.')) + { + needquotes = true; + break; + } + needquotes = false; + } + + if (needquotes) + { + appendPQExpBufferChar(buf, '\''); + while (*str) + { + /* ' and \ must be escaped by to \' and \\ */ + if (*str == '\'' || *str == '\\') + appendPQExpBufferChar(buf, '\\'); + + appendPQExpBufferChar(buf, *str); + str++; + } + appendPQExpBufferChar(buf, '\''); + } + else + appendPQExpBufferStr(buf, str); +} + + +/* + * Append a psql meta-command that connects to the given database with the + * then-current connection's user, host and port. + */ +void +appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname) +{ + const char *s; + bool complex; + + /* + * If the name is plain ASCII characters, emit a trivial "\connect "foo"". + * For other names, even many not technically requiring it, skip to the + * general case. No database has a zero-length name. + */ + complex = false; + + for (s = dbname; *s; s++) + { + if (*s == '\n' || *s == '\r') + { + fprintf(stderr, + _("database name contains a newline or carriage return: \"%s\"\n"), + dbname); + exit(EXIT_FAILURE); + } + + if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || + (*s >= '0' && *s <= '9') || *s == '_' || *s == '.')) + { + complex = true; + } + } + + if (complex) + { + PQExpBufferData connstr; + + initPQExpBuffer(&connstr); + + /* + * Force the target psql's encoding to SQL_ASCII. We don't really + * know the encoding of the database name, and it doesn't matter as + * long as psql will forward it to the server unchanged. + */ + appendPQExpBufferStr(buf, "\\encoding SQL_ASCII\n"); + appendPQExpBufferStr(buf, "\\connect -reuse-previous=on "); + + appendPQExpBufferStr(&connstr, "dbname="); + appendConnStrVal(&connstr, dbname); + + /* + * As long as the name does not contain a newline, SQL identifier + * quoting satisfies the psql meta-command parser. Prefer not to + * involve psql-interpreted single quotes, which behaved differently + * before PostgreSQL 9.2. + */ + appendPQExpBufferStr(buf, fmtIdEnc(connstr.data, PG_SQL_ASCII)); + + termPQExpBuffer(&connstr); + } + else + { + appendPQExpBufferStr(buf, "\\connect "); + appendPQExpBufferStr(buf, fmtIdEnc(dbname, PG_SQL_ASCII)); + } + appendPQExpBufferChar(buf, '\n'); +} + + +/* + * Deconstruct the text representation of a 1-dimensional Postgres array + * into individual items. + * + * On success, returns true and sets *itemarray and *nitems to describe + * an array of individual strings. On parse failure, returns false; + * *itemarray may exist or be NULL. + * + * NOTE: free'ing itemarray is sufficient to deallocate the working storage. + */ +bool +parsePGArray(const char *atext, char ***itemarray, int *nitems) +{ + int inputlen; + char **items; + char *strings; + int curitem; + + /* + * We expect input in the form of "{item,item,item}" where any item is + * either raw data, or surrounded by double quotes (in which case embedded + * characters including backslashes and quotes are backslashed). + * + * We build the result as an array of pointers followed by the actual + * string data, all in one malloc block for convenience of deallocation. + * The worst-case storage need is not more than one pointer and one + * character for each input character (consider "{,,,,,,,,,,}"). + */ + *itemarray = NULL; + *nitems = 0; + inputlen = strlen(atext); + if (inputlen < 2 || atext[0] != '{' || atext[inputlen - 1] != '}') + return false; /* bad input */ + items = (char **) malloc(inputlen * (sizeof(char *) + sizeof(char))); + if (items == NULL) + return false; /* out of memory */ + *itemarray = items; + strings = (char *) (items + inputlen); + + atext++; /* advance over initial '{' */ + curitem = 0; + while (*atext != '}') + { + if (*atext == '\0') + return false; /* premature end of string */ + items[curitem] = strings; + while (*atext != '}' && *atext != ',') + { + if (*atext == '\0') + return false; /* premature end of string */ + if (*atext != '"') + *strings++ = *atext++; /* copy unquoted data */ + else + { + /* process quoted substring */ + atext++; + while (*atext != '"') + { + if (*atext == '\0') + return false; /* premature end of string */ + if (*atext == '\\') + { + atext++; + if (*atext == '\0') + return false; /* premature end of string */ + } + *strings++ = *atext++; /* copy quoted data */ + } + atext++; + } + } + *strings++ = '\0'; + if (*atext == ',') + atext++; + curitem++; + } + if (atext[1] != '\0') + return false; /* bogus syntax (embedded '}') */ + *nitems = curitem; + return true; +} + + +/* + * Append one element to the text representation of a 1-dimensional Postgres + * array. + * + * The caller must provide the initial '{' and closing '}' of the array. + * This function handles all else, including insertion of commas and + * quoting of values. + * + * We assume that typdelim is ','. + */ +void +appendPGArray(PQExpBuffer buffer, const char *value) +{ + bool needquote; + const char *tmp; + + if (buffer->data[buffer->len - 1] != '{') + appendPQExpBufferChar(buffer, ','); + + /* Decide if we need quotes; this should match array_out()'s choices. */ + if (value[0] == '\0') + needquote = true; /* force quotes for empty string */ + else if (pg_strcasecmp(value, "NULL") == 0) + needquote = true; /* force quotes for literal NULL */ + else + needquote = false; + + if (!needquote) + { + for (tmp = value; *tmp; tmp++) + { + char ch = *tmp; + + if (ch == '"' || ch == '\\' || + ch == '{' || ch == '}' || ch == ',' || + /* these match scanner_isspace(): */ + ch == ' ' || ch == '\t' || ch == '\n' || + ch == '\r' || ch == '\v' || ch == '\f') + { + needquote = true; + break; + } + } + } + + if (needquote) + { + appendPQExpBufferChar(buffer, '"'); + for (tmp = value; *tmp; tmp++) + { + char ch = *tmp; + + if (ch == '"' || ch == '\\') + appendPQExpBufferChar(buffer, '\\'); + appendPQExpBufferChar(buffer, ch); + } + appendPQExpBufferChar(buffer, '"'); + } + else + appendPQExpBufferStr(buffer, value); +} + + +/* + * Format a reloptions array and append it to the given buffer. + * + * "prefix" is prepended to the option names; typically it's "" or "toast.". + * + * Returns false if the reloptions array could not be parsed (in which case + * nothing will have been appended to the buffer), or true on success. + * + * Note: this logic should generally match the backend's flatten_reloptions() + * (in adt/ruleutils.c). + */ +bool +appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, + const char *prefix, int encoding, bool std_strings) +{ + char **options; + int noptions; + int i; + + if (!parsePGArray(reloptions, &options, &noptions)) + { + free(options); + return false; + } + + for (i = 0; i < noptions; i++) + { + char *option = options[i]; + char *name; + char *separator; + char *value; + + /* + * Each array element should have the form name=value. If the "=" is + * missing for some reason, treat it like an empty value. + */ + name = option; + separator = strchr(option, '='); + if (separator) + { + *separator = '\0'; + value = separator + 1; + } + else + value = ""; + + if (i > 0) + appendPQExpBufferStr(buffer, ", "); + appendPQExpBuffer(buffer, "%s%s=", prefix, fmtId(name)); + + /* + * In general we need to quote the value; but to avoid unnecessary + * clutter, do not quote if it is an identifier that would not need + * quoting. (We could also allow numbers, but that is a bit trickier + * than it looks --- for example, are leading zeroes significant? We + * don't want to assume very much here about what custom reloptions + * might mean.) + */ + if (strcmp(fmtId(value), value) == 0) + appendPQExpBufferStr(buffer, value); + else + appendStringLiteral(buffer, value, encoding, std_strings); + } + + free(options); + + return true; +} + + +/* + * processSQLNamePattern + * + * Scan a wildcard-pattern string and generate appropriate WHERE clauses + * to limit the set of objects returned. The WHERE clauses are appended + * to the already-partially-constructed query in buf. Returns whether + * any clause was added. + * + * conn: connection query will be sent to (consulted for escaping rules). + * buf: output parameter. + * pattern: user-specified pattern option, or NULL if none ("*" is implied). + * have_where: true if caller already emitted "WHERE" (clauses will be ANDed + * onto the existing WHERE clause). + * force_escape: always quote regexp special characters, even outside + * double quotes (else they are quoted only between double quotes). + * schemavar: name of query variable to match against a schema-name pattern. + * Can be NULL if no schema. + * namevar: name of query variable to match against an object-name pattern. + * altnamevar: NULL, or name of an alternative variable to match against name. + * visibilityrule: clause to use if we want to restrict to visible objects + * (for example, "pg_catalog.pg_table_is_visible(p.oid)"). Can be NULL. + * dbnamebuf: output parameter receiving the database name portion of the + * pattern, if any. Can be NULL. + * dotcnt: how many separators were parsed from the pattern, by reference. + * + * Formatting note: the text already present in buf should end with a newline. + * The appended text, if any, will end with one too. + */ +bool +processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, + bool have_where, bool force_escape, + const char *schemavar, const char *namevar, + const char *altnamevar, const char *visibilityrule, + PQExpBuffer dbnamebuf, int *dotcnt) +{ + PQExpBufferData schemabuf; + PQExpBufferData namebuf; + bool added_clause = false; + int dcnt; + +#define WHEREAND() \ + (appendPQExpBufferStr(buf, have_where ? " AND " : "WHERE "), \ + have_where = true, added_clause = true) + + if (dotcnt == NULL) + dotcnt = &dcnt; + *dotcnt = 0; + if (pattern == NULL) + { + /* Default: select all visible objects */ + if (visibilityrule) + { + WHEREAND(); + appendPQExpBuffer(buf, "%s\n", visibilityrule); + } + return added_clause; + } + + initPQExpBuffer(&schemabuf); + initPQExpBuffer(&namebuf); + + /* + * Convert shell-style 'pattern' into the regular expression(s) we want to + * execute. Quoting/escaping into SQL literal format will be done below + * using appendStringLiteralConn(). + * + * If the caller provided a schemavar, we want to split the pattern on + * ".", otherwise not. + */ + patternToSQLRegex(PQclientEncoding(conn), + (schemavar ? dbnamebuf : NULL), + (schemavar ? &schemabuf : NULL), + &namebuf, + pattern, force_escape, true, dotcnt); + + /* + * Now decide what we need to emit. We may run under a hostile + * search_path, so qualify EVERY name. Note there will be a leading "^(" + * in the patterns in any case. + * + * We want the regex matches to use the database's default collation where + * collation-sensitive behavior is required (for example, which characters + * match '\w'). That happened by default before PG v12, but if the server + * is >= v12 then we need to force it through explicit COLLATE clauses, + * otherwise the "C" collation attached to "name" catalog columns wins. + */ + if (namevar && namebuf.len > 2) + { + /* We have a name pattern, so constrain the namevar(s) */ + + /* Optimize away a "*" pattern */ + if (strcmp(namebuf.data, "^(.*)$") != 0) + { + WHEREAND(); + if (altnamevar) + { + appendPQExpBuffer(buf, + "(%s OPERATOR(pg_catalog.~) ", namevar); + appendStringLiteralConn(buf, namebuf.data, conn); + if (PQserverVersion(conn) >= 120000) + appendPQExpBufferStr(buf, " COLLATE pg_catalog.default"); + appendPQExpBuffer(buf, + "\n OR %s OPERATOR(pg_catalog.~) ", + altnamevar); + appendStringLiteralConn(buf, namebuf.data, conn); + if (PQserverVersion(conn) >= 120000) + appendPQExpBufferStr(buf, " COLLATE pg_catalog.default"); + appendPQExpBufferStr(buf, ")\n"); + } + else + { + appendPQExpBuffer(buf, "%s OPERATOR(pg_catalog.~) ", namevar); + appendStringLiteralConn(buf, namebuf.data, conn); + if (PQserverVersion(conn) >= 120000) + appendPQExpBufferStr(buf, " COLLATE pg_catalog.default"); + appendPQExpBufferChar(buf, '\n'); + } + } + } + + if (schemavar && schemabuf.len > 2) + { + /* We have a schema pattern, so constrain the schemavar */ + + /* Optimize away a "*" pattern */ + if (strcmp(schemabuf.data, "^(.*)$") != 0 && schemavar) + { + WHEREAND(); + appendPQExpBuffer(buf, "%s OPERATOR(pg_catalog.~) ", schemavar); + appendStringLiteralConn(buf, schemabuf.data, conn); + if (PQserverVersion(conn) >= 120000) + appendPQExpBufferStr(buf, " COLLATE pg_catalog.default"); + appendPQExpBufferChar(buf, '\n'); + } + } + else + { + /* No schema pattern given, so select only visible objects */ + if (visibilityrule) + { + WHEREAND(); + appendPQExpBuffer(buf, "%s\n", visibilityrule); + } + } + + termPQExpBuffer(&schemabuf); + termPQExpBuffer(&namebuf); + + return added_clause; +#undef WHEREAND +} + +/* + * Transform a possibly qualified shell-style object name pattern into up to + * three SQL-style regular expressions, converting quotes, lower-casing + * unquoted letters, and adjusting shell-style wildcard characters into regexp + * notation. + * + * If the dbnamebuf and schemabuf arguments are non-NULL, and the pattern + * contains two or more dbname/schema/name separators, we parse the portions of + * the pattern prior to the first and second separators into dbnamebuf and + * schemabuf, and the rest into namebuf. + * + * If dbnamebuf is NULL and schemabuf is non-NULL, and the pattern contains at + * least one separator, we parse the first portion into schemabuf and the rest + * into namebuf. + * + * Otherwise, we parse all the pattern into namebuf. + * + * If the pattern contains more dotted parts than buffers to parse into, the + * extra dots will be treated as literal characters and written into the + * namebuf, though they will be counted. Callers should always check the value + * returned by reference in dotcnt and handle this error case appropriately. + * + * We surround the regexps with "^(...)$" to force them to match whole strings, + * as per SQL practice. We have to have parens in case strings contain "|", + * else the "^" and "$" will be bound into the first and last alternatives + * which is not what we want. Whether this is done for dbnamebuf is controlled + * by the want_literal_dbname parameter. + * + * The regexps we parse into the buffers are appended to the data (if any) + * already present. If we parse fewer fields than the number of buffers we + * were given, the extra buffers are unaltered. + * + * encoding: the character encoding for the given pattern + * dbnamebuf: output parameter receiving the database name portion of the + * pattern, if any. Can be NULL. + * schemabuf: output parameter receiving the schema name portion of the + * pattern, if any. Can be NULL. + * namebuf: output parameter receiving the database name portion of the + * pattern, if any. Can be NULL. + * pattern: user-specified pattern option, or NULL if none ("*" is implied). + * force_escape: always quote regexp special characters, even outside + * double quotes (else they are quoted only between double quotes). + * want_literal_dbname: if true, regexp special characters within the database + * name portion of the pattern will not be escaped, nor will the dbname be + * converted into a regular expression. + * dotcnt: output parameter receiving the number of separators parsed from the + * pattern. + */ +void +patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf, + PQExpBuffer namebuf, const char *pattern, bool force_escape, + bool want_literal_dbname, int *dotcnt) +{ + PQExpBufferData buf[3]; + PQExpBufferData left_literal; + PQExpBuffer curbuf; + PQExpBuffer maxbuf; + int i; + bool inquotes; + bool left; + const char *cp; + + Assert(pattern != NULL); + Assert(namebuf != NULL); + + /* callers should never expect "dbname.relname" format */ + Assert(dbnamebuf == NULL || schemabuf != NULL); + Assert(dotcnt != NULL); + + *dotcnt = 0; + inquotes = false; + cp = pattern; + + if (dbnamebuf != NULL) + maxbuf = &buf[2]; + else if (schemabuf != NULL) + maxbuf = &buf[1]; + else + maxbuf = &buf[0]; + + curbuf = &buf[0]; + if (want_literal_dbname) + { + left = true; + initPQExpBuffer(&left_literal); + } + else + left = false; + initPQExpBuffer(curbuf); + appendPQExpBufferStr(curbuf, "^("); + while (*cp) + { + char ch = *cp; + + if (ch == '"') + { + if (inquotes && cp[1] == '"') + { + /* emit one quote, stay in inquotes mode */ + appendPQExpBufferChar(curbuf, '"'); + if (left) + appendPQExpBufferChar(&left_literal, '"'); + cp++; + } + else + inquotes = !inquotes; + cp++; + } + else if (!inquotes && isupper((unsigned char) ch)) + { + appendPQExpBufferChar(curbuf, + pg_tolower((unsigned char) ch)); + if (left) + appendPQExpBufferChar(&left_literal, + pg_tolower((unsigned char) ch)); + cp++; + } + else if (!inquotes && ch == '*') + { + appendPQExpBufferStr(curbuf, ".*"); + if (left) + appendPQExpBufferChar(&left_literal, '*'); + cp++; + } + else if (!inquotes && ch == '?') + { + appendPQExpBufferChar(curbuf, '.'); + if (left) + appendPQExpBufferChar(&left_literal, '?'); + cp++; + } + else if (!inquotes && ch == '.') + { + left = false; + if (dotcnt) + (*dotcnt)++; + if (curbuf < maxbuf) + { + appendPQExpBufferStr(curbuf, ")$"); + curbuf++; + initPQExpBuffer(curbuf); + appendPQExpBufferStr(curbuf, "^("); + cp++; + } + else + appendPQExpBufferChar(curbuf, *cp++); + } + else if (ch == '$') + { + /* + * Dollar is always quoted, whether inside quotes or not. The + * reason is that it's allowed in SQL identifiers, so there's a + * significant use-case for treating it literally, while because + * we anchor the pattern automatically there is no use-case for + * having it possess its regexp meaning. + */ + appendPQExpBufferStr(curbuf, "\\$"); + if (left) + appendPQExpBufferChar(&left_literal, '$'); + cp++; + } + else + { + /* + * Ordinary data character, transfer to pattern + * + * Inside double quotes, or at all times if force_escape is true, + * quote regexp special characters with a backslash to avoid + * regexp errors. Outside quotes, however, let them pass through + * as-is; this lets knowledgeable users build regexp expressions + * that are more powerful than shell-style patterns. + * + * As an exception to that, though, always quote "[]", as that's + * much more likely to be an attempt to write an array type name + * than it is to be the start of a regexp bracket expression. + */ + if ((inquotes || force_escape) && + strchr("|*+?()[]{}.^$\\", ch)) + appendPQExpBufferChar(curbuf, '\\'); + else if (ch == '[' && cp[1] == ']') + appendPQExpBufferChar(curbuf, '\\'); + i = PQmblenBounded(cp, encoding); + while (i--) + { + if (left) + appendPQExpBufferChar(&left_literal, *cp); + appendPQExpBufferChar(curbuf, *cp++); + } + } + } + appendPQExpBufferStr(curbuf, ")$"); + + if (namebuf) + { + appendPQExpBufferStr(namebuf, curbuf->data); + termPQExpBuffer(curbuf); + curbuf--; + } + + if (schemabuf && curbuf >= buf) + { + appendPQExpBufferStr(schemabuf, curbuf->data); + termPQExpBuffer(curbuf); + curbuf--; + } + + if (dbnamebuf && curbuf >= buf) + { + if (want_literal_dbname) + appendPQExpBufferStr(dbnamebuf, left_literal.data); + else + appendPQExpBufferStr(dbnamebuf, curbuf->data); + termPQExpBuffer(curbuf); + } + + if (want_literal_dbname) + termPQExpBuffer(&left_literal); +} diff --git a/src/compat/19/pgactive_compat.h b/src/compat/19/pgactive_compat.h new file mode 100644 index 00000000..d0f594ae --- /dev/null +++ b/src/compat/19/pgactive_compat.h @@ -0,0 +1,95 @@ +#ifndef PG_pgactive_COMPAT_H +#define PG_pgactive_COMPAT_H + +#include "access/heapam.h" +#include "access/genam.h" +#include "catalog/pg_namespace.h" +#include "common/hashfn.h" +#include "access/htup_details.h" +#include "utils/syscache.h" + +/* + * XXX Should it be table_slot_create for >= 12 instead of + * ExecInitExtraTupleSlot? + */ +#define ExecInitExtraTupleSlotpgactive(estate, a) \ + ExecInitExtraTupleSlot(estate, a, &TTSOpsHeapTuple) + +#define pg_analyze_and_rewrite(parsetree, query_string, paramTypes, numParams) \ + pg_analyze_and_rewrite_fixedparams(parsetree, query_string, paramTypes, numParams, NULL) + +#define IsKnownTag(tag) (tag != CMDTAG_UNKNOWN) + +#define HeapTupleHeaderGetOid(tup) \ +( \ + ((tup)->t_infomask & HEAP_HASOID_OLD) ? \ + *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) \ + : \ + InvalidOid \ +) + +#define transformAlterTableStmtpgactive(relid, astmt, queryString) \ + transformAlterTableStmt(relid, astmt, queryString, &beforeStmts, &afterStmts) + +#define TTS_TUP(slot) (((HeapTupleTableSlot *)slot)->tuple) + +#define pgactiveGetSysCacheOid1 GetSysCacheOid1 + +#define pgactiveGetSysCacheOid2 GetSysCacheOid2 + +/* GetSysCacheOid2 equivalent that errors out if nothing is found */ +static inline Oid +GetSysCacheOid2Error(int cacheId, AttrNumber oidcol, + Datum key1, Datum key2) +{ + Oid result; + + result = GetSysCacheOid2(cacheId, oidcol, key1, key2); + + if (result == InvalidOid) + elog(ERROR, "cache lookup failure in cache %d", cacheId); + + return result; +} + +#define pgactiveGetSysCacheOid2Error(cacheId, oidcol, key1, key2) \ + GetSysCacheOid2Error(cacheId, oidcol, key1, key2) + +/* 2a10fdc4307a667883f7a3369cb93a721ade9680 */ +#define getObjectDescription(object) getObjectDescription(object, false) + +/* e997a0c642860a96df0151cbeccfecbdf0450d08 */ +#define GetFlushRecPtr() GetFlushRecPtr(NULL) + +/* 1a0586de3657cd35581f0639c87d5050c6197bb7 */ +#define MakeSingleTupleTableSlot(tupdesc) \ + MakeSingleTupleTableSlot(tupdesc, &TTSOpsHeapTuple) + +#define pg_class_ownercheck(objectid, roleid) \ + object_ownercheck(RelationRelationId, objectid, roleid) + +#define pg_database_ownercheck(objectid, roleid) \ + object_ownercheck(DatabaseRelationId, objectid, roleid) + +#define pg_namespace_ownercheck(objectid, roleid) \ + object_ownercheck(NamespaceRelationId, objectid, roleid) + +#define pg_foreign_server_aclcheck(objectid, roleid, mode) \ + object_aclcheck(ForeignServerRelationId, objectid, roleid, mode) + +#define RepOriginId ReplOriginId +#define InvalidRepOriginId InvalidReplOriginId +#define replorigin_session_origin replorigin_xact_state.origin +#define replorigin_session_origin_lsn replorigin_xact_state.origin_lsn +#define replorigin_session_origin_timestamp replorigin_xact_state.origin_timestamp + +/* + * PG 19: AssertVariableIsOfType was renamed to StaticAssertVariableIsOfType + */ +#ifndef AssertVariableIsOfType +#define AssertVariableIsOfType StaticAssertVariableIsOfType +#endif + + + +#endif diff --git a/src/compat/README.pg_dump b/src/compat/README.pg_dump index d85f5390..0ecdaf1e 100644 --- a/src/compat/README.pg_dump +++ b/src/compat/README.pg_dump @@ -39,4 +39,4 @@ Following table shows commit hash of pg_dump source | 16 | 327bd6111aede2a41f8891d2cd2501c773bc047e | | 17 | e81a5031d51f9cd68adb7bdccc424880bac6838e | | 18 | ca938ec213d954aaa9eba60bbbfbfa924fac0d7a | - +| 19 | da6874635db2e8ab95150481c992f03ec6094805 | diff --git a/src/pgactive.c b/src/pgactive.c index e6c69e5a..908ff4d0 100644 --- a/src/pgactive.c +++ b/src/pgactive.c @@ -54,6 +54,9 @@ #include "replication/origin.h" +#if PG_VERSION_NUM >= 190000 +#include "storage/fd.h" +#endif #include "storage/latch.h" #include "storage/lmgr.h" #include "storage/lwlock.h" @@ -109,8 +112,10 @@ int pgactive_max_databases; bool pgactive_skip_ddl_replication; bool prev_pgactive_skip_ddl_replication; -/* replaced by pgactive_skip_ddl_replication for now -bool pgactive_skip_ddl_locking; */ +/* + * replaced by pgactive_skip_ddl_replication for now + * bool pgactive_skip_ddl_locking; + */ bool pgactive_do_not_replicate; bool pgactive_discard_mismatched_row_attributes; bool pgactive_debug_trace_replay; @@ -494,7 +499,7 @@ pgactive_connect(const char *conninfo, */ static void pgactive_create_slot(PGconn *streamConn, Name slot_name, char *remote_ident, - RepOriginId *replication_identifier, char *snapshot) + RepOriginId * replication_identifier, char *snapshot) { StringInfoData query; PGresult *res; @@ -772,7 +777,7 @@ pgactive_establish_connection_and_slot(const char *dsn, const char *application_name_suffix, Name out_slot_name, pgactiveNodeId * out_nodeid, - RepOriginId *out_rep_origin_id, + RepOriginId * out_rep_origin_id, char *out_snapshot) { PGconn *streamConn; @@ -1039,26 +1044,27 @@ _PG_init(void) PGC_SIGHUP, 0, NULL, NULL, NULL); -/* replaced by pgactive_skip_ddl_replication for now - DefineCustomBoolVariable("pgactive.permit_ddl_locking", - "Allow commands that can acquire global DDL lock.", - NULL, - &pgactive_permit_ddl_locking, - true, - PGC_USERSET, - 0, - NULL, NULL, NULL); - - DefineCustomBoolVariable("pgactive.permit_unsafe_ddl_commands", - "Allow commands that might cause data or " \ - "replication problems under pgactive to run.", - NULL, - &pgactive_permit_unsafe_commands, - false, - PGC_SUSET, - 0, - pgactive_permit_unsafe_guc_check_hook, NULL, NULL); -*/ +/* + * replaced by pgactive_skip_ddl_replication for now + * DefineCustomBoolVariable("pgactive.permit_ddl_locking", + * "Allow commands that can acquire global DDL lock.", + * NULL, + * &pgactive_permit_ddl_locking, + * true, + * PGC_USERSET, + * 0, + * NULL, NULL, NULL); + * + * DefineCustomBoolVariable("pgactive.permit_unsafe_ddl_commands", + * "Allow commands that might cause data or " \ + * "replication problems under pgactive to run.", + * NULL, + * &pgactive_permit_unsafe_commands, + * false, + * PGC_SUSET, + * 0, + * pgactive_permit_unsafe_guc_check_hook, NULL, NULL); + */ DefineCustomBoolVariable("pgactive.skip_ddl_replication", "Internal. DDL replication in pgactive is not a fully supported feature yet.", @@ -1069,16 +1075,17 @@ _PG_init(void) PGC_SUSET, 0, pgactive_permit_unsafe_guc_check_hook, NULL, NULL); -/* replaced by pgactive_skip_ddl_replication for now - DefineCustomBoolVariable("pgactive.skip_ddl_locking", - "Don't acquire global DDL locks while performing DDL.", - "Note that it's quite dangerous to do so.", - &pgactive_skip_ddl_locking, - false, - PGC_SUSET, - 0, - pgactive_permit_unsafe_guc_check_hook, NULL, NULL); -*/ +/* + * replaced by pgactive_skip_ddl_replication for now + * DefineCustomBoolVariable("pgactive.skip_ddl_locking", + * "Don't acquire global DDL locks while performing DDL.", + * "Note that it's quite dangerous to do so.", + * &pgactive_skip_ddl_locking, + * false, + * PGC_SUSET, + * 0, + * pgactive_permit_unsafe_guc_check_hook, NULL, NULL); + */ DefineCustomIntVariable("pgactive.debug_apply_delay", "Sets apply delay for all configured pgactive connections.", "A transaction won't be replayed until at least apply_delay " diff --git a/src/pgactive_apply.c b/src/pgactive_apply.c index 674a20a3..dda214bb 100644 --- a/src/pgactive_apply.c +++ b/src/pgactive_apply.c @@ -65,11 +65,12 @@ #include "utils/memutils.h" #include "utils/snapmgr.h" -/* Useful for development: -#define VERBOSE_INSERT -#define VERBOSE_DELETE -#define VERBOSE_UPDATE -*/ +/* + * Useful for development: + * #define VERBOSE_INSERT + * #define VERBOSE_DELETE + * #define VERBOSE_UPDATE + */ /* Relation oid cache; initialized then left unchanged */ Oid QueuedDDLCommandsRelid = InvalidOid; @@ -141,7 +142,7 @@ static void process_remote_delete(StringInfo s); static void get_local_tuple_origin(HeapTuple tuple, TimestampTz *commit_ts, - RepOriginId *node_id); + RepOriginId * node_id); static void abs_timestamp_difference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs); @@ -1384,7 +1385,7 @@ process_remote_delete(StringInfo s) * Get commit timestamp and origin of the tuple */ static void -get_local_tuple_origin(HeapTuple tuple, TimestampTz *commit_ts, RepOriginId *node_id) +get_local_tuple_origin(HeapTuple tuple, TimestampTz *commit_ts, RepOriginId * node_id) { TransactionId xmin; RepOriginId node_id_raw; @@ -2568,7 +2569,7 @@ log_tuple(const char *format, TupleDesc desc, HeapTuple tup) * since we'd have to reconnect to apply most kinds of change anyway. */ static void -pgactive_apply_reload_config() +pgactive_apply_reload_config(void) { pgactiveConnectionConfig *new_apply_config; diff --git a/src/pgactive_catalogs.c b/src/pgactive_catalogs.c index df7b3e23..6e98b41e 100644 --- a/src/pgactive_catalogs.c +++ b/src/pgactive_catalogs.c @@ -34,6 +34,7 @@ #include "utils/builtins.h" #include "utils/fmgroids.h" #include "utils/guc.h" +#include "utils/lsyscache.h" #include "utils/memutils.h" #include "utils/rel.h" #include "utils/snapmgr.h" @@ -435,7 +436,7 @@ pgactive_fetch_node_id_via_sysid(const pgactiveNodeId * const node) * no corresponding pgactive.pgactive_nodes row also get ignored. */ List * -pgactive_read_connection_configs() +pgactive_read_connection_configs(void) { HeapTuple tuple; StringInfoData query; diff --git a/src/pgactive_commandfilter.c b/src/pgactive_commandfilter.c index 3d84a406..63c043c9 100644 --- a/src/pgactive_commandfilter.c +++ b/src/pgactive_commandfilter.c @@ -46,17 +46,19 @@ #include "utils/rel.h" /* -* pgactive_commandfilter.c: a ProcessUtility_hook to prevent a cluster from running -* commands that pgactive does not yet support. -*/ + * pgactive_commandfilter.c: a ProcessUtility_hook to prevent a cluster from running + * commands that pgactive does not yet support. + */ static ProcessUtility_hook_type next_ProcessUtility_hook = NULL; static ClientAuthentication_hook_type next_ClientAuthentication_hook = NULL; /* GUCs */ -/* replaced by pgactive_skip_ddl_replication for now -bool pgactive_permit_unsafe_commands = false; */ +/* + * replaced by pgactive_skip_ddl_replication for now + * bool pgactive_permit_unsafe_commands = false; + */ #if PG_VERSION_NUM >= 120000 static bool default_with_oids = false; @@ -68,11 +70,11 @@ static int pgactive_ddl_nestlevel = 0; bool pgactive_in_extension = false; /* -* Check the passed rangevar, locking it and looking it up in the cache -* then determine if the relation requires logging to WAL. If it does, then -* right now pgactive won't cope with it and we must reject the operation that -* touches this relation. -*/ + * Check the passed rangevar, locking it and looking it up in the cache + * then determine if the relation requires logging to WAL. If it does, then + * right now pgactive won't cope with it and we must reject the operation that + * touches this relation. + */ static void error_on_persistent_rv(RangeVar *rv, const char *cmdtag, @@ -311,7 +313,7 @@ filter_AlterTableStmt(Node *parsetree, astmt->missing_ok); } } - /* FALLTHROUGH */ + pg_fallthrough; case AT_AddIndex: /* produced by for example ALTER TABLE … * ADD CONSTRAINT … PRIMARY KEY */ { @@ -335,7 +337,7 @@ filter_AlterTableStmt(Node *parsetree, } } - /* FALLTHROUGH */ + pg_fallthrough; case AT_DropColumn: case AT_DropNotNull: case AT_SetNotNull: @@ -1081,7 +1083,11 @@ pgactive_commandfilter(PlannedStmt *pstmt, case T_CheckPointStmt: case T_ReindexStmt: case T_VacuumStmt: +#if PG_VERSION_NUM < 190000 case T_ClusterStmt: +#else + case T_RepackStmt: +#endif goto done; /* @@ -1181,6 +1187,7 @@ pgactive_commandfilter(PlannedStmt *pstmt, pgactive_commandfilter_dbname(((RenameStmt *) parsetree)->newname); goto done; } + pg_fallthrough; default: break; @@ -1601,7 +1608,7 @@ pgactive_commandfilter(PlannedStmt *pstmt, case T_DropStmt: if (((DropStmt *) parsetree)->removeType != OBJECT_EXTENSION) break; - /* FALLTHROUGH */ + pg_fallthrough; case T_CreateExtensionStmt: case T_AlterExtensionStmt: case T_AlterExtensionContentsStmt: diff --git a/src/pgactive_conflict_logging.c b/src/pgactive_conflict_logging.c index e89ee931..ce86bac4 100644 --- a/src/pgactive_conflict_logging.c +++ b/src/pgactive_conflict_logging.c @@ -62,7 +62,7 @@ static MemoryContext conflict_log_context; * toggled at runtime. */ void -pgactive_conflict_logging_startup() +pgactive_conflict_logging_startup(void) { Oid schema_oid; @@ -292,7 +292,7 @@ tuple_to_stringinfo(StringInfo s, TupleDesc tupdesc, HeapTuple tuple) val = (Datum) 0; if (isnull) outputstr = "(null)"; - else if (typisvarlena && VARATT_IS_EXTERNAL_ONDISK(origval)) + else if (typisvarlena && VARATT_IS_EXTERNAL_ONDISK((const void *) origval)) outputstr = "(unchanged-toast-datum)"; else if (typisvarlena) val = PointerGetDatum(PG_DETOAST_DATUM(origval)); diff --git a/src/pgactive_dbcache.c b/src/pgactive_dbcache.c index d76f23ce..091acabf 100644 --- a/src/pgactive_dbcache.c +++ b/src/pgactive_dbcache.c @@ -62,7 +62,7 @@ pgactive_dbcache_invalidate_entry(Datum arg, int cacheid, uint32 hashvalue) } static void -pgactive_dbcache_initialize() +pgactive_dbcache_initialize(void) { HASHCTL ctl; diff --git a/src/pgactive_executor.c b/src/pgactive_executor.c index 2d3c11c0..75ec4742 100644 --- a/src/pgactive_executor.c +++ b/src/pgactive_executor.c @@ -97,25 +97,38 @@ UserTableUpdateOpenIndexes(EState *estate, TupleTableSlot *slot, if (relinfo->ri_NumIndices > 0) { recheckIndexes = ExecInsertIndexTuples( -#if PG_VERSION_NUM >= 140000 +#if PG_VERSION_NUM >= 190000 relinfo, -#endif + estate, + update ? EIIT_IS_UPDATE : 0, slot, -#if PG_VERSION_NUM < 120000 - &slot->tts_tuple->t_self, -#endif - estate -#if PG_VERSION_NUM >= 140000 - ,update -#endif - ,false - ,NULL - ,NIL -#if PG_VERSION_NUM >= 160000 - ,false); + NIL, + NULL +#elif PG_VERSION_NUM >= 160000 + relinfo, + slot, + estate, + update, + false, + NULL, + NIL, + false +#elif PG_VERSION_NUM >= 140000 + relinfo, + slot, + estate, + update, + false, + NULL, + NIL #else - ); + slot, + estate, + false, + NULL, + NIL #endif + ); if (recheckIndexes != NIL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), @@ -262,7 +275,11 @@ find_pkey_tuple(ScanKey skey, pgactiveRelation * rel, Relation idxrel, NULL, #endif RelationGetNumberOfAttributes(idxrel), - 0); + 0 +#if PG_VERSION_NUM >= 190000 + ,0 +#endif + ); index_rescan(scan, skey, RelationGetNumberOfAttributes(idxrel), NULL, 0); #if PG_VERSION_NUM >= 120000 if (index_getnext_slot(scan, ForwardScanDirection, slot)) @@ -488,7 +505,9 @@ pgactiveExecutorStart(QueryDesc *queryDesc, int eflags) { bool performs_writes = false; bool read_only_node; +#if PG_VERSION_NUM < 190000 ListCell *l; +#endif List *rangeTable; PlannedStmt *plannedstmt = queryDesc->plannedstmt; Oid idxoid; @@ -562,9 +581,17 @@ pgactiveExecutorStart(QueryDesc *queryDesc, int eflags) /* Fail if query tries to UPDATE or DELETE any of tables without PK */ rangeTable = plannedstmt->rtable; +#if PG_VERSION_NUM >= 190000 + { + int rtei = -1; + + while ((rtei = bms_next_member(plannedstmt->resultRelationRelids, rtei)) >= 0) + { +#else foreach(l, plannedstmt->resultRelations) { Index rtei = lfirst_int(l); +#endif RangeTblEntry *rte = rt_fetch(rtei, rangeTable); Relation rel; @@ -617,7 +644,12 @@ pgactiveExecutorStart(QueryDesc *queryDesc, int eflags) errhint("Add a PRIMARY KEY to the table."))); RelationClose(rel); +#if PG_VERSION_NUM >= 190000 } +} +#else + } +#endif done: if (PrevExecutorStart_hook) diff --git a/src/pgactive_init_copy.c b/src/pgactive_init_copy.c index c0ea05d5..890cf321 100644 --- a/src/pgactive_init_copy.c +++ b/src/pgactive_init_copy.c @@ -110,16 +110,16 @@ static char *node_name = NULL; static void signal_handler(int sig); static void usage(void); #if PG_VERSION_NUM >= 180000 -pg_noreturn static void finish_die(); -pg_noreturn static void die(const char *fmt,...) +pg_noreturn static void finish_die(void); +pg_noreturn static void die(const char *fmt, ...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2))); #else static void finish_die() pg_attribute_noreturn(); -static void die(const char *fmt,...) +static void die(const char *fmt, ...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2))) pg_attribute_noreturn(); #endif -static void print_msg(VerbosityLevelEnum level, const char *fmt,...) +static void print_msg(VerbosityLevelEnum level, const char *fmt, ...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3))); static int run_pg_ctl(char *cmdargv[], @@ -776,7 +776,7 @@ usage(void) } static void -finish_die() +finish_die(void) { if (local_conn) PQfinish(local_conn); @@ -803,7 +803,7 @@ finish_die() * Print error and exit. */ static void -die(const char *fmt,...) +die(const char *fmt, ...) { va_list argptr; @@ -821,7 +821,7 @@ die(const char *fmt,...) * Print message to stdout and flush */ static void -print_msg(VerbosityLevelEnum level, const char *fmt,...) +print_msg(VerbosityLevelEnum level, const char *fmt, ...) { if (verbosity >= level) { @@ -1999,7 +1999,6 @@ copy_file(char *fromfile, char *tofile) int srcfd; int dstfd; int nbytes; - off_t offset; #define COPY_BUF_SIZE (8 * BLCKSZ) @@ -2024,7 +2023,7 @@ copy_file(char *fromfile, char *tofile) /* * Do the data copying. */ - for (offset = 0;; offset += nbytes) + for (;;) { nbytes = read(srcfd, buffer, COPY_BUF_SIZE); if (nbytes < 0) diff --git a/src/pgactive_init_replica.c b/src/pgactive_init_replica.c index 1414cc33..cc26ba7d 100644 --- a/src/pgactive_init_replica.c +++ b/src/pgactive_init_replica.c @@ -47,6 +47,7 @@ #include "postmaster/bgwriter.h" #include "postmaster/fork_process.h" +#include "storage/fd.h" #include "storage/latch.h" #include "storage/lwlock.h" #include "storage/proc.h" @@ -714,7 +715,7 @@ pgactive_insert_remote_conninfo(PGconn *conn, pgactiveConnectionConfig * myconfi * and it's guaranteed to retain more than the WAL we need. */ static void -pgactive_init_make_other_slots() +pgactive_init_make_other_slots(void) { List *configs; ListCell *lc; @@ -781,7 +782,7 @@ pgactive_init_make_other_slots() * Wait until all such entries are created and active, then return. */ static void -pgactive_init_wait_for_slot_creation() +pgactive_init_wait_for_slot_creation(void) { List *configs; ListCell *lc; @@ -1033,7 +1034,7 @@ pgactive_nodes_set_remote_status_ready(PGconn *conn) * Idle until our local node status goes 'r' */ static void -pgactive_wait_for_local_node_ready() +pgactive_wait_for_local_node_ready(void) { pgactiveNodeStatus status = pgactive_NODE_STATUS_NONE; pgactiveNodeId myid; diff --git a/src/pgactive_locks.c b/src/pgactive_locks.c index 0d8b2ac8..115453d1 100644 --- a/src/pgactive_locks.c +++ b/src/pgactive_locks.c @@ -179,6 +179,9 @@ #include "storage/proc.h" #include "storage/procarray.h" #include "storage/shmem.h" +#if PG_VERSION_NUM >= 190000 +#include "storage/standby.h" +#endif #include "storage/sinvaladt.h" #include "utils/builtins.h" @@ -197,8 +200,10 @@ extern Datum pgactive_get_global_locks_info(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(pgactive_get_global_locks_info); /* GUCs */ -/* replaced by !pgactive_skip_ddl_replication for now -bool pgactive_permit_ddl_locking = false; */ +/* + * replaced by !pgactive_skip_ddl_replication for now + * bool pgactive_permit_ddl_locking = false; + */ /* -1 means use max_standby_streaming_delay */ int pgactive_max_ddl_lock_delay = -1; @@ -376,7 +381,7 @@ pgactive_locks_shmem_startup(void) /* Needs to be called from a shared_preload_library _PG_init() */ void -pgactive_locks_shmem_init() +pgactive_locks_shmem_init(void) { /* Must be called from postmaster its self */ Assert(IsPostmasterEnvironment && !IsUnderPostmaster); @@ -1377,7 +1382,14 @@ cancel_conflicting_transactions(void) else { /* We reached timeout so lets kill the writing transaction */ +#if PG_VERSION_NUM >= 190000 + pid_t p = 0; + + if (SignalRecoveryConflictWithVirtualXID(*conflict, RECOVERY_CONFLICT_LOCK)) + p = ProcNumberGetProc(conflict->procNumber)->pid; +#else pid_t p = CancelVirtualTransaction(*conflict, PROCSIG_RECOVERY_CONFLICT_LOCK); +#endif /* * Either confirm kill or sleep a bit to prevent the other node diff --git a/src/pgactive_nodecache.c b/src/pgactive_nodecache.c index fdbe3234..cfe30bea 100644 --- a/src/pgactive_nodecache.c +++ b/src/pgactive_nodecache.c @@ -95,7 +95,7 @@ pgactive_nodecache_invalidate_callback(Datum arg, Oid relid) } static void -pgactive_nodecache_initialize() +pgactive_nodecache_initialize(void) { HASHCTL ctl; @@ -322,7 +322,7 @@ pgactive_nodeid_name(const pgactiveNodeId * const node, * invalidations, we could get rid of this hack. */ void -pgactive_setup_my_cached_node_names() +pgactive_setup_my_cached_node_names(void) { pgactiveNodeId myid; @@ -345,7 +345,7 @@ pgactive_setup_cached_remote_name(const pgactiveNodeId * const remote_nodeid) } const char * -pgactive_get_my_cached_node_name() +pgactive_get_my_cached_node_name(void) { if (my_node_name != NULL) return my_node_name; diff --git a/src/pgactive_output.c b/src/pgactive_output.c index 2f525f9c..2c31e8a4 100644 --- a/src/pgactive_output.c +++ b/src/pgactive_output.c @@ -68,6 +68,13 @@ extern void _PG_output_plugin_init(OutputPluginCallbacks *cb); +/* PG 15-18 moved commit_time into txn->xact_time.commit_time */ +#if PG_VERSION_NUM >= 150000 && PG_VERSION_NUM < 190000 +#define TXN_COMMIT_TIME(txn) ((txn)->xact_time.commit_time) +#else +#define TXN_COMMIT_TIME(txn) ((txn)->commit_time) +#endif + typedef struct { MemoryContext context; @@ -781,11 +788,7 @@ pg_decode_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn) * end of commit + 1 so that's what gets recorded in replication origins. */ pq_sendint64(ctx->out, txn->end_lsn); -#if PG_VERSION_NUM >= 150000 - pq_sendint64(ctx->out, txn->xact_time.commit_time); -#else - pq_sendint64(ctx->out, txn->commit_time); -#endif + pq_sendint64(ctx->out, TXN_COMMIT_TIME(txn)); pq_sendint(ctx->out, txn->xid, 4); /* and optional data selected above */ @@ -850,19 +853,11 @@ pg_decode_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, */ Assert(txn->end_lsn != InvalidXLogRecPtr); pq_sendint64(ctx->out, txn->end_lsn); -#if PG_VERSION_NUM >= 150000 - pq_sendint64(ctx->out, txn->xact_time.commit_time); -#else - pq_sendint64(ctx->out, txn->commit_time); -#endif + pq_sendint64(ctx->out, TXN_COMMIT_TIME(txn)); OutputPluginWrite(ctx, true); -#if PG_VERSION_NUM >= 150000 - committime = txn->xact_time.commit_time; -#else - committime = txn->commit_time; -#endif + committime = TXN_COMMIT_TIME(txn); /* Save last sent transaction info */ pgactive_walsender_worker->last_sent_xact_id = txn->xid; @@ -1084,7 +1079,7 @@ write_tuple(pgactiveOutputData * data, StringInfo out, Relation rel, pq_sendbyte(out, 'n'); /* null column */ continue; } - else if (att->attlen == -1 && VARATT_IS_EXTERNAL_ONDISK(values[i])) + else if (att->attlen == -1 && VARATT_IS_EXTERNAL_ONDISK(DatumGetPointer(values[i]))) { pq_sendbyte(out, 'u'); /* unchanged toast column */ continue; @@ -1125,7 +1120,7 @@ write_tuple(pgactiveOutputData * data, StringInfo out, Relation rel, char *data = DatumGetPointer(values[i]); /* send indirect datums inline */ - if (VARATT_IS_EXTERNAL_INDIRECT(values[i])) + if (VARATT_IS_EXTERNAL_INDIRECT(DatumGetPointer(values[i]))) { struct varatt_indirect redirect; diff --git a/src/pgactive_relcache.c b/src/pgactive_relcache.c index 7f952c82..7920d5c5 100644 --- a/src/pgactive_relcache.c +++ b/src/pgactive_relcache.c @@ -89,7 +89,7 @@ pgactiveRelcacheHashInvalidateCallback(Datum arg, Oid relid) } static void -pgactive_relcache_initialize() +pgactive_relcache_initialize(void) { HASHCTL ctl; diff --git a/src/pgactive_shmem.c b/src/pgactive_shmem.c index 4299fb8c..15612aad 100644 --- a/src/pgactive_shmem.c +++ b/src/pgactive_shmem.c @@ -89,7 +89,7 @@ pgactive_worker_exit(int code, Datum arg) } static size_t -pgactive_worker_shmem_size() +pgactive_worker_shmem_size(void) { Size size = 0; diff --git a/src/pgactive_supervisor.c b/src/pgactive_supervisor.c index 52c678e0..611410ba 100644 --- a/src/pgactive_supervisor.c +++ b/src/pgactive_supervisor.c @@ -40,6 +40,7 @@ #include "utils/elog.h" #include "utils/fmgroids.h" #include "utils/guc.h" +#include "utils/lsyscache.h" #include "utils/snapmgr.h" static bool destroy_temp_dump_dirs_callback_registered = false; @@ -239,7 +240,7 @@ pgactive_register_perdb_worker(Oid dboid) * TODO DYNCONF: Handle removal of pgactive from DBs */ static void -pgactive_supervisor_rescan_dbs() +pgactive_supervisor_rescan_dbs(void) { Relation secrel; ScanKeyData skey[2]; @@ -374,7 +375,7 @@ pgactive_supervisor_rescan_dbs() * without a DB connection; see comments in pgactive_supervisor_worker_main */ static void -pgactive_supervisor_createdb() +pgactive_supervisor_createdb(void) { Oid dboid; ParseState *pstate; @@ -671,7 +672,7 @@ pgactive_supervisor_worker_main(Datum main_arg) * init callback later. */ void -pgactive_supervisor_register() +pgactive_supervisor_register(void) { BackgroundWorker bgw = {0}; diff --git a/test/expected/ddl_alter_table_5.out b/test/expected/ddl_alter_table_5.out new file mode 100644 index 00000000..a0f4648b --- /dev/null +++ b/test/expected/ddl_alter_table_5.out @@ -0,0 +1,1073 @@ +CREATE TABLE test_tbl(pk int primary key, dropping_col1 text, dropping_col2 text); +ALTER TABLE test_tbl ADD COLUMN col1 text; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +---------------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + dropping_col1 | text | | | | extended | | + dropping_col2 | text | | | | extended | | + col1 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ADD COLUMN col2 text; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +---------------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + dropping_col1 | text | | | | extended | | + dropping_col2 | text | | | | extended | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ADD COLUMN col3_fail timestamptz NOT NULL DEFAULT now(); +ERROR: ALTER TABLE ... ADD COLUMN ... DEFAULT may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_tbl is a regular table +ALTER TABLE test_tbl ADD COLUMN serial_col_node1 SERIAL; +ERROR: ALTER TABLE ... ADD COLUMN ... DEFAULT may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_tbl is a regular table +ALTER TABLE test_tbl DROP COLUMN dropping_col1; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +---------------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + dropping_col2 | text | | | | extended | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl DROP COLUMN dropping_col2; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ALTER COLUMN col1 SET NOT NULL; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + +ALTER TABLE test_tbl ALTER COLUMN col2 SET NOT NULL; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +ALTER TABLE test_tbl ALTER COLUMN col1 DROP NOT NULL; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col2_not_null" NOT NULL "col2" + +ALTER TABLE test_tbl ALTER COLUMN col2 DROP NOT NULL; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ALTER COLUMN col1 SET DEFAULT 'abc'; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | 'abc'::text | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ALTER COLUMN col2 SET DEFAULT 'abc'; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | 'abc'::text | extended | | + col2 | text | | | 'abc'::text | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ALTER COLUMN col1 DROP DEFAULT; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | 'abc'::text | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ALTER COLUMN col2 DROP DEFAULT; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ADD CONSTRAINT test_const CHECK (true); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Check constraints: + "test_const" CHECK (true) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ADD CONSTRAINT test_const1 CHECK (true); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Check constraints: + "test_const" CHECK (true) + "test_const1" CHECK (true) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl DROP CONSTRAINT test_const; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Check constraints: + "test_const1" CHECK (true) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl DROP CONSTRAINT test_const1; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +ALTER TABLE test_tbl ALTER COLUMN col1 SET NOT NULL; +CREATE UNIQUE INDEX test_idx ON test_tbl(col1); +ALTER TABLE test_tbl REPLICA IDENTITY USING INDEX test_idx; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) + "test_idx" UNIQUE, btree (col1) REPLICA IDENTITY +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + +ALTER TABLE test_tbl ALTER COLUMN col2 SET NOT NULL; +CREATE UNIQUE INDEX test_idx1 ON test_tbl(col2); +ALTER TABLE test_tbl REPLICA IDENTITY USING INDEX test_idx1; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) + "test_idx" UNIQUE, btree (col1) + "test_idx1" UNIQUE, btree (col2) REPLICA IDENTITY +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +ALTER TABLE test_tbl REPLICA IDENTITY DEFAULT; +DROP INDEX test_idx; +DROP INDEX test_idx1; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +CREATE UNIQUE INDEX test_idx ON test_tbl(col1); +ALTER TABLE test_tbl REPLICA IDENTITY USING INDEX test_idx; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +DROP INDEX test_idx; +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +CREATE USER test_user; +ALTER TABLE test_tbl OWNER TO test_user; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +ALTER TABLE test_tbl RENAME COLUMN col1 TO foobar; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + foobar | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "foobar" + "test_tbl_col2_not_null" NOT NULL "col2" + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + foobar | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "foobar" + "test_tbl_col2_not_null" NOT NULL "col2" + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + foobar | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "foobar" + "test_tbl_col2_not_null" NOT NULL "col2" + +ALTER TABLE test_tbl RENAME CONSTRAINT test_tbl_pkey TO test_ddl_pk; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + foobar | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_ddl_pk" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "foobar" + "test_tbl_col2_not_null" NOT NULL "col2" + +DROP TABLE test_tbl; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +-- ALTER COLUMN ... SET STATISTICS +\c postgres +CREATE TABLE test_tbl(id int); +ALTER TABLE test_tbl ALTER COLUMN id SET STATISTICS 10; +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | 10 | + +ALTER TABLE test_tbl ALTER COLUMN id SET STATISTICS 0; +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | 0 | + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | 0 | + +ALTER TABLE test_tbl ALTER COLUMN id SET STATISTICS -1; +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | | + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | | + +DROP TABLE test_tbl; +--- INHERITANCE --- +\c postgres +CREATE TABLE test_inh_root (id int primary key, val1 varchar, val2 int); +CREATE TABLE test_inh_chld1 (child1col int) INHERITS (test_inh_root); +CREATE TABLE test_inh_chld2 () INHERITS (test_inh_chld1); +INSERT INTO test_inh_root(id, val1, val2) +SELECT x, x::text, x%4 FROM generate_series(1,10) x; +INSERT INTO test_inh_chld1(id, val1, val2, child1col) +SELECT x, x::text, x%4+1, x*2 FROM generate_series(11,20) x; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_inh_root + Table "public.test_inh_root" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | +Indexes: + "test_inh_root_pkey" PRIMARY KEY, btree (id) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" +Child tables: + test_inh_chld1 + +\d+ test_inh_chld1 + Table "public.test_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_root +Child tables: + test_inh_chld2 + +\d+ test_inh_chld2 + Table "public.test_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_chld1 + +\c regression +\d+ test_inh_root + Table "public.test_inh_root" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | +Indexes: + "test_inh_root_pkey" PRIMARY KEY, btree (id) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" +Child tables: + test_inh_chld1 + +\d+ test_inh_chld1 + Table "public.test_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_root +Child tables: + test_inh_chld2 + +\d+ test_inh_chld2 + Table "public.test_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_chld1 + +SELECT * FROM test_inh_root; + id | val1 | val2 +----+------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 +(20 rows) + +SELECT * FROM test_inh_chld1; + id | val1 | val2 | child1col +----+------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 +(10 rows) + +SELECT * FROM test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +SET pgactive.skip_ddl_replication = true; +ALTER TABLE test_inh_root ADD CONSTRAINT idchk CHECK (id > 0); +ALTER TABLE ONLY test_inh_chld1 ALTER COLUMN id SET DEFAULT 1; +ALTER TABLE ONLY test_inh_root DROP CONSTRAINT idchk; +RESET pgactive.skip_ddl_replication; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_inh_root + Table "public.test_inh_root" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | +Indexes: + "test_inh_root_pkey" PRIMARY KEY, btree (id) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" +Child tables: + test_inh_chld1 + +\d+ test_inh_chld1 + Table "public.test_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | 1 | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Check constraints: + "idchk" CHECK (id > 0) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_root +Child tables: + test_inh_chld2 + +\d+ test_inh_chld2 + Table "public.test_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Check constraints: + "idchk" CHECK (id > 0) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_chld1 + +\c postgres +\d+ test_inh_root + Table "public.test_inh_root" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | +Indexes: + "test_inh_root_pkey" PRIMARY KEY, btree (id) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" +Child tables: + test_inh_chld1 + +\d+ test_inh_chld1 + Table "public.test_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_root +Child tables: + test_inh_chld2 + +\d+ test_inh_chld2 + Table "public.test_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_chld1 + +\c regression +SELECT * FROM test_inh_root; + id | val1 | val2 +----+------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 +(20 rows) + +SELECT * FROM test_inh_chld1; + id | val1 | val2 | child1col +----+------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 +(10 rows) + +SELECT * FROM test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +-- Should fail with an ERROR +ALTER TABLE public.test_inh_chld1 NO INHERIT public.test_inh_root; +ERROR: ALTER TABLE ... [NO] INHERIT may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_inh_chld1 is a regular table +-- Will also fail with an ERROR +SELECT pgactive.pgactive_replicate_ddl_command('ALTER TABLE public.test_inh_chld1 NO INHERIT public.test_inh_root;'); +ERROR: ALTER TABLE ... [NO] INHERIT may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_inh_chld1 is a regular table +CONTEXT: during replay of DDL statement: ALTER TABLE public.test_inh_chld1 NO INHERIT public.test_inh_root; +-- Will be permitted +BEGIN; +SET LOCAL pgactive.skip_ddl_replication = true; +SELECT pgactive.pgactive_replicate_ddl_command('ALTER TABLE public.test_inh_chld1 NO INHERIT public.test_inh_root;'); +ERROR: pgactive_replicate_ddl_command execution attempt rejected by configuration +DETAIL: pgactive.skip_ddl_replication is true. +HINT: See the 'DDL replication' chapter of the documentation. +COMMIT; +SELECT * FROM test_inh_root; + id | val1 | val2 +----+------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 +(20 rows) + +SELECT * FROM test_inh_chld1; + id | val1 | val2 | child1col +----+------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 +(10 rows) + +SELECT * FROM test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +SELECT * FROM test_inh_root; + id | val1 | val2 +----+------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 +(20 rows) + +SELECT * FROM test_inh_chld1; + id | val1 | val2 | child1col +----+------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 +(10 rows) + +SELECT * FROM test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +DELETE FROM test_inh_root WHERE val2 = 0; +ERROR: cannot run UPDATE or DELETE on table test_inh_chld1 because it does not have a PRIMARY KEY +HINT: Add a PRIMARY KEY to the table. +INSERT INTO test_inh_root(id, val1, val2) VALUES (200, 'root', 1); +INSERT INTO test_inh_chld1(id, val1, val2, child1col) VALUES (200, 'child', 0, 0); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +SELECT * FROM test_inh_root; + id | val1 | val2 +-----+-------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 200 | root | 1 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 + 200 | child | 0 +(22 rows) + +SELECT * FROM test_inh_chld1; + id | val1 | val2 | child1col +-----+-------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 + 200 | child | 0 | 0 +(11 rows) + +SELECT * FROM test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +DROP TABLE test_inh_chld2; +DROP TABLE test_inh_chld1; +DROP TABLE test_inh_root; +DROP USER test_user; diff --git a/test/expected/ddl_create_5.out b/test/expected/ddl_create_5.out new file mode 100644 index 00000000..ace94484 --- /dev/null +++ b/test/expected/ddl_create_5.out @@ -0,0 +1,972 @@ +CREATE TABLE test_tbl_simple_create(val int); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_simple_create + Table "public.test_tbl_simple_create" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + +\c postgres +\d+ test_tbl_simple_create + Table "public.test_tbl_simple_create" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + +DROP TABLE test_tbl_simple_create; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_simple_create +\c regression +\d+ test_tbl_simple_create +CREATE UNLOGGED TABLE test_tbl_unlogged(val int); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_unlogged + Unlogged table "public.test_tbl_unlogged" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + +\c postgres +\d+ test_tbl_unlogged +\c regression +\d+ test_tbl_unlogged + Unlogged table "public.test_tbl_unlogged" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + +CREATE TABLE test_tbl_simple_pk(val int PRIMARY KEY); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_simple_pk + Table "public.test_tbl_simple_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | not null | | plain | | +Indexes: + "test_tbl_simple_pk_pkey" PRIMARY KEY, btree (val) +Not-null constraints: + "test_tbl_simple_pk_val_not_null" NOT NULL "val" + +\c postgres +\d+ test_tbl_simple_pk + Table "public.test_tbl_simple_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | not null | | plain | | +Indexes: + "test_tbl_simple_pk_pkey" PRIMARY KEY, btree (val) +Not-null constraints: + "test_tbl_simple_pk_val_not_null" NOT NULL "val" + +DROP TABLE test_tbl_simple_pk; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_simple_pk +\c regression +\d+ test_tbl_simple_pk +CREATE TABLE test_tbl_combined_pk(val int, val1 int, PRIMARY KEY (val, val1)); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_combined_pk + Table "public.test_tbl_combined_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | not null | | plain | | + val1 | integer | | not null | | plain | | +Indexes: + "test_tbl_combined_pk_pkey" PRIMARY KEY, btree (val, val1) +Not-null constraints: + "test_tbl_combined_pk_val_not_null" NOT NULL "val" + "test_tbl_combined_pk_val1_not_null" NOT NULL "val1" + +\c postgres +\d+ test_tbl_combined_pk + Table "public.test_tbl_combined_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | not null | | plain | | + val1 | integer | | not null | | plain | | +Indexes: + "test_tbl_combined_pk_pkey" PRIMARY KEY, btree (val, val1) +Not-null constraints: + "test_tbl_combined_pk_val_not_null" NOT NULL "val" + "test_tbl_combined_pk_val1_not_null" NOT NULL "val1" + +DROP TABLE test_tbl_combined_pk; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_combined_pk +\c regression +\d+ test_tbl_combined_pk +CREATE TABLE test_tbl_serial(val SERIAL); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial + Table "public.test_tbl_serial" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_val_seq'::regclass) | plain | | +Not-null constraints: + "test_tbl_serial_val_not_null" NOT NULL "val" + +\c postgres +\d+ test_tbl_serial + Table "public.test_tbl_serial" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_val_seq'::regclass) | plain | | +Not-null constraints: + "test_tbl_serial_val_not_null" NOT NULL "val" + +DROP TABLE test_tbl_serial; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial +\c regression +\d+ test_tbl_serial +CREATE TABLE test_tbl_serial(val SERIAL); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial + Table "public.test_tbl_serial" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_val_seq'::regclass) | plain | | +Not-null constraints: + "test_tbl_serial_val_not_null" NOT NULL "val" + +\c postgres +\d+ test_tbl_serial + Table "public.test_tbl_serial" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_val_seq'::regclass) | plain | | +Not-null constraints: + "test_tbl_serial_val_not_null" NOT NULL "val" + +CREATE TABLE test_tbl_serial_pk(val SERIAL PRIMARY KEY); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial_pk + Table "public.test_tbl_serial_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_pk_val_seq'::regclass) | plain | | +Indexes: + "test_tbl_serial_pk_pkey" PRIMARY KEY, btree (val) +Not-null constraints: + "test_tbl_serial_pk_val_not_null" NOT NULL "val" + +\c postgres +\d+ test_tbl_serial_pk + Table "public.test_tbl_serial_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_pk_val_seq'::regclass) | plain | | +Indexes: + "test_tbl_serial_pk_pkey" PRIMARY KEY, btree (val) +Not-null constraints: + "test_tbl_serial_pk_val_not_null" NOT NULL "val" + +DROP TABLE test_tbl_serial_pk; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial_pk +\c regression +\d+ test_tbl_serial_pk +CREATE TABLE test_tbl_serial_combined_pk(val SERIAL, val1 INTEGER, PRIMARY KEY (val, val1)); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial_combined_pk + Table "public.test_tbl_serial_combined_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_combined_pk_val_seq'::regclass) | plain | | + val1 | integer | | not null | | plain | | +Indexes: + "test_tbl_serial_combined_pk_pkey" PRIMARY KEY, btree (val, val1) +Not-null constraints: + "test_tbl_serial_combined_pk_val_not_null" NOT NULL "val" + "test_tbl_serial_combined_pk_val1_not_null" NOT NULL "val1" + +\c postgres +\d+ test_tbl_serial_combined_pk + Table "public.test_tbl_serial_combined_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_combined_pk_val_seq'::regclass) | plain | | + val1 | integer | | not null | | plain | | +Indexes: + "test_tbl_serial_combined_pk_pkey" PRIMARY KEY, btree (val, val1) +Not-null constraints: + "test_tbl_serial_combined_pk_val_not_null" NOT NULL "val" + "test_tbl_serial_combined_pk_val1_not_null" NOT NULL "val1" + +CREATE TABLE test_tbl_create_index (val int, val2 int); +CREATE UNIQUE INDEX test1_idx ON test_tbl_create_index(val); +CREATE INDEX test2_idx ON test_tbl_create_index (lower(val2::text)); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test1_idx" UNIQUE, btree (val) + "test2_idx" btree (lower(val2::text)) + +\c regression +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test1_idx" UNIQUE, btree (val) + "test2_idx" btree (lower(val2::text)) + +DROP INDEX test1_idx; +DROP INDEX test2_idx; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | + +\c postgres +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | + +CREATE INDEX test1_idx ON test_tbl_create_index(val, val2); +CREATE INDEX test2_idx ON test_tbl_create_index USING gist (val, UPPER(val2::text)); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test1_idx" btree (val, val2) + "test2_idx" gist (val, upper(val2::text)) + +\c regression +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test1_idx" btree (val, val2) + "test2_idx" gist (val, upper(val2::text)) + +DROP INDEX test1_idx; +DROP INDEX CONCURRENTLY test2_idx; +ERROR: DROP INDEX CONCURRENTLY is not supported without pgactive.skip_ddl_replication set +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test2_idx" gist (val, upper(val2::text)) + +\c postgres +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test2_idx" gist (val, upper(val2::text)) + +CREATE INDEX CONCURRENTLY test1_idx ON test_tbl_create_index(val, val2); +ERROR: CREATE INDEX CONCURRENTLY without pgactive.skip_ddl_replication set may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_tbl_create_index is a regular table +CREATE UNIQUE INDEX CONCURRENTLY test2_idx ON test_tbl_create_index (lower(val2::text)); +ERROR: CREATE INDEX CONCURRENTLY without pgactive.skip_ddl_replication set may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_tbl_create_index is a regular table +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test2_idx" gist (val, upper(val2::text)) + +\c regression +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test2_idx" gist (val, upper(val2::text)) + +DROP INDEX CONCURRENTLY test1_idx; +ERROR: DROP INDEX CONCURRENTLY is not supported without pgactive.skip_ddl_replication set +DROP INDEX CONCURRENTLY test2_idx; +ERROR: DROP INDEX CONCURRENTLY is not supported without pgactive.skip_ddl_replication set +DROP TABLE test_tbl_create_index; +CREATE TABLE test_simple_create_with_arrays_tbl(val int[], val1 text[]); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_arrays_tbl + Table "public.test_simple_create_with_arrays_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-----------+-----------+----------+---------+----------+--------------+------------- + val | integer[] | | | | extended | | + val1 | text[] | | | | extended | | + +\c postgres +\d+ test_simple_create_with_arrays_tbl + Table "public.test_simple_create_with_arrays_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-----------+-----------+----------+---------+----------+--------------+------------- + val | integer[] | | | | extended | | + val1 | text[] | | | | extended | | + +DROP TABLE test_simple_create_with_arrays_tbl; +CREATE TYPE test_t AS ENUM('a','b','c'); +CREATE TABLE test_simple_create_with_enums_tbl(val test_t, val1 test_t); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_enums_tbl + Table "public.test_simple_create_with_enums_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+--------+-----------+----------+---------+---------+--------------+------------- + val | test_t | | | | plain | | + val1 | test_t | | | | plain | | + +\c regression +\d+ test_simple_create_with_enums_tbl + Table "public.test_simple_create_with_enums_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+--------+-----------+----------+---------+---------+--------------+------------- + val | test_t | | | | plain | | + val1 | test_t | | | | plain | | + +DROP TABLE test_simple_create_with_enums_tbl; +DROP TYPE test_t; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_enums_tbl +\dT+ test_t + List of data types + Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description +--------+------+---------------+------+----------+-------+-------------------+------------- +(0 rows) + +\c postgres +\d+ test_simple_create_with_enums_tbl +\dT+ test_t + List of data types + Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description +--------+------+---------------+------+----------+-------+-------------------+------------- +(0 rows) + +CREATE TYPE test_t AS (f1 text, f2 float, f3 integer); +CREATE TABLE test_simple_create_with_composites_tbl(val test_t, val1 test_t); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_composites_tbl + Table "public.test_simple_create_with_composites_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+--------+-----------+----------+---------+----------+--------------+------------- + val | test_t | | | | extended | | + val1 | test_t | | | | extended | | + +\c regression +\d+ test_simple_create_with_composites_tbl + Table "public.test_simple_create_with_composites_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+--------+-----------+----------+---------+----------+--------------+------------- + val | test_t | | | | extended | | + val1 | test_t | | | | extended | | + +DROP TABLE test_simple_create_with_composites_tbl; +DROP TYPE test_t; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_composites_tbl +\dT+ test_t + List of data types + Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description +--------+------+---------------+------+----------+-------+-------------------+------------- +(0 rows) + +\c postgres +\d+ test_simple_create_with_composites_tbl +\dT+ test_t + List of data types + Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description +--------+------+---------------+------+----------+-------+-------------------+------------- +(0 rows) + +DROP TABLE test_tbl_serial; +DROP TABLE test_tbl_serial_combined_pk; +CREATE TABLE test_tbl_inh_parent(f1 text, f2 date DEFAULT '2014-01-02'); +CREATE TABLE test_tbl_inh_chld1(f1 text, f2 date DEFAULT '2014-01-02') INHERITS (test_tbl_inh_parent); +NOTICE: merging column "f1" with inherited definition +NOTICE: merging column "f2" with inherited definition +CREATE TABLE test_tbl_inh_chld2(f1 text, f2 date) INHERITS (test_tbl_inh_parent); +NOTICE: merging column "f1" with inherited definition +NOTICE: merging column "f2" with inherited definition +CREATE TABLE test_tbl_inh_chld3(f1 text) INHERITS (test_tbl_inh_parent, test_tbl_inh_chld1); +NOTICE: merging multiple inherited definitions of column "f1" +NOTICE: merging multiple inherited definitions of column "f2" +NOTICE: merging column "f1" with inherited definition +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_inh_* + Table "public.test_tbl_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent +Child tables: + test_tbl_inh_chld3 + + Table "public.test_tbl_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent + + Table "public.test_tbl_inh_chld3" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent + test_tbl_inh_chld1 + + Table "public.test_tbl_inh_parent" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Child tables: + test_tbl_inh_chld1 + test_tbl_inh_chld2 + test_tbl_inh_chld3 + +\c regression +\d+ test_tbl_inh_* + Table "public.test_tbl_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent +Child tables: + test_tbl_inh_chld3 + + Table "public.test_tbl_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent + + Table "public.test_tbl_inh_chld3" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent + test_tbl_inh_chld1 + + Table "public.test_tbl_inh_parent" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Child tables: + test_tbl_inh_chld1 + test_tbl_inh_chld2 + test_tbl_inh_chld3 + +CREATE RULE test_tbl_inh_parent_rule_ins_1 AS ON INSERT TO test_tbl_inh_parent + WHERE (f1 LIKE '%1%') DO INSTEAD + INSERT INTO test_tbl_inh_chld1 VALUES (NEW.*); +CREATE RULE test_tbl_inh_parent_rule_ins_2 AS ON INSERT TO test_tbl_inh_parent + WHERE (f1 LIKE '%2%') DO INSTEAD + INSERT INTO test_tbl_inh_chld2 VALUES (NEW.*); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_inh_parent + Table "public.test_tbl_inh_parent" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Rules: + test_tbl_inh_parent_rule_ins_1 AS + ON INSERT TO test_tbl_inh_parent + WHERE new.f1 ~~ '%1%'::text DO INSTEAD INSERT INTO test_tbl_inh_chld1 (f1, f2) + VALUES (new.f1, new.f2) + test_tbl_inh_parent_rule_ins_2 AS + ON INSERT TO test_tbl_inh_parent + WHERE new.f1 ~~ '%2%'::text DO INSTEAD INSERT INTO test_tbl_inh_chld2 (f1, f2) + VALUES (new.f1, new.f2) +Child tables: + test_tbl_inh_chld1 + test_tbl_inh_chld2 + test_tbl_inh_chld3 + +\c postgres +\d+ test_tbl_inh_parent + Table "public.test_tbl_inh_parent" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Rules: + test_tbl_inh_parent_rule_ins_1 AS + ON INSERT TO test_tbl_inh_parent + WHERE new.f1 ~~ '%1%'::text DO INSTEAD INSERT INTO test_tbl_inh_chld1 (f1, f2) + VALUES (new.f1, new.f2) + test_tbl_inh_parent_rule_ins_2 AS + ON INSERT TO test_tbl_inh_parent + WHERE new.f1 ~~ '%2%'::text DO INSTEAD INSERT INTO test_tbl_inh_chld2 (f1, f2) + VALUES (new.f1, new.f2) +Child tables: + test_tbl_inh_chld1 + test_tbl_inh_chld2 + test_tbl_inh_chld3 + +DROP TABLE test_tbl_inh_chld1; +ERROR: cannot drop table test_tbl_inh_chld1 because other objects depend on it +DETAIL: table test_tbl_inh_chld3 depends on table test_tbl_inh_chld1 +rule test_tbl_inh_parent_rule_ins_1 on table test_tbl_inh_parent depends on table test_tbl_inh_chld1 +HINT: Use DROP ... CASCADE to drop the dependent objects too. +DROP TABLE test_tbl_inh_parent CASCADE; +NOTICE: drop cascades to 3 other objects +DETAIL: drop cascades to table test_tbl_inh_chld1 +drop cascades to table test_tbl_inh_chld2 +drop cascades to table test_tbl_inh_chld3 +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_inh_* +\c regression +\d+ test_tbl_inh_* +-- ensure tables WITH OIDs can't be created +SHOW default_with_oids; + default_with_oids +------------------- + off +(1 row) + +CREATE TABLE tbl_with_oids() WITH oids; +ERROR: syntax error at or near "oids" +LINE 1: CREATE TABLE tbl_with_oids() WITH oids; + ^ +CREATE TABLE tbl_without_oids() WITHOUT oids; +DROP TABLE tbl_without_oids; +CREATE TABLE tbl_without_oids(); +DROP TABLE tbl_without_oids; +SET default_with_oids = true; +ERROR: tables declared WITH OIDS are not supported +CREATE TABLE tbl_with_oids() WITH OIDS; +ERROR: syntax error at or near "OIDS" +LINE 1: CREATE TABLE tbl_with_oids() WITH OIDS; + ^ +CREATE TABLE tbl_without_oids() WITHOUT oids; +DROP TABLE tbl_without_oids; +SET default_with_oids = false; +-- ensure storage attributes in CREATE TABLE are replicated properly +\c postgres +CREATE TABLE tbl_showfillfactor (name char(500), unique (name) with (fillfactor=65)) with (fillfactor=75); +\d+ tbl_showfillfactor + Table "public.tbl_showfillfactor" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+----------------+-----------+----------+---------+----------+--------------+------------- + name | character(500) | | | | extended | | +Indexes: + "tbl_showfillfactor_name_key" UNIQUE CONSTRAINT, btree (name) WITH (fillfactor='65') +Options: fillfactor=75 + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ tbl_showfillfactor + Table "public.tbl_showfillfactor" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+----------------+-----------+----------+---------+----------+--------------+------------- + name | character(500) | | | | extended | | +Indexes: + "tbl_showfillfactor_name_key" UNIQUE CONSTRAINT, btree (name) WITH (fillfactor='65') +Options: fillfactor=75 + +DROP TABLE tbl_showfillfactor; +--- AGGREGATE --- +\c postgres +CREATE AGGREGATE test_avg ( + sfunc = int4_avg_accum, basetype = int4, stype = _int8, + finalfunc = int8_avg, + initcond1 = '{0,0}', + sortop = = +); +-- without finalfunc; test obsolete spellings 'sfunc1' etc +CREATE AGGREGATE test_sum ( + sfunc1 = int4pl, basetype = int4, stype1 = int4, + initcond1 = '0' +); +-- zero-argument aggregate +CREATE AGGREGATE test_cnt (*) ( + sfunc = int8inc, stype = int8, + initcond = '0' +); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+----------+------------------+---------------------+------ + public | test_avg | numeric | integer | agg + public | test_cnt | bigint | | agg + public | test_sum | integer | integer | agg +(3 rows) + +\c regression +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+----------+------------------+---------------------+------ + public | test_avg | numeric | integer | agg + public | test_cnt | bigint | | agg + public | test_sum | integer | integer | agg +(3 rows) + +DROP AGGREGATE test_avg(int4); +DROP AGGREGATE test_sum(int4); +DROP AGGREGATE test_cnt(*); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+------+------------------+---------------------+------ +(0 rows) + +\c postgres +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+------+------------------+---------------------+------ +(0 rows) + +create type aggtype as (a integer, b integer, c text); +create function aggf_trans(aggtype[],integer,integer,text) returns aggtype[] +as 'select array_append($1,ROW($2,$3,$4)::aggtype)' +language sql strict immutable; +create function aggfns_trans(aggtype[],integer,integer,text) returns aggtype[] +as 'select array_append($1,ROW($2,$3,$4)::aggtype)' +language sql immutable; +create aggregate test_aggfstr(integer,integer,text) ( + sfunc = aggf_trans, stype = aggtype[], + initcond = '{}' +); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+--------------+------------------+------------------------+------ + public | test_aggfstr | aggtype[] | integer, integer, text | agg +(1 row) + +\c regression +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+--------------+------------------+------------------------+------ + public | test_aggfstr | aggtype[] | integer, integer, text | agg +(1 row) + +DROP AGGREGATE test_aggfstr(integer,integer,text); +DROP FUNCTION aggf_trans(aggtype[],integer,integer,text); +DROP FUNCTION aggfns_trans(aggtype[],integer,integer,text); +DROP TYPE aggtype; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+------+------------------+---------------------+------ +(0 rows) + +\c postgres +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+------+------------------+---------------------+------ +(0 rows) + +--- OPERATOR --- +\c postgres +CREATE OPERATOR ## ( + leftarg = path, + rightarg = path, + procedure = path_inter, + commutator = ## +); +CREATE OPERATOR @#@ ( + rightarg = int8, -- left unary + procedure = factorial +); +CREATE OPERATOR #@# ( + leftarg = int8, -- right unary + procedure = factorial +); +ERROR: operator right argument type must be specified +DETAIL: Postfix operators are not supported. +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\do public.## + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------------------------- + public | ## | path | path | boolean | implementation of ?# operator +(1 row) + +\do public.@#@ + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- + public | @#@ | | bigint | numeric | factorial +(1 row) + +\do public.#@# + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\c regression +\do public.## + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------------------------- + public | ## | path | path | boolean | implementation of ?# operator +(1 row) + +\do public.@#@ + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- + public | @#@ | | bigint | numeric | factorial +(1 row) + +\do public.#@# + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +DROP OPERATOR ##(path, path); +DROP OPERATOR @#@(none,int8); +DROP OPERATOR #@#(int8,none); +ERROR: postfix operators are not supported +\do public.## + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\do public.@#@ + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\do public.#@# + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\do public.## + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\do public.@#@ + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\do public.#@# + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + diff --git a/test/expected/ddl_fn/ddl_alter_table_5.out b/test/expected/ddl_fn/ddl_alter_table_5.out new file mode 100644 index 00000000..39d00e31 --- /dev/null +++ b/test/expected/ddl_fn/ddl_alter_table_5.out @@ -0,0 +1,1407 @@ +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE IF EXISTS public.test_tbl; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE TABLE public.test_tbl(pk int primary key, dropping_col1 text, dropping_col2 text); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ADD COLUMN col1 text; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +---------------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + dropping_col1 | text | | | | extended | | + dropping_col2 | text | | | | extended | | + col1 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ADD COLUMN col2 text; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +---------------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + dropping_col1 | text | | | | extended | | + dropping_col2 | text | | | | extended | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ADD COLUMN col3_fail timestamptz NOT NULL DEFAULT now(); +$DDL$); +ERROR: ALTER TABLE ... ADD COLUMN ... DEFAULT may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_tbl is a regular table +CONTEXT: during replay of DDL statement: +ALTER TABLE public.test_tbl ADD COLUMN col3_fail timestamptz NOT NULL DEFAULT now(); + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ADD COLUMN serial_col_node1 SERIAL; +$DDL$); +ERROR: ALTER TABLE ... ADD COLUMN ... DEFAULT may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_tbl is a regular table +CONTEXT: during replay of DDL statement: +ALTER TABLE public.test_tbl ADD COLUMN serial_col_node1 SERIAL; + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl DROP COLUMN dropping_col1; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +---------------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + dropping_col2 | text | | | | extended | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl DROP COLUMN dropping_col2; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col1 SET NOT NULL; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col2 SET NOT NULL; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col1 DROP NOT NULL; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col2_not_null" NOT NULL "col2" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col2 DROP NOT NULL; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col1 SET DEFAULT 'abc'; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | 'abc'::text | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col2 SET DEFAULT 'abc'; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | 'abc'::text | extended | | + col2 | text | | | 'abc'::text | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col1 DROP DEFAULT; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | 'abc'::text | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col2 DROP DEFAULT; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ADD CONSTRAINT test_const CHECK (true); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Check constraints: + "test_const" CHECK (true) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ADD CONSTRAINT test_const1 CHECK (true); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Check constraints: + "test_const" CHECK (true) + "test_const1" CHECK (true) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl DROP CONSTRAINT test_const; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Check constraints: + "test_const1" CHECK (true) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl DROP CONSTRAINT test_const1; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col1 SET NOT NULL; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE UNIQUE INDEX test_idx ON public.test_tbl(col1); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl REPLICA IDENTITY USING INDEX test_idx; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) + "test_idx" UNIQUE, btree (col1) REPLICA IDENTITY +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN col2 SET NOT NULL; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE UNIQUE INDEX test_idx1 ON public.test_tbl(col2); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl REPLICA IDENTITY USING INDEX test_idx1; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) + "test_idx" UNIQUE, btree (col1) + "test_idx1" UNIQUE, btree (col2) REPLICA IDENTITY +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl REPLICA IDENTITY DEFAULT; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP INDEX public.test_idx; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP INDEX public. test_idx1; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE UNIQUE INDEX test_idx ON public.test_tbl(col1); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl REPLICA IDENTITY USING INDEX test_idx; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP INDEX public.test_idx; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +CREATE USER test_user; +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl OWNER TO test_user; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + col1 | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "col1" + "test_tbl_col2_not_null" NOT NULL "col2" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl RENAME COLUMN col1 TO foobar; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + foobar | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "foobar" + "test_tbl_col2_not_null" NOT NULL "col2" + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + foobar | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "foobar" + "test_tbl_col2_not_null" NOT NULL "col2" + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + foobar | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_tbl_pkey" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "foobar" + "test_tbl_col2_not_null" NOT NULL "col2" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl RENAME CONSTRAINT test_tbl_pkey TO test_ddl_pk; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+----------+--------------+------------- + pk | integer | | not null | | plain | | + foobar | text | | not null | | extended | | + col2 | text | | not null | | extended | | +Indexes: + "test_ddl_pk" PRIMARY KEY, btree (pk) +Not-null constraints: + "test_tbl_pk_not_null" NOT NULL "pk" + "test_tbl_col1_not_null" NOT NULL "foobar" + "test_tbl_col2_not_null" NOT NULL "col2" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP TABLE public.test_tbl; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +-- ALTER COLUMN ... SET STATISTICS +\c postgres +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE TABLE public.test_tbl(id int); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN id SET STATISTICS 10; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | 10 | + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN id SET STATISTICS 0; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | 0 | + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | 0 | + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_tbl ALTER COLUMN id SET STATISTICS -1; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | | + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\d+ test_tbl + Table "public.test_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + id | integer | | | | plain | | + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP TABLE public.test_tbl; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +--- INHERITANCE --- +\c postgres +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE TABLE public.test_inh_root (id int primary key, val1 varchar, val2 int); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE TABLE public.test_inh_chld1 (child1col int) INHERITS (public.test_inh_root); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE TABLE public.test_inh_chld2 () INHERITS (public.test_inh_chld1); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +INSERT INTO public.test_inh_root(id, val1, val2) +SELECT x, x::text, x%4 FROM generate_series(1,10) x; +INSERT INTO public.test_inh_chld1(id, val1, val2, child1col) +SELECT x, x::text, x%4+1, x*2 FROM generate_series(11,20) x; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_inh_root + Table "public.test_inh_root" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | +Indexes: + "test_inh_root_pkey" PRIMARY KEY, btree (id) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" +Child tables: + test_inh_chld1 + +\d+ test_inh_chld1 + Table "public.test_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_root +Child tables: + test_inh_chld2 + +\d+ test_inh_chld2 + Table "public.test_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_chld1 + +\c regression +\d+ test_inh_root + Table "public.test_inh_root" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | +Indexes: + "test_inh_root_pkey" PRIMARY KEY, btree (id) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" +Child tables: + test_inh_chld1 + +\d+ test_inh_chld1 + Table "public.test_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_root +Child tables: + test_inh_chld2 + +\d+ test_inh_chld2 + Table "public.test_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_chld1 + +SELECT * FROM public.test_inh_root; + id | val1 | val2 +----+------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 +(20 rows) + +SELECT * FROM public.test_inh_chld1; + id | val1 | val2 | child1col +----+------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 +(10 rows) + +SELECT * FROM public.test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +SET pgactive.skip_ddl_replication = true; +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE public.test_inh_root ADD CONSTRAINT idchk CHECK (id > 0); +$DDL$); +ERROR: pgactive_replicate_ddl_command execution attempt rejected by configuration +DETAIL: pgactive.skip_ddl_replication is true. +HINT: See the 'DDL replication' chapter of the documentation. +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE ONLY public.test_inh_chld1 ALTER COLUMN id SET DEFAULT 1; +$DDL$); +ERROR: pgactive_replicate_ddl_command execution attempt rejected by configuration +DETAIL: pgactive.skip_ddl_replication is true. +HINT: See the 'DDL replication' chapter of the documentation. +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +ALTER TABLE ONLY public.test_inh_root DROP CONSTRAINT idchk; +$DDL$); +ERROR: pgactive_replicate_ddl_command execution attempt rejected by configuration +DETAIL: pgactive.skip_ddl_replication is true. +HINT: See the 'DDL replication' chapter of the documentation. +RESET pgactive.skip_ddl_replication; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_inh_root + Table "public.test_inh_root" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | +Indexes: + "test_inh_root_pkey" PRIMARY KEY, btree (id) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" +Child tables: + test_inh_chld1 + +\d+ test_inh_chld1 + Table "public.test_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_root +Child tables: + test_inh_chld2 + +\d+ test_inh_chld2 + Table "public.test_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_chld1 + +\c postgres +\d+ test_inh_root + Table "public.test_inh_root" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | +Indexes: + "test_inh_root_pkey" PRIMARY KEY, btree (id) +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" +Child tables: + test_inh_chld1 + +\d+ test_inh_chld1 + Table "public.test_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_root +Child tables: + test_inh_chld2 + +\d+ test_inh_chld2 + Table "public.test_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +-----------+-------------------+-----------+----------+---------+----------+--------------+------------- + id | integer | | not null | | plain | | + val1 | character varying | | | | extended | | + val2 | integer | | | | plain | | + child1col | integer | | | | plain | | +Not-null constraints: + "test_inh_root_id_not_null" NOT NULL "id" (inherited) +Inherits: + test_inh_chld1 + +\c regression +SELECT * FROM public.test_inh_root; + id | val1 | val2 +----+------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 +(20 rows) + +SELECT * FROM public.test_inh_chld1; + id | val1 | val2 | child1col +----+------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 +(10 rows) + +SELECT * FROM public.test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +-- Should fail with an ERROR +ALTER TABLE public.test_inh_chld1 NO INHERIT public.test_inh_root; +ERROR: ALTER TABLE ... [NO] INHERIT may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_inh_chld1 is a regular table +-- Will also fail with an ERROR +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ ALTER TABLE public.test_inh_chld1 NO INHERIT public.test_inh_root; $DDL$); +ERROR: ALTER TABLE ... [NO] INHERIT may only affect UNLOGGED or TEMPORARY tables when pgactive is active; test_inh_chld1 is a regular table +CONTEXT: during replay of DDL statement: ALTER TABLE public.test_inh_chld1 NO INHERIT public.test_inh_root; +-- Will be permitted +BEGIN; +SET LOCAL pgactive.skip_ddl_replication = true; +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ ALTER TABLE public.test_inh_chld1 NO INHERIT public.test_inh_root;$DDL$); +ERROR: pgactive_replicate_ddl_command execution attempt rejected by configuration +DETAIL: pgactive.skip_ddl_replication is true. +HINT: See the 'DDL replication' chapter of the documentation. +COMMIT; +SELECT * FROM public.test_inh_root; + id | val1 | val2 +----+------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 +(20 rows) + +SELECT * FROM public.test_inh_chld1; + id | val1 | val2 | child1col +----+------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 +(10 rows) + +SELECT * FROM public.test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +SELECT * FROM public.test_inh_root; + id | val1 | val2 +----+------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 +(20 rows) + +SELECT * FROM public.test_inh_chld1; + id | val1 | val2 | child1col +----+------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 +(10 rows) + +SELECT * FROM public.test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +DELETE FROM public.test_inh_root WHERE val2 = 0; +ERROR: cannot run UPDATE or DELETE on table test_inh_chld1 because it does not have a PRIMARY KEY +HINT: Add a PRIMARY KEY to the table. +INSERT INTO public.test_inh_root(id, val1, val2) VALUES (200, 'root', 1); +INSERT INTO public.test_inh_chld1(id, val1, val2, child1col) VALUES (200, 'child', 0, 0); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +SELECT * FROM public.test_inh_root; + id | val1 | val2 +-----+-------+------ + 1 | 1 | 1 + 2 | 2 | 2 + 3 | 3 | 3 + 4 | 4 | 0 + 5 | 5 | 1 + 6 | 6 | 2 + 7 | 7 | 3 + 8 | 8 | 0 + 9 | 9 | 1 + 10 | 10 | 2 + 200 | root | 1 + 11 | 11 | 4 + 12 | 12 | 1 + 13 | 13 | 2 + 14 | 14 | 3 + 15 | 15 | 4 + 16 | 16 | 1 + 17 | 17 | 2 + 18 | 18 | 3 + 19 | 19 | 4 + 20 | 20 | 1 + 200 | child | 0 +(22 rows) + +SELECT * FROM public.test_inh_chld1; + id | val1 | val2 | child1col +-----+-------+------+----------- + 11 | 11 | 4 | 22 + 12 | 12 | 1 | 24 + 13 | 13 | 2 | 26 + 14 | 14 | 3 | 28 + 15 | 15 | 4 | 30 + 16 | 16 | 1 | 32 + 17 | 17 | 2 | 34 + 18 | 18 | 3 | 36 + 19 | 19 | 4 | 38 + 20 | 20 | 1 | 40 + 200 | child | 0 | 0 +(11 rows) + +SELECT * FROM public.test_inh_chld2; + id | val1 | val2 | child1col +----+------+------+----------- +(0 rows) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP TABLE public.test_inh_chld2; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP TABLE public.test_inh_chld1; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP TABLE public.test_inh_root; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + diff --git a/test/expected/ddl_fn/ddl_create_5.out b/test/expected/ddl_fn/ddl_create_5.out new file mode 100644 index 00000000..02128c48 --- /dev/null +++ b/test/expected/ddl_fn/ddl_create_5.out @@ -0,0 +1,1400 @@ +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_simple_create(val int); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_simple_create + Table "public.test_tbl_simple_create" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + +\c postgres +\d+ test_tbl_simple_create + Table "public.test_tbl_simple_create" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_simple_create; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_simple_create +\c regression +\d+ test_tbl_simple_create +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE UNLOGGED TABLE public.test_tbl_unlogged_create(val int); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_unlogged_create + Unlogged table "public.test_tbl_unlogged_create" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + +\c postgres +\d+ test_tbl_unlogged_create + Unlogged table "public.test_tbl_unlogged_create" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_unlogged_create; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_unlogged_create +\c regression +\d+ test_tbl_unlogged_create +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_simple_pk(val int PRIMARY KEY); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_simple_pk + Table "public.test_tbl_simple_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | not null | | plain | | +Indexes: + "test_tbl_simple_pk_pkey" PRIMARY KEY, btree (val) +Not-null constraints: + "test_tbl_simple_pk_val_not_null" NOT NULL "val" + +\c postgres +\d+ test_tbl_simple_pk + Table "public.test_tbl_simple_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | not null | | plain | | +Indexes: + "test_tbl_simple_pk_pkey" PRIMARY KEY, btree (val) +Not-null constraints: + "test_tbl_simple_pk_val_not_null" NOT NULL "val" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_simple_pk; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_simple_pk +\c regression +\d+ test_tbl_simple_pk +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_combined_pk(val int, val1 int, PRIMARY KEY (val, val1)); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_combined_pk + Table "public.test_tbl_combined_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | not null | | plain | | + val1 | integer | | not null | | plain | | +Indexes: + "test_tbl_combined_pk_pkey" PRIMARY KEY, btree (val, val1) +Not-null constraints: + "test_tbl_combined_pk_val_not_null" NOT NULL "val" + "test_tbl_combined_pk_val1_not_null" NOT NULL "val1" + +\c postgres +\d+ test_tbl_combined_pk + Table "public.test_tbl_combined_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | not null | | plain | | + val1 | integer | | not null | | plain | | +Indexes: + "test_tbl_combined_pk_pkey" PRIMARY KEY, btree (val, val1) +Not-null constraints: + "test_tbl_combined_pk_val_not_null" NOT NULL "val" + "test_tbl_combined_pk_val1_not_null" NOT NULL "val1" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_combined_pk; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_combined_pk +\c regression +\d+ test_tbl_combined_pk +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_serial(val SERIAL); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial + Table "public.test_tbl_serial" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_val_seq'::regclass) | plain | | +Not-null constraints: + "test_tbl_serial_val_not_null" NOT NULL "val" + +\c postgres +\d+ test_tbl_serial + Table "public.test_tbl_serial" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_val_seq'::regclass) | plain | | +Not-null constraints: + "test_tbl_serial_val_not_null" NOT NULL "val" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_serial; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial +\c regression +\d+ test_tbl_serial +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_serial(val SERIAL); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial + Table "public.test_tbl_serial" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_val_seq'::regclass) | plain | | +Not-null constraints: + "test_tbl_serial_val_not_null" NOT NULL "val" + +\c postgres +\d+ test_tbl_serial + Table "public.test_tbl_serial" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_val_seq'::regclass) | plain | | +Not-null constraints: + "test_tbl_serial_val_not_null" NOT NULL "val" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_serial_pk(val SERIAL PRIMARY KEY); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial_pk + Table "public.test_tbl_serial_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_pk_val_seq'::regclass) | plain | | +Indexes: + "test_tbl_serial_pk_pkey" PRIMARY KEY, btree (val) +Not-null constraints: + "test_tbl_serial_pk_val_not_null" NOT NULL "val" + +\c postgres +\d+ test_tbl_serial_pk + Table "public.test_tbl_serial_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+-------------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_pk_val_seq'::regclass) | plain | | +Indexes: + "test_tbl_serial_pk_pkey" PRIMARY KEY, btree (val) +Not-null constraints: + "test_tbl_serial_pk_val_not_null" NOT NULL "val" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_serial_pk; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial_pk +\c regression +\d+ test_tbl_serial_pk +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_serial_combined_pk(val SERIAL, val1 INTEGER, PRIMARY KEY (val, val1)); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_serial_combined_pk + Table "public.test_tbl_serial_combined_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_combined_pk_val_seq'::regclass) | plain | | + val1 | integer | | not null | | plain | | +Indexes: + "test_tbl_serial_combined_pk_pkey" PRIMARY KEY, btree (val, val1) +Not-null constraints: + "test_tbl_serial_combined_pk_val_not_null" NOT NULL "val" + "test_tbl_serial_combined_pk_val1_not_null" NOT NULL "val1" + +\c postgres +\d+ test_tbl_serial_combined_pk + Table "public.test_tbl_serial_combined_pk" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+----------------------------------------------------------+---------+--------------+------------- + val | integer | | not null | nextval('test_tbl_serial_combined_pk_val_seq'::regclass) | plain | | + val1 | integer | | not null | | plain | | +Indexes: + "test_tbl_serial_combined_pk_pkey" PRIMARY KEY, btree (val, val1) +Not-null constraints: + "test_tbl_serial_combined_pk_val_not_null" NOT NULL "val" + "test_tbl_serial_combined_pk_val1_not_null" NOT NULL "val1" + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_create_index (val int, val2 int); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE UNIQUE INDEX test1_idx ON public.test_tbl_create_index(val); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE INDEX test2_idx ON public.test_tbl_create_index (lower(val2::text)); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test1_idx" UNIQUE, btree (val) + "test2_idx" btree (lower(val2::text)) + +\c regression +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test1_idx" UNIQUE, btree (val) + "test2_idx" btree (lower(val2::text)) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP INDEX public.test1_idx; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP INDEX public.test2_idx; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | + +\c postgres +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE INDEX test1_idx ON public.test_tbl_create_index(val, val2); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE INDEX test2_idx ON public.test_tbl_create_index USING gist (val, UPPER(val2::text)); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test1_idx" btree (val, val2) + "test2_idx" gist (val, upper(val2::text)) + +\c regression +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test1_idx" btree (val, val2) + "test2_idx" gist (val, upper(val2::text)) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP INDEX public.test1_idx; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +-- Not supported via pgactive.pgactive_replicate_ddl_command, see //github.com/2ndQuadrant/pgactive-private/issues/124 +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP INDEX CONCURRENTLY public.test2_idx; $DDL$); +ERROR: DROP INDEX CONCURRENTLY is not supported in pgactive.replicate_ddl_command +HINT: Run DROP INDEX CONCURRENTLY on each node individually with pgactive.skip_ddl_replication set. +CONTEXT: during replay of DDL statement: DROP INDEX CONCURRENTLY public.test2_idx; +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test2_idx" gist (val, upper(val2::text)) + +\c postgres +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test2_idx" gist (val, upper(val2::text)) + +-- Not supported via pgactive.pgactive_replicate_ddl_command, see //github.com/2ndQuadrant/pgactive-private/issues/124 +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE INDEX CONCURRENTLY test1_idx ON public.test_tbl_create_index(val, val2); $DDL$); +ERROR: CREATE INDEX CONCURRENTLY is not supported in pgactive.replicate_ddl_command +HINT: Run CREATE INDEX CONCURRENTLY on each node individually with pgactive.skip_ddl_replication set. +CONTEXT: during replay of DDL statement: CREATE INDEX CONCURRENTLY test1_idx ON public.test_tbl_create_index(val, val2); +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE UNIQUE INDEX CONCURRENTLY test2_idx ON public.test_tbl_create_index (lower(val2::text)); $DDL$); +ERROR: CREATE INDEX CONCURRENTLY is not supported in pgactive.replicate_ddl_command +HINT: Run CREATE INDEX CONCURRENTLY on each node individually with pgactive.skip_ddl_replication set. +CONTEXT: during replay of DDL statement: CREATE UNIQUE INDEX CONCURRENTLY test2_idx ON public.test_tbl_create_index (lower(val2::text)); +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test2_idx" gist (val, upper(val2::text)) + +\c regression +\d+ test_tbl_create_index + Table "public.test_tbl_create_index" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + val | integer | | | | plain | | + val2 | integer | | | | plain | | +Indexes: + "test2_idx" gist (val, upper(val2::text)) + +-- Not supported via pgactive.pgactive_replicate_ddl_command, see //github.com/2ndQuadrant/pgactive-private/issues/124 +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP INDEX CONCURRENTLY public.test1_idx; $DDL$); +ERROR: DROP INDEX CONCURRENTLY is not supported in pgactive.replicate_ddl_command +HINT: Run DROP INDEX CONCURRENTLY on each node individually with pgactive.skip_ddl_replication set. +CONTEXT: during replay of DDL statement: DROP INDEX CONCURRENTLY public.test1_idx; +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP INDEX CONCURRENTLY public.test2_idx; $DDL$); +ERROR: DROP INDEX CONCURRENTLY is not supported in pgactive.replicate_ddl_command +HINT: Run DROP INDEX CONCURRENTLY on each node individually with pgactive.skip_ddl_replication set. +CONTEXT: during replay of DDL statement: DROP INDEX CONCURRENTLY public.test2_idx; +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_create_index; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_simple_create_with_arrays_tbl(val int[], val1 text[]); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_arrays_tbl + Table "public.test_simple_create_with_arrays_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-----------+-----------+----------+---------+----------+--------------+------------- + val | integer[] | | | | extended | | + val1 | text[] | | | | extended | | + +\c postgres +\d+ test_simple_create_with_arrays_tbl + Table "public.test_simple_create_with_arrays_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+-----------+-----------+----------+---------+----------+--------------+------------- + val | integer[] | | | | extended | | + val1 | text[] | | | | extended | | + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_simple_create_with_arrays_tbl; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TYPE public.test_t AS ENUM('a','b','c'); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_simple_create_with_enums_tbl(val public.test_t, val1 public.test_t); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_enums_tbl + Table "public.test_simple_create_with_enums_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+--------+-----------+----------+---------+---------+--------------+------------- + val | test_t | | | | plain | | + val1 | test_t | | | | plain | | + +\c regression +\d+ test_simple_create_with_enums_tbl + Table "public.test_simple_create_with_enums_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+--------+-----------+----------+---------+---------+--------------+------------- + val | test_t | | | | plain | | + val1 | test_t | | | | plain | | + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_simple_create_with_enums_tbl; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TYPE public.test_t; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_enums_tbl +\dT test_t + List of data types + Schema | Name | Description +--------+------+------------- +(0 rows) + +\c postgres +\d+ test_simple_create_with_enums_tbl +\dT test_t + List of data types + Schema | Name | Description +--------+------+------------- +(0 rows) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TYPE public.test_t AS (f1 text, f2 float, f3 integer); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_simple_create_with_composites_tbl(val public.test_t, val1 public.test_t); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_composites_tbl + Table "public.test_simple_create_with_composites_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+--------+-----------+----------+---------+----------+--------------+------------- + val | test_t | | | | extended | | + val1 | test_t | | | | extended | | + +\c regression +\d+ test_simple_create_with_composites_tbl + Table "public.test_simple_create_with_composites_tbl" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+--------+-----------+----------+---------+----------+--------------+------------- + val | test_t | | | | extended | | + val1 | test_t | | | | extended | | + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_simple_create_with_composites_tbl; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TYPE public.test_t; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_simple_create_with_composites_tbl +\dT test_t + List of data types + Schema | Name | Description +--------+------+------------- +(0 rows) + +\c postgres +\d+ test_simple_create_with_composites_tbl +\dT test_t + List of data types + Schema | Name | Description +--------+------+------------- +(0 rows) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_serial; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_serial_combined_pk; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_inh_parent(f1 text, f2 date DEFAULT '2014-01-02'); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +\set VERBOSITY terse +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_inh_chld1(f1 text, f2 date DEFAULT '2014-01-02') INHERITS (public.test_tbl_inh_parent); $DDL$); +NOTICE: merging column "f1" with inherited definition +NOTICE: merging column "f2" with inherited definition + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_inh_chld2(f1 text, f2 date) INHERITS (public.test_tbl_inh_parent); $DDL$); +NOTICE: merging column "f1" with inherited definition +NOTICE: merging column "f2" with inherited definition + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.test_tbl_inh_chld3(f1 text) INHERITS (public.test_tbl_inh_parent, public.test_tbl_inh_chld1); $DDL$); +NOTICE: merging multiple inherited definitions of column "f1" +NOTICE: merging multiple inherited definitions of column "f2" +NOTICE: merging column "f1" with inherited definition + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +\set VERBOSITY default +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_inh_* + Table "public.test_tbl_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent +Child tables: + test_tbl_inh_chld3 + + Table "public.test_tbl_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent + + Table "public.test_tbl_inh_chld3" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent + test_tbl_inh_chld1 + + Table "public.test_tbl_inh_parent" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Child tables: + test_tbl_inh_chld1 + test_tbl_inh_chld2 + test_tbl_inh_chld3 + +\c regression +\d+ test_tbl_inh_* + Table "public.test_tbl_inh_chld1" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent +Child tables: + test_tbl_inh_chld3 + + Table "public.test_tbl_inh_chld2" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent + + Table "public.test_tbl_inh_chld3" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Inherits: + test_tbl_inh_parent + test_tbl_inh_chld1 + + Table "public.test_tbl_inh_parent" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Child tables: + test_tbl_inh_chld1 + test_tbl_inh_chld2 + test_tbl_inh_chld3 + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE RULE test_tbl_inh_parent_rule_ins_1 AS ON INSERT TO public.test_tbl_inh_parent + WHERE (f1 LIKE '%1%') DO INSTEAD + INSERT INTO public.test_tbl_inh_chld1 VALUES (NEW.*); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE RULE test_tbl_inh_parent_rule_ins_2 AS ON INSERT TO public.test_tbl_inh_parent + WHERE (f1 LIKE '%2%') DO INSTEAD + INSERT INTO public.test_tbl_inh_chld2 VALUES (NEW.*); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_inh_parent + Table "public.test_tbl_inh_parent" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Rules: + test_tbl_inh_parent_rule_ins_1 AS + ON INSERT TO test_tbl_inh_parent + WHERE new.f1 ~~ '%1%'::text DO INSTEAD INSERT INTO test_tbl_inh_chld1 (f1, f2) + VALUES (new.f1, new.f2) + test_tbl_inh_parent_rule_ins_2 AS + ON INSERT TO test_tbl_inh_parent + WHERE new.f1 ~~ '%2%'::text DO INSTEAD INSERT INTO test_tbl_inh_chld2 (f1, f2) + VALUES (new.f1, new.f2) +Child tables: + test_tbl_inh_chld1 + test_tbl_inh_chld2 + test_tbl_inh_chld3 + +\c postgres +\d+ test_tbl_inh_parent + Table "public.test_tbl_inh_parent" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+------+-----------+----------+--------------------+----------+--------------+------------- + f1 | text | | | | extended | | + f2 | date | | | '01-02-2014'::date | plain | | +Rules: + test_tbl_inh_parent_rule_ins_1 AS + ON INSERT TO test_tbl_inh_parent + WHERE new.f1 ~~ '%1%'::text DO INSTEAD INSERT INTO test_tbl_inh_chld1 (f1, f2) + VALUES (new.f1, new.f2) + test_tbl_inh_parent_rule_ins_2 AS + ON INSERT TO test_tbl_inh_parent + WHERE new.f1 ~~ '%2%'::text DO INSTEAD INSERT INTO test_tbl_inh_chld2 (f1, f2) + VALUES (new.f1, new.f2) +Child tables: + test_tbl_inh_chld1 + test_tbl_inh_chld2 + test_tbl_inh_chld3 + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_inh_chld1; $DDL$); +ERROR: cannot drop table public.test_tbl_inh_chld1 because other objects depend on it +DETAIL: table public.test_tbl_inh_chld3 depends on table public.test_tbl_inh_chld1 +rule test_tbl_inh_parent_rule_ins_1 on table public.test_tbl_inh_parent depends on table public.test_tbl_inh_chld1 +HINT: Use DROP ... CASCADE to drop the dependent objects too. +CONTEXT: during replay of DDL statement: DROP TABLE public.test_tbl_inh_chld1; +\set VERBOSITY terse +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.test_tbl_inh_parent CASCADE; $DDL$); +NOTICE: drop cascades to 3 other objects + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +\set VERBOSITY default +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_inh_* +\c regression +\d+ test_tbl_inh_* +CREATE TABLE test_tbl_exclude(val int PRIMARY KEY,EXCLUDE USING gist(id with =)); +ERROR: EXCLUDE constraints are unsafe with pgactive active +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\d+ test_tbl_exclude +\c postgres +\d+ test_tbl_exclude +\c regression +-- ensure tables WITH OIDs can't be created +SHOW default_with_oids; + default_with_oids +------------------- + off +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.tbl_with_oids() WITH oids; $DDL$); +ERROR: syntax error at or near "oids" +LINE 1: SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE ... + ^ +CONTEXT: during replay of DDL statement: CREATE TABLE public.tbl_with_oids() WITH oids; +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.tbl_without_oids() WITHOUT oids; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.tbl_without_oids; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.tbl_without_oids(); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.tbl_without_oids; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SET default_with_oids = true; +ERROR: tables declared WITH OIDS are not supported +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.tbl_with_oids() WITH OIDS; $DDL$); +ERROR: syntax error at or near "OIDS" +LINE 1: SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE ... + ^ +CONTEXT: during replay of DDL statement: CREATE TABLE public.tbl_with_oids() WITH OIDS; +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.tbl_without_oids() WITHOUT oids; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE public.tbl_without_oids; $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SET default_with_oids = false; +-- ensure storage attributes in SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.are replicated properly $DDL$); +\c postgres +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE TABLE public.tbl_showfillfactor (name char(500), unique (name) with (fillfactor=65)) with (fillfactor=75); $DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +\d+ tbl_showfillfactor + Table "public.tbl_showfillfactor" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+----------------+-----------+----------+---------+----------+--------------+------------- + name | character(500) | | | | extended | | +Indexes: + "tbl_showfillfactor_name_key" UNIQUE CONSTRAINT, btree (name) WITH (fillfactor='65') +Options: fillfactor=75 + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c regression +\d+ tbl_showfillfactor + Table "public.tbl_showfillfactor" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+----------------+-----------+----------+---------+----------+--------------+------------- + name | character(500) | | | | extended | | +Indexes: + "tbl_showfillfactor_name_key" UNIQUE CONSTRAINT, btree (name) WITH (fillfactor='65') +Options: fillfactor=75 + +\set VERBOSITY terse +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ DROP TABLE tbl_showfillfactor;$DDL$); +ERROR: relation "tbl_showfillfactor" does not exist +\set VERBOSITY default +--- AGGREGATE --- +\c postgres +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE AGGREGATE public.test_avg ( + sfunc = int4_avg_accum, basetype = int4, stype = _int8, + finalfunc = int8_avg, + initcond1 = '{0,0}', + sortop = = +); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +-- without finalfunc; test obsolete spellings 'sfunc1' etc +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE AGGREGATE public.test_sum ( + sfunc1 = int4pl, basetype = int4, stype1 = int4, + initcond1 = '0' +); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +-- zero-argument aggregate +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE AGGREGATE public.test_cnt (*) ( + sfunc = int8inc, stype = int8, + initcond = '0' +); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+----------+------------------+---------------------+------ + public | test_avg | numeric | integer | agg + public | test_cnt | bigint | | agg + public | test_sum | integer | integer | agg +(3 rows) + +\c regression +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+----------+------------------+---------------------+------ + public | test_avg | numeric | integer | agg + public | test_cnt | bigint | | agg + public | test_sum | integer | integer | agg +(3 rows) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP AGGREGATE public.test_avg(int4); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP AGGREGATE public.test_sum(int4); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP AGGREGATE public.test_cnt(*); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+------+------------------+---------------------+------ +(0 rows) + +\c postgres +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+------+------------------+---------------------+------ +(0 rows) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +create type public.aggtype as (a integer, b integer, c text); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +create function public.aggf_trans(public.aggtype[],integer,integer,text) returns public.aggtype[] +as 'select array_append($1,ROW($2,$3,$4)::public.aggtype)' +language sql strict immutable; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +create function public.aggfns_trans(public.aggtype[],integer,integer,text) returns public.aggtype[] +as 'select array_append($1,ROW($2,$3,$4)::public.aggtype)' +language sql immutable; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +create aggregate public.test_aggfstr(integer,integer,text) ( + sfunc = public.aggf_trans, stype = public.aggtype[], + initcond = '{}' +); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+--------------+------------------+------------------------+------ + public | test_aggfstr | aggtype[] | integer, integer, text | agg +(1 row) + +\c regression +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+--------------+------------------+------------------------+------ + public | test_aggfstr | aggtype[] | integer, integer, text | agg +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP AGGREGATE public.test_aggfstr(integer,integer,text); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP FUNCTION public.aggf_trans(public.aggtype[],integer,integer,text); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP FUNCTION public.aggfns_trans(public.aggtype[],integer,integer,text); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP TYPE public.aggtype; +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+------+------------------+---------------------+------ +(0 rows) + +\c postgres +\dfa test_* + List of functions + Schema | Name | Result data type | Argument data types | Type +--------+------+------------------+---------------------+------ +(0 rows) + +--- OPERATOR --- +\c postgres +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE OPERATOR public.## ( + leftarg = path, + rightarg = path, + procedure = path_inter, + commutator = OPERATOR(public.##) +); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE OPERATOR public.@#@ ( + rightarg = int8, -- left unary + procedure = factorial +); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +CREATE OPERATOR public.#@# ( + leftarg = int8, -- right unary + procedure = factorial +); +$DDL$); +ERROR: operator right argument type must be specified +DETAIL: Postfix operators are not supported. +CONTEXT: during replay of DDL statement: +CREATE OPERATOR public.#@# ( + leftarg = int8, -- right unary + procedure = factorial +); + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\do public.## + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------------------------- + public | ## | path | path | boolean | implementation of ?# operator +(1 row) + +\do public.@#@ + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- + public | @#@ | | bigint | numeric | factorial +(1 row) + +\do public.#@# + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\c regression +\do public.## + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------------------------- + public | ## | path | path | boolean | implementation of ?# operator +(1 row) + +\do public.@#@ + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- + public | @#@ | | bigint | numeric | factorial +(1 row) + +\do public.#@# + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP OPERATOR public.##(path, path); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP OPERATOR public.@#@(none,int8); +$DDL$); + pgactive_replicate_ddl_command +-------------------------------- + +(1 row) + +SELECT pgactive.pgactive_replicate_ddl_command($DDL$ +DROP OPERATOR public.#@#(int8,none); +$DDL$); +ERROR: postfix operators are not supported +CONTEXT: during replay of DDL statement: +DROP OPERATOR public.#@#(int8,none); + +\do public.## + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\do public.@#@ + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\do public.#@# + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); + pgactive_wait_for_slots_confirmed_flush_lsn +--------------------------------------------- + +(1 row) + +\c postgres +\do public.## + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\do public.@#@ + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + +\do public.#@# + List of operators + Schema | Name | Left arg type | Right arg type | Result type | Description +--------+------+---------------+----------------+-------------+------------- +(0 rows) + diff --git a/test/expected/ddl_fn/ddl_namespace.out b/test/expected/ddl_fn/ddl_namespace.out index 7ce10817..a9e2bbbe 100644 --- a/test/expected/ddl_fn/ddl_namespace.out +++ b/test/expected/ddl_fn/ddl_namespace.out @@ -1,15 +1,15 @@ \c postgres SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE SCHEMA test_schema_1 - CREATE UNIQUE INDEX abc_a_idx ON abc (a) - - CREATE VIEW abc_view AS - SELECT a+1 AS a, b+1 AS b FROM abc - CREATE TABLE abc ( a serial, b int UNIQUE - ); + ) + + CREATE UNIQUE INDEX abc_a_idx ON abc (a) + + CREATE VIEW abc_view AS + SELECT a+1 AS a, b+1 AS b FROM abc; $DDL$); pgactive_replicate_ddl_command -------------------------------- diff --git a/test/expected/ddl_namespace.out b/test/expected/ddl_namespace.out index 44d161cd..39594608 100644 --- a/test/expected/ddl_namespace.out +++ b/test/expected/ddl_namespace.out @@ -1,12 +1,12 @@ \c postgres CREATE SCHEMA test_schema_1 - CREATE UNIQUE INDEX abc_a_idx ON abc (a) - CREATE VIEW abc_view AS - SELECT a+1 AS a, b+1 AS b FROM abc CREATE TABLE abc ( a serial, b int UNIQUE - ); + ) + CREATE UNIQUE INDEX abc_a_idx ON abc (a) + CREATE VIEW abc_view AS + SELECT a+1 AS a, b+1 AS b FROM abc; CREATE FUNCTION test_schema_1.abc_func() RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql; SELECT pgactive.pgactive_wait_for_slots_confirmed_flush_lsn(NULL,NULL); diff --git a/test/run_tests.in b/test/run_tests.in index f304837c..34dd0250 100644 --- a/test/run_tests.in +++ b/test/run_tests.in @@ -61,7 +61,7 @@ done TMPDIR=@abs_srcdir@/test/tmp_check SOCKET=$TMPDIR -PORT=5440 +PORT=54@pgactive_PG_MAJORVERSION@ OPTIONS="-c config_file=${CONFIGFILE}" OPTIONS="$OPTIONS -c unix_socket_directories=$SOCKET" OPTIONS="$OPTIONS -c listen_addresses=''" diff --git a/test/sql/ddl_fn/ddl_namespace.sql b/test/sql/ddl_fn/ddl_namespace.sql index 496348ef..667c28c3 100644 --- a/test/sql/ddl_fn/ddl_namespace.sql +++ b/test/sql/ddl_fn/ddl_namespace.sql @@ -1,15 +1,15 @@ \c postgres SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE SCHEMA test_schema_1 - CREATE UNIQUE INDEX abc_a_idx ON abc (a) - - CREATE VIEW abc_view AS - SELECT a+1 AS a, b+1 AS b FROM abc - CREATE TABLE abc ( a serial, b int UNIQUE - ); + ) + + CREATE UNIQUE INDEX abc_a_idx ON abc (a) + + CREATE VIEW abc_view AS + SELECT a+1 AS a, b+1 AS b FROM abc; $DDL$); SELECT pgactive.pgactive_replicate_ddl_command($DDL$ CREATE FUNCTION test_schema_1.abc_func() RETURNS void diff --git a/test/sql/ddl_namespace.sql b/test/sql/ddl_namespace.sql index 5618b9c7..6e69c350 100644 --- a/test/sql/ddl_namespace.sql +++ b/test/sql/ddl_namespace.sql @@ -1,14 +1,14 @@ \c postgres CREATE SCHEMA test_schema_1 - CREATE UNIQUE INDEX abc_a_idx ON abc (a) - - CREATE VIEW abc_view AS - SELECT a+1 AS a, b+1 AS b FROM abc - CREATE TABLE abc ( a serial, b int UNIQUE - ); + ) + + CREATE UNIQUE INDEX abc_a_idx ON abc (a) + + CREATE VIEW abc_view AS + SELECT a+1 AS a, b+1 AS b FROM abc; CREATE FUNCTION test_schema_1.abc_func() RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;