v27-0002-Teach-pageinspect-about-nbtree-posting-lists.patch
application/octet-stream
Filename: v27-0002-Teach-pageinspect-about-nbtree-posting-lists.patch
Type: application/octet-stream
Part: 0
Patch
Format: format-patch
Series: patch v27-0002
Subject: Teach pageinspect about nbtree posting lists.
| File | + | − |
|---|---|---|
| contrib/pageinspect/btreefuncs.c | 93 | 18 |
| contrib/pageinspect/expected/btree.out | 6 | 0 |
| contrib/pageinspect/pageinspect--1.7--1.8.sql | 36 | 0 |
| doc/src/sgml/pageinspect.sgml | 46 | 34 |
From ee67a907f9a609d3106f9de82b4d7d6f056e582d Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@bowt.ie>
Date: Mon, 10 Sep 2018 19:53:51 -0700
Subject: [PATCH v27 2/3] Teach pageinspect about nbtree posting lists.
Add a column for posting list TIDs to bt_page_items(). Also add a
column that displays a single heap TID value for each tuple, regardless
of whether or not "ctid" is used for heap TID. In the case of posting
list tuples, the value is the lowest heap TID in the posting list.
Arguably I should have done this when commit dd299df8 went in, since
that added a pivot tuple representation that could have a heap TID but
didn't use ctid for that purpose.
Also add a boolean column that displays the LP_DEAD bit value for each
non-pivot tuple.
No version bump for the pageinspect extension, since there hasn't been a
stable release since the last version bump (see commit 58b4cb30).
---
contrib/pageinspect/btreefuncs.c | 111 +++++++++++++++---
contrib/pageinspect/expected/btree.out | 6 +
contrib/pageinspect/pageinspect--1.7--1.8.sql | 36 ++++++
doc/src/sgml/pageinspect.sgml | 80 +++++++------
4 files changed, 181 insertions(+), 52 deletions(-)
diff --git a/contrib/pageinspect/btreefuncs.c b/contrib/pageinspect/btreefuncs.c
index 78cdc69ec7..17f7ad186e 100644
--- a/contrib/pageinspect/btreefuncs.c
+++ b/contrib/pageinspect/btreefuncs.c
@@ -31,9 +31,11 @@
#include "access/relation.h"
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
+#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pageinspect.h"
+#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/varlena.h"
@@ -45,6 +47,8 @@ PG_FUNCTION_INFO_V1(bt_page_stats);
#define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
#define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
+#define DatumGetItemPointer(X) ((ItemPointer) DatumGetPointer(X))
+#define ItemPointerGetDatum(X) PointerGetDatum(X)
/* note: BlockNumber is unsigned, hence can't be negative */
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno) { \
@@ -243,6 +247,9 @@ struct user_args
{
Page page;
OffsetNumber offset;
+ bool leafpage;
+ bool rightmost;
+ TupleDesc tupd;
};
/*-------------------------------------------------------
@@ -252,17 +259,25 @@ struct user_args
* ------------------------------------------------------
*/
static Datum
-bt_page_print_tuples(FuncCallContext *fctx, Page page, OffsetNumber offset)
+bt_page_print_tuples(FuncCallContext *fctx, struct user_args *uargs)
{
- char *values[6];
+ Page page = uargs->page;
+ OffsetNumber offset = uargs->offset;
+ bool leafpage = uargs->leafpage;
+ bool rightmost = uargs->rightmost;
+ bool pivotoffset;
+ Datum values[9];
+ bool nulls[9];
HeapTuple tuple;
ItemId id;
IndexTuple itup;
int j;
int off;
int dlen;
- char *dump;
+ char *dump,
+ *datacstring;
char *ptr;
+ ItemPointer htid;
id = PageGetItemId(page, offset);
@@ -272,18 +287,27 @@ bt_page_print_tuples(FuncCallContext *fctx, Page page, OffsetNumber offset)
itup = (IndexTuple) PageGetItem(page, id);
j = 0;
- values[j++] = psprintf("%d", offset);
- values[j++] = psprintf("(%u,%u)",
- ItemPointerGetBlockNumberNoCheck(&itup->t_tid),
- ItemPointerGetOffsetNumberNoCheck(&itup->t_tid));
- values[j++] = psprintf("%d", (int) IndexTupleSize(itup));
- values[j++] = psprintf("%c", IndexTupleHasNulls(itup) ? 't' : 'f');
- values[j++] = psprintf("%c", IndexTupleHasVarwidths(itup) ? 't' : 'f');
+ memset(nulls, 0, sizeof(nulls));
+ values[j++] = DatumGetInt16(offset);
+ values[j++] = ItemPointerGetDatum(&itup->t_tid);
+ values[j++] = Int32GetDatum((int) IndexTupleSize(itup));
+ values[j++] = BoolGetDatum(IndexTupleHasNulls(itup));
+ values[j++] = BoolGetDatum(IndexTupleHasVarwidths(itup));
ptr = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
dlen = IndexTupleSize(itup) - IndexInfoFindDataOffset(itup->t_info);
+
+ /*
+ * Make sure that "data" column does not include posting list or pivot
+ * tuple representation of heap TID
+ */
+ if (BTreeTupleIsPosting(itup))
+ dlen -= IndexTupleSize(itup) - BTreeTupleGetPostingOffset(itup);
+ else if (BTreeTupleIsPivot(itup) && BTreeTupleGetHeapTID(itup) != NULL)
+ dlen -= MAXALIGN(sizeof(ItemPointerData));
+
dump = palloc0(dlen * 3 + 1);
- values[j] = dump;
+ datacstring = dump;
for (off = 0; off < dlen; off++)
{
if (off > 0)
@@ -291,8 +315,57 @@ bt_page_print_tuples(FuncCallContext *fctx, Page page, OffsetNumber offset)
sprintf(dump, "%02x", *(ptr + off) & 0xff);
dump += 2;
}
+ values[j++] = CStringGetTextDatum(datacstring);
+ pfree(datacstring);
- tuple = BuildTupleFromCStrings(fctx->attinmeta, values);
+ /*
+ * Avoid indicating that pivot tuple from !heapkeyspace index (which won't
+ * have v4+ status bit set) is dead or has a heap TID -- that can only
+ * happen with non-pivot tuples. (Most backend code can use the
+ * heapkeyspace field from the metapage to figure out which representation
+ * to expect, but we have to be a bit creative here.)
+ */
+ pivotoffset = (!leafpage || (!rightmost && offset == P_HIKEY));
+
+ /* LP_DEAD status bit */
+ if (!pivotoffset)
+ values[j++] = BoolGetDatum(ItemIdIsDead(id));
+ else
+ nulls[j++] = true;
+
+ htid = BTreeTupleGetHeapTID(itup);
+ if (pivotoffset && !BTreeTupleIsPivot(itup))
+ htid = NULL;
+
+ if (htid)
+ values[j++] = ItemPointerGetDatum(htid);
+ else
+ nulls[j++] = true;
+
+ if (BTreeTupleIsPosting(itup))
+ {
+ /* build an array of item pointers */
+ ItemPointer tids;
+ Datum *tids_datum;
+ int nposting;
+
+ tids = BTreeTupleGetPosting(itup);
+ nposting = BTreeTupleGetNPosting(itup);
+ tids_datum = (Datum *) palloc(nposting * sizeof(Datum));
+ for (int i = 0; i < nposting; i++)
+ tids_datum[i] = ItemPointerGetDatum(&tids[i]);
+ values[j++] = PointerGetDatum(construct_array(tids_datum,
+ nposting,
+ TIDOID,
+ sizeof(ItemPointerData),
+ false, 's'));
+ pfree(tids_datum);
+ }
+ else
+ nulls[j++] = true;
+
+ /* Build and return the result tuple */
+ tuple = heap_form_tuple(uargs->tupd, values, nulls);
return HeapTupleGetDatum(tuple);
}
@@ -378,12 +451,13 @@ bt_page_items(PG_FUNCTION_ARGS)
elog(NOTICE, "page is deleted");
fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
+ uargs->leafpage = P_ISLEAF(opaque);
+ uargs->rightmost = P_RIGHTMOST(opaque);
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
-
- fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
+ uargs->tupd = tupleDesc;
fctx->user_fctx = uargs;
@@ -395,7 +469,7 @@ bt_page_items(PG_FUNCTION_ARGS)
if (fctx->call_cntr < fctx->max_calls)
{
- result = bt_page_print_tuples(fctx, uargs->page, uargs->offset);
+ result = bt_page_print_tuples(fctx, uargs);
uargs->offset++;
SRF_RETURN_NEXT(fctx, result);
}
@@ -463,12 +537,13 @@ bt_page_items_bytea(PG_FUNCTION_ARGS)
elog(NOTICE, "page is deleted");
fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
+ uargs->leafpage = P_ISLEAF(opaque);
+ uargs->rightmost = P_RIGHTMOST(opaque);
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
-
- fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
+ uargs->tupd = tupleDesc;
fctx->user_fctx = uargs;
@@ -480,7 +555,7 @@ bt_page_items_bytea(PG_FUNCTION_ARGS)
if (fctx->call_cntr < fctx->max_calls)
{
- result = bt_page_print_tuples(fctx, uargs->page, uargs->offset);
+ result = bt_page_print_tuples(fctx, uargs);
uargs->offset++;
SRF_RETURN_NEXT(fctx, result);
}
diff --git a/contrib/pageinspect/expected/btree.out b/contrib/pageinspect/expected/btree.out
index 07c2dcd771..1d45cd5c1e 100644
--- a/contrib/pageinspect/expected/btree.out
+++ b/contrib/pageinspect/expected/btree.out
@@ -41,6 +41,9 @@ itemlen | 16
nulls | f
vars | f
data | 01 00 00 00 00 00 00 01
+dead | f
+htid | (0,1)
+tids |
SELECT * FROM bt_page_items('test1_a_idx', 2);
ERROR: block number out of range
@@ -54,6 +57,9 @@ itemlen | 16
nulls | f
vars | f
data | 01 00 00 00 00 00 00 01
+dead | f
+htid | (0,1)
+tids |
SELECT * FROM bt_page_items(get_raw_page('test1_a_idx', 2));
ERROR: block number 2 is out of range for relation "test1_a_idx"
diff --git a/contrib/pageinspect/pageinspect--1.7--1.8.sql b/contrib/pageinspect/pageinspect--1.7--1.8.sql
index 2a7c4b3516..70f1ab0467 100644
--- a/contrib/pageinspect/pageinspect--1.7--1.8.sql
+++ b/contrib/pageinspect/pageinspect--1.7--1.8.sql
@@ -14,3 +14,39 @@ CREATE FUNCTION heap_tuple_infomask_flags(
RETURNS record
AS 'MODULE_PATHNAME', 'heap_tuple_infomask_flags'
LANGUAGE C STRICT PARALLEL SAFE;
+
+--
+-- bt_page_items(text, int4)
+--
+DROP FUNCTION bt_page_items(text, int4);
+CREATE FUNCTION bt_page_items(IN relname text, IN blkno int4,
+ OUT itemoffset smallint,
+ OUT ctid tid,
+ OUT itemlen smallint,
+ OUT nulls bool,
+ OUT vars bool,
+ OUT data text,
+ OUT dead boolean,
+ OUT htid tid,
+ OUT tids tid[])
+RETURNS SETOF record
+AS 'MODULE_PATHNAME', 'bt_page_items'
+LANGUAGE C STRICT PARALLEL SAFE;
+
+--
+-- bt_page_items(bytea)
+--
+DROP FUNCTION bt_page_items(bytea);
+CREATE FUNCTION bt_page_items(IN page bytea,
+ OUT itemoffset smallint,
+ OUT ctid tid,
+ OUT itemlen smallint,
+ OUT nulls bool,
+ OUT vars bool,
+ OUT data text,
+ OUT dead boolean,
+ OUT htid tid,
+ OUT tids tid[])
+RETURNS SETOF record
+AS 'MODULE_PATHNAME', 'bt_page_items_bytea'
+LANGUAGE C STRICT PARALLEL SAFE;
diff --git a/doc/src/sgml/pageinspect.sgml b/doc/src/sgml/pageinspect.sgml
index 7e2e1487d7..1763e9c6f0 100644
--- a/doc/src/sgml/pageinspect.sgml
+++ b/doc/src/sgml/pageinspect.sgml
@@ -329,11 +329,11 @@ test=# SELECT * FROM bt_page_stats('pg_cast_oid_index', 1);
-[ RECORD 1 ]-+-----
blkno | 1
type | l
-live_items | 256
+live_items | 224
dead_items | 0
-avg_item_size | 12
+avg_item_size | 16
page_size | 8192
-free_size | 4056
+free_size | 3668
btpo_prev | 0
btpo_next | 0
btpo | 0
@@ -356,33 +356,45 @@ btpo_flags | 3
<function>bt_page_items</function> returns detailed information about
all of the items on a B-tree index page. For example:
<screen>
-test=# SELECT * FROM bt_page_items('pg_cast_oid_index', 1);
- itemoffset | ctid | itemlen | nulls | vars | data
-------------+---------+---------+-------+------+-------------
- 1 | (0,1) | 12 | f | f | 23 27 00 00
- 2 | (0,2) | 12 | f | f | 24 27 00 00
- 3 | (0,3) | 12 | f | f | 25 27 00 00
- 4 | (0,4) | 12 | f | f | 26 27 00 00
- 5 | (0,5) | 12 | f | f | 27 27 00 00
- 6 | (0,6) | 12 | f | f | 28 27 00 00
- 7 | (0,7) | 12 | f | f | 29 27 00 00
- 8 | (0,8) | 12 | f | f | 2a 27 00 00
+regression=# SELECT * FROM bt_page_items('tenk2_unique1', 5);
+ itemoffset | ctid | itemlen | nulls | vars | data | dead | htid | tids
+------------+----------+---------+-------+------+-------------------------+------+----------+------
+ 1 | (40,1) | 16 | f | f | b8 05 00 00 00 00 00 00 | | |
+ 2 | (58,11) | 16 | f | f | 4a 04 00 00 00 00 00 00 | f | (58,11) |
+ 3 | (266,4) | 16 | f | f | 4b 04 00 00 00 00 00 00 | f | (266,4) |
+ 4 | (279,25) | 16 | f | f | 4c 04 00 00 00 00 00 00 | f | (279,25) |
+ 5 | (333,11) | 16 | f | f | 4d 04 00 00 00 00 00 00 | f | (333,11) |
+ 6 | (87,24) | 16 | f | f | 4e 04 00 00 00 00 00 00 | f | (87,24) |
+ 7 | (38,22) | 16 | f | f | 4f 04 00 00 00 00 00 00 | f | (38,22) |
+ 8 | (272,17) | 16 | f | f | 50 04 00 00 00 00 00 00 | f | (272,17) |
</screen>
- In a B-tree leaf page, <structfield>ctid</structfield> points to a heap tuple.
- In an internal page, the block number part of <structfield>ctid</structfield>
- points to another page in the index itself, while the offset part
- (the second number) is ignored and is usually 1.
+ In a B-tree leaf page, <structfield>ctid</structfield> usually
+ points to a heap tuple, and <structfield>dead</structfield> may
+ indicate that the item has its <literal>LP_DEAD</literal> bit
+ set. In an internal page, the block number part of
+ <structfield>ctid</structfield> points to another page in the
+ index itself, while the offset part (the second number) encodes
+ metadata about the tuple. Posting list tuples on leaf pages
+ also use <structfield>ctid</structfield> for metadata.
+ <structfield>htid</structfield> always shows a single heap TID
+ for the tuple, regardless of how it is represented (internal
+ page tuples may need to store a heap TID when there are many
+ duplicate tuples on descendent leaf pages).
+ <structfield>tids</structfield> is a list of TIDs that is stored
+ within posting list tuples (tuples created by deduplication).
</para>
<para>
Note that the first item on any non-rightmost page (any page with
a non-zero value in the <structfield>btpo_next</structfield> field) is the
page's <quote>high key</quote>, meaning its <structfield>data</structfield>
serves as an upper bound on all items appearing on the page, while
- its <structfield>ctid</structfield> field is meaningless. Also, on non-leaf
- pages, the first real data item (the first item that is not a high
- key) is a <quote>minus infinity</quote> item, with no actual value
- in its <structfield>data</structfield> field. Such an item does have a valid
- downlink in its <structfield>ctid</structfield> field, however.
+ its <structfield>ctid</structfield> field does not point to
+ another block. Also, on non-leaf pages, the first real data item
+ (the first item that is not a high key) is a <quote>minus
+ infinity</quote> item, with no actual value in its
+ <structfield>data</structfield> field. Such an item does have a
+ valid downlink in its <structfield>ctid</structfield> field,
+ however.
</para>
</listitem>
</varlistentry>
@@ -402,17 +414,17 @@ test=# SELECT * FROM bt_page_items('pg_cast_oid_index', 1);
with <function>get_raw_page</function> should be passed as argument. So
the last example could also be rewritten like this:
<screen>
-test=# SELECT * FROM bt_page_items(get_raw_page('pg_cast_oid_index', 1));
- itemoffset | ctid | itemlen | nulls | vars | data
-------------+---------+---------+-------+------+-------------
- 1 | (0,1) | 12 | f | f | 23 27 00 00
- 2 | (0,2) | 12 | f | f | 24 27 00 00
- 3 | (0,3) | 12 | f | f | 25 27 00 00
- 4 | (0,4) | 12 | f | f | 26 27 00 00
- 5 | (0,5) | 12 | f | f | 27 27 00 00
- 6 | (0,6) | 12 | f | f | 28 27 00 00
- 7 | (0,7) | 12 | f | f | 29 27 00 00
- 8 | (0,8) | 12 | f | f | 2a 27 00 00
+regression=# SELECT * FROM bt_page_items(get_raw_page('tenk2_unique1', 5));
+ itemoffset | ctid | itemlen | nulls | vars | data | dead | htid | tids
+------------+----------+---------+-------+------+-------------------------+------+----------+------
+ 1 | (40,1) | 16 | f | f | b8 05 00 00 00 00 00 00 | | |
+ 2 | (58,11) | 16 | f | f | 4a 04 00 00 00 00 00 00 | f | (58,11) |
+ 3 | (266,4) | 16 | f | f | 4b 04 00 00 00 00 00 00 | f | (266,4) |
+ 4 | (279,25) | 16 | f | f | 4c 04 00 00 00 00 00 00 | f | (279,25) |
+ 5 | (333,11) | 16 | f | f | 4d 04 00 00 00 00 00 00 | f | (333,11) |
+ 6 | (87,24) | 16 | f | f | 4e 04 00 00 00 00 00 00 | f | (87,24) |
+ 7 | (38,22) | 16 | f | f | 4f 04 00 00 00 00 00 00 | f | (38,22) |
+ 8 | (272,17) | 16 | f | f | 50 04 00 00 00 00 00 00 | f | (272,17) |
</screen>
All the other details are the same as explained in the previous item.
</para>
--
2.17.1