Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eyLya-0005Lw-8X for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Mar 2018 18:27:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eyLyW-00025P-W3 for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Mar 2018 18:27:08 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eyLyW-00025G-9N for pgsql-hackers@lists.postgresql.org; Tue, 20 Mar 2018 18:27:08 +0000 Received: from new1-smtp.messagingengine.com ([66.111.4.221]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eyLyS-0005HF-Dv for pgsql-hackers@lists.postgresql.org; Tue, 20 Mar 2018 18:27:07 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 24CF21BA1; Tue, 20 Mar 2018 14:27:03 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute4.internal (MEProxy); Tue, 20 Mar 2018 14:27:03 -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-sender:x-me-sender:x-sasl-enc; s=fm2; bh=7Pzoqmwv8zY19lfQw C6CHgOvfoRXK0rigknIvTYNVDE=; b=anp+Jb4xd+EClFxgZi5p0ZjhdYJuG2pjU kXRICmkZwXiT2hdLuqJORKCDl9+LJJmRQeRHfebwU0xk08PkWZ4BFss9qGYP/SFm QXSHWY7pkGbQYD5TKg5P+WfNKI2UznRZibkRCMSDg8WRxL1E2OH9Ram/TJo/mGCt L54fxMXi5il2yqNAEDaapD1JzcgdrkCMPWK6+j4IQbQN8k4wv5h0Y+2W72P+s8KS 7t7OfP4uRqByRNRKFWXWN2AbrTd+J2zK+7sbKlxONiavQk2lc7VEBAh8gksTx+NR 9UaxWaIRFdjMuLFSzOrDnQjF7RreZ2wX0QNGy9LkN3CS/i1zXVnhg== X-ME-Sender: Received: from alvin.alvh.no-ip.org (unknown [179.56.29.253]) by mail.messagingengine.com (Postfix) with ESMTPA id 0E8AA7E13C; Tue, 20 Mar 2018 14:27:02 -0400 (EDT) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id D2F717BF; Tue, 20 Mar 2018 15:26:59 -0300 (-03) Date: Tue, 20 Mar 2018 15:26:59 -0300 From: Alvaro Herrera To: pgsql-hackers@lists.postgresql.org Cc: Jeevan Ladhe , Robert Haas Subject: Re: pgsql: Fix CommandCounterIncrement in partition-related DDL Message-ID: <20180320182659.nyzn3vqtjbbtfgwq@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="umb6f43xxkignn5x" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20180320145450.p33pa2udtcwngkeb@alvherre.pgsql> User-Agent: NeoMutt/20170306-137-4415bd-dirty (1.8.0) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --umb6f43xxkignn5x Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Alvaro Herrera wrote: > Alvaro Herrera wrote: > > Fix CommandCounterIncrement in partition-related DDL > > Hmm, Prion seems unhappy about this. Looking Here's a patch that seems to fix the problem, and generally looks sane to me. The previous arrangements to avoid CCI seem brittle: apparently we were forced to do StorePartitionBounds() and immediately update_default_partition_oid() without CCI in between, or various things went nuts ("unexpected partdefid"). With this patch, what we do is we call update_default_partition_oid *within* StorePartitionBounds without an intervening CCI, which seems to achieve the same result -- namely that when the relcache entry is rebuilt, it doesn't end up incomplete. The two places that previously called update_default_partition_oid with a non-zero default partition OID no longer call it, since they were storing bounds. The only places that remain outside of StorePartitionBounds call it with InvalidOid (during relation drop, and during partition detach). I also remove a crock in RelationBuildPartitionDesc to return empty when "key is null" (i.e. pg_class entry was there but not pg_partitioned_table), which makes no sense -- except if you're building the cache untimely, which apparently is what was happening. If you make sure the pg_partitioned_table entry is present before making the pg_class entry visible, then this should not be necessary. heap_drop_with_catalog contains a copy-paste failure, fixed also in this patch. I wonder about adding a syscache callback so that when an item in pg_partitioned_table is invalidated, the relcache entry for partrelid entry in pg_class is invalidated also. I can't find any precedent for anything similar, though, and there doesn't seem to be any convenient place to do it, either. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --umb6f43xxkignn5x Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: attachment; filename="0001-Fix-relcache-handling-of-the-default-partition.patch" Content-Transfer-Encoding: 8bit diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 0497332e9d..a049234390 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -1769,7 +1769,8 @@ heap_drop_with_catalog(Oid relid) * could attempt to access the just-dropped relation as its partition. We * must therefore take a table lock strong enough to prevent all queries * on the table from proceeding until we commit and send out a - * shared-cache-inval notice that will make them update their index lists. + * shared-cache-inval notice that will make them update their partition + * descriptors. */ tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid)); if (!HeapTupleIsValid(tuple)) @@ -3247,6 +3248,9 @@ RemovePartitionKeyByRelId(Oid relid) * Update pg_class tuple of rel to store the partition bound and set * relispartition to true * + * If this is the default partition, also update the default partition OID in + * pg_partitioned_table. + * * Also, invalidate the parent's relcache, so that the next rebuild will load * the new partition's info into its partition descriptor.  If there is a * default partition, we must invalidate its relcache entry as well. @@ -3298,7 +3302,17 @@ StorePartitionBound(Relation rel, Relation parent, PartitionBoundSpec *bound) heap_freetuple(newtuple); heap_close(classRel, RowExclusiveLock); - /* Make update visible */ + /* + * If we're storing bounds for the default partition, update + * pg_partitioned_table too. This must happen before CCI, to avoid risk + * of constructing a partition descriptor lacking the default partition + * OID. + */ + if (bound->is_default) + update_default_partition_oid(RelationGetRelid(parent), + RelationGetRelid(rel)); + + /* Make these updates visible */ CommandCounterIncrement(); /* diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c index 786c05df73..53855f5088 100644 --- a/src/backend/catalog/partition.c +++ b/src/backend/catalog/partition.c @@ -227,13 +227,6 @@ RelationBuildPartitionDesc(Relation rel) /* Range partitioning specific */ PartitionRangeBound **rbounds = NULL; - /* - * The following could happen in situations where rel has a pg_class entry - * but not the pg_partitioned_table entry yet. - */ - if (key == NULL) - return; - /* Get partition oids from pg_inherits */ inhoids = find_inheritance_children(RelationGetRelid(rel), NoLock); diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 8f83aa4675..c4cf0abadc 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -859,10 +859,6 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, /* Update the pg_class entry. */ StorePartitionBound(rel, parent, bound); - /* Update the default partition oid */ - if (bound->is_default) - update_default_partition_oid(RelationGetRelid(parent), relationId); - heap_close(parent, NoLock); } @@ -14020,11 +14016,6 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd) /* OK to create inheritance. Rest of the checks performed there */ CreateInheritance(attachrel, rel); - /* Update the default partition oid */ - if (cmd->bound->is_default) - update_default_partition_oid(RelationGetRelid(rel), - RelationGetRelid(attachrel)); - /* * Check that the new partition's bound is valid and does not overlap any * of existing partitions of the parent - note that it does not return on -- 2.11.0 --umb6f43xxkignn5x--