tupdesc.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*-------------------------------------------------------------------------
  2. *
  3. * tupdesc.h
  4. * POSTGRES tuple descriptor definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, 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. #include "access/attnum.h"
  17. #include "catalog/pg_attribute.h"
  18. #include "nodes/pg_list.h"
  19. typedef struct attrDefault
  20. {
  21. AttrNumber adnum;
  22. char *adbin; /* nodeToString representation of expr */
  23. } AttrDefault;
  24. typedef struct constrCheck
  25. {
  26. char *ccname;
  27. char *ccbin; /* nodeToString representation of expr */
  28. bool ccvalid;
  29. bool ccnoinherit; /* this is a non-inheritable constraint */
  30. } ConstrCheck;
  31. /* This structure contains constraints of a tuple */
  32. typedef struct tupleConstr
  33. {
  34. AttrDefault *defval; /* array */
  35. ConstrCheck *check; /* array */
  36. uint16 num_defval;
  37. uint16 num_check;
  38. bool has_not_null;
  39. } TupleConstr;
  40. /*
  41. * This struct is passed around within the backend to describe the structure
  42. * of tuples. For tuples coming from on-disk relations, the information is
  43. * collected from the pg_attribute, pg_attrdef, and pg_constraint catalogs.
  44. * Transient row types (such as the result of a join query) have anonymous
  45. * TupleDesc structs that generally omit any constraint info; therefore the
  46. * structure is designed to let the constraints be omitted efficiently.
  47. *
  48. * Note that only user attributes, not system attributes, are mentioned in
  49. * TupleDesc; with the exception that tdhasoid indicates if OID is present.
  50. *
  51. * If the tupdesc is known to correspond to a named rowtype (such as a table's
  52. * rowtype) then tdtypeid identifies that type and tdtypmod is -1. Otherwise
  53. * tdtypeid is RECORDOID, and tdtypmod can be either -1 for a fully anonymous
  54. * row type, or a value >= 0 to allow the rowtype to be looked up in the
  55. * typcache.c type cache.
  56. *
  57. * Tuple descriptors that live in caches (relcache or typcache, at present)
  58. * are reference-counted: they can be deleted when their reference count goes
  59. * to zero. Tuple descriptors created by the executor need no reference
  60. * counting, however: they are simply created in the appropriate memory
  61. * context and go away when the context is freed. We set the tdrefcount
  62. * field of such a descriptor to -1, while reference-counted descriptors
  63. * always have tdrefcount >= 0.
  64. */
  65. typedef struct tupleDesc
  66. {
  67. int natts; /* number of attributes in the tuple */
  68. Form_pg_attribute *attrs;
  69. /* attrs[N] is a pointer to the description of Attribute Number N+1 */
  70. TupleConstr *constr; /* constraints, or NULL if none */
  71. Oid tdtypeid; /* composite type ID for tuple type */
  72. int32 tdtypmod; /* typmod for tuple type */
  73. bool tdhasoid; /* tuple has oid attribute in its header */
  74. int tdrefcount; /* reference count, or -1 if not counting */
  75. } *TupleDesc;
  76. /* Accessor for the i'th attribute of tupdesc. */
  77. #define TupleDescAttr(tupdesc, i) ((tupdesc)->attrs[(i)])
  78. extern TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid);
  79. extern TupleDesc CreateTupleDesc(int natts, bool hasoid,
  80. Form_pg_attribute *attrs);
  81. extern TupleDesc CreateTupleDescCopy(TupleDesc tupdesc);
  82. extern TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc);
  83. extern void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
  84. TupleDesc src, AttrNumber srcAttno);
  85. extern void FreeTupleDesc(TupleDesc tupdesc);
  86. extern void IncrTupleDescRefCount(TupleDesc tupdesc);
  87. extern void DecrTupleDescRefCount(TupleDesc tupdesc);
  88. #define PinTupleDesc(tupdesc) \
  89. do { \
  90. if ((tupdesc)->tdrefcount >= 0) \
  91. IncrTupleDescRefCount(tupdesc); \
  92. } while (0)
  93. #define ReleaseTupleDesc(tupdesc) \
  94. do { \
  95. if ((tupdesc)->tdrefcount >= 0) \
  96. DecrTupleDescRefCount(tupdesc); \
  97. } while (0)
  98. extern bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
  99. extern void TupleDescInitEntry(TupleDesc desc,
  100. AttrNumber attributeNumber,
  101. const char *attributeName,
  102. Oid oidtypeid,
  103. int32 typmod,
  104. int attdim);
  105. extern void TupleDescInitEntryCollation(TupleDesc desc,
  106. AttrNumber attributeNumber,
  107. Oid collationid);
  108. extern TupleDesc BuildDescForRelation(List *schema);
  109. extern TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations);
  110. #endif /* TUPDESC_H */