PostgreSQL Source Code git master
tupdesc.h
Go to the documentation of this file.
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"
19#include "nodes/pg_list.h"
20
21
22typedef struct AttrDefault
23{
25 char *adbin; /* nodeToString representation of expr */
27
28typedef struct ConstrCheck
29{
30 char *ccname;
31 char *ccbin; /* nodeToString representation of expr */
33 bool ccvalid;
34 bool ccnoinherit; /* this is a non-inheritable constraint */
36
37/* This structure contains constraints of a tuple */
38typedef struct TupleConstr
39{
40 AttrDefault *defval; /* array */
41 ConstrCheck *check; /* array */
42 struct AttrMissing *missing; /* missing attributes values, NULL if none */
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 */
67typedef 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 */
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 */
128typedef 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 */
138typedef struct TupleDescData *TupleDesc;
139
140extern 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. */
152static inline FormData_pg_attribute *
154{
156
157 return &attrs[i];
158}
159
160#undef TupleDescAttrAddress
161
163
164/*
165 * Accessor for the i'th CompactAttribute element of tupdesc.
166 */
167static inline CompactAttribute *
169{
170 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 return cattr;
179}
180
182
184
186
188
190
191#define TupleDescSize(src) \
192 (offsetof(struct TupleDescData, compact_attrs) + \
193 (src)->natts * sizeof(CompactAttribute) + \
194 (src)->natts * sizeof(FormData_pg_attribute))
195
196extern void TupleDescCopy(TupleDesc dst, TupleDesc src);
197
198extern void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
199 TupleDesc src, AttrNumber srcAttno);
200
201extern void FreeTupleDesc(TupleDesc tupdesc);
202
203extern void IncrTupleDescRefCount(TupleDesc tupdesc);
204extern 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
218extern bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
219extern bool equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2);
220extern uint32 hashRowType(TupleDesc desc);
221
222extern void TupleDescInitEntry(TupleDesc desc,
223 AttrNumber attributeNumber,
224 const char *attributeName,
225 Oid oidtypeid,
226 int32 typmod,
227 int attdim);
228
229extern void TupleDescInitBuiltinEntry(TupleDesc desc,
230 AttrNumber attributeNumber,
231 const char *attributeName,
232 Oid oidtypeid,
233 int32 typmod,
234 int attdim);
235
237 AttrNumber attributeNumber,
238 Oid collationid);
239
240extern TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations);
241
243
244#endif /* TUPDESC_H */
int16 AttrNumber
Definition: attnum.h:21
uint8_t uint8
Definition: c.h:486
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:420
int16_t int16
Definition: c.h:483
int32_t int32
Definition: c.h:484
uint16_t uint16
Definition: c.h:487
uint32_t uint32
Definition: c.h:488
struct typedefs * types
Definition: ecpg.c:30
int i
Definition: isn.c:72
FormData_pg_attribute
Definition: pg_attribute.h:184
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
unsigned int Oid
Definition: postgres_ext.h:32
AttrNumber adnum
Definition: tupdesc.h:24
char * adbin
Definition: tupdesc.h:25
bool attgenerated
Definition: tupdesc.h:77
uint8 attalignby
Definition: tupdesc.h:79
bool attisdropped
Definition: tupdesc.h:76
bool attispackable
Definition: tupdesc.h:73
bool attnotnull
Definition: tupdesc.h:78
int16 attlen
Definition: tupdesc.h:70
bool atthasmissing
Definition: tupdesc.h:75
int32 attcacheoff
Definition: tupdesc.h:69
char * ccname
Definition: tupdesc.h:30
bool ccenforced
Definition: tupdesc.h:32
bool ccnoinherit
Definition: tupdesc.h:34
bool ccvalid
Definition: tupdesc.h:33
char * ccbin
Definition: tupdesc.h:31
Definition: pg_list.h:54
Definition: nodes.h:129
bool has_not_null
Definition: tupdesc.h:45
AttrDefault * defval
Definition: tupdesc.h:40
bool has_generated_stored
Definition: tupdesc.h:46
struct AttrMissing * missing
Definition: tupdesc.h:42
ConstrCheck * check
Definition: tupdesc.h:41
uint16 num_defval
Definition: tupdesc.h:43
uint16 num_check
Definition: tupdesc.h:44
int tdrefcount
Definition: tupdesc.h:133
CompactAttribute compact_attrs[FLEXIBLE_ARRAY_MEMBER]
Definition: tupdesc.h:136
TupleConstr * constr
Definition: tupdesc.h:134
int32 tdtypmod
Definition: tupdesc.h:132
Oid tdtypeid
Definition: tupdesc.h:131
#define TupleDescAttrAddress(desc)
Definition: tupdesc.h:146
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:322
void TupleDescCopy(TupleDesc dst, TupleDesc src)
Definition: tupdesc.c:404
Node * TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
Definition: tupdesc.c:1048
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:553
struct TupleConstr TupleConstr
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:478
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:535
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:164
struct AttrDefault AttrDefault
void verify_compact_attribute(TupleDesc, int attnum)
Definition: tupdesc.c:132
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:153
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:168
uint32 hashRowType(TupleDesc desc)
Definition: tupdesc.c:769
TupleDesc CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
Definition: tupdesc.c:278
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:234
void TupleDescInitBuiltinEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:874
TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
Definition: tupdesc.c:1007
struct TupleDescData TupleDescData
struct CompactAttribute CompactAttribute
void populate_compact_attribute(TupleDesc tupdesc, int attnum)
Definition: tupdesc.c:107
bool equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:733
struct TupleDescData * TupleDesc
Definition: tupdesc.h:138
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:982
TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs)
Definition: tupdesc.c:211
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:798
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:566
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:444
struct ConstrCheck ConstrCheck