>From 4019f9556f3708c2d7515ce1d6e1f42c1b724e89 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 7 Jul 2013 18:24:41 +0200
Subject: [PATCH] Introduce a new relfilenodemap cache that maps filenodes to
 oids

To make invalidations work hook into relcache invalidations.
---
 src/backend/utils/adt/dbsize.c           |   1 +
 src/backend/utils/cache/Makefile         |   3 +-
 src/backend/utils/cache/inval.c          |   2 +-
 src/backend/utils/cache/relfilenodemap.c | 261 +++++++++++++++++++++++++++++++
 src/include/utils/relfilenodemap.h       |  18 +++
 5 files changed, 283 insertions(+), 2 deletions(-)
 create mode 100644 src/backend/utils/cache/relfilenodemap.c
 create mode 100644 src/include/utils/relfilenodemap.h

diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index 34482ab..c101fee 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -28,6 +28,7 @@
 #include "utils/builtins.h"
 #include "utils/numeric.h"
 #include "utils/rel.h"
+#include "utils/relfilenodemap.h"
 #include "utils/relmapper.h"
 #include "utils/syscache.h"
 
diff --git a/src/backend/utils/cache/Makefile b/src/backend/utils/cache/Makefile
index 32d722e..a943f8e 100644
--- a/src/backend/utils/cache/Makefile
+++ b/src/backend/utils/cache/Makefile
@@ -13,6 +13,7 @@ top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
 OBJS = attoptcache.o catcache.o evtcache.o inval.o plancache.o relcache.o \
-	relmapper.o spccache.o syscache.o lsyscache.o typcache.o ts_cache.o
+	relmapper.o relfilenodemap.o spccache.o syscache.o lsyscache.o \
+	typcache.o ts_cache.o
 
 include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c
index 3356d0f..080f223 100644
--- a/src/backend/utils/cache/inval.c
+++ b/src/backend/utils/cache/inval.c
@@ -178,7 +178,7 @@ static int	maxSharedInvalidMessagesArray;
  */
 
 #define MAX_SYSCACHE_CALLBACKS 32
