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

Commit cc126b4

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 ee63709 commit cc126b4

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/backend/commands/tablecmds.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8008,10 +8008,12 @@ CloneFkReferencing(Relation pg_constraint, Relation parentRel,
80088008
ReleaseSysCache(partcontup);
80098009

80108010
/*
8011-
* Looks good! Attach this constraint. Note that the action
8012-
* triggers are no longer needed, so remove them. We identify
8013-
* them because they have our constraint OID, as well as being
8014-
* on the referenced rel.
8011+
* Looks good! Attach this constraint. The action triggers in
8012+
* the new partition become redundant -- the parent table already
8013+
* has equivalent ones, and those will be able to reach the
8014+
* partition. Remove the ones in the partition. We identify them
8015+
* because they have our constraint OID, as well as being on the
8016+
* referenced rel.
80158017
*/
80168018
trigrel = heap_open(TriggerRelationId, RowExclusiveLock);
80178019
ScanKeyInit(&key,
@@ -8024,17 +8026,30 @@ CloneFkReferencing(Relation pg_constraint, Relation parentRel,
80248026
while ((trigtup = systable_getnext(scan)) != NULL)
80258027
{
80268028
Form_pg_trigger trgform = (Form_pg_trigger) GETSTRUCT(trigtup);
8029+
ObjectAddress trigger;
80278030

80288031
if (trgform->tgconstrrelid != fk->conrelid)
80298032
continue;
80308033
if (trgform->tgrelid != fk->confrelid)
80318034
continue;
80328035

8033-
deleteDependencyRecordsForClass(TriggerRelationId,
8034-
HeapTupleGetOid(trigtup),
8035-
ConstraintRelationId,
8036-
DEPENDENCY_INTERNAL);
8037-
CatalogTupleDelete(trigrel, &trigtup->t_self);
8036+
/*
8037+
* The constraint is originally set up to contain this trigger
8038+
* as an implementation object, so there's a dependency record
8039+
* that links the two; however, since the trigger is no longer
8040+
* needed, we remove the dependency link in order to be able
8041+
* to drop the trigger while keeping the constraint intact.
8042+
*/
8043+
deleteDependencyRecordsFor(TriggerRelationId,
8044+
HeapTupleGetOid(trigtup),
8045+
false);
8046+
/* make dependency deletion visible to performDeletion */
8047+
CommandCounterIncrement();
8048+
ObjectAddressSet(trigger, TriggerRelationId,
8049+
HeapTupleGetOid(trigtup));
8050+
performDeletion(&trigger, DROP_RESTRICT, 0);
8051+
/* make trigger drop visible, in case the loop iterates */
8052+
CommandCounterIncrement();
80388053
}
80398054

80408055
systable_endscan(scan);

0 commit comments

Comments
 (0)