Fix psql \d's query for identifying parent triggers.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 18 Jan 2022 02:18:49 +0000 (21:18 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 18 Jan 2022 02:18:49 +0000 (21:18 -0500)
commit90e0f9fd8cae7cfb09c11534f690edde6d6ee06c
tree53d5488695dc3d07092cd559a8076b1bf12af540
parentd18ec312f9f36c220aa1a1497d3173729c862d66
Fix psql \d's query for identifying parent triggers.

The original coding (from c33869cc3) failed with "more than one row
returned by a subquery used as an expression" if there were unrelated
triggers of the same tgname on parent partitioned tables.  (That's
possible because statement-level triggers don't get inherited.)  Fix
by applying LIMIT 1 after sorting the candidates by inheritance level.

Also, wrap the subquery in a CASE so that we don't have to execute it at
all when the trigger is visibly non-inherited.  Aside from saving some
cycles, this avoids the need for a confusing and undocumented NULLIF().

While here, tweak the format of the emitted query to look a bit
nicer for "psql -E", and add some explanation of this subquery,
because it badly needs it.

Report and patch by Justin Pryzby (with some editing by me).
Back-patch to v13 where the faulty code came in.

Discussion: https://postgr.es/m/20211217154356.GJ17618@telsasoft.com
src/bin/psql/describe.c
src/test/regress/expected/triggers.out
src/test/regress/sql/triggers.sql