Skip to content

Commit 08cfdff

Browse files
Peter Smithhackorum
authored andcommitted
Fix escapes for psql describe schema patterns
psql \dn was building the query with a raw '%s' substitution of the user-supplied pattern, so a pattern containing a single quote (\dn "HAS ' QUOTE") broke out of the string literal and produced a syntax error on the server. The failure happened when querying to find publications involving this schema. Fixed by using a string escape function instead of raw-substitution to the SQL. Author: Peter Smith <smithpb2250@gmail.com> Reviewed-by: Steven Niu <niushiji@gmail.com> Reviewed by: Jim Jones <jim.jones@uni-muenster.de> Reviewed-by: Nishant Sharma <nishant.sharma@enterprisedb.com> Reviewed-by: surya poondla <suryapoondla4@gmail.com> Reviewed-by: Ajin Cherian <itsajin@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/SA5PPF230C70D4BFBD6006437E357AE7CF9A7CB2%40SA5PPF230C70D4B.namprd14.prod.outlook.com#c4b1b145852d2076db6590e0aae5c7da
1 parent 4255812 commit 08cfdff

3 files changed

Lines changed: 167 additions & 9 deletions

File tree

‎src/bin/psql/describe.c‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5202,20 +5202,27 @@ listSchemas(const char *pattern, bool verbose, bool showSystem)
52025202
myopt.title = _("List of schemas");
52035203
myopt.translate_header = true;
52045204

