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

Generated by: LCOV version 2.4-beta