v2-0001-Add-functions-to-pageinspect-to-inspect-GiST-inde.patch
application/octet-stream
Filename: v2-0001-Add-functions-to-pageinspect-to-inspect-GiST-inde.patch
Type: application/octet-stream
Part: 0
Message:
Re: Yet another fast GiST build
Patch
Format: format-patch
Series: patch v2-0001
Subject: Add functions to 'pageinspect' to inspect GiST indexes.
| File | + | − |
|---|---|---|
| contrib/pageinspect/expected/gist.out | 140 | 0 |
| contrib/pageinspect/gistfuncs.c | 173 | 0 |
| contrib/pageinspect/Makefile | 4 | 2 |
| contrib/pageinspect/pageinspect--1.8--1.9.sql | 27 | 0 |
| contrib/pageinspect/pageinspect.control | 1 | 1 |
| contrib/pageinspect/sql/gist.sql | 15 | 0 |
| doc/src/sgml/pageinspect.sgml | 60 | 0 |
From 078d759299ab76f1650309ffa4416f0c084be328 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 28 Sep 2020 11:01:45 +0300
Subject: [PATCH v2] Add functions to 'pageinspect' to inspect GiST indexes.
---
contrib/pageinspect/Makefile | 6 +-
contrib/pageinspect/expected/gist.out | 140 ++++++++++++++
contrib/pageinspect/gistfuncs.c | 173 ++++++++++++++++++
contrib/pageinspect/pageinspect--1.8--1.9.sql | 27 +++
contrib/pageinspect/pageinspect.control | 2 +-
contrib/pageinspect/sql/gist.sql | 15 ++
doc/src/sgml/pageinspect.sgml | 60 ++++++
7 files changed, 420 insertions(+), 3 deletions(-)
create mode 100644 contrib/pageinspect/expected/gist.out
create mode 100644 contrib/pageinspect/gistfuncs.c
create mode 100644 contrib/pageinspect/pageinspect--1.8--1.9.sql
create mode 100644 contrib/pageinspect/sql/gist.sql
diff --git a/contrib/pageinspect/Makefile b/contrib/pageinspect/Makefile
index d9d8177116..4539f0aef7 100644
--- a/contrib/pageinspect/Makefile
+++ b/contrib/pageinspect/Makefile
@@ -7,19 +7,21 @@ OBJS = \
btreefuncs.o \
fsmfuncs.o \
ginfuncs.o \
+ gistfuncs.o \
hashfuncs.o \
heapfuncs.o \
rawpage.o
EXTENSION = pageinspect
-DATA = pageinspect--1.7--1.8.sql pageinspect--1.6--1.7.sql \
+DATA = pageinspect--1.8--1.9.sql \
+ pageinspect--1.7--1.8.sql pageinspect--1.6--1.7.sql \
pageinspect--1.5.sql pageinspect--1.5--1.6.sql \
pageinspect--1.4--1.5.sql pageinspect--1.3--1.4.sql \
pageinspect--1.2--1.3.sql pageinspect--1.1--1.2.sql \
pageinspect--1.0--1.1.sql
PGFILEDESC = "pageinspect - functions to inspect contents of database pages"
-REGRESS = page btree brin gin hash checksum
+REGRESS = page btree brin gin gist hash checksum
ifdef USE_PGXS
PG_CONFIG = pg_config
diff --git a/contrib/pageinspect/expected/gist.out b/contrib/pageinspect/expected/gist.out
new file mode 100644
index 0000000000..9bcb9d402e
--- /dev/null
+++ b/contrib/pageinspect/expected/gist.out
@@ -0,0 +1,140 @@
+CREATE TABLE test_gist AS SELECT point(i,i) p, i::text t FROM
+ generate_series(1,1000) i;
+CREATE INDEX test_gist_idx ON test_gist USING gist (p);
+\x
+SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 0));
+-[ RECORD 1 ]---------
+lsn | 0/1
+nsn | 0/0
+rightlink | 4294967295
+flags | {}
+
+SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 1));
+-[ RECORD 1 ]---------
+lsn | 0/1
+nsn | 0/0
+rightlink | 4294967295
+flags | {leaf}
+
+SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 2));
+-[ RECORD 1 ]-----
+lsn | 0/1
+nsn | 0/0
+rightlink | 1
+flags | {leaf}
+
+SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 0));
+-[ RECORD 1 ]---------
+itemoffset | 1
+ctid | (1,65535)
+itemlen | 40
+-[ RECORD 2 ]---------
+itemoffset | 2
+ctid | (2,65535)
+itemlen | 40
+-[ RECORD 3 ]---------
+itemoffset | 3
+ctid | (3,65535)
+itemlen | 40
+-[ RECORD 4 ]---------
+itemoffset | 4
+ctid | (4,65535)
+itemlen | 40
+-[ RECORD 5 ]---------
+itemoffset | 5
+ctid | (5,65535)
+itemlen | 40
+-[ RECORD 6 ]---------
+itemoffset | 6
+ctid | (6,65535)
+itemlen | 40
+-[ RECORD 7 ]---------
+itemoffset | 7
+ctid | (7,65535)
+itemlen | 40
+
+SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 1)) LIMIT 10;
+-[ RECORD 1 ]------
+itemoffset | 1
+ctid | (0,1)
+itemlen | 40
+-[ RECORD 2 ]------
+itemoffset | 2
+ctid | (0,2)
+itemlen | 40
+-[ RECORD 3 ]------
+itemoffset | 3
+ctid | (0,3)
+itemlen | 40
+-[ RECORD 4 ]------
+itemoffset | 4
+ctid | (0,4)
+itemlen | 40
+-[ RECORD 5 ]------
+itemoffset | 5
+ctid | (0,5)
+itemlen | 40
+-[ RECORD 6 ]------
+itemoffset | 6
+ctid | (0,6)
+itemlen | 40
+-[ RECORD 7 ]------
+itemoffset | 7
+ctid | (0,7)
+itemlen | 40
+-[ RECORD 8 ]------
+itemoffset | 8
+ctid | (0,8)
+itemlen | 40
+-[ RECORD 9 ]------
+itemoffset | 9
+ctid | (0,9)
+itemlen | 40
+-[ RECORD 10 ]-----
+itemoffset | 10
+ctid | (0,10)
+itemlen | 40
+
+SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 2)) LIMIT 10;
+-[ RECORD 1 ]------
+itemoffset | 1
+ctid | (1,10)
+itemlen | 40
+-[ RECORD 2 ]------
+itemoffset | 2
+ctid | (1,11)
+itemlen | 40
+-[ RECORD 3 ]------
+itemoffset | 3
+ctid | (1,12)
+itemlen | 40
+-[ RECORD 4 ]------
+itemoffset | 4
+ctid | (1,13)
+itemlen | 40
+-[ RECORD 5 ]------
+itemoffset | 5
+ctid | (1,14)
+itemlen | 40
+-[ RECORD 6 ]------
+itemoffset | 6
+ctid | (1,15)
+itemlen | 40
+-[ RECORD 7 ]------
+itemoffset | 7
+ctid | (1,16)
+itemlen | 40
+-[ RECORD 8 ]------
+itemoffset | 8
+ctid | (1,17)
+itemlen | 40
+-[ RECORD 9 ]------
+itemoffset | 9
+ctid | (1,18)
+itemlen | 40
+-[ RECORD 10 ]-----
+itemoffset | 10
+ctid | (1,19)
+itemlen | 40
+
+DROP TABLE test1;
diff --git a/contrib/pageinspect/gistfuncs.c b/contrib/pageinspect/gistfuncs.c
new file mode 100644
index 0000000000..8517b4c241
--- /dev/null
+++ b/contrib/pageinspect/gistfuncs.c
@@ -0,0 +1,173 @@
+/*
+ * gistfuncs.c
+ * Functions to investigate the content of GiST indexes
+ *
+ * Copyright (c) 2014-2020, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * contrib/pageinspect/gitfuncs.c
+ */
+#include "postgres.h"
+
+#include "access/gist.h"
+#include "access/htup.h"
+#include "funcapi.h"
+#include "miscadmin.h"
+#include "pageinspect.h"
+#include "storage/itemptr.h"
+#include "utils/array.h"
+#include "utils/builtins.h"
+#include "utils/pg_lsn.h"
+
+PG_FUNCTION_INFO_V1(gist_page_opaque_info);
+PG_FUNCTION_INFO_V1(gist_page_items);
+
+#define ItemPointerGetDatum(X) PointerGetDatum(X)
+
+
+Datum
+gist_page_opaque_info(PG_FUNCTION_ARGS)
+{
+ bytea *raw_page = PG_GETARG_BYTEA_P(0);
+ TupleDesc tupdesc;
+ Page page;
+ GISTPageOpaque opaq;
+ HeapTuple resultTuple;
+ Datum values[4];
+ bool nulls[4];
+ Datum flags[16];
+ int nflags = 0;
+ uint16 flagbits;
+
+ if (!superuser())
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ errmsg("must be superuser to use raw page functions")));
+
+ page = get_page_from_raw(raw_page);
+
+ opaq = (GISTPageOpaque) PageGetSpecialPointer(page);
+
+ /* Build a tuple descriptor for our result type */
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+
+ /* Convert the flags bitmask to an array of human-readable names */
+ flagbits = opaq->flags;
+ if (flagbits & F_LEAF)
+ flags[nflags++] = CStringGetTextDatum("leaf");
+ if (flagbits & F_DELETED)
+ flags[nflags++] = CStringGetTextDatum("deleted");
+ if (flagbits & F_TUPLES_DELETED)
+ flags[nflags++] = CStringGetTextDatum("tuples_deleted");
+ if (flagbits & F_FOLLOW_RIGHT)
+ flags[nflags++] = CStringGetTextDatum("follow_right");
+ if (flagbits & F_HAS_GARBAGE)
+ flags[nflags++] = CStringGetTextDatum("has_garbage");
+ flagbits &= ~(F_LEAF | F_DELETED | F_TUPLES_DELETED | F_FOLLOW_RIGHT | F_HAS_GARBAGE);
+ if (flagbits)
+ {
+ /* any flags we don't recognize are printed in hex */
+ flags[nflags++] = DirectFunctionCall1(to_hex32, Int32GetDatum(flagbits));
+ }
+
+ memset(nulls, 0, sizeof(nulls));
+
+ values[0] = LSNGetDatum(PageGetLSN(page));
+ values[1] = LSNGetDatum(GistPageGetNSN(page));
+ values[2] = Int64GetDatum(opaq->rightlink);
+ values[3] = PointerGetDatum(construct_array(flags, nflags,
+ TEXTOID,
+ -1, false, TYPALIGN_INT));
+
+ /* Build and return the result tuple. */
+ resultTuple = heap_form_tuple(tupdesc, values, nulls);
+
+ return HeapTupleGetDatum(resultTuple);
+}
+
+typedef struct gist_page_items_state
+{
+ Page page;
+ TupleDesc tupd;
+ OffsetNumber offset;
+} gist_page_items_state;
+
+Datum
+gist_page_items(PG_FUNCTION_ARGS)
+{
+ bytea *raw_page = PG_GETARG_BYTEA_P(0);
+ FuncCallContext *fctx;
+ gist_page_items_state *inter_call_data;
+
+ if (!superuser())
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ errmsg("must be superuser to use raw page functions")));
+
+ if (SRF_IS_FIRSTCALL())
+ {
+ TupleDesc tupdesc;
+ MemoryContext mctx;
+ Page page;
+
+ fctx = SRF_FIRSTCALL_INIT();
+ mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
+
+ page = get_page_from_raw(raw_page);
+
+ inter_call_data = palloc(sizeof(gist_page_items_state));
+
+ /* Build a tuple descriptor for our result type */
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+
+ inter_call_data->page = page;
+ inter_call_data->tupd = tupdesc;
+ inter_call_data->offset = FirstOffsetNumber;
+
+ fctx->max_calls = PageGetMaxOffsetNumber(page);
+ fctx->user_fctx = inter_call_data;
+
+ MemoryContextSwitchTo(mctx);
+ }
+
+ fctx = SRF_PERCALL_SETUP();
+ inter_call_data = fctx->user_fctx;
+
+ if (fctx->call_cntr < fctx->max_calls)
+ {
+ Page page = inter_call_data->page;
+ OffsetNumber offset = inter_call_data->offset;
+ HeapTuple resultTuple;
+ Datum result;
+ Datum values[3];
+ bool nulls[3];
+ ItemId id;
+ IndexTuple itup;
+
+ id = PageGetItemId(page, offset);
+
+ if (!ItemIdIsValid(id))
+ elog(ERROR, "invalid ItemId");
+
+ itup = (IndexTuple) PageGetItem(page, id);
+
+ memset(nulls, 0, sizeof(nulls));
+
+ values[0] = DatumGetInt16(offset);
+ values[1] = ItemPointerGetDatum(&itup->t_tid);
+ values[2] = Int32GetDatum((int) IndexTupleSize(itup));
+
+ /* TODO: also print the keys */
+
+ /* Build and return the result tuple. */
+ resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
+ result = HeapTupleGetDatum(resultTuple);
+
+ inter_call_data->offset++;
+ SRF_RETURN_NEXT(fctx, result);
+ }
+
+ SRF_RETURN_DONE(fctx);
+}
diff --git a/contrib/pageinspect/pageinspect--1.8--1.9.sql b/contrib/pageinspect/pageinspect--1.8--1.9.sql
new file mode 100644
index 0000000000..e7a849cfed
--- /dev/null
+++ b/contrib/pageinspect/pageinspect--1.8--1.9.sql
@@ -0,0 +1,27 @@
+/* contrib/pageinspect/pageinspect--1.8--1.9.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION pageinspect UPDATE TO '1.9'" to load this file. \quit
+
+--
+-- gist_page_opaque_info()
+--
+CREATE FUNCTION gist_page_opaque_info(IN page bytea,
+ OUT lsn pg_lsn,
+ OUT nsn pg_lsn,
+ OUT rightlink bigint,
+ OUT flags text[])
+AS 'MODULE_PATHNAME', 'gist_page_opaque_info'
+LANGUAGE C STRICT PARALLEL SAFE;
+
+
+--
+-- gist_page_items()
+--
+CREATE FUNCTION gist_page_items(IN page bytea,
+ OUT itemoffset smallint,
+ OUT ctid tid,
+ OUT itemlen smallint)
+RETURNS SETOF record
+AS 'MODULE_PATHNAME', 'gist_page_items'
+LANGUAGE C STRICT PARALLEL SAFE;
diff --git a/contrib/pageinspect/pageinspect.control b/contrib/pageinspect/pageinspect.control
index f8cdf526c6..bd716769a1 100644
--- a/contrib/pageinspect/pageinspect.control
+++ b/contrib/pageinspect/pageinspect.control
@@ -1,5 +1,5 @@
# pageinspect extension
comment = 'inspect the contents of database pages at a low level'
-default_version = '1.8'
+default_version = '1.9'
module_pathname = '$libdir/pageinspect'
relocatable = true
diff --git a/contrib/pageinspect/sql/gist.sql b/contrib/pageinspect/sql/gist.sql
new file mode 100644
index 0000000000..12be3cd76c
--- /dev/null
+++ b/contrib/pageinspect/sql/gist.sql
@@ -0,0 +1,15 @@
+CREATE TABLE test_gist AS SELECT point(i,i) p, i::text t FROM
+ generate_series(1,1000) i;
+CREATE INDEX test_gist_idx ON test_gist USING gist (p);
+
+\x
+
+SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 0));
+SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 1));
+SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 2));
+
+SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 0));
+SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 1)) LIMIT 10;
+SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 2)) LIMIT 10;
+
+DROP TABLE test1;
diff --git a/doc/src/sgml/pageinspect.sgml b/doc/src/sgml/pageinspect.sgml
index 687c3606ba..32cfde3628 100644
--- a/doc/src/sgml/pageinspect.sgml
+++ b/doc/src/sgml/pageinspect.sgml
@@ -671,6 +671,66 @@ test=# SELECT first_tid, nbytes, tids[0:5] AS some_tids
</variablelist>
</sect2>
+ <sect2>
+ <title>GiST Functions</title>
+
+ <variablelist>
+ <varlistentry>
+ <term>
+ <function>gist_page_opaque_info(page bytea) returns record</function>
+ <indexterm>
+ <primary>gist_page_opaque_info</primary>
+ </indexterm>
+ </term>
+
+ <listitem>
+ <para>
+ <function>gist_page_opaque_info</function> returns information about
+ a <acronym>GiST</acronym> index opaque area, like the NSN, rightlink and
+ page type.
+ For example:
+<screen>
+test=# SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 2));
+ lsn | nsn | rightlink | flags
+-----+-----+-----------+--------
+ 0/1 | 0/0 | 1 | {leaf}
+(1 row)
+</screen>
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>
+ <function>gist_page_items(page bytea) returns setof record</function>
+ <indexterm>
+ <primary>gist_page_items</primary>
+ </indexterm>
+ </term>
+
+ <listitem>
+ <para>
+ <function>gist_page_items</function> returns information about
+ the data stored in a <acronym>GiST</acronym> page. For example:
+<screen>
+test=# SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 0));
+ itemoffset | ctid | itemlen
+------------+-----------+---------
+ 1 | (1,65535) | 40
+ 2 | (2,65535) | 40
+ 3 | (3,65535) | 40
+ 4 | (4,65535) | 40
+ 5 | (5,65535) | 40
+ 6 | (6,65535) | 40
+ 7 | (7,65535) | 40
+(7 rows)
+</screen>
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </sect2>
+
<sect2>
<title>Hash Functions</title>
--
2.24.3 (Apple Git-128)