Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lcXNe-0003Ew-R4 for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Apr 2021 17:56:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lcXNd-0006AK-RK for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Apr 2021 17:56:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lcXNO-0004pQ-6V for pgsql-bugs@lists.postgresql.org; Fri, 30 Apr 2021 17:56:30 +0000 Received: from [64.147.123.21] (helo=wout5-smtp.messagingengine.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lcXNK-0006Df-MY for pgsql-bugs@lists.postgresql.org; Fri, 30 Apr 2021 17:56:29 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 09203153A; Fri, 30 Apr 2021 13:56:14 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Fri, 30 Apr 2021 13:56:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=krTMbtU7rGBRF/ENbA9TCJGCQ2ZSnQuq/O57Jhtv6zo=; b=L4vUaCO+ bnqvkheT0G+7SzZ5nWg5Rhg9i1uJocDlD6yQV8gDDWaZ2cQAwUJF7rOwg7r3H1ow j+9SJsb6ZuBKkh+yJXGw40BwXX9u5yHH/W0XRn5U1srm407tv3DSkcNzOSVVWGMF GZOF8uoC3LohLyvloxdUgmioqEw2t2BUTmA4nW4rTf1bHvJRU+fQOYG1Xe0SlpHa lEiOSEOIrSgFqarY2bLUqxNVs31VEYijctE0D6FFhZEsVt9hYVK/1YCc6kjZ6GcB J9WNDqQT0HRZ0gPnLp2wdC5FnyXBAq8QJVbcghIw6Ba7PS+kesAb0K6rt41Gw6bh 8YSCkG4DMOLP9Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvddviedguddvtdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepfffhvffukfggtggugfgjfgesth ekredttderudenucfhrhhomheptehlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghr rhgvsegrlhhvhhdrnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepueffhfejie euueffgedvudekgeeltdeviedvieeufeeiieevgfetheeufeethedvnecukfhppeduledt rdelhedrudelrdelheenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Received: from perhan.alvh.no-ip.org (unknown [190.95.19.95]) by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 30 Apr 2021 13:56:13 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 854C82A0A6C; Fri, 30 Apr 2021 13:56:11 -0400 (-04) Date: Fri, 30 Apr 2021 13:56:11 -0400 From: Alvaro Herrera To: David Rowley Cc: alex.stilwell@macquarie.com, PostgreSQL mailing lists Subject: Re: BUG #16908: Postgres (12) allows you (re)-attach partitions that violate Foreign Key constraints? Message-ID: <20210430175611.GA6249@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-Host-Lookup-Failed: Reverse DNS lookup failed for 64.147.123.21 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2021-Mar-02, David Rowley wrote: > Here's a simplifed test case: > > create table p(a int primary key) partition by list(a); > create table p1 partition of p for values in(1); > create table z(a int primary key references p) partition by list(a); > create table z1 partition of z for values in(1); > insert into p values(1); > insert into z values(1); > alter table z detach partition z1; > alter table p detach partition p1; > alter table z attach partition z1 for values in(1); -- shouldn't be allowed. Hmm, I agree that this is misbehaving. However ... > But it is allowed as CloneFkReferencing() calls > tryAttachPartitionForeignKey() which checks for an existing foreign > key to the table. It happens to find one. > > \d z1 > Table "public.z1" > Column | Type | Collation | Nullable | Default > --------+---------+-----------+----------+--------- > a | integer | | not null | > Indexes: > "z1_pkey" PRIMARY KEY, btree (a) > Foreign-key constraints: > "z_a_fkey" FOREIGN KEY (a) REFERENCES p(a) > > I imagine that this foreign key should have been removed when z1 was > detached from z. If it's not then since when we detach p1 from p, we > can only check currently attached partitions on referencing tables to > see if there would be any FK violations. ... I'm not sure that we need to remove the FK from 'z1'; we know well enough that the constraint still stands. The problem IMO occurs when we detach p1 -- that's the step that causes the FK go wrong. I think that DETACH step should throw the error that there are tables referencing the values in the partition. This works correctly when 'z' is a plain table, so what is different regarding the constraint in 'z1' (which is now a plain table)? I'll have a look. -- Álvaro Herrera 39°49'30"S 73°17'W