pageinspect: Fix gist_page_items() with included columns
authorMichael Paquier
Fri, 19 May 2023 03:38:18 +0000 (12:38 +0900)
committerMichael Paquier
Fri, 19 May 2023 03:38:18 +0000 (12:38 +0900)
Non-leaf pages of GiST indexes contain key attributes, leaf pages
contain both key and non-key attributes, and gist_page_items() ignored
the handling of non-key attributes.  This caused a few problems when
using gist_page_items() on a GiST index with INCLUDE:
- On a non-leaf page, the function would crash.
- On a leaf page, the function would work, but miss to display all the
values for included attributes.

This commit fixes gist_page_items() to handle such cases in a more
appropriate way, and now displays the values of key and non-key
attributes for each item separately in a style consistent with what
ruleutils.c would generate for the attribute list, depending on the page
type dealt with.  In a way similar to how a record is displayed, values
would be double-quoted for key or non-key attributes if required.

ruleutils.c did not provide a routine able to control if non-key
attributes should be displayed, so an extended() routine for index
definitions is added to work around the leaf and non-leaf page
differences.

While on it, this commit fixes a third problem related to the amount of
data reported for key attributes.  The code originally relied on
BuildIndexValueDescription() (used for error reports on constraints)
that would not print all the data stored in the index but the index
opclass's input type, so this limited the amount of information
available.  This switch makes gist_page_items() much cheaper as there is
no need to run ACL checks for each item printed, which is not an issue
anyway as superuser rights are required to execute the functions of
pageinspect.  Opclasses whose data cannot be displayed can rely on
gist_page_items_bytea().

The documentation of this function was slightly incorrect for the
output results generated on HEAD and v15, so adjust it on these
branches.

Author: Alexander Lakhin, Michael Paquier
Discussion: https://postgr.es/m/17884-cb8c326522977acb@postgresql.org
Backpatch-through: 14

contrib/pageinspect/expected/gist.out
contrib/pageinspect/gistfuncs.c
contrib/pageinspect/sql/gist.sql
doc/src/sgml/pageinspect.sgml
src/backend/utils/adt/ruleutils.c
src/include/utils/ruleutils.h

index 3f2e720d0244ca2aaaf32516ff6dd06a9f6e47ee..e7b27b4fddded6fc0ecd359bb37b137bd4866b0b 100644 (file)
@@ -31,25 +31,25 @@ SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 2));
 
 COMMIT;
 SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 0), 'test_gist_idx');
- itemoffset |   ctid    | itemlen | dead |       keys        
-------------+-----------+---------+------+-------------------
-          1 | (1,65535) |      40 | f    | (p)=((166,166))
-          2 | (2,65535) |      40 | f    | (p)=((332,332))
-          3 | (3,65535) |      40 | f    | (p)=((498,498))
-          4 | (4,65535) |      40 | f    | (p)=((664,664))
-          5 | (5,65535) |      40 | f    | (p)=((830,830))
-          6 | (6,65535) |      40 | f    | (p)=((996,996))
-          7 | (7,65535) |      40 | f    | (p)=((1000,1000))
+ itemoffset |   ctid    | itemlen | dead |             keys              
+------------+-----------+---------+------+-------------------------------
+          1 | (1,65535) |      40 | f    | (p)=("(166,166),(1,1)")
+          2 | (2,65535) |      40 | f    | (p)=("(332,332),(167,167)")
+          3 | (3,65535) |      40 | f    | (p)=("(498,498),(333,333)")
+          4 | (4,65535) |      40 | f    | (p)=("(664,664),(499,499)")
+          5 | (5,65535) |      40 | f    | (p)=("(830,830),(665,665)")
+          6 | (6,65535) |      40 | f    | (p)=("(996,996),(831,831)")
+          7 | (7,65535) |      40 | f    | (p)=("(1000,1000),(997,997)")
 (7 rows)
 
 SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 1), 'test_gist_idx') LIMIT 5;
