let ALTER COLUMN SET DATA TYPE cope with trigger dependency

Started by jian heabout 1 year ago3 messageshackers

Thread notes

1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
hackorum-bot
1 day ago

@global #feature #moderate #enhancement

#feature#moderate#enhancement
Jump to latest
#1jian he
jian.universality@gmail.com

hi.

demo:
CREATE TABLE main_table (a int, b int);
CREATE FUNCTION trigger_func() RETURNS trigger LANGUAGE plpgsql AS '
BEGIN
RAISE NOTICE ''trigger_func(%) called: action = %, when = %, level = %'',
TG_ARGV[0], TG_OP, TG_WHEN, TG_LEVEL;
RETURN NULL;
END;';
CREATE TRIGGER modified_a BEFORE UPDATE OF a ON main_table
FOR EACH ROW WHEN (OLD.a <> NEW.a) EXECUTE PROCEDURE trigger_func('modified_a');

ALTER TABLE main_table ALTER COLUMN a SET DATA TYPE INT8;
ERROR: cannot alter type of a column used in a trigger definition
DETAIL: trigger modified_a on table main_table depends on column "a"

With the attached patch, the previous error will no longer occur.
Foreign key–related internal triggers are not directly dependent on the relation
itself; instead, they depend directly on the constraint.
Therefore, we don't need to worry about internal triggers in this context.

v1-0001: "refactor CreateTrigger and CreateTriggerFiringOn".
used also in /messages/by-id/CACJufxHJAr2FjbeB6ghg_-N5dxX5JVnjKSLOUxOyt4TeaAWQkg@mail.gmail.com
v1-0002, the actual implementation.

Attachments:

v1-0001-refactor-CreateTrigger-and-CreateTriggerFiringOn.patchtext/x-patch; charset=US-ASCII; name=v1-0001-refactor-CreateTrigger-and-CreateTriggerFiringOn.patchDownload+212-141
v1-0002-let-ALTER-COLUMN-SET-DATA-TYPE-cope-with-trigger-dependency.patchtext/x-patch; charset=UTF-8; name=v1-0002-let-ALTER-COLUMN-SET-DATA-TYPE-cope-with-trigger-dependency.patchDownload+288-18
#2jian he
jian.universality@gmail.com
In reply to: jian he (#1)
Re: let ALTER COLUMN SET DATA TYPE cope with trigger dependency

hi.

v2-0001, v2-0002 is the same as [1]/messages/by-id/CACJufxFu7Y4FhVkaKT2Kaj8ym2T5TcwN93cR_6h4x66iLrSZ-Q@mail.gmail.com.
The parameters relOid and refRelOid are removed from the CreateTrigger function
and instead added to the CreateTrigStmt structure.

The main reason is to avoid repeated name lookups.
In fact, we already did this in CreateTriggerFiringOn, see below code snippet.
If the OID is valid, use it preferentially; otherwise, fall back to resolving
the relation via the RangeVar.

```
if (OidIsValid(relOid))
rel = table_open(relOid, ShareRowExclusiveLock);
else
rel = table_openrv(stmt->relation, ShareRowExclusiveLock);

if (OidIsValid(refRelOid))
{
LockRelationOid(refRelOid, AccessShareLock);
constrrelid = refRelOid;
}
else if (stmt->constrrel != NULL)
constrrelid = RangeVarGetRelid(stmt->constrrel, AccessShareLock,
false);
```

v2-0003: is the actual implementation.
The general workflow would be as follows: collect all affected triggers,
retrieve their definitions, drop the existing triggers, and then recreate them.
Any comments associated with the triggers will also be dropped and recreated.

[1]: /messages/by-id/CACJufxFu7Y4FhVkaKT2Kaj8ym2T5TcwN93cR_6h4x66iLrSZ-Q@mail.gmail.com

--
jian
https://www.enterprisedb.com/

Attachments:

v2-0001-add-relOid-field-to-CreateTrigStmt.patchtext/x-patch; charset=UTF-8; name=v2-0001-add-relOid-field-to-CreateTrigStmt.patchDownload+33-18
v2-0002-add-constrrelOid-field-to-CreateTrigStmt.patchtext/x-patch; charset=US-ASCII; name=v2-0002-add-constrrelOid-field-to-CreateTrigStmt.patchDownload+23-21
v2-0003-let-SET-DATA-TYPE-cope-with-trigger-dependency.patchtext/x-patch; charset=UTF-8; name=v2-0003-let-SET-DATA-TYPE-cope-with-trigger-dependency.patchDownload+335-19
#3jian he
jian.universality@gmail.com
In reply to: jian he (#2)
Re: let ALTER COLUMN SET DATA TYPE cope with trigger dependency

hi.

V3 is attached. this should be more neat.
Now it looks very similar to how statistics cope with column data type change.

For statistics, in ATPostAlterTypeParse we will call
transformStatsStmt, we need to do the similar thing for the trigger
WHEN clause.
I introduced transformTriggerStmt and placed it in
src/backend/commands/trigger.c, which should be fine, I think.
It will be invoked from within CreateTriggerFiringOn.

--
jian
https://www.enterprisedb.com/

Attachments:

t52365_3
v3-0001-let-SET-DATA-TYPE-cope-with-trigger-dependency.patchapplication/x-patch; name=v3-0001-let-SET-DATA-TYPE-cope-with-trigger-dependency.patchDownload+496-28