tupdesc.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*-------------------------------------------------------------------------
  2. *
  3. * tupdesc.h
  4. * POSTGRES tuple descriptor definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, 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. struct AttrMissing *missing; /* missing attributes values, NULL if none */
  37. uint16 num_defval;
  38. uint16 num_check;
  39. bool has_not_null;
  40. bool has_generated_stored;
  41. } TupleConstr;
  42. /*
  43. * This struct is passed around within the backend to describe the structure
  44. * of tuples. For tuples coming from on-disk relations, the information is
  45. * collected from the pg_attribute, pg_attrdef, and pg_constraint catalogs.
  46. * Transient row types (such as the result of a join query) have anonymous
  47. * TupleDesc structs that generally omit any constraint info; therefore the
  48. * structure is designed to let the constraints be omitted efficiently.
  49. *
  50. * Note that only user attributes, not system attributes, are mentioned in
  51. * TupleDesc.
  52. *
  53. * If the tupdesc is known to correspond to a named rowtype (such as a table's
  54. * rowtype) then tdtypeid identifies that type and tdtypmod is -1. Otherwise
  55. * tdtypeid is RECORDOID, and tdtypmod can be either -1 for a fully anonymous
  56. * row type, or a value >= 0 to allow the rowtype to be looked up in the
  57. * typcache.c type cache.
  58. *
  59. * Note that tdtypeid is never the OID of a domain over composite, even if
  60. * we are dealing with values that are known (at some higher level) to be of
  61. * a domain-over-composite type. This is because tdtypeid/tdtypmod need to
  62. * match up with the type labeling of composite Datums, and those are never
  63. * explicitly marked as being of a domain type, either.
  64. *
  65. * Tuple descriptors that live in caches (relcache or typcache, at present)
  66. * are reference-counted: they can be deleted when their reference count goes
  67. * to zero. Tuple descriptors created by the executor need no reference
  68. * counting, however: they are simply created in the appropriate memory
  69. * context and go away when the context is freed. We set the tdrefcount
  70. * field of such a descriptor to -1, while reference-counted descriptors
  71. * always have tdrefcount >= 0.
  72. */
  73. typedef struct TupleDescData
  74. {
  75. int natts; /* number of attributes in the tuple */
  76. Oid tdtypeid; /* composite type ID for tuple type */
  77. int32 tdtypmod; /* typmod for tuple type */
  78. int tdrefcount; /* reference count, or -1 if not counting */
  79. TupleConstr *constr; /* constraints, or NULL if none */
  80. /* attrs[N] is the description of Attribute Number N+1 */
  81. FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER];
  82. } TupleDescData;
  83. typedef struct TupleDescData *TupleDesc;
  84. /* Accessor for the i'th attribute of tupdesc. */
  85. #define TupleDescAttr(tupdesc, i) (&(tupdesc)->attrs[(i)])
  86. extern TupleDesc CreateTemplateTupleDesc(int natts);
  87. extern TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs);
  88. extern TupleDesc CreateTupleDescCopy(TupleDesc tupdesc);
  89. extern TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc);
  90. #define TupleDescSize(src) \
  91. (offsetof(struct TupleDescData, attrs) + \
  92. (src)->natts * sizeof(FormData_pg_attribute))
  93. extern void TupleDescCopy(TupleDesc dst, TupleDesc src);
  94. extern void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
  95. TupleDesc src, AttrNumber srcAttno);
  96. extern void FreeTupleDesc(TupleDesc tupdesc);
  97. extern void IncrTupleDescRefCount(TupleDesc tupdesc);
  98. extern void DecrTupleDescRefCount(TupleDesc tupdesc);
  99. #define PinTupleDesc(tupdesc) \
  100. do { \
  101. if ((tupdesc)->tdrefcount >= 0) \
  102. IncrTupleDescRefCount(tupdesc); \
  103. } while (0)
  104. #define ReleaseTupleDesc(tupdesc) \
  105. do { \
  106. if ((tupdesc)->tdrefcount >= 0) \
  107. DecrTupleDescRefCount(tupdesc); \
  108. } while (0)
  109. extern bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
  110. extern uint32 hashTupleDesc(TupleDesc tupdesc);
  111. extern void TupleDescInitEntry(TupleDesc desc,
  112. AttrNumber attributeNumber,
  113. const char *attributeName,
  114. Oid oidtypeid,
  115. int32 typmod,
  116. int attdim);
  117. extern void TupleDescInitBuiltinEntry(TupleDesc desc,
  118. AttrNumber attributeNumber,
  119. const char *attributeName,
  120. Oid oidtypeid,
  121. int32 typmod,
  122. int attdim);
  123. extern void TupleDescInitEntryCollation(TupleDesc desc,
  124. AttrNumber attributeNumber,
  125. Oid collationid);
  126. extern TupleDesc BuildDescForRelation(List *schema);
  127. extern TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations);
  128. #endif /* TUPDESC_H */