LCOV - code coverage report
Current view: top level - src/include/access - tupdesc.h (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 6 6 100.0 %
Date: 2025-01-18 05:15:39 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * tupdesc.h
       4             :  *    POSTGRES tuple descriptor definitions.
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  * src/include/access/tupdesc.h
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #ifndef TUPDESC_H
      15             : #define TUPDESC_H
      16             : 
      17             : #include "access/attnum.h"
      18             : #include "catalog/pg_attribute.h"
      19             : #include "nodes/pg_list.h"
      20             : 
      21             : 
      22             : typedef struct AttrDefault
      23             : {
      24             :     AttrNumber  adnum;
      25             :     char       *adbin;          /* nodeToString representation of expr */
      26             : } AttrDefault;
      27             : 
      28             : typedef struct ConstrCheck
      29             : {
      30             :     char       *ccname;
      31             :     char       *ccbin;          /* nodeToString representation of expr */
      32             :     bool        ccenforced;
      33             :     bool        ccvalid;
      34             :     bool        ccnoinherit;    /* this is a non-inheritable constraint */
      35             : } ConstrCheck;
      36             : 
      37             : /* This structure contains constraints of a tuple */
      38             : typedef struct TupleConstr
      39             : {
      40             :     AttrDefault *defval;        /* array */
      41             :     ConstrCheck *check;         /* array */
      42             :     struct AttrMissing *missing;    /* missing attributes values, NULL if none */
      43             :     uint16      num_defval;
      44             :     uint16      num_check;
      45             :     bool        has_not_null;
      46             :     bool        has_generated_stored;
      47             : } TupleConstr;
      48             : 
      49             : /*
      50             :  * CompactAttribute
      51             :  *      Cut-down version of FormData_pg_attribute for faster access for tasks
      52             :  *      such as tuple deformation.  The fields of this struct are populated
      53             :  *      using the populate_compact_attribute() function, which must be called
      54             :  *      directly after the FormData_pg_attribute struct is populated or
      55             :  *      altered in any way.
      56             :  *
      57             :  * Currently, this struct is 16 bytes.  Any code changes which enlarge this
      58             :  * struct should be considered very carefully.
      59             :  *
      60             :  * Code which must access a TupleDesc's attribute data should always make use
      61             :  * the fields of this struct when required fields are available here.  It's
      62             :  * more efficient to access the memory in CompactAttribute due to it being a
      63             :  * more compact representation of FormData_pg_attribute and also because
      64             :  * accessing the FormData_pg_attribute requires an additional calculations to
      65             :  * obtain the base address of the array within the TupleDesc.
      66             :  */
      67             : typedef struct CompactAttribute
      68             : {
      69             :     int32       attcacheoff;    /* fixed offset into tuple, if known, or -1 */
      70             :     int16       attlen;         /* attr len in bytes or -1 = varlen, -2 =
      71             :                                  * cstring */
      72             :     bool        attbyval;       /* as FormData_pg_attribute.attbyval */
      73             :     bool        attispackable;  /* FormData_pg_attribute.attstorage !=
      74             :                                  * TYPSTORAGE_PLAIN */
      75             :     bool        atthasmissing;  /* as FormData_pg_attribute.atthasmissing */
      76             :     bool        attisdropped;   /* as FormData_pg_attribute.attisdropped */
      77             :     bool        attgenerated;   /* FormData_pg_attribute.attgenerated != '\0' */
      78             :     bool        attnotnull;     /* as FormData_pg_attribute.attnotnull */
      79             :     uint8       attalignby;     /* alignment requirement in bytes */
      80             : } CompactAttribute;
      81             : 
      82             : /*
      83             :  * This struct is passed around within the backend to describe the structure
      84             :  * of tuples.  For tuples coming from on-disk relations, the information is
      85             :  * collected from the pg_attribute, pg_attrdef, and pg_constraint catalogs.
      86             :  * Transient row types (such as the result of a join query) have anonymous
      87             :  * TupleDesc structs that generally omit any constraint info; therefore the
      88             :  * structure is designed to let the constraints be omitted efficiently.
      89             :  *
      90             :  * Note that only user attributes, not system attributes, are mentioned in
      91             :  * TupleDesc.
      92             :  *
      93             :  * If the tupdesc is known to correspond to a named rowtype (such as a table's
      94             :  * rowtype) then tdtypeid identifies that type and tdtypmod is -1.  Otherwise
      95             :  * tdtypeid is RECORDOID, and tdtypmod can be either -1 for a fully anonymous
      96             :  * row type, or a value >= 0 to allow the rowtype to be looked up in the
      97             :  * typcache.c type cache.
      98             :  *
      99             :  * Note that tdtypeid is never the OID of a domain over composite, even if
     100             :  * we are dealing with values that are known (at some higher level) to be of
     101             :  * a domain-over-composite type.  This is because tdtypeid/tdtypmod need to
     102             :  * match up with the type labeling of composite Datums, and those are never
     103             :  * explicitly marked as being of a domain type, either.
     104             :  *
     105             :  * Tuple descriptors that live in caches (relcache or typcache, at present)
     106             :  * are reference-counted: they can be deleted when their reference count goes
     107             :  * to zero.  Tuple descriptors created by the executor need no reference
     108             :  * counting, however: they are simply created in the appropriate memory
     109             :  * context and go away when the context is freed.  We set the tdrefcount
     110             :  * field of such a descriptor to -1, while reference-counted descriptors
     111             :  * always have tdrefcount >= 0.
     112             :  *
     113             :  * Beyond the compact_attrs variable length array, the TupleDesc stores an
     114             :  * array of FormData_pg_attribute.  The TupleDescAttr() function, as defined
     115             :  * below, takes care of calculating the address of the elements of the
     116             :  * FormData_pg_attribute array.
     117             :  *
     118             :  * The array of CompactAttribute is effectively an abbreviated version of the
     119             :  * array of FormData_pg_attribute.  Because CompactAttribute is significantly
     120             :  * smaller than FormData_pg_attribute, code, especially performance-critical
     121             :  * code, should prioritize using the fields from the CompactAttribute over the
     122             :  * equivalent fields in FormData_pg_attribute.
     123             :  *
     124             :  * Any code making changes manually to and fields in the FormData_pg_attribute
     125             :  * array must subsequently call populate_compact_attribute() to flush the
     126             :  * changes out to the corresponding 'compact_attrs' element.
     127             :  */
     128             : typedef struct TupleDescData
     129             : {
     130             :     int         natts;          /* number of attributes in the tuple */
     131             :     Oid         tdtypeid;       /* composite type ID for tuple type */
     132             :     int32       tdtypmod;       /* typmod for tuple type */
     133             :     int         tdrefcount;     /* reference count, or -1 if not counting */
     134             :     TupleConstr *constr;        /* constraints, or NULL if none */
     135             :     /* compact_attrs[N] is the compact metadata of Attribute Number N+1 */
     136             :     CompactAttribute compact_attrs[FLEXIBLE_ARRAY_MEMBER];
     137             : }           TupleDescData;
     138             : typedef struct TupleDescData *TupleDesc;
     139             : 
     140             : extern void populate_compact_attribute(TupleDesc tupdesc, int attnum);
     141             : 
     142             : /*
     143             :  * Calculates the base address of the Form_pg_attribute at the end of the
     144             :  * TupleDescData struct.
     145             :  */
     146             : #define TupleDescAttrAddress(desc) \
     147             :     (Form_pg_attribute) ((char *) (desc) + \
     148             :      (offsetof(struct TupleDescData, compact_attrs) + \
     149             :      (desc)->natts * sizeof(CompactAttribute)))
     150             : 
     151             : /* Accessor for the i'th FormData_pg_attribute element of tupdesc. */
     152             : static inline FormData_pg_attribute *
     153   287484524 : TupleDescAttr(TupleDesc tupdesc, int i)
     154             : {
     155   287484524 :     FormData_pg_attribute *attrs = TupleDescAttrAddress(tupdesc);
     156             : 
     157   287484524 :     return &attrs[i];
     158             : }
     159             : 
     160             : #undef TupleDescAttrAddress
     161             : 
     162             : extern void verify_compact_attribute(TupleDesc, int attnum);
     163             : 
     164             : /*
     165             :  * Accessor for the i'th CompactAttribute element of tupdesc.
     166             :  */
     167             : static inline CompactAttribute *
     168  2503186368 : TupleDescCompactAttr(TupleDesc tupdesc, int i)
     169             : {
     170  2503186368 :     CompactAttribute *cattr = &tupdesc->compact_attrs[i];
     171             : 
     172             : #ifdef USE_ASSERT_CHECKING
     173             : 
     174             :     /* Check that the CompactAttribute is correctly populated */
     175             :     verify_compact_attribute(tupdesc, i);
     176             : #endif
     177             : 
     178  2503186368 :     return cattr;
     179             : }
     180             : 
     181             : extern TupleDesc CreateTemplateTupleDesc(int natts);
     182             : 
     183             : extern TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs);
     184             : 
     185             : extern TupleDesc CreateTupleDescCopy(TupleDesc tupdesc);
     186             : 
     187             : extern TupleDesc CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts);
     188             : 
     189             : extern TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc);
     190             : 
     191             : #define TupleDescSize(src) \
     192             :     (offsetof(struct TupleDescData, compact_attrs) + \
     193             :      (src)->natts * sizeof(CompactAttribute) + \
     194             :      (src)->natts * sizeof(FormData_pg_attribute))
     195             : 
     196             : extern void TupleDescCopy(TupleDesc dst, TupleDesc src);
     197             : 
     198             : extern void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
     199             :                                TupleDesc src, AttrNumber srcAttno);
     200             : 
     201             : extern void FreeTupleDesc(TupleDesc tupdesc);
     202             : 
     203             : extern void IncrTupleDescRefCount(TupleDesc tupdesc);
     204             : extern void DecrTupleDescRefCount(TupleDesc tupdesc);
     205             : 
     206             : #define PinTupleDesc(tupdesc) \
     207             :     do { \
     208             :         if ((tupdesc)->tdrefcount >= 0) \
     209             :             IncrTupleDescRefCount(tupdesc); \
     210             :     } while (0)
     211             : 
     212             : #define ReleaseTupleDesc(tupdesc) \
     213             :     do { \
     214             :         if ((tupdesc)->tdrefcount >= 0) \
     215             :             DecrTupleDescRefCount(tupdesc); \
     216             :     } while (0)
     217             : 
     218             : extern bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
     219             : extern bool equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2);
     220             : extern uint32 hashRowType(TupleDesc desc);
     221             : 
     222             : extern void TupleDescInitEntry(TupleDesc desc,
     223             :                                AttrNumber attributeNumber,
     224             :                                const char *attributeName,
     225             :                                Oid oidtypeid,
     226             :                                int32 typmod,
     227             :                                int attdim);
     228             : 
     229             : extern void TupleDescInitBuiltinEntry(TupleDesc desc,
     230             :                                       AttrNumber attributeNumber,
     231             :                                       const char *attributeName,
     232             :                                       Oid oidtypeid,
     233             :                                       int32 typmod,
     234             :                                       int attdim);
     235             : 
     236             : extern void TupleDescInitEntryCollation(TupleDesc desc,
     237             :                                         AttrNumber attributeNumber,
     238             :                                         Oid collationid);
     239             : 
     240             : extern TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations);
     241             : 
     242             : extern Node *TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum);
     243             : 
     244             : #endif                          /* TUPDESC_H */

Generated by: LCOV version 1.14