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-2016, 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 
19 #include "access/tupdesc.h"
20 #include "fmgr.h"
21 
22 
23 /* DomainConstraintCache is an opaque struct known only within typcache.c */
24 typedef struct DomainConstraintCache DomainConstraintCache;
25 
26 /* TypeCacheEnumData is an opaque struct known only within typcache.c */
27 struct TypeCacheEnumData;
28 
29 typedef struct TypeCacheEntry
30 {
31 	/* typeId is the hash lookup key and MUST BE FIRST */
32 	Oid			type_id;		/* OID of the data type */
33 
34 	/* some subsidiary information copied from the pg_type row */
35 	int16		typlen;
36 	bool		typbyval;
37 	char		typalign;
38 	char		typstorage;
39 	char		typtype;
40 	Oid			typrelid;
41 
42 	/*
43 	 * Information obtained from opfamily entries
44 	 *
45 	 * These will be InvalidOid if no match could be found, or if the
46 	 * information hasn't yet been requested.  Also note that for array and
47 	 * composite types, typcache.c checks that the contained types are
48 	 * comparable or hashable before allowing eq_opr etc to become set.
49 	 */
50 	Oid			btree_opf;		/* the default btree opclass' family */
51 	Oid			btree_opintype; /* the default btree opclass' opcintype */
52 	Oid			hash_opf;		/* the default hash opclass' family */
53 	Oid			hash_opintype;	/* the default hash opclass' opcintype */
54 	Oid			eq_opr;			/* the equality operator */
55 	Oid			lt_opr;			/* the less-than operator */
56 	Oid			gt_opr;			/* the greater-than operator */
57 	Oid			cmp_proc;		/* the btree comparison function */
58 	Oid			hash_proc;		/* the hash calculation function */
59 
60 	/*
61 	 * Pre-set-up fmgr call info for the equality operator, the btree
62 	 * comparison function, and the hash calculation function.  These are kept
63 	 * in the type cache to avoid problems with memory leaks in repeated calls
64 	 * to functions such as array_eq, array_cmp, hash_array.  There is not
65 	 * currently a need to maintain call info for the lt_opr or gt_opr.
66 	 */
67 	FmgrInfo	eq_opr_finfo;
68 	FmgrInfo	cmp_proc_finfo;
69 	FmgrInfo	hash_proc_finfo;
70 
71 	/*
72 	 * Tuple descriptor if it's a composite type (row type).  NULL if not
73 	 * composite or information hasn't yet been requested.  (NOTE: this is a
74 	 * reference-counted tupledesc.)
75 	 */
76 	TupleDesc	tupDesc;
77 
78 	/*
79 	 * Fields computed when TYPECACHE_RANGE_INFO is requested.  Zeroes if not
80 	 * a range type or information hasn't yet been requested.  Note that
81 	 * rng_cmp_proc_finfo could be different from the element type's default
82 	 * btree comparison function.
83 	 */
84 	struct TypeCacheEntry *rngelemtype; /* range's element type */
85 	Oid			rng_collation;	/* collation for comparisons, if any */
86 	FmgrInfo	rng_cmp_proc_finfo;		/* comparison function */
87 	FmgrInfo	rng_canonical_finfo;	/* canonicalization function, if any */
88 	FmgrInfo	rng_subdiff_finfo;		/* difference function, if any */
89 
90 	/*
91 	 * Domain constraint data if it's a domain type.  NULL if not domain, or
92 	 * if domain has no constraints, or if information hasn't been requested.
93 	 */
94 	DomainConstraintCache *domainData;
95 
96 	/* Private data, for internal use of typcache.c only */
97 	int			flags;			/* flags about what we've computed */
98 
99 	/*
100 	 * Private information about an enum type.  NULL if not enum or
101 	 * information hasn't been requested.
102 	 */
103 	struct TypeCacheEnumData *enumData;
104 
105 	/* We also maintain a list of all known domain-type cache entries */
106 	struct TypeCacheEntry *nextDomain;
107 } TypeCacheEntry;
108 
109 /* Bit flags to indicate which fields a given caller needs to have set */
110 #define TYPECACHE_EQ_OPR			0x0001
111 #define TYPECACHE_LT_OPR			0x0002
112 #define TYPECACHE_GT_OPR			0x0004
113 #define TYPECACHE_CMP_PROC			0x0008
114 #define TYPECACHE_HASH_PROC			0x0010
115 #define TYPECACHE_EQ_OPR_FINFO		0x0020
116 #define TYPECACHE_CMP_PROC_FINFO	0x0040
117 #define TYPECACHE_HASH_PROC_FINFO	0x0080
118 #define TYPECACHE_TUPDESC			0x0100
119 #define TYPECACHE_BTREE_OPFAMILY	0x0200
120 #define TYPECACHE_HASH_OPFAMILY		0x0400
121 #define TYPECACHE_RANGE_INFO		0x0800
122 #define TYPECACHE_DOMAIN_INFO		0x1000
123 
124 /*
125  * Callers wishing to maintain a long-lived reference to a domain's constraint
126  * set must store it in one of these.  Use InitDomainConstraintRef() and
127  * UpdateDomainConstraintRef() to manage it.  Note: DomainConstraintState is
128  * considered an executable expression type, so it's defined in execnodes.h.
129  */
130 typedef struct DomainConstraintRef
131 {
132 	List	   *constraints;	/* list of DomainConstraintState nodes */
133 	MemoryContext refctx;		/* context holding DomainConstraintRef */
134 	TypeCacheEntry *tcache;		/* typcache entry for domain type */
135 
136 	/* Management data --- treat these fields as private to typcache.c */
137 	DomainConstraintCache *dcc; /* current constraints, or NULL if none */
138 	MemoryContextCallback callback;		/* used to release refcount when done */
139 } DomainConstraintRef;
140 
141 
142 extern TypeCacheEntry *lookup_type_cache(Oid type_id, int flags);
143 
144 extern void InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref,
145 						MemoryContext refctx);
146 
147 extern void UpdateDomainConstraintRef(DomainConstraintRef *ref);
148 
149 extern bool DomainHasConstraints(Oid type_id);
150 
151 extern TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod);
152 
153 extern TupleDesc lookup_rowtype_tupdesc_noerror(Oid type_id, int32 typmod,
154 							   bool noError);
155 
156 extern TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod);
157 
158 extern void assign_record_type_typmod(TupleDesc tupDesc);
159 
160 extern int	compare_values_of_enum(TypeCacheEntry *tcache, Oid arg1, Oid arg2);
161 
162 #endif   /* TYPCACHE_H */
163