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