LCOV - code coverage report
Current view: top level - contrib/pageinspect - gistfuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 139 155 89.7 %
Date: 2026-02-02 14:17:46 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * gistfuncs.c
       3             :  *      Functions to investigate the content of GiST indexes
       4             :  *
       5             :  * Copyright (c) 2014-2026, PostgreSQL Global Development Group
       6             :  *
       7             :  * IDENTIFICATION
       8             :  *      contrib/pageinspect/gistfuncs.c
       9             :  */
      10             : #include "postgres.h"
      11             : 
      12             : #include "access/genam.h"
      13             : #include "access/gist.h"
      14             : #include "access/htup.h"
      15             : #include "access/htup_details.h"
      16             : #include "access/relation.h"
      17             : #include "catalog/pg_am_d.h"
      18             : #include "funcapi.h"
      19             : #include "miscadmin.h"
      20             : #include "pageinspect.h"
      21             : #include "storage/itemptr.h"
      22             : #include "utils/array.h"
      23             : #include "utils/builtins.h"
      24             : #include "utils/lsyscache.h"
      25             : #include "utils/pg_lsn.h"
      26             : #include "utils/rel.h"
      27             : #include "utils/ruleutils.h"
      28             : 
      29          16 : PG_FUNCTION_INFO_V1(gist_page_opaque_info);
      30          16 : PG_FUNCTION_INFO_V1(gist_page_items);
      31          16 : PG_FUNCTION_INFO_V1(gist_page_items_bytea);
      32             : 
      33             : #define IS_GIST(r) ((r)->rd_rel->relam == GIST_AM_OID)
      34             : 
      35             : 
      36             : static Page verify_gist_page(bytea *raw_page);
      37             : 
      38             : /*
      39             :  * Verify that the given bytea contains a GIST page or die in the attempt.
      40             :  * A pointer to the page is returned.
      41             :  */
      42             : static Page
      43          36 : verify_gist_page(bytea *raw_page)
      44             : {
      45          36 :     Page        page = get_page_from_raw(raw_page);
      46             :     GISTPageOpaque opaq;
      47             : 
      48          30 :     if (PageIsNew(page))
      49           6 :         return page;
      50             : 
      51             :     /* verify the special space has the expected size */
      52          24 :     if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GISTPageOpaqueData)))
      53           4 :         ereport(ERROR,
      54             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
      55             :                  errmsg("input page is not a valid %s page", "GiST"),
      56             :                  errdetail("Expected special size %d, got %d.",
      57             :                            (int) MAXALIGN(sizeof(GISTPageOpaqueData)),
      58             :                            (int) PageGetSpecialSize(page))));
      59             : 
      60          20 :     opaq = GistPageGetOpaque(page);
      61          20 :     if (opaq->gist_page_id != GIST_PAGE_ID)
      62           4 :         ereport(ERROR,
      63             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
      64             :                  errmsg("input page is not a valid %s page", "GiST"),
      65             :                  errdetail("Expected %08x, got %08x.",
      66             :                            GIST_PAGE_ID,
      67             :                            opaq->gist_page_id)));
      68             : 
      69          16 :     return page;
      70             : }
      71             : 
      72             : Datum
      73          12 : gist_page_opaque_info(PG_FUNCTION_ARGS)
      74             : {
      75          12 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
      76             :     TupleDesc   tupdesc;
      77             :     Page        page;
      78             :     HeapTuple   resultTuple;
      79             :     Datum       values[4];
      80             :     bool        nulls[4];
      81             :     Datum       flags[16];
      82          12 :     int         nflags = 0;
      83             :     uint16      flagbits;
      84             : 
      85          12 :     if (!superuser())
      86           0 :         ereport(ERROR,
      87             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
      88             :                  errmsg("must be superuser to use raw page functions")));
      89             : 
      90          12 :     page = verify_gist_page(raw_page);
      91             : 
      92           8 :     if (PageIsNew(page))
      93           2 :         PG_RETURN_NULL();
      94             : 
      95             :     /* Build a tuple descriptor for our result type */
      96           6 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
      97           0 :         elog(ERROR, "return type must be a row type");
      98             : 
      99             :     /* Convert the flags bitmask to an array of human-readable names */
     100           6 :     flagbits = GistPageGetOpaque(page)->flags;
     101           6 :     if (flagbits & F_LEAF)
     102           4 :         flags[nflags++] = CStringGetTextDatum("leaf");
     103           6 :     if (flagbits & F_DELETED)
     104           0 :         flags[nflags++] = CStringGetTextDatum("deleted");
     105           6 :     if (flagbits & F_TUPLES_DELETED)
     106           0 :         flags[nflags++] = CStringGetTextDatum("tuples_deleted");
     107           6 :     if (flagbits & F_FOLLOW_RIGHT)
     108           0 :         flags[nflags++] = CStringGetTextDatum("follow_right");
     109           6 :     if (flagbits & F_HAS_GARBAGE)
     110           0 :         flags[nflags++] = CStringGetTextDatum("has_garbage");
     111           6 :     flagbits &= ~(F_LEAF | F_DELETED | F_TUPLES_DELETED | F_FOLLOW_RIGHT | F_HAS_GARBAGE);
     112           6 :     if (flagbits)
     113             :     {
     114             :         /* any flags we don't recognize are printed in hex */
     115           0 :         flags[nflags++] = DirectFunctionCall1(to_hex32, Int32GetDatum(flagbits));
     116             :     }
     117             : 
     118           6 :     memset(nulls, 0, sizeof(nulls));
     119             : 
     120           6 :     values[0] = LSNGetDatum(PageGetLSN(page));
     121           6 :     values[1] = LSNGetDatum(GistPageGetNSN(page));
     122           6 :     values[2] = Int64GetDatum(GistPageGetOpaque(page)->rightlink);
     123           6 :     values[3] = PointerGetDatum(construct_array_builtin(flags, nflags, TEXTOID));
     124             : 
     125             :     /* Build and return the result tuple. */
     126           6 :     resultTuple = heap_form_tuple(tupdesc, values, nulls);
     127             : 
     128           6 :     return HeapTupleGetDatum(resultTuple);
     129             : }
     130             : 
     131             : Datum
     132          10 : gist_page_items_bytea(PG_FUNCTION_ARGS)
     133             : {
     134          10 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
     135          10 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
     136             :     Page        page;
     137             :     OffsetNumber offset;
     138          10 :     OffsetNumber maxoff = InvalidOffsetNumber;
     139             : 
     140          10 :     if (!superuser())
     141           0 :         ereport(ERROR,
     142             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     143             :                  errmsg("must be superuser to use raw page functions")));
     144             : 
     145          10 :     InitMaterializedSRF(fcinfo, 0);
     146             : 
     147          10 :     page = verify_gist_page(raw_page);
     148             : 
     149           4 :     if (PageIsNew(page))
     150           2 :         PG_RETURN_NULL();
     151             : 
     152             :     /* Avoid bogus PageGetMaxOffsetNumber() call with deleted pages */
     153           2 :     if (GistPageIsDeleted(page))
     154           0 :         elog(NOTICE, "page is deleted");
     155             :     else
     156           2 :         maxoff = PageGetMaxOffsetNumber(page);
     157             : 
     158           2 :     for (offset = FirstOffsetNumber;
     159          14 :          offset <= maxoff;
     160          12 :          offset++)
     161             :     {
     162             :         Datum       values[5];
     163             :         bool        nulls[5];
     164             :         ItemId      id;
     165             :         IndexTuple  itup;
     166             :         bytea      *tuple_bytea;
     167             :         int         tuple_len;
     168             : 
     169          12 :         id = PageGetItemId(page, offset);
     170             : 
     171          12 :         if (!ItemIdIsValid(id))
     172           0 :             elog(ERROR, "invalid ItemId");
     173             : 
     174          12 :         itup = (IndexTuple) PageGetItem(page, id);
     175          12 :         tuple_len = IndexTupleSize(itup);
     176             : 
     177          12 :         memset(nulls, 0, sizeof(nulls));
     178             : 
     179          12 :         values[0] = UInt16GetDatum(offset);
     180          12 :         values[1] = ItemPointerGetDatum(&itup->t_tid);
     181          12 :         values[2] = Int32GetDatum((int) IndexTupleSize(itup));
     182             : 
     183          12 :         tuple_bytea = (bytea *) palloc(tuple_len + VARHDRSZ);
     184          12 :         SET_VARSIZE(tuple_bytea, tuple_len + VARHDRSZ);
     185          12 :         memcpy(VARDATA(tuple_bytea), itup, tuple_len);
     186          12 :         values[3] = BoolGetDatum(ItemIdIsDead(id));
     187          12 :         values[4] = PointerGetDatum(tuple_bytea);
     188             : 
     189          12 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
     190             :     }
     191             : 
     192           2 :     return (Datum) 0;
     193             : }
     194             : 
     195             : Datum
     196          16 : gist_page_items(PG_FUNCTION_ARGS)
     197             : {
     198          16 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
     199          16 :     Oid         indexRelid = PG_GETARG_OID(1);
     200          16 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
     201             :     Relation    indexRel;
     202             :     TupleDesc   tupdesc;
     203             :     Page        page;
     204             :     uint16      flagbits;
     205          16 :     bits16      printflags = 0;
     206             :     OffsetNumber offset;
     207          16 :     OffsetNumber maxoff = InvalidOffsetNumber;
     208             :     char       *index_columns;
     209             : 
     210          16 :     if (!superuser())
     211           0 :         ereport(ERROR,
     212             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     213             :                  errmsg("must be superuser to use raw page functions")));
     214             : 
     215          16 :     InitMaterializedSRF(fcinfo, 0);
     216             : 
     217             :     /* Open the relation */
     218          16 :     indexRel = index_open(indexRelid, AccessShareLock);
     219             : 
     220          16 :     if (!IS_GIST(indexRel))
     221           2 :         ereport(ERROR,
     222             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     223             :                  errmsg("\"%s\" is not a %s index",
     224             :                         RelationGetRelationName(indexRel), "GiST")));
     225             : 
     226          14 :     page = verify_gist_page(raw_page);
     227             : 
     228          10 :     if (PageIsNew(page))
     229             :     {
     230           2 :         index_close(indexRel, AccessShareLock);
     231           2 :         PG_RETURN_NULL();
     232             :     }
     233             : 
     234           8 :     flagbits = GistPageGetOpaque(page)->flags;
     235             : 
     236             :     /*
     237             :      * Included attributes are added when dealing with leaf pages, discarded
     238             :      * for non-leaf pages as these include only data for key attributes.
     239             :      */
     240           8 :     printflags |= RULE_INDEXDEF_PRETTY;
     241           8 :     if (flagbits & F_LEAF)
     242             :     {
     243           4 :         tupdesc = RelationGetDescr(indexRel);
     244             :     }
     245             :     else
     246             :     {
     247           4 :         tupdesc = CreateTupleDescTruncatedCopy(RelationGetDescr(indexRel),
     248           4 :                                                IndexRelationGetNumberOfKeyAttributes(indexRel));
     249           4 :         printflags |= RULE_INDEXDEF_KEYS_ONLY;
     250             :     }
     251             : 
     252           8 :     index_columns = pg_get_indexdef_columns_extended(indexRelid,
     253             :                                                      printflags);
     254             : 
     255             :     /* Avoid bogus PageGetMaxOffsetNumber() call with deleted pages */
     256           8 :     if (GistPageIsDeleted(page))
     257           0 :         elog(NOTICE, "page is deleted");
     258             :     else
     259           8 :         maxoff = PageGetMaxOffsetNumber(page);
     260             : 
     261           8 :     for (offset = FirstOffsetNumber;
     262         676 :          offset <= maxoff;
     263         668 :          offset++)
     264             :     {
     265             :         Datum       values[5];
     266             :         bool        nulls[5];
     267             :         ItemId      id;
     268             :         IndexTuple  itup;
     269             :         Datum       itup_values[INDEX_MAX_KEYS];
     270             :         bool        itup_isnull[INDEX_MAX_KEYS];
     271             :         StringInfoData buf;
     272             :         int         i;
     273             : 
     274         668 :         id = PageGetItemId(page, offset);
     275             : 
     276         668 :         if (!ItemIdIsValid(id))
     277           0 :             elog(ERROR, "invalid ItemId");
     278             : 
     279         668 :         itup = (IndexTuple) PageGetItem(page, id);
     280             : 
     281         668 :         index_deform_tuple(itup, tupdesc,
     282             :                            itup_values, itup_isnull);
     283             : 
     284         668 :         memset(nulls, 0, sizeof(nulls));
     285             : 
     286         668 :         values[0] = UInt16GetDatum(offset);
     287         668 :         values[1] = ItemPointerGetDatum(&itup->t_tid);
     288         668 :         values[2] = Int32GetDatum((int) IndexTupleSize(itup));
     289         668 :         values[3] = BoolGetDatum(ItemIdIsDead(id));
     290             : 
     291         668 :         if (index_columns)
     292             :         {
     293         668 :             initStringInfo(&buf);
     294         668 :             appendStringInfo(&buf, "(%s)=(", index_columns);
     295             : 
     296             :             /* Most of this is copied from record_out(). */
     297        1876 :             for (i = 0; i < tupdesc->natts; i++)
     298             :             {
     299             :                 char       *value;
     300             :                 char       *tmp;
     301        1208 :                 bool        nq = false;
     302             : 
     303        1208 :                 if (itup_isnull[i])
     304         270 :                     value = "null";
     305             :                 else
     306             :                 {
     307             :                     Oid         foutoid;
     308             :                     bool        typisvarlena;
     309             :                     Oid         typoid;
     310             : 
     311         938 :                     typoid = TupleDescAttr(tupdesc, i)->atttypid;
     312         938 :                     getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
     313         938 :                     value = OidOutputFunctionCall(foutoid, itup_values[i]);
     314             :                 }
     315             : 
     316        1208 :                 if (i == IndexRelationGetNumberOfKeyAttributes(indexRel))
     317         270 :                     appendStringInfoString(&buf, ") INCLUDE (");
     318         938 :                 else if (i > 0)
     319         270 :                     appendStringInfoString(&buf, ", ");
     320             : 
     321             :                 /* Check whether we need double quotes for this value */
     322        1208 :                 nq = (value[0] == '\0');    /* force quotes for empty string */
     323        2882 :                 for (tmp = value; *tmp; tmp++)
     324             :                 {
     325        2342 :                     char        ch = *tmp;
     326             : 
     327        2342 :                     if (ch == '"' || ch == '\\' ||
     328        1674 :                         ch == '(' || ch == ')' || ch == ',' ||
     329        1674 :                         isspace((unsigned char) ch))
     330             :                     {
     331         668 :                         nq = true;
     332         668 :                         break;
     333             :                     }
     334             :                 }
     335             : 
     336             :                 /* And emit the string */
     337        1208 :                 if (nq)
     338         668 :                     appendStringInfoCharMacro(&buf, '"');
     339       13838 :                 for (tmp = value; *tmp; tmp++)
     340             :                 {
     341       12630 :                     char        ch = *tmp;
     342             : 
     343       12630 :                     if (ch == '"' || ch == '\\')
     344           0 :                         appendStringInfoCharMacro(&buf, ch);
     345       12630 :                     appendStringInfoCharMacro(&buf, ch);
     346             :                 }
     347        1208 :                 if (nq)
     348         668 :                     appendStringInfoCharMacro(&buf, '"');
     349             :             }
     350             : 
     351         668 :             appendStringInfoChar(&buf, ')');
     352             : 
     353         668 :             values[4] = CStringGetTextDatum(buf.data);
     354         668 :             nulls[4] = false;
     355             :         }
     356             :         else
     357             :         {
     358           0 :             values[4] = (Datum) 0;
     359           0 :             nulls[4] = true;
     360             :         }
     361             : 
     362         668 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
     363             :     }
     364             : 
     365           8 :     index_close(indexRel, AccessShareLock);
     366             : 
     367           8 :     return (Datum) 0;
     368             : }

Generated by: LCOV version 1.16