Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cb90de1

Browse files
committed
Fix trigger drop procedure
After commit 123cc69, we remove redundant FK action triggers during partition ATTACH by merely deleting the catalog tuple, but that's wrong: it should use performDeletion() instead. Repair, and make the comments more explicit. Per code review from Tom Lane. Discussion: https://postgr.es/m/18885.1549642539@sss.pgh.pa.us
1 parent 068503c commit cb90de1

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/backend/commands/tablecmds.c

+24-9
Original file line numberDiff line numberDiff line change
@@ -7919,10 +7919,12 @@ CloneFkReferencing(Relation pg_constraint, Relation parentRel,
79197919
ReleaseSysCache(partcontup);
79207920

79217921
/*
7922-
* Looks good! Attach this constraint. Note that the action
7923-
* triggers are no longer needed, so remove them. We identify
7924-
* them because they have our constraint OID, as well as being
7925-
* on the referenced rel.
7922+
* Looks good! Attach this constraint. The action triggers in
7923+
* the new partition become redundant -- the parent table already
7924+
* has equivalent ones, and those will be able to reach the
7925+
* partition. Remove the ones in the partition. We identify them
7926+
* because they have our constraint OID, as well as being on the
7927+
* referenced rel.
79267928
*/
79277929
trigrel = heap_open(TriggerRelationId, RowExclusiveLock);
79287930
ScanKeyInit(&key,
@@ -7935,17 +7937,30 @@ CloneFkReferencing(Relation pg_constraint, Relation parentRel,
79357937
while ((trigtup = systable_getnext(scan)) != NULL)
79367938
{
79377939
Form_pg_trigger trgform = (Form_pg_trigger) GETSTRUCT(trigtup);
7940+
ObjectAddress trigger;
79387941

79397942
if (trgform->tgconstrrelid != fk->conrelid)
79407943
continue;
79417944
if (trgform->tgrelid != fk->confrelid)
79427945
continue;
79437946

7944-
deleteDependencyRecordsForClass(TriggerRelationId,
7945-
trgform->oid,
7946-
ConstraintRelationId,
7947-
DEPENDENCY_INTERNAL);
7948-
CatalogTupleDelete(trigrel, &trigtup->t_self);
7947+
/*
7948+
* The constraint is originally set up to contain this trigger
7949+
* as an implementation object, so there's a dependency record
7950+
* that links the two; however, since the trigger is no longer
7951+
* needed, we remove the dependency link in order to be able
7952+
* to drop the trigger while keeping the constraint intact.
7953+
*/
7954+
deleteDependencyRecordsFor(TriggerRelationId,
7955+
trgform->oid,
7956+
false);
7957+
/* make dependency deletion visible to performDeletion */
7958+
CommandCounterIncrement();
7959+
ObjectAddressSet(trigger, TriggerRelationId,
7960+
trgform->oid);
7961+
performDeletion(&trigger, DROP_RESTRICT, 0);
7962+
/* make trigger drop visible, in case the loop iterates */
7963+
CommandCounterIncrement();
79497964
}
79507965

79517966
systable_endscan(scan);

0 commit comments

Comments
 (0)