LCOV - differential code coverage report
Current view: top level - contrib/pageinspect - heapfuncs.c (source / functions) Coverage Total Hit UNC UBC GNC CBC EUB DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 87.0 % 247 215 3 29 4 211 4 6
Current Date: 2026-03-14 14:10:32 -0400 Functions: 90.0 % 10 9 1 3 6
Baseline: lcov-20260315-024220-baseline Branches: 61.4 % 202 124 1 77 1 123 24 4 2
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 2 2 2
(30,360] days: 72.2 % 18 13 3 2 2 11
(360..) days: 88.1 % 227 200 27 200
Function coverage date bins:
(360..) days: 90.0 % 10 9 1 3 6
Branch coverage date bins:
(1,7] days: 50.0 % 2 1 1 1
(30,360] days: 50.0 % 4 2 2 2
(360..) days: 55.0 % 220 121 75 121 24

 Age         Owner                    Branch data    TLA  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
  416 peter@eisentraut.org       47                 :UBC           0 : HeapTupleHeaderGetOidOld(const HeapTupleHeaderData *tup)
                                 48                 :                : {
                                 49         [ #  # ]:              0 :     if (tup->t_infomask & HEAP_HASOID_OLD)
   48 peter@eisentraut.org       50                 :UNC           0 :         return *((const Oid *) ((const char *) (tup) + (tup)->t_hoff - sizeof(Oid)));
                                 51                 :                :     else
  416 peter@eisentraut.org       52                 :UBC           0 :         return InvalidOid;
                                 53                 :                : }
                                 54                 :                : 
                                 55                 :                : 
                                 56                 :                : /*
                                 57                 :                :  * bits_to_text
                                 58                 :                :  *
                                 59                 :                :  * Converts a bits8-array of 'len' bits to a human-readable
                                 60                 :                :  * c-string representation.
                                 61                 :                :  */
                                 62                 :                : static char *
 6877 bruce@momjian.us           63                 :CBC           4 : bits_to_text(bits8 *bits, int len)
                                 64                 :                : {
                                 65                 :                :     int         i;
                                 66                 :                :     char       *str;
                                 67                 :                : 
                                 68                 :              4 :     str = palloc(len + 1);
                                 69                 :                : 
 6695                            70         [ +  + ]:             36 :     for (i = 0; i < len; i++)
 6877                            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 bits8-array. This is
                                 83                 :                :  * the reverse operation of previous routine.
                                 84                 :                :  */
                                 85                 :                : static bits8 *
 3763 teodor@sigaev.ru           86                 :              2 : text_to_bits(char *str, int len)
                                 87                 :                : {
                                 88                 :                :     bits8      *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
 3763 teodor@sigaev.ru          102         [ #  # ]:UBC           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                 :                : 
 3763 teodor@sigaev.ru          107         [ +  + ]:CBC          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                 :                :  */
 6877 bruce@momjian.us          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                 :                : {
 6695                           133                 :             63 :     bytea      *raw_page = PG_GETARG_BYTEA_P(0);
 6877                           134                 :             63 :     heap_page_items_state *inter_call_data = NULL;
                                135                 :                :     FuncCallContext *fctx;
                                136                 :                : 
                                137         [ -  + ]:             63 :     if (!superuser())
 6877 bruce@momjian.us          138         [ #  # ]:UBC           0 :         ereport(ERROR,
                                139                 :                :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
                                140                 :                :                  errmsg("must be superuser to use raw page functions")));
                                141                 :                : 
 6877 bruce@momjian.us          142         [ +  + ]:CBC          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                 :                : 
  100 michael@paquier.xyz       150                 :GNC          10 :         inter_call_data = palloc_object(heap_page_items_state);
                                151                 :                : 
                                152                 :                :         /* Build a tuple descriptor for our result type */
 6877 bruce@momjian.us          153         [ -  + ]:CBC          10 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 6877 bruce@momjian.us          154         [ #  # ]:UBC           0 :             elog(ERROR, "return type must be a row type");
                                155                 :                : 
 6877 bruce@momjian.us          156                 :CBC          10 :         inter_call_data->tupd = tupdesc;
                                157                 :                : 
                                158                 :             10 :         inter_call_data->offset = FirstOffsetNumber;
    4 tomas.vondra@postgre      159                 :GNC          10 :         inter_call_data->page = get_page_from_raw(raw_page);
                                160                 :                : 
 6877 bruce@momjian.us          161                 :CBC          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                 :                : 
 6695                           188                 :             53 :         lp_offset = ItemIdGetOffset(id);
                                189                 :             53 :         lp_flags = ItemIdGetFlags(id);
                                190                 :             53 :         lp_len = ItemIdGetLength(id);
                                191                 :                : 
 6877                           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                 :                :          */
 6759 tgl@sss.pgh.pa.us         202   [ +  +  +  - ]:             53 :         if (ItemIdHasStorage(id) &&
 4040                           203                 :             48 :             lp_len >= MinHeapTupleSize &&
 6759                           204         [ +  - ]:             48 :             lp_offset == MAXALIGN(lp_offset) &&
    4 tomas.vondra@postgre      205         [ +  - ]:GNC          48 :             lp_offset + lp_len <= BLCKSZ)
 6877 bruce@momjian.us          206                 :CBC          48 :         {
                                207                 :                :             HeapTupleHeader tuphdr;
                                208                 :                : 
                                209                 :                :             /* Extract information from the tuple header */
                                210                 :             48 :             tuphdr = (HeapTupleHeader) PageGetItem(page, id);
                                211                 :                : 
 4466 rhaas@postgresql.org      212                 :             48 :             values[4] = UInt32GetDatum(HeapTupleHeaderGetRawXmin(tuphdr));
 4799 alvherre@alvh.no-ip.      213                 :             48 :             values[5] = UInt32GetDatum(HeapTupleHeaderGetRawXmax(tuphdr));
                                214                 :                :             /* shared with xvac */
 3763 teodor@sigaev.ru          215                 :             48 :             values[6] = UInt32GetDatum(HeapTupleHeaderGetRawCommandId(tuphdr));
 6877 bruce@momjian.us          216                 :             48 :             values[7] = PointerGetDatum(&tuphdr->t_ctid);
 5497 alvherre@alvh.no-ip.      217                 :             48 :             values[8] = UInt32GetDatum(tuphdr->t_infomask2);
                                218                 :             48 :             values[9] = UInt32GetDatum(tuphdr->t_infomask);
 6877 bruce@momjian.us          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                 :                :              */
 4040 tgl@sss.pgh.pa.us         227         [ +  - ]:             48 :             if (tuphdr->t_hoff >= SizeofHeapTupleHeader &&
                                228         [ +  - ]:             48 :                 tuphdr->t_hoff <= lp_len &&
                                229         [ +  - ]:             48 :                 tuphdr->t_hoff == MAXALIGN(tuphdr->t_hoff))
 6877 bruce@momjian.us          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                 :                : 
  330 tgl@sss.pgh.pa.us         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
  330 tgl@sss.pgh.pa.us         246                 :UBC           0 :                         nulls[11] = true;
                                247                 :                :                 }
                                248                 :                :                 else
 6877 bruce@momjian.us          249                 :CBC          44 :                     nulls[11] = true;
                                250                 :                : 
 2672 andres@anarazel.de        251         [ -  + ]:             48 :                 if (tuphdr->t_infomask & HEAP_HASOID_OLD)
  219 peter@eisentraut.org      252                 :UNC           0 :                     values[12] = ObjectIdGetDatum(HeapTupleHeaderGetOidOld(tuphdr));
                                253                 :                :                 else
 6877 bruce@momjian.us          254                 :CBC          48 :                     nulls[12] = true;
                                255                 :                : 
                                256                 :                :                 /* Copy raw tuple data into bytea attribute */
  330 tgl@sss.pgh.pa.us         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                 :                :             {
 6877 bruce@momjian.us          268                 :UBC           0 :                 nulls[11] = true;
                                269                 :              0 :                 nulls[12] = true;
  330 tgl@sss.pgh.pa.us         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                 :                : 
 3763 teodor@sigaev.ru          281         [ +  + ]:CBC          55 :             for (i = 4; i <= 13; i++)
 6877 bruce@momjian.us          282                 :             50 :                 nulls[i] = true;
                                283                 :                :         }
                                284                 :                : 
                                285                 :                :         /* Build and return the result tuple. */
 6695                           286                 :             53 :         resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
                                287                 :             53 :         result = HeapTupleGetDatum(resultTuple);
                                288                 :                : 
 6877                           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
 3763 teodor@sigaev.ru          306                 :              5 : tuple_data_split_internal(Oid relid, char *tupdata,
                                307                 :                :                           uint16 tupdata_len, uint16 t_infomask,
                                308                 :                :                           uint16 t_infomask2, bits8 *t_bits,
                                309                 :                :                           bool do_detoast)
                                310                 :                : {
                                311                 :                :     ArrayBuildState *raw_attrs;
                                312                 :                :     int         nattrs;
                                313                 :                :     int         i;
 3566 rhaas@postgresql.org      314                 :              5 :     int         off = 0;
                                315                 :                :     Relation    rel;
                                316                 :                :     TupleDesc   tupdesc;
                                317                 :                : 
                                318                 :                :     /* Get tuple descriptor from relation OID */
 2722 tgl@sss.pgh.pa.us         319                 :              5 :     rel = relation_open(relid, AccessShareLock);
                                320                 :              5 :     tupdesc = RelationGetDescr(rel);
                                321                 :                : 
 3763 teodor@sigaev.ru          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                 :                :      */
  549 nathan@postgresql.or      328         [ +  + ]:              5 :     if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
                                329         [ -  + ]:              4 :         rel->rd_rel->relam != HEAP_TABLE_AM_OID)
 2540 andres@anarazel.de        330         [ #  # ]:UBC           0 :         ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                331                 :                :                         errmsg("only heap AM is supported")));
                                332                 :                : 
 3763 teodor@sigaev.ru          333         [ -  + ]:CBC           5 :     if (nattrs < (t_infomask2 & HEAP_NATTS_MASK))
 3763 teodor@sigaev.ru          334         [ #  # ]:UBC           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                 :                : 
 3763 teodor@sigaev.ru          338         [ +  + ]:CBC          22 :     for (i = 0; i < nattrs; i++)
                                339                 :                :     {
                                340                 :                :         CompactAttribute *attr;
                                341                 :                :         bool        is_null;
 3566 rhaas@postgresql.org      342                 :             17 :         bytea      *attr_data = NULL;
                                343                 :                : 
  450 drowley@postgresql.o      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                 :                :          */
 3763 teodor@sigaev.ru          352         [ -  + ]:             17 :         if (i >= (t_infomask2 & HEAP_NATTS_MASK))
 3763 teodor@sigaev.ru          353                 :UBC           0 :             is_null = true;
                                354                 :                :         else
 2722 tgl@sss.pgh.pa.us         355   [ +  +  +  + ]:CBC          17 :             is_null = (t_infomask & HEAP_HASNULL) && att_isnull(i, t_bits);
                                356                 :                : 
 3763 teodor@sigaev.ru          357         [ +  + ]:             17 :         if (!is_null)
                                358                 :                :         {
                                359                 :                :             int         len;
                                360                 :                : 
                                361         [ -  + ]:             10 :             if (attr->attlen == -1)
                                362                 :                :             {
  449 drowley@postgresql.o      363         [ #  # ]:UBC           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                 :                :                  */
 3763 teodor@sigaev.ru          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                 :                :             {
  449 drowley@postgresql.o      382                 :CBC          10 :                 off = att_nominal_alignby(off, attr->attalignby);
 3763 teodor@sigaev.ru          383                 :             10 :                 len = attr->attlen;
                                384                 :                :             }
                                385                 :                : 
                                386         [ -  + ]:             10 :             if (tupdata_len < off + len)
 3763 teodor@sigaev.ru          387         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                388                 :                :                         (errcode(ERRCODE_DATA_CORRUPTED),
                                389                 :                :                          errmsg("unexpected end of tuple data")));
                                390                 :                : 
 3763 teodor@sigaev.ru          391   [ -  +  -  - ]:CBC          10 :             if (attr->attlen == -1 && do_detoast)
   32 michael@paquier.xyz       392                 :UNC           0 :                 attr_data = pg_detoast_datum_copy((varlena *) (tupdata + off));
                                393                 :                :             else
                                394                 :                :             {
 3763 teodor@sigaev.ru          395                 :CBC          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                 :                : 
 3129 andres@anarazel.de        400   [ +  -  -  -  :             10 :             off = att_addlength_pointer(off, attr->attlen,
                                     -  -  -  -  -  
                                     -  -  -  -  -  
                                              -  - ]
                                401                 :                :                                         tupdata + off);
                                402                 :                :         }
                                403                 :                : 
 3763 teodor@sigaev.ru          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)
 3763 teodor@sigaev.ru          411         [ #  # ]:UBC           0 :         ereport(ERROR,
                                412                 :                :                 (errcode(ERRCODE_DATA_CORRUPTED),
                                413                 :                :                  errmsg("end of tuple reached without looking at all its data")));
                                414                 :                : 
 2722 tgl@sss.pgh.pa.us         415                 :CBC           5 :     relation_close(rel, AccessShareLock);
                                416                 :                : 
 3763 teodor@sigaev.ru          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;
 3566 rhaas@postgresql.org      436                 :              5 :     bool        do_detoast = false;
                                437                 :              5 :     bits8      *t_bits = NULL;
                                438                 :                :     Datum       res;
                                439                 :                : 
 3763 teodor@sigaev.ru          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)
 3763 teodor@sigaev.ru          448                 :UBC           0 :         do_detoast = PG_GETARG_BOOL(5);
                                449                 :                : 
 3763 teodor@sigaev.ru          450         [ -  + ]:CBC           5 :     if (!superuser())
 3763 teodor@sigaev.ru          451         [ #  # ]:UBC           0 :         ereport(ERROR,
                                452                 :                :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
                                453                 :                :                  errmsg("must be superuser to use raw page functions")));
                                454                 :                : 
 3763 teodor@sigaev.ru          455         [ -  + ]:CBC           5 :     if (!raw_data)
 3763 teodor@sigaev.ru          456                 :UBC           0 :         PG_RETURN_NULL();
                                457                 :                : 
                                458                 :                :     /*
                                459                 :                :      * Convert t_bits string back to the bits8 array as represented in the
                                460                 :                :      * tuple header.
                                461                 :                :      */
 3763 teodor@sigaev.ru          462         [ +  + ]:CBC           5 :     if (t_infomask & HEAP_HASNULL)
                                463                 :                :     {
                                464                 :                :         size_t      bits_str_len;
                                465                 :                :         size_t      bits_len;
                                466                 :                : 
 2992 tgl@sss.pgh.pa.us         467                 :              2 :         bits_len = BITMAPLEN(t_infomask2 & HEAP_NATTS_MASK) * BITS_PER_BYTE;
 3763 teodor@sigaev.ru          468         [ -  + ]:              2 :         if (!t_bits_str)
 3763 teodor@sigaev.ru          469         [ #  # ]:UBC           0 :             ereport(ERROR,
                                470                 :                :                     (errcode(ERRCODE_DATA_CORRUPTED),
                                471                 :                :                      errmsg("t_bits string must not be NULL")));
                                472                 :                : 
 3763 teodor@sigaev.ru          473                 :CBC           2 :         bits_str_len = strlen(t_bits_str);
 2992 tgl@sss.pgh.pa.us         474         [ -  + ]:              2 :         if (bits_len != bits_str_len)
 3763 teodor@sigaev.ru          475         [ #  # ]:UBC           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 */
 3763 teodor@sigaev.ru          481                 :CBC           2 :         t_bits = text_to_bits(t_bits_str, bits_str_len);
                                482                 :                :     }
                                483                 :                :     else
                                484                 :                :     {
                                485         [ -  + ]:              3 :         if (t_bits_str)
 3763 teodor@sigaev.ru          486         [ #  # ]:UBC           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 */
 3763 teodor@sigaev.ru          493                 :CBC          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                 :                : 
 1295 peter@eisentraut.org      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                 :                :  */
 2376 michael@paquier.xyz       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
 1338 peter@eisentraut.org      516                 :             11 :     Datum       values[HEAP_TUPLE_INFOMASK_COLS] = {0};
                                517                 :             11 :     bool        nulls[HEAP_TUPLE_INFOMASK_COLS] = {0};
 2376 michael@paquier.xyz       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())
 2376 michael@paquier.xyz       528         [ #  # ]:UBC           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 */
 2369 michael@paquier.xyz       533         [ -  + ]:CBC          11 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 2369 michael@paquier.xyz       534         [ #  # ]:UBC           0 :         elog(ERROR, "return type must be a row type");
                                535                 :                : 
 2376 michael@paquier.xyz       536                 :CBC          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                 :                :     {
 2369                           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 */
  100 michael@paquier.xyz       549                 :GNC          10 :     flags = palloc0_array(Datum, bitcnt);
                                550                 :                : 
                                551                 :                :     /* decode t_infomask */
 2376 michael@paquier.xyz       552         [ +  + ]:CBC          10 :     if ((t_infomask & HEAP_HASNULL) != 0)
 2369                           553                 :              3 :         flags[cnt++] = CStringGetTextDatum("HEAP_HASNULL");
 2376                           554         [ +  + ]:             10 :     if ((t_infomask & HEAP_HASVARWIDTH) != 0)
 2369                           555                 :              2 :         flags[cnt++] = CStringGetTextDatum("HEAP_HASVARWIDTH");
 2376                           556         [ +  + ]:             10 :     if ((t_infomask & HEAP_HASEXTERNAL) != 0)
 2369                           557                 :              2 :         flags[cnt++] = CStringGetTextDatum("HEAP_HASEXTERNAL");
 2376                           558         [ +  + ]:             10 :     if ((t_infomask & HEAP_HASOID_OLD) != 0)
 2369                           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");
 2376                           562         [ +  + ]:             10 :     if ((t_infomask & HEAP_COMBOCID) != 0)
 2369                           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");
 2376                           572         [ +  + ]:             10 :     if ((t_infomask & HEAP_XMAX_COMMITTED) != 0)
 2369                           573                 :              2 :         flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_COMMITTED");
 2376                           574         [ +  + ]:             10 :     if ((t_infomask & HEAP_XMAX_INVALID) != 0)
 2369                           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");
 2376                           578         [ +  + ]:             10 :     if ((t_infomask & HEAP_UPDATED) != 0)
 2369                           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 */
 2376                           586         [ +  + ]:             10 :     if ((t_infomask2 & HEAP_KEYS_UPDATED) != 0)
 2369                           587                 :              2 :         flags[cnt++] = CStringGetTextDatum("HEAP_KEYS_UPDATED");
 2376                           588         [ +  + ]:             10 :     if ((t_infomask2 & HEAP_HOT_UPDATED) != 0)
 2369                           589                 :              2 :         flags[cnt++] = CStringGetTextDatum("HEAP_HOT_UPDATED");
 2376                           590         [ +  + ]:             10 :     if ((t_infomask2 & HEAP_ONLY_TUPLE) != 0)
 2369                           591                 :              2 :         flags[cnt++] = CStringGetTextDatum("HEAP_ONLY_TUPLE");
                                592                 :                : 
                                593                 :                :     /* build value */
 2376                           594         [ -  + ]:             10 :     Assert(cnt <= bitcnt);
 1353 peter@eisentraut.org      595                 :             10 :     a = construct_array_builtin(flags, cnt, TEXTOID);
 2369 michael@paquier.xyz       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
 1353 peter@eisentraut.org      617                 :              7 :         a = construct_array_builtin(flags, cnt, TEXTOID);
 2369 michael@paquier.xyz       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.4-beta