aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/describe.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/psql/describe.c')
-rw-r--r--src/bin/psql/describe.c38
1 files changed, 30 insertions, 8 deletions
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 8587b19160d..40433e32fa0 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -2990,16 +2990,38 @@ describeOneTableDetails(const char *schemaname,
printfPQExpBuffer(&buf,
"SELECT t.tgname, "
"pg_catalog.pg_get_triggerdef(t.oid, true), "
- "t.tgenabled, t.tgisinternal, %s\n"
+ "t.tgenabled, t.tgisinternal,\n");
+
+ /*
+ * Detect whether each trigger is inherited, and if so, get the name
+ * of the topmost table it's inherited from. We have no easy way to
+ * do that pre-v13, for lack of the tgparentid column. Even with
+ * tgparentid, a straightforward search for the topmost parent would
+ * require a recursive CTE, which seems unduly expensive. We cheat a
+ * bit by assuming parent triggers will match by tgname; then, joining
+ * with pg_partition_ancestors() allows the planner to make use of
+ * pg_trigger_tgrelid_tgname_index if it wishes. We ensure we find
+ * the correct topmost parent by stopping at the first-in-partition-
+ * ancestry-order trigger that has tgparentid = 0. (There might be
+ * unrelated, non-inherited triggers with the same name further up the
+ * stack, so this is important.)
+ */
+ if (pset.sversion >= 130000)
+ appendPQExpBufferStr(&buf,
+ " CASE WHEN t.tgparentid != 0 THEN\n"
+ " (SELECT u.tgrelid::pg_catalog.regclass\n"
+ " FROM pg_catalog.pg_trigger AS u,\n"
+ " pg_catalog.pg_partition_ancestors(t.tgrelid) WITH ORDINALITY AS a(relid, depth)\n"
+ " WHERE u.tgname = t.tgname AND u.tgrelid = a.relid\n"
+ " AND u.tgparentid = 0\n"
+ " ORDER BY a.depth LIMIT 1)\n"
+ " END AS parent\n");
+ else
+ appendPQExpBufferStr(&buf, " NULL AS parent\n");
+
+ appendPQExpBuffer(&buf,
"FROM pg_catalog.pg_trigger t\n"
"WHERE t.tgrelid = '%s' AND ",
- (pset.sversion >= 130000 ?
- "(SELECT (NULLIF(a.relid, t.tgrelid))::pg_catalog.regclass"
- " FROM pg_catalog.pg_trigger AS u, "
- " pg_catalog.pg_partition_ancestors(t.tgrelid) AS a"
- " WHERE u.tgname = t.tgname AND u.tgrelid = a.relid"
- " AND u.tgparentid = 0) AS parent" :
- "NULL AS parent"),
oid);
/*