Going back through all the feedback and comments, plus having some time to think things through, I am including a new patch, v7, that greatly simplifies things, and only makes changes inside of describe.c. In the spirit of not letting the perfect be the enemy of the good, I'm not worrying at all about the number of stars, or the width, and simply adding a small consistent description at the top of each query. I also realized that having these queries show up in someone's server log could be quite confusing, so I had them output as part of the query itself. In other words, they show up in both psql -E and the server logs. A few benefits to doing this:
* Simplifies the code
* Makes searching the web for what generated this code a lot easier (a
comment versus a giant blob of SQL)
* Makes all the SQL a little bit self-documented everywhere it shows up
* Easier to maintain describe.c, as the comment is always
printfPQExpBuffer, and everything
else is appendPQExpBuffer, rather than trying to figure out which to use
for each section of SQL.
Also removes bugs like the append-first in objectDescription()
Here's what the new output looks like via psql -E:
/******** QUERY *********/
/* Get matching aggregates */
SELECT n.nspname as "Schema",
p.proname AS "Name",
pg_catalog.format_type(p.prorettype, NULL) AS "Result data type",
CASE WHEN p.pronargs = 0
THEN CAST('*' AS pg_catalog.text)
ELSE pg_catalog.pg_get_function_arguments(p.oid)
END AS "Argument data types",
pg_catalog.obj_description(p.oid, 'pg_proc') as "Description"
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE p.prokind = 'a'
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND pg_catalog.pg_function_is_visible(p.oid)
ORDER BY 1, 2, 4;
/************************/
and more examples:
/******** QUERY *********/
/* Get publications that exclude this table */
SELECT pubname
FROM pg_catalog.pg_publication p
JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid
WHERE (pr.prrelid = '16403' OR pr.prrelid =
pg_catalog.pg_partition_root('16403'))
AND pr.prexcept
ORDER BY 1;
/************************/
/******** QUERY *********/
/* Get parent tables */
SELECT c.oid::pg_catalog.regclass
FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i
WHERE c.oid = i.inhparent AND i.inhrelid = '16403'
AND c.relkind != 'p' AND c.relkind != 'I'
ORDER BY inhseqno;
/************************/
Cheers,
Greg
0007-Add-comment-header-for-generated-SQL-inside-psql.patch
Description: Binary data
