Re: Adding comments to help understand psql hidden queries

From: Greg Sabino Mullane <htamfids(at)gmail(dot)com>
To: Maiquel Grassi <grassi(at)hotmail(dot)com(dot)br>
Cc: David Christensen <david+pg(at)pgguru(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Peter Eisentraut <peter(at)eisentraut(dot)org>, Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Adding comments to help understand psql hidden queries
Date: 2026-03-22 15:15:38
Message-ID: CAKAnmmJfa1JsBp8pUYjGXfnHg70f0CynHctin8P8d-Og0pphYA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
0007-Add-comment-header-for-generated-SQL-inside-psql.patch application/octet-stream 34.7 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Lukas Fittl 2026-03-22 15:23:49 Re: Avoid use of TopMemoryContext for resource owner cleanup in portals
Previous Message Evgeny Voropaev 2026-03-22 14:39:39 Re: Compress prune/freeze records with Delta Frame of Reference algorithm