Skip to content

Commit 731d514

Browse files
alvherreamitlan
andcommitted
Fix ENABLE/DISABLE TRIGGER to handle recursion correctly
Using ATSimpleRecursion() in ATPrepCmd() to do so as bbb927b did is not correct, because ATPrepCmd() can't distinguish between triggers that may be cloned and those that may not, so would wrongly try to recurse for the latter category of triggers. So this commit restores the code in EnableDisableTrigger() that 86f5759 had added to do the recursion, which would do it only for triggers that may be cloned, that is, row-level triggers. This also changes tablecmds.c such that ATExecCmd() is able to pass the value of ONLY flag down to EnableDisableTrigger() using its new 'recurse' parameter. This also fixes what seems like an oversight of 86f5759 that the recursion to partition triggers would only occur if EnableDisableTrigger() had actually changed the trigger. It is more apt to recurse to inspect partition triggers even if the parent's trigger didn't need to be changed: only then can we be certain that all descendants share the same state afterwards. Backpatch all the way back to 11, like bbb927b. Care is taken not to break ABI compatibility (and that no catversion bump is needed.) Co-authored-by: Amit Langote <amitlangote09@gmail.com> Reviewed-by: Dmitry Koval <d.koval@postgrespro.ru> Discussion: https://postgr.es/m/CA+HiwqG-cZT3XzGAnEgZQLoQbyfJApVwOTQaCaas1mhpf+4V5A@mail.gmail.com
1 parent efba7a6 commit 731d514

File tree

8 files changed

+134
-34
lines changed

8 files changed

+134
-34
lines changed

src/backend/commands/tablecmds.c

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,8 @@ static void ATExecSetRelOptions(Relation rel, List *defList,
553553
AlterTableType operation,
554554
LOCKMODE lockmode);
555555
static void ATExecEnableDisableTrigger(Relation rel, const char *trigname,
556-
char fires_when, bool skip_system, LOCKMODE lockmode);
556+
char fires_when, bool skip_system, bool recurse,
557+
LOCKMODE lockmode);
557558
static void ATExecEnableDisableRule(Relation rel, const char *rulename,
558559
char fires_when, LOCKMODE lockmode);
559560
static void ATPrepAddInherit(Relation child_rel);
@@ -4021,16 +4022,20 @@ AlterTableLookupRelation(AlterTableStmt *stmt, LOCKMODE lockmode)
40214022
* be done in this phase. Generally, this phase acquires table locks,
40224023
* checks permissions and relkind, and recurses to find child tables.
40234024
*
4024-
* ATRewriteCatalogs performs phase 2 for each affected table. (Note that
4025-
* phases 2 and 3 normally do no explicit recursion, since phase 1 already
4026-
* did it --- although some subcommands have to recurse in phase 2 instead.)
4025+
* ATRewriteCatalogs performs phase 2 for each affected table.
40274026
* Certain subcommands need to be performed before others to avoid
40284027
* unnecessary conflicts; for example, DROP COLUMN should come before
40294028
* ADD COLUMN. Therefore phase 1 divides the subcommands into multiple
40304029
* lists, one for each logical "pass" of phase 2.
40314030
*
40324031
* ATRewriteTables performs phase 3 for those tables that need it.
40334032
*
4033+
* For most subcommand types, phases 2 and 3 do no explicit recursion,
4034+
* since phase 1 already does it. However, for certain subcommand types
4035+
* it is only possible to determine how to recurse at phase 2 time; for
4036+
* those cases, phase 1 sets the cmd->recurse flag (or, in some older coding,
4037+
* changes the command subtype of a "Recurse" variant XXX to be cleaned up.)
4038+
*
40344039
* Thanks to the magic of MVCC, an error anywhere along the way rolls back
40354040
* the whole operation; we don't have to do anything special to clean up.
40364041
*
@@ -4450,10 +4455,10 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
44504455
errhint("Use ALTER TABLE ... DETACH PARTITION ... FINALIZE to complete the pending detach operation."));
44514456

44524457
/*
4453-
* Copy the original subcommand for each table. This avoids conflicts
4454-
* when different child tables need to make different parse
4455-
* transformations (for example, the same column may have different column
4456-
* numbers in different children).
4458+
* Copy the original subcommand for each table, so we can scribble on it.
4459+
* This avoids conflicts when different child tables need to make
4460+
* different parse transformations (for example, the same column may have
4461+
* different column numbers in different children).
44574462
*/
44584463
cmd = copyObject(cmd);
44594464

