>From ba39817ac4ccc6182eae720a6f54df0f66584574 Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Fri, 22 Jan 2016 18:31:39 +0900
Subject: [PATCH 05/10] Add partition system catalog.

Named pg_partition, it contains the following information:

 1. Partition relation oid (pg_class.oid)
 2. Partition parent oid
 3. Whether partition is valid
 4. List partition literal values
 5. Range partition maximum value per key column

It has a unique index on partrelid and the related syscache. Also,
an index on partparent.
---
 doc/src/sgml/catalogs.sgml         |   75 +++++++++++++++++++++++++++++++++++-
 src/backend/catalog/Makefile       |    2 +-
 src/backend/utils/cache/syscache.c |   12 ++++++
 src/include/catalog/indexing.h     |    5 ++
 src/include/catalog/pg_partition.h |   62 +++++++++++++++++++++++++++++
 src/include/utils/syscache.h       |    1 +
 6 files changed, 155 insertions(+), 2 deletions(-)
 create mode 100644 src/include/catalog/pg_partition.h

diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml
index db8d0ac..a5fe8a6 100644
--- a/doc/src/sgml/catalogs.sgml
+++ b/doc/src/sgml/catalogs.sgml
@@ -219,8 +219,13 @@
      </row>
 
      <row>
+      <entry><link linkend="catalog-pg-partition"><structname>pg_partition</structname></link></entry>
+      <entry>information about partitions</entry>
+     </row>
+
+     <row>
       <entry><link linkend="catalog-pg-partitioned-rel"><structname>pg_partitioned_rel</structname></link></entry>
-      <entry>information about partitioned tables, including the partition key</entry>
+      <entry>information about partitioned tables</entry>
      </row>
 
      <row>
@@ -4542,6 +4547,74 @@
 
  </sect1>
 
+ <sect1 id="catalog-pg-partition">
+  <title><structname>pg_partition</structname></title>
+
+  <indexterm zone="catalog-pg-partition">
+   <primary>pg_partition</primary>
+  </indexterm>
+
+  <para>
+   The catalog <structname>pg_partition</structname> stores
+   information about partitions of partitioned tables.
+  </para>
+
+  <table>
+   <title><structname>pg_partition</> Columns</title>
+
+   <tgroup cols="4">
+    <thead>
+     <row>
+      <entry>Name</entry>
+      <entry>Type</entry>
+      <entry>References</entry>
+      <entry>Description</entry>
+     </row>
+    </thead>
+
+    <tbody>
+
+     <row>
+      <entry><structfield>partrelid</structfield></entry>
+      <entry><type>oid</type></entry>
+      <entry><literal><link linkend="catalog-pg-class"><structname>pg_class</structname></link>.oid</literal></entry>
+      <entry>The OID of the <structname>pg_class</> entry for this partition</entry>
+     </row>
+
+     <row>
+      <entry><structfield>partparent</structfield></entry>
+      <entry><type>oid</type></entry>
+      <entry><literal><link linkend="catalog-pg-class"><structname>pg_class</structname></link>.oid</literal></entry>
+      <entry>The OID of the partitioned table this partition is part of</entry>
+     </row>
+
+     <row>
+      <entry><structfield>partvalid</structfield></entry>
+      <entry><type>bool</type></entry>
+      <entry></entry>
+      <entry>Partition is valid</entry>
+     </row>
+
+     <row>
+      <entry><structfield>partlistvals</structfield></entry>
+      <entry><type>anyarray</type></entry>
+      <entry></entry>
+      <entry>For partitions of list partitioned table, list of values assigned to this partition</entry>
+     </row>
+
+     <row>
+      <entry><structfield>partrangemaxs</structfield></entry>
+      <entry><type>anyarray</type></entry>
+      <entry></entry>
+      <entry>For partitions of range partitioned table, array of maximum values per partition key column</entry>
+     </row>
+
+    </tbody>
+   </tgroup>
+  </table>
+
+ </sect1>
+
  <sect1 id="catalog-pg-partitioned-rel">
   <title><structname>pg_partitioned_rel</structname></title>
 
diff --git a/src/backend/catalog/Makefile b/src/backend/catalog/Makefile
index ffec071..fc85518 100644
--- a/src/backend/catalog/Makefile
+++ b/src/backend/catalog/Makefile
@@ -41,7 +41,7 @@ POSTGRES_BKI_SRCS = $(addprefix $(top_srcdir)/src/include/catalog/,\
 	pg_foreign_data_wrapper.h pg_foreign_server.h pg_user_mapping.h \
 	pg_foreign_table.h pg_policy.h pg_replication_origin.h \
 	pg_default_acl.h pg_seclabel.h pg_shseclabel.h \
-	pg_collation.h pg_range.h pg_transform.h pg_partitioned_rel.h\
+	pg_collation.h pg_range.h pg_transform.h pg_partitioned_rel.h pg_partition.h \
 	toasting.h indexing.h \
     )
 
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index f703668..fe194d5 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -48,6 +48,7 @@
 #include "catalog/pg_opclass.h"
 #include "catalog/pg_operator.h"
 #include "catalog/pg_opfamily.h"
