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-2019, 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 */
26 } AttrDefault;
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 */
34 } ConstrCheck;
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 */
46 } TupleConstr;
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 */
87  FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER];
89 typedef struct TupleDescData *TupleDesc;
90 
91 /* Accessor for the i'th attribute of tupdesc. */
92 #define TupleDescAttr(tupdesc, i) (&(tupdesc)->attrs[(i)])
93 
94 extern TupleDesc CreateTemplateTupleDesc(int natts);
95 
96 extern TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs);
97 
98 extern TupleDesc CreateTupleDescCopy(TupleDesc tupdesc);
99 
100 extern TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc);
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 
130 extern uint32 hashTupleDesc(TupleDesc tupdesc);
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 BuildDescForRelation(List *schema);
151 
152 extern TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations);
153 
154 #endif /* TUPDESC_H */
FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER]
Definition: tupdesc.h:87
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:150
uint32 hashTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:574
struct TupleDescData TupleDescData
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:271
struct typedefs * types
Definition: ecpg.c:29
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:313
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:375
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:393
struct ConstrCheck ConstrCheck
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:347
AttrDefault * defval
Definition: tupdesc.h:39
struct AttrDefault AttrDefault
bool has_generated_stored
Definition: tupdesc.h:45
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:411
unsigned short uint16
Definition: c.h:358
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:894
int32 tdtypmod
Definition: tupdesc.h:83
TupleConstr * constr
Definition: tupdesc.h:85
char * ccname
Definition: tupdesc.h:30
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
unsigned int uint32
Definition: c.h:359
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
uint16 num_check
Definition: tupdesc.h:43
FormData_pg_attribute
Definition: pg_attribute.h:184
void TupleDescInitBuiltinEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:676
TupleDesc BuildDescForRelation(List *schema)
Definition: tupdesc.c:794
TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs)
Definition: tupdesc.c:89
struct AttrMissing * missing
Definition: tupdesc.h:41
struct TupleConstr TupleConstr
uint16 num_defval
Definition: tupdesc.h:42
Oid tdtypeid
Definition: tupdesc.h:82
struct TupleDescData * TupleDesc
Definition: tupdesc.h:89
AttrNumber adnum
Definition: tupdesc.h:24
char * adbin
Definition: tupdesc.h:25
void TupleDescCopy(TupleDesc dst, TupleDesc src)
Definition: tupdesc.c:233
int tdrefcount
Definition: tupdesc.h:84
ConstrCheck * check
Definition: tupdesc.h:40
bool ccvalid
Definition: tupdesc.h:32
bool has_not_null
Definition: tupdesc.h:44
Definition: pg_list.h:50
char * ccbin
Definition: tupdesc.h:31
int16 AttrNumber
Definition: attnum.h:21
bool ccnoinherit
Definition: tupdesc.h:33