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

Generated by: LCOV version 1.14