@@ -4722,8 +4727,9 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
47224727
case AT_DisableTrigAll:
47234728
case AT_DisableTrigUser:
47244729
ATSimplePermissions(rel, ATT_TABLE | ATT_FOREIGN_TABLE);
4725-
if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
4726-
ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode, context);
4730+
/* Set up recursion for phase 2; no other prep needed */
4731+
if (recurse)
4732+
cmd->recurse = true;
47274733
pass = AT_PASS_MISC;
47284734
break;
47294735
case AT_EnableRule: /* ENABLE/DISABLE RULE variants */
@@ -5061,35 +5067,51 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
50615067
break;
50625068
case AT_EnableTrig: /* ENABLE TRIGGER name */
50635069
ATExecEnableDisableTrigger(rel, cmd->name,
5064-
TRIGGER_FIRES_ON_ORIGIN, false, lockmode);
5070+
TRIGGER_FIRES_ON_ORIGIN, false,
5071+
cmd->recurse,
5072+
lockmode);
50655073
break;
50665074
case AT_EnableAlwaysTrig: /* ENABLE ALWAYS TRIGGER name */
50675075
ATExecEnableDisableTrigger(rel, cmd->name,
5068-
TRIGGER_FIRES_ALWAYS, false, lockmode);
5076+
TRIGGER_FIRES_ALWAYS, false,
5077+
cmd->recurse,
5078+
lockmode);
50695079
break;
50705080
case AT_EnableReplicaTrig: /* ENABLE REPLICA TRIGGER name */
50715081
ATExecEnableDisableTrigger(rel, cmd->name,
5072-
TRIGGER_FIRES_ON_REPLICA, false, lockmode);
5082+
TRIGGER_FIRES_ON_REPLICA, false,
5083+
cmd->recurse,
5084+
lockmode);
50735085
break;
50745086
case AT_DisableTrig: /* DISABLE TRIGGER name */
50755087
ATExecEnableDisableTrigger(rel, cmd->name,
5076-
TRIGGER_DISABLED, false, lockmode);
5088+
TRIGGER_DISABLED, false,
5089+
cmd->recurse,
5090+
lockmode);
50775091
break;
50785092
case AT_EnableTrigAll: /* ENABLE TRIGGER ALL */
50795093
ATExecEnableDisableTrigger(rel, NULL,
5080-
TRIGGER_FIRES_ON_ORIGIN, false, lockmode);
5094+
TRIGGER_FIRES_ON_ORIGIN, false,
5095+
cmd->recurse,
5096+
lockmode);
50815097
break;
50825098
case AT_DisableTrigAll: /* DISABLE TRIGGER ALL */
50835099
ATExecEnableDisableTrigger(rel, NULL,
5084-
TRIGGER_DISABLED, false, lockmode);
5100+
TRIGGER_DISABLED, false,
5101+
cmd->recurse,
5102+
lockmode);
50855103
break;
50865104
case AT_EnableTrigUser: /* ENABLE TRIGGER USER */
50875105
ATExecEnableDisableTrigger(rel, NULL,
5088-
TRIGGER_FIRES_ON_ORIGIN, true, lockmode);
5106+
TRIGGER_FIRES_ON_ORIGIN, true,
5107+
cmd->recurse,
5108+
lockmode);
50895109
break;
50905110
case AT_DisableTrigUser: /* DISABLE TRIGGER USER */
50915111
ATExecEnableDisableTrigger(rel, NULL,
5092-
TRIGGER_DISABLED, true, lockmode);
5112+
TRIGGER_DISABLED, true,
5113+
cmd->recurse,
5114+
lockmode);
50935115
break;
50945116

50955117
case AT_EnableRule: /* ENABLE RULE name */
@@ -14178,9 +14200,11 @@ index_copy_data(Relation rel, RelFileNode newrnode)
1417814200
*/
1417914201
static void
1418014202
ATExecEnableDisableTrigger(Relation rel, const char *trigname,
14181-
char fires_when, bool skip_system, LOCKMODE lockmode)
14203+
char fires_when, bool skip_system, bool recurse,
14204+
LOCKMODE lockmode)
1418214205
{
14183-
EnableDisableTrigger(rel, trigname, fires_when, skip_system, lockmode);
14206+
EnableDisableTriggerNew(rel, trigname, fires_when, skip_system, recurse,
14207+
lockmode);
1418414208
}
1418514209

