From 9be1c99e5eb432aaaab56401a1594d13edfa9077 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Wed, 13 Jun 2012 14:58:04 -0400 Subject: [PATCH] Rejigger things so that indexes don't go through heap_create(). --- src/backend/bootstrap/bootparse.y | 1 - src/backend/catalog/heap.c | 33 ++++---------------------- src/backend/catalog/index.c | 37 +++++++++++++++++++----------- src/backend/utils/cache/relcache.c | 13 +++++++++++ src/include/catalog/heap.h | 1 - 5 files changed, 40 insertions(+), 45 deletions(-) diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y index 18f0add852..9655bfd2b0 100644 --- a/src/backend/bootstrap/bootparse.y +++ b/src/backend/bootstrap/bootparse.y @@ -217,7 +217,6 @@ Boot_CreateStmt: PG_CATALOG_NAMESPACE, shared_relation ? GLOBALTABLESPACE_OID : 0, $3, - InvalidOid, tupdesc, RELKIND_RELATION, RELPERSISTENCE_PERMANENT, diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index c91df90038..cb162810f7 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -224,10 +224,6 @@ SystemAttributeByName(const char *attname, bool relhasoids) /* ---------------------------------------------------------------- * heap_create - Create an uncataloged heap relation * - * Note API change: the caller must now always provide the OID - * to use for the relation. The relfilenode may (and, normally, - * should) be left unspecified. - * * rel->rd_rel is initialized by RelationBuildLocalRelation, * and is mostly zeroes at return. * ---------------------------------------------------------------- @@ -237,7 +233,6 @@ heap_create(const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, - Oid relfilenode, TupleDesc tupDesc, char relkind, char relpersistence, @@ -250,6 +245,9 @@ heap_create(const char *relname, /* The caller must have provided an OID for the relation. */ Assert(OidIsValid(relid)); + /* Use index_create() for indexes. */ + Assert(relkind != RELKIND_INDEX); + /* * Decide if we need storage or not, and handle a couple other special * cases for particular relkinds. @@ -281,28 +279,6 @@ heap_create(const char *relname, 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. - */ - if (OidIsValid(relfilenode)) - create_storage = false; - else - relfilenode = relid; - - /* - * Never allow a pg_class entry to explicitly specify the database's - * default tablespace in reltablespace; force it to zero instead. This - * ensures that if the database is cloned with a different default - * tablespace, the pg_class entry will still match where CREATE DATABASE - * will put the physically copied relation. - * - * Yes, this is a bit of a hack. - */ - if (reltablespace == MyDatabaseTableSpace) - reltablespace = InvalidOid; - /* * build the relcache entry. */ @@ -310,7 +286,7 @@ heap_create(const char *relname, relnamespace, tupDesc, relid, - relfilenode, + relid, reltablespace, shared_relation, mapped_relation, @@ -1106,7 +1082,6 @@ heap_create_with_catalog(const char *relname, relnamespace, reltablespace, relid, - InvalidOid, tupdesc, relkind, relpersistence, diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 0c51923be8..d1c02180c2 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -803,23 +803,32 @@ index_create(Relation heapRelation, } /* - * create the index relation's relcache entry and physical disk file. (If - * we fail further down, it's the smgr's responsibility to remove the disk - * file again.) - */ - indexRelation = heap_create(indexRelationName, - namespaceId, - tableSpaceId, - indexRelationId, - relFileNode, - indexTupDesc, - RELKIND_INDEX, - relpersistence, - shared_relation, - mapped_relation); + * create the index relation's relcache entry. + */ + indexRelation = RelationBuildLocalRelation(indexRelationName, + namespaceId, + indexTupDesc, + indexRelationId, + OidIsValid(relFileNode) ? relFileNode : indexRelationId, + tableSpaceId, + shared_relation, + mapped_relation, + relpersistence, + RELKIND_INDEX); Assert(indexRelationId == RelationGetRelid(indexRelation)); + /* + * create the physical disk file, unless the caller provided an existing + * relFileNode for reuse. (If we fail further down, it's the smgr's + * responsibility to remove the disk file again.) + */ + if (!OidIsValid(relFileNode)) + { + RelationOpenSmgr(indexRelation); + RelationCreateStorage(indexRelation->rd_node, relpersistence); + } + /* * Obtain exclusive lock on it. Although no other backends can see it * until we commit, this prevents deadlock-risk complaints from lock diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index b187a4ebf0..65e4372b11 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -2428,6 +2428,19 @@ RelationBuildLocalRelation(const char *relname, bool nailit; AssertArg(natts >= 0); + Assert(OidIsValid(relfilenode)); + + /* + * Never allow a pg_class entry to explicitly specify the database's + * default tablespace in reltablespace; force it to zero instead. This + * ensures that if the database is cloned with a different default + * tablespace, the pg_class entry will still match where CREATE DATABASE + * will put the physically copied relation. + * + * Yes, this is a bit of a hack. + */ + if (reltablespace == MyDatabaseTableSpace) + reltablespace = InvalidOid; /* * check for creation of a rel that must be nailed in cache. diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h index bc8c63a15e..adb7ea2a1e 100644 --- a/src/include/catalog/heap.h +++ b/src/include/catalog/heap.h @@ -41,7 +41,6 @@ extern Relation heap_create(const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, - Oid relfilenode, TupleDesc tupDesc, char relkind, char relpersistence, -- 2.39.5