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 1lZ11A-0001Gx-QW for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Apr 2021 00:47:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lZ118-0000Dh-G5 for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Apr 2021 00:46:58 +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 1lZ118-0000Da-8S for pgsql-hackers@lists.postgresql.org; Wed, 21 Apr 2021 00:46:58 +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 1lZ115-0008Gq-Ib for pgsql-hackers@lists.postgresql.org; Wed, 21 Apr 2021 00:46:57 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 98D0B5C01B7; Tue, 20 Apr 2021 20:46:53 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Tue, 20 Apr 2021 20:46:53 -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=9Bql3dbtoRBVuR38xISsr/4u4yG1jn1TsSfWP9SybiQ=; b=QdYsplSB RhpAXlkoNbcv27oxlb9U8jFWaheW06BEhgLEeMvqYO+YtS4q8R4yweU6ZyG/Eepo 7TH/bGOfbyz2KcgiL1oH7JqM8ytuFbwjxeKYpzNme2op57POljvlufgVoReDOcKN CZgSwU3bwQRa34HneGUYGtacjEPbifghkcOy1+BD6ncV9RfFmCBlsXr9dmjWnVcw rU/spgobbeMnLWh3Q/i81KinQnEdO2wVOsDuDuFIF8DTH0A9b28miNvlLrUoUYr+ XVQrcbfArhHZYFkdVl6KWZsW3rkhF3GA4whQ1rGbfBAXGPi7uSJnL9xG4YtjvbqF Z8ASUo4oFF1gvA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvddtjedgfeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfggtggugfgjfgesmhekreertderudenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvdevvdfgffehkefhvdelfedvteefieeuffeigeeivefg hedtfedtleefgffgtdfgnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenuc fkphepudeltddrleehrdduledrleehnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Received: from perhan.alvh.no-ip.org (unknown [190.95.19.95]) by mail.messagingengine.com (Postfix) with ESMTPA id 4871B240066; Tue, 20 Apr 2021 20:46:52 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id A92BE2A099A; Tue, 20 Apr 2021 20:46:49 -0400 (-04) Date: Tue, 20 Apr 2021 20:46:49 -0400 From: Alvaro Herrera To: Amit Langote Cc: Justin Pryzby , Tom Lane , PostgreSQL Hackers Subject: Re: ALTER TABLE .. DETACH PARTITION CONCURRENTLY Message-ID: <20210421004649.GA1951@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="7AUc2qLy4jB3hD7Z" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20210420224107.GA11029@alvherre.pgsql> 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 --7AUc2qLy4jB3hD7Z Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Actually I had a silly bug in the version that attempted to cache a partdesc that omits detached partitions. This one, while not fully baked, seems to work correctly (on top of the previous one). The thing that I don't fully understand is why we have to require to have built the regular one first. -- Álvaro Herrera 39°49'30"S 73°17'W "This is what I like so much about PostgreSQL. Most of the surprises are of the "oh wow! That's cool" Not the "oh shit!" kind. :)" Scott Marlowe, http://archives.postgresql.org/pgsql-admin/2008-10/msg00152.php --7AUc2qLy4jB3hD7Z Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-separately-cache-a-partdesc-that-omits-detached-part.patch" From b45e9db9cd08282f7a7fabcef944880cc3a3d415 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Tue, 20 Apr 2021 20:42:30 -0400 Subject: [PATCH] separately cache a partdesc that omits detached partitions, too --- src/backend/partitioning/partdesc.c | 21 +++++++++++++++++++-- src/backend/utils/cache/relcache.c | 4 ++++ src/include/utils/rel.h | 1 + 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/backend/partitioning/partdesc.c b/src/backend/partitioning/partdesc.c index 7d019a72be..f245cbd664 100644 --- a/src/backend/partitioning/partdesc.c +++ b/src/backend/partitioning/partdesc.c @@ -67,10 +67,25 @@ RelationGetPartitionDesc(Relation rel, bool include_detached) { Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE); + /* + * The common case is that there are no partitions pending detach and + * that the appropriate descriptor has already been built; use that. + */ if (likely(rel->rd_partdesc && (!rel->rd_partdesc->detached_exist || include_detached))) return rel->rd_partdesc; + /* + * If we're asked for a descriptor with detached partitions omitted, and + * we have already built it, return that. + */ + if (!include_detached && + rel->rd_partdesc && + rel->rd_partdesc->detached_exist && + rel->rd_partdesc_nodetached) + return rel->rd_partdesc_nodetached; + + /* Otherwise build one afresh and store it for next time */ return RelationBuildPartitionDesc(rel, include_detached); } @@ -278,9 +293,11 @@ RelationBuildPartitionDesc(Relation rel, bool include_detached) MemoryContextSetParent(rel->rd_pdcxt, new_pdcxt); rel->rd_pdcxt = new_pdcxt; - /* Store it into relcache, but only if no detached partitions exist */ - if (!detached_exist) + /* Store it into the appropriate member of the relcache entry */ + if (!detached_exist || include_detached) rel->rd_partdesc = partdesc; + else + rel->rd_partdesc_nodetached = partdesc; return partdesc; } diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index 29702d6eab..87a7613584 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -1157,6 +1157,7 @@ RelationBuildDesc(Oid targetRelId, bool insertIt) relation->rd_partkey = NULL; relation->rd_partkeycxt = NULL; relation->rd_partdesc = NULL; + relation->rd_partdesc_nodetached = NULL; relation->rd_pdcxt = NULL; relation->rd_partcheck = NIL; relation->rd_partcheckvalid = false; @@ -2672,12 +2673,14 @@ RelationClearRelation(Relation relation, bool rebuild) * newrel. */ relation->rd_partdesc = NULL; /* ensure rd_partdesc is invalid */ + relation->rd_partdesc_nodetached = NULL; if (relation->rd_pdcxt != NULL) /* probably never happens */ MemoryContextSetParent(newrel->rd_pdcxt, relation->rd_pdcxt); else relation->rd_pdcxt = newrel->rd_pdcxt; /* drop newrel's pointers so we don't destroy it below */ newrel->rd_partdesc = NULL; + newrel->rd_partdesc_nodetached = NULL; newrel->rd_pdcxt = NULL; } @@ -5942,6 +5945,7 @@ load_relcache_init_file(bool shared) rel->rd_partkey = NULL; rel->rd_partkeycxt = NULL; rel->rd_partdesc = NULL; + rel->rd_partdesc_nodetached = NULL; rel->rd_pdcxt = NULL; rel->rd_partcheck = NIL; rel->rd_partcheckvalid = false; diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h index 34e25eb597..3d480a3ab2 100644 --- a/src/include/utils/rel.h +++ b/src/include/utils/rel.h @@ -127,6 +127,7 @@ typedef struct RelationData /* data managed by RelationGetPartitionDesc: */ PartitionDesc rd_partdesc; /* partition descriptor, or NULL */ + PartitionDesc rd_partdesc_nodetached; /* partition descriptor omitting detached partitions */ MemoryContext rd_pdcxt; /* private context for rd_partdesc, if any */ /* data managed by RelationGetPartitionQual: */ -- 2.20.1 --7AUc2qLy4jB3hD7Z--