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

Generated by: LCOV version 1.16