Re: let ALTER TABLE DROP COLUMN drop whole-row referenced object
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: Chao Li <li.evan.chao@gmail.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2025-09-23T05:37:44Z
Lists: pgsql-hackers
Attachments
- v5-0001-ALTER-TABLE-DROP-COLUMN-drop-wholerow-referenced-object.patch (text/x-patch) patch v5-0001
- v5-0002-disallow-ALTER-COLUMN-SET-DATA-TYPE-when-wholerow-referenced-cons.patch (text/x-patch) patch v5-0002
- v5-0003-disallow-change-or-drop-column-when-wholerow-referenced-policy-ex.patch (text/x-patch) patch v5-0003
On Mon, Sep 15, 2025 at 8:40 PM jian he <jian.universality@gmail.com> wrote:
>
> Summary of attached v4:
> v4-0001: Handles ALTER TABLE DROP COLUMN when whole-row Vars are
> referenced in check constraints and indexes.
>
> v4-0002: Handles ALTER TABLE ALTER COLUMN SET DATA TYPE when whole-row
> Vars are referenced in check constraints and indexes.
>
> v4-0003: Handle ALTER TABLE ALTER COLUMN SET DATA TYPE and ALTER TABLE DROP
> COLUMN when policy objects reference whole-row Vars. Policy quals and check
> quals may contain whole-row Vars and can include sublinks (unplanned
> subqueries), pull_varattnos is not enough to locate whole-row Var. Instead,
> obtain the whole-row type OID and recursively check each Var in expression node
> to see if its vartype matches the whole-row type OID.
in v4, I use
+ TupleConstr *constr = RelationGetDescr(rel)->constr;
+
+ if (constr && constr->num_check > 0)
+{
+ systable_beginscan
+}
to check if a relation's check constraint expression contains a whole-row or
not. however this will have multiple systable_beginscan if multiple check
constraints contain wholerow expr.
I changed it to systable_beginscan pg_constraint once and check if the scan
returned pg_constraint tuple meets our condition or not.
and some minor adjustments to regression tests.