Header And Logo

PostgreSQL
| The world's most advanced open source database.

tupdesc.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * tupdesc.h
00004  *    POSTGRES tuple descriptor definitions.
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  * src/include/access/tupdesc.h
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef TUPDESC_H
00015 #define TUPDESC_H
00016 
00017 #include "access/attnum.h"
00018 #include "catalog/pg_attribute.h"
00019 #include "nodes/pg_list.h"
00020 
00021 
00022 typedef struct attrDefault
00023 {
00024     AttrNumber  adnum;
00025     char       *adbin;          /* nodeToString representation of expr */
00026 } AttrDefault;
00027 
00028 typedef struct constrCheck
00029 {
00030     char       *ccname;
00031     char       *ccbin;          /* nodeToString representation of expr */
00032     bool        ccvalid;
00033     bool        ccnoinherit;    /* this is a non-inheritable constraint */
00034 } ConstrCheck;
00035 
00036 /* This structure contains constraints of a tuple */
00037 typedef struct tupleConstr
00038 {
00039     AttrDefault *defval;        /* array */
00040     ConstrCheck *check;         /* array */
00041     uint16      num_defval;
00042     uint16      num_check;
00043     bool        has_not_null;
00044 } TupleConstr;
00045 
00046 /*
00047  * This struct is passed around within the backend to describe the structure
00048  * of tuples.  For tuples coming from on-disk relations, the information is
00049  * collected from the pg_attribute, pg_attrdef, and pg_constraint catalogs.
00050  * Transient row types (such as the result of a join query) have anonymous
00051  * TupleDesc structs that generally omit any constraint info; therefore the
00052  * structure is designed to let the constraints be omitted efficiently.
00053  *
00054  * Note that only user attributes, not system attributes, are mentioned in
00055  * TupleDesc; with the exception that tdhasoid indicates if OID is present.
00056  *
00057  * If the tupdesc is known to correspond to a named rowtype (such as a table's
00058  * rowtype) then tdtypeid identifies that type and tdtypmod is -1.  Otherwise
00059  * tdtypeid is RECORDOID, and tdtypmod can be either -1 for a fully anonymous
00060  * row type, or a value >= 0 to allow the rowtype to be looked up in the
00061  * typcache.c type cache.
00062  *
00063  * Tuple descriptors that live in caches (relcache or typcache, at present)
00064  * are reference-counted: they can be deleted when their reference count goes
00065  * to zero.  Tuple descriptors created by the executor need no reference
00066  * counting, however: they are simply created in the appropriate memory
00067  * context and go away when the context is freed.  We set the tdrefcount
00068  * field of such a descriptor to -1, while reference-counted descriptors
00069  * always have tdrefcount >= 0.
00070  */
00071 typedef struct tupleDesc
00072 {
00073     int         natts;          /* number of attributes in the tuple */
00074     Form_pg_attribute *attrs;
00075     /* attrs[N] is a pointer to the description of Attribute Number N+1 */
00076     TupleConstr *constr;        /* constraints, or NULL if none */
00077     Oid         tdtypeid;       /* composite type ID for tuple type */
00078     int32       tdtypmod;       /* typmod for tuple type */
00079     bool        tdhasoid;       /* tuple has oid attribute in its header */
00080     int         tdrefcount;     /* reference count, or -1 if not counting */
00081 }   *TupleDesc;
00082 
00083 
00084 extern TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid);
00085 
00086 extern TupleDesc CreateTupleDesc(int natts, bool hasoid,
00087                 Form_pg_attribute *attrs);
00088 
00089 extern TupleDesc CreateTupleDescCopy(TupleDesc tupdesc);
00090 
00091 extern TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc);
00092 
00093 extern void FreeTupleDesc(TupleDesc tupdesc);
00094 
00095 extern void IncrTupleDescRefCount(TupleDesc tupdesc);
00096 extern void DecrTupleDescRefCount(TupleDesc tupdesc);
00097 
00098 #define PinTupleDesc(tupdesc) \
00099     do { \
00100         if ((tupdesc)->tdrefcount >= 0) \
00101             IncrTupleDescRefCount(tupdesc); \
00102     } while (0)
00103 
00104 #define ReleaseTupleDesc(tupdesc) \
00105     do { \
00106         if ((tupdesc)->tdrefcount >= 0) \
00107             DecrTupleDescRefCount(tupdesc); \
00108     } while (0)
00109 
00110 extern bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
00111 
00112 extern void TupleDescInitEntry(TupleDesc desc,
00113                    AttrNumber attributeNumber,
00114                    const char *attributeName,
00115                    Oid oidtypeid,
00116                    int32 typmod,
00117                    int attdim);
00118 
00119 extern void TupleDescInitEntryCollation(TupleDesc desc,
00120                             AttrNumber attributeNumber,
00121                             Oid collationid);
00122 
00123 extern TupleDesc BuildDescForRelation(List *schema);
00124 
00125 extern TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations);
00126 
00127 #endif   /* TUPDESC_H */