LCOV - code coverage report
Current view: top level - contrib/pageinspect - ginfuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 89 104 85.6 %
Date: 2019-11-21 13:06:38 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * ginfuncs.c
       3             :  *      Functions to investigate the content of GIN indexes
       4             :  *
       5             :  * Copyright (c) 2014-2019, PostgreSQL Global Development Group
       6             :  *
       7             :  * IDENTIFICATION
       8             :  *      contrib/pageinspect/ginfuncs.c
       9             :  */
      10             : #include "postgres.h"
      11             : 
      12             : #include "access/gin.h"
      13             : #include "access/gin_private.h"
      14             : #include "access/htup_details.h"
      15             : #include "catalog/namespace.h"
      16             : #include "catalog/pg_type.h"
      17             : #include "funcapi.h"
      18             : #include "miscadmin.h"
      19             : #include "pageinspect.h"
      20             : #include "utils/array.h"
      21             : #include "utils/builtins.h"
      22             : #include "utils/rel.h"
      23             : 
      24             : #define DatumGetItemPointer(X)   ((ItemPointer) DatumGetPointer(X))
      25             : #define ItemPointerGetDatum(X)   PointerGetDatum(X)
      26             : 
      27             : 
      28           8 : PG_FUNCTION_INFO_V1(gin_metapage_info);
      29           8 : PG_FUNCTION_INFO_V1(gin_page_opaque_info);
      30           8 : PG_FUNCTION_INFO_V1(gin_leafpage_items);
      31             : 
      32             : 
      33             : Datum
      34           4 : gin_metapage_info(PG_FUNCTION_ARGS)
      35             : {
      36           4 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
      37             :     TupleDesc   tupdesc;
      38             :     Page        page;
      39             :     GinPageOpaque opaq;
      40             :     GinMetaPageData *metadata;
      41             :     HeapTuple   resultTuple;
      42             :     Datum       values[10];
      43             :     bool        nulls[10];
      44             : 
      45           4 :     if (!superuser())
      46           0 :         ereport(ERROR,
      47             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
      48             :                  (errmsg("must be superuser to use raw page functions"))));
      49             : 
      50           4 :     page = get_page_from_raw(raw_page);
      51             : 
      52           4 :     opaq = (GinPageOpaque) PageGetSpecialPointer(page);
      53           4 :     if (opaq->flags != GIN_META)
      54           2 :         ereport(ERROR,
      55             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
      56             :                  errmsg("input page is not a GIN metapage"),
      57             :                  errdetail("Flags %04X, expected %04X",
      58             :                            opaq->flags, GIN_META)));
      59             : 
      60             :     /* Build a tuple descriptor for our result type */
      61           2 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
      62           0 :         elog(ERROR, "return type must be a row type");
      63             : 
      64           2 :     metadata = GinPageGetMeta(page);
      65             : 
      66           2 :     memset(nulls, 0, sizeof(nulls));
      67             : 
      68           2 :     values[0] = Int64GetDatum(metadata->head);
      69           2 :     values[1] = Int64GetDatum(metadata->tail);
      70           2 :     values[2] = Int32GetDatum(metadata->tailFreeSize);
      71           2 :     values[3] = Int64GetDatum(metadata->nPendingPages);
      72           2 :     values[4] = Int64GetDatum(metadata->nPendingHeapTuples);
      73             : 
      74             :     /* statistics, updated by VACUUM */
      75           2 :     values[5] = Int64GetDatum(metadata->nTotalPages);
      76           2 :     values[6] = Int64GetDatum(metadata->nEntryPages);
      77           2 :     values[7] = Int64GetDatum(metadata->nDataPages);
      78           2 :     values[8] = Int64GetDatum(metadata->nEntries);
      79             : 
      80           2 :     values[9] = Int32GetDatum(metadata->ginVersion);
      81             : 
      82             :     /* Build and return the result tuple. */
      83           2 :     resultTuple = heap_form_tuple(tupdesc, values, nulls);
      84             : 
      85           2 :     return HeapTupleGetDatum(resultTuple);
      86             : }
      87             : 
      88             : 
      89             : Datum
      90           2 : gin_page_opaque_info(PG_FUNCTION_ARGS)
      91             : {
      92           2 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
      93             :     TupleDesc   tupdesc;
      94             :     Page        page;
      95             :     GinPageOpaque opaq;
      96             :     HeapTuple   resultTuple;
      97             :     Datum       values[3];
      98             :     bool        nulls[3];
      99             :     Datum       flags[16];
     100           2 :     int         nflags = 0;
     101             :     uint16      flagbits;
     102             : 
     103           2 :     if (!superuser())
     104           0 :         ereport(ERROR,
     105             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     106             :                  (errmsg("must be superuser to use raw page functions"))));
     107             : 
     108           2 :     page = get_page_from_raw(raw_page);
     109             : 
     110           2 :     opaq = (GinPageOpaque) PageGetSpecialPointer(page);
     111             : 
     112             :     /* Build a tuple descriptor for our result type */
     113           2 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     114           0 :         elog(ERROR, "return type must be a row type");
     115             : 
     116             :     /* Convert the flags bitmask to an array of human-readable names */
     117           2 :     flagbits = opaq->flags;
     118           2 :     if (flagbits & GIN_DATA)
     119           0 :         flags[nflags++] = CStringGetTextDatum("data");
     120           2 :     if (flagbits & GIN_LEAF)
     121           2 :         flags[nflags++] = CStringGetTextDatum("leaf");
     122           2 :     if (flagbits & GIN_DELETED)
     123           0 :         flags[nflags++] = CStringGetTextDatum("deleted");
     124           2 :     if (flagbits & GIN_META)
     125           0 :         flags[nflags++] = CStringGetTextDatum("meta");
     126           2 :     if (flagbits & GIN_LIST)
     127           0 :         flags[nflags++] = CStringGetTextDatum("list");
     128           2 :     if (flagbits & GIN_LIST_FULLROW)
     129           0 :         flags[nflags++] = CStringGetTextDatum("list_fullrow");
     130           2 :     if (flagbits & GIN_INCOMPLETE_SPLIT)
     131           0 :         flags[nflags++] = CStringGetTextDatum("incomplete_split");
     132           2 :     if (flagbits & GIN_COMPRESSED)
     133           0 :         flags[nflags++] = CStringGetTextDatum("compressed");
     134           2 :     flagbits &= ~(GIN_DATA | GIN_LEAF | GIN_DELETED | GIN_META | GIN_LIST |
     135             :                   GIN_LIST_FULLROW | GIN_INCOMPLETE_SPLIT | GIN_COMPRESSED);
     136           2 :     if (flagbits)
     137             :     {
     138             :         /* any flags we don't recognize are printed in hex */
     139           0 :         flags[nflags++] = DirectFunctionCall1(to_hex32, Int32GetDatum(flagbits));
     140             :     }
     141             : 
     142           2 :     memset(nulls, 0, sizeof(nulls));
     143             : 
     144           2 :     values[0] = Int64GetDatum(opaq->rightlink);
     145           2 :     values[1] = Int32GetDatum(opaq->maxoff);
     146           2 :     values[2] = PointerGetDatum(construct_array(flags, nflags,
     147             :                                                 TEXTOID, -1, false, 'i'));
     148             : 
     149             :     /* Build and return the result tuple. */
     150           2 :     resultTuple = heap_form_tuple(tupdesc, values, nulls);
     151             : 
     152           2 :     return HeapTupleGetDatum(resultTuple);
     153             : }
     154             : 
     155             : typedef struct gin_leafpage_items_state
     156             : {
     157             :     TupleDesc   tupd;
     158             :     GinPostingList *seg;
     159             :     GinPostingList *lastseg;
     160             : } gin_leafpage_items_state;
     161             : 
     162             : Datum
     163          46 : gin_leafpage_items(PG_FUNCTION_ARGS)
     164             : {
     165          46 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
     166             :     FuncCallContext *fctx;
     167             :     gin_leafpage_items_state *inter_call_data;
     168             : 
     169          46 :     if (!superuser())
     170           0 :         ereport(ERROR,
     171             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     172             :                  (errmsg("must be superuser to use raw page functions"))));
     173             : 
     174          46 :     if (SRF_IS_FIRSTCALL())
     175             :     {
     176             :         TupleDesc   tupdesc;
     177             :         MemoryContext mctx;
     178             :         Page        page;
     179             :         GinPageOpaque opaq;
     180             : 
     181           4 :         fctx = SRF_FIRSTCALL_INIT();
     182           4 :         mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
     183             : 
     184           4 :         page = get_page_from_raw(raw_page);
     185             : 
     186           4 :         if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
     187           0 :             ereport(ERROR,
     188             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     189             :                      errmsg("input page is not a valid GIN data leaf page"),
     190             :                      errdetail("Special size %d, expected %d",
     191             :                                (int) PageGetSpecialSize(page),
     192             :                                (int) MAXALIGN(sizeof(GinPageOpaqueData)))));
     193             : 
     194           4 :         opaq = (GinPageOpaque) PageGetSpecialPointer(page);
     195           4 :         if (opaq->flags != (GIN_DATA | GIN_LEAF | GIN_COMPRESSED))
     196           2 :             ereport(ERROR,
     197             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     198             :                      errmsg("input page is not a compressed GIN data leaf page"),
     199             :                      errdetail("Flags %04X, expected %04X",
     200             :                                opaq->flags,
     201             :                                (GIN_DATA | GIN_LEAF | GIN_COMPRESSED))));
     202             : 
     203           2 :         inter_call_data = palloc(sizeof(gin_leafpage_items_state));
     204             : 
     205             :         /* Build a tuple descriptor for our result type */
     206           2 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     207           0 :             elog(ERROR, "return type must be a row type");
     208             : 
     209           2 :         inter_call_data->tupd = tupdesc;
     210             : 
     211           2 :         inter_call_data->seg = GinDataLeafPageGetPostingList(page);
     212           2 :         inter_call_data->lastseg = (GinPostingList *)
     213           4 :             (((char *) inter_call_data->seg) +
     214           2 :              GinDataLeafPageGetPostingListSize(page));
     215             : 
     216           2 :         fctx->user_fctx = inter_call_data;
     217             : 
     218           2 :         MemoryContextSwitchTo(mctx);
     219             :     }
     220             : 
     221          44 :     fctx = SRF_PERCALL_SETUP();
     222          44 :     inter_call_data = fctx->user_fctx;
     223             : 
     224          44 :     if (inter_call_data->seg != inter_call_data->lastseg)
     225             :     {
     226          42 :         GinPostingList *cur = inter_call_data->seg;
     227             :         HeapTuple   resultTuple;
     228             :         Datum       result;
     229             :         Datum       values[3];
     230             :         bool        nulls[3];
     231             :         int         ndecoded,
     232             :                     i;
     233             :         ItemPointer tids;
     234             :         Datum      *tids_datum;
     235             : 
     236          42 :         memset(nulls, 0, sizeof(nulls));
     237             : 
     238          42 :         values[0] = ItemPointerGetDatum(&cur->first);
     239          42 :         values[1] = UInt16GetDatum(cur->nbytes);
     240             : 
     241             :         /* build an array of decoded item pointers */
     242          42 :         tids = ginPostingListDecode(cur, &ndecoded);
     243          42 :         tids_datum = (Datum *) palloc(ndecoded * sizeof(Datum));
     244       12164 :         for (i = 0; i < ndecoded; i++)
     245       12122 :             tids_datum[i] = ItemPointerGetDatum(&tids[i]);
     246          42 :         values[2] = PointerGetDatum(construct_array(tids_datum,
     247             :                                                     ndecoded,
     248             :                                                     TIDOID,
     249             :                                                     sizeof(ItemPointerData),
     250             :                                                     false, 's'));
     251          42 :         pfree(tids_datum);
     252          42 :         pfree(tids);
     253             : 
     254             :         /* Build and return the result tuple. */
     255          42 :         resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
     256          42 :         result = HeapTupleGetDatum(resultTuple);
     257             : 
     258          42 :         inter_call_data->seg = GinNextPostingListSegment(cur);
     259             : 
     260          42 :         SRF_RETURN_NEXT(fctx, result);
     261             :     }
     262             :     else
     263           2 :         SRF_RETURN_DONE(fctx);
     264             : }

Generated by: LCOV version 1.13