Skip to content

Commit c33869c

Browse files
committed
psql \d: Display table where trigger is defined, if inherited
It's important to know that a trigger is cloned from a parent table, because of the behavior that the trigger is dropped on detach. Make psql's \d display it. We'd like to backpatch, but lack of the pg_trigger.tgparentid column makes it more difficult. Punt for now. If somebody wants to volunteer an implementation that reads pg_depend on older versions, that can probably be backpatched. Authors: Justin Pryzby, Amit Langote, Álvaro Herrera Discussion: https://postgr.es/m/20200419002206.GM26953@telsasoft.com
1 parent 27dbe1a commit c33869c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/bin/psql/describe.c

+16-2
Original file line numberDiff line numberDiff line change
@@ -2939,14 +2939,22 @@ describeOneTableDetails(const char *schemaname,
29392939
printfPQExpBuffer(&buf,
29402940
"SELECT t.tgname, "
29412941
"pg_catalog.pg_get_triggerdef(t.oid%s), "
2942-
"t.tgenabled, %s\n"
2942+
"t.tgenabled, %s, %s\n"
29432943
"FROM pg_catalog.pg_trigger t\n"
29442944
"WHERE t.tgrelid = '%s' AND ",
29452945
(pset.sversion >= 90000 ? ", true" : ""),
29462946
(pset.sversion >= 90000 ? "t.tgisinternal" :
29472947
pset.sversion >= 80300 ?
29482948
"t.tgconstraint <> 0 AS tgisinternal" :
2949-
"false AS tgisinternal"), oid);
2949+
"false AS tgisinternal"),
2950+
(pset.sversion >= 130000 ?
2951+
"(SELECT (NULLIF(a.relid, t.tgrelid))::pg_catalog.regclass"
2952+
" FROM pg_catalog.pg_trigger AS u, "
2953+
" pg_catalog.pg_partition_ancestors(t.tgrelid) AS a"
2954+
" WHERE u.tgname = t.tgname AND u.tgrelid = a.relid"
2955+
" AND u.tgparentid = 0) AS parent" :
2956+
"NULL AS parent"),
2957+
oid);
29502958
if (pset.sversion >= 110000)
29512959
appendPQExpBufferStr(&buf, "(NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D') \n"
29522960
" OR EXISTS (SELECT 1 FROM pg_catalog.pg_depend WHERE objid = t.oid \n"
@@ -3062,6 +3070,12 @@ describeOneTableDetails(const char *schemaname,
30623070
tgdef = usingpos + 9;
30633071

30643072
printfPQExpBuffer(&buf, " %s", tgdef);
3073+
3074+
/* Visually distinguish inherited triggers */
3075+
if (!PQgetisnull(result, i, 4))
3076+
appendPQExpBuffer(&buf, ", ON TABLE %s",
3077+
PQgetvalue(result, i, 4));
3078+
30653079
printTableAddFooter(&cont, buf.data);
30663080
}
30673081
}

src/test/regress/expected/triggers.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,7 @@ create trigger trg1 after insert on trigpart for each row execute procedure trig
20332033
b | integer | | |
20342034
Partition of: trigpart FOR VALUES FROM (2000) TO (3000)
20352035
Triggers:
2036-
trg1 AFTER INSERT ON trigpart3 FOR EACH ROW EXECUTE FUNCTION trigger_nothing()
2036+
trg1 AFTER INSERT ON trigpart3 FOR EACH ROW EXECUTE FUNCTION trigger_nothing(), ON TABLE trigpart
20372037

20382038
alter table trigpart detach partition trigpart3;
20392039
drop trigger trg1 on trigpart3; -- fail due to "does not exist"

0 commit comments

Comments
 (0)