PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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  * Tuple descriptors that live in caches (relcache or typcache, at present)
64  * are reference-counted: they can be deleted when their reference count goes
65  * to zero. Tuple descriptors created by the executor need no reference
66  * counting, however: they are simply created in the appropriate memory
67  * context and go away when the context is freed. We set the tdrefcount
68  * field of such a descriptor to -1, while reference-counted descriptors
69  * always have tdrefcount >= 0.
70  */
71 typedef struct tupleDesc
72 {
73  int natts; /* number of attributes in the tuple */
74  Oid tdtypeid; /* composite type ID for tuple type */
75  int32 tdtypmod; /* typmod for tuple type */
76  bool tdhasoid; /* tuple has oid attribute in its header */
77  int tdrefcount; /* reference count, or -1 if not counting */
78  TupleConstr *constr; /* constraints, or NULL if none */
79  /* attrs[N] is the description of Attribute Number N+1 */
80  FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER];
81 } *TupleDesc;
82 
83 /* Accessor for the i'th attribute of tupdesc. */
84 #define TupleDescAttr(tupdesc, i) (&(tupdesc)->attrs[(i)])
85 
86 extern TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid);
87 
88 extern TupleDesc CreateTupleDesc(int natts, bool hasoid,
89  Form_pg_attribute *attrs);
90 
92 
94 
95 #define TupleDescSize(src) \
96  (offsetof(struct tupleDesc, attrs) + \
97  (src)->natts * sizeof(FormData_pg_attribute))
98 
99 extern void TupleDescCopy(TupleDesc dst, TupleDesc src);
100 
101 extern void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
102  TupleDesc src, AttrNumber srcAttno);
103 
104 extern void FreeTupleDesc(TupleDesc tupdesc);
105 
106 extern void IncrTupleDescRefCount(TupleDesc tupdesc);
107 extern void DecrTupleDescRefCount(TupleDesc tupdesc);
108 
109 #define PinTupleDesc(tupdesc) \
110  do { \
111  if ((tupdesc)->tdrefcount >= 0) \
112  IncrTupleDescRefCount(tupdesc); \
113  } while (0)
114 
115 #define ReleaseTupleDesc(tupdesc) \
116  do { \
117  if ((tupdesc)->tdrefcount >= 0) \
118  DecrTupleDescRefCount(tupdesc); \
119  } while (0)
120 
121 extern bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
122 
123 extern uint32 hashTupleDesc(TupleDesc tupdesc);
124 
125 extern void TupleDescInitEntry(TupleDesc desc,
126  AttrNumber attributeNumber,
127  const char *attributeName,
128  Oid oidtypeid,
129  int32 typmod,
130  int attdim);
131 
132 extern void TupleDescInitBuiltinEntry(TupleDesc desc,
133  AttrNumber attributeNumber,
134  const char *attributeName,
135  Oid oidtypeid,
136  int32 typmod,
137  int attdim);
138 
139 extern void TupleDescInitEntryCollation(TupleDesc desc,
140  AttrNumber attributeNumber,
141  Oid collationid);
142 
143 extern TupleDesc BuildDescForRelation(List *schema);
144 
145 extern TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations);
146 
147 #endif /* TUPDESC_H */
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:131
uint32 hashTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:475
Oid tdtypeid
Definition: tupdesc.h:74
char * ccname
Definition: tupdesc.h:30
bool tdhasoid
Definition: tupdesc.h:76
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:73
int32 tdtypmod
Definition: tupdesc.h:75
signed int int32
Definition: c.h:246
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:337
unsigned short uint16
Definition: c.h:257
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:258
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:78
struct attrDefault AttrDefault
TupleDesc BuildDescForRelation(List *schema)
Definition: tupdesc.c:689
FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER]
Definition: tupdesc.h:80
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:43
bool ccvalid
Definition: tupdesc.h:32
int tdrefcount
Definition: tupdesc.h:77
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