Re: [BUG] Fix DETACH with FK pointing to a partitioned table fails

From: Tender Wang <tndrwang(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Cc: Alexander Lakhin <exclusion(at)gmail(dot)com>, Jehan-Guillaume de Rorthais <jgdr(at)dalibo(dot)com>, Junwang Zhao <zhjwpku(at)gmail(dot)com>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>, Guillaume Lelarge <guillaume(at)lelarge(dot)info>, Michael Paquier <michael(at)paquier(dot)xyz>
Subject: Re: [BUG] Fix DETACH with FK pointing to a partitioned table fails
Date: 2024-10-28 08:45:17
Message-ID: CAHewXN=OPgvuvQn=3DzN=YWLi_Q_VikS1QuqxHu-fJRSBtj2ug@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org> 于2024年10月25日周五 23:14写道:

> On 2024-Oct-25, Alexander Lakhin wrote:
>
> > I've also discovered another anomaly with a similar setup, but it's not
> > related to 53af9491a.
>
> Hmm, it may well be a preexisting problem, but I do think it involves
> the same code. As far as I can tell, the value "2" here
>
> > This script ends up with:
> > ERROR: invalid attribute number 2
> > ERROR: cache lookup failed for attribute 2 of relation 16398
>
> is coming from riinfo->confdelsetcols which was set up by
> DetachPartitionFinalize during the last DETACH operation.
>

Hmm, actually, the confdelsetcols before detach and after detach is always
{2}, as below:

postgres=# select oid, conname, conrelid,conparentid,confdelsetcols from
pg_constraint where conrelid = 16397;
oid | conname | conrelid | conparentid | confdelsetcols
-------+-----------+----------+-------------+----------------
16400 | pt_a_fkey | 16397 | 16392 | {2}
(1 row)

postgres=# ALTER TABLE pt DETACH PARTITION tp1;
ALTER TABLE
postgres=# select oid, conname, conrelid,conparentid,confdelsetcols from
pg_constraint where conrelid = 16397;
oid | conname | conrelid | conparentid | confdelsetcols
-------+-----------+----------+-------------+----------------
16400 | pt_a_fkey | 16397 | 0 | {2}
(1 row)

Even though no detach, the confdelsetcols is {2} . But no error report.
Because the rel->rd_att->natts of pt is 2.
It will not go into tp1 because tp1 is a partition of pt. But after
detach, the rel->rd_att->natts of tp1 is 1,
so "ERROR: invalid attribute number 2" will report.

CREATE TABLE tp1... will ignore the dropped column of parent, so the natts
of tp1 is 1, but its parent is 2.

--
Thanks,
Tender Wang

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2024-10-28 08:50:03 Re: Pgoutput not capturing the generated columns
Previous Message Jingtang Zhang 2024-10-28 08:43:09 Re: Introduce new multi insert Table AM and improve performance of various SQL commands with it for Heap AM