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-2018, 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 attrMissing *MissingPtr;
29 
30 typedef struct constrCheck
31 {
32  char *ccname;
33  char *ccbin; /* nodeToString representation of expr */
34  bool ccvalid;
35  bool ccnoinherit; /* this is a non-inheritable constraint */
36 } ConstrCheck;
37 
38 /* This structure contains constraints of a tuple */
39 typedef struct tupleConstr
40 {
41  AttrDefault *defval; /* array */
42  ConstrCheck *check; /* array */
43  MissingPtr missing; /* missing attributes values, NULL if none */
47 } TupleConstr;
48 
49 /*
50  * This struct is passed around within the backend to describe the structure
51  * of tuples. For tuples coming from on-disk relations, the information is
52  * collected from the pg_attribute, pg_attrdef, and pg_constraint catalogs.
53  * Transient row types (such as the result of a join query) have anonymous
54  * TupleDesc structs that generally omit any constraint info; therefore the
55  * structure is designed to let the constraints be omitted efficiently.
56  *
57  * Note that only user attributes, not system attributes, are mentioned in
58  * TupleDesc; with the exception that tdhasoid indicates if OID is present.
59  *
60  * If the tupdesc is known to correspond to a named rowtype (such as a table's
61  * rowtype) then tdtypeid identifies that type and tdtypmod is -1. Otherwise
62  * tdtypeid is RECORDOID, and tdtypmod can be either -1 for a fully anonymous
63  * row type, or a value >= 0 to allow the rowtype to be looked up in the
64  * typcache.c type cache.
65  *
66  * Note that tdtypeid is never the OID of a domain over composite, even if
67  * we are dealing with values that are known (at some higher level) to be of
68  * a domain-over-composite type. This is because tdtypeid/tdtypmod need to
69  * match up with the type labeling of composite Datums, and those are never
70  * explicitly marked as being of a domain type, either.
71  *
72  * Tuple descriptors that live in caches (relcache or typcache, at present)
73  * are reference-counted: they can be deleted when their reference count goes
74  * to zero. Tuple descriptors created by the executor need no reference
75  * counting, however: they are simply created in the appropriate memory
76  * context and go away when the context is freed. We set the tdrefcount
77  * field of such a descriptor to -1, while reference-counted descriptors
78  * always have tdrefcount >= 0.
79  */
80 typedef struct tupleDesc
81 {
82  int natts; /* number of attributes in the tuple */
83  Oid tdtypeid; /* composite type ID for tuple type */
84  int32 tdtypmod; /* typmod for tuple type */
85  bool tdhasoid; /* tuple has oid attribute in its header */
86  int tdrefcount; /* reference count, or -1 if not counting */
87  TupleConstr *constr; /* constraints, or NULL if none */
88  /* attrs[N] is the description of Attribute Number N+1 */
89  FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER];
90 } *TupleDesc;
91 
92 /* Accessor for the i'th attribute of tupdesc. */
93 #define TupleDescAttr(tupdesc, i) (&(tupdesc)->attrs[(i)])
94 
95 extern TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid);
96 
97 extern TupleDesc CreateTupleDesc(int natts, bool hasoid,
98  Form_pg_attribute *attrs);
99 
100 extern TupleDesc CreateTupleDescCopy(TupleDesc tupdesc);
101 
103 
104 #define TupleDescSize(src) \
105  (offsetof(struct tupleDesc, attrs) + \
106  (src)->natts * sizeof(FormData_pg_attribute))
107 
108 extern void TupleDescCopy(TupleDesc dst, TupleDesc src);
109 
110 extern void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
111  TupleDesc src, AttrNumber srcAttno);
112 
113 extern void FreeTupleDesc(TupleDesc tupdesc);
114 
115 extern void IncrTupleDescRefCount(TupleDesc tupdesc);
116 extern void DecrTupleDescRefCount(TupleDesc tupdesc);
117 
118 #define PinTupleDesc(tupdesc) \
119  do { \
120  if ((tupdesc)->tdrefcount >= 0) \
121  IncrTupleDescRefCount(tupdesc); \
122  } while (0)
123 
124 #define ReleaseTupleDesc(tupdesc) \
125  do { \
126  if ((tupdesc)->tdrefcount >= 0) \
127  DecrTupleDescRefCount(tupdesc); \
128  } while (0)
129 
130 extern bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
131 
132 extern uint32 hashTupleDesc(TupleDesc tupdesc);
133 
134 extern void TupleDescInitEntry(TupleDesc desc,
135  AttrNumber attributeNumber,
136  const char *attributeName,
137  Oid oidtypeid,
138  int32 typmod,
139  int attdim);
140 
141 extern void TupleDescInitBuiltinEntry(TupleDesc desc,
142  AttrNumber attributeNumber,
143  const char *attributeName,
144  Oid oidtypeid,
145  int32 typmod,
146  int attdim);
147 
148 extern void TupleDescInitEntryCollation(TupleDesc desc,
149  AttrNumber attributeNumber,
150  Oid collationid);
151 
152 extern TupleDesc BuildDescForRelation(List *schema);
153 
154 extern TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations);
155 
156 #endif /* TUPDESC_H */
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:151
uint32 hashTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:570
Oid tdtypeid
Definition: tupdesc.h:83
char * ccname
Definition: tupdesc.h:32
bool tdhasoid
Definition: tupdesc.h:85
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:270
struct typedefs * types
Definition: ecpg.c:29
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:761
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:311
bool ccnoinherit
Definition: tupdesc.h:35
ConstrCheck * check
Definition: tupdesc.h:42
char * ccbin
Definition: tupdesc.h:33
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:373
MissingPtr missing
Definition: tupdesc.h:43
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:391
struct tupleConstr TupleConstr
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:82
int32 tdtypmod
Definition: tupdesc.h:84
signed int int32
Definition: c.h:313
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:409
unsigned short uint16
Definition: c.h:324
AttrDefault * defval
Definition: tupdesc.h:41
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:885
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:600
AttrNumber adnum
Definition: tupdesc.h:24
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
unsigned int uint32
Definition: c.h:325
char * adbin
Definition: tupdesc.h:25
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:112
bool has_not_null
Definition: tupdesc.h:46
struct constrCheck ConstrCheck
struct attrMissing * MissingPtr
Definition: tupdesc.h:28
FormData_pg_attribute
Definition: pg_attribute.h:181
uint16 num_defval
Definition: tupdesc.h:44
struct tupleDesc * TupleDesc
void TupleDescInitBuiltinEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:672
TupleConstr * constr
Definition: tupdesc.h:87
struct attrDefault AttrDefault
TupleDesc BuildDescForRelation(List *schema)
Definition: tupdesc.c:786
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:45
bool ccvalid
Definition: tupdesc.h:34
int tdrefcount
Definition: tupdesc.h:86
void TupleDescCopy(TupleDesc dst, TupleDesc src)
Definition: tupdesc.c:233
uint16 num_check
Definition: tupdesc.h:45
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
TupleDesc CreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs)
Definition: tupdesc.c:91