Commit 2f9eb313 authored by Alvaro Herrera's avatar Alvaro Herrera

pg_dump: Allow dumping data of specific foreign servers

The new command-line switch --include-foreign-data=PATTERN lets the user
specify foreign servers from which to dump foreign table data.  This can
be refined by further inclusion/exclusion switches, so that the user has
full control over which tables to dump.

A limitation is that this doesn't work in combination with parallel
dumps, for implementation reasons.  This might be lifted in the future,
but requires shuffling some code around.

Author: Luis Carril <luis.carril@swarm64.com>
Reviewed-by: default avatarDaniel Gustafsson <daniel@yesql.se>
Reviewed-by: default avatarSurafel Temesgen <surafel3000@gmail.com>
Reviewed-by: default avatarvignesh C <vignesh21@gmail.com>
Reviewed-by: default avatarÁlvaro Herrera <alvherre@2ndQuadrant.com>
Discussion: https://postgr.es/m/LEJPR01MB0185483C0079D2F651B16231E7FC0@LEJPR01MB0185.DEUPRD01.PROD.OUTLOOK.DE
parent bda6dedb
...@@ -767,6 +767,36 @@ PostgreSQL documentation ...@@ -767,6 +767,36 @@ PostgreSQL documentation
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--include-foreign-data=<replaceable class="parameter">foreignserver</replaceable></option></term>
<listitem>
<para>
Dump the data for any foreign table with a foreign server
matching <replaceable class="parameter">foreignserver</replaceable>
pattern. Multiple foreign servers can be selected by writing multiple
<option>--include-foreign-data</option> switches.
Also, the <replaceable class="parameter">foreignserver</replaceable> parameter is
interpreted as a pattern according to the same rules used by
<application>psql</application>'s <literal>\d</literal> commands (see <xref
linkend="app-psql-patterns" endterm="app-psql-patterns-title"/>),
so multiple foreign servers can also be selected by writing wildcard characters
in the pattern. When using wildcards, be careful to quote the pattern
if needed to prevent the shell from expanding the wildcards; see
<xref linkend="pg-dump-examples" endterm="pg-dump-examples-title"/>.
The only exception is that an empty pattern is disallowed.
</para>
<note>
<para>
When <option>--include-foreign-data</option> is specified,
<application>pg_dump</application> does not check that the foreign
table is writeable. Therefore, there is no guarantee that the
results of a foreign table dump can be successfully restored.
</para>
</note>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>--inserts</option></term> <term><option>--inserts</option></term>
<listitem> <listitem>
......
This diff is collapsed.
...@@ -283,6 +283,7 @@ typedef struct _tableInfo ...@@ -283,6 +283,7 @@ typedef struct _tableInfo
uint32 toast_minmxid; /* toast table's relminmxid */ uint32 toast_minmxid; /* toast table's relminmxid */
int ncheck; /* # of CHECK expressions */ int ncheck; /* # of CHECK expressions */
char *reloftype; /* underlying type for typed table */ char *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: */ /* these two are set only if table is a sequence owned by a column: */
Oid owning_tab; /* OID of table owning sequence */ Oid owning_tab; /* OID of table owning sequence */
int owning_col; /* attr # of column owning sequence */ int owning_col; /* attr # of column owning sequence */
......
...@@ -4,7 +4,7 @@ use warnings; ...@@ -4,7 +4,7 @@ use warnings;
use Config; use Config;
use PostgresNode; use PostgresNode;
use TestLib; use TestLib;
use Test::More tests => 74; use Test::More tests => 78;
my $tempdir = TestLib::tempdir; my $tempdir = TestLib::tempdir;
my $tempdir_short = TestLib::tempdir_short; my $tempdir_short = TestLib::tempdir_short;
...@@ -49,6 +49,18 @@ command_fails_like( ...@@ -49,6 +49,18 @@ command_fails_like(
'pg_dump: options -s/--schema-only and -a/--data-only cannot be used together' 'pg_dump: options -s/--schema-only and -a/--data-only cannot be used together'
); );
command_fails_like(
[ 'pg_dump', '-s', '--include-foreign-data=xxx' ],
qr/\Qpg_dump: error: options -s\/--schema-only and --include-foreign-data cannot be used together\E/,
'pg_dump: options -s/--schema-only and --include-foreign-data cannot be used together'
);
command_fails_like(
[ 'pg_dump', '-j2', '--include-foreign-data=xxx' ],
qr/\Qpg_dump: error: option --include-foreign-data is not supported with parallel backup\E/,
'pg_dump: option --include-foreign-data is not supported with parallel backup'
);
command_fails_like( command_fails_like(
['pg_restore'], ['pg_restore'],
qr{\Qpg_restore: error: one of -d/--dbname and -f/--file must be specified\E}, qr{\Qpg_restore: error: one of -d/--dbname and -f/--file must be specified\E},
......
use strict;
use warnings;
use PostgresNode;
use TestLib;
use Test::More tests => 3;
my $tempdir = TestLib::tempdir;
my $tempdir_short = TestLib::tempdir_short;
my $node = get_new_node('main');
my $port = $node->port;
$node->init;
$node->start;
#########################################
# Verify that dumping foreign data includes only foreign tables of
# matching servers
$node->safe_psql( 'postgres', "CREATE FOREIGN DATA WRAPPER dummy");
$node->safe_psql( 'postgres', "CREATE SERVER s0 FOREIGN DATA WRAPPER dummy");
$node->safe_psql( 'postgres', "CREATE SERVER s1 FOREIGN DATA WRAPPER dummy");
$node->safe_psql( 'postgres', "CREATE SERVER s2 FOREIGN DATA WRAPPER dummy");
$node->safe_psql( 'postgres', "CREATE FOREIGN TABLE t0 (a int) SERVER s0");
$node->safe_psql( 'postgres', "CREATE FOREIGN TABLE t1 (a int) SERVER s1");
my ($cmd, $stdout, $stderr, $result);
command_fails_like(
[ "pg_dump", '-p', $port, 'postgres', '--include-foreign-data=s0' ],
qr/foreign-data wrapper \"dummy\" has no handler\r?\npg_dump: error: query was:.*t0/,
"correctly fails to dump a foreign table from a dummy FDW");
command_ok(
[ "pg_dump", '-p', $port, 'postgres', '-a', '--include-foreign-data=s2' ] ,
"dump foreign server with no tables");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment