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 1gYdJv-0004fK-6D for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Dec 2018 20:47:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYdJt-0001k7-Ie for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Dec 2018 20:47:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYdJt-0001k0-BM for pgsql-hackers@lists.postgresql.org; Sun, 16 Dec 2018 20:47:25 +0000 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYdJp-0008Ul-Lv for pgsql-hackers@lists.postgresql.org; Sun, 16 Dec 2018 20:47:24 +0000 Received: by mail-qt1-x844.google.com with SMTP id l12so10088746qtf.8 for ; Sun, 16 Dec 2018 12:47:21 -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=Q/LdfqrQD5t4lFgVfcrU7q+EvD7LjX2NIOUyMqYtOZM=; b=l6Mi/pTZlWk0pKQs/rru+vFld16HJ4quIO4YfjkEMLPZ8oZ26euw/gqwF1B8tyL2Gn Q+63rYOfG6PBgw4NK7JJSdXA7PN3QiNIu+SK/4TkD9AIqlR80X8h0uXjSBzqMIEsRMbZ 1difxROMRO9sq/aQFdt/AT0ocQPekIu8y1pvQbcrKCiR7VzYbDXj++i9G3I40dP4iPaY JBJ+s7IBZNoAsyfLvjy+qfWmjIsLWyClqatayOpKkUosZDmyTd7oNnoCug11t7cLMFyY 3QKYKtfYmGzh/pXmgengZUCu4yer1m4xmKaE+4UnX0nFWdkUWTzc9bTk5OBDQrYZegvH CpfQ== 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=Q/LdfqrQD5t4lFgVfcrU7q+EvD7LjX2NIOUyMqYtOZM=; b=ea9Bz8zPLJkr00OO4YxwbbXdJ9fiGP5bLpwW4368vVPo7qCLqjx9rrbsFz0R6Wj/l+ bpiTPORNGQTmLPflMxwXVbo7UKOZZTgqRHo19PQ3HAgh3VoBbV1bFHo726Yvcd6uz4EP 906Bv5lyTGBfl021O+c9CRlKl+GaywTefO0Lcygev8OKTAjEHr8x0tZ3ySloT434swdd 5CKz5qA9yTP2Mf7D6tOcw0rGY1H8cCr6uhPNds7M5/woqMjIS0MVjdbI5CduRvjDW3C/ z+oiCZuKMooSHy8wQiFlS4ia3j8nQSOttHlMkzUyoA4zt5LHSDQVOQcKRJBnnn98V81U EgsQ== X-Gm-Message-State: AA+aEWZaIJBPTPJoJdybr8159altEgy+7Bv7Qh5CFBOXRmwuwjl3EC0Q HAYq9cMDuQThI98oIwOmQQoeuZLRgA3OUqNqMUKbHOb/EbxbNy3rcm2XkblcI+08J0tewxfVGvj +S9YehaLmKfNo9HyciHAttwTrOJy0l/DzanRPgW9uvWPQCGkgoHG21b75TEX8EHwdUMFmz/UcYZ 7MbRrzpzjlkprWZA== X-Google-Smtp-Source: AFSGD/UT7BTfvPCp/qj8fT9X6BdjOljxsEJDWWFkPoKEz+V7mB6+LRhCSfY6Mr+Uq5E34AQ8zFRX6Q== X-Received: by 2002:ac8:101a:: with SMTP id z26mr11250864qti.184.1544993239411; Sun, 16 Dec 2018 12:47:19 -0800 (PST) Received: from alvin.alvh.no-ip.org ([179.57.110.234]) by smtp.gmail.com with ESMTPSA id p47sm7625561qta.36.2018.12.16.12.47.18 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 16 Dec 2018 12:47:18 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id A4AE26FB; Sun, 16 Dec 2018 17:47:16 -0300 (-03) Date: Sun, 16 Dec 2018 17:47:16 -0300 From: Alvaro Herrera To: Michael Paquier Cc: Pg Hackers Subject: Re: don't create storage when unnecessary Message-ID: <20181216204716.jddzijk3fb2dpdk4@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ddym5aqvwjcagxxk" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20181207000617.GM2407@paquier.xyz> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --ddym5aqvwjcagxxk Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2018-Dec-07, Michael Paquier wrote: > A macro makes sense to control that. I added Ashutosh's RELKIND_HAS_STORAGE, but renamed it to RELKIND_CAN_HAVE_STORAGE, because some of the relkinds can be mapped and thus would have relfilenode set to 0. I think this is a bit misleading either way. > Now I have to admit that I don't > like your solution. Wouldn't it be cleaner to assign InvalidOid to > relfilenode in such cases? The callers of heap_create would need to be > made smarter when they now pass down a relfilenode (looking at you, > DefineIndex!), but that seems way more consistent to me. I don't follow. When a relfilenode is passed by callers, they indicate that the storage has already been created. Contrariwise, when a relation kind that *does* have storage but is not yet created, they pass InvalidOid as relfilenode, and heap_create is in charge of creating storage. Maybe I'm not quite seeing what problem you mean. Or I could add a separate boolean, but that seems pointless. Another possible improvement is to remove the create_storage boolean > Some tests would also be welcome. Added a test in sanity_check.sql that there's no relation with the relkinds that aren't supposed to have storage. Without the code fix it fails in current regression database, but in the failure result set there isn't any relation of kinds 'p' or 'I', so this isn't a terribly comprehensive test -- the query runs too early in the regression sequence. I'm not sure it's worth bothering further. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --ddym5aqvwjcagxxk Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-don-t-create-storage-when-not-necessary.patch" diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 11debaa780..8aeddfa3d3 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -324,17 +324,13 @@ 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: case RELKIND_PARTITIONED_TABLE: - create_storage = false; /* * Force reltablespace to zero if the relation has no physical @@ -343,16 +339,7 @@ heap_create(const char *relname, reltablespace = InvalidOid; break; - case RELKIND_PARTITIONED_INDEX: - /* - * Preserve tablespace so that it's used as tablespace for indexes - * on future partitions. - */ - create_storage = false; - break; - case RELKIND_SEQUENCE: - create_storage = true; /* * Force reltablespace to zero for sequences, since we don't @@ -361,19 +348,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..911686e6bb 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/catalog/pg_class.h b/src/include/catalog/pg_class.h index 84e63c6d06..321c9a1973 100644 --- a/src/include/catalog/pg_class.h +++ b/src/include/catalog/pg_class.h @@ -122,6 +122,19 @@ typedef FormData_pg_class *Form_pg_class; */ #define REPLICA_IDENTITY_INDEX 'i' +/* + * Relation kinds that have physical storage. These relations normally have + * relfilenode set to non-zero, but it can also be zero if the relation is + * mapped. + */ +#define RELKIND_CAN_HAVE_STORAGE(relkind) \ + ((relkind) == RELKIND_RELATION || \ + (relkind) == RELKIND_INDEX || \ + (relkind) == RELKIND_SEQUENCE || \ + (relkind) == RELKIND_TOASTVALUE || \ + (relkind) == RELKIND_MATVIEW) + + #endif /* EXPOSE_TO_CLIENT_CODE */ #endif /* PG_CLASS_H */ 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; --ddym5aqvwjcagxxk--