Re: Bug in detaching a partition with a foreign key.

From: Sami Imseih <samimseih(at)gmail(dot)com>
To: Amul Sul <sulamul(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in detaching a partition with a foreign key.
Date: 2025-01-20 15:15:08
Message-ID: CAA5RZ0ssXJjOw=gPK_7rmFVGrdyWHVdSbPvib69DsCPxs8Cs7w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> should cut the link between the parent constraint and the constraint on
> the partition being detached.
correct by setting the conparentid to 0 in pg_constraint and to delete
the pg_depend record for partition dependency. But in the repro case,
we don't have a dependency as the table the foreign key is on is a
partition.

> The trial patch I posted was meant to prevent
> cutting the link between the partition's constraint and the constraint that
> inherits it. Your patch seems to achieve the same, AFAIUC,

Yes, exactly.

> but it would be helpful if you could provide more
> context -- such as crash detail and/or a test case.

Below is the repro I used. Similar as you original repro,
but without subpartition on foo_p0. This also results in the segfault
with your attached patch.

""
CREATE TABLE bar(id int PRIMARY KEY) PARTITION BY RANGE(id);
CREATE TABLE bar_p0 PARTITION OF bar FOR VALUES FROM (0) TO (100);
CREATE TABLE foo(id int) PARTITION BY RANGE(id);
CREATE TABLE foo_p0 PARTITION OF foo FOR VALUES FROM (0) TO (100);
ALTER TABLE foo_p0 ADD CONSTRAINT child_fk_con FOREIGN KEY (id) REFERENCES bar;
ALTER TABLE foo DETACH PARTITION foo_p0;
"""

Here is the core dump
"""
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `postgres: postgres postgres [local] ALTER T'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 GetMemoryChunkMethodID (pointer=0x0) at mcxt.c:205
205 header = *((const uint64 *) ((const char *) pointer -
sizeof(uint64)));

Thread 1 (Thread 0xffffb6e34f00 (LWP 17508)):
#0 GetMemoryChunkMethodID (pointer=0x0) at mcxt.c:205
header = 12273896
#1 0x0000000000c05edc in repalloc (pointer=0x0, size=128) at mcxt.c:1566
ret = 0x10b5b98380
#2 0x00000000007fbf24 in enlarge_list (list=0x18cd1490, min_size=1)
at list.c:209
new_max_len = 16
#3 0x00000000007fc22c in new_tail_cell (list=0x18cd1490) at list.c:327
No locals.
#4 0x00000000007fc334 in lappend_oid (list=0x18cd1490, datum=16404)
at list.c:382
No locals.
"""

I was able to get your patch working by changing the way the fksid
is built with the below:

/* Collect all the constraint ids */
foreach(cell, fks)
{
ForeignKeyCacheInfo *fk = lfirst(cell);
fksids = lappend_oid(fksids, fk->conoid);
}

Regards,

Sami

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Álvaro Herrera 2025-01-20 15:26:35 Re: Bug in detaching a partition with a foreign key.
Previous Message Zhou, Zhiguo 2025-01-20 15:04:40 Re: [RFC] Lock-free XLog Reservation from WAL