- itemoffset | ctid  | itemlen | dead |    keys     
-------------+-------+---------+------+-------------
-          1 | (0,1) |      40 | f    | (p)=((1,1))
-          2 | (0,2) |      40 | f    | (p)=((2,2))
-          3 | (0,3) |      40 | f    | (p)=((3,3))
-          4 | (0,4) |      40 | f    | (p)=((4,4))
-          5 | (0,5) |      40 | f    | (p)=((5,5))
+ itemoffset | ctid  | itemlen | dead |        keys         
+------------+-------+---------+------+---------------------
+          1 | (0,1) |      40 | f    | (p)=("(1,1),(1,1)")
+          2 | (0,2) |      40 | f    | (p)=("(2,2),(2,2)")
+          3 | (0,3) |      40 | f    | (p)=("(3,3),(3,3)")
+          4 | (0,4) |      40 | f    | (p)=("(4,4),(4,4)")
+          5 | (0,5) |      40 | f    | (p)=("(5,5),(5,5)")
 (5 rows)
 
 -- gist_page_items_bytea prints the raw key data as a bytea. The output of that is
@@ -109,4 +109,27 @@ SELECT gist_page_opaque_info(decode(repeat('00', :block_size), 'hex'));
  
 (1 row)
 
+-- Test gist_page_items with included columns.
+-- Non-leaf pages contain only the key attributes, and leaf pages contain
+-- the included attributes.
+ALTER TABLE test_gist ADD COLUMN i int DEFAULT NULL;
+CREATE INDEX test_gist_idx_inc ON test_gist
+  USING gist (p) INCLUDE (t, i);
+-- Mask the value of the key attribute to avoid alignment issues.
+SELECT regexp_replace(keys, '\(p\)=\("(.*?)"\)', '(p)=("")') AS keys_nonleaf_1
+  FROM gist_page_items(get_raw_page('test_gist_idx_inc', 0), 'test_gist_idx_inc')
+  WHERE itemoffset = 1;
+ keys_nonleaf_1 
+----------------
+ (p)=("")
+(1 row)
+
+SELECT keys AS keys_leaf_1
+  FROM gist_page_items(get_raw_page('test_gist_idx_inc', 1), 'test_gist_idx_inc')
+  WHERE itemoffset = 1;
+                     keys_leaf_1                      
+------------------------------------------------------
+ (p) INCLUDE (t, i)=("(1,1),(1,1)") INCLUDE (1, null)
+(1 row)
+
 DROP TABLE test_gist;
index 0ae8f7459c165aebd0923bcba456865c84f9e227..a9ded2037a07ea04a1427ffff246cc52fffe375c 100644 (file)
 #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"
 
 PG_FUNCTION_INFO_V1(gist_page_opaque_info);
@@ -233,8 +235,11 @@ gist_page_items(PG_FUNCTION_ARGS)
    Tuplestorestate *tupstore;
    MemoryContext oldcontext;
    Page        page;
+   uint16      flagbits;
+   bits16      printflags = 0;
    OffsetNumber offset;
    OffsetNumber maxoff = InvalidOffsetNumber;
+   char       *index_columns;
 
    if (!superuser())
        ereport(ERROR,
@@ -282,6 +287,27 @@ gist_page_items(PG_FUNCTION_ARGS)
        PG_RETURN_NULL();
    }
 
+   flagbits = 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 |= RULE_INDEXDEF_PRETTY;
+   if (flagbits & F_LEAF)
+   {
+       tupdesc = RelationGetDescr(indexRel);
+   }
+   else
+   {
+       tupdesc = CreateTupleDescCopy(RelationGetDescr(indexRel));
+       tupdesc->natts = IndexRelationGetNumberOfKeyAttributes(indexRel);
+       printflags |= RULE_INDEXDEF_KEYS_ONLY;
+   }
+
+   index_columns = pg_get_indexdef_columns_extended(indexRelid,
+                                                    printflags);
+
    /* Avoid bogus PageGetMaxOffsetNumber() call with deleted pages */
    if (GistPageIsDeleted(page))
        elog(NOTICE, "page is deleted");
@@ -298,7 +324,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;
 
        id = PageGetItemId(page, offset);
 
