Fix detaching partitions with cloned row triggers
authorAlvaro Herrera
Tue, 21 Apr 2020 17:57:00 +0000 (13:57 -0400)
committerAlvaro Herrera
Tue, 21 Apr 2020 17:57:00 +0000 (13:57 -0400)
commitafccd76f1ccef73a341e9b0c6efb29a429f35aa4
tree80984fd38d001ed3dc05e9247b1c9e605c6e8653
parent1542e16f2ca257656717ab8ea263bc310f1b6d51
Fix detaching partitions with cloned row triggers

When a partition is detached, any triggers that had been cloned from its
parent were not properly disentangled from its parent triggers.
This resulted in triggers that could not be dropped because they
depended on the trigger in the trigger in the no-longer-parent table:
  ALTER TABLE t DETACH PARTITION t1;
  DROP TRIGGER trig ON t1;
    ERROR:  cannot drop trigger trig on table t1 because trigger trig on table t requires it
    HINT:  You can drop trigger trig on table t instead.

Moreover the table can no longer be re-attached to its parent, because
the trigger name is already taken:
  ALTER TABLE t ATTACH PARTITION t1 FOR VALUES FROM (1)TO(2);
    ERROR:  trigger "trig" for relation "t1" already exists

The former is a bug introduced in commit 86f575948c77.  (The latter is
not necessarily a bug, but it makes the bug more uncomfortable.)

To avoid the complexity that would be needed to tell whether the trigger
has a local definition that has to be merged with the one coming from
the parent table, establish the behavior that the trigger is removed
when the table is detached.

Backpatch to pg11.

Author: Justin Pryzby 
Reviewed-by: Amit Langote
Reviewed-by: Álvaro Herrera
Discussion: https://www.postgresql.org/message-id/flat/20200408152412[email protected]
doc/src/sgml/ref/create_trigger.sgml
src/backend/commands/tablecmds.c
src/test/regress/expected/triggers.out
src/test/regress/sql/triggers.sql