From d674c58098580ec59f9e1e47255c68b8497178b1 Mon Sep 17 00:00:00 2001
From: jesperpedersen <jesper.pedersen@redhat.com>
Date: Fri, 3 Feb 2017 11:28:19 -0500
Subject: [PATCH] Add pageinspect.h

---
 contrib/pageinspect/brinfuncs.c   |  1 +
 contrib/pageinspect/btreefuncs.c  |  1 +
 contrib/pageinspect/fsmfuncs.c    |  1 +
 contrib/pageinspect/ginfuncs.c    | 21 +-------------
 contrib/pageinspect/hashfuncs.c   |  3 +-
 contrib/pageinspect/pageinspect.h | 61 +++++++++++++++++++++++++++++++++++++++
 contrib/pageinspect/rawpage.c     | 25 ++++++++++++++++
 7 files changed, 92 insertions(+), 21 deletions(-)
 create mode 100644 contrib/pageinspect/pageinspect.h

diff --git a/contrib/pageinspect/brinfuncs.c b/contrib/pageinspect/brinfuncs.c
index 7b877e3..23136d2 100644
--- a/contrib/pageinspect/brinfuncs.c
+++ b/contrib/pageinspect/brinfuncs.c
@@ -9,6 +9,7 @@
  */
 #include "postgres.h"
 
+#include "pageinspect.h"
 #include "access/htup_details.h"
 #include "access/brin.h"
 #include "access/brin_internal.h"
diff --git a/contrib/pageinspect/btreefuncs.c b/contrib/pageinspect/btreefuncs.c
index 3f09d5f..e63b5fb 100644
--- a/contrib/pageinspect/btreefuncs.c
+++ b/contrib/pageinspect/btreefuncs.c
@@ -27,6 +27,7 @@
 
 #include "postgres.h"
 
+#include "pageinspect.h"
 #include "access/nbtree.h"
 #include "catalog/namespace.h"
 #include "catalog/pg_am.h"
diff --git a/contrib/pageinspect/fsmfuncs.c b/contrib/pageinspect/fsmfuncs.c
index 6541646..372de39 100644
--- a/contrib/pageinspect/fsmfuncs.c
+++ b/contrib/pageinspect/fsmfuncs.c
@@ -19,6 +19,7 @@
 
 #include "postgres.h"
 
+#include "pageinspect.h"
 #include "funcapi.h"
 #include "lib/stringinfo.h"
 #include "miscadmin.h"
diff --git a/contrib/pageinspect/ginfuncs.c b/contrib/pageinspect/ginfuncs.c
index cea77d3..5cfcbcb 100644
--- a/contrib/pageinspect/ginfuncs.c
+++ b/contrib/pageinspect/ginfuncs.c
@@ -9,6 +9,7 @@
  */
 #include "postgres.h"
 
+#include "pageinspect.h"
 #include "access/gin.h"
 #include "access/gin_private.h"
 #include "access/htup_details.h"
@@ -29,26 +30,6 @@ PG_FUNCTION_INFO_V1(gin_page_opaque_info);
 PG_FUNCTION_INFO_V1(gin_leafpage_items);
 
 
-static Page
-get_page_from_raw(bytea *raw_page)
-{
-	int			raw_page_size;
-	Page		page;
-
-	raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
-	if (raw_page_size < BLCKSZ)
-		ereport(ERROR,
-				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("input page too small (%d bytes)", raw_page_size)));
-
-	/* make a copy so that the page is properly aligned for struct access */
-	page = palloc(raw_page_size);
-	memcpy(page, VARDATA(raw_page), raw_page_size);
-
-	return page;
-}
-
-
 Datum
 gin_metapage_info(PG_FUNCTION_ARGS)
 {
diff --git a/contrib/pageinspect/hashfuncs.c b/contrib/pageinspect/hashfuncs.c
index 83b4698..09bb9ee 100644
--- a/contrib/pageinspect/hashfuncs.c
+++ b/contrib/pageinspect/hashfuncs.c
@@ -10,6 +10,7 @@
 
 #include "postgres.h"
 
+#include "pageinspect.h"
 #include "access/hash.h"
 #include "access/htup_details.h"
 #include "catalog/pg_type.h"
@@ -67,7 +68,7 @@ verify_hash_page(bytea *raw_page, int flags)
 				 errdetail("Expected size %d, got %d",
 						   BLCKSZ, raw_page_size)));
 
