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-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"
18 #include "catalog/pg_attribute.h"
19 #include "nodes/pg_list.h"
20 
21 
22 typedef struct AttrDefault
23 {
25  char *adbin; /* nodeToString representation of expr */
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 */
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 */
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 */
89 typedef struct TupleDescData *TupleDesc;
90 
91 /* Accessor for the i'th attribute of tupdesc. */
92 #define TupleDescAttr(tupdesc, i) (&(tupdesc)->attrs[(i)])
93 
95 
97 
99 
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 extern bool equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2);
130 extern uint32 hashRowType(TupleDesc desc);
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 BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations);
151 
153 
154 #endif /* TUPDESC_H */
int16 AttrNumber
Definition: attnum.h:21
unsigned short uint16
Definition: c.h:492
unsigned int uint32
Definition: c.h:493
signed int int32
Definition: c.h:481
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:385
struct typedefs * types
Definition: ecpg.c:29
FormData_pg_attribute
Definition: pg_attribute.h:193
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
unsigned int Oid
Definition: postgres_ext.h:31
AttrNumber adnum
Definition: tupdesc.h:24
char * adbin
Definition: tupdesc.h:25
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:84
TupleConstr * constr
Definition: tupdesc.h:85
int32 tdtypmod
Definition: tupdesc.h:83
FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER]
Definition: tupdesc.h:87
Oid tdtypeid
Definition: tupdesc.h:82
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:173
void TupleDescCopy(TupleDesc dst, TupleDesc src)
Definition: tupdesc.c:251
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:406
struct TupleConstr TupleConstr
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:331
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:388
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:67
Node * TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
Definition: tupdesc.c:899
struct AttrDefault AttrDefault
uint32 hashRowType(TupleDesc desc)
Definition: tupdesc.c:622
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:133
void TupleDescInitBuiltinEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:726
TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
Definition: tupdesc.c:858
struct TupleDescData TupleDescData
bool equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:586
struct TupleDescData * TupleDesc
Definition: tupdesc.h:89
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:833
TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs)
Definition: tupdesc.c:112
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:651
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:419
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:289
struct ConstrCheck ConstrCheck