Skip to content

Commit a2fc343

Browse files
committed
Clean up SQL emitted by psql/describe.c.
Fix assorted places that had not bothered with the convention of prefixing catalog and function names with "pg_catalog.". That could possibly result in query failure when running with a nondefault search_path. Also fix two places that weren't quoting OID literals. I think the latter hasn't mattered much since about 7.3, but it's still a bad idea to be doing it in 99 places and not in 2 others. Also remove a useless EXISTS sub-select that someone had stuck into describeOneTableDetails' queries for child tables. We just got the OID out of pg_class, so I hardly see how checking that it exists in pg_class was doing anything helpful. In passing, try to improve the emitted formatting of a couple of these queries, though I didn't work really hard on that. And merge unnecessarily duplicative coding in some other places. Much of this was new in HEAD, but some was quite old; back-patch as appropriate.
1 parent 8c34876 commit a2fc343

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

src/bin/psql/describe.c

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,8 +1553,8 @@ describeOneTableDetails(const char *schemaname,
15531553
appendPQExpBufferStr(&buf, ",\n NULL AS indexdef");
15541554
if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
15551555
appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1556-
" '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM "
1557-
" pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1556+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM "
1557+
" pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
15581558
else
15591559
appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions");
15601560
if (verbose)
@@ -1904,7 +1904,7 @@ describeOneTableDetails(const char *schemaname,
19041904
"\n a.attnum=d.refobjsubid)"
19051905
"\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
19061906
"\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1907-
"\n AND d.objid=%s"
1907+
"\n AND d.objid='%s'"
19081908
"\n AND d.deptype='a'",
19091909
oid);
19101910

@@ -2138,7 +2138,7 @@ describeOneTableDetails(const char *schemaname,
21382138
{
21392139
printfPQExpBuffer(&buf,
21402140
"SELECT pol.polname,\n"
2141-
"CASE WHEN pol.polroles = '{0}' THEN NULL ELSE array_to_string(array(select rolname from pg_roles where oid = any (pol.polroles) order by 1),',') END,\n"
2141+
"CASE WHEN pol.polroles = '{0}' THEN NULL ELSE pg_catalog.array_to_string(array(select rolname from pg_catalog.pg_roles where oid = any (pol.polroles) order by 1),',') END,\n"
21422142
"pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
21432143
"pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
21442144
"CASE pol.polcmd \n"
@@ -2498,13 +2498,13 @@ describeOneTableDetails(const char *schemaname,
24982498
/* Footer information about foreign table */
24992499
printfPQExpBuffer(&buf,
25002500
"SELECT s.srvname,\n"
2501-
" array_to_string(ARRAY(SELECT "
2502-
" quote_ident(option_name) || ' ' || "
2503-
" quote_literal(option_value) FROM "
2504-
" pg_options_to_table(ftoptions)), ', ') "
2501+
" pg_catalog.array_to_string(ARRAY(\n"
2502+
" SELECT pg_catalog.quote_ident(option_name)"
2503+
" || ' ' || pg_catalog.quote_literal(option_value)\n"
2504+
" FROM pg_catalog.pg_options_to_table(ftoptions)), ', ')\n"
25052505
"FROM pg_catalog.pg_foreign_table f,\n"
25062506
" pg_catalog.pg_foreign_server s\n"
2507-
"WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
2507+
"WHERE f.ftrelid = '%s' AND s.oid = f.ftserver;",
25082508
oid);
25092509
result = PSQLexec(buf.data);
25102510
if (!result)
@@ -2935,16 +2935,16 @@ listDbRoleSettings(const char *pattern, const char *pattern2)
29352935

29362936
printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
29372937
"pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
2938-
"FROM pg_db_role_setting AS s\n"
2939-
"LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
2940-
"LEFT JOIN pg_roles ON pg_roles.oid = setrole\n",
2938+
"FROM pg_catalog.pg_db_role_setting s\n"
2939+
"LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
2940+
"LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
29412941
gettext_noop("Role"),
29422942
gettext_noop("Database"),
29432943
gettext_noop("Settings"));
29442944
havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
2945-
NULL, "pg_roles.rolname", NULL, NULL);
2945+
NULL, "r.rolname", NULL, NULL);
29462946
processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
2947-
NULL, "pg_database.datname", NULL, NULL);
2947+
NULL, "d.datname", NULL, NULL);
29482948
appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
29492949
}
29502950
else
@@ -3173,13 +3173,13 @@ listLanguages(const char *pattern, bool verbose, bool showSystem)
31733173
{
31743174
appendPQExpBuffer(&buf,
31753175
",\n NOT l.lanispl AS \"%s\",\n"
3176-
" l.lanplcallfoid::regprocedure AS \"%s\",\n"
3177-
" l.lanvalidator::regprocedure AS \"%s\",\n ",
3176+
" l.lanplcallfoid::pg_catalog.regprocedure AS \"%s\",\n"
3177+
" l.lanvalidator::pg_catalog.regprocedure AS \"%s\",\n ",
31783178
gettext_noop("Internal Language"),
31793179
gettext_noop("Call Handler"),
31803180
gettext_noop("Validator"));
31813181
if (pset.sversion >= 90000)
3182-
appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n ",
3182+
appendPQExpBuffer(&buf, "l.laninline::pg_catalog.regprocedure AS \"%s\",\n ",
31833183
gettext_noop("Inline Handler"));
31843184
printACLColumn(&buf, "l.lanacl");
31853185
}
@@ -4304,10 +4304,10 @@ listForeignDataWrappers(const char *pattern, bool verbose)
43044304
printACLColumn(&buf, "fdwacl");
43054305
appendPQExpBuffer(&buf,
43064306
",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
4307-
" '(' || array_to_string(ARRAY(SELECT "
4308-
" quote_ident(option_name) || ' ' || "
4309-
" quote_literal(option_value) FROM "
4310-
" pg_options_to_table(fdwoptions)), ', ') || ')' "
4307+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4308+
" pg_catalog.quote_ident(option_name) || ' ' || "
4309+
" pg_catalog.quote_literal(option_value) FROM "
4310+
" pg_catalog.pg_options_to_table(fdwoptions)), ', ') || ')' "
43114311
" END AS \"%s\"",
43124312
gettext_noop("FDW Options"));
43134313