@@ -307,7 +334,7 @@ gist_page_items(PG_FUNCTION_ARGS)
 
        itup = (IndexTuple) PageGetItem(page, id);
 
-       index_deform_tuple(itup, RelationGetDescr(indexRel),
+       index_deform_tuple(itup, tupdesc,
                           itup_values, itup_isnull);
 
        memset(nulls, 0, sizeof(nulls));
@@ -317,16 +344,79 @@ gist_page_items(PG_FUNCTION_ARGS)
        values[2] = Int32GetDatum((int) IndexTupleSize(itup));
        values[3] = BoolGetDatum(ItemIdIsDead(id));
 
-       key_desc = BuildIndexValueDescription(indexRel, itup_values, itup_isnull);
-       if (key_desc)
-           values[4] = CStringGetTextDatum(key_desc);
+       if (index_columns)
+       {
+           initStringInfo(&buf);
+           appendStringInfo(&buf, "(%s)=(", index_columns);
+
+           /* Most of this is copied from record_out(). */
+           for (i = 0; i < tupdesc->natts; i++)
+           {
+               char       *value;
+               char       *tmp;
+               bool        nq = false;
+
+               if (itup_isnull[i])
+                   value = "null";
+               else
+               {
+                   Oid         foutoid;
+                   bool        typisvarlena;
+                   Oid         typoid;
+
+                   typoid = tupdesc->attrs[i].atttypid;
+                   getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
+                   value = OidOutputFunctionCall(foutoid, itup_values[i]);
+               }
+
+               if (i == IndexRelationGetNumberOfKeyAttributes(indexRel))
+                   appendStringInfoString(&buf, ") INCLUDE (");
+               else if (i > 0)
+                   appendStringInfoString(&buf, ", ");
+
+               /* Check whether we need double quotes for this value */
+               nq = (value[0] == '\0');    /* force quotes for empty string */
+               for (tmp = value; *tmp; tmp++)
+               {
+                   char        ch = *tmp;
+
+                   if (ch == '"' || ch == '\\' ||
+                       ch == '(' || ch == ')' || ch == ',' ||
+                       isspace((unsigned char) ch))
+                   {
+                       nq = true;
+                       break;
+                   }
+               }
+
+               /* And emit the string */
+               if (nq)
+                   appendStringInfoCharMacro(&buf, '"');
+               for (tmp = value; *tmp; tmp++)
+               {
+                   char        ch = *tmp;
+
+                   if (ch == '"' || ch == '\\')
+                       appendStringInfoCharMacro(&buf, ch);
+                   appendStringInfoCharMacro(&buf, ch);
+               }
+               if (nq)
+                   appendStringInfoCharMacro(&buf, '"');
+           }
+
+           appendStringInfoChar(&buf, ')');
+
+           values[4] = CStringGetTextDatum(buf.data);
+           nulls[4] = false;
+       }
        else
        {
            values[4] = (Datum) 0;
            nulls[4] = true;
        }
 
-       tuplestore_putvalues(tupstore, tupdesc, values, nulls);
+       tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
+                            values, nulls);
    }
 
    relation_close(indexRel, AccessShareLock);
index 963d5d40a3c7f4736efe0ab31babe5b40c5a9d96..7efd91967928b93eb7c2cd66969cda12403b9908 100644 (file)
@@ -52,4 +52,18 @@ SELECT gist_page_items_bytea(decode(repeat('00', :block_size), 'hex'));
 SELECT gist_page_items(decode(repeat('00', :block_size), 'hex'), 'test_gist_idx'::regclass);
 SELECT gist_page_opaque_info(decode(repeat('00', :block_size), 'hex'));
 
