--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * partdir.c
+ * Support for partition directories
+ *
+ * Partition directories provide a mechanism for looking up the
+ * PartitionDesc for a relation in such a way that the answer will be
+ * the same every time the directory is interrogated, even in the face
+ * of concurrent DDL.
+ *
+ * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ * src/backend/partitioning/partdir.c
+ *
+ *-------------------------------------------------------------------------
+*/
+#include "postgres.h"
+
+#include "catalog/pg_class.h"
+#include "partitioning/partdir.h"
+#include "utils/hsearch.h"
+#include "utils/rel.h"
+
+typedef struct PartitionDirectoryData
+{
+ MemoryContext pdir_mcxt;
+ HTAB *pdir_htab;
+} PartitionDirectoryData;
+
+typedef struct PartitionDirectoryEntry
+{
+ Oid relid;
+ PartitionDesc pd;
+} PartitionDirectoryEntry;
+
+PartitionDirectory
+CreatePartitionDirectory(MemoryContext mcxt)
+{
+ HASHCTL hctl;
+ MemoryContext oldcontext;
+ PartitionDirectory pdir;
+
+ hctl.keysize = sizeof(Oid);
+ hctl.entrysize = sizeof(PartitionDirectoryEntry);
+ hctl.hcxt = mcxt;
+
+ oldcontext = MemoryContextSwitchTo(mcxt);
+
+ pdir = palloc(sizeof(PartitionDirectoryData));
+ pdir->pdir_mcxt = mcxt;
+ pdir->pdir_htab = hash_create("PartitionDirectory", 256, &hctl,
+ HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
+
+ MemoryContextSwitchTo(oldcontext);
+ return pdir;
+}
+
+PartitionDesc
+PartitionDirectoryLookup(PartitionDirectory pdir, Relation rel)
+{
+ PartitionDirectoryEntry *pde;
+ Oid relid;
+ bool found;
+
+ Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+ relid = RelationGetRelid(rel);
+ pde = hash_search(pdir->pdir_htab, &relid, HASH_ENTER, &found);
+ if (!found)
+ {
+ pde->pd = RelationGetPartitionDesc(rel);
+ Assert(pde->pd != NULL);
+ }
+ return pde->pd;
+}
--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * partdir.h
+ * A partition directory provides stable PartitionDesc lookups
+ *
+ * Copyright (c) 2007-2018, PostgreSQL Global Development Group
+ *
+ * src/include/partitioning/partdir.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef PARTDIR_H
+#define PARTDIR_H
+
+#include "partitioning/partdefs.h"
+#include "utils/relcache.h"
+
+extern PartitionDirectory CreatePartitionDirectory(MemoryContext mcxt);
+extern PartitionDesc PartitionDirectoryLookup(PartitionDirectory, Relation);
+
+#endif /* PARTDIR_H */