| 1 | /*------------------------------------------------------------------------- |
| 2 | * |
| 3 | * tupdesc.h |
| 4 | * POSTGRES tuple descriptor definitions. |
| 5 | * |
| 6 | * |
| 7 | * Portions Copyright (c) 1996-2019, 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 ccvalid; |
| 33 | bool ccnoinherit; /* this is a non-inheritable constraint */ |
| 34 | } ConstrCheck; |
| 35 | |
| 36 | /* This structure contains constraints of a tuple */ |
| 37 | typedef struct TupleConstr |
| 38 | { |
| 39 | AttrDefault *defval; /* array */ |
| 40 | ConstrCheck *check; /* array */ |
| 41 | struct AttrMissing *missing; /* missing attributes values, NULL if none */ |
| 42 | uint16 num_defval; |
| 43 | uint16 num_check; |
| 44 | bool has_not_null; |
| 45 | bool has_generated_stored; |
| 46 | } TupleConstr; |
| 47 | |
| 48 | /* |
| 49 | * This struct is passed around within the backend to describe the structure |
| 50 | * of tuples. For tuples coming from on-disk relations, the information is |
| 51 | * collected from the pg_attribute, pg_attrdef, and pg_constraint catalogs. |
| 52 | * Transient row types (such as the result of a join query) have anonymous |
| 53 | * TupleDesc structs that generally omit any constraint info; therefore the |
| 54 | * structure is designed to let the constraints be omitted efficiently. |
| 55 | * |
| 56 | * Note that only user attributes, not system attributes, are mentioned in |
| 57 | * TupleDesc. |
| 58 | * |
| 59 | * If the tupdesc is known to correspond to a named rowtype (such as a table's |
| 60 | * rowtype) then tdtypeid identifies that type and tdtypmod is -1. Otherwise |
| 61 | * tdtypeid is RECORDOID, and tdtypmod can be either -1 for a fully anonymous |
| 62 | * row type, or a value >= 0 to allow the rowtype to be looked up in the |
| 63 | * typcache.c type cache. |
| 64 | * |
| 65 | * Note that tdtypeid is never the OID of a domain over composite, even if |
| 66 | * we are dealing with values that are known (at some higher level) to be of |
| 67 | * a domain-over-composite type. This is because tdtypeid/tdtypmod need to |
| 68 | * match up with the type labeling of composite Datums, and those are never |
| 69 | * explicitly marked as being of a domain type, either. |
| 70 | * |
| 71 | * Tuple descriptors that live in caches (relcache or typcache, at present) |
| 72 | * are reference-counted: they can be deleted when their reference count goes |
| 73 | * to zero. Tuple descriptors created by the executor need no reference |
| 74 | * counting, however: they are simply created in the appropriate memory |
| 75 | * context and go away when the context is freed. We set the tdrefcount |
| 76 | * field of such a descriptor to -1, while reference-counted descriptors |
| 77 | * always have tdrefcount >= 0. |
| 78 | */ |
| 79 | typedef struct TupleDescData |
| 80 | { |
| 81 | int natts; /* number of attributes in the tuple */ |
| 82 | Oid tdtypeid; /* composite type ID for tuple type */ |
| 83 | int32 tdtypmod; /* typmod for tuple type */ |
| 84 | int tdrefcount; /* reference count, or -1 if not counting */ |
| 85 | TupleConstr *constr; /* constraints, or NULL if none */ |
| 86 | /* attrs[N] is the description of Attribute Number N+1 */ |
| 87 | FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER]; |
| 88 | } TupleDescData; |
| 89 | typedef struct TupleDescData *TupleDesc; |
| 90 | |
| 91 | /* Accessor for the i'th attribute of tupdesc. */ |
| 92 | #define TupleDescAttr(tupdesc, i) (&(tupdesc)->attrs[(i)]) |
| 93 | |
| 94 | extern TupleDesc CreateTemplateTupleDesc(int natts); |
| 95 | |
| 96 | extern TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs); |
| 97 | |
| 98 | extern TupleDesc CreateTupleDescCopy(TupleDesc tupdesc); |
| 99 | |
| 100 | extern TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc); |
| 101 | |
| 102 | #define TupleDescSize(src) \ |
| 103 | (offsetof(struct TupleDescData, attrs) + \ |
| 104 | (src)->natts * sizeof(FormData_pg_attribute)) |
| 105 | |
| 106 | extern void TupleDescCopy(TupleDesc dst, TupleDesc src); |
| 107 | |
| 108 | extern void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, |
| 109 | TupleDesc src, AttrNumber srcAttno); |
| 110 | |
| 111 | extern void FreeTupleDesc(TupleDesc tupdesc); |
| 112 | |
| 113 | extern void IncrTupleDescRefCount(TupleDesc tupdesc); |
| 114 | extern void DecrTupleDescRefCount(TupleDesc tupdesc); |
| 115 | |
| 116 | #define PinTupleDesc(tupdesc) \ |
| 117 | do { \ |
| 118 | if ((tupdesc)->tdrefcount >= 0) \ |
| 119 | IncrTupleDescRefCount(tupdesc); \ |
| 120 | } while (0) |
| 121 | |
| 122 | #define ReleaseTupleDesc(tupdesc) \ |
| 123 | do { \ |
| 124 | if ((tupdesc)->tdrefcount >= 0) \ |
| 125 | DecrTupleDescRefCount(tupdesc); \ |
| 126 | } while (0) |
| 127 | |
| 128 | extern bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2); |
| 129 | |
| 130 | extern uint32 hashTupleDesc(TupleDesc tupdesc); |
| 131 | |
| 132 | extern void TupleDescInitEntry(TupleDesc desc, |
| 133 | AttrNumber attributeNumber, |
| 134 | const char *attributeName, |
| 135 | Oid oidtypeid, |
| 136 | int32 typmod, |
| 137 | int attdim); |
| 138 | |
| 139 | extern void TupleDescInitBuiltinEntry(TupleDesc desc, |
| 140 | AttrNumber attributeNumber, |
| 141 | const char *attributeName, |
| 142 | Oid oidtypeid, |
| 143 | int32 typmod, |
| 144 | int attdim); |
| 145 | |
| 146 | extern void TupleDescInitEntryCollation(TupleDesc desc, |
| 147 | AttrNumber attributeNumber, |
| 148 | Oid collationid); |
| 149 | |
| 150 | extern TupleDesc BuildDescForRelation(List *schema); |
| 151 | |
| 152 | extern TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations); |
| 153 | |
| 154 | #endif /* TUPDESC_H */ |
| 155 | |