+-- Test gist_page_items with included columns.
+-- Non-leaf pages contain only the key attributes, and leaf pages contain
+-- the included attributes.
+ALTER TABLE test_gist ADD COLUMN i int DEFAULT NULL;
+CREATE INDEX test_gist_idx_inc ON test_gist
+  USING gist (p) INCLUDE (t, i);
+-- Mask the value of the key attribute to avoid alignment issues.
+SELECT regexp_replace(keys, '\(p\)=\("(.*?)"\)', '(p)=("")') AS keys_nonleaf_1
+  FROM gist_page_items(get_raw_page('test_gist_idx_inc', 0), 'test_gist_idx_inc')
+  WHERE itemoffset = 1;
+SELECT keys AS keys_leaf_1
+  FROM gist_page_items(get_raw_page('test_gist_idx_inc', 1), 'test_gist_idx_inc')
+  WHERE itemoffset = 1;
+
 DROP TABLE test_gist;
index 24b5e463ed9a39b9baa986c27c58b43b51e83a16..58dc93d7f139ae45dd36120a0c706ff11aabcb85 100644 (file)
@@ -714,15 +714,15 @@ test=# SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 2));
       the data stored in a page of a GiST index.  For example:
 
 test=# SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 0), 'test_gist_idx');
- itemoffset |   ctid    | itemlen | dead |       keys        
-------------+-----------+---------+------+-------------------
-          1 | (1,65535) |      40 | f    | (p)=((166,166))
-          2 | (2,65535) |      40 | f    | (p)=((332,332))
-          3 | (3,65535) |      40 | f    | (p)=((498,498))
-          4 | (4,65535) |      40 | f    | (p)=((664,664))
-          5 | (5,65535) |      40 | f    | (p)=((830,830))
-          6 | (6,65535) |      40 | f    | (p)=((996,996))
-          7 | (7,65535) |      40 | f    | (p)=((1000,1000))
+ itemoffset |   ctid    | itemlen | dead |             keys
+------------+-----------+---------+------+-------------------------------
+          1 | (1,65535) |      40 | f    | (p)=("(166,166),(1,1)")
+          2 | (2,65535) |      40 | f    | (p)=("(332,332),(167,167)")
+          3 | (3,65535) |      40 | f    | (p)=("(498,498),(333,333)")
+          4 | (4,65535) |      40 | f    | (p)=("(664,664),(499,499)")
+          5 | (5,65535) |      40 | f    | (p)=("(830,830),(665,665)")
+          6 | (6,65535) |      40 | f    | (p)=("(996,996),(831,831)")
+          7 | (7,65535) |      40 | f    | (p)=("(1000,1000),(997,997)")
 (7 rows)
 
      
index 29b2e4f5d84a63f5148b25f19129baf726186e63..d13476d7ba3ee348e6ab69fcff3dc2c991a278c3 100644 (file)
@@ -1193,6 +1193,22 @@ pg_get_indexdef_columns(Oid indexrelid, bool pretty)
                                  prettyFlags, false);
 }
 
+/* Internal version, extensible with flags to control its behavior */
+char *
+pg_get_indexdef_columns_extended(Oid indexrelid, bits16 flags)
+{
+   bool        pretty = ((flags & RULE_INDEXDEF_PRETTY) != 0);
+   bool        keys_only = ((flags & RULE_INDEXDEF_KEYS_ONLY) != 0);
+   int         prettyFlags;
+
+   prettyFlags = pretty ? (PRETTYFLAG_PAREN | PRETTYFLAG_INDENT | PRETTYFLAG_SCHEMA) : PRETTYFLAG_INDENT;
+
+   return pg_get_indexdef_worker(indexrelid, 0, NULL,
+                                 true, keys_only,
+                                 false, false,
+                                 prettyFlags, false);
+}
+
 /*
  * Internal workhorse to decompile an index definition.
  *
index d333e5e8a56d03f505d1bc7d7a1e60cefdc74046..030c996808c34666cdbaf4e39cb09a6739850d7e 100644 (file)
 struct Plan;                   /* avoid including plannodes.h here */
 struct PlannedStmt;
 
+/* Flags for pg_get_indexdef_columns_extended() */
+#define RULE_INDEXDEF_PRETTY       0x01
+#define RULE_INDEXDEF_KEYS_ONLY        0x02    /* ignore included attributes */
 
 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_partkeydef_columns(Oid relid, bool pretty);
 extern char *pg_get_partconstrdef_string(Oid partitionId, char *aliasname);