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 1lWLcg-0007i4-Qn for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Apr 2021 16:10:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lWLcf-0006Ft-Pr for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Apr 2021 16:10:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWLcf-0006Fm-1r for pgsql-hackers@lists.postgresql.org; Tue, 13 Apr 2021 16:10:41 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWLcb-0005M2-OB for pgsql-hackers@lists.postgresql.org; Tue, 13 Apr 2021 16:10:40 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id F13865C01E0; Tue, 13 Apr 2021 12:10:33 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Tue, 13 Apr 2021 12:10:33 -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=buXP/KPrjyaY8/aocdv1tkojUdq8UkiiAHSYq+XW4Jc=; b=CHqeZy0Y 2J9Mnbmpg7KR3blgZmi8hVL8Ubm7OFj8+qeOfxwyxGcseN2BQjjmtGIpg9266vbi dw1Y4KQ8WXjJyAxVXSsW1W+hmYqGjyO0BF9Bujwfmrh+n2mPtICBAcm9jH1WP9MQ fuDoJc0VO71OuYYpJ0dXTiPDfAhyXceKFW0R4AMcRwWumBE11r2UyAhlczZbvmRo fUHLsoZYJQwNTR13gcTTJflP7lRA5jMAdZVNLWiZK4K1/IiGWpIlrpbrvc4lQz1q ppq9E3E11s6/H4ofrCtKqCnaDZMf3II3sjqKBTZYmRWRwOHpMQevk6fsU9WSbii/ vJ9dKm4xjLdZ5A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudekledgleekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfggtggugfgjfgesthekredttderudenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepueffhfejieeuueffgedvudekgeeltdeviedvieeufeei ieevgfetheeufeethedvnecukfhppeduledtrdelhedrudelrdelheenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghl vhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Received: from perhan.alvh.no-ip.org (unknown [190.95.19.95]) by mail.messagingengine.com (Postfix) with ESMTPA id 13799240054; Tue, 13 Apr 2021 12:10:33 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 5DDB52A0D51; Tue, 13 Apr 2021 12:10:30 -0400 (-04) Date: Tue, 13 Apr 2021 12:10:30 -0400 From: Alvaro Herrera To: Amit Langote Cc: Justin Pryzby , Tom Lane , PostgreSQL Hackers Subject: Re: ALTER TABLE .. DETACH PARTITION CONCURRENTLY Message-ID: <20210413161030.GA10776@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) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2021-Apr-13, Amit Langote wrote: > Actually it occurred to me this morning that CLOBBER_CACHE_ALWAYS is > what exposed this problem on this animal (not sure if other such > animals did too though). With CLOBBER_CACHE_ALWAYS, a PartitionDesc > will be built afresh on most uses. In this particular case, the RI > query executed by the insert has to build a new one (for d4_primary), > correctly excluding the detach-pending partition (d4_primary1) per the > snapshot with which it is run. In normal builds, it would reuse the > one built by an earlier query in the transaction, which does include > the detach-pending partition, thus allowing the insert trying to > insert a row referencing that partition to succeed. There is a > provision in RelationGetPartitionDesc() to rebuild if any > detach-pending partitions in the existing copy of PartitionDesc are > not to be seen by the current query, but a bug mentioned in my earlier > reply prevents that from kicking in. Right -- that explanation makes perfect sense: the problem stems from the fact that the cached copy of the partition descriptor is not valid depending on the visibility of detached partitions for the operation that requests the descriptor. I think your patch is a critical part to a complete solution, but one thing is missing: we don't actually know that the detached partitions we see now are the same detached partitions we saw a moment ago. After all, a partitioned table can have several partitions in the process of being detached; so if you just go with the boolean "does it have any detached or not" bit, you could end up with a descriptor that doesn't include/ignore *all* detached partitions, just the older one(s). I think you could fix this aspect easily by decreeing that you can only have only one partition-being-detached at one point. So if you try to DETACH CONCURRENTLY and there's another one in that state, raise an error. Maybe for simplicity we should do that anyway. But I think there's another hidden assumption in your patch, which is that the descriptor is rebuilt every now and then *anyway* because the flag for detached flips between parser and executor, and because we send invalidation messages for each detach. I don't think we would ever change things that would break this flipping (it sounds like planner and executor necessarily have to be doing things differently all the time), but it seems fragile as heck. I would feel much safer if we just avoided caching the wrong thing ... or perhaps keep a separate cache entry (one descriptor including detached, another one not), to avoid pointless rebuilds. -- Álvaro Herrera Valdivia, Chile