1 /*-------------------------------------------------------------------------
2  *
3  * typcache.c
4  *	  POSTGRES type cache code
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.  For
8  * example, we use a type's default btree opclass, or the default hash
9  * opclass if no btree opclass exists, to determine which operators should
10  * be used for grouping and sorting the type (GROUP BY, ORDER BY ASC/DESC).
11  *
12  * Several seemingly-odd choices have been made to support use of the type
13  * cache by generic array and record handling routines, such as array_eq(),
14  * record_cmp(), and hash_array().  Because those routines are used as index
15  * support operations, they cannot leak memory.  To allow them to execute
16  * efficiently, all information that they would like to re-use across calls
17  * is kept in the type cache.
18  *
19  * Once created, a type cache entry lives as long as the backend does, so
20  * there is no need for a call to release a cache entry.  If the type is
21  * dropped, the cache entry simply becomes wasted storage.  This is not
22  * expected to happen often, and assuming that typcache entries are good
23  * permanently allows caching pointers to them in long-lived places.
24  *
25  * We have some provisions for updating cache entries if the stored data
26  * becomes obsolete.  Core data extracted from the pg_type row is updated
27  * when we detect updates to pg_type.  Information dependent on opclasses is
28  * cleared if we detect updates to pg_opclass.  We also support clearing the
29  * tuple descriptor and operator/function parts of a rowtype's cache entry,
30  * since those may need to change as a consequence of ALTER TABLE.  Domain
31  * constraint changes are also tracked properly.
32  *
33  *
34  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
35  * Portions Copyright (c) 1994, Regents of the University of California
36  *
37  * IDENTIFICATION
38  *	  src/backend/utils/cache/typcache.c
39  *
40  *-------------------------------------------------------------------------
41  */
42 #include "postgres.h"
43 
44 #include <limits.h>
45 
46 #include "access/hash.h"
47 #include "access/htup_details.h"
48 #include "access/nbtree.h"
49 #include "access/parallel.h"
50 #include "access/relation.h"
51 #include "access/session.h"
52 #include "access/table.h"
53 #include "catalog/indexing.h"
54 #include "catalog/pg_am.h"
55 #include "catalog/pg_constraint.h"
56 #include "catalog/pg_enum.h"
57 #include "catalog/pg_operator.h"
58 #include "catalog/pg_range.h"
59 #include "catalog/pg_type.h"
60 #include "commands/defrem.h"
61 #include "executor/executor.h"
62 #include "lib/dshash.h"
63 #include "optimizer/optimizer.h"
64 #include "storage/lwlock.h"
65 #include "utils/builtins.h"
66 #include "utils/catcache.h"
67 #include "utils/fmgroids.h"
68 #include "utils/inval.h"
69 #include "utils/lsyscache.h"
70 #include "utils/memutils.h"
71 #include "utils/rel.h"
72 #include "utils/snapmgr.h"
73 #include "utils/syscache.h"
74 #include "utils/typcache.h"
75 
76 
77 /* The main type cache hashtable searched by lookup_type_cache */
78 static HTAB *TypeCacheHash = NULL;
79 
80 /* List of type cache entries for domain types */
81 static TypeCacheEntry *firstDomainTypeEntry = NULL;
82 
83 /* Private flag bits in the TypeCacheEntry.flags field */
84 #define TCFLAGS_HAVE_PG_TYPE_DATA			0x000001
85 #define TCFLAGS_CHECKED_BTREE_OPCLASS		0x000002
86 #define TCFLAGS_CHECKED_HASH_OPCLASS		0x000004
87 #define TCFLAGS_CHECKED_EQ_OPR				0x000008
88 #define TCFLAGS_CHECKED_LT_OPR				0x000010
89 #define TCFLAGS_CHECKED_GT_OPR				0x000020
90 #define TCFLAGS_CHECKED_CMP_PROC			0x000040
91 #define TCFLAGS_CHECKED_HASH_PROC			0x000080
92 #define TCFLAGS_CHECKED_HASH_EXTENDED_PROC	0x000100
93 #define TCFLAGS_CHECKED_ELEM_PROPERTIES		0x000200
94 #define TCFLAGS_HAVE_ELEM_EQUALITY			0x000400
95 #define TCFLAGS_HAVE_ELEM_COMPARE			0x000800
96 #define TCFLAGS_HAVE_ELEM_HASHING			0x001000
97 #define TCFLAGS_HAVE_ELEM_EXTENDED_HASHING	0x002000
98 #define TCFLAGS_CHECKED_FIELD_PROPERTIES	0x004000
99 #define TCFLAGS_HAVE_FIELD_EQUALITY			0x008000
100 #define TCFLAGS_HAVE_FIELD_COMPARE			0x010000
101 #define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS	0x020000
102 #define TCFLAGS_DOMAIN_BASE_IS_COMPOSITE	0x040000
103 
104 /* The flags associated with equality/comparison/hashing are all but these: */
105 #define TCFLAGS_OPERATOR_FLAGS \
106 	(~(TCFLAGS_HAVE_PG_TYPE_DATA | \
107 	   TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS | \
108 	   TCFLAGS_DOMAIN_BASE_IS_COMPOSITE))
109 
110 /*
111  * Data stored about a domain type's constraints.  Note that we do not create
112  * this struct for the common case of a constraint-less domain; we just set
113  * domainData to NULL to indicate that.
114  *
115  * Within a DomainConstraintCache, we store expression plan trees, but the
116  * check_exprstate fields of the DomainConstraintState nodes are just NULL.
117  * When needed, expression evaluation nodes are built by flat-copying the
118  * DomainConstraintState nodes and applying ExecInitExpr to check_expr.
119  * Such a node tree is not part of the DomainConstraintCache, but is
120  * considered to belong to a DomainConstraintRef.
121  */
122 struct DomainConstraintCache
123 {
124 	List	   *constraints;	/* list of DomainConstraintState nodes */
125 	MemoryContext dccContext;	/* memory context holding all associated data */
126 	long		dccRefCount;	/* number of references to this struct */
127 };
128 
129 /* Private information to support comparisons of enum values */
130 typedef struct
131 {
132 	Oid			enum_oid;		/* OID of one enum value */
133 	float4		sort_order;		/* its sort position */
134 } EnumItem;
135 
136 typedef struct TypeCacheEnumData
137 {
138 	Oid			bitmap_base;	/* OID corresponding to bit 0 of bitmapset */
139 	Bitmapset  *sorted_values;	/* Set of OIDs known to be in order */
140 	int			num_values;		/* total number of values in enum */
141 	EnumItem	enum_values[FLEXIBLE_ARRAY_MEMBER];
142 } TypeCacheEnumData;
143 
144 /*
145  * We use a separate table for storing the definitions of non-anonymous
146  * record types.  Once defined, a record type will be remembered for the
147  * life of the backend.  Subsequent uses of the "same" record type (where
148  * sameness means equalTupleDescs) will refer to the existing table entry.
149  *
150  * Stored record types are remembered in a linear array of TupleDescs,
151  * which can be indexed quickly with the assigned typmod.  There is also
152  * a hash table to speed searches for matching TupleDescs.
153  */
154 
155 typedef struct RecordCacheEntry
156 {
157 	TupleDesc	tupdesc;
158 } RecordCacheEntry;
159 
160 /*
161  * To deal with non-anonymous record types that are exchanged by backends
162  * involved in a parallel query, we also need a shared version of the above.
163  */
164 struct SharedRecordTypmodRegistry
165 {
166 	/* A hash table for finding a matching TupleDesc. */
167 	dshash_table_handle record_table_handle;
168 	/* A hash table for finding a TupleDesc by typmod. */
169 	dshash_table_handle typmod_table_handle;
170 	/* A source of new record typmod numbers. */
171 	pg_atomic_uint32 next_typmod;
172 };
173 
174 /*
175  * When using shared tuple descriptors as hash table keys we need a way to be
176  * able to search for an equal shared TupleDesc using a backend-local
177  * TupleDesc.  So we use this type which can hold either, and hash and compare
178  * functions that know how to handle both.
179  */
180 typedef struct SharedRecordTableKey
181 {
182 	union
183 	{
184 		TupleDesc	local_tupdesc;
185 		dsa_pointer shared_tupdesc;
186 	}			u;
187 	bool		shared;
188 } SharedRecordTableKey;
189 
190 /*
191  * The shared version of RecordCacheEntry.  This lets us look up a typmod
192  * using a TupleDesc which may be in local or shared memory.
193  */
194 typedef struct SharedRecordTableEntry
195 {
196 	SharedRecordTableKey key;
197 } SharedRecordTableEntry;
198 
199 /*
200  * An entry in SharedRecordTypmodRegistry's typmod table.  This lets us look
201  * up a TupleDesc in shared memory using a typmod.
202  */
203 typedef struct SharedTypmodTableEntry
204 {
205 	uint32		typmod;
206 	dsa_pointer shared_tupdesc;
207 } SharedTypmodTableEntry;
208 
209 /*
210  * A comparator function for SharedRecordTableKey.
211  */
212 static int
shared_record_table_compare(const void * a,const void * b,size_t size,void * arg)213 shared_record_table_compare(const void *a, const void *b, size_t size,
214 							void *arg)
215 {
216 	dsa_area   *area = (dsa_area *) arg;
217 	SharedRecordTableKey *k1 = (SharedRecordTableKey *) a;
218 	SharedRecordTableKey *k2 = (SharedRecordTableKey *) b;
219 	TupleDesc	t1;
220 	TupleDesc	t2;
221 
222 	if (k1->shared)
223 		t1 = (TupleDesc) dsa_get_address(area, k1->u.shared_tupdesc);
224 	else
225 		t1 = k1->u.local_tupdesc;
226 
227 	if (k2->shared)
228 		t2 = (TupleDesc) dsa_get_address(area, k2->u.shared_tupdesc);
229 	else
230 		t2 = k2->u.local_tupdesc;
231 
232 	return equalTupleDescs(t1, t2) ? 0 : 1;
233 }
234 
235 /*
236  * A hash function for SharedRecordTableKey.
237  */
238 static uint32
shared_record_table_hash(const void * a,size_t size,void * arg)239 shared_record_table_hash(const void *a, size_t size, void *arg)
240 {
241 	dsa_area   *area = (dsa_area *) arg;
242 	SharedRecordTableKey *k = (SharedRecordTableKey *) a;
243 	TupleDesc	t;
244 
245 	if (k->shared)
246 		t = (TupleDesc) dsa_get_address(area, k->u.shared_tupdesc);
247 	else
248 		t = k->u.local_tupdesc;
249 
250 	return hashTupleDesc(t);
251 }
252 
253 /* Parameters for SharedRecordTypmodRegistry's TupleDesc table. */
254 static const dshash_parameters srtr_record_table_params = {
255 	sizeof(SharedRecordTableKey),	/* unused */
256 	sizeof(SharedRecordTableEntry),
257 	shared_record_table_compare,
258 	shared_record_table_hash,
259 	LWTRANCHE_PER_SESSION_RECORD_TYPE
260 };
261 
262 /* Parameters for SharedRecordTypmodRegistry's typmod hash table. */
263 static const dshash_parameters srtr_typmod_table_params = {
264 	sizeof(uint32),
265 	sizeof(SharedTypmodTableEntry),
266 	dshash_memcmp,
267 	dshash_memhash,
268 	LWTRANCHE_PER_SESSION_RECORD_TYPMOD
269 };
270 
271 /* hashtable for recognizing registered record types */
272 static HTAB *RecordCacheHash = NULL;
273 
274 /* arrays of info about registered record types, indexed by assigned typmod */
275 static TupleDesc *RecordCacheArray = NULL;
276 static uint64 *RecordIdentifierArray = NULL;
277 static int32 RecordCacheArrayLen = 0;	/* allocated length of above arrays */
278 static int32 NextRecordTypmod = 0;	/* number of entries used */
279 
280 /*
281  * Process-wide counter for generating unique tupledesc identifiers.
282  * Zero and one (INVALID_TUPLEDESC_IDENTIFIER) aren't allowed to be chosen
283  * as identifiers, so we start the counter at INVALID_TUPLEDESC_IDENTIFIER.
284  */
285 static uint64 tupledesc_id_counter = INVALID_TUPLEDESC_IDENTIFIER;
286 
287 static void load_typcache_tupdesc(TypeCacheEntry *typentry);
288 static void load_rangetype_info(TypeCacheEntry *typentry);
289 static void load_domaintype_info(TypeCacheEntry *typentry);
290 static int	dcs_cmp(const void *a, const void *b);
291 static void decr_dcc_refcount(DomainConstraintCache *dcc);
292 static void dccref_deletion_callback(void *arg);
293 static List *prep_domain_constraints(List *constraints, MemoryContext execctx);
294 static bool array_element_has_equality(TypeCacheEntry *typentry);
295 static bool array_element_has_compare(TypeCacheEntry *typentry);
296 static bool array_element_has_hashing(TypeCacheEntry *typentry);
297 static bool array_element_has_extended_hashing(TypeCacheEntry *typentry);
298 static void cache_array_element_properties(TypeCacheEntry *typentry);
299 static bool record_fields_have_equality(TypeCacheEntry *typentry);
300 static bool record_fields_have_compare(TypeCacheEntry *typentry);
301 static void cache_record_field_properties(TypeCacheEntry *typentry);
302 static bool range_element_has_hashing(TypeCacheEntry *typentry);
303 static bool range_element_has_extended_hashing(TypeCacheEntry *typentry);
304 static void cache_range_element_properties(TypeCacheEntry *typentry);
305 static void TypeCacheRelCallback(Datum arg, Oid relid);
306 static void TypeCacheTypCallback(Datum arg, int cacheid, uint32 hashvalue);
307 static void TypeCacheOpcCallback(Datum arg, int cacheid, uint32 hashvalue);
308 static void TypeCacheConstrCallback(Datum arg, int cacheid, uint32 hashvalue);
309 static void load_enum_cache_data(TypeCacheEntry *tcache);
310 static EnumItem *find_enumitem(TypeCacheEnumData *enumdata, Oid arg);
311 static int	enum_oid_cmp(const void *left, const void *right);
312 static void shared_record_typmod_registry_detach(dsm_segment *segment,
313 												 Datum datum);
314 static TupleDesc find_or_make_matching_shared_tupledesc(TupleDesc tupdesc);
315 static dsa_pointer share_tupledesc(dsa_area *area, TupleDesc tupdesc,
316 								   uint32 typmod);
317 
318 
319 /*
320  * lookup_type_cache
321  *
322  * Fetch the type cache entry for the specified datatype, and make sure that
323  * all the fields requested by bits in 'flags' are valid.
324  *
325  * The result is never NULL --- we will ereport() if the passed type OID is
326  * invalid.  Note however that we may fail to find one or more of the
327  * values requested by 'flags'; the caller needs to check whether the fields
328  * are InvalidOid or not.
329  */
330 TypeCacheEntry *
lookup_type_cache(Oid type_id,int flags)331 lookup_type_cache(Oid type_id, int flags)
332 {
333 	TypeCacheEntry *typentry;
334 	bool		found;
335 
336 	if (TypeCacheHash == NULL)
337 	{
338 		/* First time through: initialize the hash table */
339 		HASHCTL		ctl;
340 
341 		MemSet(&ctl, 0, sizeof(ctl));
342 		ctl.keysize = sizeof(Oid);
343 		ctl.entrysize = sizeof(TypeCacheEntry);
344 		TypeCacheHash = hash_create("Type information cache", 64,
345 									&ctl, HASH_ELEM | HASH_BLOBS);
346 
347 		/* Also set up callbacks for SI invalidations */
348 		CacheRegisterRelcacheCallback(TypeCacheRelCallback, (Datum) 0);
349 		CacheRegisterSyscacheCallback(TYPEOID, TypeCacheTypCallback, (Datum) 0);
350 		CacheRegisterSyscacheCallback(CLAOID, TypeCacheOpcCallback, (Datum) 0);
351 		CacheRegisterSyscacheCallback(CONSTROID, TypeCacheConstrCallback, (Datum) 0);
352 
353 		/* Also make sure CacheMemoryContext exists */
354 		if (!CacheMemoryContext)
355 			CreateCacheMemoryContext();
356 	}
357 
358 	/* Try to look up an existing entry */
359 	typentry = (TypeCacheEntry *) hash_search(TypeCacheHash,
360 											  (void *) &type_id,
361 											  HASH_FIND, NULL);
362 	if (typentry == NULL)
363 	{
364 		/*
365 		 * If we didn't find one, we want to make one.  But first look up the
366 		 * pg_type row, just to make sure we don't make a cache entry for an
367 		 * invalid type OID.  If the type OID is not valid, present a
368 		 * user-facing error, since some code paths such as domain_in() allow
369 		 * this function to be reached with a user-supplied OID.
370 		 */
371 		HeapTuple	tp;
372 		Form_pg_type typtup;
373 
374 		tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
375 		if (!HeapTupleIsValid(tp))
376 			ereport(ERROR,
377 					(errcode(ERRCODE_UNDEFINED_OBJECT),
378 					 errmsg("type with OID %u does not exist", type_id)));
379 		typtup = (Form_pg_type) GETSTRUCT(tp);
380 		if (!typtup->typisdefined)
381 			ereport(ERROR,
382 					(errcode(ERRCODE_UNDEFINED_OBJECT),
383 					 errmsg("type \"%s\" is only a shell",
384 							NameStr(typtup->typname))));
385 
386 		/* Now make the typcache entry */
387 		typentry = (TypeCacheEntry *) hash_search(TypeCacheHash,
388 												  (void *) &type_id,
389 												  HASH_ENTER, &found);
390 		Assert(!found);			/* it wasn't there a moment ago */
391 
392 		MemSet(typentry, 0, sizeof(TypeCacheEntry));
393 
394 		/* These fields can never change, by definition */
395 		typentry->type_id = type_id;
396 		typentry->type_id_hash = GetSysCacheHashValue1(TYPEOID,
397 													   ObjectIdGetDatum(type_id));
398 
399 		/* Keep this part in sync with the code below */
400 		typentry->typlen = typtup->typlen;
401 		typentry->typbyval = typtup->typbyval;
402 		typentry->typalign = typtup->typalign;
403 		typentry->typstorage = typtup->typstorage;
404 		typentry->typtype = typtup->typtype;
405 		typentry->typrelid = typtup->typrelid;
406 		typentry->typelem = typtup->typelem;
407 		typentry->typcollation = typtup->typcollation;
408 		typentry->flags |= TCFLAGS_HAVE_PG_TYPE_DATA;
409 
410 		/* If it's a domain, immediately thread it into the domain cache list */
411 		if (typentry->typtype == TYPTYPE_DOMAIN)
412 		{
413 			typentry->nextDomain = firstDomainTypeEntry;
414 			firstDomainTypeEntry = typentry;
415 		}
416 
417 		ReleaseSysCache(tp);
418 	}
419 	else if (!(typentry->flags & TCFLAGS_HAVE_PG_TYPE_DATA))
420 	{
421 		/*
422 		 * We have an entry, but its pg_type row got changed, so reload the
423 		 * data obtained directly from pg_type.
424 		 */
425 		HeapTuple	tp;
426 		Form_pg_type typtup;
427 
428 		tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
429 		if (!HeapTupleIsValid(tp))
430 			ereport(ERROR,
431 					(errcode(ERRCODE_UNDEFINED_OBJECT),
432 					 errmsg("type with OID %u does not exist", type_id)));
433 		typtup = (Form_pg_type) GETSTRUCT(tp);
434 		if (!typtup->typisdefined)
435 			ereport(ERROR,
436 					(errcode(ERRCODE_UNDEFINED_OBJECT),
437 					 errmsg("type \"%s\" is only a shell",
438 							NameStr(typtup->typname))));
439 
440 		/*
441 		 * Keep this part in sync with the code above.  Many of these fields
442 		 * shouldn't ever change, particularly typtype, but copy 'em anyway.
443 		 */
444 		typentry->typlen = typtup->typlen;
445 		typentry->typbyval = typtup->typbyval;
446 		typentry->typalign = typtup->typalign;
447 		typentry->typstorage = typtup->typstorage;
448 		typentry->typtype = typtup->typtype;
449 		typentry->typrelid = typtup->typrelid;
450 		typentry->typelem = typtup->typelem;
451 		typentry->typcollation = typtup->typcollation;
452 		typentry->flags |= TCFLAGS_HAVE_PG_TYPE_DATA;
453 
454 		ReleaseSysCache(tp);
455 	}
456 
457 	/*
458 	 * Look up opclasses if we haven't already and any dependent info is
459 	 * requested.
460 	 */
461 	if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_LT_OPR | TYPECACHE_GT_OPR |
462 				  TYPECACHE_CMP_PROC |
463 				  TYPECACHE_EQ_OPR_FINFO | TYPECACHE_CMP_PROC_FINFO |
464 				  TYPECACHE_BTREE_OPFAMILY)) &&
465 		!(typentry->flags & TCFLAGS_CHECKED_BTREE_OPCLASS))
466 	{
467 		Oid			opclass;
468 
469 		opclass = GetDefaultOpClass(type_id, BTREE_AM_OID);
470 		if (OidIsValid(opclass))
471 		{
472 			typentry->btree_opf = get_opclass_family(opclass);
473 			typentry->btree_opintype = get_opclass_input_type(opclass);
474 		}
475 		else
476 		{
477 			typentry->btree_opf = typentry->btree_opintype = InvalidOid;
478 		}
479 
480 		/*
481 		 * Reset information derived from btree opclass.  Note in particular
482 		 * that we'll redetermine the eq_opr even if we previously found one;
483 		 * this matters in case a btree opclass has been added to a type that
484 		 * previously had only a hash opclass.
485 		 */
486 		typentry->flags &= ~(TCFLAGS_CHECKED_EQ_OPR |
487 							 TCFLAGS_CHECKED_LT_OPR |
488 							 TCFLAGS_CHECKED_GT_OPR |
489 							 TCFLAGS_CHECKED_CMP_PROC);
490 		typentry->flags |= TCFLAGS_CHECKED_BTREE_OPCLASS;
491 	}
492 
493 	/*
494 	 * If we need to look up equality operator, and there's no btree opclass,
495 	 * force lookup of hash opclass.
496 	 */
497 	if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
498 		!(typentry->flags & TCFLAGS_CHECKED_EQ_OPR) &&
499 		typentry->btree_opf == InvalidOid)
500 		flags |= TYPECACHE_HASH_OPFAMILY;
501 
502 	if ((flags & (TYPECACHE_HASH_PROC | TYPECACHE_HASH_PROC_FINFO |
503 				  TYPECACHE_HASH_EXTENDED_PROC |
504 				  TYPECACHE_HASH_EXTENDED_PROC_FINFO |
505 				  TYPECACHE_HASH_OPFAMILY)) &&
506 		!(typentry->flags & TCFLAGS_CHECKED_HASH_OPCLASS))
507 	{
508 		Oid			opclass;
509 
510 		opclass = GetDefaultOpClass(type_id, HASH_AM_OID);
511 		if (OidIsValid(opclass))
512 		{
513 			typentry->hash_opf = get_opclass_family(opclass);
514 			typentry->hash_opintype = get_opclass_input_type(opclass);
515 		}
516 		else
517 		{
518 			typentry->hash_opf = typentry->hash_opintype = InvalidOid;
519 		}
520 
521 		/*
522 		 * Reset information derived from hash opclass.  We do *not* reset the
523 		 * eq_opr; if we already found one from the btree opclass, that
524 		 * decision is still good.
525 		 */
526 		typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
527 							 TCFLAGS_CHECKED_HASH_EXTENDED_PROC);
528 		typentry->flags |= TCFLAGS_CHECKED_HASH_OPCLASS;
529 	}
530 
531 	/*
532 	 * Look for requested operators and functions, if we haven't already.
533 	 */
534 	if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
535 		!(typentry->flags & TCFLAGS_CHECKED_EQ_OPR))
536 	{
537 		Oid			eq_opr = InvalidOid;
538 
539 		if (typentry->btree_opf != InvalidOid)
540 			eq_opr = get_opfamily_member(typentry->btree_opf,
541 										 typentry->btree_opintype,
542 										 typentry->btree_opintype,
543 										 BTEqualStrategyNumber);
544 		if (eq_opr == InvalidOid &&
545 			typentry->hash_opf != InvalidOid)
546 			eq_opr = get_opfamily_member(typentry->hash_opf,
547 										 typentry->hash_opintype,
548 										 typentry->hash_opintype,
549 										 HTEqualStrategyNumber);
550 
551 		/*
552 		 * If the proposed equality operator is array_eq or record_eq, check
553 		 * to see if the element type or column types support equality.  If
554 		 * not, array_eq or record_eq would fail at runtime, so we don't want
555 		 * to report that the type has equality.  (We can omit similar
556 		 * checking for ranges because ranges can't be created in the first
557 		 * place unless their subtypes support equality.)
558 		 */
559 		if (eq_opr == ARRAY_EQ_OP &&
560 			!array_element_has_equality(typentry))
561 			eq_opr = InvalidOid;
562 		else if (eq_opr == RECORD_EQ_OP &&
563 				 !record_fields_have_equality(typentry))
564 			eq_opr = InvalidOid;
565 
566 		/* Force update of eq_opr_finfo only if we're changing state */
567 		if (typentry->eq_opr != eq_opr)
568 			typentry->eq_opr_finfo.fn_oid = InvalidOid;
569 
570 		typentry->eq_opr = eq_opr;
571 
572 		/*
573 		 * Reset info about hash functions whenever we pick up new info about
574 		 * equality operator.  This is so we can ensure that the hash
575 		 * functions match the operator.
576 		 */
577 		typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
578 							 TCFLAGS_CHECKED_HASH_EXTENDED_PROC);
579 		typentry->flags |= TCFLAGS_CHECKED_EQ_OPR;
580 	}
581 	if ((flags & TYPECACHE_LT_OPR) &&
582 		!(typentry->flags & TCFLAGS_CHECKED_LT_OPR))
583 	{
584 		Oid			lt_opr = InvalidOid;
585 
586 		if (typentry->btree_opf != InvalidOid)
587 			lt_opr = get_opfamily_member(typentry->btree_opf,
588 										 typentry->btree_opintype,
589 										 typentry->btree_opintype,
590 										 BTLessStrategyNumber);
591 
592 		/*
593 		 * As above, make sure array_cmp or record_cmp will succeed; but again
594 		 * we need no special check for ranges.
595 		 */
596 		if (lt_opr == ARRAY_LT_OP &&
597 			!array_element_has_compare(typentry))
598 			lt_opr = InvalidOid;
599 		else if (lt_opr == RECORD_LT_OP &&
600 				 !record_fields_have_compare(typentry))
601 			lt_opr = InvalidOid;
602 
603 		typentry->lt_opr = lt_opr;
604 		typentry->flags |= TCFLAGS_CHECKED_LT_OPR;
605 	}
606 	if ((flags & TYPECACHE_GT_OPR) &&
607 		!(typentry->flags & TCFLAGS_CHECKED_GT_OPR))
608 	{
609 		Oid			gt_opr = InvalidOid;
610 
611 		if (typentry->btree_opf != InvalidOid)
612 			gt_opr = get_opfamily_member(typentry->btree_opf,
613 										 typentry->btree_opintype,
614 										 typentry->btree_opintype,
615 										 BTGreaterStrategyNumber);
616 
617 		/*
618 		 * As above, make sure array_cmp or record_cmp will succeed; but again
619 		 * we need no special check for ranges.
620 		 */
621 		if (gt_opr == ARRAY_GT_OP &&
622 			!array_element_has_compare(typentry))
623 			gt_opr = InvalidOid;
624 		else if (gt_opr == RECORD_GT_OP &&
625 				 !record_fields_have_compare(typentry))
626 			gt_opr = InvalidOid;
627 
628 		typentry->gt_opr = gt_opr;
629 		typentry->flags |= TCFLAGS_CHECKED_GT_OPR;
630 	}
631 	if ((flags & (TYPECACHE_CMP_PROC | TYPECACHE_CMP_PROC_FINFO)) &&
632 		!(typentry->flags & TCFLAGS_CHECKED_CMP_PROC))
633 	{
634 		Oid			cmp_proc = InvalidOid;
635 
636 		if (typentry->btree_opf != InvalidOid)
637 			cmp_proc = get_opfamily_proc(typentry->btree_opf,
638 										 typentry->btree_opintype,
639 										 typentry->btree_opintype,
640 										 BTORDER_PROC);
641 
642 		/*
643 		 * As above, make sure array_cmp or record_cmp will succeed; but again
644 		 * we need no special check for ranges.
645 		 */
646 		if (cmp_proc == F_BTARRAYCMP &&
647 			!array_element_has_compare(typentry))
648 			cmp_proc = InvalidOid;
649 		else if (cmp_proc == F_BTRECORDCMP &&
650 				 !record_fields_have_compare(typentry))
651 			cmp_proc = InvalidOid;
652 
653 		/* Force update of cmp_proc_finfo only if we're changing state */
654 		if (typentry->cmp_proc != cmp_proc)
655 			typentry->cmp_proc_finfo.fn_oid = InvalidOid;
656 
657 		typentry->cmp_proc = cmp_proc;
658 		typentry->flags |= TCFLAGS_CHECKED_CMP_PROC;
659 	}
660 	if ((flags & (TYPECACHE_HASH_PROC | TYPECACHE_HASH_PROC_FINFO)) &&
661 		!(typentry->flags & TCFLAGS_CHECKED_HASH_PROC))
662 	{
663 		Oid			hash_proc = InvalidOid;
664 
665 		/*
666 		 * We insist that the eq_opr, if one has been determined, match the
667 		 * hash opclass; else report there is no hash function.
668 		 */
669 		if (typentry->hash_opf != InvalidOid &&
670 			(!OidIsValid(typentry->eq_opr) ||
671 			 typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
672 													 typentry->hash_opintype,
673 													 typentry->hash_opintype,
674 													 HTEqualStrategyNumber)))
675 			hash_proc = get_opfamily_proc(typentry->hash_opf,
676 										  typentry->hash_opintype,
677 										  typentry->hash_opintype,
678 										  HASHSTANDARD_PROC);
679 
680 		/*
681 		 * As above, make sure hash_array will succeed.  We don't currently
682 		 * support hashing for composite types, but when we do, we'll need
683 		 * more logic here to check that case too.
684 		 */
685 		if (hash_proc == F_HASH_ARRAY &&
686 			!array_element_has_hashing(typentry))
687 			hash_proc = InvalidOid;
688 
689 		/*
690 		 * Likewise for hash_range.
691 		 */
692 		if (hash_proc == F_HASH_RANGE &&
693 			!range_element_has_hashing(typentry))
694 			hash_proc = InvalidOid;
695 
696 		/* Force update of hash_proc_finfo only if we're changing state */
697 		if (typentry->hash_proc != hash_proc)
698 			typentry->hash_proc_finfo.fn_oid = InvalidOid;
699 
700 		typentry->hash_proc = hash_proc;
701 		typentry->flags |= TCFLAGS_CHECKED_HASH_PROC;
702 	}
703 	if ((flags & (TYPECACHE_HASH_EXTENDED_PROC |
704 				  TYPECACHE_HASH_EXTENDED_PROC_FINFO)) &&
705 		!(typentry->flags & TCFLAGS_CHECKED_HASH_EXTENDED_PROC))
706 	{
707 		Oid			hash_extended_proc = InvalidOid;
708 
709 		/*
710 		 * We insist that the eq_opr, if one has been determined, match the
711 		 * hash opclass; else report there is no hash function.
712 		 */
713 		if (typentry->hash_opf != InvalidOid &&
714 			(!OidIsValid(typentry->eq_opr) ||
715 			 typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
716 													 typentry->hash_opintype,
717 													 typentry->hash_opintype,
718 													 HTEqualStrategyNumber)))
719 			hash_extended_proc = get_opfamily_proc(typentry->hash_opf,
720 												   typentry->hash_opintype,
721 												   typentry->hash_opintype,
722 												   HASHEXTENDED_PROC);
723 
724 		/*
725 		 * As above, make sure hash_array_extended will succeed.  We don't
726 		 * currently support hashing for composite types, but when we do,
727 		 * we'll need more logic here to check that case too.
728 		 */
729 		if (hash_extended_proc == F_HASH_ARRAY_EXTENDED &&
730 			!array_element_has_extended_hashing(typentry))
731 			hash_extended_proc = InvalidOid;
732 
733 		/*
734 		 * Likewise for hash_range_extended.
735 		 */
736 		if (hash_extended_proc == F_HASH_RANGE_EXTENDED &&
737 			!range_element_has_extended_hashing(typentry))
738 			hash_extended_proc = InvalidOid;
739 
740 		/* Force update of proc finfo only if we're changing state */
741 		if (typentry->hash_extended_proc != hash_extended_proc)
742 			typentry->hash_extended_proc_finfo.fn_oid = InvalidOid;
743 
744 		typentry->hash_extended_proc = hash_extended_proc;
745 		typentry->flags |= TCFLAGS_CHECKED_HASH_EXTENDED_PROC;
746 	}
747 
748 	/*
749 	 * Set up fmgr lookup info as requested
750 	 *
751 	 * Note: we tell fmgr the finfo structures live in CacheMemoryContext,
752 	 * which is not quite right (they're really in the hash table's private
753 	 * memory context) but this will do for our purposes.
754 	 *
755 	 * Note: the code above avoids invalidating the finfo structs unless the
756 	 * referenced operator/function OID actually changes.  This is to prevent
757 	 * unnecessary leakage of any subsidiary data attached to an finfo, since
758 	 * that would cause session-lifespan memory leaks.
759 	 */
760 	if ((flags & TYPECACHE_EQ_OPR_FINFO) &&
761 		typentry->eq_opr_finfo.fn_oid == InvalidOid &&
762 		typentry->eq_opr != InvalidOid)
763 	{
764 		Oid			eq_opr_func;
765 
766 		eq_opr_func = get_opcode(typentry->eq_opr);
767 		if (eq_opr_func != InvalidOid)
768 			fmgr_info_cxt(eq_opr_func, &typentry->eq_opr_finfo,
769 						  CacheMemoryContext);
770 	}
771 	if ((flags & TYPECACHE_CMP_PROC_FINFO) &&
772 		typentry->cmp_proc_finfo.fn_oid == InvalidOid &&
773 		typentry->cmp_proc != InvalidOid)
774 	{
775 		fmgr_info_cxt(typentry->cmp_proc, &typentry->cmp_proc_finfo,
776 					  CacheMemoryContext);
777 	}
778 	if ((flags & TYPECACHE_HASH_PROC_FINFO) &&
779 		typentry->hash_proc_finfo.fn_oid == InvalidOid &&
780 		typentry->hash_proc != InvalidOid)
781 	{
782 		fmgr_info_cxt(typentry->hash_proc, &typentry->hash_proc_finfo,
783 					  CacheMemoryContext);
784 	}
785 	if ((flags & TYPECACHE_HASH_EXTENDED_PROC_FINFO) &&
786 		typentry->hash_extended_proc_finfo.fn_oid == InvalidOid &&
787 		typentry->hash_extended_proc != InvalidOid)
788 	{
789 		fmgr_info_cxt(typentry->hash_extended_proc,
790 					  &typentry->hash_extended_proc_finfo,
791 					  CacheMemoryContext);
792 	}
793 
794 	/*
795 	 * If it's a composite type (row type), get tupdesc if requested
796 	 */
797 	if ((flags & TYPECACHE_TUPDESC) &&
798 		typentry->tupDesc == NULL &&
799 		typentry->typtype == TYPTYPE_COMPOSITE)
800 	{
801 		load_typcache_tupdesc(typentry);
802 	}
803 
804 	/*
805 	 * If requested, get information about a range type
806 	 *
807 	 * This includes making sure that the basic info about the range element
808 	 * type is up-to-date.
809 	 */
810 	if ((flags & TYPECACHE_RANGE_INFO) &&
811 		typentry->typtype == TYPTYPE_RANGE)
812 	{
813 		if (typentry->rngelemtype == NULL)
814 			load_rangetype_info(typentry);
815 		else if (!(typentry->rngelemtype->flags & TCFLAGS_HAVE_PG_TYPE_DATA))
816 			(void) lookup_type_cache(typentry->rngelemtype->type_id, 0);
817 	}
818 
819 	/*
820 	 * If requested, get information about a domain type
821 	 */
822 	if ((flags & TYPECACHE_DOMAIN_BASE_INFO) &&
823 		typentry->domainBaseType == InvalidOid &&
824 		typentry->typtype == TYPTYPE_DOMAIN)
825 	{
826 		typentry->domainBaseTypmod = -1;
827 		typentry->domainBaseType =
828 			getBaseTypeAndTypmod(type_id, &typentry->domainBaseTypmod);
829 	}
830 	if ((flags & TYPECACHE_DOMAIN_CONSTR_INFO) &&
831 		(typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
832 		typentry->typtype == TYPTYPE_DOMAIN)
833 	{
834 		load_domaintype_info(typentry);
835 	}
836 
837 	return typentry;
838 }
839 
840 /*
841  * load_typcache_tupdesc --- helper routine to set up composite type's tupDesc
842  */
843 static void
load_typcache_tupdesc(TypeCacheEntry * typentry)844 load_typcache_tupdesc(TypeCacheEntry *typentry)
845 {
846 	Relation	rel;
847 
848 	if (!OidIsValid(typentry->typrelid))	/* should not happen */
849 		elog(ERROR, "invalid typrelid for composite type %u",
850 			 typentry->type_id);
851 	rel = relation_open(typentry->typrelid, AccessShareLock);
852 	Assert(rel->rd_rel->reltype == typentry->type_id);
853 
854 	/*
855 	 * Link to the tupdesc and increment its refcount (we assert it's a
856 	 * refcounted descriptor).  We don't use IncrTupleDescRefCount() for this,
857 	 * because the reference mustn't be entered in the current resource owner;
858 	 * it can outlive the current query.
859 	 */
860 	typentry->tupDesc = RelationGetDescr(rel);
861 
862 	Assert(typentry->tupDesc->tdrefcount > 0);
863 	typentry->tupDesc->tdrefcount++;
864 
865 	/*
866 	 * In future, we could take some pains to not change tupDesc_identifier if
867 	 * the tupdesc didn't really change; but for now it's not worth it.
868 	 */
869 	typentry->tupDesc_identifier = ++tupledesc_id_counter;
870 
871 	relation_close(rel, AccessShareLock);
872 }
873 
874 /*
875  * load_rangetype_info --- helper routine to set up range type information
876  */
877 static void
load_rangetype_info(TypeCacheEntry * typentry)878 load_rangetype_info(TypeCacheEntry *typentry)
879 {
880 	Form_pg_range pg_range;
881 	HeapTuple	tup;
882 	Oid			subtypeOid;
883 	Oid			opclassOid;
884 	Oid			canonicalOid;
885 	Oid			subdiffOid;
886 	Oid			opfamilyOid;
887 	Oid			opcintype;
888 	Oid			cmpFnOid;
889 
890 	/* get information from pg_range */
891 	tup = SearchSysCache1(RANGETYPE, ObjectIdGetDatum(typentry->type_id));
892 	/* should not fail, since we already checked typtype ... */
893 	if (!HeapTupleIsValid(tup))
894 		elog(ERROR, "cache lookup failed for range type %u",
895 			 typentry->type_id);
896 	pg_range = (Form_pg_range) GETSTRUCT(tup);
897 
898 	subtypeOid = pg_range->rngsubtype;
899 	typentry->rng_collation = pg_range->rngcollation;
900 	opclassOid = pg_range->rngsubopc;
901 	canonicalOid = pg_range->rngcanonical;
902 	subdiffOid = pg_range->rngsubdiff;
903 
904 	ReleaseSysCache(tup);
905 
906 	/* get opclass properties and look up the comparison function */
907 	opfamilyOid = get_opclass_family(opclassOid);
908 	opcintype = get_opclass_input_type(opclassOid);
909 
910 	cmpFnOid = get_opfamily_proc(opfamilyOid, opcintype, opcintype,
911 								 BTORDER_PROC);
912 	if (!RegProcedureIsValid(cmpFnOid))
913 		elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
914 			 BTORDER_PROC, opcintype, opcintype, opfamilyOid);
915 
916 	/* set up cached fmgrinfo structs */
917 	fmgr_info_cxt(cmpFnOid, &typentry->rng_cmp_proc_finfo,
918 				  CacheMemoryContext);
919 	if (OidIsValid(canonicalOid))
920 		fmgr_info_cxt(canonicalOid, &typentry->rng_canonical_finfo,
921 					  CacheMemoryContext);
922 	if (OidIsValid(subdiffOid))
923 		fmgr_info_cxt(subdiffOid, &typentry->rng_subdiff_finfo,
924 					  CacheMemoryContext);
925 
926 	/* Lastly, set up link to the element type --- this marks data valid */
927 	typentry->rngelemtype = lookup_type_cache(subtypeOid, 0);
928 }
929 
930 
931 /*
932  * load_domaintype_info --- helper routine to set up domain constraint info
933  *
934  * Note: we assume we're called in a relatively short-lived context, so it's
935  * okay to leak data into the current context while scanning pg_constraint.
936  * We build the new DomainConstraintCache data in a context underneath
937  * CurrentMemoryContext, and reparent it under CacheMemoryContext when
938  * complete.
939  */
940 static void
load_domaintype_info(TypeCacheEntry * typentry)941 load_domaintype_info(TypeCacheEntry *typentry)
942 {
943 	Oid			typeOid = typentry->type_id;
944 	DomainConstraintCache *dcc;
945 	bool		notNull = false;
946 	DomainConstraintState **ccons;
947 	int			cconslen;
948 	Relation	conRel;
949 	MemoryContext oldcxt;
950 
951 	/*
952 	 * If we're here, any existing constraint info is stale, so release it.
953 	 * For safety, be sure to null the link before trying to delete the data.
954 	 */
955 	if (typentry->domainData)
956 	{
957 		dcc = typentry->domainData;
958 		typentry->domainData = NULL;
959 		decr_dcc_refcount(dcc);
960 	}
961 
962 	/*
963 	 * We try to optimize the common case of no domain constraints, so don't
964 	 * create the dcc object and context until we find a constraint.  Likewise
965 	 * for the temp sorting array.
966 	 */
967 	dcc = NULL;
968 	ccons = NULL;
969 	cconslen = 0;
970 
971 	/*
972 	 * Scan pg_constraint for relevant constraints.  We want to find
973 	 * constraints for not just this domain, but any ancestor domains, so the
974 	 * outer loop crawls up the domain stack.
975 	 */
976 	conRel = table_open(ConstraintRelationId, AccessShareLock);
977 
978 	for (;;)
979 	{
980 		HeapTuple	tup;
981 		HeapTuple	conTup;
982 		Form_pg_type typTup;
983 		int			nccons = 0;
984 		ScanKeyData key[1];
985 		SysScanDesc scan;
986 
987 		tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeOid));
988 		if (!HeapTupleIsValid(tup))
989 			elog(ERROR, "cache lookup failed for type %u", typeOid);
990 		typTup = (Form_pg_type) GETSTRUCT(tup);
991 
992 		if (typTup->typtype != TYPTYPE_DOMAIN)
993 		{
994 			/* Not a domain, so done */
995 			ReleaseSysCache(tup);
996 			break;
997 		}
998 
999 		/* Test for NOT NULL Constraint */
1000 		if (typTup->typnotnull)
1001 			notNull = true;
1002 
1003 		/* Look for CHECK Constraints on this domain */
1004 		ScanKeyInit(&key[0],
1005 					Anum_pg_constraint_contypid,
1006 					BTEqualStrategyNumber, F_OIDEQ,
1007 					ObjectIdGetDatum(typeOid));
1008 
1009 		scan = systable_beginscan(conRel, ConstraintTypidIndexId, true,
1010 								  NULL, 1, key);
1011 
1012 		while (HeapTupleIsValid(conTup = systable_getnext(scan)))
1013 		{
1014 			Form_pg_constraint c = (Form_pg_constraint) GETSTRUCT(conTup);
1015 			Datum		val;
1016 			bool		isNull;
1017 			char	   *constring;
1018 			Expr	   *check_expr;
1019 			DomainConstraintState *r;
1020 
1021 			/* Ignore non-CHECK constraints (presently, shouldn't be any) */
1022 			if (c->contype != CONSTRAINT_CHECK)
1023 				continue;
1024 
1025 			/* Not expecting conbin to be NULL, but we'll test for it anyway */
1026 			val = fastgetattr(conTup, Anum_pg_constraint_conbin,
1027 							  conRel->rd_att, &isNull);
1028 			if (isNull)
1029 				elog(ERROR, "domain \"%s\" constraint \"%s\" has NULL conbin",
1030 					 NameStr(typTup->typname), NameStr(c->conname));
1031 
1032 			/* Convert conbin to C string in caller context */
1033 			constring = TextDatumGetCString(val);
1034 
1035 			/* Create the DomainConstraintCache object and context if needed */
1036 			if (dcc == NULL)
1037 			{
1038 				MemoryContext cxt;
1039 
1040 				cxt = AllocSetContextCreate(CurrentMemoryContext,
1041 											"Domain constraints",
1042 											ALLOCSET_SMALL_SIZES);
1043 				dcc = (DomainConstraintCache *)
1044 					MemoryContextAlloc(cxt, sizeof(DomainConstraintCache));
1045 				dcc->constraints = NIL;
1046 				dcc->dccContext = cxt;
1047 				dcc->dccRefCount = 0;
1048 			}
1049 
1050 			/* Create node trees in DomainConstraintCache's context */
1051 			oldcxt = MemoryContextSwitchTo(dcc->dccContext);
1052 
1053 			check_expr = (Expr *) stringToNode(constring);
1054 
1055 			/*
1056 			 * Plan the expression, since ExecInitExpr will expect that.
1057 			 *
1058 			 * Note: caching the result of expression_planner() is not very
1059 			 * good practice.  Ideally we'd use a CachedExpression here so
1060 			 * that we would react promptly to, eg, changes in inlined
1061 			 * functions.  However, because we don't support mutable domain
1062 			 * CHECK constraints, it's not really clear that it's worth the
1063 			 * extra overhead to do that.
1064 			 */
1065 			check_expr = expression_planner(check_expr);
1066 
1067 			r = makeNode(DomainConstraintState);
1068 			r->constrainttype = DOM_CONSTRAINT_CHECK;
1069 			r->name = pstrdup(NameStr(c->conname));
1070 			r->check_expr = check_expr;
1071 			r->check_exprstate = NULL;
1072 
1073 			MemoryContextSwitchTo(oldcxt);
1074 
1075 			/* Accumulate constraints in an array, for sorting below */
1076 			if (ccons == NULL)
1077 			{
1078 				cconslen = 8;
1079 				ccons = (DomainConstraintState **)
1080 					palloc(cconslen * sizeof(DomainConstraintState *));
1081 			}
1082 			else if (nccons >= cconslen)
1083 			{
1084 				cconslen *= 2;
1085 				ccons = (DomainConstraintState **)
1086 					repalloc(ccons, cconslen * sizeof(DomainConstraintState *));
1087 			}
1088 			ccons[nccons++] = r;
1089 		}
1090 
1091 		systable_endscan(scan);
1092 
1093 		if (nccons > 0)
1094 		{
1095 			/*
1096 			 * Sort the items for this domain, so that CHECKs are applied in a
1097 			 * deterministic order.
1098 			 */
1099 			if (nccons > 1)
1100 				qsort(ccons, nccons, sizeof(DomainConstraintState *), dcs_cmp);
1101 
1102 			/*
1103 			 * Now attach them to the overall list.  Use lcons() here because
1104 			 * constraints of parent domains should be applied earlier.
1105 			 */
1106 			oldcxt = MemoryContextSwitchTo(dcc->dccContext);
1107 			while (nccons > 0)
1108 				dcc->constraints = lcons(ccons[--nccons], dcc->constraints);
1109 			MemoryContextSwitchTo(oldcxt);
1110 		}
1111 
1112 		/* loop to next domain in stack */
1113 		typeOid = typTup->typbasetype;
1114 		ReleaseSysCache(tup);
1115 	}
1116 
1117 	table_close(conRel, AccessShareLock);
1118 
1119 	/*
1120 	 * Only need to add one NOT NULL check regardless of how many domains in
1121 	 * the stack request it.
1122 	 */
1123 	if (notNull)
1124 	{
1125 		DomainConstraintState *r;
1126 
1127 		/* Create the DomainConstraintCache object and context if needed */
1128 		if (dcc == NULL)
1129 		{
1130 			MemoryContext cxt;
1131 
1132 			cxt = AllocSetContextCreate(CurrentMemoryContext,
1133 										"Domain constraints",
1134 										ALLOCSET_SMALL_SIZES);
1135 			dcc = (DomainConstraintCache *)
1136 				MemoryContextAlloc(cxt, sizeof(DomainConstraintCache));
1137 			dcc->constraints = NIL;
1138 			dcc->dccContext = cxt;
1139 			dcc->dccRefCount = 0;
1140 		}
1141 
1142 		/* Create node trees in DomainConstraintCache's context */
1143 		oldcxt = MemoryContextSwitchTo(dcc->dccContext);
1144 
1145 		r = makeNode(DomainConstraintState);
1146 
1147 		r->constrainttype = DOM_CONSTRAINT_NOTNULL;
1148 		r->name = pstrdup("NOT NULL");
1149 		r->check_expr = NULL;
1150 		r->check_exprstate = NULL;
1151 
1152 		/* lcons to apply the nullness check FIRST */
1153 		dcc->constraints = lcons(r, dcc->constraints);
1154 
1155 		MemoryContextSwitchTo(oldcxt);
1156 	}
1157 
1158 	/*
1159 	 * If we made a constraint object, move it into CacheMemoryContext and
1160 	 * attach it to the typcache entry.
1161 	 */
1162 	if (dcc)
1163 	{
1164 		MemoryContextSetParent(dcc->dccContext, CacheMemoryContext);
1165 		typentry->domainData = dcc;
1166 		dcc->dccRefCount++;		/* count the typcache's reference */
1167 	}
1168 
1169 	/* Either way, the typcache entry's domain data is now valid. */
1170 	typentry->flags |= TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS;
1171 }
1172 
1173 /*
1174  * qsort comparator to sort DomainConstraintState pointers by name
1175  */
1176 static int
dcs_cmp(const void * a,const void * b)1177 dcs_cmp(const void *a, const void *b)
1178 {
1179 	const DomainConstraintState *const *ca = (const DomainConstraintState *const *) a;
1180 	const DomainConstraintState *const *cb = (const DomainConstraintState *const *) b;
1181 
1182 	return strcmp((*ca)->name, (*cb)->name);
1183 }
1184 
1185 /*
1186  * decr_dcc_refcount --- decrement a DomainConstraintCache's refcount,
1187  * and free it if no references remain
1188  */
1189 static void
decr_dcc_refcount(DomainConstraintCache * dcc)1190 decr_dcc_refcount(DomainConstraintCache *dcc)
1191 {
1192 	Assert(dcc->dccRefCount > 0);
1193 	if (--(dcc->dccRefCount) <= 0)
1194 		MemoryContextDelete(dcc->dccContext);
1195 }
1196 
1197 /*
1198  * Context reset/delete callback for a DomainConstraintRef
1199  */
1200 static void
dccref_deletion_callback(void * arg)1201 dccref_deletion_callback(void *arg)
1202 {
1203 	DomainConstraintRef *ref = (DomainConstraintRef *) arg;
1204 	DomainConstraintCache *dcc = ref->dcc;
1205 
1206 	/* Paranoia --- be sure link is nulled before trying to release */
1207 	if (dcc)
1208 	{
1209 		ref->constraints = NIL;
1210 		ref->dcc = NULL;
1211 		decr_dcc_refcount(dcc);
1212 	}
1213 }
1214 
1215 /*
1216  * prep_domain_constraints --- prepare domain constraints for execution
1217  *
1218  * The expression trees stored in the DomainConstraintCache's list are
1219  * converted to executable expression state trees stored in execctx.
1220  */
1221 static List *
prep_domain_constraints(List * constraints,MemoryContext execctx)1222 prep_domain_constraints(List *constraints, MemoryContext execctx)
1223 {
1224 	List	   *result = NIL;
1225 	MemoryContext oldcxt;
1226 	ListCell   *lc;
1227 
1228 	oldcxt = MemoryContextSwitchTo(execctx);
1229 
1230 	foreach(lc, constraints)
1231 	{
1232 		DomainConstraintState *r = (DomainConstraintState *) lfirst(lc);
1233 		DomainConstraintState *newr;
1234 
1235 		newr = makeNode(DomainConstraintState);
1236 		newr->constrainttype = r->constrainttype;
1237 		newr->name = r->name;
1238 		newr->check_expr = r->check_expr;
1239 		newr->check_exprstate = ExecInitExpr(r->check_expr, NULL);
1240 
1241 		result = lappend(result, newr);
1242 	}
1243 
1244 	MemoryContextSwitchTo(oldcxt);
1245 
1246 	return result;
1247 }
1248 
1249 /*
1250  * InitDomainConstraintRef --- initialize a DomainConstraintRef struct
1251  *
1252  * Caller must tell us the MemoryContext in which the DomainConstraintRef
1253  * lives.  The ref will be cleaned up when that context is reset/deleted.
1254  *
1255  * Caller must also tell us whether it wants check_exprstate fields to be
1256  * computed in the DomainConstraintState nodes attached to this ref.
1257  * If it doesn't, we need not make a copy of the DomainConstraintState list.
1258  */
1259 void
InitDomainConstraintRef(Oid type_id,DomainConstraintRef * ref,MemoryContext refctx,bool need_exprstate)1260 InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref,
1261 						MemoryContext refctx, bool need_exprstate)
1262 {
1263 	/* Look up the typcache entry --- we assume it survives indefinitely */
1264 	ref->tcache = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO);
1265 	ref->need_exprstate = need_exprstate;
1266 	/* For safety, establish the callback before acquiring a refcount */
1267 	ref->refctx = refctx;
1268 	ref->dcc = NULL;
1269 	ref->callback.func = dccref_deletion_callback;
1270 	ref->callback.arg = (void *) ref;
1271 	MemoryContextRegisterResetCallback(refctx, &ref->callback);
1272 	/* Acquire refcount if there are constraints, and set up exported list */
1273 	if (ref->tcache->domainData)
1274 	{
1275 		ref->dcc = ref->tcache->domainData;
1276 		ref->dcc->dccRefCount++;
1277 		if (ref->need_exprstate)
1278 			ref->constraints = prep_domain_constraints(ref->dcc->constraints,
1279 													   ref->refctx);
1280 		else
1281 			ref->constraints = ref->dcc->constraints;
1282 	}
1283 	else
1284 		ref->constraints = NIL;
1285 }
1286 
1287 /*
1288  * UpdateDomainConstraintRef --- recheck validity of domain constraint info
1289  *
1290  * If the domain's constraint set changed, ref->constraints is updated to
1291  * point at a new list of cached constraints.
1292  *
1293  * In the normal case where nothing happened to the domain, this is cheap
1294  * enough that it's reasonable (and expected) to check before *each* use
1295  * of the constraint info.
1296  */
1297 void
UpdateDomainConstraintRef(DomainConstraintRef * ref)1298 UpdateDomainConstraintRef(DomainConstraintRef *ref)
1299 {
1300 	TypeCacheEntry *typentry = ref->tcache;
1301 
1302 	/* Make sure typcache entry's data is up to date */
1303 	if ((typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
1304 		typentry->typtype == TYPTYPE_DOMAIN)
1305 		load_domaintype_info(typentry);
1306 
1307 	/* Transfer to ref object if there's new info, adjusting refcounts */
1308 	if (ref->dcc != typentry->domainData)
1309 	{
1310 		/* Paranoia --- be sure link is nulled before trying to release */
1311 		DomainConstraintCache *dcc = ref->dcc;
1312 
1313 		if (dcc)
1314 		{
1315 			/*
1316 			 * Note: we just leak the previous list of executable domain
1317 			 * constraints.  Alternatively, we could keep those in a child
1318 			 * context of ref->refctx and free that context at this point.
1319 			 * However, in practice this code path will be taken so seldom
1320 			 * that the extra bookkeeping for a child context doesn't seem
1321 			 * worthwhile; we'll just allow a leak for the lifespan of refctx.
1322 			 */
1323 			ref->constraints = NIL;
1324 			ref->dcc = NULL;
1325 			decr_dcc_refcount(dcc);
1326 		}
1327 		dcc = typentry->domainData;
1328 		if (dcc)
1329 		{
1330 			ref->dcc = dcc;
1331 			dcc->dccRefCount++;
1332 			if (ref->need_exprstate)
1333 				ref->constraints = prep_domain_constraints(dcc->constraints,
1334 														   ref->refctx);
1335 			else
1336 				ref->constraints = dcc->constraints;
1337 		}
1338 	}
1339 }
1340 
1341 /*
1342  * DomainHasConstraints --- utility routine to check if a domain has constraints
1343  *
1344  * This is defined to return false, not fail, if type is not a domain.
1345  */
1346 bool
DomainHasConstraints(Oid type_id)1347 DomainHasConstraints(Oid type_id)
1348 {
1349 	TypeCacheEntry *typentry;
1350 
1351 	/*
1352 	 * Note: a side effect is to cause the typcache's domain data to become
1353 	 * valid.  This is fine since we'll likely need it soon if there is any.
1354 	 */
1355 	typentry = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO);
1356 
1357 	return (typentry->domainData != NULL);
1358 }
1359 
1360 
1361 /*
1362  * array_element_has_equality and friends are helper routines to check
1363  * whether we should believe that array_eq and related functions will work
1364  * on the given array type or composite type.
1365  *
1366  * The logic above may call these repeatedly on the same type entry, so we
1367  * make use of the typentry->flags field to cache the results once known.
1368  * Also, we assume that we'll probably want all these facts about the type
1369  * if we want any, so we cache them all using only one lookup of the
1370  * component datatype(s).
1371  */
1372 
1373 static bool
array_element_has_equality(TypeCacheEntry * typentry)1374 array_element_has_equality(TypeCacheEntry *typentry)
1375 {
1376 	if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1377 		cache_array_element_properties(typentry);
1378 	return (typentry->flags & TCFLAGS_HAVE_ELEM_EQUALITY) != 0;
1379 }
1380 
1381 static bool
array_element_has_compare(TypeCacheEntry * typentry)1382 array_element_has_compare(TypeCacheEntry *typentry)
1383 {
1384 	if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1385 		cache_array_element_properties(typentry);
1386 	return (typentry->flags & TCFLAGS_HAVE_ELEM_COMPARE) != 0;
1387 }
1388 
1389 static bool
array_element_has_hashing(TypeCacheEntry * typentry)1390 array_element_has_hashing(TypeCacheEntry *typentry)
1391 {
1392 	if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1393 		cache_array_element_properties(typentry);
1394 	return (typentry->flags & TCFLAGS_HAVE_ELEM_HASHING) != 0;
1395 }
1396 
1397 static bool
array_element_has_extended_hashing(TypeCacheEntry * typentry)1398 array_element_has_extended_hashing(TypeCacheEntry *typentry)
1399 {
1400 	if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1401 		cache_array_element_properties(typentry);
1402 	return (typentry->flags & TCFLAGS_HAVE_ELEM_EXTENDED_HASHING) != 0;
1403 }
1404 
1405 static void
cache_array_element_properties(TypeCacheEntry * typentry)1406 cache_array_element_properties(TypeCacheEntry *typentry)
1407 {
1408 	Oid			elem_type = get_base_element_type(typentry->type_id);
1409 
1410 	if (OidIsValid(elem_type))
1411 	{
1412 		TypeCacheEntry *elementry;
1413 
1414 		elementry = lookup_type_cache(elem_type,
1415 									  TYPECACHE_EQ_OPR |
1416 									  TYPECACHE_CMP_PROC |
1417 									  TYPECACHE_HASH_PROC |
1418 									  TYPECACHE_HASH_EXTENDED_PROC);
1419 		if (OidIsValid(elementry->eq_opr))
1420 			typentry->flags |= TCFLAGS_HAVE_ELEM_EQUALITY;
1421 		if (OidIsValid(elementry->cmp_proc))
1422 			typentry->flags |= TCFLAGS_HAVE_ELEM_COMPARE;
1423 		if (OidIsValid(elementry->hash_proc))
1424 			typentry->flags |= TCFLAGS_HAVE_ELEM_HASHING;
1425 		if (OidIsValid(elementry->hash_extended_proc))
1426 			typentry->flags |= TCFLAGS_HAVE_ELEM_EXTENDED_HASHING;
1427 	}
1428 	typentry->flags |= TCFLAGS_CHECKED_ELEM_PROPERTIES;
1429 }
1430 
1431 /*
1432  * Likewise, some helper functions for composite types.
1433  */
1434 
1435 static bool
record_fields_have_equality(TypeCacheEntry * typentry)1436 record_fields_have_equality(TypeCacheEntry *typentry)
1437 {
1438 	if (!(typentry->flags & TCFLAGS_CHECKED_FIELD_PROPERTIES))
1439 		cache_record_field_properties(typentry);
1440 	return (typentry->flags & TCFLAGS_HAVE_FIELD_EQUALITY) != 0;
1441 }
1442 
1443 static bool
record_fields_have_compare(TypeCacheEntry * typentry)1444 record_fields_have_compare(TypeCacheEntry *typentry)
1445 {
1446 	if (!(typentry->flags & TCFLAGS_CHECKED_FIELD_PROPERTIES))
1447 		cache_record_field_properties(typentry);
1448 	return (typentry->flags & TCFLAGS_HAVE_FIELD_COMPARE) != 0;
1449 }
1450 
1451 static void
cache_record_field_properties(TypeCacheEntry * typentry)1452 cache_record_field_properties(TypeCacheEntry *typentry)
1453 {
1454 	/*
1455 	 * For type RECORD, we can't really tell what will work, since we don't
1456 	 * have access here to the specific anonymous type.  Just assume that
1457 	 * everything will (we may get a failure at runtime ...)
1458 	 */
1459 	if (typentry->type_id == RECORDOID)
1460 		typentry->flags |= (TCFLAGS_HAVE_FIELD_EQUALITY |
1461 							TCFLAGS_HAVE_FIELD_COMPARE);
1462 	else if (typentry->typtype == TYPTYPE_COMPOSITE)
1463 	{
1464 		TupleDesc	tupdesc;
1465 		int			newflags;
1466 		int			i;
1467 
1468 		/* Fetch composite type's tupdesc if we don't have it already */
1469 		if (typentry->tupDesc == NULL)
1470 			load_typcache_tupdesc(typentry);
1471 		tupdesc = typentry->tupDesc;
1472 
1473 		/* Must bump the refcount while we do additional catalog lookups */
1474 		IncrTupleDescRefCount(tupdesc);
1475 
1476 		/* Have each property if all non-dropped fields have the property */
1477 		newflags = (TCFLAGS_HAVE_FIELD_EQUALITY |
1478 					TCFLAGS_HAVE_FIELD_COMPARE);
1479 		for (i = 0; i < tupdesc->natts; i++)
1480 		{
1481 			TypeCacheEntry *fieldentry;
1482 			Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
1483 
1484 			if (attr->attisdropped)
1485 				continue;
1486 
1487 			fieldentry = lookup_type_cache(attr->atttypid,
1488 										   TYPECACHE_EQ_OPR |
1489 										   TYPECACHE_CMP_PROC);
1490 			if (!OidIsValid(fieldentry->eq_opr))
1491 				newflags &= ~TCFLAGS_HAVE_FIELD_EQUALITY;
1492 			if (!OidIsValid(fieldentry->cmp_proc))
1493 				newflags &= ~TCFLAGS_HAVE_FIELD_COMPARE;
1494 
1495 			/* We can drop out of the loop once we disprove all bits */
1496 			if (newflags == 0)
1497 				break;
1498 		}
1499 		typentry->flags |= newflags;
1500 
1501 		DecrTupleDescRefCount(tupdesc);
1502 	}
1503 	else if (typentry->typtype == TYPTYPE_DOMAIN)
1504 	{
1505 		/* If it's domain over composite, copy base type's properties */
1506 		TypeCacheEntry *baseentry;
1507 
1508 		/* load up basetype info if we didn't already */
1509 		if (typentry->domainBaseType == InvalidOid)
1510 		{
1511 			typentry->domainBaseTypmod = -1;
1512 			typentry->domainBaseType =
1513 				getBaseTypeAndTypmod(typentry->type_id,
1514 									 &typentry->domainBaseTypmod);
1515 		}
1516 		baseentry = lookup_type_cache(typentry->domainBaseType,
1517 									  TYPECACHE_EQ_OPR |
1518 									  TYPECACHE_CMP_PROC);
1519 		if (baseentry->typtype == TYPTYPE_COMPOSITE)
1520 		{
1521 			typentry->flags |= TCFLAGS_DOMAIN_BASE_IS_COMPOSITE;
1522 			typentry->flags |= baseentry->flags & (TCFLAGS_HAVE_FIELD_EQUALITY |
1523 												   TCFLAGS_HAVE_FIELD_COMPARE);
1524 		}
1525 	}
1526 	typentry->flags |= TCFLAGS_CHECKED_FIELD_PROPERTIES;
1527 }
1528 
1529 /*
1530  * Likewise, some helper functions for range types.
1531  *
1532  * We can borrow the flag bits for array element properties to use for range
1533  * element properties, since those flag bits otherwise have no use in a
1534  * range type's typcache entry.
1535  */
1536 
1537 static bool
range_element_has_hashing(TypeCacheEntry * typentry)1538 range_element_has_hashing(TypeCacheEntry *typentry)
1539 {
1540 	if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1541 		cache_range_element_properties(typentry);
1542 	return (typentry->flags & TCFLAGS_HAVE_ELEM_HASHING) != 0;
1543 }
1544 
1545 static bool
range_element_has_extended_hashing(TypeCacheEntry * typentry)1546 range_element_has_extended_hashing(TypeCacheEntry *typentry)
1547 {
1548 	if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1549 		cache_range_element_properties(typentry);
1550 	return (typentry->flags & TCFLAGS_HAVE_ELEM_EXTENDED_HASHING) != 0;
1551 }
1552 
1553 static void
cache_range_element_properties(TypeCacheEntry * typentry)1554 cache_range_element_properties(TypeCacheEntry *typentry)
1555 {
1556 	/* load up subtype link if we didn't already */
1557 	if (typentry->rngelemtype == NULL &&
1558 		typentry->typtype == TYPTYPE_RANGE)
1559 		load_rangetype_info(typentry);
1560 
1561 	if (typentry->rngelemtype != NULL)
1562 	{
1563 		TypeCacheEntry *elementry;
1564 
1565 		/* might need to calculate subtype's hash function properties */
1566 		elementry = lookup_type_cache(typentry->rngelemtype->type_id,
1567 									  TYPECACHE_HASH_PROC |
1568 									  TYPECACHE_HASH_EXTENDED_PROC);
1569 		if (OidIsValid(elementry->hash_proc))
1570 			typentry->flags |= TCFLAGS_HAVE_ELEM_HASHING;
1571 		if (OidIsValid(elementry->hash_extended_proc))
1572 			typentry->flags |= TCFLAGS_HAVE_ELEM_EXTENDED_HASHING;
1573 	}
1574 	typentry->flags |= TCFLAGS_CHECKED_ELEM_PROPERTIES;
1575 }
1576 
1577 /*
1578  * Make sure that RecordCacheArray and RecordIdentifierArray are large enough
1579  * to store 'typmod'.
1580  */
1581 static void
ensure_record_cache_typmod_slot_exists(int32 typmod)1582 ensure_record_cache_typmod_slot_exists(int32 typmod)
1583 {
1584 	if (RecordCacheArray == NULL)
1585 	{
1586 		RecordCacheArray = (TupleDesc *)
1587 			MemoryContextAllocZero(CacheMemoryContext, 64 * sizeof(TupleDesc));
1588 		RecordIdentifierArray = (uint64 *)
1589 			MemoryContextAllocZero(CacheMemoryContext, 64 * sizeof(uint64));
1590 		RecordCacheArrayLen = 64;
1591 	}
1592 
1593 	if (typmod >= RecordCacheArrayLen)
1594 	{
1595 		int32		newlen = RecordCacheArrayLen * 2;
1596 
1597 		while (typmod >= newlen)
1598 			newlen *= 2;
1599 
1600 		RecordCacheArray = (TupleDesc *) repalloc(RecordCacheArray,
1601 												  newlen * sizeof(TupleDesc));
1602 		memset(RecordCacheArray + RecordCacheArrayLen, 0,
1603 			   (newlen - RecordCacheArrayLen) * sizeof(TupleDesc));
1604 		RecordIdentifierArray = (uint64 *) repalloc(RecordIdentifierArray,
1605 													newlen * sizeof(uint64));
1606 		memset(RecordIdentifierArray + RecordCacheArrayLen, 0,
1607 			   (newlen - RecordCacheArrayLen) * sizeof(uint64));
1608 		RecordCacheArrayLen = newlen;
1609 	}
1610 }
1611 
1612 /*
1613  * lookup_rowtype_tupdesc_internal --- internal routine to lookup a rowtype
1614  *
1615  * Same API as lookup_rowtype_tupdesc_noerror, but the returned tupdesc
1616  * hasn't had its refcount bumped.
1617  */
1618 static TupleDesc
lookup_rowtype_tupdesc_internal(Oid type_id,int32 typmod,bool noError)1619 lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
1620 {
1621 	if (type_id != RECORDOID)
1622 	{
1623 		/*
1624 		 * It's a named composite type, so use the regular typcache.
1625 		 */
1626 		TypeCacheEntry *typentry;
1627 
1628 		typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
1629 		if (typentry->tupDesc == NULL && !noError)
1630 			ereport(ERROR,
1631 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1632 					 errmsg("type %s is not composite",
1633 							format_type_be(type_id))));
1634 		return typentry->tupDesc;
1635 	}
1636 	else
1637 	{
1638 		/*
1639 		 * It's a transient record type, so look in our record-type table.
1640 		 */
1641 		if (typmod >= 0)
1642 		{
1643 			/* It is already in our local cache? */
1644 			if (typmod < RecordCacheArrayLen &&
1645 				RecordCacheArray[typmod] != NULL)
1646 				return RecordCacheArray[typmod];
1647 
1648 			/* Are we attached to a shared record typmod registry? */
1649 			if (CurrentSession->shared_typmod_registry != NULL)
1650 			{
1651 				SharedTypmodTableEntry *entry;
1652 
1653 				/* Try to find it in the shared typmod index. */
1654 				entry = dshash_find(CurrentSession->shared_typmod_table,
1655 									&typmod, false);
1656 				if (entry != NULL)
1657 				{
1658 					TupleDesc	tupdesc;
1659 
1660 					tupdesc = (TupleDesc)
1661 						dsa_get_address(CurrentSession->area,
1662 										entry->shared_tupdesc);
1663 					Assert(typmod == tupdesc->tdtypmod);
1664 
1665 					/* We may need to extend the local RecordCacheArray. */
1666 					ensure_record_cache_typmod_slot_exists(typmod);
1667 
1668 					/*
1669 					 * Our local array can now point directly to the TupleDesc
1670 					 * in shared memory, which is non-reference-counted.
1671 					 */
1672 					RecordCacheArray[typmod] = tupdesc;
1673 					Assert(tupdesc->tdrefcount == -1);
1674 
1675 					/*
1676 					 * We don't share tupdesc identifiers across processes, so
1677 					 * assign one locally.
1678 					 */
1679 					RecordIdentifierArray[typmod] = ++tupledesc_id_counter;
1680 
1681 					dshash_release_lock(CurrentSession->shared_typmod_table,
1682 										entry);
1683 
1684 					return RecordCacheArray[typmod];
1685 				}
1686 			}
1687 		}
1688 
1689 		if (!noError)
1690 			ereport(ERROR,
1691 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1692 					 errmsg("record type has not been registered")));
1693 		return NULL;
1694 	}
1695 }
1696 
1697 /*
1698  * lookup_rowtype_tupdesc
1699  *
1700  * Given a typeid/typmod that should describe a known composite type,
1701  * return the tuple descriptor for the type.  Will ereport on failure.
1702  * (Use ereport because this is reachable with user-specified OIDs,
1703  * for example from record_in().)
1704  *
1705  * Note: on success, we increment the refcount of the returned TupleDesc,
1706  * and log the reference in CurrentResourceOwner.  Caller should call
1707  * ReleaseTupleDesc or DecrTupleDescRefCount when done using the tupdesc.
1708  */
1709 TupleDesc
lookup_rowtype_tupdesc(Oid type_id,int32 typmod)1710 lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
1711 {
1712 	TupleDesc	tupDesc;
1713 
1714 	tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, false);
1715 	PinTupleDesc(tupDesc);
1716 	return tupDesc;
1717 }
1718 
1719 /*
1720  * lookup_rowtype_tupdesc_noerror
1721  *
1722  * As above, but if the type is not a known composite type and noError
1723  * is true, returns NULL instead of ereport'ing.  (Note that if a bogus
1724  * type_id is passed, you'll get an ereport anyway.)
1725  */
1726 TupleDesc
lookup_rowtype_tupdesc_noerror(Oid type_id,int32 typmod,bool noError)1727 lookup_rowtype_tupdesc_noerror(Oid type_id, int32 typmod, bool noError)
1728 {
1729 	TupleDesc	tupDesc;
1730 
1731 	tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1732 	if (tupDesc != NULL)
1733 		PinTupleDesc(tupDesc);
1734 	return tupDesc;
1735 }
1736 
1737 /*
1738  * lookup_rowtype_tupdesc_copy
1739  *
1740  * Like lookup_rowtype_tupdesc(), but the returned TupleDesc has been
1741  * copied into the CurrentMemoryContext and is not reference-counted.
1742  */
1743 TupleDesc
lookup_rowtype_tupdesc_copy(Oid type_id,int32 typmod)1744 lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
1745 {
1746 	TupleDesc	tmp;
1747 
1748 	tmp = lookup_rowtype_tupdesc_internal(type_id, typmod, false);
1749 	return CreateTupleDescCopyConstr(tmp);
1750 }
1751 
1752 /*
1753  * lookup_rowtype_tupdesc_domain
1754  *
1755  * Same as lookup_rowtype_tupdesc_noerror(), except that the type can also be
1756  * a domain over a named composite type; so this is effectively equivalent to
1757  * lookup_rowtype_tupdesc_noerror(getBaseType(type_id), typmod, noError)
1758  * except for being a tad faster.
1759  *
1760  * Note: the reason we don't fold the look-through-domain behavior into plain
1761  * lookup_rowtype_tupdesc() is that we want callers to know they might be
1762  * dealing with a domain.  Otherwise they might construct a tuple that should
1763  * be of the domain type, but not apply domain constraints.
1764  */
1765 TupleDesc
lookup_rowtype_tupdesc_domain(Oid type_id,int32 typmod,bool noError)1766 lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod, bool noError)
1767 {
1768 	TupleDesc	tupDesc;
1769 
1770 	if (type_id != RECORDOID)
1771 	{
1772 		/*
1773 		 * Check for domain or named composite type.  We might as well load
1774 		 * whichever data is needed.
1775 		 */
1776 		TypeCacheEntry *typentry;
1777 
1778 		typentry = lookup_type_cache(type_id,
1779 									 TYPECACHE_TUPDESC |
1780 									 TYPECACHE_DOMAIN_BASE_INFO);
1781 		if (typentry->typtype == TYPTYPE_DOMAIN)
1782 			return lookup_rowtype_tupdesc_noerror(typentry->domainBaseType,
1783 												  typentry->domainBaseTypmod,
1784 												  noError);
1785 		if (typentry->tupDesc == NULL && !noError)
1786 			ereport(ERROR,
1787 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1788 					 errmsg("type %s is not composite",
1789 							format_type_be(type_id))));
1790 		tupDesc = typentry->tupDesc;
1791 	}
1792 	else
1793 		tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1794 	if (tupDesc != NULL)
1795 		PinTupleDesc(tupDesc);
1796 	return tupDesc;
1797 }
1798 
1799 /*
1800  * Hash function for the hash table of RecordCacheEntry.
1801  */
1802 static uint32
record_type_typmod_hash(const void * data,size_t size)1803 record_type_typmod_hash(const void *data, size_t size)
1804 {
1805 	RecordCacheEntry *entry = (RecordCacheEntry *) data;
1806 
1807 	return hashTupleDesc(entry->tupdesc);
1808 }
1809 
1810 /*
1811  * Match function for the hash table of RecordCacheEntry.
1812  */
1813 static int
record_type_typmod_compare(const void * a,const void * b,size_t size)1814 record_type_typmod_compare(const void *a, const void *b, size_t size)
1815 {
1816 	RecordCacheEntry *left = (RecordCacheEntry *) a;
1817 	RecordCacheEntry *right = (RecordCacheEntry *) b;
1818 
1819 	return equalTupleDescs(left->tupdesc, right->tupdesc) ? 0 : 1;
1820 }
1821 
1822 /*
1823  * assign_record_type_typmod
1824  *
1825  * Given a tuple descriptor for a RECORD type, find or create a cache entry
1826  * for the type, and set the tupdesc's tdtypmod field to a value that will
1827  * identify this cache entry to lookup_rowtype_tupdesc.
1828  */
1829 void
assign_record_type_typmod(TupleDesc tupDesc)1830 assign_record_type_typmod(TupleDesc tupDesc)
1831 {
1832 	RecordCacheEntry *recentry;
1833 	TupleDesc	entDesc;
1834 	bool		found;
1835 	MemoryContext oldcxt;
1836 
1837 	Assert(tupDesc->tdtypeid == RECORDOID);
1838 
1839 	if (RecordCacheHash == NULL)
1840 	{
1841 		/* First time through: initialize the hash table */
1842 		HASHCTL		ctl;
1843 
1844 		MemSet(&ctl, 0, sizeof(ctl));
1845 		ctl.keysize = sizeof(TupleDesc);	/* just the pointer */
1846 		ctl.entrysize = sizeof(RecordCacheEntry);
1847 		ctl.hash = record_type_typmod_hash;
1848 		ctl.match = record_type_typmod_compare;
1849 		RecordCacheHash = hash_create("Record information cache", 64,
1850 									  &ctl,
1851 									  HASH_ELEM | HASH_FUNCTION | HASH_COMPARE);
1852 
1853 		/* Also make sure CacheMemoryContext exists */
1854 		if (!CacheMemoryContext)
1855 			CreateCacheMemoryContext();
1856 	}
1857 
1858 	/*
1859 	 * Find a hashtable entry for this tuple descriptor. We don't use
1860 	 * HASH_ENTER yet, because if it's missing, we need to make sure that all
1861 	 * the allocations succeed before we create the new entry.
1862 	 */
1863 	recentry = (RecordCacheEntry *) hash_search(RecordCacheHash,
1864 												(void *) &tupDesc,
1865 												HASH_FIND, &found);
1866 	if (found && recentry->tupdesc != NULL)
1867 	{
1868 		tupDesc->tdtypmod = recentry->tupdesc->tdtypmod;
1869 		return;
1870 	}
1871 
1872 	/* Not present, so need to manufacture an entry */
1873 	oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
1874 
1875 	/* Look in the SharedRecordTypmodRegistry, if attached */
1876 	entDesc = find_or_make_matching_shared_tupledesc(tupDesc);
1877 	if (entDesc == NULL)
1878 	{
1879 		/*
1880 		 * Make sure we have room before we CreateTupleDescCopy() or advance
1881 		 * NextRecordTypmod.
1882 		 */
1883 		ensure_record_cache_typmod_slot_exists(NextRecordTypmod);
1884 
1885 		/* Reference-counted local cache only. */
1886 		entDesc = CreateTupleDescCopy(tupDesc);
1887 		entDesc->tdrefcount = 1;
1888 		entDesc->tdtypmod = NextRecordTypmod++;
1889 	}
1890 	else
1891 	{
1892 		ensure_record_cache_typmod_slot_exists(entDesc->tdtypmod);
1893 	}
1894 
1895 	RecordCacheArray[entDesc->tdtypmod] = entDesc;
1896 
1897 	/* Assign a unique tupdesc identifier, too. */
1898 	RecordIdentifierArray[entDesc->tdtypmod] = ++tupledesc_id_counter;
1899 
1900 	/* Fully initialized; create the hash table entry */
1901 	recentry = (RecordCacheEntry *) hash_search(RecordCacheHash,
1902 												(void *) &tupDesc,
1903 												HASH_ENTER, NULL);
1904 	recentry->tupdesc = entDesc;
1905 
1906 	/* Update the caller's tuple descriptor. */
1907 	tupDesc->tdtypmod = entDesc->tdtypmod;
1908 
1909 	MemoryContextSwitchTo(oldcxt);
1910 }
1911 
1912 /*
1913  * assign_record_type_identifier
1914  *
1915  * Get an identifier, which will be unique over the lifespan of this backend
1916  * process, for the current tuple descriptor of the specified composite type.
1917  * For named composite types, the value is guaranteed to change if the type's
1918  * definition does.  For registered RECORD types, the value will not change
1919  * once assigned, since the registered type won't either.  If an anonymous
1920  * RECORD type is specified, we return a new identifier on each call.
1921  */
1922 uint64
assign_record_type_identifier(Oid type_id,int32 typmod)1923 assign_record_type_identifier(Oid type_id, int32 typmod)
1924 {
1925 	if (type_id != RECORDOID)
1926 	{
1927 		/*
1928 		 * It's a named composite type, so use the regular typcache.
1929 		 */
1930 		TypeCacheEntry *typentry;
1931 
1932 		typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
1933 		if (typentry->tupDesc == NULL)
1934 			ereport(ERROR,
1935 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1936 					 errmsg("type %s is not composite",
1937 							format_type_be(type_id))));
1938 		Assert(typentry->tupDesc_identifier != 0);
1939 		return typentry->tupDesc_identifier;
1940 	}
1941 	else
1942 	{
1943 		/*
1944 		 * It's a transient record type, so look in our record-type table.
1945 		 */
1946 		if (typmod >= 0 && typmod < RecordCacheArrayLen &&
1947 			RecordCacheArray[typmod] != NULL)
1948 		{
1949 			Assert(RecordIdentifierArray[typmod] != 0);
1950 			return RecordIdentifierArray[typmod];
1951 		}
1952 
1953 		/* For anonymous or unrecognized record type, generate a new ID */
1954 		return ++tupledesc_id_counter;
1955 	}
1956 }
1957 
1958 /*
1959  * Return the amount of shmem required to hold a SharedRecordTypmodRegistry.
1960  * This exists only to avoid exposing private innards of
1961  * SharedRecordTypmodRegistry in a header.
1962  */
1963 size_t
SharedRecordTypmodRegistryEstimate(void)1964 SharedRecordTypmodRegistryEstimate(void)
1965 {
1966 	return sizeof(SharedRecordTypmodRegistry);
1967 }
1968 
1969 /*
1970  * Initialize 'registry' in a pre-existing shared memory region, which must be
1971  * maximally aligned and have space for SharedRecordTypmodRegistryEstimate()
1972  * bytes.
1973  *
1974  * 'area' will be used to allocate shared memory space as required for the
1975  * typemod registration.  The current process, expected to be a leader process
1976  * in a parallel query, will be attached automatically and its current record
1977  * types will be loaded into *registry.  While attached, all calls to
1978  * assign_record_type_typmod will use the shared registry.  Worker backends
1979  * will need to attach explicitly.
1980  *
1981  * Note that this function takes 'area' and 'segment' as arguments rather than
1982  * accessing them via CurrentSession, because they aren't installed there
1983  * until after this function runs.
1984  */
1985 void
SharedRecordTypmodRegistryInit(SharedRecordTypmodRegistry * registry,dsm_segment * segment,dsa_area * area)1986 SharedRecordTypmodRegistryInit(SharedRecordTypmodRegistry *registry,
1987 							   dsm_segment *segment,
1988 							   dsa_area *area)
1989 {
1990 	MemoryContext old_context;
1991 	dshash_table *record_table;
1992 	dshash_table *typmod_table;
1993 	int32		typmod;
1994 
1995 	Assert(!IsParallelWorker());
1996 
1997 	/* We can't already be attached to a shared registry. */
1998 	Assert(CurrentSession->shared_typmod_registry == NULL);
1999 	Assert(CurrentSession->shared_record_table == NULL);
2000 	Assert(CurrentSession->shared_typmod_table == NULL);
2001 
2002 	old_context = MemoryContextSwitchTo(TopMemoryContext);
2003 
2004 	/* Create the hash table of tuple descriptors indexed by themselves. */
2005 	record_table = dshash_create(area, &srtr_record_table_params, area);
2006 
2007 	/* Create the hash table of tuple descriptors indexed by typmod. */
2008 	typmod_table = dshash_create(area, &srtr_typmod_table_params, NULL);
2009 
2010 	MemoryContextSwitchTo(old_context);
2011 
2012 	/* Initialize the SharedRecordTypmodRegistry. */
2013 	registry->record_table_handle = dshash_get_hash_table_handle(record_table);
2014 	registry->typmod_table_handle = dshash_get_hash_table_handle(typmod_table);
2015 	pg_atomic_init_u32(&registry->next_typmod, NextRecordTypmod);
2016 
2017 	/*
2018 	 * Copy all entries from this backend's private registry into the shared
2019 	 * registry.
2020 	 */
2021 	for (typmod = 0; typmod < NextRecordTypmod; ++typmod)
2022 	{
2023 		SharedTypmodTableEntry *typmod_table_entry;
2024 		SharedRecordTableEntry *record_table_entry;
2025 		SharedRecordTableKey record_table_key;
2026 		dsa_pointer shared_dp;
2027 		TupleDesc	tupdesc;
2028 		bool		found;
2029 
2030 		tupdesc = RecordCacheArray[typmod];
2031 		if (tupdesc == NULL)
2032 			continue;
2033 
2034 		/* Copy the TupleDesc into shared memory. */
2035 		shared_dp = share_tupledesc(area, tupdesc, typmod);
2036 
2037 		/* Insert into the typmod table. */
2038 		typmod_table_entry = dshash_find_or_insert(typmod_table,
2039 												   &tupdesc->tdtypmod,
2040 												   &found);
2041 		if (found)
2042 			elog(ERROR, "cannot create duplicate shared record typmod");
2043 		typmod_table_entry->typmod = tupdesc->tdtypmod;
2044 		typmod_table_entry->shared_tupdesc = shared_dp;
2045 		dshash_release_lock(typmod_table, typmod_table_entry);
2046 
2047 		/* Insert into the record table. */
2048 		record_table_key.shared = false;
2049 		record_table_key.u.local_tupdesc = tupdesc;
2050 		record_table_entry = dshash_find_or_insert(record_table,
2051 												   &record_table_key,
2052 												   &found);
2053 		if (!found)
2054 		{
2055 			record_table_entry->key.shared = true;
2056 			record_table_entry->key.u.shared_tupdesc = shared_dp;
2057 		}
2058 		dshash_release_lock(record_table, record_table_entry);
2059 	}
2060 
2061 	/*
2062 	 * Set up the global state that will tell assign_record_type_typmod and
2063 	 * lookup_rowtype_tupdesc_internal about the shared registry.
2064 	 */
2065 	CurrentSession->shared_record_table = record_table;
2066 	CurrentSession->shared_typmod_table = typmod_table;
2067 	CurrentSession->shared_typmod_registry = registry;
2068 
2069 	/*
2070 	 * We install a detach hook in the leader, but only to handle cleanup on
2071 	 * failure during GetSessionDsmHandle().  Once GetSessionDsmHandle() pins
2072 	 * the memory, the leader process will use a shared registry until it
2073 	 * exits.
2074 	 */
2075 	on_dsm_detach(segment, shared_record_typmod_registry_detach, (Datum) 0);
2076 }
2077 
2078 /*
2079  * Attach to 'registry', which must have been initialized already by another
2080  * backend.  Future calls to assign_record_type_typmod and
2081  * lookup_rowtype_tupdesc_internal will use the shared registry until the
2082  * current session is detached.
2083  */
2084 void
SharedRecordTypmodRegistryAttach(SharedRecordTypmodRegistry * registry)2085 SharedRecordTypmodRegistryAttach(SharedRecordTypmodRegistry *registry)
2086 {
2087 	MemoryContext old_context;
2088 	dshash_table *record_table;
2089 	dshash_table *typmod_table;
2090 
2091 	Assert(IsParallelWorker());
2092 
2093 	/* We can't already be attached to a shared registry. */
2094 	Assert(CurrentSession != NULL);
2095 	Assert(CurrentSession->segment != NULL);
2096 	Assert(CurrentSession->area != NULL);
2097 	Assert(CurrentSession->shared_typmod_registry == NULL);
2098 	Assert(CurrentSession->shared_record_table == NULL);
2099 	Assert(CurrentSession->shared_typmod_table == NULL);
2100 
2101 	/*
2102 	 * We can't already have typmods in our local cache, because they'd clash
2103 	 * with those imported by SharedRecordTypmodRegistryInit.  This should be
2104 	 * a freshly started parallel worker.  If we ever support worker
2105 	 * recycling, a worker would need to zap its local cache in between
2106 	 * servicing different queries, in order to be able to call this and
2107 	 * synchronize typmods with a new leader; but that's problematic because
2108 	 * we can't be very sure that record-typmod-related state hasn't escaped
2109 	 * to anywhere else in the process.
2110 	 */
2111 	Assert(NextRecordTypmod == 0);
2112 
2113 	old_context = MemoryContextSwitchTo(TopMemoryContext);
2114 
2115 	/* Attach to the two hash tables. */
2116 	record_table = dshash_attach(CurrentSession->area,
2117 								 &srtr_record_table_params,
2118 								 registry->record_table_handle,
2119 								 CurrentSession->area);
2120 	typmod_table = dshash_attach(CurrentSession->area,
2121 								 &srtr_typmod_table_params,
2122 								 registry->typmod_table_handle,
2123 								 NULL);
2124 
2125 	MemoryContextSwitchTo(old_context);
2126 
2127 	/*
2128 	 * Set up detach hook to run at worker exit.  Currently this is the same
2129 	 * as the leader's detach hook, but in future they might need to be
2130 	 * different.
2131 	 */
2132 	on_dsm_detach(CurrentSession->segment,
2133 				  shared_record_typmod_registry_detach,
2134 				  PointerGetDatum(registry));
2135 
2136 	/*
2137 	 * Set up the session state that will tell assign_record_type_typmod and
2138 	 * lookup_rowtype_tupdesc_internal about the shared registry.
2139 	 */
2140 	CurrentSession->shared_typmod_registry = registry;
2141 	CurrentSession->shared_record_table = record_table;
2142 	CurrentSession->shared_typmod_table = typmod_table;
2143 }
2144 
2145 /*
2146  * TypeCacheRelCallback
2147  *		Relcache inval callback function
2148  *
2149  * Delete the cached tuple descriptor (if any) for the given rel's composite
2150  * type, or for all composite types if relid == InvalidOid.  Also reset
2151  * whatever info we have cached about the composite type's comparability.
2152  *
2153  * This is called when a relcache invalidation event occurs for the given
2154  * relid.  We must scan the whole typcache hash since we don't know the
2155  * type OID corresponding to the relid.  We could do a direct search if this
2156  * were a syscache-flush callback on pg_type, but then we would need all
2157  * ALTER-TABLE-like commands that could modify a rowtype to issue syscache
2158  * invals against the rel's pg_type OID.  The extra SI signaling could very
2159  * well cost more than we'd save, since in most usages there are not very
2160  * many entries in a backend's typcache.  The risk of bugs-of-omission seems
2161  * high, too.
2162  *
2163  * Another possibility, with only localized impact, is to maintain a second
2164  * hashtable that indexes composite-type typcache entries by their typrelid.
2165  * But it's still not clear it's worth the trouble.
2166  */
2167 static void
TypeCacheRelCallback(Datum arg,Oid relid)2168 TypeCacheRelCallback(Datum arg, Oid relid)
2169 {
2170 	HASH_SEQ_STATUS status;
2171 	TypeCacheEntry *typentry;
2172 
2173 	/* TypeCacheHash must exist, else this callback wouldn't be registered */
2174 	hash_seq_init(&status, TypeCacheHash);
2175 	while ((typentry = (TypeCacheEntry *) hash_seq_search(&status)) != NULL)
2176 	{
2177 		if (typentry->typtype == TYPTYPE_COMPOSITE)
2178 		{
2179 			/* Skip if no match, unless we're zapping all composite types */
2180 			if (relid != typentry->typrelid && relid != InvalidOid)
2181 				continue;
2182 
2183 			/* Delete tupdesc if we have it */
2184 			if (typentry->tupDesc != NULL)
2185 			{
2186 				/*
2187 				 * Release our refcount, and free the tupdesc if none remain.
2188 				 * (Can't use DecrTupleDescRefCount because this reference is
2189 				 * not logged in current resource owner.)
2190 				 */
2191 				Assert(typentry->tupDesc->tdrefcount > 0);
2192 				if (--typentry->tupDesc->tdrefcount == 0)
2193 					FreeTupleDesc(typentry->tupDesc);
2194 				typentry->tupDesc = NULL;
2195 
2196 				/*
2197 				 * Also clear tupDesc_identifier, so that anything watching
2198 				 * that will realize that the tupdesc has possibly changed.
2199 				 * (Alternatively, we could specify that to detect possible
2200 				 * tupdesc change, one must check for tupDesc != NULL as well
2201 				 * as tupDesc_identifier being the same as what was previously
2202 				 * seen.  That seems error-prone.)
2203 				 */
2204 				typentry->tupDesc_identifier = 0;
2205 			}
2206 
2207 			/* Reset equality/comparison/hashing validity information */
2208 			typentry->flags &= ~TCFLAGS_OPERATOR_FLAGS;
2209 		}
2210 		else if (typentry->typtype == TYPTYPE_DOMAIN)
2211 		{
2212 			/*
2213 			 * If it's domain over composite, reset flags.  (We don't bother
2214 			 * trying to determine whether the specific base type needs a
2215 			 * reset.)  Note that if we haven't determined whether the base
2216 			 * type is composite, we don't need to reset anything.
2217 			 */
2218 			if (typentry->flags & TCFLAGS_DOMAIN_BASE_IS_COMPOSITE)
2219 				typentry->flags &= ~TCFLAGS_OPERATOR_FLAGS;
2220 		}
2221 	}
2222 }
2223 
2224 /*
2225  * TypeCacheTypCallback
2226  *		Syscache inval callback function
2227  *
2228  * This is called when a syscache invalidation event occurs for any
2229  * pg_type row.  If we have information cached about that type, mark
2230  * it as needing to be reloaded.
2231  */
2232 static void
TypeCacheTypCallback(Datum arg,int cacheid,uint32 hashvalue)2233 TypeCacheTypCallback(Datum arg, int cacheid, uint32 hashvalue)
2234 {
2235 	HASH_SEQ_STATUS status;
2236 	TypeCacheEntry *typentry;
2237 
2238 	/* TypeCacheHash must exist, else this callback wouldn't be registered */
2239 	hash_seq_init(&status, TypeCacheHash);
2240 	while ((typentry = (TypeCacheEntry *) hash_seq_search(&status)) != NULL)
2241 	{
2242 		/* Is this the targeted type row (or it's a total cache flush)? */
2243 		if (hashvalue == 0 || typentry->type_id_hash == hashvalue)
2244 		{
2245 			/*
2246 			 * Mark the data obtained directly from pg_type as invalid.  Also,
2247 			 * if it's a domain, typnotnull might've changed, so we'll need to
2248 			 * recalculate its constraints.
2249 			 */
2250 			typentry->flags &= ~(TCFLAGS_HAVE_PG_TYPE_DATA |
2251 								 TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS);
2252 		}
2253 	}
2254 }
2255 
2256 /*
2257  * TypeCacheOpcCallback
2258  *		Syscache inval callback function
2259  *
2260  * This is called when a syscache invalidation event occurs for any pg_opclass
2261  * row.  In principle we could probably just invalidate data dependent on the
2262  * particular opclass, but since updates on pg_opclass are rare in production
2263  * it doesn't seem worth a lot of complication: we just mark all cached data
2264  * invalid.
2265  *
2266  * Note that we don't bother watching for updates on pg_amop or pg_amproc.
2267  * This should be safe because ALTER OPERATOR FAMILY ADD/DROP OPERATOR/FUNCTION
2268  * is not allowed to be used to add/drop the primary operators and functions
2269  * of an opclass, only cross-type members of a family; and the latter sorts
2270  * of members are not going to get cached here.
2271  */
2272 static void
TypeCacheOpcCallback(Datum arg,int cacheid,uint32 hashvalue)2273 TypeCacheOpcCallback(Datum arg, int cacheid, uint32 hashvalue)
2274 {
2275 	HASH_SEQ_STATUS status;
2276 	TypeCacheEntry *typentry;
2277 
2278 	/* TypeCacheHash must exist, else this callback wouldn't be registered */
2279 	hash_seq_init(&status, TypeCacheHash);
2280 	while ((typentry = (TypeCacheEntry *) hash_seq_search(&status)) != NULL)
2281 	{
2282 		/* Reset equality/comparison/hashing validity information */
2283 		typentry->flags &= ~TCFLAGS_OPERATOR_FLAGS;
2284 	}
2285 }
2286 
2287 /*
2288  * TypeCacheConstrCallback
2289  *		Syscache inval callback function
2290  *
2291  * This is called when a syscache invalidation event occurs for any
2292  * pg_constraint row.  We flush information about domain constraints
2293  * when this happens.
2294  *
2295  * It's slightly annoying that we can't tell whether the inval event was for
2296  * a domain constraint record or not; there's usually more update traffic
2297  * for table constraints than domain constraints, so we'll do a lot of
2298  * useless flushes.  Still, this is better than the old no-caching-at-all
2299  * approach to domain constraints.
2300  */
2301 static void
TypeCacheConstrCallback(Datum arg,int cacheid,uint32 hashvalue)2302 TypeCacheConstrCallback(Datum arg, int cacheid, uint32 hashvalue)
2303 {
2304 	TypeCacheEntry *typentry;
2305 
2306 	/*
2307 	 * Because this is called very frequently, and typically very few of the
2308 	 * typcache entries are for domains, we don't use hash_seq_search here.
2309 	 * Instead we thread all the domain-type entries together so that we can
2310 	 * visit them cheaply.
2311 	 */
2312 	for (typentry = firstDomainTypeEntry;
2313 		 typentry != NULL;
2314 		 typentry = typentry->nextDomain)
2315 	{
2316 		/* Reset domain constraint validity information */
2317 		typentry->flags &= ~TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS;
2318 	}
2319 }
2320 
2321 
2322 /*
2323  * Check if given OID is part of the subset that's sortable by comparisons
2324  */
2325 static inline bool
enum_known_sorted(TypeCacheEnumData * enumdata,Oid arg)2326 enum_known_sorted(TypeCacheEnumData *enumdata, Oid arg)
2327 {
2328 	Oid			offset;
2329 
2330 	if (arg < enumdata->bitmap_base)
2331 		return false;
2332 	offset = arg - enumdata->bitmap_base;
2333 	if (offset > (Oid) INT_MAX)
2334 		return false;
2335 	return bms_is_member((int) offset, enumdata->sorted_values);
2336 }
2337 
2338 
2339 /*
2340  * compare_values_of_enum
2341  *		Compare two members of an enum type.
2342  *		Return <0, 0, or >0 according as arg1 <, =, or > arg2.
2343  *
2344  * Note: currently, the enumData cache is refreshed only if we are asked
2345  * to compare an enum value that is not already in the cache.  This is okay
2346  * because there is no support for re-ordering existing values, so comparisons
2347  * of previously cached values will return the right answer even if other
2348  * values have been added since we last loaded the cache.
2349  *
2350  * Note: the enum logic has a special-case rule about even-numbered versus
2351  * odd-numbered OIDs, but we take no account of that rule here; this
2352  * routine shouldn't even get called when that rule applies.
2353  */
2354 int
compare_values_of_enum(TypeCacheEntry * tcache,Oid arg1,Oid arg2)2355 compare_values_of_enum(TypeCacheEntry *tcache, Oid arg1, Oid arg2)
2356 {
2357 	TypeCacheEnumData *enumdata;
2358 	EnumItem   *item1;
2359 	EnumItem   *item2;
2360 
2361 	/*
2362 	 * Equal OIDs are certainly equal --- this case was probably handled by
2363 	 * our caller, but we may as well check.
2364 	 */
2365 	if (arg1 == arg2)
2366 		return 0;
2367 
2368 	/* Load up the cache if first time through */
2369 	if (tcache->enumData == NULL)
2370 		load_enum_cache_data(tcache);
2371 	enumdata = tcache->enumData;
2372 
2373 	/*
2374 	 * If both OIDs are known-sorted, we can just compare them directly.
2375 	 */
2376 	if (enum_known_sorted(enumdata, arg1) &&
2377 		enum_known_sorted(enumdata, arg2))
2378 	{
2379 		if (arg1 < arg2)
2380 			return -1;
2381 		else
2382 			return 1;
2383 	}
2384 
2385 	/*
2386 	 * Slow path: we have to identify their actual sort-order positions.
2387 	 */
2388 	item1 = find_enumitem(enumdata, arg1);
2389 	item2 = find_enumitem(enumdata, arg2);
2390 
2391 	if (item1 == NULL || item2 == NULL)
2392 	{
2393 		/*
2394 		 * We couldn't find one or both values.  That means the enum has
2395 		 * changed under us, so re-initialize the cache and try again. We
2396 		 * don't bother retrying the known-sorted case in this path.
2397 		 */
2398 		load_enum_cache_data(tcache);
2399 		enumdata = tcache->enumData;
2400 
2401 		item1 = find_enumitem(enumdata, arg1);
2402 		item2 = find_enumitem(enumdata, arg2);
2403 
2404 		/*
2405 		 * If we still can't find the values, complain: we must have corrupt
2406 		 * data.
2407 		 */
2408 		if (item1 == NULL)
2409 			elog(ERROR, "enum value %u not found in cache for enum %s",
2410 				 arg1, format_type_be(tcache->type_id));
2411 		if (item2 == NULL)
2412 			elog(ERROR, "enum value %u not found in cache for enum %s",
2413 				 arg2, format_type_be(tcache->type_id));
2414 	}
2415 
2416 	if (item1->sort_order < item2->sort_order)
2417 		return -1;
2418 	else if (item1->sort_order > item2->sort_order)
2419 		return 1;
2420 	else
2421 		return 0;
2422 }
2423 
2424 /*
2425  * Load (or re-load) the enumData member of the typcache entry.
2426  */
2427 static void
load_enum_cache_data(TypeCacheEntry * tcache)2428 load_enum_cache_data(TypeCacheEntry *tcache)
2429 {
2430 	TypeCacheEnumData *enumdata;
2431 	Relation	enum_rel;
2432 	SysScanDesc enum_scan;
2433 	HeapTuple	enum_tuple;
2434 	ScanKeyData skey;
2435 	EnumItem   *items;
2436 	int			numitems;
2437 	int			maxitems;
2438 	Oid			bitmap_base;
2439 	Bitmapset  *bitmap;
2440 	MemoryContext oldcxt;
2441 	int			bm_size,
2442 				start_pos;
2443 
2444 	/* Check that this is actually an enum */
2445 	if (tcache->typtype != TYPTYPE_ENUM)
2446 		ereport(ERROR,
2447 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
2448 				 errmsg("%s is not an enum",
2449 						format_type_be(tcache->type_id))));
2450 
2451 	/*
2452 	 * Read all the information for members of the enum type.  We collect the
2453 	 * info in working memory in the caller's context, and then transfer it to
2454 	 * permanent memory in CacheMemoryContext.  This minimizes the risk of
2455 	 * leaking memory from CacheMemoryContext in the event of an error partway
2456 	 * through.
2457 	 */
2458 	maxitems = 64;
2459 	items = (EnumItem *) palloc(sizeof(EnumItem) * maxitems);
2460 	numitems = 0;
2461 
2462 	/* Scan pg_enum for the members of the target enum type. */
2463 	ScanKeyInit(&skey,
2464 				Anum_pg_enum_enumtypid,
2465 				BTEqualStrategyNumber, F_OIDEQ,
2466 				ObjectIdGetDatum(tcache->type_id));
2467 
2468 	enum_rel = table_open(EnumRelationId, AccessShareLock);
2469 	enum_scan = systable_beginscan(enum_rel,
2470 								   EnumTypIdLabelIndexId,
2471 								   true, NULL,
2472 								   1, &skey);
2473 
2474 	while (HeapTupleIsValid(enum_tuple = systable_getnext(enum_scan)))
2475 	{
2476 		Form_pg_enum en = (Form_pg_enum) GETSTRUCT(enum_tuple);
2477 
2478 		if (numitems >= maxitems)
2479 		{
2480 			maxitems *= 2;
2481 			items = (EnumItem *) repalloc(items, sizeof(EnumItem) * maxitems);
2482 		}
2483 		items[numitems].enum_oid = en->oid;
2484 		items[numitems].sort_order = en->enumsortorder;
2485 		numitems++;
2486 	}
2487 
2488 	systable_endscan(enum_scan);
2489 	table_close(enum_rel, AccessShareLock);
2490 
2491 	/* Sort the items into OID order */
2492 	qsort(items, numitems, sizeof(EnumItem), enum_oid_cmp);
2493 
2494 	/*
2495 	 * Here, we create a bitmap listing a subset of the enum's OIDs that are
2496 	 * known to be in order and can thus be compared with just OID comparison.
2497 	 *
2498 	 * The point of this is that the enum's initial OIDs were certainly in
2499 	 * order, so there is some subset that can be compared via OID comparison;
2500 	 * and we'd rather not do binary searches unnecessarily.
2501 	 *
2502 	 * This is somewhat heuristic, and might identify a subset of OIDs that
2503 	 * isn't exactly what the type started with.  That's okay as long as the
2504 	 * subset is correctly sorted.
2505 	 */
2506 	bitmap_base = InvalidOid;
2507 	bitmap = NULL;
2508 	bm_size = 1;				/* only save sets of at least 2 OIDs */
2509 
2510 	for (start_pos = 0; start_pos < numitems - 1; start_pos++)
2511 	{
2512 		/*
2513 		 * Identify longest sorted subsequence starting at start_pos
2514 		 */
2515 		Bitmapset  *this_bitmap = bms_make_singleton(0);
2516 		int			this_bm_size = 1;
2517 		Oid			start_oid = items[start_pos].enum_oid;
2518 		float4		prev_order = items[start_pos].sort_order;
2519 		int			i;
2520 
2521 		for (i = start_pos + 1; i < numitems; i++)
2522 		{
2523 			Oid			offset;
2524 
2525 			offset = items[i].enum_oid - start_oid;
2526 			/* quit if bitmap would be too large; cutoff is arbitrary */
2527 			if (offset >= 8192)
2528 				break;
2529 			/* include the item if it's in-order */
2530 			if (items[i].sort_order > prev_order)
2531 			{
2532 				prev_order = items[i].sort_order;
2533 				this_bitmap = bms_add_member(this_bitmap, (int) offset);
2534 				this_bm_size++;
2535 			}
2536 		}
2537 
2538 		/* Remember it if larger than previous best */
2539 		if (this_bm_size > bm_size)
2540 		{
2541 			bms_free(bitmap);
2542 			bitmap_base = start_oid;
2543 			bitmap = this_bitmap;
2544 			bm_size = this_bm_size;
2545 		}
2546 		else
2547 			bms_free(this_bitmap);
2548 
2549 		/*
2550 		 * Done if it's not possible to find a longer sequence in the rest of
2551 		 * the list.  In typical cases this will happen on the first
2552 		 * iteration, which is why we create the bitmaps on the fly instead of
2553 		 * doing a second pass over the list.
2554 		 */
2555 		if (bm_size >= (numitems - start_pos - 1))
2556 			break;
2557 	}
2558 
2559 	/* OK, copy the data into CacheMemoryContext */
2560 	oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2561 	enumdata = (TypeCacheEnumData *)
2562 		palloc(offsetof(TypeCacheEnumData, enum_values) +
2563 			   numitems * sizeof(EnumItem));
2564 	enumdata->bitmap_base = bitmap_base;
2565 	enumdata->sorted_values = bms_copy(bitmap);
2566 	enumdata->num_values = numitems;
2567 	memcpy(enumdata->enum_values, items, numitems * sizeof(EnumItem));
2568 	MemoryContextSwitchTo(oldcxt);
2569 
2570 	pfree(items);
2571 	bms_free(bitmap);
2572 
2573 	/* And link the finished cache struct into the typcache */
2574 	if (tcache->enumData != NULL)
2575 		pfree(tcache->enumData);
2576 	tcache->enumData = enumdata;
2577 }
2578 
2579 /*
2580  * Locate the EnumItem with the given OID, if present
2581  */
2582 static EnumItem *
find_enumitem(TypeCacheEnumData * enumdata,Oid arg)2583 find_enumitem(TypeCacheEnumData *enumdata, Oid arg)
2584 {
2585 	EnumItem	srch;
2586 
2587 	/* On some versions of Solaris, bsearch of zero items dumps core */
2588 	if (enumdata->num_values <= 0)
2589 		return NULL;
2590 
2591 	srch.enum_oid = arg;
2592 	return bsearch(&srch, enumdata->enum_values, enumdata->num_values,
2593 				   sizeof(EnumItem), enum_oid_cmp);
2594 }
2595 
2596 /*
2597  * qsort comparison function for OID-ordered EnumItems
2598  */
2599 static int
enum_oid_cmp(const void * left,const void * right)2600 enum_oid_cmp(const void *left, const void *right)
2601 {
2602 	const EnumItem *l = (const EnumItem *) left;
2603 	const EnumItem *r = (const EnumItem *) right;
2604 
2605 	if (l->enum_oid < r->enum_oid)
2606 		return -1;
2607 	else if (l->enum_oid > r->enum_oid)
2608 		return 1;
2609 	else
2610 		return 0;
2611 }
2612 
2613 /*
2614  * Copy 'tupdesc' into newly allocated shared memory in 'area', set its typmod
2615  * to the given value and return a dsa_pointer.
2616  */
2617 static dsa_pointer
share_tupledesc(dsa_area * area,TupleDesc tupdesc,uint32 typmod)2618 share_tupledesc(dsa_area *area, TupleDesc tupdesc, uint32 typmod)
2619 {
2620 	dsa_pointer shared_dp;
2621 	TupleDesc	shared;
2622 
2623 	shared_dp = dsa_allocate(area, TupleDescSize(tupdesc));
2624 	shared = (TupleDesc) dsa_get_address(area, shared_dp);
2625 	TupleDescCopy(shared, tupdesc);
2626 	shared->tdtypmod = typmod;
2627 
2628 	return shared_dp;
2629 }
2630 
2631 /*
2632  * If we are attached to a SharedRecordTypmodRegistry, use it to find or
2633  * create a shared TupleDesc that matches 'tupdesc'.  Otherwise return NULL.
2634  * Tuple descriptors returned by this function are not reference counted, and
2635  * will exist at least as long as the current backend remained attached to the
2636  * current session.
2637  */
2638 static TupleDesc
find_or_make_matching_shared_tupledesc(TupleDesc tupdesc)2639 find_or_make_matching_shared_tupledesc(TupleDesc tupdesc)
2640 {
2641 	TupleDesc	result;
2642 	SharedRecordTableKey key;
2643 	SharedRecordTableEntry *record_table_entry;
2644 	SharedTypmodTableEntry *typmod_table_entry;
2645 	dsa_pointer shared_dp;
2646 	bool		found;
2647 	uint32		typmod;
2648 
2649 	/* If not even attached, nothing to do. */
2650 	if (CurrentSession->shared_typmod_registry == NULL)
2651 		return NULL;
2652 
2653 	/* Try to find a matching tuple descriptor in the record table. */
2654 	key.shared = false;
2655 	key.u.local_tupdesc = tupdesc;
2656 	record_table_entry = (SharedRecordTableEntry *)
2657 		dshash_find(CurrentSession->shared_record_table, &key, false);
2658 	if (record_table_entry)
2659 	{
2660 		Assert(record_table_entry->key.shared);
2661 		dshash_release_lock(CurrentSession->shared_record_table,
2662 							record_table_entry);
2663 		result = (TupleDesc)
2664 			dsa_get_address(CurrentSession->area,
2665 							record_table_entry->key.u.shared_tupdesc);
2666 		Assert(result->tdrefcount == -1);
2667 
2668 		return result;
2669 	}
2670 
2671 	/* Allocate a new typmod number.  This will be wasted if we error out. */
2672 	typmod = (int)
2673 		pg_atomic_fetch_add_u32(&CurrentSession->shared_typmod_registry->next_typmod,
2674 								1);
2675 
2676 	/* Copy the TupleDesc into shared memory. */
2677 	shared_dp = share_tupledesc(CurrentSession->area, tupdesc, typmod);
2678 
2679 	/*
2680 	 * Create an entry in the typmod table so that others will understand this
2681 	 * typmod number.
2682 	 */
2683 	PG_TRY();
2684 	{
2685 		typmod_table_entry = (SharedTypmodTableEntry *)
2686 			dshash_find_or_insert(CurrentSession->shared_typmod_table,
2687 								  &typmod, &found);
2688 		if (found)
2689 			elog(ERROR, "cannot create duplicate shared record typmod");
2690 	}
2691 	PG_CATCH();
2692 	{
2693 		dsa_free(CurrentSession->area, shared_dp);
2694 		PG_RE_THROW();
2695 	}
2696 	PG_END_TRY();
2697 	typmod_table_entry->typmod = typmod;
2698 	typmod_table_entry->shared_tupdesc = shared_dp;
2699 	dshash_release_lock(CurrentSession->shared_typmod_table,
2700 						typmod_table_entry);
2701 
2702 	/*
2703 	 * Finally create an entry in the record table so others with matching
2704 	 * tuple descriptors can reuse the typmod.
2705 	 */
2706 	record_table_entry = (SharedRecordTableEntry *)
2707 		dshash_find_or_insert(CurrentSession->shared_record_table, &key,
2708 							  &found);
2709 	if (found)
2710 	{
2711 		/*
2712 		 * Someone concurrently inserted a matching tuple descriptor since the
2713 		 * first time we checked.  Use that one instead.
2714 		 */
2715 		dshash_release_lock(CurrentSession->shared_record_table,
2716 							record_table_entry);
2717 
2718 		/* Might as well free up the space used by the one we created. */
2719 		found = dshash_delete_key(CurrentSession->shared_typmod_table,
2720 								  &typmod);
2721 		Assert(found);
2722 		dsa_free(CurrentSession->area, shared_dp);
2723 
2724 		/* Return the one we found. */
2725 		Assert(record_table_entry->key.shared);
2726 		result = (TupleDesc)
2727 			dsa_get_address(CurrentSession->area,
2728 							record_table_entry->key.u.shared_tupdesc);
2729 		Assert(result->tdrefcount == -1);
2730 
2731 		return result;
2732 	}
2733 
2734 	/* Store it and return it. */
2735 	record_table_entry->key.shared = true;
2736 	record_table_entry->key.u.shared_tupdesc = shared_dp;
2737 	dshash_release_lock(CurrentSession->shared_record_table,
2738 						record_table_entry);
2739 	result = (TupleDesc)
2740 		dsa_get_address(CurrentSession->area, shared_dp);
2741 	Assert(result->tdrefcount == -1);
2742 
2743 	return result;
2744 }
2745 
2746 /*
2747  * On-DSM-detach hook to forget about the current shared record typmod
2748  * infrastructure.  This is currently used by both leader and workers.
2749  */
2750 static void
shared_record_typmod_registry_detach(dsm_segment * segment,Datum datum)2751 shared_record_typmod_registry_detach(dsm_segment *segment, Datum datum)
2752 {
2753 	/* Be cautious here: maybe we didn't finish initializing. */
2754 	if (CurrentSession->shared_record_table != NULL)
2755 	{
2756 		dshash_detach(CurrentSession->shared_record_table);
2757 		CurrentSession->shared_record_table = NULL;
2758 	}
2759 	if (CurrentSession->shared_typmod_table != NULL)
2760 	{
2761 		dshash_detach(CurrentSession->shared_typmod_table);
2762 		CurrentSession->shared_typmod_table = NULL;
2763 	}
2764 	CurrentSession->shared_typmod_registry = NULL;
2765 }
2766