-#define MAX_RELCACHE_CALLBACKS 5
+#define MAX_RELCACHE_CALLBACKS 6
 
 static struct SYSCACHECALLBACK
 {
diff --git a/src/backend/utils/cache/relfilenodemap.c b/src/backend/utils/cache/relfilenodemap.c
new file mode 100644
index 0000000..9f543cd
--- /dev/null
+++ b/src/backend/utils/cache/relfilenodemap.c
@@ -0,0 +1,261 @@
+/*-------------------------------------------------------------------------
+ *
+ * relfilenodemap.c
+ *	  relfilenode to oid mapping cache.
+ *
+ * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *	  src/backend/utils/cache/relfilenode.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/genam.h"
+#include "access/heapam.h"
+#include "access/htup_details.h"
+#include "catalog/indexing.h"
+#include "catalog/pg_class.h"
+#include "catalog/pg_tablespace.h"
+#include "utils/builtins.h"
+#include "utils/catcache.h"
+#include "utils/hsearch.h"
+#include "utils/inval.h"
+#include "utils/fmgroids.h"
+#include "utils/rel.h"
+#include "utils/relfilenodemap.h"
+#include "utils/relmapper.h"
+
+/* Hash table for informations about each relfilenode <-> oid pair */
+static HTAB *RelfilenodeMapHash = NULL;
+
+/* built first time through in InitializeRelfilenodeMap */
+ScanKeyData relfilenode_skey[2];
+
+typedef struct
+{
+	Oid			reltablespace;
+	Oid			relfilenode;
+} RelfilenodeMapKey;
+
+typedef struct
+{
+	RelfilenodeMapKey key;	/* lookup key - must be first */
+	Oid			relid;			/* pg_class.oid */
+} RelfilenodeMapEntry;
+
+/*
+ * RelfilenodeMapInvalidateCallback
+ *		Flush mapping entries when pg_class is updated in a relevant fashion.
+ */
+static void
+RelfilenodeMapInvalidateCallback(Datum arg, Oid relid)
+{
+	HASH_SEQ_STATUS status;
+	RelfilenodeMapEntry *entry;
+
+	/* not active or deleted */
+	if (RelfilenodeMapHash == NULL)
+		return;
+
+	/* delete entire cache */
+	if (relid == InvalidOid)
+	{
+		hash_destroy(RelfilenodeMapHash);
+		RelfilenodeMapHash = NULL;
+		return;
+	}
+
+	hash_seq_init(&status, RelfilenodeMapHash);
+	while ((entry = (RelfilenodeMapEntry *) hash_seq_search(&status)) != NULL)
+	{
+		/*
+		 * Note that there might be multiple entries for one oid at the same
+		 * time while we're processing invalidations.
+		 */
+		if (entry->relid == relid)
+		{
+			if (hash_search(RelfilenodeMapHash,
+							(void *) &entry->key,
+							HASH_REMOVE,
+							NULL) == NULL)
+				elog(ERROR, "hash table corrupted");
+		}
+	}
+}
+
+static void
+InitializeRelfilenodeMap(void)
+{
+	HASHCTL		ctl;
+	static bool	initial_init_done = false;
+	int i;
+
+	/* Make sure we've initialized CacheMemoryContext. */
+	if (CacheMemoryContext == NULL)
+		CreateCacheMemoryContext();
+
+	/* Initialize the hash table. */
+	MemSet(&ctl, 0, sizeof(ctl));
+	ctl.keysize = sizeof(RelfilenodeMapKey);
+	ctl.entrysize = sizeof(RelfilenodeMapEntry);
+	ctl.hash = tag_hash;
+	ctl.hcxt = CacheMemoryContext;
+
+	RelfilenodeMapHash =
+		hash_create("RelfilenodeMap cache", 1024, &ctl,
+					HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
+
+	/*
+	 * For complete resets we simply delete the entire hash, but there's no
+	 * need to do the other stuff multiple times. Especially the initialization
+	 * of the relcche invalidation should only be done once.
+	 */
+	if (initial_init_done)
+		return;
+
+	/* build skey */
+	MemSet(&relfilenode_skey, 0, sizeof(relfilenode_skey));
+
+	for (i = 0; i < 2; i++)
+	{
+		fmgr_info_cxt(F_OIDEQ,
+					  &relfilenode_skey[i].sk_func,
+					  CacheMemoryContext);
+		relfilenode_skey[i].sk_strategy = BTEqualStrategyNumber;
+		relfilenode_skey[i].sk_subtype = InvalidOid;
+		relfilenode_skey[i].sk_collation = InvalidOid;
+	}
+
+	relfilenode_skey[0].sk_attno = Anum_pg_class_reltablespace;
+	relfilenode_skey[1].sk_attno = Anum_pg_class_relfilenode;
+
+	/* Watch for invalidation events. */
+	CacheRegisterRelcacheCallback(RelfilenodeMapInvalidateCallback,
+								  (Datum) 0);
+	initial_init_done = true;
+}
+
+/*
+ * Map a relation's (tablespace, filenode) to a relation's oid and cache the
+ * result.
+ *
+ * Instead of DEFAULTTABLESPACE_OID InvalidOid/0 can be passed as
+ * tablespace. The table identified by the parameter pair can be a shared,
+ * nailed or normal relation.
+ *
+ * Returns InvalidOid if no relation mapping the criteria could be found.
+ */
+Oid
+RelidByRelfilenode(Oid reltablespace, Oid relfilenode)
+{
+	RelfilenodeMapKey key;
+	RelfilenodeMapEntry *entry;
+	bool found;
+	SysScanDesc scandesc;
+	Relation relation;
+	HeapTuple ntp;
+	ScanKeyData skey[2];
+
+	if (RelfilenodeMapHash == NULL)
+		InitializeRelfilenodeMap();
+
+	/*
+	 * relations in the default tablespace are stored with InvalidOid as
+	 * pg_class.reltablespace.
+	 */
+	if (reltablespace == DEFAULTTABLESPACE_OID)
+		reltablespace = 0;
+
+	MemSet(&key, 0, sizeof(key));
+	key.reltablespace = reltablespace;
+	key.relfilenode = relfilenode;
+
+	/*
+	 * Check cache and enter entry if nothing could be found. Even if no target
+	 * relation can be found lateron we store the negative match and return a
+	 * InvalidOid from cache. That's not really necessary for performance since
+	 * querinyg invalid values isn't supposed to be a frequent thing, but it's
+	 * simpler implementation wise this way.
+	 */
+	entry = hash_search(RelfilenodeMapHash,
+						(void *) &key,
+						HASH_ENTER,
+						&found);
+
+	if (found)
+		return entry->relid;
+
+	/* ok, no previous cache entry, do it the hard way */
+
+	/* check shared tables */
+	if (reltablespace == GLOBALTABLESPACE_OID)
+	{
+		entry->relid = RelationMapFilenodeToOid(relfilenode, true);
+		return entry->relid;
+	}
+
+	/* check plain relations by looking in pg_class */
+	relation = heap_open(RelationRelationId, AccessShareLock);
+
+	/* copy scankey to local copy, it will be modified during the scan */
+	memcpy(skey, relfilenode_skey, sizeof(skey));
+
+	/* set scan arguments */
+	skey[0].sk_argument = ObjectIdGetDatum(reltablespace);
+	skey[1].sk_argument = ObjectIdGetDatum(relfilenode);
+
+	scandesc = systable_beginscan(relation,
+								  ClassTblspcRelfilenodeIndexId,
+								  true,
+								  NULL,
+								  2,
+								  skey);
+
+	found = false;
+
+	while (HeapTupleIsValid(ntp = systable_getnext(scandesc)))
+	{
+		bool isnull;
+
+		if (found)
+			elog(ERROR, "duplicate in GetOidByFilenode");
+		found = true;
+
+#ifdef USE_ASSERT_CHECKING
+		if (assert_enabled)
+		{
+			Oid check;
+			check = fastgetattr(ntp, Anum_pg_class_reltablespace,
+								RelationGetDescr(relation),
+								&isnull);
+
+			/*
+			 * reltablespace is already set to InvalidOid above if we're
+			 * looking for DEFAULTTABLESPACE_OID.
+			 */
+			if (isnull || check != reltablespace)
+				elog(ERROR, "borked reltablespace lookup");
+
+			check = fastgetattr(ntp, Anum_pg_class_relfilenode,
+								RelationGetDescr(relation),
+								&isnull);
+
+			if (isnull || check != relfilenode)
+				elog(ERROR, "borked relfilenode lookup");
+		}
+#endif
+		entry->relid = HeapTupleGetOid(ntp);
+	}
+
+	systable_endscan(scandesc);
+	heap_close(relation, AccessShareLock);
+
+	/* check for nailed tables, those will not have been in pg_class */
+	if (!found)
+		entry->relid = RelationMapFilenodeToOid(relfilenode, false);
+
+	return entry->relid;
+}
diff --git a/src/include/utils/relfilenodemap.h b/src/include/utils/relfilenodemap.h
new file mode 100644
index 0000000..cdb9bbf
--- /dev/null
+++ b/src/include/utils/relfilenodemap.h
@@ -0,0 +1,18 @@
+/*-------------------------------------------------------------------------
+ *
+ * relfilenodemap.h
+ *	  relfilenode to oid mapping cache.
+ *
+ * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/utils/relfilenodemap.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef RELFILENODEMAP_H
+#define RELFILENODEMAP_H
+
+Oid RelidByRelfilenode(Oid reltablespace, Oid relfilenode);
+
+#endif   /* RELFILENODEMAP_H */
-- 
1.8.3.251.g1462b67

