Skip to content

Commit 9761ad6

Browse files
committed
Fix some bogosities in pg_dump's foreign-table support.
The server name for a foreign table was not quoted at need, as per report from Ronan Dunklau. Also, queries related to FDW options were inadequately schema-qualified in places where the search path isn't just pg_catalog, and were inconsistently formatted everywhere, and we didn't always check that we got the expected number of rows from them.
1 parent 64aea1e commit 9761ad6

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5667,11 +5667,11 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
56675667
"array_to_string(a.attoptions, ', ') AS attoptions, "
56685668
"CASE WHEN a.attcollation <> t.typcollation "
56695669
"THEN a.attcollation ELSE 0 END AS attcollation, "
5670-
"array_to_string(ARRAY("
5671-
" SELECT quote_ident(option_name) || ' ' || "
5672-
" quote_literal(option_value) "
5673-
" FROM pg_options_to_table(attfdwoptions)), ', ') "
5674-
" AS attfdwoptions "
5670+
"pg_catalog.array_to_string(ARRAY("
5671+
"SELECT pg_catalog.quote_ident(option_name) || "
5672+
"' ' || pg_catalog.quote_literal(option_value) "
5673+
"FROM pg_catalog.pg_options_to_table(attfdwoptions)"
5674+
"), ', ') AS attfdwoptions "
56755675
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
56765676
"ON a.atttypid = t.oid "
56775677
"WHERE a.attrelid = '%u'::pg_catalog.oid "
@@ -6485,9 +6485,10 @@ getForeignDataWrappers(int *numForeignDataWrappers)
64856485
"fdwhandler::pg_catalog.regproc, "
64866486
"fdwvalidator::pg_catalog.regproc, fdwacl, "
64876487
"array_to_string(ARRAY("
6488-
" SELECT quote_ident(option_name) || ' ' || "
6489-
" quote_literal(option_value) "
6490-
" FROM pg_options_to_table(fdwoptions)), ', ') AS fdwoptions "
6488+
"SELECT quote_ident(option_name) || ' ' || "
6489+
"quote_literal(option_value) "
6490+
"FROM pg_options_to_table(fdwoptions)"
6491+
"), ', ') AS fdwoptions "
64916492
"FROM pg_foreign_data_wrapper",
64926493
username_subquery);
64936494
}
@@ -6498,9 +6499,10 @@ getForeignDataWrappers(int *numForeignDataWrappers)
64986499
"'-' AS fdwhandler, "
64996500
"fdwvalidator::pg_catalog.regproc, fdwacl, "
65006501
"array_to_string(ARRAY("
6501-
" SELECT quote_ident(option_name) || ' ' || "
6502-
" quote_literal(option_value) "
6503-
" FROM pg_options_to_table(fdwoptions)), ', ') AS fdwoptions "
6502+
"SELECT quote_ident(option_name) || ' ' || "
6503+
"quote_literal(option_value) "
6504+
"FROM pg_options_to_table(fdwoptions)"
6505+
"), ', ') AS fdwoptions "
65046506
"FROM pg_foreign_data_wrapper",
65056507
username_subquery);
65066508
}
@@ -6586,9 +6588,10 @@ getForeignServers(int *numForeignServers)
65866588
"(%s srvowner) AS rolname, "
65876589
"srvfdw, srvtype, srvversion, srvacl,"
65886590
"array_to_string(ARRAY("
6589-
" SELECT quote_ident(option_name) || ' ' || "
6590-
" quote_literal(option_value) "
6591-
" FROM pg_options_to_table(srvoptions)), ', ') AS srvoptions "
6591+
"SELECT quote_ident(option_name) || ' ' || "
6592+
"quote_literal(option_value) "
6593+
"FROM pg_options_to_table(srvoptions)"
6594+
"), ', ') AS srvoptions "
65926595
"FROM pg_foreign_server",
65936596
username_subquery);
65946597

@@ -11650,9 +11653,13 @@ dumpUserMappings(Archive *fout,
1165011653

1165111654
appendPQExpBuffer(query,
1165211655
"SELECT usename, "
11653-
"array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM pg_options_to_table(umoptions)), ', ') AS umoptions\n"
11656+
"array_to_string(ARRAY("
11657+
"SELECT quote_ident(option_name) || ' ' || "
11658+
"quote_literal(option_value) "
11659+
"FROM pg_options_to_table(umoptions)"
11660+
"), ', ') AS umoptions "
1165411661
"FROM pg_user_mappings "
11655-
"WHERE srvid = %u",
11662+
"WHERE srvid = '%u'",
1165611663
catalogId.oid);
1165711664

1165811665
res = PQexec(g_conn, query->data);
@@ -12212,12 +12219,12 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1221212219
int numParents;
1221312220
TableInfo **parents;
1221412221
int actual_atts; /* number of attrs in this CREATE statment */
12215-
char *reltypename;
12222+
const char *reltypename;
1221612223
char *storage;
12224+
char *srvname;
12225+
char *ftoptions;
1221712226
int j,
1221812227
k;
12219-
char *srvname;
12220-
char *ftoptions = NULL;
1222112228

1222212229
/* Make sure we are in proper schema */
1222312230
selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
@@ -12303,15 +12310,25 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1230312310

1230412311
/* retrieve name of foreign server and generic options */
1230512312
appendPQExpBuffer(query,
12306-
"SELECT fs.srvname, array_to_string(ARRAY("
12307-
" SELECT quote_ident(option_name) || ' ' || "
12308-
" quote_literal(option_value)"
12309-
" FROM pg_options_to_table(ftoptions)), ', ') AS ftoptions "
12310-
"FROM pg_foreign_table ft JOIN pg_foreign_server fs "
12311-
" ON (fs.oid = ft.ftserver) "
12312-
"WHERE ft.ftrelid = %u", tbinfo->dobj.catId.oid);
12313+
"SELECT fs.srvname, "
12314+
"pg_catalog.array_to_string(ARRAY("
12315+
"SELECT pg_catalog.quote_ident(option_name) || "
12316+
"' ' || pg_catalog.quote_literal(option_value) "
12317+
"FROM pg_catalog.pg_options_to_table(ftoptions)"
12318+
"), ', ') AS ftoptions "
12319+
"FROM pg_catalog.pg_foreign_table ft "
12320+
"JOIN pg_catalog.pg_foreign_server fs "
12321+
"ON (fs.oid = ft.ftserver) "
12322+
"WHERE ft.ftrelid = '%u'",
12323+
tbinfo->dobj.catId.oid);
1231312324
res = PQexec(g_conn, query->data);
1231412325
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
12326+
if (PQntuples(res) != 1)
12327+
{
12328+
write_msg(NULL, "query returned %d foreign server entries for foreign table \"%s\"\n",
12329+
PQntuples(res), tbinfo->dobj.name);
12330+
exit_nicely();
12331+
}
1231512332
i_srvname = PQfnumber(res, "srvname");
1231612333
i_ftoptions = PQfnumber(res, "ftoptions");
1231712334
srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
@@ -12502,7 +12519,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1250212519
}
1250312520

1250412521
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
12505-
appendPQExpBuffer(q, "\nSERVER %s", srvname);
12522+
appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
1250612523

1250712524
if ((tbinfo->reloptions && strlen(tbinfo->reloptions) > 0) ||
1250812525
(tbinfo->toast_reloptions && strlen(tbinfo->toast_reloptions) > 0))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy