LCOV - code coverage report
Current view: top level - contrib/pageinspect - btreefuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 204 248 82.3 %
Date: 2020-05-31 22:07:05 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * contrib/pageinspect/btreefuncs.c
       3             :  *
       4             :  *
       5             :  * btreefuncs.c
       6             :  *
       7             :  * Copyright (c) 2006 Satoshi Nagayasu <nagayasus@nttdata.co.jp>
       8             :  *
       9             :  * Permission to use, copy, modify, and distribute this software and
      10             :  * its documentation for any purpose, without fee, and without a
      11             :  * written agreement is hereby granted, provided that the above
      12             :  * copyright notice and this paragraph and the following two
      13             :  * paragraphs appear in all copies.
      14             :  *
      15             :  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
      16             :  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
      17             :  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
      18             :  * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
      19             :  * OF THE POSSIBILITY OF SUCH DAMAGE.
      20             :  *
      21             :  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
      24             :  * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
      25             :  * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
      26             :  */
      27             : 
      28             : #include "postgres.h"
      29             : 
      30             : #include "access/nbtree.h"
      31             : #include "access/relation.h"
      32             : #include "catalog/namespace.h"
      33             : #include "catalog/pg_am.h"
      34             : #include "catalog/pg_type.h"
      35             : #include "funcapi.h"
      36             : #include "miscadmin.h"
      37             : #include "pageinspect.h"
      38             : #include "utils/array.h"
      39             : #include "utils/builtins.h"
      40             : #include "utils/rel.h"
      41             : #include "utils/varlena.h"
      42             : 
      43          20 : PG_FUNCTION_INFO_V1(bt_metap);
      44          14 : PG_FUNCTION_INFO_V1(bt_page_items);
      45          14 : PG_FUNCTION_INFO_V1(bt_page_items_bytea);
      46           8 : PG_FUNCTION_INFO_V1(bt_page_stats);
      47             : 
      48             : #define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
      49             : #define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
      50             : #define DatumGetItemPointer(X)   ((ItemPointer) DatumGetPointer(X))
      51             : #define ItemPointerGetDatum(X)   PointerGetDatum(X)
      52             : 
      53             : /* note: BlockNumber is unsigned, hence can't be negative */
      54             : #define CHECK_RELATION_BLOCK_RANGE(rel, blkno) { \
      55             :         if ( RelationGetNumberOfBlocks(rel) <= (BlockNumber) (blkno) ) \
      56             :              elog(ERROR, "block number out of range"); }
      57             : 
      58             : /* ------------------------------------------------
      59             :  * structure for single btree page statistics
      60             :  * ------------------------------------------------
      61             :  */
      62             : typedef struct BTPageStat
      63             : {
      64             :     uint32      blkno;
      65             :     uint32      live_items;
      66             :     uint32      dead_items;
      67             :     uint32      page_size;
      68             :     uint32      max_avail;
      69             :     uint32      free_size;
      70             :     uint32      avg_item_size;
      71             :     char        type;
      72             : 
      73             :     /* opaque data */
      74             :     BlockNumber btpo_prev;
      75             :     BlockNumber btpo_next;
      76             :     union
      77             :     {
      78             :         uint32      level;
      79             :         TransactionId xact;
      80             :     }           btpo;
      81             :     uint16      btpo_flags;
      82             :     BTCycleId   btpo_cycleid;
      83             : } BTPageStat;
      84             : 
      85             : 
      86             : /* -------------------------------------------------
      87             :  * GetBTPageStatistics()
      88             :  *
      89             :  * Collect statistics of single b-tree page
      90             :  * -------------------------------------------------
      91             :  */
      92             : static void
      93           2 : GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
      94             : {
      95           2 :     Page        page = BufferGetPage(buffer);
      96           2 :     PageHeader  phdr = (PageHeader) page;
      97           2 :     OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
      98           2 :     BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
      99           2 :     int         item_size = 0;
     100             :     int         off;
     101             : 
     102           2 :     stat->blkno = blkno;
     103             : 
     104           2 :     stat->max_avail = BLCKSZ - (BLCKSZ - phdr->pd_special + SizeOfPageHeaderData);
     105             : 
     106           2 :     stat->dead_items = stat->live_items = 0;
     107             : 
     108           2 :     stat->page_size = PageGetPageSize(page);
     109             : 
     110             :     /* page type (flags) */
     111           2 :     if (P_ISDELETED(opaque))
     112             :     {
     113           0 :         stat->type = 'd';
     114           0 :         stat->btpo.xact = opaque->btpo.xact;
     115           0 :         return;
     116             :     }
     117           2 :     else if (P_IGNORE(opaque))
     118           0 :         stat->type = 'e';
     119           2 :     else if (P_ISLEAF(opaque))
     120           2 :         stat->type = 'l';
     121           0 :     else if (P_ISROOT(opaque))
     122           0 :         stat->type = 'r';
     123             :     else
     124           0 :         stat->type = 'i';
     125             : 
     126             :     /* btpage opaque data */
     127           2 :     stat->btpo_prev = opaque->btpo_prev;
     128           2 :     stat->btpo_next = opaque->btpo_next;
     129           2 :     stat->btpo.level = opaque->btpo.level;
     130           2 :     stat->btpo_flags = opaque->btpo_flags;
     131           2 :     stat->btpo_cycleid = opaque->btpo_cycleid;
     132             : 
     133             :     /* count live and dead tuples, and free space */
     134           4 :     for (off = FirstOffsetNumber; off <= maxoff; off++)
     135             :     {
     136             :         IndexTuple  itup;
     137             : 
     138           2 :         ItemId      id = PageGetItemId(page, off);
     139             : 
     140           2 :         itup = (IndexTuple) PageGetItem(page, id);
     141             : 
     142           2 :         item_size += IndexTupleSize(itup);
     143             : 
     144           2 :         if (!ItemIdIsDead(id))
     145           2 :             stat->live_items++;
     146             :         else
     147           0 :             stat->dead_items++;
     148             :     }
     149           2 :     stat->free_size = PageGetFreeSpace(page);
     150             : 
     151           2 :     if ((stat->live_items + stat->dead_items) > 0)
     152           2 :         stat->avg_item_size = item_size / (stat->live_items + stat->dead_items);
     153             :     else
     154           0 :         stat->avg_item_size = 0;
     155             : }
     156             : 
     157             : /* -----------------------------------------------
     158             :  * bt_page_stats()
     159             :  *
     160             :  * Usage: SELECT * FROM bt_page_stats('t1_pkey', 1);
     161             :  * -----------------------------------------------
     162             :  */
     163             : Datum
     164           6 : bt_page_stats(PG_FUNCTION_ARGS)
     165             : {
     166           6 :     text       *relname = PG_GETARG_TEXT_PP(0);
     167           6 :     uint32      blkno = PG_GETARG_UINT32(1);
     168             :     Buffer      buffer;
     169             :     Relation    rel;
     170             :     RangeVar   *relrv;
     171             :     Datum       result;
     172             :     HeapTuple   tuple;
     173             :     TupleDesc   tupleDesc;
     174             :     int         j;
     175             :     char       *values[11];
     176             :     BTPageStat  stat;
     177             : 
     178           6 :     if (!superuser())
     179           0 :         ereport(ERROR,
     180             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     181             :                  errmsg("must be superuser to use pageinspect functions")));
     182             : 
     183           6 :     relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
     184           6 :     rel = relation_openrv(relrv, AccessShareLock);
     185             : 
     186           6 :     if (!IS_INDEX(rel) || !IS_BTREE(rel))
     187           0 :         elog(ERROR, "relation \"%s\" is not a btree index",
     188             :              RelationGetRelationName(rel));
     189             : 
     190             :     /*
     191             :      * Reject attempts to read non-local temporary relations; we would be
     192             :      * likely to get wrong data since we have no visibility into the owning
     193             :      * session's local buffers.
     194             :      */
     195           6 :     if (RELATION_IS_OTHER_TEMP(rel))
     196           0 :         ereport(ERROR,
     197             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     198             :                  errmsg("cannot access temporary tables of other sessions")));
     199             : 
     200           6 :     if (blkno == 0)
     201           2 :         elog(ERROR, "block 0 is a meta page");
     202             : 
     203           4 :     CHECK_RELATION_BLOCK_RANGE(rel, blkno);
     204             : 
     205           2 :     buffer = ReadBuffer(rel, blkno);
     206           2 :     LockBuffer(buffer, BUFFER_LOCK_SHARE);
     207             : 
     208             :     /* keep compiler quiet */
     209           2 :     stat.btpo_prev = stat.btpo_next = InvalidBlockNumber;
     210           2 :     stat.btpo_flags = stat.free_size = stat.avg_item_size = 0;
     211             : 
     212           2 :     GetBTPageStatistics(blkno, buffer, &stat);
     213             : 
     214           2 :     UnlockReleaseBuffer(buffer);
     215           2 :     relation_close(rel, AccessShareLock);
     216             : 
     217             :     /* Build a tuple descriptor for our result type */
     218           2 :     if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
     219           0 :         elog(ERROR, "return type must be a row type");
     220             : 
     221           2 :     j = 0;
     222           2 :     values[j++] = psprintf("%d", stat.blkno);
     223           2 :     values[j++] = psprintf("%c", stat.type);
     224           2 :     values[j++] = psprintf("%d", stat.live_items);
     225           2 :     values[j++] = psprintf("%d", stat.dead_items);
     226           2 :     values[j++] = psprintf("%d", stat.avg_item_size);
     227           2 :     values[j++] = psprintf("%d", stat.page_size);
     228           2 :     values[j++] = psprintf("%d", stat.free_size);
     229           2 :     values[j++] = psprintf("%d", stat.btpo_prev);
     230           2 :     values[j++] = psprintf("%d", stat.btpo_next);
     231           2 :     values[j++] = psprintf("%d", (stat.type == 'd') ? stat.btpo.xact : stat.btpo.level);
     232           2 :     values[j++] = psprintf("%d", stat.btpo_flags);
     233             : 
     234           2 :     tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
     235             :                                    values);
     236             : 
     237           2 :     result = HeapTupleGetDatum(tuple);
     238             : 
     239           2 :     PG_RETURN_DATUM(result);
     240             : }
     241             : 
     242             : 
     243             : /*
     244             :  * cross-call data structure for SRF
     245             :  */
     246             : struct user_args
     247             : {
     248             :     Page        page;
     249             :     OffsetNumber offset;
     250             :     bool        leafpage;
     251             :     bool        rightmost;
     252             :     TupleDesc   tupd;
     253             : };
     254             : 
     255             : /*-------------------------------------------------------
     256             :  * bt_page_print_tuples()
     257             :  *
     258             :  * Form a tuple describing index tuple at a given offset
     259             :  * ------------------------------------------------------
     260             :  */
     261             : static Datum
     262           4 : bt_page_print_tuples(FuncCallContext *fctx, struct user_args *uargs)
     263             : {
     264           4 :     Page        page = uargs->page;
     265           4 :     OffsetNumber offset = uargs->offset;
     266           4 :     bool        leafpage = uargs->leafpage;
     267           4 :     bool        rightmost = uargs->rightmost;
     268             :     bool        ispivottuple;
     269             :     Datum       values[9];
     270             :     bool        nulls[9];
     271             :     HeapTuple   tuple;
     272             :     ItemId      id;
     273             :     IndexTuple  itup;
     274             :     int         j;
     275             :     int         off;
     276             :     int         dlen;
     277             :     char       *dump,
     278             :                *datacstring;
     279             :     char       *ptr;
     280             :     ItemPointer htid;
     281             : 
     282           4 :     id = PageGetItemId(page, offset);
     283             : 
     284           4 :     if (!ItemIdIsValid(id))
     285           0 :         elog(ERROR, "invalid ItemId");
     286             : 
     287           4 :     itup = (IndexTuple) PageGetItem(page, id);
     288             : 
     289           4 :     j = 0;
     290           4 :     memset(nulls, 0, sizeof(nulls));
     291           4 :     values[j++] = DatumGetInt16(offset);
     292           4 :     values[j++] = ItemPointerGetDatum(&itup->t_tid);
     293           4 :     values[j++] = Int32GetDatum((int) IndexTupleSize(itup));
     294           4 :     values[j++] = BoolGetDatum(IndexTupleHasNulls(itup));
     295           4 :     values[j++] = BoolGetDatum(IndexTupleHasVarwidths(itup));
     296             : 
     297           4 :     ptr = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
     298           4 :     dlen = IndexTupleSize(itup) - IndexInfoFindDataOffset(itup->t_info);
     299             : 
     300             :     /*
     301             :      * Make sure that "data" column does not include posting list or pivot
     302             :      * tuple representation of heap TID(s).
     303             :      *
     304             :      * Note: BTreeTupleIsPivot() won't work reliably on !heapkeyspace indexes
     305             :      * (those built before BTREE_VERSION 4), but we have no way of determining
     306             :      * if this page came from a !heapkeyspace index.  We may only have a bytea
     307             :      * nbtree page image to go on, so in general there is no metapage that we
     308             :      * can check.
     309             :      *
     310             :      * That's okay here because BTreeTupleIsPivot() can only return false for
     311             :      * a !heapkeyspace pivot, never true for a !heapkeyspace non-pivot.  Since
     312             :      * heap TID isn't part of the keyspace in a !heapkeyspace index anyway,
     313             :      * there cannot possibly be a pivot tuple heap TID representation that we
     314             :      * fail to make an adjustment for.  A !heapkeyspace index can have
     315             :      * BTreeTupleIsPivot() return true (due to things like suffix truncation
     316             :      * for INCLUDE indexes in Postgres v11), but when that happens
     317             :      * BTreeTupleGetHeapTID() can be trusted to work reliably (i.e. return
     318             :      * NULL).
     319             :      *
     320             :      * Note: BTreeTupleIsPosting() always works reliably, even with
     321             :      * !heapkeyspace indexes.
     322             :      */
     323           4 :     if (BTreeTupleIsPosting(itup))
     324           0 :         dlen -= IndexTupleSize(itup) - BTreeTupleGetPostingOffset(itup);
     325           4 :     else if (BTreeTupleIsPivot(itup) && BTreeTupleGetHeapTID(itup) != NULL)
     326           0 :         dlen -= MAXALIGN(sizeof(ItemPointerData));
     327             : 
     328           4 :     if (dlen < 0 || dlen > INDEX_SIZE_MASK)
     329           0 :         elog(ERROR, "invalid tuple length %d for tuple at offset number %u",
     330             :              dlen, offset);
     331           4 :     dump = palloc0(dlen * 3 + 1);
     332           4 :     datacstring = dump;
     333          36 :     for (off = 0; off < dlen; off++)
     334             :     {
     335          32 :         if (off > 0)
     336          28 :             *dump++ = ' ';
     337          32 :         sprintf(dump, "%02x", *(ptr + off) & 0xff);
     338          32 :         dump += 2;
     339             :     }
     340           4 :     values[j++] = CStringGetTextDatum(datacstring);
     341           4 :     pfree(datacstring);
     342             : 
     343             :     /*
     344             :      * We need to work around the BTreeTupleIsPivot() !heapkeyspace limitation
     345             :      * again.  Deduce whether or not tuple must be a pivot tuple based on
     346             :      * whether or not the page is a leaf page, as well as the page offset
     347             :      * number of the tuple.
     348             :      */
     349           4 :     ispivottuple = (!leafpage || (!rightmost && offset == P_HIKEY));
     350             : 
     351             :     /* LP_DEAD bit can never be set for pivot tuples, so show a NULL there */
     352           4 :     if (!ispivottuple)
     353           4 :         values[j++] = BoolGetDatum(ItemIdIsDead(id));
     354             :     else
     355             :     {
     356             :         Assert(!ItemIdIsDead(id));
     357           0 :         nulls[j++] = true;
     358             :     }
     359             : 
     360           4 :     htid = BTreeTupleGetHeapTID(itup);
     361           4 :     if (ispivottuple && !BTreeTupleIsPivot(itup))
     362             :     {
     363             :         /* Don't show bogus heap TID in !heapkeyspace pivot tuple */
     364           0 :         htid = NULL;
     365             :     }
     366             : 
     367           4 :     if (htid)
     368           4 :         values[j++] = ItemPointerGetDatum(htid);
     369             :     else
     370           0 :         nulls[j++] = true;
     371             : 
     372           4 :     if (BTreeTupleIsPosting(itup))
     373             :     {
     374             :         /* Build an array of item pointers */
     375             :         ItemPointer tids;
     376             :         Datum      *tids_datum;
     377             :         int         nposting;
     378             : 
     379           0 :         tids = BTreeTupleGetPosting(itup);
     380           0 :         nposting = BTreeTupleGetNPosting(itup);
     381           0 :         tids_datum = (Datum *) palloc(nposting * sizeof(Datum));
     382           0 :         for (int i = 0; i < nposting; i++)
     383           0 :             tids_datum[i] = ItemPointerGetDatum(&tids[i]);
     384           0 :         values[j++] = PointerGetDatum(construct_array(tids_datum,
     385             :                                                       nposting,
     386             :                                                       TIDOID,
     387             :                                                       sizeof(ItemPointerData),
     388             :                                                       false, TYPALIGN_SHORT));
     389           0 :         pfree(tids_datum);
     390             :     }
     391             :     else
     392           4 :         nulls[j++] = true;
     393             : 
     394             :     /* Build and return the result tuple */
     395           4 :     tuple = heap_form_tuple(uargs->tupd, values, nulls);
     396             : 
     397           4 :     return HeapTupleGetDatum(tuple);
     398             : }
     399             : 
     400             : /*-------------------------------------------------------
     401             :  * bt_page_items()
     402             :  *
     403             :  * Get IndexTupleData set in a btree page
     404             :  *
     405             :  * Usage: SELECT * FROM bt_page_items('t1_pkey', 1);
     406             :  *-------------------------------------------------------
     407             :  */
     408             : Datum
     409           8 : bt_page_items(PG_FUNCTION_ARGS)
     410             : {
     411           8 :     text       *relname = PG_GETARG_TEXT_PP(0);
     412           8 :     uint32      blkno = PG_GETARG_UINT32(1);
     413             :     Datum       result;
     414             :     FuncCallContext *fctx;
     415             :     MemoryContext mctx;
     416             :     struct user_args *uargs;
     417             : 
     418           8 :     if (!superuser())
     419           0 :         ereport(ERROR,
     420             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     421             :                  errmsg("must be superuser to use pageinspect functions")));
     422             : 
     423           8 :     if (SRF_IS_FIRSTCALL())
     424             :     {
     425             :         RangeVar   *relrv;
     426             :         Relation    rel;
     427             :         Buffer      buffer;
     428             :         BTPageOpaque opaque;
     429             :         TupleDesc   tupleDesc;
     430             : 
     431           6 :         fctx = SRF_FIRSTCALL_INIT();
     432             : 
     433           6 :         relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
     434           6 :         rel = relation_openrv(relrv, AccessShareLock);
     435             : 
     436           6 :         if (!IS_INDEX(rel) || !IS_BTREE(rel))
     437           0 :             elog(ERROR, "relation \"%s\" is not a btree index",
     438             :                  RelationGetRelationName(rel));
     439             : 
     440             :         /*
     441             :          * Reject attempts to read non-local temporary relations; we would be
     442             :          * likely to get wrong data since we have no visibility into the
     443             :          * owning session's local buffers.
     444             :          */
     445           6 :         if (RELATION_IS_OTHER_TEMP(rel))
     446           0 :             ereport(ERROR,
     447             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     448             :                      errmsg("cannot access temporary tables of other sessions")));
     449             : 
     450           6 :         if (blkno == 0)
     451           2 :             elog(ERROR, "block 0 is a meta page");
     452             : 
     453           4 :         CHECK_RELATION_BLOCK_RANGE(rel, blkno);
     454             : 
     455           2 :         buffer = ReadBuffer(rel, blkno);
     456           2 :         LockBuffer(buffer, BUFFER_LOCK_SHARE);
     457             : 
     458             :         /*
     459             :          * We copy the page into local storage to avoid holding pin on the
     460             :          * buffer longer than we must, and possibly failing to release it at
     461             :          * all if the calling query doesn't fetch all rows.
     462             :          */
     463           2 :         mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
     464             : 
     465           2 :         uargs = palloc(sizeof(struct user_args));
     466             : 
     467           2 :         uargs->page = palloc(BLCKSZ);
     468           2 :         memcpy(uargs->page, BufferGetPage(buffer), BLCKSZ);
     469             : 
     470           2 :         UnlockReleaseBuffer(buffer);
     471           2 :         relation_close(rel, AccessShareLock);
     472             : 
     473           2 :         uargs->offset = FirstOffsetNumber;
     474             : 
     475           2 :         opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
     476             : 
     477           2 :         if (P_ISDELETED(opaque))
     478           0 :             elog(NOTICE, "page is deleted");
     479             : 
     480           2 :         fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
     481           2 :         uargs->leafpage = P_ISLEAF(opaque);
     482           2 :         uargs->rightmost = P_RIGHTMOST(opaque);
     483             : 
     484             :         /* Build a tuple descriptor for our result type */
     485           2 :         if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
     486           0 :             elog(ERROR, "return type must be a row type");
     487           2 :         tupleDesc = BlessTupleDesc(tupleDesc);
     488             : 
     489           2 :         uargs->tupd = tupleDesc;
     490             : 
     491           2 :         fctx->user_fctx = uargs;
     492             : 
     493           2 :         MemoryContextSwitchTo(mctx);
     494             :     }
     495             : 
     496           4 :     fctx = SRF_PERCALL_SETUP();
     497           4 :     uargs = fctx->user_fctx;
     498             : 
     499           4 :     if (fctx->call_cntr < fctx->max_calls)
     500             :     {
     501           2 :         result = bt_page_print_tuples(fctx, uargs);
     502           2 :         uargs->offset++;
     503           2 :         SRF_RETURN_NEXT(fctx, result);
     504             :     }
     505             : 
     506           2 :     SRF_RETURN_DONE(fctx);
     507             : }
     508             : 
     509             : /*-------------------------------------------------------
     510             :  * bt_page_items_bytea()
     511             :  *
     512             :  * Get IndexTupleData set in a btree page
     513             :  *
     514             :  * Usage: SELECT * FROM bt_page_items(get_raw_page('t1_pkey', 1));
     515             :  *-------------------------------------------------------
     516             :  */
     517             : 
     518             : Datum
     519           6 : bt_page_items_bytea(PG_FUNCTION_ARGS)
     520             : {
     521           6 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
     522             :     Datum       result;
     523             :     FuncCallContext *fctx;
     524             :     struct user_args *uargs;
     525             :     int         raw_page_size;
     526             : 
     527           6 :     if (!superuser())
     528           0 :         ereport(ERROR,
     529             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     530             :                  errmsg("must be superuser to use raw page functions")));
     531             : 
     532           6 :     if (SRF_IS_FIRSTCALL())
     533             :     {
     534             :         BTPageOpaque opaque;
     535             :         MemoryContext mctx;
     536             :         TupleDesc   tupleDesc;
     537             : 
     538           4 :         raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
     539             : 
     540           4 :         if (raw_page_size < SizeOfPageHeaderData)
     541           0 :             ereport(ERROR,
     542             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     543             :                      errmsg("input page too small (%d bytes)", raw_page_size)));
     544             : 
     545           4 :         fctx = SRF_FIRSTCALL_INIT();
     546           4 :         mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
     547             : 
     548           4 :         uargs = palloc(sizeof(struct user_args));
     549             : 
     550           4 :         uargs->page = VARDATA(raw_page);
     551             : 
     552           4 :         uargs->offset = FirstOffsetNumber;
     553             : 
     554           4 :         opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
     555             : 
     556           4 :         if (P_ISMETA(opaque))
     557           2 :             ereport(ERROR,
     558             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     559             :                      errmsg("block is a meta page")));
     560             : 
     561           2 :         if (P_ISDELETED(opaque))
     562           0 :             elog(NOTICE, "page is deleted");
     563             : 
     564           2 :         fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
     565           2 :         uargs->leafpage = P_ISLEAF(opaque);
     566           2 :         uargs->rightmost = P_RIGHTMOST(opaque);
     567             : 
     568             :         /* Build a tuple descriptor for our result type */
     569           2 :         if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
     570           0 :             elog(ERROR, "return type must be a row type");
     571           2 :         tupleDesc = BlessTupleDesc(tupleDesc);
     572             : 
     573           2 :         uargs->tupd = tupleDesc;
     574             : 
     575           2 :         fctx->user_fctx = uargs;
     576             : 
     577           2 :         MemoryContextSwitchTo(mctx);
     578             :     }
     579             : 
     580           4 :     fctx = SRF_PERCALL_SETUP();
     581           4 :     uargs = fctx->user_fctx;
     582             : 
     583           4 :     if (fctx->call_cntr < fctx->max_calls)
     584             :     {
     585           2 :         result = bt_page_print_tuples(fctx, uargs);
     586           2 :         uargs->offset++;
     587           2 :         SRF_RETURN_NEXT(fctx, result);
     588             :     }
     589             : 
     590           2 :     SRF_RETURN_DONE(fctx);
     591             : }
     592             : 
     593             : /* Number of output arguments (columns) for bt_metap() */
     594             : #define BT_METAP_COLS_V1_8      9
     595             : 
     596             : /* ------------------------------------------------
     597             :  * bt_metap()
     598             :  *
     599             :  * Get a btree's meta-page information
     600             :  *
     601             :  * Usage: SELECT * FROM bt_metap('t1_pkey')
     602             :  * ------------------------------------------------
     603             :  */
     604             : Datum
     605           2 : bt_metap(PG_FUNCTION_ARGS)
     606             : {
     607           2 :     text       *relname = PG_GETARG_TEXT_PP(0);
     608             :     Datum       result;
     609             :     Relation    rel;
     610             :     RangeVar   *relrv;
     611             :     BTMetaPageData *metad;
     612             :     TupleDesc   tupleDesc;
     613             :     int         j;
     614             :     char       *values[9];
     615             :     Buffer      buffer;
     616             :     Page        page;
     617             :     HeapTuple   tuple;
     618             : 
     619           2 :     if (!superuser())
     620           0 :         ereport(ERROR,
     621             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     622             :                  errmsg("must be superuser to use pageinspect functions")));
     623             : 
     624           2 :     relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
     625           2 :     rel = relation_openrv(relrv, AccessShareLock);
     626             : 
     627           2 :     if (!IS_INDEX(rel) || !IS_BTREE(rel))
     628           0 :         elog(ERROR, "relation \"%s\" is not a btree index",
     629             :              RelationGetRelationName(rel));
     630             : 
     631             :     /*
     632             :      * Reject attempts to read non-local temporary relations; we would be
     633             :      * likely to get wrong data since we have no visibility into the owning
     634             :      * session's local buffers.
     635             :      */
     636           2 :     if (RELATION_IS_OTHER_TEMP(rel))
     637           0 :         ereport(ERROR,
     638             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     639             :                  errmsg("cannot access temporary tables of other sessions")));
     640             : 
     641           2 :     buffer = ReadBuffer(rel, 0);
     642           2 :     LockBuffer(buffer, BUFFER_LOCK_SHARE);
     643             : 
     644           2 :     page = BufferGetPage(buffer);
     645           2 :     metad = BTPageGetMeta(page);
     646             : 
     647             :     /* Build a tuple descriptor for our result type */
     648           2 :     if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
     649           0 :         elog(ERROR, "return type must be a row type");
     650             : 
     651             :     /*
     652             :      * We need a kluge here to detect API versions prior to 1.8.  Earlier
     653             :      * versions incorrectly used int4 for certain columns.  This caused
     654             :      * various problems.  For example, an int4 version of the "oldest_xact"
     655             :      * column would not work with TransactionId values that happened to exceed
     656             :      * PG_INT32_MAX.
     657             :      *
     658             :      * There is no way to reliably avoid the problems created by the old
     659             :      * function definition at this point, so insist that the user update the
     660             :      * extension.
     661             :      */
     662           2 :     if (tupleDesc->natts < BT_METAP_COLS_V1_8)
     663           0 :         ereport(ERROR,
     664             :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
     665             :                  errmsg("function has wrong number of declared columns"),
     666             :                  errhint("To resolve the problem, update the \"pageinspect\" extension to the latest version.")));
     667             : 
     668           2 :     j = 0;
     669           2 :     values[j++] = psprintf("%d", metad->btm_magic);
     670           2 :     values[j++] = psprintf("%d", metad->btm_version);
     671           2 :     values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_root);
     672           2 :     values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_level);
     673           2 :     values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_fastroot);
     674           2 :     values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_fastlevel);
     675             : 
     676             :     /*
     677             :      * Get values of extended metadata if available, use default values
     678             :      * otherwise.  Note that we rely on the assumption that btm_allequalimage
     679             :      * is initialized to zero with indexes that were built on versions prior
     680             :      * to Postgres 13 (just like _bt_metaversion()).
     681             :      */
     682           2 :     if (metad->btm_version >= BTREE_NOVAC_VERSION)
     683             :     {
     684           2 :         values[j++] = psprintf("%u", metad->btm_oldest_btpo_xact);
     685           2 :         values[j++] = psprintf("%f", metad->btm_last_cleanup_num_heap_tuples);
     686           2 :         values[j++] = metad->btm_allequalimage ? "t" : "f";
     687             :     }
     688             :     else
     689             :     {
     690           0 :         values[j++] = "0";
     691           0 :         values[j++] = "-1";
     692           0 :         values[j++] = "f";
     693             :     }
     694             : 
     695           2 :     tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
     696             :                                    values);
     697             : 
     698           2 :     result = HeapTupleGetDatum(tuple);
     699             : 
     700           2 :     UnlockReleaseBuffer(buffer);
     701           2 :     relation_close(rel, AccessShareLock);
     702             : 
     703           2 :     PG_RETURN_DATUM(result);
     704             : }

Generated by: LCOV version 1.13