@@ -4385,10 +4385,10 @@ listForeignServers(const char *pattern, bool verbose)
43854385
" s.srvtype AS \"%s\",\n"
43864386
" s.srvversion AS \"%s\",\n"
43874387
" CASE WHEN srvoptions IS NULL THEN '' ELSE "
4388-
" '(' || array_to_string(ARRAY(SELECT "
4389-
" quote_ident(option_name) || ' ' || "
4390-
" quote_literal(option_value) FROM "
4391-
" pg_options_to_table(srvoptions)), ', ') || ')' "
4388+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4389+
" pg_catalog.quote_ident(option_name) || ' ' || "
4390+
" pg_catalog.quote_literal(option_value) FROM "
4391+
" pg_catalog.pg_options_to_table(srvoptions)), ', ') || ')' "
43924392
" END AS \"%s\",\n"
43934393
" d.description AS \"%s\"",
43944394
gettext_noop("Type"),
@@ -4403,7 +4403,7 @@ listForeignServers(const char *pattern, bool verbose)
44034403

44044404
if (verbose)
44054405
appendPQExpBufferStr(&buf,
4406-
"LEFT JOIN pg_description d\n "
4406+
"LEFT JOIN pg_catalog.pg_description d\n "
44074407
"ON d.classoid = s.tableoid AND d.objoid = s.oid "
44084408
"AND d.objsubid = 0\n");
44094409

@@ -4459,10 +4459,10 @@ listUserMappings(const char *pattern, bool verbose)
44594459
if (verbose)
44604460
appendPQExpBuffer(&buf,
44614461
",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
4462-
" '(' || array_to_string(ARRAY(SELECT "
4463-
" quote_ident(option_name) || ' ' || "
4464-
" quote_literal(option_value) FROM "
4465-
" pg_options_to_table(umoptions)), ', ') || ')' "
4462+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4463+
" pg_catalog.quote_ident(option_name) || ' ' || "
4464+
" pg_catalog.quote_literal(option_value) FROM "
4465+
" pg_catalog.pg_options_to_table(umoptions)), ', ') || ')' "
44664466
" END AS \"%s\"",
44674467
gettext_noop("FDW Options"));
44684468

@@ -4522,10 +4522,10 @@ listForeignTables(const char *pattern, bool verbose)
45224522
if (verbose)
45234523
appendPQExpBuffer(&buf,
45244524
",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
4525-
" '(' || array_to_string(ARRAY(SELECT "
4526-
" quote_ident(option_name) || ' ' || "
4527-
" quote_literal(option_value) FROM "
4528-
" pg_options_to_table(ftoptions)), ', ') || ')' "
4525+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4526+
" pg_catalog.quote_ident(option_name) || ' ' || "
4527+
" pg_catalog.quote_literal(option_value) FROM "
4528+
" pg_catalog.pg_options_to_table(ftoptions)), ', ') || ')' "
45294529
" END AS \"%s\",\n"
45304530
" d.description AS \"%s\"",
45314531
gettext_noop("FDW Options"),

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