From a690c18221dd9c05cc7c9ae42b5046c866dae5a7 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Thu, 29 Sep 2016 12:00:00 -0400
Subject: [PATCH 2/2] Add tests for pageinspect hash

---
 contrib/pageinspect/Makefile          |   2 +-
 contrib/pageinspect/expected/hash.out | 131 ++++++++++++++++++++++++++++++++++
 contrib/pageinspect/sql/hash.sql      |  29 ++++++++
 3 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 contrib/pageinspect/expected/hash.out
 create mode 100644 contrib/pageinspect/sql/hash.sql

diff --git a/contrib/pageinspect/Makefile b/contrib/pageinspect/Makefile
index ecf0df0..d566c35 100644
--- a/contrib/pageinspect/Makefile
+++ b/contrib/pageinspect/Makefile
@@ -11,7 +11,7 @@ DATA = pageinspect--1.6.sql pageinspect--1.5--1.6.sql \
 	pageinspect--1.0--1.1.sql pageinspect--unpackaged--1.0.sql
 PGFILEDESC = "pageinspect - functions to inspect contents of database pages"
 
-REGRESS = page btree brin gin
+REGRESS = page btree brin gin hash
 
 ifdef USE_PGXS
 PG_CONFIG = pg_config
diff --git a/contrib/pageinspect/expected/hash.out b/contrib/pageinspect/expected/hash.out
new file mode 100644
index 0000000..c605297
--- /dev/null
+++ b/contrib/pageinspect/expected/hash.out
@@ -0,0 +1,131 @@
+CREATE TABLE test1 (a int, b text);
+INSERT INTO test1 VALUES (1, 'one');
+CREATE INDEX test1_a_idx ON test1 USING hash (a);
+WARNING:  hash indexes are not WAL-logged and their use is discouraged
+\x
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 0));
+-[ RECORD 1 ]--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+magic     | 0x06440640
+version   | 2
+ntuples   | 1
+ffactor   | 307
+bsize     | 8152
+bmsize    | 4096
+bmshift   | 15
+maxbucket | 3
+highmask  | 7
+lowmask   | 3
+ovflpoint | 2
+firstfree | 0
+nmaps     | 1
+procid    | 450
+spares    | 0000 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
+mapp      | 0005 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
+
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 1));
+ERROR:  page is not a metadata page
+DETAIL:  Expected 00000008, got 00000002.
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 2));
+ERROR:  page is not a metadata page
+DETAIL:  Expected 00000008, got 00000002.
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 3));
+ERROR:  page is not a metadata page
+DETAIL:  Expected 00000008, got 00000002.
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 4));
+ERROR:  page is not a metadata page
+DETAIL:  Expected 00000008, got 00000002.
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 5));
+ERROR:  page is not a metadata page
+DETAIL:  Expected 00000008, got 00000004.
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 6));
+ERROR:  block number 6 is out of range for relation "test1_a_idx"
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 0));
+ERROR:  page is a metadata page
+DETAIL:  Flags 00000008.
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 1));
+-[ RECORD 1 ]---+-------
+type            | bucket
+live_items      | 0
+dead_items      | 0
+page_size       | 8192
+free_size       | 8148
+hasho_prevblkno | -1
+hasho_nextblkno | -1
+hasho_bucket    | 0
+hasho_flag      | 2
+hasho_page_id   | 65408
+
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 2));
+-[ RECORD 1 ]---+-------
+type            | bucket
+live_items      | 0
+dead_items      | 0
+page_size       | 8192
+free_size       | 8148
+hasho_prevblkno | -1
+hasho_nextblkno | -1
+hasho_bucket    | 1
+hasho_flag      | 2
+hasho_page_id   | 65408
+
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 3));
+-[ RECORD 1 ]---+-------
+type            | bucket
+live_items      | 1
+dead_items      | 0
+page_size       | 8192
+free_size       | 8128
+hasho_prevblkno | -1
+hasho_nextblkno | -1
+hasho_bucket    | 2
+hasho_flag      | 2
+hasho_page_id   | 65408
+
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 4));
+-[ RECORD 1 ]---+-------
+type            | bucket
+live_items      | 0
+dead_items      | 0
+page_size       | 8192
+free_size       | 8148
+hasho_prevblkno | -1
+hasho_nextblkno | -1
+hasho_bucket    | 3
+hasho_flag      | 2
+hasho_page_id   | 65408
+
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 5));
+-[ RECORD 1 ]---+-------
+type            | bitmap
+live_items      | 0
+dead_items      | 0
+page_size       | 8192
+free_size       | 8148
+hasho_prevblkno | -1
+hasho_nextblkno | -1
+hasho_bucket    | -1
+hasho_flag      | 4
+hasho_page_id   | 65408
+
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 0));
+ERROR:  page is a metadata page
+DETAIL:  Flags 00000008.
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 1));
+(0 rows)
+
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 2));
+(0 rows)
+
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 3));
+-[ RECORD 1 ]-----------------------
+itemoffset | 1
+ctid       | (2359296,12328)
+data       | 46 17 73 8e 00 00 00 00
+
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 4));
+(0 rows)
+
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 5));
+(0 rows)
+
+DROP TABLE test1;
diff --git a/contrib/pageinspect/sql/hash.sql b/contrib/pageinspect/sql/hash.sql
new file mode 100644
index 0000000..5f5f6f8
--- /dev/null
+++ b/contrib/pageinspect/sql/hash.sql
@@ -0,0 +1,29 @@
+CREATE TABLE test1 (a int, b text);
+INSERT INTO test1 VALUES (1, 'one');
+CREATE INDEX test1_a_idx ON test1 USING hash (a);
+
+\x
+
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 0));
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 1));
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 2));
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 3));
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 4));
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 5));
+SELECT * FROM hash_metapage_info(get_raw_page('test1_a_idx', 6));
+
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 0));
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 1));
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 2));
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 3));
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 4));
+SELECT * FROM hash_page_stats(get_raw_page('test1_a_idx', 5));
+
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 0));
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 1));
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 2));
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 3));
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 4));
+SELECT * FROM hash_page_items(get_raw_page('test1_a_idx', 5));
+
+DROP TABLE test1;
-- 
2.10.0