1418614210
/*

src/backend/commands/trigger.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,14 +1543,16 @@ renametrig(RenameStmt *stmt)
15431543
* enablement/disablement, this also defines when the trigger
15441544
* should be fired in session replication roles.
15451545
* skip_system: if true, skip "system" triggers (constraint triggers)
1546+
* recurse: if true, recurse to partitions
15461547
*
15471548
* Caller should have checked permissions for the table; here we also
15481549
* enforce that superuser privilege is required to alter the state of
15491550
* system triggers
15501551
*/
15511552
void
1552-
EnableDisableTrigger(Relation rel, const char *tgname,
1553-
char fires_when, bool skip_system, LOCKMODE lockmode)
1553+
EnableDisableTriggerNew(Relation rel, const char *tgname,
1554+
char fires_when, bool skip_system, bool recurse,
1555+
LOCKMODE lockmode)
15541556
{
15551557
Relation tgrel;
15561558
int nkeys;
@@ -1616,6 +1618,34 @@ EnableDisableTrigger(Relation rel, const char *tgname,
16161618
changed = true;
16171619
}
16181620

1621+
/*
1622+
* When altering FOR EACH ROW triggers on a partitioned table, do the
1623+
* same on the partitions as well, unless ONLY is specified.
1624+
*
1625+
* Note that we recurse even if we didn't change the trigger above,
1626+
* because the partitions' copy of the trigger may have a different
1627+
* value of tgenabled than the parent's trigger and thus might need to
1628+
* be changed.
1629+
*/
1630+
if (recurse &&
1631+
rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
1632+
(TRIGGER_FOR_ROW(oldtrig->tgtype)))
1633+
{
1634+
PartitionDesc partdesc = RelationGetPartitionDesc(rel, true);
1635+
int i;
1636+
1637+
for (i = 0; i < partdesc->nparts; i++)
1638+
{
1639+
Relation part;
1640+
1641+
part = relation_open(partdesc->oids[i], lockmode);
1642+
EnableDisableTriggerNew(part, NameStr(oldtrig->tgname),
1643+
fires_when, skip_system, recurse,
1644+
lockmode);
1645+
table_close(part, NoLock); /* keep lock till commit */
1646+
}
1647+
}
1648+
16191649
InvokeObjectPostAlterHook(TriggerRelationId,
16201650
oldtrig->oid, 0);
16211651
}
@@ -1639,6 +1669,19 @@ EnableDisableTrigger(Relation rel, const char *tgname,
16391669
CacheInvalidateRelcache(rel);
16401670
}
16411671

1672+
/*
1673+
* ABI-compatible wrapper for the above. To keep as close possible to the old
1674+
* behavior, this never recurses. Do not call this function in new code.
1675+
*/
1676+
void
1677+
EnableDisableTrigger(Relation rel, const char *tgname,
1678+
char fires_when, bool skip_system,
1679+
LOCKMODE lockmode)
1680+
{
1681+
EnableDisableTriggerNew(rel, tgname, fires_when, skip_system,
1682+
true, lockmode);
1683+
}
1684+
16421685

16431686
/*
16441687
* Build trigger data to attach to the given relcache entry.

src/backend/nodes/copyfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,6 +3344,7 @@ _copyAlterTableCmd(const AlterTableCmd *from)
33443344
COPY_NODE_FIELD(def);
33453345
COPY_SCALAR_FIELD(behavior);
33463346
COPY_SCALAR_FIELD(missing_ok);
3347+
COPY_SCALAR_FIELD(recurse);
33473348

33483349
return newnode;
33493350
}

src/backend/nodes/equalfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ _equalAlterTableCmd(const AlterTableCmd *a, const AlterTableCmd *b)
11361136
COMPARE_NODE_FIELD(def);
11371137
COMPARE_SCALAR_FIELD(behavior);
11381138
COMPARE_SCALAR_FIELD(missing_ok);
1139+
COMPARE_SCALAR_FIELD(recurse);
11391140

11401141
return true;
11411142
}

src/include/commands/trigger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ extern Oid get_trigger_oid(Oid relid, const char *name, bool missing_ok);
165165

166166
extern ObjectAddress renametrig(RenameStmt *stmt);
167167

168+
extern void EnableDisableTriggerNew(Relation rel, const char *tgname,
169+
char fires_when, bool skip_system, bool recurse,
170+
LOCKMODE lockmode);
168171
extern void EnableDisableTrigger(Relation rel, const char *tgname,
169172
char fires_when, bool skip_system, LOCKMODE lockmode);
170173

src/include/nodes/parsenodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,7 @@ typedef struct AlterTableCmd /* one subcommand of an ALTER TABLE */
19561956
* constraint, or parent table */
19571957
DropBehavior behavior; /* RESTRICT or CASCADE for DROP cases */
19581958
bool missing_ok; /* skip error if missing? */
1959+
bool recurse; /* exec-time recursion */
19591960
} AlterTableCmd;
19601961

