LCOV - code coverage report
Current view: top level - contrib/pageinspect - heapfuncs.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 87.0 % 246 214
Test Date: 2026-04-07 14:16:30 Functions: 90.0 % 10 9
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * heapfuncs.c
       4              :  *    Functions to investigate heap pages
       5              :  *
       6              :  * We check the input to these functions for corrupt pointers etc. that
       7              :  * might cause crashes, but at the same time we try to print out as much
       8              :  * information as possible, even if it's nonsense. That's because if a
       9              :  * page is corrupt, we don't know why and how exactly it is corrupt, so we
      10              :  * let the user judge it.
      11              :  *
      12              :  * These functions are restricted to superusers for the fear of introducing
      13              :  * security holes if the input checking isn't as water-tight as it should be.
      14              :  * You'd need to be superuser to obtain a raw page image anyway, so
      15              :  * there's hardly any use case for using these without superuser-rights
      16              :  * anyway.
      17              :  *
      18              :  * Copyright (c) 2007-2026, PostgreSQL Global Development Group
      19              :  *
      20              :  * IDENTIFICATION
      21              :  *    contrib/pageinspect/heapfuncs.c
      22              :  *
      23              :  *-------------------------------------------------------------------------
      24              :  */
      25              : 
      26              : #include "postgres.h"
      27              : 
      28              : #include "access/htup_details.h"
      29              : #include "access/relation.h"
      30              : #include "catalog/pg_am_d.h"
      31              : #include "catalog/pg_type.h"
      32              : #include "funcapi.h"
      33              : #include "mb/pg_wchar.h"
      34              : #include "miscadmin.h"
      35              : #include "pageinspect.h"
      36              : #include "port/pg_bitutils.h"
      37              : #include "utils/array.h"
      38              : #include "utils/builtins.h"
      39              : #include "utils/rel.h"
      40              : 
      41              : /*
      42              :  * It's not supported to create tuples with oids anymore, but when pg_upgrade
      43              :  * was used to upgrade from an older version, tuples might still have an
      44              :  * oid. Seems worthwhile to display that.
      45              :  */
      46              : static inline Oid
      47            0 : HeapTupleHeaderGetOidOld(const HeapTupleHeaderData *tup)
      48              : {
      49            0 :     if (tup->t_infomask & HEAP_HASOID_OLD)
      50            0 :         return *((const Oid *) ((const char *) (tup) + (tup)->t_hoff - sizeof(Oid)));
      51              :     else
      52            0 :         return InvalidOid;
      53              : }
      54              : 
      55              : 
      56              : /*
      57              :  * bits_to_text
      58              :  *
      59              :  * Converts a uint8-array of 'len' bits to a human-readable
      60              :  * c-string representation.
      61              :  */
      62              : static char *
      63            4 : bits_to_text(uint8 *bits, int len)
      64              : {
      65              :     int         i;
      66              :     char       *str;
      67              : 
      68            4 :     str = palloc(len + 1);
      69              : 
      70           36 :     for (i = 0; i < len; i++)
      71           32 :         str[i] = (bits[(i / 8)] & (1 << (i % 8))) ? '1' : '0';
      72              : 
      73            4 :     str[i] = '\0';
      74              : 
      75            4 :     return str;
      76              : }
      77              : 
      78              : 
      79              : /*
      80              :  * text_to_bits
      81              :  *
      82              :  * Converts a c-string representation of bits into a uint8-array. This is
      83              :  * the reverse operation of previous routine.
      84              :  */
      85              : static uint8 *
      86            2 : text_to_bits(char *str, int len)
      87              : {
      88              :     uint8      *bits;
      89            2 :     int         off = 0;
      90            2 :     char        byte = 0;
      91              : 
      92            2 :     bits = palloc(len + 1);
      93              : 
      94           18 :     while (off < len)
      95              :     {
      96           16 :         if (off % 8 == 0)
      97            2 :             byte = 0;
      98              : 
      99           16 :         if ((str[off] == '0') || (str[off] == '1'))
     100           16 :             byte = byte | ((str[off] - '0') << off % 8);
     101              :         else
     102            0 :             ereport(ERROR,
     103              :                     (errcode(ERRCODE_DATA_CORRUPTED),
     104              :                      errmsg("invalid character \"%.*s\" in t_bits string",
     105              :                             pg_mblen_cstr(str + off), str + off)));
     106              : 
     107           16 :         if (off % 8 == 7)
     108            2 :             bits[off / 8] = byte;
     109              : 
     110           16 :         off++;
     111              :     }
     112              : 
     113            2 :     return bits;
     114              : }
     115              : 
     116              : /*
     117              :  * heap_page_items
     118              :  *
     119              :  * Allows inspection of line pointers and tuple headers of a heap page.
     120              :  */
     121            9 : PG_FUNCTION_INFO_V1(heap_page_items);
     122              : 
     123              : typedef struct heap_page_items_state
     124              : {
     125              :     TupleDesc   tupd;
     126              :     Page        page;
     127              :     uint16      offset;
     128              : } heap_page_items_state;
     129              : 
     130              : Datum
     131           63 : heap_page_items(PG_FUNCTION_ARGS)
     132              : {
     133           63 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
     134           63 :     heap_page_items_state *inter_call_data = NULL;
     135              :     FuncCallContext *fctx;
     136              : 
     137           63 :     if (!superuser())
     138            0 :         ereport(ERROR,
     139              :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     140              :                  errmsg("must be superuser to use raw page functions")));
     141              : 
     142           63 :     if (SRF_IS_FIRSTCALL())
     143              :     {
     144              :         TupleDesc   tupdesc;
     145              :         MemoryContext mctx;
     146              : 
     147           10 :         fctx = SRF_FIRSTCALL_INIT();
     148           10 :         mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
     149              : 
     150           10 :         inter_call_data = palloc_object(heap_page_items_state);
     151              : 
     152              :         /* Build a tuple descriptor for our result type */
     153           10 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     154            0 :             elog(ERROR, "return type must be a row type");
     155              : 
     156           10 :         inter_call_data->tupd = tupdesc;
     157              : 
     158           10 :         inter_call_data->offset = FirstOffsetNumber;
     159           10 :         inter_call_data->page = get_page_from_raw(raw_page);
     160              : 
     161           10 :         fctx->max_calls = PageGetMaxOffsetNumber(inter_call_data->page);
     162           10 :         fctx->user_fctx = inter_call_data;
     163              : 
     164           10 :         MemoryContextSwitchTo(mctx);
     165              :     }
     166              : 
     167           63 :     fctx = SRF_PERCALL_SETUP();
     168           63 :     inter_call_data = fctx->user_fctx;
     169              : 
     170           63 :     if (fctx->call_cntr < fctx->max_calls)
     171              :     {
     172           53 :         Page        page = inter_call_data->page;
     173              :         HeapTuple   resultTuple;
     174              :         Datum       result;
     175              :         ItemId      id;
     176              :         Datum       values[14];
     177              :         bool        nulls[14];
     178              :         uint16      lp_offset;
     179              :         uint16      lp_flags;
     180              :         uint16      lp_len;
     181              : 
     182           53 :         memset(nulls, 0, sizeof(nulls));
     183              : 
     184              :         /* Extract information from the line pointer */
     185              : 
     186           53 :         id = PageGetItemId(page, inter_call_data->offset);
     187              : 
     188           53 :         lp_offset = ItemIdGetOffset(id);
     189           53 :         lp_flags = ItemIdGetFlags(id);
     190           53 :         lp_len = ItemIdGetLength(id);
     191              : 
     192           53 :         values[0] = UInt16GetDatum(inter_call_data->offset);
     193           53 :         values[1] = UInt16GetDatum(lp_offset);
     194           53 :         values[2] = UInt16GetDatum(lp_flags);
     195           53 :         values[3] = UInt16GetDatum(lp_len);
     196              : 
     197              :         /*
     198              :          * We do just enough validity checking to make sure we don't reference
     199              :          * data outside the page passed to us. The page could be corrupt in
     200              :          * many other ways, but at least we won't crash.
     201              :          */
     202           53 :         if (ItemIdHasStorage(id) &&
     203           48 :             lp_len >= MinHeapTupleSize &&
     204           48 :             lp_offset == MAXALIGN(lp_offset) &&
     205           48 :             lp_offset + lp_len <= BLCKSZ)
     206           48 :         {
     207              :             HeapTupleHeader tuphdr;
     208              : 
     209              :             /* Extract information from the tuple header */
     210           48 :             tuphdr = (HeapTupleHeader) PageGetItem(page, id);
     211              : 
     212           48 :             values[4] = UInt32GetDatum(HeapTupleHeaderGetRawXmin(tuphdr));
     213           48 :             values[5] = UInt32GetDatum(HeapTupleHeaderGetRawXmax(tuphdr));
     214              :             /* shared with xvac */
     215           48 :             values[6] = UInt32GetDatum(HeapTupleHeaderGetRawCommandId(tuphdr));
     216           48 :             values[7] = PointerGetDatum(&tuphdr->t_ctid);
     217           48 :             values[8] = UInt32GetDatum(tuphdr->t_infomask2);
     218           48 :             values[9] = UInt32GetDatum(tuphdr->t_infomask);
     219           48 :             values[10] = UInt8GetDatum(tuphdr->t_hoff);
     220              : 
     221              :             /*
     222              :              * We already checked that the item is completely within the raw
     223              :              * page passed to us, with the length given in the line pointer.
     224              :              * But t_hoff could be out of range, so check it before relying on
     225              :              * it to fetch additional info.
     226              :              */
     227           48 :             if (tuphdr->t_hoff >= SizeofHeapTupleHeader &&
     228           48 :                 tuphdr->t_hoff <= lp_len &&
     229           48 :                 tuphdr->t_hoff == MAXALIGN(tuphdr->t_hoff))
     230           48 :             {
     231              :                 int         tuple_data_len;
     232              :                 bytea      *tuple_data_bytea;
     233              : 
     234              :                 /* Copy null bitmask and OID, if present */
     235           48 :                 if (tuphdr->t_infomask & HEAP_HASNULL)
     236              :                 {
     237              :                     int         bitmaplen;
     238              : 
     239            4 :                     bitmaplen = BITMAPLEN(HeapTupleHeaderGetNatts(tuphdr));
     240              :                     /* better range-check the attribute count, too */
     241            4 :                     if (bitmaplen <= tuphdr->t_hoff - SizeofHeapTupleHeader)
     242            4 :                         values[11] =
     243            4 :                             CStringGetTextDatum(bits_to_text(tuphdr->t_bits,
     244              :                                                              bitmaplen * BITS_PER_BYTE));
     245              :                     else
     246            0 :                         nulls[11] = true;
     247              :                 }
     248              :                 else
     249           44 :                     nulls[11] = true;
     250              : 
     251           48 :                 if (tuphdr->t_infomask & HEAP_HASOID_OLD)
     252            0 :                     values[12] = ObjectIdGetDatum(HeapTupleHeaderGetOidOld(tuphdr));
     253              :                 else
     254           48 :                     nulls[12] = true;
     255              : 
     256              :                 /* Copy raw tuple data into bytea attribute */
     257           48 :                 tuple_data_len = lp_len - tuphdr->t_hoff;
     258           48 :                 tuple_data_bytea = (bytea *) palloc(tuple_data_len + VARHDRSZ);
     259           48 :                 SET_VARSIZE(tuple_data_bytea, tuple_data_len + VARHDRSZ);
     260           48 :                 if (tuple_data_len > 0)
     261           48 :                     memcpy(VARDATA(tuple_data_bytea),
     262           48 :                            (char *) tuphdr + tuphdr->t_hoff,
     263              :                            tuple_data_len);
     264           48 :                 values[13] = PointerGetDatum(tuple_data_bytea);
     265              :             }
     266              :             else
     267              :             {
     268            0 :                 nulls[11] = true;
     269            0 :                 nulls[12] = true;
     270            0 :                 nulls[13] = true;
     271              :             }
     272              :         }
     273              :         else
     274              :         {
     275              :             /*
     276              :              * The line pointer is not used, or it's invalid. Set the rest of
     277              :              * the fields to NULL
     278              :              */
     279              :             int         i;
     280              : 
     281           55 :             for (i = 4; i <= 13; i++)
     282           50 :                 nulls[i] = true;
     283              :         }
     284              : 
     285              :         /* Build and return the result tuple. */
     286           53 :         resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
     287           53 :         result = HeapTupleGetDatum(resultTuple);
     288              : 
     289           53 :         inter_call_data->offset++;
     290              : 
     291           53 :         SRF_RETURN_NEXT(fctx, result);
     292              :     }
     293              :     else
     294           10 :         SRF_RETURN_DONE(fctx);
     295              : }
     296              : 
     297              : /*
     298              :  * tuple_data_split_internal
     299              :  *
     300              :  * Split raw tuple data taken directly from a page into an array of bytea
     301              :  * elements. This routine does a lookup on NULL values and creates array
     302              :  * elements accordingly. This is a reimplementation of nocachegetattr()
     303              :  * in heaptuple.c simplified for educational purposes.
     304              :  */
     305              : static Datum
     306            5 : tuple_data_split_internal(Oid relid, char *tupdata,
     307              :                           uint16 tupdata_len, uint16 t_infomask,
     308              :                           uint16 t_infomask2, uint8 *t_bits,
     309              :                           bool do_detoast)
     310              : {
     311              :     ArrayBuildState *raw_attrs;
     312              :     int         nattrs;
     313              :     int         i;
     314            5 :     int         off = 0;
     315              :     Relation    rel;
     316              :     TupleDesc   tupdesc;
     317              : 
     318              :     /* Get tuple descriptor from relation OID */
     319            5 :     rel = relation_open(relid, AccessShareLock);
     320            5 :     tupdesc = RelationGetDescr(rel);
     321              : 
     322            5 :     raw_attrs = initArrayResult(BYTEAOID, CurrentMemoryContext, false);
     323            5 :     nattrs = tupdesc->natts;
     324              : 
     325              :     /*
     326              :      * Sequences always use heap AM, but they don't show that in the catalogs.
     327              :      */
     328            5 :     if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
     329            4 :         rel->rd_rel->relam != HEAP_TABLE_AM_OID)
     330            0 :         ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     331              :                         errmsg("only heap AM is supported")));
     332              : 
     333            5 :     if (nattrs < (t_infomask2 & HEAP_NATTS_MASK))
     334            0 :         ereport(ERROR,
     335              :                 (errcode(ERRCODE_DATA_CORRUPTED),
     336              :                  errmsg("number of attributes in tuple header is greater than number of attributes in tuple descriptor")));
     337              : 
     338           22 :     for (i = 0; i < nattrs; i++)
     339              :     {
     340              :         CompactAttribute *attr;
     341              :         bool        is_null;
     342           17 :         bytea      *attr_data = NULL;
     343              : 
     344           17 :         attr = TupleDescCompactAttr(tupdesc, i);
     345              : 
     346              :         /*
     347              :          * Tuple header can specify fewer attributes than tuple descriptor as
     348              :          * ALTER TABLE ADD COLUMN without DEFAULT keyword does not actually
     349              :          * change tuples in pages, so attributes with numbers greater than
     350              :          * (t_infomask2 & HEAP_NATTS_MASK) should be treated as NULL.
     351              :          */
     352           17 :         if (i >= (t_infomask2 & HEAP_NATTS_MASK))
     353            0 :             is_null = true;
     354              :         else
     355           17 :             is_null = (t_infomask & HEAP_HASNULL) && att_isnull(i, t_bits);
     356              : 
     357           17 :         if (!is_null)
     358              :         {
     359              :             int         len;
     360              : 
     361           10 :             if (attr->attlen == -1)
     362              :             {
     363            0 :                 off = att_pointer_alignby(off, attr->attalignby, -1,
     364              :                                           tupdata + off);
     365              : 
     366              :                 /*
     367              :                  * As VARSIZE_ANY throws an exception if it can't properly
     368              :                  * detect the type of external storage in macros VARTAG_SIZE,
     369              :                  * this check is repeated to have a nicer error handling.
     370              :                  */
     371            0 :                 if (VARATT_IS_EXTERNAL(tupdata + off) &&
     372            0 :                     !VARATT_IS_EXTERNAL_ONDISK(tupdata + off) &&
     373            0 :                     !VARATT_IS_EXTERNAL_INDIRECT(tupdata + off))
     374            0 :                     ereport(ERROR,
     375              :                             (errcode(ERRCODE_DATA_CORRUPTED),
     376              :                              errmsg("first byte of varlena attribute is incorrect for attribute %d", i)));
     377              : 
     378            0 :                 len = VARSIZE_ANY(tupdata + off);
     379              :             }
     380              :             else
     381              :             {
     382           10 :                 off = att_nominal_alignby(off, attr->attalignby);
     383           10 :                 len = attr->attlen;
     384              :             }
     385              : 
     386           10 :             if (tupdata_len < off + len)
     387            0 :                 ereport(ERROR,
     388              :                         (errcode(ERRCODE_DATA_CORRUPTED),
     389              :                          errmsg("unexpected end of tuple data")));
     390              : 
     391           10 :             if (attr->attlen == -1 && do_detoast)
     392            0 :                 attr_data = pg_detoast_datum_copy((varlena *) (tupdata + off));
     393              :             else
     394              :             {
     395           10 :                 attr_data = (bytea *) palloc(len + VARHDRSZ);
     396           10 :                 SET_VARSIZE(attr_data, len + VARHDRSZ);
     397           10 :                 memcpy(VARDATA(attr_data), tupdata + off, len);
     398              :             }
     399              : 
     400           10 :             off = att_addlength_pointer(off, attr->attlen,
     401              :                                         tupdata + off);
     402              :         }
     403              : 
     404           17 :         raw_attrs = accumArrayResult(raw_attrs, PointerGetDatum(attr_data),
     405              :                                      is_null, BYTEAOID, CurrentMemoryContext);
     406           17 :         if (attr_data)
     407           10 :             pfree(attr_data);
     408              :     }
     409              : 
     410            5 :     if (tupdata_len != off)
     411            0 :         ereport(ERROR,
     412              :                 (errcode(ERRCODE_DATA_CORRUPTED),
     413              :                  errmsg("end of tuple reached without looking at all its data")));
     414              : 
     415            5 :     relation_close(rel, AccessShareLock);
     416              : 
     417            5 :     return makeArrayResult(raw_attrs, CurrentMemoryContext);
     418              : }
     419              : 
     420              : /*
     421              :  * tuple_data_split
     422              :  *
     423              :  * Split raw tuple data taken directly from page into distinct elements
     424              :  * taking into account null values.
     425              :  */
     426           15 : PG_FUNCTION_INFO_V1(tuple_data_split);
     427              : 
     428              : Datum
     429            5 : tuple_data_split(PG_FUNCTION_ARGS)
     430              : {
     431              :     Oid         relid;
     432              :     bytea      *raw_data;
     433              :     uint16      t_infomask;
     434              :     uint16      t_infomask2;
     435              :     char       *t_bits_str;
     436            5 :     bool        do_detoast = false;
     437            5 :     uint8      *t_bits = NULL;
     438              :     Datum       res;
     439              : 
     440            5 :     relid = PG_GETARG_OID(0);
     441            5 :     raw_data = PG_ARGISNULL(1) ? NULL : PG_GETARG_BYTEA_P(1);
     442            5 :     t_infomask = PG_GETARG_INT16(2);
     443            5 :     t_infomask2 = PG_GETARG_INT16(3);
     444            5 :     t_bits_str = PG_ARGISNULL(4) ? NULL :
     445            2 :         text_to_cstring(PG_GETARG_TEXT_PP(4));
     446              : 
     447            5 :     if (PG_NARGS() >= 6)
     448            0 :         do_detoast = PG_GETARG_BOOL(5);
     449              : 
     450            5 :     if (!superuser())
     451            0 :         ereport(ERROR,
     452              :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     453              :                  errmsg("must be superuser to use raw page functions")));
     454              : 
     455            5 :     if (!raw_data)
     456            0 :         PG_RETURN_NULL();
     457              : 
     458              :     /*
     459              :      * Convert t_bits string back to the uint8 array as represented in the
     460              :      * tuple header.
     461              :      */
     462            5 :     if (t_infomask & HEAP_HASNULL)
     463              :     {
     464              :         size_t      bits_str_len;
     465              :         size_t      bits_len;
     466              : 
     467            2 :         bits_len = BITMAPLEN(t_infomask2 & HEAP_NATTS_MASK) * BITS_PER_BYTE;
     468            2 :         if (!t_bits_str)
     469            0 :             ereport(ERROR,
     470              :                     (errcode(ERRCODE_DATA_CORRUPTED),
     471              :                      errmsg("t_bits string must not be NULL")));
     472              : 
     473            2 :         bits_str_len = strlen(t_bits_str);
     474            2 :         if (bits_len != bits_str_len)
     475            0 :             ereport(ERROR,
     476              :                     (errcode(ERRCODE_DATA_CORRUPTED),
     477              :                      errmsg("unexpected length of t_bits string: %zu, expected %zu",
     478              :                             bits_str_len, bits_len)));
     479              : 
     480              :         /* do the conversion */
     481            2 :         t_bits = text_to_bits(t_bits_str, bits_str_len);
     482              :     }
     483              :     else
     484              :     {
     485            3 :         if (t_bits_str)
     486            0 :             ereport(ERROR,
     487              :                     (errcode(ERRCODE_DATA_CORRUPTED),
     488              :                      errmsg("t_bits string is expected to be NULL, but instead it is %zu bytes long",
     489              :                             strlen(t_bits_str))));
     490              :     }
     491              : 
     492              :     /* Split tuple data */
     493           10 :     res = tuple_data_split_internal(relid, (char *) raw_data + VARHDRSZ,
     494            5 :                                     VARSIZE(raw_data) - VARHDRSZ,
     495              :                                     t_infomask, t_infomask2, t_bits,
     496              :                                     do_detoast);
     497              : 
     498            5 :     if (t_bits)
     499            2 :         pfree(t_bits);
     500              : 
     501            5 :     PG_RETURN_DATUM(res);
     502              : }
     503              : 
     504              : /*
     505              :  * heap_tuple_infomask_flags
     506              :  *
     507              :  * Decode into a human-readable format t_infomask and t_infomask2 associated
     508              :  * to a tuple.  All the flags are described in access/htup_details.h.
     509              :  */
     510            8 : PG_FUNCTION_INFO_V1(heap_tuple_infomask_flags);
     511              : 
     512              : Datum
     513           11 : heap_tuple_infomask_flags(PG_FUNCTION_ARGS)
     514              : {
     515              : #define HEAP_TUPLE_INFOMASK_COLS 2
     516           11 :     Datum       values[HEAP_TUPLE_INFOMASK_COLS] = {0};
     517           11 :     bool        nulls[HEAP_TUPLE_INFOMASK_COLS] = {0};
     518           11 :     uint16      t_infomask = PG_GETARG_INT16(0);
     519           11 :     uint16      t_infomask2 = PG_GETARG_INT16(1);
     520           11 :     int         cnt = 0;
     521              :     ArrayType  *a;
     522              :     int         bitcnt;
     523              :     Datum      *flags;
     524              :     TupleDesc   tupdesc;
     525              :     HeapTuple   tuple;
     526              : 
     527           11 :     if (!superuser())
     528            0 :         ereport(ERROR,
     529              :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     530              :                  errmsg("must be superuser to use raw page functions")));
     531              : 
     532              :     /* Build a tuple descriptor for our result type */
     533           11 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     534            0 :         elog(ERROR, "return type must be a row type");
     535              : 
     536           11 :     bitcnt = pg_popcount((const char *) &t_infomask, sizeof(uint16)) +
     537           11 :         pg_popcount((const char *) &t_infomask2, sizeof(uint16));
     538              : 
     539              :     /* If no flags, return a set of empty arrays */
     540           11 :     if (bitcnt <= 0)
     541              :     {
     542            1 :         values[0] = PointerGetDatum(construct_empty_array(TEXTOID));
     543            1 :         values[1] = PointerGetDatum(construct_empty_array(TEXTOID));
     544            1 :         tuple = heap_form_tuple(tupdesc, values, nulls);
     545            1 :         PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
     546              :     }
     547              : 
     548              :     /* build set of raw flags */
     549           10 :     flags = palloc0_array(Datum, bitcnt);
     550              : 
     551              :     /* decode t_infomask */
     552           10 :     if ((t_infomask & HEAP_HASNULL) != 0)
     553            3 :         flags[cnt++] = CStringGetTextDatum("HEAP_HASNULL");
     554           10 :     if ((t_infomask & HEAP_HASVARWIDTH) != 0)
     555            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_HASVARWIDTH");
     556           10 :     if ((t_infomask & HEAP_HASEXTERNAL) != 0)
     557            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_HASEXTERNAL");
     558           10 :     if ((t_infomask & HEAP_HASOID_OLD) != 0)
     559            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_HASOID_OLD");
     560           10 :     if ((t_infomask & HEAP_XMAX_KEYSHR_LOCK) != 0)
     561            4 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_KEYSHR_LOCK");
     562           10 :     if ((t_infomask & HEAP_COMBOCID) != 0)
     563            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_COMBOCID");
     564           10 :     if ((t_infomask & HEAP_XMAX_EXCL_LOCK) != 0)
     565            3 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_EXCL_LOCK");
     566           10 :     if ((t_infomask & HEAP_XMAX_LOCK_ONLY) != 0)
     567            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_LOCK_ONLY");
     568           10 :     if ((t_infomask & HEAP_XMIN_COMMITTED) != 0)
     569            4 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_COMMITTED");
     570           10 :     if ((t_infomask & HEAP_XMIN_INVALID) != 0)
     571            4 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_INVALID");
     572           10 :     if ((t_infomask & HEAP_XMAX_COMMITTED) != 0)
     573            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_COMMITTED");
     574           10 :     if ((t_infomask & HEAP_XMAX_INVALID) != 0)
     575            5 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_INVALID");
     576           10 :     if ((t_infomask & HEAP_XMAX_IS_MULTI) != 0)
     577            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_IS_MULTI");
     578           10 :     if ((t_infomask & HEAP_UPDATED) != 0)
     579            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_UPDATED");
     580           10 :     if ((t_infomask & HEAP_MOVED_OFF) != 0)
     581            4 :         flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_OFF");
     582           10 :     if ((t_infomask & HEAP_MOVED_IN) != 0)
     583            4 :         flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_IN");
     584              : 
     585              :     /* decode t_infomask2 */
     586           10 :     if ((t_infomask2 & HEAP_KEYS_UPDATED) != 0)
     587            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_KEYS_UPDATED");
     588           10 :     if ((t_infomask2 & HEAP_HOT_UPDATED) != 0)
     589            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_HOT_UPDATED");
     590           10 :     if ((t_infomask2 & HEAP_ONLY_TUPLE) != 0)
     591            2 :         flags[cnt++] = CStringGetTextDatum("HEAP_ONLY_TUPLE");
     592              : 
     593              :     /* build value */
     594              :     Assert(cnt <= bitcnt);
     595           10 :     a = construct_array_builtin(flags, cnt, TEXTOID);
     596           10 :     values[0] = PointerGetDatum(a);
     597              : 
     598              :     /*
     599              :      * Build set of combined flags.  Use the same array as previously, this
     600              :      * keeps the code simple.
     601              :      */
     602           10 :     cnt = 0;
     603           92 :     MemSet(flags, 0, sizeof(Datum) * bitcnt);
     604              : 
     605              :     /* decode combined masks of t_infomask */
     606           10 :     if ((t_infomask & HEAP_XMAX_SHR_LOCK) == HEAP_XMAX_SHR_LOCK)
     607            3 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_SHR_LOCK");
     608           10 :     if ((t_infomask & HEAP_XMIN_FROZEN) == HEAP_XMIN_FROZEN)
     609            4 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_FROZEN");
     610           10 :     if ((t_infomask & HEAP_MOVED) == HEAP_MOVED)
     611            4 :         flags[cnt++] = CStringGetTextDatum("HEAP_MOVED");
     612              : 
     613              :     /* Build an empty array if there are no combined flags */
     614           10 :     if (cnt == 0)
     615            3 :         a = construct_empty_array(TEXTOID);
     616              :     else
     617            7 :         a = construct_array_builtin(flags, cnt, TEXTOID);
     618           10 :     pfree(flags);
     619           10 :     values[1] = PointerGetDatum(a);
     620              : 
     621              :     /* Returns the record as Datum */
     622           10 :     tuple = heap_form_tuple(tupdesc, values, nulls);
     623           10 :     PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
     624              : }
        

Generated by: LCOV version 2.0-1