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 */
75  /* attrs[N] is a pointer to the description of Attribute Number N+1 */
76  TupleConstr *constr; /* constraints, or NULL if none */
77  Oid tdtypeid; /* composite type ID for tuple type */
78  int32 tdtypmod; /* typmod for tuple type */
79  bool tdhasoid; /* tuple has oid attribute in its header */
80  int tdrefcount; /* reference count, or -1 if not counting */
81 } *TupleDesc;
82 
83 
84 extern TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid);
85 
86 extern TupleDesc CreateTupleDesc(int natts, bool hasoid,
87  Form_pg_attribute *attrs);
88 
90 
92 
93 extern void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
94  TupleDesc src, AttrNumber srcAttno);
95 
96 extern void FreeTupleDesc(TupleDesc tupdesc);
97 
98 extern void IncrTupleDescRefCount(TupleDesc tupdesc);
99 extern void DecrTupleDescRefCount(TupleDesc tupdesc);
100 
101 #define PinTupleDesc(tupdesc) \
102  do { \
103  if ((tupdesc)->tdrefcount >= 0) \
104  IncrTupleDescRefCount(tupdesc); \
105  } while (0)
106 
107 #define ReleaseTupleDesc(tupdesc) \
108  do { \
109  if ((tupdesc)->tdrefcount >= 0) \
110  DecrTupleDescRefCount(tupdesc); \
111  } while (0)
112 
113 extern bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
114 
115 extern void TupleDescInitEntry(TupleDesc desc,
116  AttrNumber attributeNumber,
117  const char *attributeName,
118  Oid oidtypeid,
119  int32 typmod,
120  int attdim);
121 
122 extern void TupleDescInitBuiltinEntry(TupleDesc desc,
123  AttrNumber attributeNumber,
124  const char *attributeName,
125  Oid oidtypeid,
126  int32 typmod,
127  int attdim);
128 
129 extern void TupleDescInitEntryCollation(TupleDesc desc,
130  AttrNumber attributeNumber,
131  Oid collationid);
132 
133 extern TupleDesc BuildDescForRelation(List *schema);
134 
135 extern TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations);
136 
137 #endif /* TUPDESC_H */
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:167
Oid tdtypeid
Definition: tupdesc.h:77
char * ccname
Definition: tupdesc.h:30
bool tdhasoid
Definition: tupdesc.h:79
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:229
struct typedefs * types
Definition: ecpg.c:29
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:650
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:266
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:316
Form_pg_attribute * attrs
Definition: tupdesc.h:74
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:334
struct tupleConstr TupleConstr
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
int32 tdtypmod
Definition: tupdesc.h:78
signed int int32
Definition: c.h:253
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:352
unsigned short uint16
Definition: c.h:264
AttrDefault * defval
Definition: tupdesc.h:39
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:771
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:493
AttrNumber adnum
Definition: tupdesc.h:24
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
char * adbin
Definition: tupdesc.h:25
struct tupleDesc * TupleDesc
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
bool has_not_null
Definition: tupdesc.h:43
struct constrCheck ConstrCheck
uint16 num_defval
Definition: tupdesc.h:41
void TupleDescInitBuiltinEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:563
TupleConstr * constr
Definition: tupdesc.h:76
struct attrDefault AttrDefault
TupleDesc BuildDescForRelation(List *schema)
Definition: tupdesc.c:675
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
bool ccvalid
Definition: tupdesc.h:32
int tdrefcount
Definition: tupdesc.h:80
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:112