Rejigger metadata creation.
authorRobert Haas <rhaas@postgresql.org>
Wed, 13 Jun 2012 19:49:15 +0000 (15:49 -0400)
committerRobert Haas <rhaas@postgresql.org>
Thu, 14 Jun 2012 14:33:05 +0000 (10:33 -0400)
src/backend/access/common/metapage.c
src/backend/utils/cache/relcache.c
src/include/access/metapage.h

index b143c55e7756774339ecc0e131fb6edb2110a6ff..f311eb91e1937f0cc98b3055d95982192194246b 100644 (file)
@@ -38,9 +38,29 @@ MetapageInit(Relation relation, Page page)
 {
        RelationMetapage        meta;
 
+       if (relation->rd_metapage == NULL)
+               RelationBuildMetadata(relation);
        PageSetRelationMetapage(page);
-
        meta = BlindGetRelationMeta(page);
+       memcpy(meta, relation->rd_metapage, sizeof(RelationMetapageData));
+}
+
+/*
+ * Build metadata for a new relation.
+ */
+void
+RelationBuildMetadata(Relation relation)
+{
+       RelationMetapage        meta;
+
+       /* The metadata shouldn't already be initialized. */
+       Assert(relation->rd_metapage == NULL);
+
+       /* Allocate memory for the data. */
+       meta = (RelationMetapage) MemoryContextAllocZero(CacheMemoryContext,
+               sizeof(RelationMetapageData));
+
+       /* Initialize. */
        meta->rmp_magic = METAPAGE_MAGIC;
        meta->rmp_version = METAPAGE_VERSION;
        meta->rmp_dboid = relation->rd_node.dbNode;
@@ -51,6 +71,9 @@ MetapageInit(Relation relation, Page page)
        meta->rmp_minlayout = PG_PAGE_LAYOUT_VERSION;
        meta->rmp_maxlayout = PG_PAGE_LAYOUT_VERSION;
        meta->rmp_relfilenode_time = (pg_time_t) time(NULL);
+
+       /* Save it. */
+       relation->rd_metapage = meta;
 }
 
 /*
@@ -75,7 +98,7 @@ RelationGetMetadata(Relation relation)
         * If the relation has a metapage, read it.
         *
         * XXX: It's pretty annoying to have to call RelationGetNumberOfBlocks.
-        * Can't we have an RBM_ERROR option for ReadBufferExtended?
+        * Can't we have an RBM_FAIL option for ReadBufferExtended?
         */
        if (RelationGetNumberOfBlocks(relation) > METAPAGE_BLKNO)
        {
index 65e4372b11a6549ef1ce4d18a319b1ac5df9625e..685955e58b31dc15026fa438dbf89f9aecc57814 100644 (file)
@@ -30,6 +30,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 
+#include "access/metapage.h"
 #include "access/reloptions.h"
 #include "access/sysattr.h"
 #include "access/transam.h"
@@ -2587,6 +2588,10 @@ RelationBuildLocalRelation(const char *relname,
 
        RelationInitPhysicalAddr(rel);
 
+       /* XXX: Not for heaps yet. */
+       if (relkind == RELKIND_INDEX)
+               RelationBuildMetadata(rel);
+
        /*
         * Okay to insert into the relcache hash tables.
         */
index 65c68bdabd6c5c0d4aa5d7cc808d1a2e691dea3e..a119dc4dc6fd6acd6fa94a0b28f6e5554df97881 100644 (file)
@@ -72,6 +72,7 @@ typedef RelationMetapageData *RelationMetapage;
  * Function prototypes.
  */
 extern void MetapageInit(Relation relation, Page page);
+extern void RelationBuildMetadata(Relation relation);
 extern RelationMetapage RelationGetMetadata(Relation rel);
 
 /*