Skip to content

Add --with-childs option to pg_dump #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/src/sgml/ref/pg_dump.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,17 @@ PostgreSQL documentation
</listitem>
</varlistentry>

<varlistentry>
<term><option>--with-childs</option></term>
<listitem>
<para>
Include or exclude from a dump all child and partition tables when a parent
table is specified using option <option>-t</option>/<option>--table</option>
or <option>-T</option>/<option>--exclude-table</option>.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><option>-?</option></term>
<term><option>--help</option></term>
Expand Down
1 change: 1 addition & 0 deletions src/bin/pg_dump/pg_backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ typedef struct _dumpOptions

int sequence_data; /* dump sequence data even in schema-only mode */
int do_nothing;
bool with_childs;
} DumpOptions;

/*
Expand Down
39 changes: 39 additions & 0 deletions src/bin/pg_dump/pg_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ main(int argc, char **argv)
{"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
{"rows-per-insert", required_argument, NULL, 10},
{"include-foreign-data", required_argument, NULL, 11},
{"with-childs", no_argument, NULL, 12},

{NULL, 0, NULL, 0}
};
Expand Down Expand Up @@ -631,6 +632,10 @@ main(int argc, char **argv)
optarg);
break;

case 12: /* dump child table too */
dopt.with_childs = true;
break;

default:
/* getopt_long already emitted a complaint */
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
Expand Down Expand Up @@ -810,6 +815,14 @@ main(int argc, char **argv)
false);
/* non-matching exclusion patterns aren't an error */

/*
* The include child option require that there is
* at least one table inclusion
*/
if (dopt.with_childs && table_include_patterns.head == NULL
&& table_exclude_patterns.head == NULL)
pg_fatal("option --with-childs requires option -t/--table or -T/--exclude-table");

/* Expand table selection patterns into OID lists */
if (table_include_patterns.head != NULL)
{
Expand Down Expand Up @@ -1088,6 +1101,9 @@ help(const char *progname)
printf(_(" --use-set-session-authorization\n"
" use SET SESSION AUTHORIZATION commands instead of\n"
" ALTER OWNER commands to set ownership\n"));
printf(_(" --with-childs include or exclude from a dump all child and partition\n"
" tables when a parent table is specified using\n"
" -t/--table or -T/--exclude-table\n"));

printf(_("\nConnection options:\n"));
printf(_(" -d, --dbname=DBNAME database to dump\n"));
Expand Down Expand Up @@ -1520,6 +1536,15 @@ expand_table_name_patterns(Archive *fout,
PQExpBufferData dbbuf;
int dotcnt;

/*
* With --include_child we look recursively to the inheritance
* tree to find the childs tables of the matching include filter
*/
if (fout->dopt->with_childs)
{
appendPQExpBuffer(query, "WITH RECURSIVE child_tree (relid) AS (\n");
}

/*
* Query must remain ABSOLUTELY devoid of unqualified names. This
* would be unnecessary given a pg_table_is_visible() variant taking a
Expand Down Expand Up @@ -1547,6 +1572,20 @@ expand_table_name_patterns(Archive *fout,
prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
termPQExpBuffer(&dbbuf);

if (fout->dopt->with_childs)
{
appendPQExpBuffer(query, "\n UNION ALL"
"\n SELECT c.oid AS relid"
"\n FROM child_tree AS p"
"\n JOIN pg_catalog.pg_inherits AS i"
"\n ON (p.relid OPERATOR(pg_catalog.=) i.inhparent)"
"\n JOIN pg_catalog.pg_class AS c"
"\n ON (c.oid OPERATOR(pg_catalog.=) i.inhrelid)"
"\n)"
"\nSELECT relid FROM child_tree");

}

ExecuteSqlStatement(fout, "RESET search_path");
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
PQclear(ExecuteSqlQueryForSingleRow(fout,
Expand Down
Loading