PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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 */
45 bool has_not_null; /* any not-null, including not valid ones */
49
50/*
51 * CompactAttribute
52 * Cut-down version of FormData_pg_attribute for faster access for tasks
53 * such as tuple deformation. The fields of this struct are populated
54 * using the populate_compact_attribute() function, which must be called
55 * directly after the FormData_pg_attribute struct is populated or
56 * altered in any way.
57 *
58 * Currently, this struct is 8 bytes. Any code changes which enlarge this
59 * struct should be considered very carefully.
60 *
61 * Code which must access a TupleDesc's attribute data should always make use
62 * the fields of this struct when required fields are available here. It's
63 * more efficient to access the memory in CompactAttribute due to it being a
64 * more compact representation of FormData_pg_attribute and also because
65 * accessing the FormData_pg_attribute requires an additional calculations to
66 * obtain the base address of the array within the TupleDesc.
67 */
68typedef struct CompactAttribute
69{
70 int16 attcacheoff; /* fixed offset into tuple, if known, or -1 */
71 int16 attlen; /* attr len in bytes or -1 = varlen, -2 =
72 * cstring */
73 bool attbyval; /* as FormData_pg_attribute.attbyval */
74 uint8 attalignby; /* alignment requirement in bytes */
75 bool attispackable:1; /* FormData_pg_attribute.attstorage !=
76 * TYPSTORAGE_PLAIN */
77 bool atthasmissing:1; /* as FormData_pg_attribute.atthasmissing */
78 bool attisdropped:1; /* as FormData_pg_attribute.attisdropped */
79 bool attgenerated:1; /* FormData_pg_attribute.attgenerated != '\0' */
80 char attnullability; /* status of not-null constraint, see below */
82
83/* Valid values for CompactAttribute->attnullability */
84#define ATTNULLABLE_UNRESTRICTED 'f' /* No constraint exists */
85#define ATTNULLABLE_UNKNOWN 'u' /* constraint exists, validity unknown */
86#define ATTNULLABLE_VALID 'v' /* valid constraint exists */
87#define ATTNULLABLE_INVALID 'i' /* constraint exists, marked invalid */
88
89/*
90 * This struct is passed around within the backend to describe the structure
91 * of tuples. For tuples coming from on-disk relations, the information is
92 * collected from the pg_attribute, pg_attrdef, and pg_constraint catalogs.
93 * Transient row types (such as the result of a join query) have anonymous
94 * TupleDesc structs that generally omit any constraint info; therefore the
95 * structure is designed to let the constraints be omitted efficiently.
96 *
97 * Note that only user attributes, not system attributes, are mentioned in
98 * TupleDesc.
99 *
100 * If the tupdesc is known to correspond to a named rowtype (such as a table's
101 * rowtype) then tdtypeid identifies that type and tdtypmod is -1. Otherwise
102 * tdtypeid is RECORDOID, and tdtypmod can be either -1 for a fully anonymous
103 * row type, or a value >= 0 to allow the rowtype to be looked up in the
104 * typcache.c type cache.
105 *
106 * Note that tdtypeid is never the OID of a domain over composite, even if
107 * we are dealing with values that are known (at some higher level) to be of
108 * a domain-over-composite type. This is because tdtypeid/tdtypmod need to
109 * match up with the type labeling of composite Datums, and those are never
110 * explicitly marked as being of a domain type, either.
111 *
112 * Tuple descriptors that live in caches (relcache or typcache, at present)
113 * are reference-counted: they can be deleted when their reference count goes
114 * to zero. Tuple descriptors created by the executor need no reference
115 * counting, however: they are simply created in the appropriate memory
116 * context and go away when the context is freed. We set the tdrefcount
117 * field of such a descriptor to -1, while reference-counted descriptors
118 * always have tdrefcount >= 0.
119 *
120 * Beyond the compact_attrs variable length array, the TupleDesc stores an
121 * array of FormData_pg_attribute. The TupleDescAttr() function, as defined
122 * below, takes care of calculating the address of the elements of the
123 * FormData_pg_attribute array.
124 *
125 * The array of CompactAttribute is effectively an abbreviated version of the
126 * array of FormData_pg_attribute. Because CompactAttribute is significantly
127 * smaller than FormData_pg_attribute, code, especially performance-critical
128 * code, should prioritize using the fields from the CompactAttribute over the
129 * equivalent fields in FormData_pg_attribute.
130 *
131 * Any code making changes manually to and fields in the FormData_pg_attribute
132 * array must subsequently call populate_compact_attribute() to flush the
133 * changes out to the corresponding 'compact_attrs' element.
134 *
135 * firstNonCachedOffsetAttr stores the index into the compact_attrs array for
136 * the first attribute that we don't have a known attcacheoff for.
137 *
138 * firstNonGuaranteedAttr stores the index to into the compact_attrs array for
139 * the first attribute that is either NULLable, missing, or !attbyval. This
140 * can be used in locations as a guarantee that attributes before this will
141 * always exist in tuples. The !attbyval part isn't required for this, but
142 * including this allows various tuple deforming routines to forego any checks
143 * for !attbyval.
144 *
145 * Once a TupleDesc has been populated, before it is used for any purpose,
146 * TupleDescFinalize() must be called on it.
147 */
148typedef struct TupleDescData
149{
150 int natts; /* number of attributes in the tuple */
151 Oid tdtypeid; /* composite type ID for tuple type */
152 int32 tdtypmod; /* typmod for tuple type */
153 int tdrefcount; /* reference count, or -1 if not counting */
154 int firstNonCachedOffsetAttr; /* index of first compact_attrs
155 * element without an attcacheoff */
156 int firstNonGuaranteedAttr; /* index of the first nullable,
157 * missing, dropped, or !attbyval
158 * compact_attrs element. */
159 TupleConstr *constr; /* constraints, or NULL if none */
160 /* compact_attrs[N] is the compact metadata of Attribute Number N+1 */
163typedef struct TupleDescData *TupleDesc;
164
165extern void populate_compact_attribute(TupleDesc tupdesc, int attnum);
166
167/*
168 * Calculates the base address of the Form_pg_attribute at the end of the
169 * TupleDescData struct.
170 */
171#define TupleDescAttrAddress(desc) \
172 (Form_pg_attribute) ((char *) (desc) + \
173 (offsetof(struct TupleDescData, compact_attrs) + \
174 (desc)->natts * sizeof(CompactAttribute)))
175
176/* Accessor for the i'th FormData_pg_attribute element of tupdesc. */
177static inline FormData_pg_attribute *
179{
181
182 return &attrs[i];
183}
184
185#undef TupleDescAttrAddress
186
188
189/*
190 * Accessor for the i'th CompactAttribute element of tupdesc.
191 */
192static inline CompactAttribute *
194{
196
197#ifdef USE_ASSERT_CHECKING
198
199 /* Check that the CompactAttribute is correctly populated */
200 verify_compact_attribute(tupdesc, i);
201#endif
202
203 return cattr;
204}
205
207
209
211
213
215
216#define TupleDescSize(src) \
217 (offsetof(struct TupleDescData, compact_attrs) + \
218 (src)->natts * sizeof(CompactAttribute) + \
219 (src)->natts * sizeof(FormData_pg_attribute))
220
221extern void TupleDescCopy(TupleDesc dst, TupleDesc src);
222
225
226extern void TupleDescFinalize(TupleDesc tupdesc);
227extern void FreeTupleDesc(TupleDesc tupdesc);
228
229extern void IncrTupleDescRefCount(TupleDesc tupdesc);
230extern void DecrTupleDescRefCount(TupleDesc tupdesc);
231
232#define PinTupleDesc(tupdesc) \
233 do { \
234 if ((tupdesc)->tdrefcount >= 0) \
235 IncrTupleDescRefCount(tupdesc); \
236 } while (0)
237
238#define ReleaseTupleDesc(tupdesc) \
239 do { \
240 if ((tupdesc)->tdrefcount >= 0) \
241 DecrTupleDescRefCount(tupdesc); \
242 } while (0)
243
246extern uint32 hashRowType(TupleDesc desc);
247
248extern void TupleDescInitEntry(TupleDesc desc,
250 const char *attributeName,
252 int32 typmod,
253 int attdim);
254
255extern void TupleDescInitBuiltinEntry(TupleDesc desc,
257 const char *attributeName,
259 int32 typmod,
260 int attdim);
261
265
266extern TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations);
267
269
270#endif /* TUPDESC_H */
int16 AttrNumber
Definition attnum.h:21
uint8_t uint8
Definition c.h:616
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:552
int16_t int16
Definition c.h:613
int32_t int32
Definition c.h:614
uint16_t uint16
Definition c.h:617
uint32_t uint32
Definition c.h:618
struct typedefs * types
Definition ecpg.c:30
int i
Definition isn.c:77
FormData_pg_attribute
int16 attnum
FormData_pg_attribute * Form_pg_attribute
unsigned int Oid
static int fb(int x)
AttrNumber adnum
Definition tupdesc.h:24
char * adbin
Definition tupdesc.h:25
bool attgenerated
Definition tupdesc.h:79
int16 attcacheoff
Definition tupdesc.h:70
uint8 attalignby
Definition tupdesc.h:74
bool attisdropped
Definition tupdesc.h:78
bool attispackable
Definition tupdesc.h:75
char attnullability
Definition tupdesc.h:80
bool atthasmissing
Definition tupdesc.h:77
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:135
bool has_generated_virtual
Definition tupdesc.h:47
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 firstNonCachedOffsetAttr
Definition tupdesc.h:154
CompactAttribute compact_attrs[FLEXIBLE_ARRAY_MEMBER]
Definition tupdesc.h:161
TupleConstr * constr
Definition tupdesc.h:159
int32 tdtypmod
Definition tupdesc.h:152
int firstNonGuaranteedAttr
Definition tupdesc.h:156
#define TupleDescAttrAddress(desc)
Definition tupdesc.h:171
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition tupdesc.c:334
void TupleDescCopy(TupleDesc dst, TupleDesc src)
Definition tupdesc.c:424
Node * TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
Definition tupdesc.c:1149
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition tupdesc.c:632
void FreeTupleDesc(TupleDesc tupdesc)
Definition tupdesc.c:557
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition tupdesc.c:614
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:508
void verify_compact_attribute(TupleDesc, int attnum)
Definition tupdesc.c:125
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:193
uint32 hashRowType(TupleDesc desc)
Definition tupdesc.c:868
TupleDesc CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
Definition tupdesc.c:288
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition tupdesc.c:242
void TupleDescInitBuiltinEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:973
TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
Definition tupdesc.c:1106
void populate_compact_attribute(TupleDesc tupdesc, int attnum)
Definition tupdesc.c:100
bool equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition tupdesc.c:832
struct TupleDescData * TupleDesc
Definition tupdesc.h:163
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition tupdesc.c:1081
TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs)
Definition tupdesc.c:216
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:897
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition tupdesc.c:645
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition tupdesc.c:469