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