Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tZtUr-00E5GY-AV for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Jan 2025 15:15:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tZtUq-0039gN-AG for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Jan 2025 15:15:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tZtUq-0039gC-0U for pgsql-hackers@lists.postgresql.org; Mon, 20 Jan 2025 15:15:24 +0000 Received: from mail-lj1-x22f.google.com ([2a00:1450:4864:20::22f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tZtUn-000ZPB-1i for pgsql-hackers@postgresql.org; Mon, 20 Jan 2025 15:15:22 +0000 Received: by mail-lj1-x22f.google.com with SMTP id 38308e7fff4ca-30613802a59so49356731fa.0 for ; Mon, 20 Jan 2025 07:15:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737386120; x=1737990920; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=cvD1YF8lBx73J21VswzdUcAwW5m0OeZVA5ImyLwMlXs=; b=SvPp6IoOEd2ivHTuciNgsSUEE8v+8HxU3c41JvkANSWT7/tPVS7NgfrhL17rbQziu4 YT3utElTIsa6hSfcc0Jfxuu/O10HGr6bS1kq2DAIXkDHUVTSvKsHzgpsYrOxMOto1rXr fdU0gZsl9hxRWKhZAMraEAa3YJ8Mb71UtPe5tHvj126MHqwMVK/+YZC7UmrtwybLG+4w T8hTITtt+usMjb+fErtBqNIwV7IJJiMciMsu4FFhcdGJAbKpC6YRtQvCQsRPFuJoLpVB EhM5AsM2IoWlHKQSZzanJgWpps/PCzrfMYBtraGDOGVVurEG3w+8QCtDd6hgZq36MjQY N6jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737386120; x=1737990920; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=cvD1YF8lBx73J21VswzdUcAwW5m0OeZVA5ImyLwMlXs=; b=tqj7TBtYgcW6vc6RN1AYPWh+YmvTQ+mdTK/oKAaNiJ2GaGpgdhZX1A2ZZMXS5OX5Hd ZH/wbmCa45j8qHWipH/3asObQrdcu6QmogBT+t5MaVNYbnQ9Cz/2DFeJpcfOnAlGyft8 19Tkrm7Gxvuus4fQ3Fjo94xMEP2H5IM6brCq1yIh4g5I44nfZxHbJI0M9DVif6g93qvo +x4N0FwEkIuke4YyG9PehJ2MkI0upWRCiSjgSf6IjG3sM0sl2FbCplunZzXUBWf58jHg tkE8ZCYgBQoNnDl/md8Q8nRooWsnpTADMabGOnxWpxN41ua5zRW9B8JCkqNxMRaNEb71 PA3A== X-Gm-Message-State: AOJu0Ywx6MYJHyzd6dZO7X0Pl/yzErV3I5hOx6cZH+UmK47CGW+hDv6p Is67nyYWsK6Gd7HxTCw81O/2ZdQokazdqBcrNKwncR7qwZV2d9f9jv+pL9yRQ5h1eYQc9eiQnCe yFhpLIqT8FiPPL26keKfyXYuAneU= X-Gm-Gg: ASbGncvBvlAWvDGALX/ZL4fuabC4CNMe4uGxWVZX1rDIe7XZ9AcTCZ3DBV3MTx7Bmoe vAPIxyeaZvsb23wmCwg6glJiGWdrA848c/jjXRs3tpPQTuDYf2w== X-Google-Smtp-Source: AGHT+IH1/jWOJpTO0+/AxA32XXzwRFBVSs7b49LwqAIfQLDlBQt+tHq/hS/LKRJuomJChlWMxZ4bUm1aRQdvxPjwUcA= X-Received: by 2002:a2e:a449:0:b0:300:2a29:d47c with SMTP id 38308e7fff4ca-3072cb1f853mr42572111fa.24.1737386120048; Mon, 20 Jan 2025 07:15:20 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Sami Imseih Date: Mon, 20 Jan 2025 09:15:08 -0600 X-Gm-Features: AbW1kvYpQG-yFcJglV-d3ocJYZAiYpJtHdmS5J2YaclWlj1gM8MB306O43gElI0 Message-ID: Subject: Re: Bug in detaching a partition with a foreign key. To: Amul Sul Cc: PostgreSQL-development Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > 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