Re: ALTER COLUMN SET EXPRESSION on partitions not work in case of constraint dependencies

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Alberto Piai <alberto(dot)piai(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: ALTER COLUMN SET EXPRESSION on partitions not work in case of constraint dependencies
Date: 2026-08-10 08:28:46
Message-ID: CACJufxE4PXVEtJ5t_W3FWz-Q-YPbVgne5chmu0BUoX5Sas8B=w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Aug 7, 2026 at 9:35 PM Alberto Piai <alberto(dot)piai(at)gmail(dot)com> wrote:
>
> It's admittedly a long shot, since I'm still studying all of this, but:
> what if, when we RememberConstraintForRebuilding() a constraint with
> conislocal=false, we also RememberConstraintForRebuilding() its
> corresponding parent constraint?
>
> I have not fully considered the implications of this and I'm only
> thinking about CHECK and NN constraints.
>
That will make the entire hierarchy take an AccessExclusiveLock,
which is not ideal and may require consensus.
I think if the partition's generated column has an index on it, then
directly changing its generation expression will not work.

Please check the two attached patches:
v3-0001-Disallow-directly-ALTER-TABLE-SET-EXPRESSION-on-child-table-if-de.patch
is for disallow directly running ALTER TABLE SET EXPRESSION on child table if
dependencies (indexes, constraints) exist.

v3-0001-Fix-dependency-issue-when-directly-ALTER-TABLE-SET-EXPRESSION-o.nocfbot
It allows directly running ALTER TABLE SET EXPRESSION on a partition, if that
partition only has constraint dependencies, not index dependencies. This is a
POC with extensive tests.
Rationale: changing the generation expression only affects the underlying column
data, not its type. So instead of rebuilding these constraints, we can just
re-validate them against the newly computed data. This avoids unnecessary
constraint rebuilds and gets rid of the ACCESS EXCLUSIVE lock requirement on the
whole partition hierarchy when we're only touching one partition.

We may or may not error out if a generated column has index or
constraint dependencies, so we need
to call RememberAllDependentForRebuilding earlier, so I moved it from
ATExecSetExpression to ATPrepCmd.
Looking for generated column dependencies in phase 1 should be fine.
See ATPrepSetExpression.

Given the complexity of the whole thing, we should error out in the back branch.

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

Attachment Content-Type Size
v3-0001-Disallow-directly-ALTER-TABLE-SET-EXPRESSION-on-child-table-if-de.patch text/x-patch 17.8 KB
v3-0001-Fix-dependency-issue-when-directly-ALTER-TABLE-SET-EXPRESSION-o.nocfbot application/octet-stream 27.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-08-10 08:40:43 Re: [PATCH] Fix SIGSEGV in GrantLockLocal when OOM leaves LOCALLOCK.lockOwners NULL
Previous Message Michael Paquier 2026-08-10 08:25:39 Re: [PATCH] Fix TOCTOU races in recovery/t/020_archive_status.pl archive checks