1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-05 09:19:17 +03:00

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.
This commit is contained in:
Tom Lane 2017-07-26 19:35:35 -04:00
parent 8c348765f9
commit a2fc3431c3

View File

@ -1553,8 +1553,8 @@ describeOneTableDetails(const char *schemaname,
appendPQExpBufferStr(&buf, ",\n NULL AS indexdef"); appendPQExpBufferStr(&buf, ",\n NULL AS indexdef");
if (tableinfo.relkind == 'f' && pset.sversion >= 90200) if (tableinfo.relkind == 'f' && pset.sversion >= 90200)
appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE " appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE "
" '(' || array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM " " '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM "
" pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions"); " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions");
else else
appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions"); appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions");
if (verbose) if (verbose)
@ -1904,7 +1904,7 @@ describeOneTableDetails(const char *schemaname,
"\n a.attnum=d.refobjsubid)" "\n a.attnum=d.refobjsubid)"
"\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass" "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass"
"\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass" "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass"
"\n AND d.objid=%s" "\n AND d.objid='%s'"
"\n AND d.deptype='a'", "\n AND d.deptype='a'",
oid); oid);
@ -2138,7 +2138,7 @@ describeOneTableDetails(const char *schemaname,
{ {
printfPQExpBuffer(&buf, printfPQExpBuffer(&buf,
"SELECT pol.polname,\n" "SELECT pol.polname,\n"
"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" "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"
"pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n" "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid),\n"
"pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n" "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid),\n"
"CASE pol.polcmd \n" "CASE pol.polcmd \n"
@ -2498,13 +2498,13 @@ describeOneTableDetails(const char *schemaname,
/* Footer information about foreign table */ /* Footer information about foreign table */
printfPQExpBuffer(&buf, printfPQExpBuffer(&buf,
"SELECT s.srvname,\n" "SELECT s.srvname,\n"
" array_to_string(ARRAY(SELECT " " pg_catalog.array_to_string(ARRAY(\n"
" quote_ident(option_name) || ' ' || " " SELECT pg_catalog.quote_ident(option_name)"
" quote_literal(option_value) FROM " " || ' ' || pg_catalog.quote_literal(option_value)\n"
" pg_options_to_table(ftoptions)), ', ') " " FROM pg_catalog.pg_options_to_table(ftoptions)), ', ')\n"
"FROM pg_catalog.pg_foreign_table f,\n" "FROM pg_catalog.pg_foreign_table f,\n"
" pg_catalog.pg_foreign_server s\n" " pg_catalog.pg_foreign_server s\n"
"WHERE f.ftrelid = %s AND s.oid = f.ftserver;", "WHERE f.ftrelid = '%s' AND s.oid = f.ftserver;",
oid); oid);
result = PSQLexec(buf.data); result = PSQLexec(buf.data);
if (!result) if (!result)
@ -2935,16 +2935,16 @@ listDbRoleSettings(const char *pattern, const char *pattern2)
printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n" printfPQExpBuffer(&buf, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"
"pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n" "pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"
"FROM pg_db_role_setting AS s\n" "FROM pg_catalog.pg_db_role_setting s\n"
"LEFT JOIN pg_database ON pg_database.oid = setdatabase\n" "LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"
"LEFT JOIN pg_roles ON pg_roles.oid = setrole\n", "LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
gettext_noop("Role"), gettext_noop("Role"),
gettext_noop("Database"), gettext_noop("Database"),
gettext_noop("Settings")); gettext_noop("Settings"));
havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false, havewhere = processSQLNamePattern(pset.db, &buf, pattern, false, false,
NULL, "pg_roles.rolname", NULL, NULL); NULL, "r.rolname", NULL, NULL);
processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false, processSQLNamePattern(pset.db, &buf, pattern2, havewhere, false,
NULL, "pg_database.datname", NULL, NULL); NULL, "d.datname", NULL, NULL);
appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;");
} }
else else
@ -3173,13 +3173,13 @@ listLanguages(const char *pattern, bool verbose, bool showSystem)
{ {
appendPQExpBuffer(&buf, appendPQExpBuffer(&buf,
",\n NOT l.lanispl AS \"%s\",\n" ",\n NOT l.lanispl AS \"%s\",\n"
" l.lanplcallfoid::regprocedure AS \"%s\",\n" " l.lanplcallfoid::pg_catalog.regprocedure AS \"%s\",\n"
" l.lanvalidator::regprocedure AS \"%s\",\n ", " l.lanvalidator::pg_catalog.regprocedure AS \"%s\",\n ",
gettext_noop("Internal Language"), gettext_noop("Internal Language"),
gettext_noop("Call Handler"), gettext_noop("Call Handler"),
gettext_noop("Validator")); gettext_noop("Validator"));
if (pset.sversion >= 90000) if (pset.sversion >= 90000)
appendPQExpBuffer(&buf, "l.laninline::regprocedure AS \"%s\",\n ", appendPQExpBuffer(&buf, "l.laninline::pg_catalog.regprocedure AS \"%s\",\n ",
gettext_noop("Inline Handler")); gettext_noop("Inline Handler"));
printACLColumn(&buf, "l.lanacl"); printACLColumn(&buf, "l.lanacl");
} }
@ -4304,10 +4304,10 @@ listForeignDataWrappers(const char *pattern, bool verbose)
printACLColumn(&buf, "fdwacl"); printACLColumn(&buf, "fdwacl");
appendPQExpBuffer(&buf, appendPQExpBuffer(&buf,
",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE " ",\n CASE WHEN fdwoptions IS NULL THEN '' ELSE "
" '(' || array_to_string(ARRAY(SELECT " " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
" quote_ident(option_name) || ' ' || " " pg_catalog.quote_ident(option_name) || ' ' || "
" quote_literal(option_value) FROM " " pg_catalog.quote_literal(option_value) FROM "
" pg_options_to_table(fdwoptions)), ', ') || ')' " " pg_catalog.pg_options_to_table(fdwoptions)), ', ') || ')' "
" END AS \"%s\"", " END AS \"%s\"",
gettext_noop("FDW Options")); gettext_noop("FDW Options"));
@ -4385,10 +4385,10 @@ listForeignServers(const char *pattern, bool verbose)
" s.srvtype AS \"%s\",\n" " s.srvtype AS \"%s\",\n"
" s.srvversion AS \"%s\",\n" " s.srvversion AS \"%s\",\n"
" CASE WHEN srvoptions IS NULL THEN '' ELSE " " CASE WHEN srvoptions IS NULL THEN '' ELSE "
" '(' || array_to_string(ARRAY(SELECT " " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
" quote_ident(option_name) || ' ' || " " pg_catalog.quote_ident(option_name) || ' ' || "
" quote_literal(option_value) FROM " " pg_catalog.quote_literal(option_value) FROM "
" pg_options_to_table(srvoptions)), ', ') || ')' " " pg_catalog.pg_options_to_table(srvoptions)), ', ') || ')' "
" END AS \"%s\",\n" " END AS \"%s\",\n"
" d.description AS \"%s\"", " d.description AS \"%s\"",
gettext_noop("Type"), gettext_noop("Type"),
@ -4403,7 +4403,7 @@ listForeignServers(const char *pattern, bool verbose)
if (verbose) if (verbose)
appendPQExpBufferStr(&buf, appendPQExpBufferStr(&buf,
"LEFT JOIN pg_description d\n " "LEFT JOIN pg_catalog.pg_description d\n "
"ON d.classoid = s.tableoid AND d.objoid = s.oid " "ON d.classoid = s.tableoid AND d.objoid = s.oid "
"AND d.objsubid = 0\n"); "AND d.objsubid = 0\n");
@ -4459,10 +4459,10 @@ listUserMappings(const char *pattern, bool verbose)
if (verbose) if (verbose)
appendPQExpBuffer(&buf, appendPQExpBuffer(&buf,
",\n CASE WHEN umoptions IS NULL THEN '' ELSE " ",\n CASE WHEN umoptions IS NULL THEN '' ELSE "
" '(' || array_to_string(ARRAY(SELECT " " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
" quote_ident(option_name) || ' ' || " " pg_catalog.quote_ident(option_name) || ' ' || "
" quote_literal(option_value) FROM " " pg_catalog.quote_literal(option_value) FROM "
" pg_options_to_table(umoptions)), ', ') || ')' " " pg_catalog.pg_options_to_table(umoptions)), ', ') || ')' "
" END AS \"%s\"", " END AS \"%s\"",
gettext_noop("FDW Options")); gettext_noop("FDW Options"));
@ -4522,10 +4522,10 @@ listForeignTables(const char *pattern, bool verbose)
if (verbose) if (verbose)
appendPQExpBuffer(&buf, appendPQExpBuffer(&buf,
",\n CASE WHEN ftoptions IS NULL THEN '' ELSE " ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE "
" '(' || array_to_string(ARRAY(SELECT " " '(' || pg_catalog.array_to_string(ARRAY(SELECT "
" quote_ident(option_name) || ' ' || " " pg_catalog.quote_ident(option_name) || ' ' || "
" quote_literal(option_value) FROM " " pg_catalog.quote_literal(option_value) FROM "
" pg_options_to_table(ftoptions)), ', ') || ')' " " pg_catalog.pg_options_to_table(ftoptions)), ', ') || ')' "
" END AS \"%s\",\n" " END AS \"%s\",\n"
" d.description AS \"%s\"", " d.description AS \"%s\"",
gettext_noop("FDW Options"), gettext_noop("FDW Options"),