5205-
if (pattern && pset.sversion >= 150000)
5205+
/*
5206+
* \dn returns a LIST of schemas, so adding a publications footer to the
5207+
* describe output makes sense only if the list has a single row.
5208+
*/
5209+
if (pattern && pset.sversion >= 150000 && PQntuples(res) == 1)
52065210
{
52075211
PGresult *result;
52085212
int i;
5213+
const char *schema_name = PQgetvalue(res, 0, 0);
52095214

52105215
addQueryComment(&buf, "Get publications that publish this schema");
5211-
appendPQExpBuffer(&buf,
5212-
"SELECT pubname \n"
5213-
"FROM pg_catalog.pg_publication p\n"
5214-
" JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n"
5215-
" JOIN pg_catalog.pg_namespace n ON n.oid = pn.pnnspid \n"
5216-
"WHERE n.nspname = '%s'\n"
5217-
"ORDER BY 1",
5218-
pattern);
5216+
5217+
appendPQExpBufferStr(&buf,
5218+
"SELECT pubname \n"
5219+
"FROM pg_catalog.pg_publication p\n"
5220+
" JOIN pg_catalog.pg_publication_namespace pn ON p.oid = pn.pnpubid\n"
5221+
" JOIN pg_catalog.pg_namespace n ON n.oid = pn.pnnspid \n"
5222+
"WHERE n.nspname = ");
5223+
appendStringLiteralConn(&buf, schema_name, pset.db);
5224+
appendPQExpBufferStr(&buf, "\nORDER BY 1;");
5225+
52195226
result = PSQLexec(buf.data);
52205227
if (!result)
52215228
goto error_return;

‎src/test/regress/expected/publication.out‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,120 @@ CREATE PUBLICATION testpub_parsertst FOR TABLES IN SCHEMA foo, test.foo;
171171
ERROR: invalid schema name
172172
LINE 1: ...CATION testpub_parsertst FOR TABLES IN SCHEMA foo, test.foo;
173173
^
174+
-- create schema names with embedded quotes or backslashes
175+
-- should be able to describe them in psql
176+
CREATE SCHEMA "test-schema-embedded ' quote";
177+
CREATE SCHEMA "test-schema-embedded \ backslash";
178+
\dn "test-schema-embedded ' quote"
179+
List of schemas
180+
Name | Owner
181+
------------------------------+--------------------------
182+
test-schema-embedded ' quote | regress_publication_user
183+
(1 row)
184+
185+
\dn "test-schema-embedded \ backslash"
186+
List of schemas
187+
Name | Owner
188+
----------------------------------+--------------------------
189+
test-schema-embedded \ backslash | regress_publication_user
190+
(1 row)
191+
192+
\dn+ test-schema-embedded*
193+
List of schemas
194+
Name | Owner | Access privileges | Description
195+
----------------------------------+--------------------------+-------------------+-------------
196+
test-schema-embedded ' quote | regress_publication_user | |
197+
test-schema-embedded \ backslash | regress_publication_user | |
198+
(2 rows)
199+
200+
-- should be able to publish schemas with embedded quotes
201+
-- or backslashes, and see that they are published
202+
CREATE TABLE "test-schema-embedded ' quote"."test-table-embedded ' quote"("test-col1-embedded ' quote" int, "test-col2-embedded ' quote" int);
203+
CREATE TABLE "test-schema-embedded \ backslash".tab1(a int);
204+
CREATE PUBLICATION regress_pub_embedded1
205+
FOR TABLES IN SCHEMA "test-schema-embedded ' quote", "test-schema-embedded \ backslash";
206+
CREATE PUBLICATION regress_pub_embedded2 FOR ALL TABLES EXCEPT (TABLE "test-schema-embedded ' quote"."test-table-embedded ' quote");
207+
\dn "test-schema-embedded ' quote"
208+
List of schemas
209+
Name | Owner
210+
------------------------------+--------------------------
211+
test-schema-embedded ' quote | regress_publication_user
212+
Included in publications:
213+
"regress_pub_embedded1"
214+
215+
\dn "test-schema-embedded \ backslash"
216+
List of schemas
217+
Name | Owner
218+
----------------------------------+--------------------------
219+
test-schema-embedded \ backslash | regress_publication_user
220+
Included in publications:
221+
"regress_pub_embedded1"
222+
223+
\d "test-schema-embedded ' quote"."test-table-embedded ' quote"
224+
Table "test-schema-embedded ' quote.test-table-embedded ' quote"
225+
Column | Type | Collation | Nullable | Default
226+
----------------------------+---------+-----------+----------+---------
227+
test-col1-embedded ' quote | integer | | |
228+
test-col2-embedded ' quote | integer | | |
229+
Included in publications:
230+
"regress_pub_embedded1"
231+
"testpub_foralltables"
232+
Excluded from publications:
233+
"regress_pub_embedded2"
234+
235+
\dRp+ regress_pub_embedded*
236+
Publication regress_pub_embedded1
237+
Owner | All tables | All sequences | Inserts | Updates | Deletes | Truncates | Generated columns | Via root | Description
238+
--------------------------+------------+---------------+---------+---------+---------+-----------+-------------------+----------+-------------
239+
regress_publication_user | f | f | t | t | t | t | none | f |
240+
Tables from schemas:
241+
"test-schema-embedded ' quote"
242+
"test-schema-embedded \ backslash"
243+
244+
Publication regress_pub_embedded2
245+
Owner | All tables | All sequences | Inserts | Updates | Deletes | Truncates | Generated columns | Via root | Description
246+
--------------------------+------------+---------------+---------+---------+---------+-----------+-------------------+----------+-------------
247+
regress_publication_user | t | f | t | t | t | t | none | f |
248+
Except tables:
249+
"test-schema-embedded ' quote.test-table-embedded ' quote"
250+
251+
DROP PUBLICATION regress_pub_embedded1, regress_pub_embedded2;
252+
DROP SCHEMA "test-schema-embedded ' quote" CASCADE;
253+
NOTICE: drop cascades to table "test-schema-embedded ' quote"."test-table-embedded ' quote"
254+
DROP SCHEMA "test-schema-embedded \ backslash" CASCADE;
255+
NOTICE: drop cascades to table "test-schema-embedded \ backslash".tab1
256+
-- test describe and publication of some other tricky schema names
257+
CREATE SCHEMA "MixedCase";
258+
CREATE SCHEMA "2200";
259+
CREATE PUBLICATION regress_pub_tricky_schemas FOR TABLES IN SCHEMA "MixedCase", "2200";
260+
\dn "MixedCase"
261+
List of schemas
262+
Name | Owner
263+
-----------+--------------------------
264+
MixedCase | regress_publication_user
265+
Included in publications:
266+
"regress_pub_tricky_schemas"
267+
268+
\dn 2200
269+
List of schemas
270+
Name | Owner
271+
------+--------------------------
272+
2200 | regress_publication_user
273+
Included in publications:
274+
"regress_pub_tricky_schemas"
275+
276+
\dRp+ regress_pub_tricky_schemas
277+
Publication regress_pub_tricky_schemas
278+
Owner | All tables | All sequences | Inserts | Updates | Deletes | Truncates | Generated columns | Via root | Description
279+
--------------------------+------------+---------------+---------+---------+---------+-----------+-------------------+----------+-------------
280+
regress_publication_user | f | f | t | t | t | t | none | f |
281+
Tables from schemas:
282+
"2200"
283+
"MixedCase"
284+
285+
DROP PUBLICATION regress_pub_tricky_schemas;
286+
DROP SCHEMA "MixedCase" CASCADE;
287+
DROP SCHEMA "2200" CASCADE;
174288
-- should be able to add a table of the same schema to the schema publication
175289
ALTER PUBLICATION testpub_forschema ADD TABLE pub_test.testpub_nopk;
176290
\dRp+ testpub_forschema

‎src/test/regress/sql/publication.sql‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,43 @@ RESET client_min_messages;
9090
CREATE PUBLICATION testpub_parsertst FOR TABLE pub_test.testpub_nopk, CURRENT_SCHEMA;
9191
CREATE PUBLICATION testpub_parsertst FOR TABLES IN SCHEMA foo, test.foo;
9292

93+
-- create schema names with embedded quotes or backslashes
94+
-- should be able to describe them in psql
95+
CREATE SCHEMA "test-schema-embedded ' quote";
96+
CREATE SCHEMA "test-schema-embedded \ backslash";
97+
\dn "test-schema-embedded ' quote"
98+
\dn "test-schema-embedded \ backslash"
99+
\dn+ test-schema-embedded*
100+
101+
-- should be able to publish schemas with embedded quotes
102+
-- or backslashes, and see that they are published
103+
CREATE TABLE "test-schema-embedded ' quote"."test-table-embedded ' quote"("test-col1-embedded ' quote" int, "test-col2-embedded ' quote" int);
104+
CREATE TABLE "test-schema-embedded \ backslash".tab1(a int);
105+
106+
CREATE PUBLICATION regress_pub_embedded1
107+
FOR TABLES IN SCHEMA "test-schema-embedded ' quote", "test-schema-embedded \ backslash";
108+
CREATE PUBLICATION regress_pub_embedded2 FOR ALL TABLES EXCEPT (TABLE "test-schema-embedded ' quote"."test-table-embedded ' quote");
109+
110+
\dn "test-schema-embedded ' quote"
111+
\dn "test-schema-embedded \ backslash"
112+
\d "test-schema-embedded ' quote"."test-table-embedded ' quote"
113+
\dRp+ regress_pub_embedded*
114+
115+
DROP PUBLICATION regress_pub_embedded1, regress_pub_embedded2;
116+
DROP SCHEMA "test-schema-embedded ' quote" CASCADE;
117+
DROP SCHEMA "test-schema-embedded \ backslash" CASCADE;
118+
119+
-- test describe and publication of some other tricky schema names
120+
CREATE SCHEMA "MixedCase";
121+
CREATE SCHEMA "2200";
122+
CREATE PUBLICATION regress_pub_tricky_schemas FOR TABLES IN SCHEMA "MixedCase", "2200";
123+
\dn "MixedCase"
124+
\dn 2200
125+
\dRp+ regress_pub_tricky_schemas
126+
DROP PUBLICATION regress_pub_tricky_schemas;
127+
DROP SCHEMA "MixedCase" CASCADE;
128+
DROP SCHEMA "2200" CASCADE;
129+
93130
-- should be able to add a table of the same schema to the schema publication
94131
ALTER PUBLICATION testpub_forschema ADD TABLE pub_test.testpub_nopk;
95132
\dRp+ testpub_forschema

0 commit comments

Comments
 (0)