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