19611962

src/test/regress/expected/triggers.out

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,24 +2655,42 @@ create table parent (a int) partition by list (a);
26552655
create table child1 partition of parent for values in (1);
26562656
create trigger tg after insert on parent
26572657
for each row execute procedure trig_nothing();
2658+
create trigger tg_stmt after insert on parent
2659+
for statement execute procedure trig_nothing();
26582660
select tgrelid::regclass, tgname, tgenabled from pg_trigger
26592661
where tgrelid in ('parent'::regclass, 'child1'::regclass)
26602662
order by tgrelid::regclass::text;
2661-
tgrelid | tgname | tgenabled
2662-
---------+--------+-----------
2663-
child1 | tg | O
2664-
parent | tg | O
2665-
(2 rows)
2663+
tgrelid | tgname | tgenabled
2664+
---------+---------+-----------
2665+
child1 | tg | O
2666+
parent | tg | O
2667+
parent | tg_stmt | O
2668+
(3 rows)
26662669

2667-
alter table only parent enable always trigger tg;
2670+
alter table only parent enable always trigger tg; -- no recursion because ONLY
2671+
alter table parent enable always trigger tg_stmt; -- no recursion because statement trigger
26682672
select tgrelid::regclass, tgname, tgenabled from pg_trigger
26692673
where tgrelid in ('parent'::regclass, 'child1'::regclass)
26702674
order by tgrelid::regclass::text;
2671-
tgrelid | tgname | tgenabled
2672-
---------+--------+-----------
2673-
child1 | tg | O
2674-
parent | tg | A
2675-
(2 rows)
2675+
tgrelid | tgname | tgenabled
2676+
---------+---------+-----------
2677+
child1 | tg | O
2678+
parent | tg | A
2679+
parent | tg_stmt | A
2680+
(3 rows)
2681+
2682+
-- The following is a no-op for the parent trigger but not so
2683+
-- for the child trigger, so recursion should be applied.
2684+
alter table parent enable always trigger tg;
2685+
select tgrelid::regclass, tgname, tgenabled from pg_trigger
2686+
where tgrelid in ('parent'::regclass, 'child1'::regclass)
2687+
order by tgrelid::regclass::text;
2688+
tgrelid | tgname | tgenabled
2689+
---------+---------+-----------
2690+
child1 | tg | A
2691+
parent | tg | A
2692+
parent | tg_stmt | A
2693+
(3 rows)
26762694

26772695
drop table parent, child1;
26782696
-- Verify that firing state propagates correctly on creation, too

src/test/regress/sql/triggers.sql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1832,10 +1832,19 @@ create table parent (a int) partition by list (a);
18321832
create table child1 partition of parent for values in (1);
18331833
create trigger tg after insert on parent
18341834
for each row execute procedure trig_nothing();
1835+
create trigger tg_stmt after insert on parent
1836+
for statement execute procedure trig_nothing();
18351837
select tgrelid::regclass, tgname, tgenabled from pg_trigger
18361838
where tgrelid in ('parent'::regclass, 'child1'::regclass)
18371839
order by tgrelid::regclass::text;
1838-
alter table only parent enable always trigger tg;
1840+
alter table only parent enable always trigger tg; -- no recursion because ONLY
1841+
alter table parent enable always trigger tg_stmt; -- no recursion because statement trigger
1842+
select tgrelid::regclass, tgname, tgenabled from pg_trigger
1843+
where tgrelid in ('parent'::regclass, 'child1'::regclass)
1844+
order by tgrelid::regclass::text;
1845+
-- The following is a no-op for the parent trigger but not so
1846+
-- for the child trigger, so recursion should be applied.
1847+
alter table parent enable always trigger tg;
18391848
select tgrelid::regclass, tgname, tgenabled from pg_trigger
18401849
where tgrelid in ('parent'::regclass, 'child1'::regclass)
18411850
order by tgrelid::regclass::text;

0 commit comments

Comments
 (0)