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

Generated by: LCOV version 1.14