-	page = VARDATA(raw_page);
+	page = get_page_from_raw(raw_page);
 
 	if (PageIsNew(page))
 		ereport(ERROR,
diff --git a/contrib/pageinspect/pageinspect.h b/contrib/pageinspect/pageinspect.h
new file mode 100644
index 0000000..9ec3aad
--- /dev/null
+++ b/contrib/pageinspect/pageinspect.h
@@ -0,0 +1,61 @@
+/*
+ * pageinspect.h
+ *		Functions to investigate the content of indexes
+ *
+ * Copyright (c) 2017, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *		contrib/pageinspect/pageinspect.h
+ */
+#ifndef PAGEINSPECT_H
+#define PAGEINSPECT_H
+
+#include "postgres.h"
+
+#include "funcapi.h"
+
+// Raw page functions
+
+extern Datum get_raw_page(PG_FUNCTION_ARGS);
+extern Datum get_raw_page_fork(PG_FUNCTION_ARGS);
+extern Page get_page_from_raw(bytea *raw_page);
+
+extern Datum page_header(PG_FUNCTION_ARGS);
+
+// FSM functions
+
+extern Datum fsm_page_contents(PG_FUNCTION_ARGS);
+
+// Heap functions
+
+extern Datum heap_page_items(PG_FUNCTION_ARGS);
+extern Datum tuple_data_split(PG_FUNCTION_ARGS);
+
+// B-tree functions
+
+extern Datum bt_page_stats(PG_FUNCTION_ARGS);
+extern Datum bt_page_items(PG_FUNCTION_ARGS);
+extern Datum bt_metap(PG_FUNCTION_ARGS);
+
+// GIN functions
+
+extern Datum gin_metapage_info(PG_FUNCTION_ARGS);
+extern Datum gin_page_opaque_info(PG_FUNCTION_ARGS);
+extern Datum gin_leafpage_items(PG_FUNCTION_ARGS);
+
+// BRIN functions
+
+extern Datum brin_page_type(PG_FUNCTION_ARGS);
+extern Datum brin_page_items(PG_FUNCTION_ARGS);
+extern Datum brin_metapage_info(PG_FUNCTION_ARGS);
+extern Datum brin_revmap_data(PG_FUNCTION_ARGS);
+
+// Hash functions
+
+extern Datum hash_page_type(PG_FUNCTION_ARGS);
+extern Datum hash_page_stats(PG_FUNCTION_ARGS);
+extern Datum hash_page_items(PG_FUNCTION_ARGS);
+extern Datum hash_bitmap_info(PG_FUNCTION_ARGS);
+extern Datum hash_metapage_info(PG_FUNCTION_ARGS);
+
+#endif
diff --git a/contrib/pageinspect/rawpage.c b/contrib/pageinspect/rawpage.c
index a2ac078..991ae84 100644
--- a/contrib/pageinspect/rawpage.c
+++ b/contrib/pageinspect/rawpage.c
@@ -15,6 +15,7 @@
 
 #include "postgres.h"
 
+#include "pageinspect.h"
 #include "access/htup_details.h"
 #include "catalog/catalog.h"
 #include "catalog/namespace.h"
@@ -86,6 +87,30 @@ get_raw_page_fork(PG_FUNCTION_ARGS)
 }
 
 /*
+ * get_page_from_raw
+ *
+ * Get a page
+ */
+Page
+get_page_from_raw(bytea *raw_page)
+{
+	int			raw_page_size;
+	Page		page;
+
+	raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
+	if (raw_page_size < BLCKSZ)
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				 errmsg("input page too small (%d bytes)", raw_page_size)));
+
+	/* make a copy so that the page is properly aligned for struct access */
+	page = palloc(raw_page_size);
+	memcpy(page, VARDATA(raw_page), raw_page_size);
+
+	return page;
+}
+
+/*
  * workhorse
  */
 static bytea *
-- 
2.7.4

