2
0

typcache.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*-------------------------------------------------------------------------
  2. *
  3. * typcache.h
  4. * Type cache definitions.
  5. *
  6. * The type cache exists to speed lookup of certain information about data
  7. * types that is not directly available from a type's pg_type row.
  8. *
  9. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  10. * Portions Copyright (c) 1994, Regents of the University of California
  11. *
  12. * src/include/utils/typcache.h
  13. *
  14. *-------------------------------------------------------------------------
  15. */
  16. #ifndef TYPCACHE_H
  17. #define TYPCACHE_H
  18. #include "access/tupdesc.h"
  19. #include "fmgr.h"
  20. #include "storage/dsm.h"
  21. #include "utils/dsa.h"
  22. /* DomainConstraintCache is an opaque struct known only within typcache.c */
  23. typedef struct DomainConstraintCache DomainConstraintCache;
  24. /* TypeCacheEnumData is an opaque struct known only within typcache.c */
  25. struct TypeCacheEnumData;
  26. typedef struct TypeCacheEntry
  27. {
  28. /* typeId is the hash lookup key and MUST BE FIRST */
  29. Oid type_id; /* OID of the data type */
  30. uint32 type_id_hash; /* hashed value of the OID */
  31. /* some subsidiary information copied from the pg_type row */
  32. int16 typlen;
  33. bool typbyval;
  34. char typalign;
  35. char typstorage;
  36. char typtype;
  37. Oid typrelid;
  38. Oid typsubscript;
  39. Oid typelem;
  40. Oid typcollation;
  41. /*
  42. * Information obtained from opfamily entries
  43. *
  44. * These will be InvalidOid if no match could be found, or if the
  45. * information hasn't yet been requested. Also note that for array and
  46. * composite types, typcache.c checks that the contained types are
  47. * comparable or hashable before allowing eq_opr etc to become set.
  48. */
  49. Oid btree_opf; /* the default btree opclass' family */
  50. Oid btree_opintype; /* the default btree opclass' opcintype */
  51. Oid hash_opf; /* the default hash opclass' family */
  52. Oid hash_opintype; /* the default hash opclass' opcintype */
  53. Oid eq_opr; /* the equality operator */
  54. Oid lt_opr; /* the less-than operator */
  55. Oid gt_opr; /* the greater-than operator */
  56. Oid cmp_proc; /* the btree comparison function */
  57. Oid hash_proc; /* the hash calculation function */
  58. Oid hash_extended_proc; /* the extended hash calculation function */
  59. /*
  60. * Pre-set-up fmgr call info for the equality operator, the btree
  61. * comparison function, and the hash calculation function. These are kept
  62. * in the type cache to avoid problems with memory leaks in repeated calls
  63. * to functions such as array_eq, array_cmp, hash_array. There is not
  64. * currently a need to maintain call info for the lt_opr or gt_opr.
  65. */
  66. FmgrInfo eq_opr_finfo;
  67. FmgrInfo cmp_proc_finfo;
  68. FmgrInfo hash_proc_finfo;
  69. FmgrInfo hash_extended_proc_finfo;
  70. /*
  71. * Tuple descriptor if it's a composite type (row type). NULL if not
  72. * composite or information hasn't yet been requested. (NOTE: this is a
  73. * reference-counted tupledesc.)
  74. *
  75. * To simplify caching dependent info, tupDesc_identifier is an identifier
  76. * for this tupledesc that is unique for the life of the process, and
  77. * changes anytime the tupledesc does. Zero if not yet determined.
  78. */
  79. TupleDesc tupDesc;
  80. uint64 tupDesc_identifier;
  81. /*
  82. * Fields computed when TYPECACHE_RANGE_INFO is requested. Zeroes if not
  83. * a range type or information hasn't yet been requested. Note that
  84. * rng_cmp_proc_finfo could be different from the element type's default
  85. * btree comparison function.
  86. */
  87. struct TypeCacheEntry *rngelemtype; /* range's element type */
  88. Oid rng_collation; /* collation for comparisons, if any */
  89. FmgrInfo rng_cmp_proc_finfo; /* comparison function */
  90. FmgrInfo rng_canonical_finfo; /* canonicalization function, if any */
  91. FmgrInfo rng_subdiff_finfo; /* difference function, if any */
  92. /*
  93. * Fields computed when TYPECACHE_MULTIRANGE_INFO is required.
  94. */
  95. struct TypeCacheEntry *rngtype; /* multirange's range underlying type */
  96. /*
  97. * Domain's base type and typmod if it's a domain type. Zeroes if not
  98. * domain, or if information hasn't been requested.
  99. */
  100. Oid domainBaseType;
  101. int32 domainBaseTypmod;
  102. /*
  103. * Domain constraint data if it's a domain type. NULL if not domain, or
  104. * if domain has no constraints, or if information hasn't been requested.
  105. */
  106. DomainConstraintCache *domainData;
  107. /* Private data, for internal use of typcache.c only */
  108. int flags; /* flags about what we've computed */
  109. /*
  110. * Private information about an enum type. NULL if not enum or
  111. * information hasn't been requested.
  112. */
  113. struct TypeCacheEnumData *enumData;
  114. /* We also maintain a list of all known domain-type cache entries */
  115. struct TypeCacheEntry *nextDomain;
  116. } TypeCacheEntry;
  117. /* Bit flags to indicate which fields a given caller needs to have set */
  118. #define TYPECACHE_EQ_OPR 0x00001
  119. #define TYPECACHE_LT_OPR 0x00002
  120. #define TYPECACHE_GT_OPR 0x00004
  121. #define TYPECACHE_CMP_PROC 0x00008
  122. #define TYPECACHE_HASH_PROC 0x00010
  123. #define TYPECACHE_EQ_OPR_FINFO 0x00020
  124. #define TYPECACHE_CMP_PROC_FINFO 0x00040
  125. #define TYPECACHE_HASH_PROC_FINFO 0x00080
  126. #define TYPECACHE_TUPDESC 0x00100
  127. #define TYPECACHE_BTREE_OPFAMILY 0x00200
  128. #define TYPECACHE_HASH_OPFAMILY 0x00400
  129. #define TYPECACHE_RANGE_INFO 0x00800
  130. #define TYPECACHE_DOMAIN_BASE_INFO 0x01000
  131. #define TYPECACHE_DOMAIN_CONSTR_INFO 0x02000
  132. #define TYPECACHE_HASH_EXTENDED_PROC 0x04000
  133. #define TYPECACHE_HASH_EXTENDED_PROC_FINFO 0x08000
  134. #define TYPECACHE_MULTIRANGE_INFO 0x10000
  135. /* This value will not equal any valid tupledesc identifier, nor 0 */
  136. #define INVALID_TUPLEDESC_IDENTIFIER ((uint64) 1)
  137. /*
  138. * Callers wishing to maintain a long-lived reference to a domain's constraint
  139. * set must store it in one of these. Use InitDomainConstraintRef() and
  140. * UpdateDomainConstraintRef() to manage it. Note: DomainConstraintState is
  141. * considered an executable expression type, so it's defined in execnodes.h.
  142. */
  143. typedef struct DomainConstraintRef
  144. {
  145. List *constraints; /* list of DomainConstraintState nodes */
  146. MemoryContext refctx; /* context holding DomainConstraintRef */
  147. TypeCacheEntry *tcache; /* typcache entry for domain type */
  148. bool need_exprstate; /* does caller need check_exprstate? */
  149. /* Management data --- treat these fields as private to typcache.c */
  150. DomainConstraintCache *dcc; /* current constraints, or NULL if none */
  151. MemoryContextCallback callback; /* used to release refcount when done */
  152. } DomainConstraintRef;
  153. typedef struct SharedRecordTypmodRegistry SharedRecordTypmodRegistry;
  154. extern TypeCacheEntry *lookup_type_cache(Oid type_id, int flags);
  155. extern void InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref,
  156. MemoryContext refctx, bool need_exprstate);
  157. extern void UpdateDomainConstraintRef(DomainConstraintRef *ref);
  158. extern bool DomainHasConstraints(Oid type_id);
  159. extern TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod);
  160. extern TupleDesc lookup_rowtype_tupdesc_noerror(Oid type_id, int32 typmod,
  161. bool noError);
  162. extern TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod);
  163. extern TupleDesc lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod,
  164. bool noError);
  165. extern void assign_record_type_typmod(TupleDesc tupDesc);
  166. extern uint64 assign_record_type_identifier(Oid type_id, int32 typmod);
  167. extern int compare_values_of_enum(TypeCacheEntry *tcache, Oid arg1, Oid arg2);
  168. extern size_t SharedRecordTypmodRegistryEstimate(void);
  169. extern void SharedRecordTypmodRegistryInit(SharedRecordTypmodRegistry *,
  170. dsm_segment *segment, dsa_area *area);
  171. extern void SharedRecordTypmodRegistryAttach(SharedRecordTypmodRegistry *);
  172. #endif /* TYPCACHE_H */