Re: bug, ALTER TABLE call ATPostAlterTypeCleanup twice for the same relation

From: Nikhil Sontakke <nikhil(at)planetscale(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Bryan Green <dbryan(dot)green(at)gmail(dot)com>, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, David Rowley <dgrowleyml(at)gmail(dot)com>, amul sul <sulamul(at)gmail(dot)com>, Peter Eisentraut <peter(at)eisentraut(dot)org>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: bug, ALTER TABLE call ATPostAlterTypeCleanup twice for the same relation
Date: 2026-12-03 11:58:47
Message-ID: CA+UBoq3Zk4EB-GSrbM=TjDft_YoCbjrs7GSYWo9+SgCDQF2Zuw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Jian,

On Tue, Sep 15, 2026 at 12:21 PM jian he <jian(dot)universality(at)gmail(dot)com>
wrote:

> On Mon, Sep 14, 2026 at 5:21 AM Bryan Green <dbryan(dot)green(at)gmail(dot)com>
> wrote:
> >
> > On 8/3/2026 1:00 AM, Chao Li wrote:
> > >
> > >> On Aug 2, 2026, at 19:52, jian he <jian(dot)universality(at)gmail(dot)com>
> wrote:
> > >>
>
> > With that list added, the rest of the approach looks correct to me.
> >
>
> Thanks.
> I did not notice the newly added AlteredTableInfo->changedStatisticsOwners.
> I have added some tests to stats_ext.sql
>
>
>
Hi jian,

I tested v7 and found a case that it gets wrong. The command no longer
fails with "cache lookup failed", but it now succeeds and leaves a foreign
key violated!

CREATE TABLE pk (x varchar(10), y int, PRIMARY KEY (x, y));
INSERT INTO pk VALUES ('a', 1);
CREATE TABLE fk (f varchar(10), a int,
e int GENERATED ALWAYS AS (a) STORED,
FOREIGN KEY (f, e) REFERENCES pk);
INSERT INTO fk (f, a) VALUES ('a', 1);

ALTER TABLE fk ALTER COLUMN f TYPE varchar(20),
ALTER COLUMN e SET EXPRESSION AS (a + 100);

SELECT * FROM fk; -- ('a', 101): no matching row in pk

On HEAD the ALTER fails with "cache lookup failed for constraint". With v7
it succeeds, and the FK stays marked as valid. Note that running the two
changes as separate ALTER TABLE commands correctly fails with "insert
or update on table "fk" violates foreign key constraint".

Here is what happens. The first ATPostAlterTypeCleanup() call, after the
ALTER TYPE pass, re-adds the FK using tab->rewrite as it stands at that
moment. Because varchar(10) -> varchar(20) needs no rewrite,
TryReuseForeignKey() lets the FK skip validation. The stored SET EXPRESSION
runs afterwards: it sets tab->rewrite and changes e's values, but the FK is
never checked again.

The same thing happens on the referenced side, when the altered table's
primary key includes the generated column and another table references it,
and with a partitioned referencing table.

Regards,
Nikhil
---
Nikhil Sontakke
PlanetScale

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message 'Álvaro Herrera' 2026-12-03 11:58:55 Re: logical decoding: removing change_useless_for_repack()
Previous Message Etsuro Fujita 2026-12-03 11:50:07 Re: postgres_fdw: statistics import can silently fall back to sampling across encodings