LCOV - code coverage report
Current view: top level - src/include - varatt.h (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 97.0 % 101 98
Test Date: 2026-11-24 13:16:08 Functions: 100.0 % 36 36
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 91.7 % 48 44

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * varatt.h
       4                 :             :  *    variable-length datatypes (TOAST support)
       5                 :             :  *
       6                 :             :  *
       7                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       8                 :             :  * Portions Copyright (c) 1995, Regents of the University of California
       9                 :             :  *
      10                 :             :  * src/include/varatt.h
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : 
      15                 :             : #ifndef VARATT_H
      16                 :             : #define VARATT_H
      17                 :             : 
      18                 :             : /*
      19                 :             :  * varatt_external_oid is a traditional "TOAST pointer", that is, the
      20                 :             :  * information needed to fetch a Datum stored out-of-line in a TOAST table.
      21                 :             :  * The data is compressed if and only if the external size stored in
      22                 :             :  * va_extinfo is less than va_rawsize - VARHDRSZ.
      23                 :             :  *
      24                 :             :  * This struct must not contain any padding, because we sometimes compare
      25                 :             :  * these pointers using memcmp.
      26                 :             :  *
      27                 :             :  * Note that this information is stored unaligned within actual tuples, so
      28                 :             :  * you need to memcpy from the tuple into a local struct variable before
      29                 :             :  * you can look at these fields!  (The reason we use memcmp is to avoid
      30                 :             :  * having to do that just to detect equality of two TOAST pointers...)
      31                 :             :  */
      32                 :             : typedef struct varatt_external_oid
      33                 :             : {
      34                 :             :     int32       va_rawsize;     /* Original data size (includes header) */
      35                 :             :     uint32      va_extinfo;     /* External saved size (without header) and
      36                 :             :                                  * compression method */
      37                 :             :     Oid         va_valueid;     /* Unique ID of value within TOAST table */
      38                 :             :     Oid         va_toastrelid;  /* RelID of TOAST table containing it */
      39                 :             : } varatt_external_oid;
      40                 :             : 
      41                 :             : StaticAssertDecl((sizeof(int32) + sizeof(uint32) + 2 * sizeof(Oid)) ==
      42                 :             :                  sizeof(varatt_external_oid),
      43                 :             :                  "varatt_external_oid should have no padding");
      44                 :             : 
      45                 :             : /*
      46                 :             :  * varatt_external_oid8 is a "TOAST pointer" for TOAST tables that use
      47                 :             :  * Oid8 (64-bit) as their chunk_id type.  Same layout as varatt_external_oid
      48                 :             :  * except for the value ID, which is 8 bytes wide.  The value ID is split
      49                 :             :  * into two uint32 to force alignment.
      50                 :             :  *
      51                 :             :  * This struct must not contain any padding, because we sometimes compare
      52                 :             :  * these pointers using memcmp.
      53                 :             :  */
      54                 :             : typedef struct varatt_external_oid8
      55                 :             : {
      56                 :             :     int32       va_rawsize;     /* Original data size (includes header) */
      57                 :             :     uint32      va_extinfo;     /* External saved size (without header) and
      58                 :             :                                  * compression method */
      59                 :             :     uint32      va_valueid_lo;  /* Low 32 bits of value ID */
      60                 :             :     uint32      va_valueid_hi;  /* High 32 bits of value ID */
      61                 :             :     Oid         va_toastrelid;  /* RelID of TOAST table containing it */
      62                 :             : } varatt_external_oid8;
      63                 :             : 
      64                 :             : StaticAssertDecl((sizeof(int32) + 3 * sizeof(uint32) + sizeof(Oid)) ==
      65                 :             :                  sizeof(varatt_external_oid8),
      66                 :             :                  "varatt_external_oid8 should have no padding");
      67                 :             : 
      68                 :             : /* Get/set the 64-bit value ID from the lo/hi halves */
      69                 :             : static inline Oid8
      70                 :         232 : VARATT_EXTERNAL_OID8_GET_VALUEID(varatt_external_oid8 toast_pointer)
      71                 :             : {
      72                 :         464 :     return ((Oid8) toast_pointer.va_valueid_lo) |
      73                 :         232 :         (((Oid8) toast_pointer.va_valueid_hi) << 32);
      74                 :             : }
      75                 :             : 
      76                 :             : static inline void
      77                 :          78 : VARATT_EXTERNAL_OID8_SET_VALUEID(varatt_external_oid8 *toast_pointer, Oid8 id)
      78                 :             : {
      79                 :          78 :     toast_pointer->va_valueid_lo = (uint32) id;
      80                 :          78 :     toast_pointer->va_valueid_hi = (uint32) (id >> 32);
      81                 :          78 : }
      82                 :             : 
      83                 :             : /*
      84                 :             :  * These macros define the "saved size" portion of va_extinfo.  This value has
      85                 :             :  * been chosen to avoid integer wraparound; see detoast_external_attr().  The
      86                 :             :  * remaining two high-order bits identify the compression method.
      87                 :             :  */
      88                 :             : #define VARLENA_EXTSIZE_BITS    30
      89                 :             : #define VARLENA_EXTSIZE_MASK    ((1U << VARLENA_EXTSIZE_BITS) - 1)
      90                 :             : 
      91                 :             : /*
      92                 :             :  * varatt_indirect is a "TOAST pointer" representing an out-of-line
      93                 :             :  * Datum that's stored in memory, not in an external toast relation.
      94                 :             :  * The creator of such a Datum is entirely responsible that the referenced
      95                 :             :  * storage survives for as long as referencing pointer Datums can exist.
      96                 :             :  *
      97                 :             :  * Note that just as for varatt_external_oid, this struct is stored
      98                 :             :  * unaligned within any containing tuple.
      99                 :             :  */
     100                 :             : typedef struct varatt_indirect
     101                 :             : {
     102                 :             :     varlena    *pointer;        /* Pointer to in-memory varlena */
     103                 :             : } varatt_indirect;
     104                 :             : 
     105                 :             : /*
     106                 :             :  * varatt_expanded is a "TOAST pointer" representing an out-of-line
     107                 :             :  * Datum that is stored in memory, in some type-specific, not necessarily
     108                 :             :  * physically contiguous format that is convenient for computation not
     109                 :             :  * storage.  APIs for this, in particular the definition of struct
     110                 :             :  * ExpandedObjectHeader, are in src/include/utils/expandeddatum.h.
     111                 :             :  *
     112                 :             :  * Note that just as for varatt_external_oid, this struct is stored
     113                 :             :  * unaligned within any containing tuple.
     114                 :             :  */
     115                 :             : typedef struct ExpandedObjectHeader ExpandedObjectHeader;
     116                 :             : 
     117                 :             : typedef struct varatt_expanded
     118                 :             : {
     119                 :             :     ExpandedObjectHeader *eohptr;
     120                 :             : } varatt_expanded;
     121                 :             : 
     122                 :             : /*
     123                 :             :  * Type tag for the various sorts of "TOAST pointer" datums.  The peculiar
     124                 :             :  * value for VARTAG_ONDISK_OID comes from a requirement for on-disk
     125                 :             :  * compatibility with a previous notion that the tag field was the pointer
     126                 :             :  * datum's length.
     127                 :             :  */
     128                 :             : typedef enum vartag_external
     129                 :             : {
     130                 :             :     VARTAG_INDIRECT = 1,
     131                 :             :     VARTAG_EXPANDED_RO = 2,
     132                 :             :     VARTAG_EXPANDED_RW = 3,
     133                 :             :     VARTAG_ONDISK_OID8 = 4,
     134                 :             :     VARTAG_ONDISK_OID = 18
     135                 :             : } vartag_external;
     136                 :             : 
     137                 :             : /* Is a TOAST pointer either type of expanded-object pointer? */
     138                 :             : /* this test relies on the specific tag values above */
     139                 :             : static inline bool
     140                 :      265896 : VARTAG_IS_EXPANDED(vartag_external tag)
     141                 :             : {
     142                 :      265896 :     return ((tag & ~1) == VARTAG_EXPANDED_RO);
     143                 :             : }
     144                 :             : 
     145                 :             : /* Size of the data part of a "TOAST pointer" datum */
     146                 :             : static inline Size
     147                 :      146044 : VARTAG_SIZE(vartag_external tag)
     148                 :             : {
     149         [ +  + ]:      146044 :     if (tag == VARTAG_INDIRECT)
     150                 :        1035 :         return sizeof(varatt_indirect);
     151         [ -  + ]:      145009 :     else if (VARTAG_IS_EXPANDED(tag))
     152                 :           0 :         return sizeof(varatt_expanded);
     153         [ +  + ]:      145009 :     else if (tag == VARTAG_ONDISK_OID)
     154                 :      144208 :         return sizeof(varatt_external_oid);
     155         [ +  - ]:         801 :     else if (tag == VARTAG_ONDISK_OID8)
     156                 :         801 :         return sizeof(varatt_external_oid8);
     157                 :             :     else
     158                 :             :     {
     159                 :             :         Assert(false);
     160                 :           0 :         return 0;
     161                 :             :     }
     162                 :             : }
     163                 :             : 
     164                 :             : /*
     165                 :             :  * These structs describe the header of a varlena object that may have been
     166                 :             :  * TOASTed.  Generally, don't reference these structs directly, but use the
     167                 :             :  * functions and macros below.
     168                 :             :  *
     169                 :             :  * We use separate structs for the aligned and unaligned cases because the
     170                 :             :  * compiler might otherwise think it could generate code that assumes
     171                 :             :  * alignment while touching fields of a 1-byte-header varlena.
     172                 :             :  */
     173                 :             : typedef union
     174                 :             : {
     175                 :             :     struct                      /* Normal varlena (4-byte length) */
     176                 :             :     {
     177                 :             :         uint32      va_header;
     178                 :             :         char        va_data[FLEXIBLE_ARRAY_MEMBER];
     179                 :             :     }           va_4byte;
     180                 :             :     struct                      /* Compressed-in-line format */
     181                 :             :     {
     182                 :             :         uint32      va_header;
     183                 :             :         uint32      va_tcinfo;  /* Original data size (excludes header) and
     184                 :             :                                  * compression method; see va_extinfo */
     185                 :             :         char        va_data[FLEXIBLE_ARRAY_MEMBER]; /* Compressed data */
     186                 :             :     }           va_compressed;
     187                 :             : } varattrib_4b;
     188                 :             : 
     189                 :             : typedef struct
     190                 :             : {
     191                 :             :     uint8       va_header;
     192                 :             :     char        va_data[FLEXIBLE_ARRAY_MEMBER]; /* Data begins here */
     193                 :             : } varattrib_1b;
     194                 :             : 
     195                 :             : /* TOAST pointers are a subset of varattrib_1b with an identifying tag byte */
     196                 :             : typedef struct
     197                 :             : {
     198                 :             :     uint8       va_header;      /* Always 0x80 or 0x01 */
     199                 :             :     uint8       va_tag;         /* Type of datum */
     200                 :             :     char        va_data[FLEXIBLE_ARRAY_MEMBER]; /* Type-specific data */
     201                 :             : } varattrib_1b_e;
     202                 :             : 
     203                 :             : /*
     204                 :             :  * Bit layouts for varlena headers on big-endian machines:
     205                 :             :  *
     206                 :             :  * 00xxxxxx 4-byte length word, aligned, uncompressed data (up to 1G)
     207                 :             :  * 01xxxxxx 4-byte length word, aligned, *compressed* data (up to 1G)
     208                 :             :  * 10000000 1-byte length word, unaligned, TOAST pointer
     209                 :             :  * 1xxxxxxx 1-byte length word, unaligned, uncompressed data (up to 126b)
     210                 :             :  *
     211                 :             :  * Bit layouts for varlena headers on little-endian machines:
     212                 :             :  *
     213                 :             :  * xxxxxx00 4-byte length word, aligned, uncompressed data (up to 1G)
     214                 :             :  * xxxxxx10 4-byte length word, aligned, *compressed* data (up to 1G)
     215                 :             :  * 00000001 1-byte length word, unaligned, TOAST pointer
     216                 :             :  * xxxxxxx1 1-byte length word, unaligned, uncompressed data (up to 126b)
     217                 :             :  *
     218                 :             :  * The "xxx" bits are the length field (which includes itself in all cases).
     219                 :             :  * In the big-endian case we mask to extract the length, in the little-endian
     220                 :             :  * case we shift.  Note that in both cases the flag bits are in the physically
     221                 :             :  * first byte.  Also, it is not possible for a 1-byte length word to be zero;
     222                 :             :  * this lets us disambiguate alignment padding bytes from the start of an
     223                 :             :  * unaligned datum.  (We now *require* pad bytes to be filled with zero!)
     224                 :             :  *
     225                 :             :  * In TOAST pointers the va_tag field (see varattrib_1b_e) is used to discern
     226                 :             :  * the specific type and length of the pointer datum.
     227                 :             :  */
     228                 :             : 
     229                 :             : /*
     230                 :             :  * Endian-dependent macros.  These are considered internal --- use the
     231                 :             :  * external functions below instead of using these directly.  All of these
     232                 :             :  * expect an argument that is a pointer, not a Datum.  Some of them have
     233                 :             :  * multiple-evaluation hazards, too.
     234                 :             :  *
     235                 :             :  * Note: IS_1B is true for external toast records but VARSIZE_1B will return 0
     236                 :             :  * for such records. Hence you should usually check for IS_EXTERNAL before
     237                 :             :  * checking for IS_1B.
     238                 :             :  */
     239                 :             : 
     240                 :             : #ifdef WORDS_BIGENDIAN
     241                 :             : 
     242                 :             : #define VARATT_IS_4B(PTR) \
     243                 :             :     ((((const varattrib_1b *) (PTR))->va_header & 0x80) == 0x00)
     244                 :             : #define VARATT_IS_4B_U(PTR) \
     245                 :             :     ((((const varattrib_1b *) (PTR))->va_header & 0xC0) == 0x00)
     246                 :             : #define VARATT_IS_4B_C(PTR) \
     247                 :             :     ((((const varattrib_1b *) (PTR))->va_header & 0xC0) == 0x40)
     248                 :             : #define VARATT_IS_1B(PTR) \
     249                 :             :     ((((const varattrib_1b *) (PTR))->va_header & 0x80) == 0x80)
     250                 :             : #define VARATT_IS_1B_E(PTR) \
     251                 :             :     ((((const varattrib_1b *) (PTR))->va_header) == 0x80)
     252                 :             : #define VARATT_NOT_PAD_BYTE(PTR) \
     253                 :             :     (*((const uint8 *) (PTR)) != 0)
     254                 :             : 
     255                 :             : /* VARSIZE_4B() should only be used on known-aligned data */
     256                 :             : #define VARSIZE_4B(PTR) \
     257                 :             :     (((const varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
     258                 :             : #define VARSIZE_1B(PTR) \
     259                 :             :     (((const varattrib_1b *) (PTR))->va_header & 0x7F)
     260                 :             : #define VARTAG_1B_E(PTR) \
     261                 :             :     ((vartag_external) ((const varattrib_1b_e *) (PTR))->va_tag)
     262                 :             : 
     263                 :             : #define SET_VARSIZE_4B(PTR,len) \
     264                 :             :     (((varattrib_4b *) (PTR))->va_4byte.va_header = (len) & 0x3FFFFFFF)
     265                 :             : #define SET_VARSIZE_4B_C(PTR,len) \
     266                 :             :     (((varattrib_4b *) (PTR))->va_4byte.va_header = ((len) & 0x3FFFFFFF) | 0x40000000)
     267                 :             : #define SET_VARSIZE_1B(PTR,len) \
     268                 :             :     (((varattrib_1b *) (PTR))->va_header = (len) | 0x80)
     269                 :             : #define SET_VARTAG_1B_E(PTR,tag) \
     270                 :             :     (((varattrib_1b_e *) (PTR))->va_header = 0x80, \
     271                 :             :      ((varattrib_1b_e *) (PTR))->va_tag = (tag))
     272                 :             : 
     273                 :             : #else                           /* !WORDS_BIGENDIAN */
     274                 :             : 
     275                 :             : #define VARATT_IS_4B(PTR) \
     276                 :             :     ((((const varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
     277                 :             : #define VARATT_IS_4B_U(PTR) \
     278                 :             :     ((((const varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
     279                 :             : #define VARATT_IS_4B_C(PTR) \
     280                 :             :     ((((const varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
     281                 :             : #define VARATT_IS_1B(PTR) \
     282                 :             :     ((((const varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
     283                 :             : #define VARATT_IS_1B_E(PTR) \
     284                 :             :     ((((const varattrib_1b *) (PTR))->va_header) == 0x01)
     285                 :             : #define VARATT_NOT_PAD_BYTE(PTR) \
     286                 :             :     (*((const uint8 *) (PTR)) != 0)
     287                 :             : 
     288                 :             : /* VARSIZE_4B() should only be used on known-aligned data */
     289                 :             : #define VARSIZE_4B(PTR) \
     290                 :             :     ((((const varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
     291                 :             : #define VARSIZE_1B(PTR) \
     292                 :             :     ((((const varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
     293                 :             : #define VARTAG_1B_E(PTR) \
     294                 :             :     ((vartag_external) ((const varattrib_1b_e *) (PTR))->va_tag)
     295                 :             : 
     296                 :             : #define SET_VARSIZE_4B(PTR,len) \
     297                 :             :     (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2))
     298                 :             : #define SET_VARSIZE_4B_C(PTR,len) \
     299                 :             :     (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2) | 0x02)
     300                 :             : #define SET_VARSIZE_1B(PTR,len) \
     301                 :             :     (((varattrib_1b *) (PTR))->va_header = (((uint8) (len)) << 1) | 0x01)
     302                 :             : #define SET_VARTAG_1B_E(PTR,tag) \
     303                 :             :     (((varattrib_1b_e *) (PTR))->va_header = 0x01, \
     304                 :             :      ((varattrib_1b_e *) (PTR))->va_tag = (tag))
     305                 :             : 
     306                 :             : #endif                          /* WORDS_BIGENDIAN */
     307                 :             : 
     308                 :             : #define VARDATA_4B(PTR)     (((varattrib_4b *) (PTR))->va_4byte.va_data)
     309                 :             : #define VARDATA_4B_C(PTR)   (((varattrib_4b *) (PTR))->va_compressed.va_data)
     310                 :             : #define VARDATA_1B(PTR)     (((varattrib_1b *) (PTR))->va_data)
     311                 :             : #define VARDATA_1B_E(PTR)   (((varattrib_1b_e *) (PTR))->va_data)
     312                 :             : 
     313                 :             : /*
     314                 :             :  * Externally visible TOAST functions and macros begin here.  All of these
     315                 :             :  * were originally macros, accounting for the upper-case naming.
     316                 :             :  *
     317                 :             :  * Most of these functions accept a pointer to a value of a toastable data
     318                 :             :  * type.  The caller's variable might be declared "text *" or the like,
     319                 :             :  * so we use "void *" here.  Callers that are working with a Datum variable
     320                 :             :  * must apply DatumGetPointer before calling these functions.
     321                 :             :  */
     322                 :             : 
     323                 :             : #define VARHDRSZ_EXTERNAL       offsetof(varattrib_1b_e, va_data)
     324                 :             : #define VARHDRSZ_COMPRESSED     offsetof(varattrib_4b, va_compressed.va_data)
     325                 :             : #define VARHDRSZ_SHORT          offsetof(varattrib_1b, va_data)
     326                 :             : #define VARATT_SHORT_MAX        0x7F
     327                 :             : 
     328                 :             : /*
     329                 :             :  * In consumers oblivious to data alignment, call PG_DETOAST_DATUM_PACKED(),
     330                 :             :  * VARDATA_ANY(), VARSIZE_ANY() and VARSIZE_ANY_EXHDR().  Elsewhere, call
     331                 :             :  * PG_DETOAST_DATUM(), VARDATA() and VARSIZE().  Directly fetching an int16,
     332                 :             :  * int32 or wider field in the struct representing the datum layout requires
     333                 :             :  * aligned data.  memcpy() is alignment-oblivious, as are most operations on
     334                 :             :  * datatypes, such as text, whose layout struct contains only char fields.
     335                 :             :  *
     336                 :             :  * Code assembling a new datum should call VARDATA() and SET_VARSIZE().
     337                 :             :  * (Datums begin life untoasted.)
     338                 :             :  *
     339                 :             :  * Other functions here should usually be used only by tuple assembly/disassembly
     340                 :             :  * code and code that specifically wants to work with still-toasted Datums.
     341                 :             :  */
     342                 :             : 
     343                 :             : /* Size of a known-not-toasted varlena datum, including header */
     344                 :             : static inline Size
     345                 :   180445653 : VARSIZE(const void *PTR)
     346                 :             : {
     347                 :   180445653 :     return VARSIZE_4B(PTR);
     348                 :             : }
     349                 :             : 
     350                 :             : /* Start of data area of a known-not-toasted varlena datum */
     351                 :             : static inline char *
     352                 :    93908843 : VARDATA(const void *PTR)
     353                 :             : {
     354                 :    93908843 :     return VARDATA_4B(PTR);
     355                 :             : }
     356                 :             : 
     357                 :             : /* Size of a known-short-header varlena datum, including header */
     358                 :             : static inline Size
     359                 :    50168973 : VARSIZE_SHORT(const void *PTR)
     360                 :             : {
     361                 :    50168973 :     return VARSIZE_1B(PTR);
     362                 :             : }
     363                 :             : 
     364                 :             : /* Start of data area of a known-short-header varlena datum */
     365                 :             : static inline char *
     366                 :    34020709 : VARDATA_SHORT(const void *PTR)
     367                 :             : {
     368                 :    34020709 :     return VARDATA_1B(PTR);
     369                 :             : }
     370                 :             : 
     371                 :             : /* Type tag of a "TOAST pointer" datum */
     372                 :             : static inline vartag_external
     373                 :      436747 : VARTAG_EXTERNAL(const void *PTR)
     374                 :             : {
     375                 :      436747 :     return VARTAG_1B_E(PTR);
     376                 :             : }
     377                 :             : 
     378                 :             : /* Size of a "TOAST pointer" datum, including header */
     379                 :             : static inline Size
     380                 :      122504 : VARSIZE_EXTERNAL(const void *PTR)
     381                 :             : {
     382                 :      122504 :     return VARHDRSZ_EXTERNAL + VARTAG_SIZE(VARTAG_EXTERNAL(PTR));
     383                 :             : }
     384                 :             : 
     385                 :             : /* Start of data area of a "TOAST pointer" datum */
     386                 :             : static inline char *
     387                 :      215983 : VARDATA_EXTERNAL(const void *PTR)
     388                 :             : {
     389                 :      215983 :     return VARDATA_1B_E(PTR);
     390                 :             : }
     391                 :             : 
     392                 :             : /* Is varlena datum in inline-compressed format? */
     393                 :             : static inline bool
     394                 :   202529784 : VARATT_IS_COMPRESSED(const void *PTR)
     395                 :             : {
     396                 :   202529784 :     return VARATT_IS_4B_C(PTR);
     397                 :             : }
     398                 :             : 
     399                 :             : /* Is varlena datum a "TOAST pointer" datum? */
     400                 :             : static inline bool
     401                 :   378791485 : VARATT_IS_EXTERNAL(const void *PTR)
     402                 :             : {
     403                 :   378791485 :     return VARATT_IS_1B_E(PTR);
     404                 :             : }
     405                 :             : 
     406                 :             : /* Is varlena datum a pointer to on-disk toasted data? */
     407                 :             : static inline bool
     408                 :    54211384 : VARATT_IS_EXTERNAL_ONDISK(const void *PTR)
     409                 :             : {
     410                 :             :     vartag_external tag;
     411                 :             : 
     412         [ +  + ]:    54211384 :     if (!VARATT_IS_EXTERNAL(PTR))
     413                 :    54148033 :         return false;
     414                 :       63351 :     tag = VARTAG_EXTERNAL(PTR);
     415   [ +  +  +  + ]:       63351 :     return (tag == VARTAG_ONDISK_OID || tag == VARTAG_ONDISK_OID8);
     416                 :             : }
     417                 :             : 
     418                 :             : /* Is varlena datum an indirect pointer? */
     419                 :             : static inline bool
     420                 :    53932362 : VARATT_IS_EXTERNAL_INDIRECT(const void *PTR)
     421                 :             : {
     422   [ +  +  +  + ]:    53932362 :     return VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_INDIRECT;
     423                 :             : }
     424                 :             : 
     425                 :             : /* Is varlena datum a read-only pointer to an expanded object? */
     426                 :             : static inline bool
     427                 :       20830 : VARATT_IS_EXTERNAL_EXPANDED_RO(const void *PTR)
     428                 :             : {
     429   [ +  +  +  - ]:       20830 :     return VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_EXPANDED_RO;
     430                 :             : }
     431                 :             : 
     432                 :             : /* Is varlena datum a read-write pointer to an expanded object? */
     433                 :             : static inline bool
     434                 :     6385582 : VARATT_IS_EXTERNAL_EXPANDED_RW(const void *PTR)
     435                 :             : {
     436   [ +  +  +  + ]:     6385582 :     return VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_EXPANDED_RW;
     437                 :             : }
     438                 :             : 
     439                 :             : /* Is varlena datum either type of pointer to an expanded object? */
     440                 :             : static inline bool
     441                 :    81042499 : VARATT_IS_EXTERNAL_EXPANDED(const void *PTR)
     442                 :             : {
     443   [ +  +  +  + ]:    81042499 :     return VARATT_IS_EXTERNAL(PTR) && VARTAG_IS_EXPANDED(VARTAG_EXTERNAL(PTR));
     444                 :             : }
     445                 :             : 
     446                 :             : /* Is varlena datum a "TOAST pointer", but not for an expanded object? */
     447                 :             : static inline bool
     448                 :       29539 : VARATT_IS_EXTERNAL_NON_EXPANDED(const void *PTR)
     449                 :             : {
     450   [ +  +  +  + ]:       29539 :     return VARATT_IS_EXTERNAL(PTR) && !VARTAG_IS_EXPANDED(VARTAG_EXTERNAL(PTR));
     451                 :             : }
     452                 :             : 
     453                 :             : /* Is varlena datum a short-header datum? */
     454                 :             : static inline bool
     455                 :   123863486 : VARATT_IS_SHORT(const void *PTR)
     456                 :             : {
     457                 :   123863486 :     return VARATT_IS_1B(PTR);
     458                 :             : }
     459                 :             : 
     460                 :             : /* Is varlena datum not in traditional (4-byte-header, uncompressed) format? */
     461                 :             : static inline bool
     462                 :    95707759 : VARATT_IS_EXTENDED(const void *PTR)
     463                 :             : {
     464                 :    95707759 :     return !VARATT_IS_4B_U(PTR);
     465                 :             : }
     466                 :             : 
     467                 :             : /* Is varlena datum short enough to convert to short-header format? */
     468                 :             : static inline bool
     469                 :    59603679 : VARATT_CAN_MAKE_SHORT(const void *PTR)
     470                 :             : {
     471         [ +  + ]:   114848311 :     return VARATT_IS_4B_U(PTR) &&
     472         [ +  + ]:    55244632 :         (VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT) <= VARATT_SHORT_MAX;
     473                 :             : }
     474                 :             : 
     475                 :             : /* Size that datum will have in short-header format, including header */
     476                 :             : static inline Size
     477                 :    53732835 : VARATT_CONVERTED_SHORT_SIZE(const void *PTR)
     478                 :             : {
     479                 :    53732835 :     return VARSIZE(PTR) - VARHDRSZ + VARHDRSZ_SHORT;
     480                 :             : }
     481                 :             : 
     482                 :             : /* Set the size (including header) of a 4-byte-header varlena datum */
     483                 :             : static inline void
     484                 :   100194288 : SET_VARSIZE(void *PTR, Size len)
     485                 :             : {
     486                 :   100194288 :     SET_VARSIZE_4B(PTR, len);
     487                 :   100194288 : }
     488                 :             : 
     489                 :             : /* Set the size (including header) of a short-header varlena datum */
     490                 :             : static inline void
     491                 :    26764174 : SET_VARSIZE_SHORT(void *PTR, Size len)
     492                 :             : {
     493                 :    26764174 :     SET_VARSIZE_1B(PTR, len);
     494                 :    26764174 : }
     495                 :             : 
     496                 :             : /* Set the size (including header) of an inline-compressed varlena datum */
     497                 :             : static inline void
     498                 :       48097 : SET_VARSIZE_COMPRESSED(void *PTR, Size len)
     499                 :             : {
     500                 :       48097 :     SET_VARSIZE_4B_C(PTR, len);
     501                 :       48097 : }
     502                 :             : 
     503                 :             : /* Set the type tag of a "TOAST pointer" datum */
     504                 :             : static inline void
     505                 :       86156 : SET_VARTAG_EXTERNAL(void *PTR, vartag_external tag)
     506                 :             : {
     507                 :       86156 :     SET_VARTAG_1B_E(PTR, tag);
     508                 :       86156 : }
     509                 :             : 
     510                 :             : /* Size of a varlena datum of any format, including header */
     511                 :             : static inline Size
     512                 :   118275482 : VARSIZE_ANY(const void *PTR)
     513                 :             : {
     514         [ +  + ]:   118275482 :     if (VARATT_IS_1B_E(PTR))
     515                 :      109898 :         return VARSIZE_EXTERNAL(PTR);
     516         [ +  + ]:   118165584 :     else if (VARATT_IS_1B(PTR))
     517                 :    81171668 :         return VARSIZE_1B(PTR);
     518                 :             :     else
     519                 :    36993916 :         return VARSIZE_4B(PTR);
     520                 :             : }
     521                 :             : 
     522                 :             : /* Size of a varlena datum of any format, excluding header */
     523                 :             : static inline Size
     524                 :   141620723 : VARSIZE_ANY_EXHDR(const void *PTR)
     525                 :             : {
     526         [ -  + ]:   141620723 :     if (VARATT_IS_1B_E(PTR))
     527                 :           0 :         return VARSIZE_EXTERNAL(PTR) - VARHDRSZ_EXTERNAL;
     528         [ +  + ]:   141620723 :     else if (VARATT_IS_1B(PTR))
     529                 :    14771840 :         return VARSIZE_1B(PTR) - VARHDRSZ_SHORT;
     530                 :             :     else
     531                 :   126848883 :         return VARSIZE_4B(PTR) - VARHDRSZ;
     532                 :             : }
     533                 :             : 
     534                 :             : /* Start of data area of a plain or short-header varlena datum */
     535                 :             : /* caution: this will not work on an external or compressed-in-line Datum */
     536                 :             : /* caution: this will return a possibly unaligned pointer */
     537                 :             : static inline char *
     538                 :   147509582 : VARDATA_ANY(const void *PTR)
     539                 :             : {
     540         [ +  + ]:   147509582 :     return VARATT_IS_1B(PTR) ? VARDATA_1B(PTR) : VARDATA_4B(PTR);
     541                 :             : }
     542                 :             : 
     543                 :             : /* Decompressed size of a compressed-in-line varlena datum */
     544                 :             : static inline Size
     545                 :      230347 : VARDATA_COMPRESSED_GET_EXTSIZE(const void *PTR)
     546                 :             : {
     547                 :      230347 :     return ((const varattrib_4b *) PTR)->va_compressed.va_tcinfo & VARLENA_EXTSIZE_MASK;
     548                 :             : }
     549                 :             : 
     550                 :             : /* Compression method of a compressed-in-line varlena datum */
     551                 :             : static inline uint32
     552                 :      114279 : VARDATA_COMPRESSED_GET_COMPRESS_METHOD(const void *PTR)
     553                 :             : {
     554                 :      114279 :     return ((const varattrib_4b *) PTR)->va_compressed.va_tcinfo >> VARLENA_EXTSIZE_BITS;
     555                 :             : }
     556                 :             : 
     557                 :             : /*
     558                 :             :  * Set the decompressed size and the compression method of a
     559                 :             :  * compressed-in-line varlena datum.  The length word is not touched here; the
     560                 :             :  * caller is expected to have set it with SET_VARSIZE_COMPRESSED().
     561                 :             :  */
     562                 :             : static inline void
     563                 :       26306 : VARDATA_COMPRESSED_SET_TCINFO(void *PTR, uint32 extsize, uint32 cmethod)
     564                 :             : {
     565                 :             :     Assert(extsize > 0 && extsize <= VARLENA_EXTSIZE_MASK);
     566                 :             :     Assert(cmethod < (1U << (32 - VARLENA_EXTSIZE_BITS)));
     567                 :             : 
     568                 :       26306 :     ((varattrib_4b *) PTR)->va_compressed.va_tcinfo =
     569                 :       26306 :         extsize | (cmethod << VARLENA_EXTSIZE_BITS);
     570                 :       26306 : }
     571                 :             : 
     572                 :             : /*
     573                 :             :  * Same for external Datums, saved into a va_extinfo.
     574                 :             :  */
     575                 :             : static inline Size
     576                 :       75760 : VARATT_EXTINFO_GET_EXTSIZE(uint32 extinfo)
     577                 :             : {
     578                 :       75760 :     return extinfo & VARLENA_EXTSIZE_MASK;
     579                 :             : }
     580                 :             : 
     581                 :             : static inline uint32
     582                 :        2844 : VARATT_EXTINFO_GET_COMPRESS_METHOD(uint32 extinfo)
     583                 :             : {
     584                 :        2844 :     return extinfo >> VARLENA_EXTSIZE_BITS;
     585                 :             : }
     586                 :             : 
     587                 :             : /*
     588                 :             :  * Testing whether an externally-stored value is compressed requires comparing
     589                 :             :  * the saved size stored in va_extinfo (the actual length of the external data)
     590                 :             :  * to rawsize (the original uncompressed datum's size).  The latter includes
     591                 :             :  * VARHDRSZ overhead, the former doesn't.  We never use compression unless it
     592                 :             :  * actually saves space, so we expect either equality or less-than.
     593                 :             :  */
     594                 :             : static inline bool
     595                 :       45167 : VARATT_EXTINFO_IS_COMPRESSED(uint32 extinfo, int32 rawsize)
     596                 :             : {
     597                 :       45167 :     return VARATT_EXTINFO_GET_EXTSIZE(extinfo) < (Size) (rawsize - VARHDRSZ);
     598                 :             : }
     599                 :             : 
     600                 :             : #endif
        

Generated by: LCOV version 2.0-1