8000 Clean up SQL emitted by psql/describe.c. · satanson/postgres@3095609 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3095609

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 0d4604a commit 3095609

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
@@ -1458,8 +1458,8 @@ describeOneTableDetails(const char *schemaname,
14581458
appendPQExpBufferStr(&buf, ",\n NULL AS indexdef");
14591459
if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
14601460
appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
1461-
" '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM "
1462-
" pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
1461+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM "
1462+
" pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
14631463
else
14641464
appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions");
14651465
if (verbose)
@@ -1809,7 +1809,7 @@ describeOneTableDetails(const char *schemaname,
18091809
"\n a.attnum=d.refobjsubid)"
18101810
"\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regcla 8000 ss"
18111811
"\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
1812-
"\n AND d.objid=%s"
1812+
"\n AND d.objid='%s'"
18131813
"\n AND d.deptype='a'",
18141814
oid);
18151815

@@ -2043,7 +2043,7 @@ describeOneTableDetails(const char *schemaname,
20432043
{
20442044
printfPQExpBuffer(&buf,
20452045
"SELECT pol.polname,\n"
2046-
"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"
2046+
"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"
20472047
"pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
20482048
"pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
20492049
"CASE pol.polcmd \n"
@@ -2403,13 +2403,13 @@ describeOneTableDetails(const char *schemaname,
24032403
/* Footer information about foreign table */
24042404
printfPQExpBuffer(&buf,
24052405
"SELECT s.srvname,\n"
2406-
" array_to_string(ARRAY(SELECT "
2407-
" quote_ident(option_name) || ' ' || "
2408-
" quote_literal(option_value) FROM "
2409-
" pg_options_to_table(ftoptions)), ', ') "
2406+
" pg_catalog.array_to_string(ARRAY(\n"
2407+
" SELECT pg_catalog.quote_ident(option_name)"
2408+
" || ' ' || pg_catalog.quote_literal(option_value)\n"
2409+
" FROM pg_catalog.pg_options_to_table(ftoptions)), ', ')\n"
24102410
"FROM pg_catalog.pg_foreign_table f,\n"
24112411
" pg_catalog.pg_foreign_server s\n"
2412-
"WHERE f.ftrelid = %s AND s.oid = f.ftserver;",
2412+
"WHERE f.ftrelid = '%s' AND s.oid = f.ftserver;",
24132413
oid);
24142414
result = PSQLexec(buf.data);
24152415
if (!result)
@@ -2837,16 +2837,16 @@ listDbRoleSettings(const char *pattern, const char *pattern2)
28372837

28382838
printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
28392839
"pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
2840-
"FROM pg_db_role_setting AS s\n"
2841-
"LEFT JOIN pg_database ON pg_database.oid = setdatabase\n"
2842-
"LEFT JOIN pg_roles ON pg_roles.oid = setrole\n",
2840+
"FROM pg_catalog.pg_db_role_setting s\n"
2841+
"LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
2842+
"LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
28432843
gettext_noop("Role"),
28442844
gettext_noop("Database"),
28452845
gettext_noop("Settings"));
28462846
havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
2847-
NULL, "pg_roles.rolname", NULL, NULL);
2847+
NULL, "r.rolname", NULL, NULL);
28482848
processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
2849-
NULL, "pg_database.datname", NULL, NULL);
2849+
NULL, "d.datname", NULL, NULL);
28502850
appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
28512851
}
28522852
else
@@ -3075,13 +3075,13 @@ listLanguages(const char *pattern, bool verbose, bool showSystem)
30753075
{
30763076
appendPQExpBuffer(&buf,
30773077
",\n NOT l.lanispl AS \"%s\",\n"
3078-
" l.lanplcallfoid::regprocedure AS \"%s\",\n"
3079-
" l.lanvalidator::regprocedure AS \"%s\",\n ",
3078+
" l.lanplcallfoid::pg_catalog.regprocedure AS \"%s\",\n"
3079+
" l.lanvalidator::pg_catalog.regprocedure AS \"%s\",\n ",
30803080
gettext_noop("Internal Language"),
30813081
gettext_noop("Call Handler"),
30823082
gettext_noop("Validator"));
30833083
if (pset.sversion >= 90000)
3084-
appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n ",
3084+
appendPQExpBuffer(&buf, "l.laninline::pg_catalog.regprocedure AS \"%s\",\n ",
30853085
gettext_noop("Inline Handler"));
30863086
printACLColumn(&buf, "l.lanacl");
30873087
}
@@ -4206,10 +4206,10 @@ listForeignDataWrappers(const char *pattern, bool verbose)
42064206
printACLColumn(&buf, "fdwacl");
42074207
appendPQExpBuffer(&buf,
42084208
",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
4209-
" '(' || array_to_string(ARRAY(SELECT "
4210-
" quote_ident(option_name) || ' ' || "
4211-
" quote_literal(option_value) FROM "
4212-
" pg_options_to_table(fdwoptions)), ', ') || ')' "
4209+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4210+
" pg_catalog.quote_ident(option_name) || ' ' || "
4211+
" pg_catalog.quote_literal(option_value) FROM "
4212+
" pg_catalog.pg_options_to_table(fdwoptions)), ', ') || ')' "
42134213
" END AS \"%s\"",
42144214
gettext_noop("FDW Options"));
42154215

