Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYynA-000893-2m for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Dec 2018 19:43:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYyn8-0004nk-Lx for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Dec 2018 19:43:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYyn8-0004nc-BQ for pgsql-hackers@lists.postgresql.org; Mon, 17 Dec 2018 19:43:02 +0000 Received: from mail-qk1-x733.google.com ([2607:f8b0:4864:20::733]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYyn0-0005Sf-Uv for pgsql-hackers@lists.postgresql.org; Mon, 17 Dec 2018 19:43:00 +0000 Received: by mail-qk1-x733.google.com with SMTP id 189so8065753qkj.8 for ; Mon, 17 Dec 2018 11:42:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=CpfSKwdqIylFsfx3lpYvA0CVf7dt369BZhVqh3Pg/74=; b=poB7I5VjWWmVATRUDpRta1FuOt2zPHnNBN45/5WoA5PvA2V55F+8PN7gZvS9+vmlZK YKyz6ehUqR0vSzj/BK2QW+nL7Dx9DImK2oYko4i8+GK8hQnVgvDXAk/vfAj/nyPLwfad YutiwqV29FBznaptkjr6VymoWP7G5PfZJQyhLBy5N/FiD6ANbJFuLVHzkIxnizb9+LOC FAigZwuqkoKNcaTU2J4k2A7ti8ugVUnSo+ZKNeM00xaMOQK2s2MQDQvwfFAxOyFO5uWx +Fyl5LVPwEXWOT9Tgw2LaUjplN2ESwwsLxPd+FIbHatlyqoDxH0Zi3AhQ9cSlqqc2t7M 1kyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=CpfSKwdqIylFsfx3lpYvA0CVf7dt369BZhVqh3Pg/74=; b=g/i1zBDDBmMe/Mz1xz2nIzoFkqNmTwOebLb5dLJ9xDyBlq7Lx1PbTYvj5l9QnTlwhV gTKjkt1bWLph5SZdrT44R2JHZ3JfG6HZNxyjPJJKQbMRZGEybv8mv/A3b5e3ay6taiAr WIGIAlQ8iIpHKpEowGD2AKZIevcmAlHHDs1mk2NBI9t7obV59yPPQH3/iLhZc2bWsCYc fP4IekN6ZEXmqVz9+uD0SrPy6UIG7xOOr+pggMI2sMgONoq2AkLPcMvvZil4LIuv8vOa b4VzeDKugUyMuZIRhoY2/rW6UtsKeuGNiibH4Yb4nB8Xo7EjY0IfNGI3EkF7S46CE+7y siOQ== X-Gm-Message-State: AA+aEWYeFQ9atozbctk9UZT3j5kIaho4H+jRt+8LPP1u5aTVDlGDgwS9 Qe/W27ZkZOnfklf5NP8HZg5ksTFAU41neWyWSIhl9lvJeGTx7/hca24IAUrePUGtm/3kjNTaO71 uGoGCF9ZfatdqXYCmbX/GPWp44Z1qhQNwfRJHRDK2lMdhgLfXfv1G5/aCKx3EQ3zpNdeJRqRQ5Y duMQ0TXY5GHF5yTQdT X-Google-Smtp-Source: AFSGD/WGWaHIzxzo1gm/xXJT0EHuc6spMyKRICbb6n2rJbFS1CDbG2WU6kUdHYQUQTe5BaVvt5RNEw== X-Received: by 2002:a37:bf42:: with SMTP id p63mr13139985qkf.287.1545075773638; Mon, 17 Dec 2018 11:42:53 -0800 (PST) Received: from alvin.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id m79sm8196752qkm.58.2018.12.17.11.42.52 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 17 Dec 2018 11:42:52 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id D2454958; Mon, 17 Dec 2018 16:42:50 -0300 (-03) Date: Mon, 17 Dec 2018 16:42:50 -0300 From: Alvaro Herrera To: Michael Paquier Cc: Pg Hackers Subject: Re: don't create storage when unnecessary Message-ID: <20181217194250.aa3v4hx5e6zc2wzb@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="xi4je5yf6ujbc2ip" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20181216204716.jddzijk3fb2dpdk4@alvherre.pgsql> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --xi4je5yf6ujbc2ip Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Rebased over today's conflicting commits. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --xi4je5yf6ujbc2ip Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-don-t-create-storage-when-not-necessary.patch" diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 4d5b82aaa9..b128959cb7 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -324,16 +324,12 @@ heap_create(const char *relname, get_namespace_name(relnamespace), relname), errdetail("System catalog modifications are currently disallowed."))); - /* - * Decide if we need storage or not, and handle a couple other special - * cases for particular relkinds. - */ + /* Handle reltablespace for specific relkinds. */ switch (relkind) { case RELKIND_VIEW: case RELKIND_COMPOSITE_TYPE: case RELKIND_FOREIGN_TABLE: - create_storage = false; /* * Force reltablespace to zero if the relation has no physical @@ -342,17 +338,7 @@ heap_create(const char *relname, reltablespace = InvalidOid; break; - case RELKIND_PARTITIONED_TABLE: - case RELKIND_PARTITIONED_INDEX: - /* - * For partitioned tables and indexes, preserve tablespace so that - * it's used as the tablespace for future partitions. - */ - create_storage = false; - break; - case RELKIND_SEQUENCE: - create_storage = true; /* * Force reltablespace to zero for sequences, since we don't @@ -361,19 +347,21 @@ heap_create(const char *relname, reltablespace = InvalidOid; break; default: - create_storage = true; break; } /* - * Unless otherwise requested, the physical ID (relfilenode) is initially - * the same as the logical ID (OID). When the caller did specify a - * relfilenode, it already exists; do not attempt to create it. + * Decide whether to create storage. If caller passed a valid relfilenode, + * storage is already created, so don't do it here. Also don't create it + * for relkinds without physical storage. */ - if (OidIsValid(relfilenode)) + if (!RELKIND_CAN_HAVE_STORAGE(relkind) || OidIsValid(relfilenode)) create_storage = false; else + { + create_storage = true; relfilenode = relid; + } /* * Never allow a pg_class entry to explicitly specify the database's diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index c3071db1cd..a4c8a9e385 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -1253,6 +1253,10 @@ RelationBuildDesc(Oid targetRelId, bool insertIt) static void RelationInitPhysicalAddr(Relation relation) { + /* these relations kinds never have storage */ + if (!RELKIND_CAN_HAVE_STORAGE(relation->rd_rel->relkind)) + return; + if (relation->rd_rel->reltablespace) relation->rd_node.spcNode = relation->rd_rel->reltablespace; else diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h index 2217081dcc..393edd33d2 100644 --- a/src/include/utils/rel.h +++ b/src/include/utils/rel.h @@ -450,13 +450,12 @@ typedef struct ViewOptions /* * RelationIsMapped - * True if the relation uses the relfilenode map. - * - * NB: this is only meaningful for relkinds that have storage, else it - * will misleadingly say "true". + * True if the relation uses the relfilenode map. Note multiple eval + * of argument! */ #define RelationIsMapped(relation) \ - ((relation)->rd_rel->relfilenode == InvalidOid) + (RELKIND_CAN_HAVE_STORAGE((relation)->rd_rel->relkind) && \ + ((relation)->rd_rel->relfilenode == InvalidOid)) /* * RelationOpenSmgr diff --git a/src/test/regress/expected/sanity_check.out b/src/test/regress/expected/sanity_check.out index 009a89fc1a..89537bcfcf 100644 --- a/src/test/regress/expected/sanity_check.out +++ b/src/test/regress/expected/sanity_check.out @@ -224,3 +224,12 @@ SELECT relname, nspname ---------+--------- (0 rows) +-- check that relations without storage don't have relfilenode +SELECT relname, relkind + FROM pg_class + WHERE relkind IN ('v', 'c', 'f', 'p', 'I') + AND relfilenode <> 0; + relname | relkind +---------+--------- +(0 rows) + diff --git a/src/test/regress/sql/sanity_check.sql b/src/test/regress/sql/sanity_check.sql index a2feebc91b..a4ec00309f 100644 --- a/src/test/regress/sql/sanity_check.sql +++ b/src/test/regress/sql/sanity_check.sql @@ -31,3 +31,9 @@ SELECT relname, nspname AND NOT EXISTS (SELECT 1 FROM pg_index i WHERE indrelid = c.oid AND indkey[0] = a.attnum AND indnatts = 1 AND indisunique AND indimmediate); + +-- check that relations without storage don't have relfilenode +SELECT relname, relkind + FROM pg_class + WHERE relkind IN ('v', 'c', 'f', 'p', 'I') + AND relfilenode <> 0; --xi4je5yf6ujbc2ip--