1 /*-------------------------------------------------------------------------
4 * POSTGRES tuple descriptor definitions.
7 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/access/tupdesc.h
12 *-------------------------------------------------------------------------
17 #include "access/attnum.h"
18 #include "catalog/pg_attribute.h"
19 #include "nodes/pg_list.h"
22 typedef struct AttrDefault
25 char *adbin
; /* nodeToString representation of expr */
28 typedef struct ConstrCheck
31 char *ccbin
; /* nodeToString representation of expr */
33 bool ccnoinherit
; /* this is a non-inheritable constraint */
36 /* This structure contains constraints of a tuple */
37 typedef struct TupleConstr
39 AttrDefault
*defval
; /* array */
40 ConstrCheck
*check
; /* array */
41 struct AttrMissing
*missing
; /* missing attributes values, NULL if none */
45 bool has_generated_stored
;
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.
56 * Note that only user attributes, not system attributes, are mentioned in
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.
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.
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.
79 typedef struct TupleDescData
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
];
89 typedef struct TupleDescData
*TupleDesc
;
91 /* Accessor for the i'th attribute of tupdesc. */
92 #define TupleDescAttr(tupdesc, i) (&(tupdesc)->attrs[(i)])
94 extern TupleDesc
CreateTemplateTupleDesc(int natts
);
96 extern TupleDesc
CreateTupleDesc(int natts
, Form_pg_attribute
*attrs
);
98 extern TupleDesc
CreateTupleDescCopy(TupleDesc tupdesc
);
100 extern TupleDesc
CreateTupleDescCopyConstr(TupleDesc tupdesc
);
102 #define TupleDescSize(src) \
103 (offsetof(struct TupleDescData, attrs) + \
104 (src)->natts * sizeof(FormData_pg_attribute))
106 extern void TupleDescCopy(TupleDesc dst
, TupleDesc src
);
108 extern void TupleDescCopyEntry(TupleDesc dst
, AttrNumber dstAttno
,
109 TupleDesc src
, AttrNumber srcAttno
);
111 extern void FreeTupleDesc(TupleDesc tupdesc
);
113 extern void IncrTupleDescRefCount(TupleDesc tupdesc
);
114 extern void DecrTupleDescRefCount(TupleDesc tupdesc
);
116 #define PinTupleDesc(tupdesc) \
118 if ((tupdesc)->tdrefcount >= 0) \
119 IncrTupleDescRefCount(tupdesc); \
122 #define ReleaseTupleDesc(tupdesc) \
124 if ((tupdesc)->tdrefcount >= 0) \
125 DecrTupleDescRefCount(tupdesc); \
128 extern bool equalTupleDescs(TupleDesc tupdesc1
, TupleDesc tupdesc2
);
130 extern uint32
hashTupleDesc(TupleDesc tupdesc
);
132 extern void TupleDescInitEntry(TupleDesc desc
,
133 AttrNumber attributeNumber
,
134 const char *attributeName
,
139 extern void TupleDescInitBuiltinEntry(TupleDesc desc
,
140 AttrNumber attributeNumber
,
141 const char *attributeName
,
146 extern void TupleDescInitEntryCollation(TupleDesc desc
,
147 AttrNumber attributeNumber
,
150 extern TupleDesc
BuildDescForRelation(List
*schema
);
152 extern TupleDesc
BuildDescFromLists(List
*names
, List
*types
, List
*typmods
, List
*collations
);
154 #endif /* TUPDESC_H */