+#include "catalog/pg_partition.h"
 #include "catalog/pg_partitioned_rel.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_range.h"
@@ -569,6 +570,17 @@ static const struct cachedesc cacheinfo[] = {
 		},
 		8
 	},
+	{PartitionRelationId,		/* PARTRELID */
+		PartitionRelidIndexId,
+		1,
+		{
+			Anum_pg_partition_partrelid,
+			0,
+			0,
+			0
+		},
+		128
+	},
 	{PartitionedRelRelationId,		/* PARTEDRELIDLEVEL */
 		PartitionedRelRelidLevelIndexId,
 		2,
diff --git a/src/include/catalog/indexing.h b/src/include/catalog/indexing.h
index 343204b..a2a583d 100644
--- a/src/include/catalog/indexing.h
+++ b/src/include/catalog/indexing.h
@@ -322,6 +322,11 @@ DECLARE_UNIQUE_INDEX(pg_partitioned_rel_partrelid_level_index, 3319, on pg_parti
 DECLARE_INDEX(pg_partitioned_rel_partrelid_index, 3320, on pg_partitioned_rel using btree(partrelid oid_ops));
 #define PartitionedRelRelidIndexId          3320
 
+DECLARE_UNIQUE_INDEX(pg_partition_partrelid_index, 3322, on pg_partition using btree(partrelid oid_ops));
+#define PartitionRelidIndexId		3322
+DECLARE_INDEX(pg_partition_parent_index, 3323, on pg_partition using btree(partparent oid_ops));
+#define PartitionParentIndexId		3323
+
 /* last step of initialization script: build the indexes declared above */
 BUILD_INDICES
 
diff --git a/src/include/catalog/pg_partition.h b/src/include/catalog/pg_partition.h
new file mode 100644
index 0000000..aa89338
--- /dev/null
+++ b/src/include/catalog/pg_partition.h
@@ -0,0 +1,62 @@
+/*-------------------------------------------------------------------------
+ *
+ * pg_partition.h
+ *	  definition of the system "partition" relation (pg_partition)
+ *	  along with the relation's initial contents.
+ *
+ *
+ * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
+ *
+ * $PostgreSQL: pgsql/src/include/catalog/pg_partition.h $
+ *
+ * NOTES
+ *	  the genbki.sh script reads this file and generates .bki
+ *	  information from the DATA() statements.
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef PG_PARTITION_H
+#define PG_PARTITION_H
+
+#include "catalog/genbki.h"
+
+/* ----------------
+ *		pg_partitioned_rel definition.  cpp turns this into
+ *		typedef struct FormData_pg_partitioned_rel
+ * ----------------
+ */
+#define PartitionRelationId 3321
+
+CATALOG(pg_partition,3321) BKI_WITHOUT_OIDS
+{
+	Oid			partrelid;		/* partition oid */
+	Oid			partparent;		/* parent oid */
+	bool		partvalid;		/* is partition valid */
+
+#ifdef CATALOG_VARLEN			/* variable-length fields start here */
+	anyarray	partlistvals;	/* array of allowed values for the only
+								 * partition column */
+	anyarray	partrangemaxs;	/* array of rangemax values, one per
+								 * key column */
+#endif
+} FormData_pg_partition;
+
+/* ----------------
+ *      Form_pg_partition corresponds to a pointer to a tuple with
+ *      the format of pg_partition relation.
+ * ----------------
+ */
+typedef FormData_pg_partition *Form_pg_partition;
+
+/* ----------------
+ *      compiler constants for pg_partition
+ * ----------------
+ */
+#define Natts_pg_partition					5
+#define Anum_pg_partition_partrelid			1
+#define Anum_pg_partition_partparent		2
+#define Anum_pg_partition_partvalid			3
+#define Anum_pg_partition_partlistvals		4
+#define Anum_pg_partition_partrangemaxs		5
+
+#endif   /* PG_PARTITION_H */
diff --git a/src/include/utils/syscache.h b/src/include/utils/syscache.h
index 1c961c7..600047b 100644
--- a/src/include/utils/syscache.h
+++ b/src/include/utils/syscache.h
@@ -72,6 +72,7 @@ enum SysCacheIdentifier
 	OPEROID,
 	OPFAMILYAMNAMENSP,
 	OPFAMILYOID,
+	PARTRELID,
 	PARTEDRELIDLEVEL,
 	PROCNAMEARGSNSP,
 	PROCOID,
-- 
1.7.1

