build_remattrmap() deparses a list of remote column names for a query
that retrieves attribute stats for them from the remote server.
Previously, it did so by using the array-literal syntax with each column
name individually quoted by quote_identifier(), causing the query to
fail on the remote server with a syntax error or no results when that
column name included a single quote or backslash, as quote_identifier()
doesn't escape those characters, making the query invalid or incorrect.
Fix by switching from the array-literal syntax to the ARRAY constructor
syntax with each column name individually quoted by
deparseStringLiteral().
Oversight in commit
28972b6fc.
Reported-by: Satya Narlapuram <satyanarlapuram@gmail.com>
Reported-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Author: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Reviewed-by: Alex Guo <guo.alex.hengchen@gmail.com>
Reviewed-by: Zhenwei Shang <a934172442@gmail.com>
Reviewed-by: Etsuro Fujita <etsuro.fujita@gmail.com>
Discussion: https://postgr.es/m/CAHg%2BQDc9%3DWtYi%3DJW6QUL6ASOJc6PcGPTuxoMkhnkQ7oi7j5atg%40mail.gmail.com
Discussion: https://postgr.es/m/CAJTYsWWGhVDFjr%2BsmdYdU-Q_TT9YMzXA4QcLCr7rizDOyrEEow%40mail.gmail.com
ALTER FOREIGN TABLE simport_fview OPTIONS (ADD restore_stats 'true');
ANALYZE simport_fview; -- should fail
WARNING: could not import statistics for foreign table "public.simport_fview" --- remote table "public.simport_view" is of relkind "v" which cannot have statistics
+-- This tests build_remattrmap()'s deparsing of column names that include
+-- single quotes or backslashes
+CREATE TABLE dtest_table ("col'quote" int, "col\backslash" int);
+CREATE FOREIGN TABLE dtest_ftable ("col'quote" int, "col\backslash" int)
+ SERVER loopback OPTIONS (table_name 'dtest_table', restore_stats 'true');
+INSERT INTO dtest_table SELECT g, g FROM generate_series(1, 10) g;
+ANALYZE dtest_table;
+ANALYZE VERBOSE dtest_ftable; -- should work
+INFO: importing statistics for foreign table "public.dtest_ftable"
+INFO: finished importing statistics for foreign table "public.dtest_ftable"
-- cleanup
DROP FOREIGN TABLE simport_ftable;
DROP FOREIGN TABLE simport_fview;
DROP VIEW simport_view;
DROP TABLE simport_table;
+DROP FOREIGN TABLE dtest_ftable;
+DROP TABLE dtest_table;
-- ===================================================================
-- test for postgres_fdw_get_connections function with check_conn = true
-- ===================================================================
" AND tablename = ");
deparseStringLiteral(&sql, remote_relname);
appendStringInfo(&sql,
- " AND attname = ANY('%s'::text[])",
+ " AND attname = ANY(%s)",
column_list);
/* inherited is supported since Postgres 9.0 */
remattrmap = palloc_array(RemoteAttributeMapping, tupdesc->natts);
initStringInfo(column_list);
- appendStringInfoChar(column_list, '{');
+ appendStringInfoString(column_list, "ARRAY[");
for (int i = 0; i < tupdesc->natts; i++)
{
Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
if (attrcnt > 0)
appendStringInfoString(column_list, ", ");
- appendStringInfoString(column_list, quote_identifier(remote_attname));
+ deparseStringLiteral(column_list, remote_attname);
remattrmap[attrcnt].local_attnum = attnum;
strncpy(remattrmap[attrcnt].local_attname, attname, NAMEDATALEN);
remattrmap[attrcnt].res_index = -1;
attrcnt++;
}
- appendStringInfoChar(column_list, '}');
+ appendStringInfoChar(column_list, ']');
/* Sort mapping by remote attribute name if needed. */
if (attrcnt > 1)
ANALYZE simport_fview; -- should fail
+-- This tests build_remattrmap()'s deparsing of column names that include
+-- single quotes or backslashes
+CREATE TABLE dtest_table ("col'quote" int, "col\backslash" int);
+CREATE FOREIGN TABLE dtest_ftable ("col'quote" int, "col\backslash" int)
+ SERVER loopback OPTIONS (table_name 'dtest_table', restore_stats 'true');
+
+INSERT INTO dtest_table SELECT g, g FROM generate_series(1, 10) g;
+ANALYZE dtest_table;
+
+ANALYZE VERBOSE dtest_ftable; -- should work
+
-- cleanup
DROP FOREIGN TABLE simport_ftable;
DROP FOREIGN TABLE simport_fview;
DROP VIEW simport_view;
DROP TABLE simport_table;
+DROP FOREIGN TABLE dtest_ftable;
+DROP TABLE dtest_table;
-- ===================================================================
-- test for postgres_fdw_get_connections function with check_conn = true