Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pzSn0-0001qA-J5 for pgsql-bugs@arkaria.postgresql.org; Thu, 18 May 2023 01:50:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pzSmy-0004sp-Hl for pgsql-bugs@arkaria.postgresql.org; Thu, 18 May 2023 01:50:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pzSmw-0004i6-TW for pgsql-bugs@lists.postgresql.org; Thu, 18 May 2023 01:50:44 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pzSmr-000PNF-4U for pgsql-bugs@lists.postgresql.org; Thu, 18 May 2023 01:50:41 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 9CDA25C0139; Wed, 17 May 2023 21:50:35 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 17 May 2023 21:50:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm1; t=1684374635; x=1684461035; bh=0j dHKgCbsj7+FzmeVZ1Dtfxa2OwfydjZv2J3J6NxTRE=; b=Ovqu4L9Unf/BNN03r8 ng6cYvBUqJ7aoiXQNyfq8/JvXe765Ijn6G58m6KONJpRHrecp0QQMZ1c9VnWm02U YWofCjxSCCR+RXxMq6iP9moSLspKV+ceKlpJlIiwl9f/19wwOJHUVT3cPGhYZy9e ADtFc0tBHvlOSgGIzcnKD4c1lNAkiGb0JOAlQAgTSLXUwuXEdv3gv+gJUee3ZgfM AGflyDbA4yIUfAglBsZdZ1VhlybPsXpbAGG8tGUEvOxVNMv1zJ750v5y0nh0Sv1u axvScj9nipE9m4rga0DMeubUC+FYevn3FEPDGf0WdGWqo7avG9vr25RryJI4cq9b YMLA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1684374635; x=1684461035; bh=0jdHKgCbsj7+F zmeVZ1Dtfxa2OwfydjZv2J3J6NxTRE=; b=pzU9CqiJ7Tn9E3/ApdZiCW7zUywGs 2gbwX3+qvJVkURaZsrNFITlKRyk2li6Kb38szpTFf7ZjuTsv/45vDX9qNw87+KkO Rf4VCE59jho1MQ87p5tFufZhbToHLx7Jl2PQYmsEFLPAWJm+otoqVsEqZR2B423v aDia9UKU3a9SRGFvz+PigO8zC6pX/pLQl1uXOalOJfvMYd4R+18VBVb8/eTfuJ5a visWOBLv14aptWTkb4D7UnUBP4YugkaMLBKVweYt9WrzeTPvYUdMcl7IU2pRtX8e jWbeA0JcH3/yLbHRANXTbP8ME8UfUfKDZ+OXy95cwmEiwfd6ua8Bmxj0g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeeivddgheduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdljedtmdenucfjughrpeffhffvvefukfhfgggtuggjsehgtderredt tdejnecuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesph grqhhuihgvrhdrgiihiieqnecuggftrfgrthhtvghrnhepveetjefgjeevgedukeehieeu ieeuieevueeiudegheevuefggfduueelgeelieetnecuvehluhhsthgvrhfuihiivgeptd enucfrrghrrghmpehmrghilhhfrhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiih ii X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 17 May 2023 21:50:33 -0400 (EDT) Date: Thu, 18 May 2023 10:50:28 +0900 From: Michael Paquier To: Alexander Lakhin Cc: pgsql-bugs@lists.postgresql.org, Andrey Borodin Subject: Re: BUG #17884: gist_page_items() crashes for a non-leaf page of an index with non-key columns Message-ID: References: <17884-cb8c326522977acb@postgresql.org> <92873f8d-a9b1-0ee8-5753-ee4a1664fb0e@gmail.com> <8a991814-97c0-81d6-1e24-60c26308f005@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="JMe+/IHMUsqiTmKk" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --JMe+/IHMUsqiTmKk Content-Type: multipart/mixed; boundary="cfLnmG/ymEzmVu0r" Content-Disposition: inline --cfLnmG/ymEzmVu0r Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, May 17, 2023 at 05:00:00PM +0300, Alexander Lakhin wrote: > While thinking about naming (what exactly "attroid" means there), I've > reread the comment above BuildIndexValueDescription(): > =C2=A0* The passed-in values/nulls arrays are the "raw" input to the inde= x AM, > =C2=A0* e.g. results of FormIndexDatum --- this is not necessarily what i= s stored > =C2=A0* in the index, but it's what the user perceives to be stored. Yeah, I was wondering about that a bit as well yesterday, see below. But in short I'd agree that showing all the data from a single item as stored is more helpful than just showing what would be perceived by the user, through originally what's an API to generate error messages on constraint failures. > and recognized that I've made a mistake when relied on the existing codin= g. > A simple example: > create extension pageinspect; > create extension pg_trgm; >=20 > create table test_trgm(t text); > create index trgm_idx on test_trgm using gist (t gist_trgm_ops); > insert into test_trgm select to_char(g, 'textFM000')=C2=A0 from generate_= series(1, 1000) g; > select gist_page_items(get_raw_page('trgm_idx', 0), 'trgm_idx'::regclass); > select gist_page_items(get_raw_page('trgm_idx', 1), 'trgm_idx'::regclass); >=20 > (I chose gist_trgm_ops because that opclass defined as follows: > CREATE OPERATOR CLASS gist_trgm_ops FOR TYPE text USING gist ... STORAGE = gtrgm) > shows garbage values: > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 gist_page_items > --------------------------------------------- > =C2=A0(1,"(1,65535)",32,f,"(t)=3D(\x02\x7F\x7F_=DF=9F)") > =C2=A0(2,"(3,65535)",32,f,"(t)=3D(\x02')") > =C2=A0(3,"(2,65535)",32,f,"(t)=3D(\x02=DF=BD)") > =C2=A0(4,"(5,65535)",32,f,"(t)=3D(\x02\x7F)") > =C2=A0(5,"(4,65535)",32,f,"(t)=3D(\x02w?\x7F)") > =C2=A0(6,"(6,65535)",32,f,"(t)=3D(\x02/)") > (6 rows) Fun. > It means that pageinspect tries to print stored gtrgm values as text, and= I > believe that's the third problem here. And it is not possible to display them as its output function says. People could just use the bytea flavor in this case to show such items > Moreover, for the point_ops class opcintype is point, but opckeytype is b= ox, > thus indexes test_gist_idx and test_gist_idx_inc used in the test in fact > contain boxes, not points. So the fixed gist_page_items() produces differ= ent > output even for existing test cases. (Please look at the patch attached.) > I'm somewhat confused with the output: Hmm. I was actually looking at something pretty close to that yesterday, but discarded the thought when I sent v3. Included attributes don't support expressions, but key attributes do, and the generated output was a bit confusing when not applying parenthesis for each attribute printed. For example, take this case: CREATE EXTENSION pageinspect; CREATE TABLE test_gist AS SELECT point(i,i) p, i i FROM generate_series(1, 1000) i; CREATE INDEX test_gist_idx_inc ON test_gist USING gist (p, point(i,i), (point(i, i))); SELECT keys FROM gist_page_items(get_raw_page('test_gist_idx_inc', 1), 'tes= t_gist_idx_inc') WHERE itemoffset =3D 1; On HEAD or with v3, I get that, which is parseable: keys = =20 ---------------------------------------------------------------------------= -------------------------------------------------- (p, point(i::double precision, i::double precision), point(i::double preci= sion, i::double precision))=3D((1,1), (1,1), (1,1)) (1 row) v4 would give that once you include the full contents of the page items: keys = =20 ---------------------------------------------------------------------------= -------------------------------------------------------------------- (p, point(i::double precision, i::double precision), point(i::double preci= sion, i::double precision))=3D((1,1),(1,1), (1,1),(1,1), (1,1),(1,1)) (1 row) So, as you say, we should really add more parenthesis in this case. Even with that, we cannot add too many of them, or the output could be confusing. For example, appending always parenthesis for included attributes could be seen as an expression, but they can never be that. So my take would be something a little bit more tricky, as of: - Show NULL as it is. - If dealing with a key attribute, always add parenthesis to it to clarify that we are interested in the opckeytype. - If dealing with an included attribute, don't add parenthesis at all. I have expanded the tests to show how this applies for more data types, like point or integers. Any thoughts about the v5 attached?=20 -- Michael --cfLnmG/ymEzmVu0r Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename*0=v5-01-fix-gist_page_items-for-index-with-INCLUDE-and-diff-sto; filename*1="rage.patch" Content-Transfer-Encoding: quoted-printable =46rom 0680c5c6fc2134b6ba86daa4f5e0e27843acb42b Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Thu, 18 May 2023 10:48:50 +0900 Subject: [PATCH v5] pageinspect: Fix gist_page_items with included columns Blah. Author: Alexander Lakhin Discussion: https://postgr.es/m/17884-cb8c326522977acb@postgresql.org Backpatch-through: 14 --- src/include/utils/ruleutils.h | 5 ++ src/backend/utils/adt/ruleutils.c | 16 +++++ contrib/pageinspect/expected/gist.out | 89 ++++++++++++++++++++++----- contrib/pageinspect/gistfuncs.c | 81 ++++++++++++++++++++++-- contrib/pageinspect/sql/gist.sql | 30 +++++++++ 5 files changed, 200 insertions(+), 21 deletions(-) diff --git a/src/include/utils/ruleutils.h b/src/include/utils/ruleutils.h index 1a42d9f39b..b006d9d475 100644 --- a/src/include/utils/ruleutils.h +++ b/src/include/utils/ruleutils.h @@ -20,9 +20,14 @@ struct Plan; /* avoid including plannodes.h here */ struct PlannedStmt; =20 +/* Flags for pg_get_indexdef_columns_extended() */ +#define RULE_INDEXDEF_PRETTY 0x01 +#define RULE_INDEXDEF_KEYS_ONLY 0x02 /* ignore included attributes */ =20 extern char *pg_get_indexdef_string(Oid indexrelid); extern char *pg_get_indexdef_columns(Oid indexrelid, bool pretty); +extern char *pg_get_indexdef_columns_extended(Oid indexrelid, + bits16 flags); extern char *pg_get_querydef(Query *query, bool pretty); =20 extern char *pg_get_partkeydef_columns(Oid relid, bool pretty); diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/rule= utils.c index e93d66a7ec..6d673493cb 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -1215,6 +1215,22 @@ pg_get_indexdef_columns(Oid indexrelid, bool pretty) prettyFlags, false); } =20 +/* Internal version, extensible with flags to control its behavior */ +char * +pg_get_indexdef_columns_extended(Oid indexrelid, bits16 flags) +{ + bool pretty =3D ((flags & RULE_INDEXDEF_PRETTY) !=3D 0); + bool keys_only =3D ((flags & RULE_INDEXDEF_KEYS_ONLY) !=3D 0); + int prettyFlags; + + prettyFlags =3D GET_PRETTY_FLAGS(pretty); + + return pg_get_indexdef_worker(indexrelid, 0, NULL, + true, keys_only, + false, false, + prettyFlags, false); +} + /* * Internal workhorse to decompile an index definition. * diff --git a/contrib/pageinspect/expected/gist.out b/contrib/pageinspect/ex= pected/gist.out index 460bef3037..c240e51415 100644 --- a/contrib/pageinspect/expected/gist.out +++ b/contrib/pageinspect/expected/gist.out @@ -31,24 +31,24 @@ SELECT * FROM gist_page_opaque_info(get_raw_page('test_= gist_idx', 2)); =20 COMMIT; SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 0), 'test_gist= _idx'); - itemoffset | ctid | itemlen | dead | keys =20 -------------+-----------+---------+------+------------------- - 1 | (1,65535) | 40 | f | (p)=3D((185,185)) - 2 | (2,65535) | 40 | f | (p)=3D((370,370)) - 3 | (3,65535) | 40 | f | (p)=3D((555,555)) - 4 | (4,65535) | 40 | f | (p)=3D((740,740)) - 5 | (5,65535) | 40 | f | (p)=3D((870,870)) - 6 | (6,65535) | 40 | f | (p)=3D((1000,1000)) + itemoffset | ctid | itemlen | dead | keys = =20 +------------+-----------+---------+------+------------------------------- + 1 | (1,65535) | 40 | f | (p)=3D(((185,185),(1,1))) + 2 | (2,65535) | 40 | f | (p)=3D(((370,370),(186,186))) + 3 | (3,65535) | 40 | f | (p)=3D(((555,555),(371,371))) + 4 | (4,65535) | 40 | f | (p)=3D(((740,740),(556,556))) + 5 | (5,65535) | 40 | f | (p)=3D(((870,870),(741,741))) + 6 | (6,65535) | 40 | f | (p)=3D(((1000,1000),(871,871))) (6 rows) =20 SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 1), 'test_gist= _idx') LIMIT 5; - itemoffset | ctid | itemlen | dead | keys =20 -------------+-------+---------+------+------------- - 1 | (0,1) | 40 | f | (p)=3D((1,1)) - 2 | (0,2) | 40 | f | (p)=3D((2,2)) - 3 | (0,3) | 40 | f | (p)=3D((3,3)) - 4 | (0,4) | 40 | f | (p)=3D((4,4)) - 5 | (0,5) | 40 | f | (p)=3D((5,5)) + itemoffset | ctid | itemlen | dead | keys =20 +------------+-------+---------+------+--------------------- + 1 | (0,1) | 40 | f | (p)=3D(((1,1),(1,1))) + 2 | (0,2) | 40 | f | (p)=3D(((2,2),(2,2))) + 3 | (0,3) | 40 | f | (p)=3D(((3,3),(3,3))) + 4 | (0,4) | 40 | f | (p)=3D(((4,4),(4,4))) + 5 | (0,5) | 40 | f | (p)=3D(((5,5),(5,5))) (5 rows) =20 -- gist_page_items_bytea prints the raw key data as a bytea. The output of= that is @@ -107,4 +107,63 @@ SELECT gist_page_opaque_info(decode(repeat('00', :bloc= k_size), 'hex')); =20 (1 row) =20 +-- Test gist_page_items with included columns. +-- Non-leaf pages have only the key attributes, and leaf pages have +-- the included attributes. +ALTER TABLE test_gist ADD COLUMN i1 int DEFAULT NULL; +ALTER TABLE test_gist ADD COLUMN i2 int DEFAULT 1; +CREATE INDEX test_gist_idx_inc_1 ON test_gist + USING gist (p, point(i1, i1)) INCLUDE (p, i1); +CREATE INDEX test_gist_idx_inc_12 ON test_gist + USING gist (p, point(i1, i2)) INCLUDE (t, i1, i2); +CREATE INDEX test_gist_idx_inc_2 ON test_gist + USING gist (p, point(i2, i2)) INCLUDE (p, t, i2); +SELECT keys AS keys_nonleaf_1 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_1', 0), 'test_gist_= idx_inc_1') + WHERE itemoffset =3D 1; + keys_nonleaf_1 = =20 +--------------------------------------------------------------------------= -------- + (p, point(i1::double precision, i1::double precision))=3D(((119,119),(1,1= )), null) +(1 row) + +SELECT keys AS keys_leaf_1 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_1', 1), 'test_gist_= idx_inc_1') + WHERE itemoffset =3D 1; + keys_leaf_1 = =20 +--------------------------------------------------------------------------= ------------------------------------------ + (p, point(i1::double precision, i1::double precision)) INCLUDE (p, i1)=3D= (((1,1),(1,1)), null) INCLUDE ((1,1), null) +(1 row) + +SELECT keys AS keys_nonleaf_12 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_12', 0), 'test_gist= _idx_inc_12') + WHERE itemoffset =3D 1; + keys_nonleaf_12 = =20 +--------------------------------------------------------------------------= -------- + (p, point(i1::double precision, i2::double precision))=3D(((135,135),(1,1= )), null) +(1 row) + +SELECT keys AS keys_leaf_12 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_12', 1), 'test_gist= _idx_inc_12') + WHERE itemoffset =3D 1; + keys_leaf_12 = =20 +--------------------------------------------------------------------------= --------------------------------------------- + (p, point(i1::double precision, i2::double precision)) INCLUDE (t, i1, i2= )=3D(((1,1),(1,1)), null) INCLUDE (1, null, 1) +(1 row) + +SELECT keys AS keys_nonleaf_2 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_2', 0), 'test_gist_= idx_inc_2') + WHERE itemoffset =3D 1; + keys_nonleaf_2 = =20 +--------------------------------------------------------------------------= --------------- + (p, point(i2::double precision, i2::double precision))=3D(((81,81),(1,1))= , ((1,1),(1,1))) +(1 row) + +SELECT keys AS keys_leaf_2 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_2', 1), 'test_gist_= idx_inc_2') + WHERE itemoffset =3D 1; + keys_leaf_2 = =20 +--------------------------------------------------------------------------= ------------------------------------------------------ + (p, point(i2::double precision, i2::double precision)) INCLUDE (p, t, i2)= =3D(((1,1),(1,1)), ((1,1),(1,1))) INCLUDE ((1,1), 1, 1) +(1 row) + DROP TABLE test_gist; diff --git a/contrib/pageinspect/gistfuncs.c b/contrib/pageinspect/gistfunc= s.c index 3dca7f1318..c11d2915dc 100644 --- a/contrib/pageinspect/gistfuncs.c +++ b/contrib/pageinspect/gistfuncs.c @@ -21,8 +21,10 @@ #include "storage/itemptr.h" #include "utils/array.h" #include "utils/builtins.h" -#include "utils/rel.h" #include "utils/pg_lsn.h" +#include "utils/lsyscache.h" +#include "utils/rel.h" +#include "utils/ruleutils.h" #include "utils/varlena.h" =20 PG_FUNCTION_INFO_V1(gist_page_opaque_info); @@ -198,9 +200,13 @@ gist_page_items(PG_FUNCTION_ARGS) Oid indexRelid =3D PG_GETARG_OID(1); ReturnSetInfo *rsinfo =3D (ReturnSetInfo *) fcinfo->resultinfo; Relation indexRel; + TupleDesc tupdesc; Page page; + uint16 flagbits; + bits16 printflags =3D 0; OffsetNumber offset; OffsetNumber maxoff =3D InvalidOffsetNumber; + char *index_columns; =20 if (!superuser()) ereport(ERROR, @@ -226,6 +232,27 @@ gist_page_items(PG_FUNCTION_ARGS) PG_RETURN_NULL(); } =20 + flagbits =3D GistPageGetOpaque(page)->flags; + + /* + * Included attributes are added when dealing with leaf pages, discarded + * for non-leaf pages as these include only data for key attributes. + */ + printflags |=3D RULE_INDEXDEF_PRETTY; + if (flagbits & F_LEAF) + { + tupdesc =3D RelationGetDescr(indexRel); + } + else + { + tupdesc =3D CreateTupleDescCopy(RelationGetDescr(indexRel)); + tupdesc->natts =3D IndexRelationGetNumberOfKeyAttributes(indexRel); + printflags |=3D RULE_INDEXDEF_KEYS_ONLY; + } + + index_columns =3D pg_get_indexdef_columns_extended(indexRelid, + printflags); + /* Avoid bogus PageGetMaxOffsetNumber() call with deleted pages */ if (GistPageIsDeleted(page)) elog(NOTICE, "page is deleted"); @@ -242,7 +269,8 @@ gist_page_items(PG_FUNCTION_ARGS) IndexTuple itup; Datum itup_values[INDEX_MAX_KEYS]; bool itup_isnull[INDEX_MAX_KEYS]; - char *key_desc; + StringInfoData buf; + int i; =20 id =3D PageGetItemId(page, offset); =20 @@ -251,7 +279,7 @@ gist_page_items(PG_FUNCTION_ARGS) =20 itup =3D (IndexTuple) PageGetItem(page, id); =20 - index_deform_tuple(itup, RelationGetDescr(indexRel), + index_deform_tuple(itup, tupdesc, itup_values, itup_isnull); =20 memset(nulls, 0, sizeof(nulls)); @@ -261,9 +289,50 @@ gist_page_items(PG_FUNCTION_ARGS) values[2] =3D Int32GetDatum((int) IndexTupleSize(itup)); values[3] =3D BoolGetDatum(ItemIdIsDead(id)); =20 - key_desc =3D BuildIndexValueDescription(indexRel, itup_values, itup_isnu= ll); - if (key_desc) - values[4] =3D CStringGetTextDatum(key_desc); + if (index_columns) + { + initStringInfo(&buf); + appendStringInfo(&buf, "(%s)=3D(", index_columns); + + for (i =3D 0; i < tupdesc->natts; i++) + { + char *val; + + if (itup_isnull[i]) + val =3D "null"; + else + { + Oid foutoid; + bool typisvarlena; + Oid typoid; + + typoid =3D tupdesc->attrs[i].atttypid; + getTypeOutputInfo(typoid, &foutoid, &typisvarlena); + val =3D OidOutputFunctionCall(foutoid, itup_values[i]); + } + + if (i =3D=3D IndexRelationGetNumberOfKeyAttributes(indexRel)) + appendStringInfoString(&buf, ") INCLUDE ("); + else if (i > 0) + appendStringInfoString(&buf, ", "); + + /* + * NULL values are printed as-is, and included attributes + * don't require more parenthesis for clarity in the output. + */ + if (itup_isnull[i]) + appendStringInfoString(&buf, val); + else if (i >=3D IndexRelationGetNumberOfKeyAttributes(indexRel)) + appendStringInfo(&buf, "%s", val); + else + appendStringInfo(&buf, "(%s)", val); + } + + appendStringInfoChar(&buf, ')'); + + values[4] =3D CStringGetTextDatum(buf.data); + nulls[4] =3D false; + } else { values[4] =3D (Datum) 0; diff --git a/contrib/pageinspect/sql/gist.sql b/contrib/pageinspect/sql/gis= t.sql index 4787b784a4..5d9384907e 100644 --- a/contrib/pageinspect/sql/gist.sql +++ b/contrib/pageinspect/sql/gist.sql @@ -52,4 +52,34 @@ SELECT gist_page_items_bytea(decode(repeat('00', :block_= size), 'hex')); SELECT gist_page_items(decode(repeat('00', :block_size), 'hex'), 'test_gis= t_idx'::regclass); SELECT gist_page_opaque_info(decode(repeat('00', :block_size), 'hex')); =20 +-- Test gist_page_items with included columns. +-- Non-leaf pages have only the key attributes, and leaf pages have +-- the included attributes. +ALTER TABLE test_gist ADD COLUMN i1 int DEFAULT NULL; +ALTER TABLE test_gist ADD COLUMN i2 int DEFAULT 1; +CREATE INDEX test_gist_idx_inc_1 ON test_gist + USING gist (p, point(i1, i1)) INCLUDE (p, i1); +CREATE INDEX test_gist_idx_inc_12 ON test_gist + USING gist (p, point(i1, i2)) INCLUDE (t, i1, i2); +CREATE INDEX test_gist_idx_inc_2 ON test_gist + USING gist (p, point(i2, i2)) INCLUDE (p, t, i2); +SELECT keys AS keys_nonleaf_1 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_1', 0), 'test_gist_= idx_inc_1') + WHERE itemoffset =3D 1; +SELECT keys AS keys_leaf_1 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_1', 1), 'test_gist_= idx_inc_1') + WHERE itemoffset =3D 1; +SELECT keys AS keys_nonleaf_12 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_12', 0), 'test_gist= _idx_inc_12') + WHERE itemoffset =3D 1; +SELECT keys AS keys_leaf_12 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_12', 1), 'test_gist= _idx_inc_12') + WHERE itemoffset =3D 1; +SELECT keys AS keys_nonleaf_2 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_2', 0), 'test_gist_= idx_inc_2') + WHERE itemoffset =3D 1; +SELECT keys AS keys_leaf_2 + FROM gist_page_items(get_raw_page('test_gist_idx_inc_2', 1), 'test_gist_= idx_inc_2') + WHERE itemoffset =3D 1; + DROP TABLE test_gist; --=20 2.40.1 --cfLnmG/ymEzmVu0r-- --JMe+/IHMUsqiTmKk Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmRlhGQACgkQnvQgOdby QH1OxQ/7BDR0AYXv2L2oIRENEQgLxH1sMsZdk2Y40ekIHjGvoyOJdHgHsu9si6AH Lb//E7G9Qr/UrhBP1vnPGWXgOCkeq83LtUOEjvAlHjYeqIsUFGQDS25KslGTrNMx FFlyIZxAeaMTt2FgibNrx7llqe34WQ+/m6ihY5dWZXnTf3QMr2MyCk13gXKSt605 6xqNsuzxINxot+YXWzBUikY8Bvq9qAqSs65WjYRynOxZyUgv+uZEacuIemnxyEjR sBAFzfeghuBO542ws23UdhSdt/NbCOYi8j+cs2xs/whfon9PyaOexEwxZ0HPKTWb Xs8bxI7hORAszbpERJK4Y1DpLjM2Y1tX0zq4fW0klQr7mhSOqKWs/6sgqA1Ed0tk URwoJs1ASdAQSJ17sdnxhuodkMG/ex0MtggzaqGRKqGD5U4lMEPS5dYhVa76nJBc XZLWE4mCqpZHpXWNGZQziUBXB8nkna8LAHBftNH7znrRrKJzrBeAdWGvKbkburEt Yh5hMYbboHndYAy66zsZCy6aZaqm2ZSAqWgPQGPSLoihR75jKXTNQVdDqJYwHdDj CLlx/g+CHK0WZjk5xnbIERKLqTb8pWexdYwlT1lnRwewbeYfolVdLrkYyx7Kbw9g VqBU405cdqvjcovpSfNbW+YdOW9hVcLxjHYOChSWrjv4RgDo8Xs= =4rAY -----END PGP SIGNATURE----- --JMe+/IHMUsqiTmKk--