| From: | jian he <jian(dot)universality(at)gmail(dot)com> |
|---|---|
| To: | Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> |
| Cc: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
| Subject: | Re: let ALTER TABLE DROP COLUMN drop whole-row referenced object |
| Date: | 2025-12-27 03:00:36 |
| Message-ID: | CACJufxHvZbH6p+h1HSQXn6D2aVNP+xg5WQedYSaTuBkbd2jHdg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
hi.
CREATE FUNCTION dummy_trigger() RETURNS TRIGGER AS $$
BEGIN
RETURN NULL;
END
$$ language plpgsql;
create table main_table(a int);
CREATE TRIGGER before_ins_stmt_trig BEFORE INSERT ON main_table
FOR EACH ROW
WHEN (new.a > 0)
EXECUTE PROCEDURE dummy_trigger();
ALTER TABLE main_table ALTER COLUMN a SET DATA TYPE INT8; --error
ALTER TABLE main_table DROP COLUMN a; --error
Dropping a column or changing its data type will fail if the column is
referenced in a trigger’s WHEN clause, that's the current behavior.
I think we should expand that to a whole-row reference WHEN clause in trigger.
DROP TRIGGER before_ins_stmt_trig ON main_table;
CREATE TRIGGER before_ins_stmt_trig BEFORE INSERT ON main_table
FOR EACH ROW
WHEN (new is null)
EXECUTE PROCEDURE dummy_trigger();
ALTER TABLE main_table ALTER COLUMN a SET DATA TYPE INT8; --expect to error
ALTER TABLE main_table DROP COLUMN a; --expect to error
new summary:
For (constraints, indexes, policies, triggers) that contain whole-row
references:
ALTER TABLE DROP COLUMN [CASCADE] will drop these objects too.
ALTER COLUMN SET DATA TYPE will error out because whole-row–dependent objects
exist.
| Attachment | Content-Type | Size |
|---|---|---|
| v7-0001-fix-DDL-wholerow-referenced-constraints-and-indexes.patch | application/x-patch | 16.2 KB |
| v7-0003-disallow-ALTER-TABLE-ALTER-COLUMN-when-wholerow-referenced-policy.patch | application/x-patch | 13.5 KB |
| v7-0002-disallow-drop-or-change-column-if-wholerow-trigger-exists.patch | application/x-patch | 8.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2025-12-27 04:15:11 | Re: Question about partial index WHERE clause predicate ordering |
| Previous Message | Xuneng Zhou | 2025-12-27 01:30:51 | Re: Run perltidy on src/test/perl/PostgreSQL/Test/Cluster.pm |