LCOV - code coverage report
Current view: top level - contrib/pageinspect - gistfuncs.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 89.7 % 155 139
Test Date: 2026-03-05 04:15:12 Functions: 100.0 % 7 7
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            8 : PG_FUNCTION_INFO_V1(gist_page_opaque_info);
      30            8 : PG_FUNCTION_INFO_V1(gist_page_items);
      31            8 : 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           18 : verify_gist_page(bytea *raw_page)
      44              : {
      45           18 :     Page        page = get_page_from_raw(raw_page);
      46              :     GISTPageOpaque opaq;
      47              : 
      48           15 :     if (PageIsNew(page))
      49            3 :         return page;
      50              : 
      51              :     /* verify the special space has the expected size */
      52           12 :     if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GISTPageOpaqueData)))
      53            2 :         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           10 :     opaq = GistPageGetOpaque(page);
      61           10 :     if (opaq->gist_page_id != GIST_PAGE_ID)
      62            2 :         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            8 :     return page;
      70              : }
      71              : 
      72              : Datum
      73            6 : gist_page_opaque_info(PG_FUNCTION_ARGS)
      74              : {
      75            6 :     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            6 :     int         nflags = 0;
      83              :     uint16      flagbits;
      84              : 
      85            6 :     if (!superuser())
      86            0 :         ereport(ERROR,
      87              :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
      88              :                  errmsg("must be superuser to use raw page functions")));
      89              : 
      90            6 :     page = verify_gist_page(raw_page);
      91              : 
      92            4 :     if (PageIsNew(page))
      93            1 :         PG_RETURN_NULL();
      94              : 
      95              :     /* Build a tuple descriptor for our result type */
      96            3 :     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            3 :     flagbits = GistPageGetOpaque(page)->flags;
     101            3 :     if (flagbits & F_LEAF)
     102            2 :         flags[nflags++] = CStringGetTextDatum("leaf");
     103            3 :     if (flagbits & F_DELETED)
     104            0 :         flags[nflags++] = CStringGetTextDatum("deleted");
     105            3 :     if (flagbits & F_TUPLES_DELETED)
     106            0 :         flags[nflags++] = CStringGetTextDatum("tuples_deleted");
     107            3 :     if (flagbits & F_FOLLOW_RIGHT)
     108            0 :         flags[nflags++] = CStringGetTextDatum("follow_right");
     109            3 :     if (flagbits & F_HAS_GARBAGE)
     110            0 :         flags[nflags++] = CStringGetTextDatum("has_garbage");
     111            3 :     flagbits &= ~(F_LEAF | F_DELETED | F_TUPLES_DELETED | F_FOLLOW_RIGHT | F_HAS_GARBAGE);
     112            3 :     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            3 :     memset(nulls, 0, sizeof(nulls));
     119              : 
     120            3 :     values[0] = LSNGetDatum(PageGetLSN(page));
     121            3 :     values[1] = LSNGetDatum(GistPageGetNSN(page));
     122            3 :     values[2] = Int64GetDatum(GistPageGetOpaque(page)->rightlink);
     123            3 :     values[3] = PointerGetDatum(construct_array_builtin(flags, nflags, TEXTOID));
     124              : 
     125              :     /* Build and return the result tuple. */
     126            3 :     resultTuple = heap_form_tuple(tupdesc, values, nulls);
     127              : 
     128            3 :     return HeapTupleGetDatum(resultTuple);
     129              : }
     130              : 
     131              : Datum
     132            5 : gist_page_items_bytea(PG_FUNCTION_ARGS)
     133              : {
     134            5 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
     135            5 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
     136              :     Page        page;
     137              :     OffsetNumber offset;
     138            5 :     OffsetNumber maxoff = InvalidOffsetNumber;
     139              : 
     140            5 :     if (!superuser())
     141            0 :         ereport(ERROR,
     142              :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     143              :                  errmsg("must be superuser to use raw page functions")));
     144              : 
     145            5 :     InitMaterializedSRF(fcinfo, 0);
     146              : 
     147            5 :     page = verify_gist_page(raw_page);
     148              : 
     149            2 :     if (PageIsNew(page))
     150            1 :         PG_RETURN_NULL();
     151              : 
     152              :     /* Avoid bogus PageGetMaxOffsetNumber() call with deleted pages */
     153            1 :     if (GistPageIsDeleted(page))
     154            0 :         elog(NOTICE, "page is deleted");
     155              :     else
     156            1 :         maxoff = PageGetMaxOffsetNumber(page);
     157              : 
     158            1 :     for (offset = FirstOffsetNumber;
     159            7 :          offset <= maxoff;
     160            6 :          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            6 :         id = PageGetItemId(page, offset);
     170              : 
     171            6 :         if (!ItemIdIsValid(id))
     172            0 :             elog(ERROR, "invalid ItemId");
     173              : 
     174            6 :         itup = (IndexTuple) PageGetItem(page, id);
     175            6 :         tuple_len = IndexTupleSize(itup);
     176              : 
     177            6 :         memset(nulls, 0, sizeof(nulls));
     178              : 
     179            6 :         values[0] = UInt16GetDatum(offset);
     180            6 :         values[1] = ItemPointerGetDatum(&itup->t_tid);
     181            6 :         values[2] = Int32GetDatum((int) IndexTupleSize(itup));
     182              : 
     183            6 :         tuple_bytea = (bytea *) palloc(tuple_len + VARHDRSZ);
     184            6 :         SET_VARSIZE(tuple_bytea, tuple_len + VARHDRSZ);
     185            6 :         memcpy(VARDATA(tuple_bytea), itup, tuple_len);
     186            6 :         values[3] = BoolGetDatum(ItemIdIsDead(id));
     187            6 :         values[4] = PointerGetDatum(tuple_bytea);
     188              : 
     189            6 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
     190              :     }
     191              : 
     192            1 :     return (Datum) 0;
     193              : }
     194              : 
     195              : Datum
     196            8 : gist_page_items(PG_FUNCTION_ARGS)
     197              : {
     198            8 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
     199            8 :     Oid         indexRelid = PG_GETARG_OID(1);
     200            8 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
     201              :     Relation    indexRel;
     202              :     TupleDesc   tupdesc;
     203              :     Page        page;
     204              :     uint16      flagbits;
     205            8 :     bits16      printflags = 0;
     206              :     OffsetNumber offset;
     207            8 :     OffsetNumber maxoff = InvalidOffsetNumber;
     208              :     char       *index_columns;
     209              : 
     210            8 :     if (!superuser())
     211            0 :         ereport(ERROR,
     212              :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     213              :                  errmsg("must be superuser to use raw page functions")));
     214              : 
     215            8 :     InitMaterializedSRF(fcinfo, 0);
     216              : 
     217              :     /* Open the relation */
     218            8 :     indexRel = index_open(indexRelid, AccessShareLock);
     219              : 
     220            8 :     if (!IS_GIST(indexRel))
     221            1 :         ereport(ERROR,
     222              :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     223              :                  errmsg("\"%s\" is not a %s index",
     224              :                         RelationGetRelationName(indexRel), "GiST")));
     225              : 
     226            7 :     page = verify_gist_page(raw_page);
     227              : 
     228            5 :     if (PageIsNew(page))
     229              :     {
     230            1 :         index_close(indexRel, AccessShareLock);
     231            1 :         PG_RETURN_NULL();
     232              :     }
     233              : 
     234            4 :     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            4 :     printflags |= RULE_INDEXDEF_PRETTY;
     241            4 :     if (flagbits & F_LEAF)
     242              :     {
     243            2 :         tupdesc = RelationGetDescr(indexRel);
     244              :     }
     245              :     else
     246              :     {
     247            2 :         tupdesc = CreateTupleDescTruncatedCopy(RelationGetDescr(indexRel),
     248            2 :                                                IndexRelationGetNumberOfKeyAttributes(indexRel));
     249            2 :         printflags |= RULE_INDEXDEF_KEYS_ONLY;
     250              :     }
     251              : 
     252            4 :     index_columns = pg_get_indexdef_columns_extended(indexRelid,
     253              :                                                      printflags);
     254              : 
     255              :     /* Avoid bogus PageGetMaxOffsetNumber() call with deleted pages */
     256            4 :     if (GistPageIsDeleted(page))
     257            0 :         elog(NOTICE, "page is deleted");
     258              :     else
     259            4 :         maxoff = PageGetMaxOffsetNumber(page);
     260              : 
     261            4 :     for (offset = FirstOffsetNumber;
     262          338 :          offset <= maxoff;
     263          334 :          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          334 :         id = PageGetItemId(page, offset);
     275              : 
     276          334 :         if (!ItemIdIsValid(id))
     277            0 :             elog(ERROR, "invalid ItemId");
     278              : 
     279          334 :         itup = (IndexTuple) PageGetItem(page, id);
     280              : 
     281          334 :         index_deform_tuple(itup, tupdesc,
     282              :                            itup_values, itup_isnull);
     283              : 
     284          334 :         memset(nulls, 0, sizeof(nulls));
     285              : 
     286          334 :         values[0] = UInt16GetDatum(offset);
     287          334 :         values[1] = ItemPointerGetDatum(&itup->t_tid);
     288          334 :         values[2] = Int32GetDatum((int) IndexTupleSize(itup));
     289          334 :         values[3] = BoolGetDatum(ItemIdIsDead(id));
     290              : 
     291          334 :         if (index_columns)
     292              :         {
     293          334 :             initStringInfo(&buf);
     294          334 :             appendStringInfo(&buf, "(%s)=(", index_columns);
     295              : 
     296              :             /* Most of this is copied from record_out(). */
     297          938 :             for (i = 0; i < tupdesc->natts; i++)
     298              :             {
     299              :                 char       *value;
     300              :                 char       *tmp;
     301          604 :                 bool        nq = false;
     302              : 
     303          604 :                 if (itup_isnull[i])
     304          135 :                     value = "null";
     305              :                 else
     306              :                 {
     307              :                     Oid         foutoid;
     308              :                     bool        typisvarlena;
     309              :                     Oid         typoid;
     310              : 
     311          469 :                     typoid = TupleDescAttr(tupdesc, i)->atttypid;
     312          469 :                     getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
     313          469 :                     value = OidOutputFunctionCall(foutoid, itup_values[i]);
     314              :                 }
     315              : 
     316          604 :                 if (i == IndexRelationGetNumberOfKeyAttributes(indexRel))
     317          135 :                     appendStringInfoString(&buf, ") INCLUDE (");
     318          469 :                 else if (i > 0)
     319          135 :                     appendStringInfoString(&buf, ", ");
     320              : 
     321              :                 /* Check whether we need double quotes for this value */
     322          604 :                 nq = (value[0] == '\0');    /* force quotes for empty string */
     323         1441 :                 for (tmp = value; *tmp; tmp++)
     324              :                 {
     325         1171 :                     char        ch = *tmp;
     326              : 
     327         1171 :                     if (ch == '"' || ch == '\\' ||
     328          837 :                         ch == '(' || ch == ')' || ch == ',' ||
     329          837 :                         isspace((unsigned char) ch))
     330              :                     {
     331          334 :                         nq = true;
     332          334 :                         break;
     333              :                     }
     334              :                 }
     335              : 
     336              :                 /* And emit the string */
     337          604 :                 if (nq)
     338          334 :                     appendStringInfoCharMacro(&buf, '"');
     339         6919 :                 for (tmp = value; *tmp; tmp++)
     340              :                 {
     341         6315 :                     char        ch = *tmp;
     342              : 
     343         6315 :                     if (ch == '"' || ch == '\\')
     344            0 :                         appendStringInfoCharMacro(&buf, ch);
     345         6315 :                     appendStringInfoCharMacro(&buf, ch);
     346              :                 }
     347          604 :                 if (nq)
     348          334 :                     appendStringInfoCharMacro(&buf, '"');
     349              :             }
     350              : 
     351          334 :             appendStringInfoChar(&buf, ')');
     352              : 
     353          334 :             values[4] = CStringGetTextDatum(buf.data);
     354          334 :             nulls[4] = false;
     355              :         }
     356              :         else
     357              :         {
     358            0 :             values[4] = (Datum) 0;
     359            0 :             nulls[4] = true;
     360              :         }
     361              : 
     362          334 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
     363              :     }
     364              : 
     365            4 :     index_close(indexRel, AccessShareLock);
     366              : 
     367            4 :     return (Datum) 0;
     368              : }
        

Generated by: LCOV version 2.0-1