From 4712253f66e03f6666d57a7a7a971a9f00c492c6 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Mon, 20 Apr 2020 13:46:06 -0500
Subject: [PATCH v6 2/2] show inherited triggers Amit's way..

..this also updates the query to avoid saying things like:
TABLE trigpart .* INSERT ON trigpart3
---
 src/bin/psql/describe.c                | 42 +++++++++++++++++---------
 src/test/regress/expected/triggers.out |  2 +-
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 5ef56f7a9d..3b02eab84e 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -2936,10 +2936,27 @@ describeOneTableDetails(const char *schemaname,
 		PGresult   *result;
 		int			tuples;
 
-		printfPQExpBuffer(&buf,
+		if (pset.sversion >= 130000)
+		{
+			printfPQExpBuffer(&buf,
+						  "SELECT t.tgname, "
+						  "pg_catalog.pg_get_triggerdef(COALESCE(u.oid, t.oid), true), "
+						  "t.tgenabled, t.tgisinternal, a.relid AS othertable\n"
+						  "FROM pg_catalog.pg_trigger t LEFT JOIN\n"
+						  "pg_catalog.pg_partition_ancestors(t.tgrelid) AS a\n"
+						  "ON true LEFT JOIN\n"
+						  "pg_catalog.pg_trigger AS u ON\n"
+						  "u.tgname = t.tgname AND u.tgrelid = a.relid\n"
+						  "WHERE t.tgrelid = '%s' AND "
+						  "(u.tgparentid = 0 OR a.relid IS NULL) AND \n",
+						  oid);
+		}
+		else
+		{
+			printfPQExpBuffer(&buf,
 						  "SELECT t.tgname, "
 						  "pg_catalog.pg_get_triggerdef(t.oid%s), "
-						  "t.tgenabled, %s, %s\n"
+						  "t.tgenabled, %s, NULL AS parent\n"
 						  "FROM pg_catalog.pg_trigger t\n"
 						  "WHERE t.tgrelid = '%s' AND ",
 						  (pset.sversion >= 90000 ? ", true" : ""),
@@ -2947,14 +2964,9 @@ describeOneTableDetails(const char *schemaname,
 						   pset.sversion >= 80300 ?
 						   "t.tgconstraint <> 0 AS tgisinternal" :
 						   "false AS tgisinternal"),
-						  (pset.sversion >= 130000 ? "\n"
-						   "  (SELECT (NULLIF(a.relid, t.tgrelid))::pg_catalog.regclass\n"
-						   "   FROM pg_catalog.pg_trigger AS u,\n"
-						   "   pg_catalog.pg_partition_ancestors(t.tgrelid) AS a\n"
-						   "   WHERE u.tgname = t.tgname AND u.tgrelid = a.relid\n"
-						   "        AND u.tgparentid = 0) AS parent" :
-						   "NULL AS parent"),
 						  oid);
+		}
+
 		if (pset.sversion >= 110000)
 			appendPQExpBufferStr(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D') \n"
 								 "    OR EXISTS (SELECT 1 FROM pg_catalog.pg_depend WHERE objid = t.oid \n"
@@ -3069,12 +3081,12 @@ describeOneTableDetails(const char *schemaname,
 					if (usingpos)
 						tgdef = usingpos + 9;
 
-					printfPQExpBuffer(&buf, "    %s", tgdef);
-
-					/* Visually distinguish inherited triggers */
-					if (!PQgetisnull(result, i, 4))
-						appendPQExpBuffer(&buf, ", ON TABLE \"%s\"",
-								PQgetvalue(result, i, 4));
+					if (PQgetisnull(result, i, 4))
+						printfPQExpBuffer(&buf, "    %s", tgdef);
+					else
+						/* Visually distinguish inherited triggers */
+						printfPQExpBuffer(&buf, "    TABLE \"%s\" TRIGGER %s",
+								PQgetvalue(result, i, 4), tgdef);
 
 					printTableAddFooter(&cont, buf.data);
 				}
diff --git a/src/test/regress/expected/triggers.out b/src/test/regress/expected/triggers.out
index 4104711c29..a1b81f49ff 100644
--- a/src/test/regress/expected/triggers.out
+++ b/src/test/regress/expected/triggers.out
@@ -2033,7 +2033,7 @@ create trigger trg1 after insert on trigpart for each row execute procedure trig
  b      | integer |           |          | 
 Partition of: trigpart FOR VALUES FROM (2000) TO (3000)
 Triggers:
-    trg1 AFTER INSERT ON trigpart3 FOR EACH ROW EXECUTE FUNCTION trigger_nothing(), ON TABLE trigpart
+    TABLE "trigpart" TRIGGER trg1 AFTER INSERT ON trigpart3 FOR EACH ROW EXECUTE FUNCTION trigger_nothing()
 
 alter table trigpart detach partition trigpart3;
 drop trigger trg1 on trigpart3; -- fail due to "does not exist"
-- 
2.17.0

