Don't throw an error if a queued AFTER trigger no longer exists.
authorTom Lane <[email protected]>
Thu, 20 Jun 2024 18:21:36 +0000 (14:21 -0400)
committerTom Lane <[email protected]>
Thu, 20 Jun 2024 18:21:36 +0000 (14:21 -0400)
afterTriggerInvokeEvents and AfterTriggerExecute have always
treated it as an error if the trigger OID mentioned in a queued
after-trigger event can't be found.  However, that fails to
account for the edge case where the trigger's been dropped in
the current transaction since queueing the event.  There seems
no very good reason to disallow that case, so instead silently
do nothing if the trigger OID can't be found.

This does give up a little bit of bug-detection ability, but I don't
recall that these error messages have ever actually revealed a bug,
so it seems mostly theoretical.  Alternatives such as marking
pending events DONE at the time of dropping a trigger would be
complicated and perhaps introduce bugs of their own.

Per bug #18517 from Alexander Lakhin.  Back-patch to all
supported branches.

Discussion: https://postgr.es/m/18517-af2d19882240902c@postgresql.org

src/backend/commands/trigger.c
src/test/regress/expected/triggers.out
src/test/regress/sql/triggers.sql

index 7362e99fa5054dd47b92ef2483bf0a80458cd17c..c852b137f06682401a9a79dc4816b77a357441d6 100644 (file)
@@ -3997,8 +3997,12 @@ AfterTriggerExecute(EState *estate,
    bool        should_free_new = false;
 
    /*
-    * Locate trigger in trigdesc.
+    * Locate trigger in trigdesc.  It might not be present, and in fact the
+    * trigdesc could be NULL, if the trigger was dropped since the event was
+    * queued.  In that case, silently do nothing.
     */
+   if (trigdesc == NULL)
+       return;
    for (tgindx = 0; tgindx < trigdesc->numtriggers; tgindx++)
    {
        if (trigdesc->triggers[tgindx].tgoid == tgoid)
@@ -4008,7 +4012,7 @@ AfterTriggerExecute(EState *estate,
        }
    }
    if (LocTriggerData.tg_trigger == NULL)
-       elog(ERROR, "could not find trigger %u", tgoid);
+       return;
 
    /*
     * If doing EXPLAIN ANALYZE, start charging time to this trigger. We want
@@ -4332,6 +4336,7 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
                    /* Catch calls with insufficient relcache refcounting */
                    Assert(!RelationHasReferenceCountZero(rel));
                    trigdesc = rInfo->ri_TrigDesc;
+                   /* caution: trigdesc could be NULL here */
                    finfo = rInfo->ri_TrigFunctions;
                    instr = rInfo->ri_TrigInstrument;
                    if (slot1 != NULL)
@@ -4347,9 +4352,6 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
                        slot2 = MakeSingleTupleTableSlot(rel->rd_att,
                                                         &TTSOpsMinimalTuple);
                    }
-                   if (trigdesc == NULL)   /* should not happen */
-                       elog(ERROR, "relation %u has no triggers",
-                            evtshared->ats_relid);
                }
 
                /*
index 45562bd35c4fa55cc999b0ae1c8afa8a19df878d..8a203a34ed18b1486e11ccf3bf17611afbe36a1c 100644 (file)
@@ -3286,6 +3286,17 @@ select * from trig_table;
 
 drop table refd_table, trig_table;
 --
+-- Test that we can drop a not-yet-fired deferred trigger
+--
+create table refd_table (id int primary key);
+create table trig_table (fk int references refd_table initially deferred);
+begin;
+insert into trig_table values (1);
+drop table refd_table cascade;
+NOTICE:  drop cascades to constraint trig_table_fk_fkey on table trig_table
+commit;
+drop table trig_table;
+--
 -- self-referential FKs are even more fun
 --
 create table self_ref (a int primary key,
index a1aa52521e74c9424354c15769e9313f6a9d9169..c841f8f17c5c8a74d8ec10a04c3fee9dd19d4069 100644 (file)
@@ -2406,6 +2406,20 @@ select * from trig_table;
 
 drop table refd_table, trig_table;
 
+--
+-- Test that we can drop a not-yet-fired deferred trigger
+--
+
+create table refd_table (id int primary key);
+create table trig_table (fk int references refd_table initially deferred);
+
+begin;
+insert into trig_table values (1);
+drop table refd_table cascade;
+commit;
+
+drop table trig_table;
+
 --
 -- self-referential FKs are even more fun
 --