Commit 9761ad67 authored by Tom Lane's avatar Tom Lane

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.
parent 64aea1eb
...@@ -5667,11 +5667,11 @@ getTableAttrs(TableInfo *tblinfo, int numTables) ...@@ -5667,11 +5667,11 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
"array_to_string(a.attoptions, ', ') AS attoptions, " "array_to_string(a.attoptions, ', ') AS attoptions, "
"CASE WHEN a.attcollation <> t.typcollation " "CASE WHEN a.attcollation <> t.typcollation "
"THEN a.attcollation ELSE 0 END AS attcollation, " "THEN a.attcollation ELSE 0 END AS attcollation, "
"array_to_string(ARRAY(" "pg_catalog.array_to_string(ARRAY("
" SELECT quote_ident(option_name) || ' ' || " "SELECT pg_catalog.quote_ident(option_name) || "
" quote_literal(option_value) " "' ' || pg_catalog.quote_literal(option_value) "
" FROM pg_options_to_table(attfdwoptions)), ', ') " "FROM pg_catalog.pg_options_to_table(attfdwoptions)"
" AS attfdwoptions " "), ', ') AS attfdwoptions "
"FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t " "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
"ON a.atttypid = t.oid " "ON a.atttypid = t.oid "
"WHERE a.attrelid = '%u'::pg_catalog.oid " "WHERE a.attrelid = '%u'::pg_catalog.oid "
...@@ -6485,9 +6485,10 @@ getForeignDataWrappers(int *numForeignDataWrappers) ...@@ -6485,9 +6485,10 @@ getForeignDataWrappers(int *numForeignDataWrappers)
"fdwhandler::pg_catalog.regproc, " "fdwhandler::pg_catalog.regproc, "
"fdwvalidator::pg_catalog.regproc, fdwacl, " "fdwvalidator::pg_catalog.regproc, fdwacl, "
"array_to_string(ARRAY(" "array_to_string(ARRAY("
" SELECT quote_ident(option_name) || ' ' || " "SELECT quote_ident(option_name) || ' ' || "
" quote_literal(option_value) " "quote_literal(option_value) "
" FROM pg_options_to_table(fdwoptions)), ', ') AS fdwoptions " "FROM pg_options_to_table(fdwoptions)"
"), ', ') AS fdwoptions "
"FROM pg_foreign_data_wrapper", "FROM pg_foreign_data_wrapper",
username_subquery); username_subquery);
} }
...@@ -6498,9 +6499,10 @@ getForeignDataWrappers(int *numForeignDataWrappers) ...@@ -6498,9 +6499,10 @@ getForeignDataWrappers(int *numForeignDataWrappers)
"'-' AS fdwhandler, " "'-' AS fdwhandler, "
"fdwvalidator::pg_catalog.regproc, fdwacl, " "fdwvalidator::pg_catalog.regproc, fdwacl, "
"array_to_string(ARRAY(" "array_to_string(ARRAY("
" SELECT quote_ident(option_name) || ' ' || " "SELECT quote_ident(option_name) || ' ' || "
" quote_literal(option_value) " "quote_literal(option_value) "
" FROM pg_options_to_table(fdwoptions)), ', ') AS fdwoptions " "FROM pg_options_to_table(fdwoptions)"
"), ', ') AS fdwoptions "
"FROM pg_foreign_data_wrapper", "FROM pg_foreign_data_wrapper",
username_subquery); username_subquery);
} }
...@@ -6586,9 +6588,10 @@ getForeignServers(int *numForeignServers) ...@@ -6586,9 +6588,10 @@ getForeignServers(int *numForeignServers)
"(%s srvowner) AS rolname, " "(%s srvowner) AS rolname, "
"srvfdw, srvtype, srvversion, srvacl," "srvfdw, srvtype, srvversion, srvacl,"
"array_to_string(ARRAY(" "array_to_string(ARRAY("
" SELECT quote_ident(option_name) || ' ' || " "SELECT quote_ident(option_name) || ' ' || "
" quote_literal(option_value) " "quote_literal(option_value) "
" FROM pg_options_to_table(srvoptions)), ', ') AS srvoptions " "FROM pg_options_to_table(srvoptions)"
"), ', ') AS srvoptions "
"FROM pg_foreign_server", "FROM pg_foreign_server",
username_subquery); username_subquery);
...@@ -11650,9 +11653,13 @@ dumpUserMappings(Archive *fout, ...@@ -11650,9 +11653,13 @@ dumpUserMappings(Archive *fout,
appendPQExpBuffer(query, appendPQExpBuffer(query,
"SELECT usename, " "SELECT usename, "
"array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM pg_options_to_table(umoptions)), ', ') AS umoptions\n" "array_to_string(ARRAY("
"SELECT quote_ident(option_name) || ' ' || "
"quote_literal(option_value) "
"FROM pg_options_to_table(umoptions)"
"), ', ') AS umoptions "
"FROM pg_user_mappings " "FROM pg_user_mappings "
"WHERE srvid = %u", "WHERE srvid = '%u'",
catalogId.oid); catalogId.oid);
res = PQexec(g_conn, query->data); res = PQexec(g_conn, query->data);
...@@ -12212,12 +12219,12 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) ...@@ -12212,12 +12219,12 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
int numParents; int numParents;
TableInfo **parents; TableInfo **parents;
int actual_atts; /* number of attrs in this CREATE statment */ int actual_atts; /* number of attrs in this CREATE statment */
char *reltypename; const char *reltypename;
char *storage; char *storage;
char *srvname;
char *ftoptions;
int j, int j,
k; k;
char *srvname;
char *ftoptions = NULL;
/* Make sure we are in proper schema */ /* Make sure we are in proper schema */
selectSourceSchema(tbinfo->dobj.namespace->dobj.name); selectSourceSchema(tbinfo->dobj.namespace->dobj.name);
...@@ -12303,15 +12310,25 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) ...@@ -12303,15 +12310,25 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
/* retrieve name of foreign server and generic options */ /* retrieve name of foreign server and generic options */
appendPQExpBuffer(query, appendPQExpBuffer(query,
"SELECT fs.srvname, array_to_string(ARRAY(" "SELECT fs.srvname, "
" SELECT quote_ident(option_name) || ' ' || " "pg_catalog.array_to_string(ARRAY("
" quote_literal(option_value)" "SELECT pg_catalog.quote_ident(option_name) || "
" FROM pg_options_to_table(ftoptions)), ', ') AS ftoptions " "' ' || pg_catalog.quote_literal(option_value) "
"FROM pg_foreign_table ft JOIN pg_foreign_server fs " "FROM pg_catalog.pg_options_to_table(ftoptions)"
" ON (fs.oid = ft.ftserver) " "), ', ') AS ftoptions "
"WHERE ft.ftrelid = %u", tbinfo->dobj.catId.oid); "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 = PQexec(g_conn, query->data); res = PQexec(g_conn, query->data);
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK); check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) != 1)
{
write_msg(NULL, "query returned %d foreign server entries for foreign table \"%s\"\n",
PQntuples(res), tbinfo->dobj.name);
exit_nicely();
}
i_srvname = PQfnumber(res, "srvname"); i_srvname = PQfnumber(res, "srvname");
i_ftoptions = PQfnumber(res, "ftoptions"); i_ftoptions = PQfnumber(res, "ftoptions");
srvname = pg_strdup(PQgetvalue(res, 0, i_srvname)); srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
...@@ -12502,7 +12519,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo) ...@@ -12502,7 +12519,7 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
} }
if (tbinfo->relkind == RELKIND_FOREIGN_TABLE) if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
appendPQExpBuffer(q, "\nSERVER %s", srvname); appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
if ((tbinfo->reloptions && strlen(tbinfo->reloptions) > 0) || if ((tbinfo->reloptions && strlen(tbinfo->reloptions) > 0) ||
(tbinfo->toast_reloptions && strlen(tbinfo->toast_reloptions) > 0)) (tbinfo->toast_reloptions && strlen(tbinfo->toast_reloptions) > 0))
......
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