Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5c19c05

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 0702c86 commit 5c19c05

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6378,9 +6378,10 @@ getForeignDataWrappers(int *numForeignDataWrappers)
63786378
"fdwhandler::pg_catalog.regproc, "
63796379
"fdwvalidator::pg_catalog.regproc, fdwacl, "
63806380
"array_to_string(ARRAY("
6381-
" SELECT quote_ident(option_name) || ' ' || "
6382-
" quote_literal(option_value) "
6383-
" FROM pg_options_to_table(fdwoptions)), ', ') AS fdwoptions "
6381+
"SELECT quote_ident(option_name) || ' ' || "
6382+
"quote_literal(option_value) "
6383+
"FROM pg_options_to_table(fdwoptions)"
6384+
"), ', ') AS fdwoptions "
63846385
"FROM pg_foreign_data_wrapper",
63856386
username_subquery);
63866387
}
@@ -6391,9 +6392,10 @@ getForeignDataWrappers(int *numForeignDataWrappers)
63916392
"'-' AS fdwhandler, "
63926393
"fdwvalidator::pg_catalog.regproc, fdwacl, "
63936394
"array_to_string(ARRAY("
6394-
" SELECT quote_ident(option_name) || ' ' || "
6395-
" quote_literal(option_value) "
6396-
" FROM pg_options_to_table(fdwoptions)), ', ') AS fdwoptions "
6395+
"SELECT quote_ident(option_name) || ' ' || "
6396+
"quote_literal(option_value) "
6397+
"FROM pg_options_to_table(fdwoptions)"
6398+
"), ', ') AS fdwoptions "
63976399
"FROM pg_foreign_data_wrapper",
63986400
username_subquery);
63996401
}
@@ -6479,9 +6481,10 @@ getForeignServers(int *numForeignServers)
64796481
"(%s srvowner) AS rolname, "
64806482
"srvfdw, srvtype, srvversion, srvacl,"
64816483
"array_to_string(ARRAY("
6482-
" SELECT quote_ident(option_name) || ' ' || "
6483-
" quote_literal(option_value) "
6484-
" FROM pg_options_to_table(srvoptions)), ', ') AS srvoptions "
6484+
"SELECT quote_ident(option_name) || ' ' || "
6485+
"quote_literal(option_value) "
6486+
"FROM pg_options_to_table(srvoptions)"
6487+
"), ', ') AS srvoptions "
64856488
"FROM pg_foreign_server",
64866489
username_subquery);
64876490

@@ -11407,9 +11410,13 @@ dumpUserMappings(Archive *fout,
1140711410

1140811411
appendPQExpBuffer(query,
1140911412
"SELECT usename, "
11410-
"array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM pg_options_to_table(umoptions)), ', ') AS umoptions\n"
11413+
"array_to_string(ARRAY("
11414+
"SELECT quote_ident(option_name) || ' ' || "
11415+
"quote_literal(option_value) "
11416+
"FROM pg_options_to_table(umoptions)"
11417+
"), ', ') AS umoptions "
1141111418
"FROM pg_user_mappings "
11412-
"WHERE srvid = %u",
11419+
"WHERE srvid = '%u'",
1141311420
catalogId.oid);
1141411421

1141511422
res = PQexec(g_conn, query->data);
@@ -11969,12 +11976,12 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1196911976
int numParents;
1197011977
TableInfo **parents;
1197111978
int actual_atts; /* number of attrs in this CREATE statment */
11972-
char *reltypename;
11979+
const char *reltypename;
1197311980
char *storage;
11981+
char *srvname;
11982+
char *ftoptions;
1197411983
int j,
1197511984
k;
11976-
char *srvname;
11977-
char *ftoptions = NULL;
1197811985

1197911986
/* Make sure we are in proper schema */
1198011987
selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
@@ -12060,15 +12067,25 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1206012067

1206112068
/* retrieve name of foreign server and generic options */
1206212069
appendPQExpBuffer(query,
12063-
"SELECT fs.srvname, array_to_string(ARRAY("
12064-
" SELECT quote_ident(option_name) || ' ' || "
12065-
" quote_literal(option_value)"
12066-
" FROM pg_options_to_table(ftoptions)), ', ') AS ftoptions "
12067-
"FROM pg_foreign_table ft JOIN pg_foreign_server fs "
12068-
" ON (fs.oid = ft.ftserver) "
12069-
"WHERE ft.ftrelid = %u", tbinfo->dobj.catId.oid);
12070+
"SELECT fs.srvname, "
12071+
"pg_catalog.array_to_string(ARRAY("
12072+
"SELECT pg_catalog.quote_ident(option_name) || "
12073+
"' ' || pg_catalog.quote_literal(option_value) "
12074+
"FROM pg_catalog.pg_options_to_table(ftoptions)"
12075+
"), ', ') AS ftoptions "
12076+
"FROM pg_catalog.pg_foreign_table ft "
12077+
"JOIN pg_catalog.pg_foreign_server fs "
12078+
"ON (fs.oid = ft.ftserver) "
12079+
"WHERE ft.ftrelid = '%u'",
12080+
tbinfo->dobj.catId.oid);
1207012081
res = PQexec(g_conn, query->data);
1207112082
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
12083+
if (PQntuples(res) != 1)
12084+
{
12085+
write_msg(NULL, "query returned %d foreign server entries for foreign table \"%s\"\n",
12086+
PQntuples(res), tbinfo->dobj.name);
12087+
exit_nicely();
12088+
}
1207212089
i_srvname = PQfnumber(res, "srvname");
1207312090
i_ftoptions = PQfnumber(res, "ftoptions");
1207412091
srvname = strdup(PQgetvalue(res, 0, i_srvname));
@@ -12259,7 +12276,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1225912276
}
1226012277

1226112278
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
12262-
appendPQExpBuffer(q, "\nSERVER %s", srvname);
12279+
appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
1226312280

1226412281
if ((tbinfo->reloptions && strlen(tbinfo->reloptions) > 0) ||
1226512282
(tbinfo->toast_reloptions && strlen(tbinfo->toast_reloptions) > 0))

0 commit comments

Comments
 (0)