Re: Fix ALTER TABLE DROP EXPRESSION with inheritance hierarchy

From: BharatDB <bharatdbpg(at)gmail(dot)com>
To: Kirill Reshke <reshkekirill(at)gmail(dot)com>
Cc: jian he <jian(dot)universality(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix ALTER TABLE DROP EXPRESSION with inheritance hierarchy
Date: 2025-10-22 12:26:41
Message-ID: CAAh00ER+dssk3U6hniUzUFLnYu-RDLVQVGo0TiMivNLj8EEWTA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi all,

I tried to fix a bug in PostgreSQL where ALTER TABLE ... DROP EXPRESSION
fails on multi-level inheritance hierarchies.

Bug:
When a parent table has a generated column and child/grandchild tables
inherit from it, executing:

ALTER TABLE parent ALTER COLUMN d DROP EXPRESSION;

ERROR: ALTER TABLE / DROP EXPRESSION must be applied to child tables too

Fix Details:

-

Updated file: src/backend/commands/tablecmds.c

-

Function modified: ATPrepDropExpression()

-

Change: Added !recursing check to ensure proper recursion across
inheritance.

if (!recurse && !recursing &&

find_inheritance_children(RelationGetRelid(rel), lockmode))

ereport(ERROR,

errcode(ERRCODE_FEATURE_NOT_SUPPORTED),

errmsg("ALTER TABLE / DROP EXPRESSION must be applied to
child tables too"),

errhint("Do not specify the ONLY keyword."));

-

Test Query DROP TABLE IF EXISTS parent CASCADE;
CREATE TABLE parent (a int, d int GENERATED ALWAYS AS (11) STORED);
CREATE TABLE child () INHERITS (parent);
CREATE TABLE grandchild () INHERITS (child);
ALTER TABLE parent ALTER COLUMN d DROP EXPRESSION;
-

Output ALTER TABLE

On Thu, Aug 28, 2025 at 10:49 AM Kirill Reshke <reshkekirill(at)gmail(dot)com>
wrote:

> On Thu, 28 Aug 2025 at 08:35, jian he <jian(dot)universality(at)gmail(dot)com> wrote:
> >
> > That means, we don't need to change the ATPrepDropExpression function
> > argument for now?
>
> Sure. V3 with this attached, and I think we can move cf entry to RFC
>
> --
> Best regards,
> Kirill Reshke
>

Attachment Content-Type Size
0001-Fix-ALTER-TABLE-DROP-EXPRESSION-for-inheritance-hier.patch text/x-patch 1.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nazir Bilal Yavuz 2025-10-22 12:33:37 Re: Speed up COPY FROM text/CSV parsing using SIMD
Previous Message Daniel Gustafsson 2025-10-22 12:06:44 Re: CI: Add task that runs pgindent