@@ -4287,10 +4287,10 @@ listForeignServers(const char *pattern, bool verbose)
42874287
" s.srvtype AS \"%s\",\n"
42884288
" s.srvversion AS \"%s\",\n"
42894289
" CASE WHEN srvoptions IS NULL THEN '' ELSE "
4290-
" '(' || array_to_string(ARRAY(SELECT "
4291-
" quote_ident(option_name) || ' ' || "
4292-
" quote_literal(option_value) FROM "
4293-
" pg_options_to_table(srvoptions)), ', ') || ')' "
4290+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4291+
" pg_catalog.quote_ident(option_name) || ' ' || "
4292+
" pg_catalog.quote_literal(option_value) FROM "
4293+
" pg_catalog.pg_options_to_table(srvoptions)), ', ') || ')' "
42944294
" END AS \"%s\",\n"
42954295
" d.description AS \"%s\"",
42964296
gettext_noop("Type"),
@@ -4305,7 +4305,7 @@ listForeignServers(const char *pattern, bool verbose)
43054305

43064306
if (verbose)
43074307
appendPQExpBufferStr(&buf,
4308-
"LEFT JOIN pg_description d\n "
4308+
"LEFT JOIN pg_catalog.pg_description d\n "
43094309
"ON d.classoid = s.tableoid AND d.objoid = s.oid "
43104310
"AND d.objsubid = 0\n");
43114311

@@ -4361,10 +4361,10 @@ listUserMappings(const char *pattern, bool verbose)
43614361
if (verbose)
43624362
appendPQExpBuffer(&buf,
43634363
",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
4364-
" '(' || array_to_string(ARRAY(SELECT "
4365-
" quote_ident(option_name) || ' ' || "
4366-
" quote_literal(option_value) FROM "
4367-
" pg_options_to_table(umoptions)), ', ') || ')' "
4364+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4365+
" pg_catalog.quote_ident(option_name) || ' ' || "
4366+
" pg_catalog.quote_literal(option_value) FROM "
4367+
" pg_catalog.pg_options_to_table(umoptions)), ', ') || ')' "
43684368
" END AS \"%s\"",
43694369
gettext_noop("FDW Options"));
43704370

@@ -4424,10 +4424,10 @@ listForeignTables(const char *pattern, bool verbose)
44244424
if (verbose)
44254425
appendPQExpBuffer(&buf,
44264426
",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
4427-
" '(' || array_to_string(ARRAY(SELECT "
4428-
" quote_ident(option_name) || ' ' || "
4429-
" quote_literal(option_value) FROM "
4430-
" pg_options_to_table(ftoptions)), ', ') || ')' "
4427+
" '(' || pg_catalog.array_to_string(ARRAY(SELECT "
4428+
" pg_catalog.quote_ident(option_name) || ' ' || "
4429+
" pg_catalog.quote_literal(option_value) FROM "
4430+
" pg_catalog.pg_options_to_table(ftoptions)), ', ') || ')' "
44314431
" END AS \"%s\",\n"
44324432
" d.description AS \"%s\"",
44334433
gettext_noop("FDW Options"),

0 commit comments

Comments
 (0)
0