LCOV - differential code coverage report
Current view: top level - src/backend/utils/cache - typcache.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 89.0 % 972 865 2 105 4 1 860 2 1
Current Date: 2026-11-28 16:48:44 +0900 Functions: 96.7 % 60 58 2 2 56
Baseline: lcov-20261128-baseline Branches: 70.0 % 674 472 202 3 469
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 94.7 % 19 18 1 18
(30,360] days: 76.3 % 38 29 2 7 1 28
(360..) days: 89.4 % 915 818 97 4 814
Function coverage date bins:
(30,360] days: 100.0 % 4 4 4
(360..) days: 96.4 % 56 54 2 2 52
Branch coverage date bins:
(7,30] days: 62.5 % 8 5 3 5
(30,360] days: 37.5 % 24 9 15 9
(360..) days: 71.3 % 642 458 184 3 455

 Age         Owner                    Branch data    TLA  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
 3362 andres@anarazel.de        231                 :CBC         137 : shared_record_table_compare(const void *a, const void *b, size_t size,
                                232                 :                :                             void *arg)
                                233                 :                : {
                                234                 :            137 :     dsa_area   *area = (dsa_area *) arg;
  320 peter@eisentraut.org      235                 :            137 :     const SharedRecordTableKey *k1 = a;
                                236                 :            137 :     const SharedRecordTableKey *k2 = b;
                                237                 :                :     TupleDesc   t1;
                                238                 :                :     TupleDesc   t2;
                                239                 :                : 
 3362 andres@anarazel.de        240         [ -  + ]:            137 :     if (k1->shared)
 3361 tgl@sss.pgh.pa.us         241                 :UBC           0 :         t1 = (TupleDesc) dsa_get_address(area, k1->u.shared_tupdesc);
                                242                 :                :     else
 3361 tgl@sss.pgh.pa.us         243                 :CBC         137 :         t1 = k1->u.local_tupdesc;
                                244                 :                : 
 3362 andres@anarazel.de        245         [ +  - ]:            137 :     if (k2->shared)
 3361 tgl@sss.pgh.pa.us         246                 :            137 :         t2 = (TupleDesc) dsa_get_address(area, k2->u.shared_tupdesc);
                                247                 :                :     else
 3361 tgl@sss.pgh.pa.us         248                 :UBC           0 :         t2 = k2->u.local_tupdesc;
                                249                 :                : 
  986 peter@eisentraut.org      250                 :CBC         137 :     return equalRowTypes(t1, t2) ? 0 : 1;
                                251                 :                : }
                                252                 :                : 
                                253                 :                : /*
                                254                 :                :  * A hash function for SharedRecordTableKey.
                                255                 :                :  */
                                256                 :                : static uint32
 3362 andres@anarazel.de        257                 :            362 : shared_record_table_hash(const void *a, size_t size, void *arg)
                                258                 :                : {
  320 peter@eisentraut.org      259                 :            362 :     dsa_area   *area = arg;
                                260                 :            362 :     const SharedRecordTableKey *k = a;
                                261                 :                :     TupleDesc   t;
                                262                 :                : 
 3362 andres@anarazel.de        263         [ -  + ]:            362 :     if (k->shared)
 3361 tgl@sss.pgh.pa.us         264                 :UBC           0 :         t = (TupleDesc) dsa_get_address(area, k->u.shared_tupdesc);
                                265                 :                :     else
 3361 tgl@sss.pgh.pa.us         266                 :CBC         362 :         t = k->u.local_tupdesc;
                                267                 :                : 
  986 peter@eisentraut.org      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
  843 akorotkov@postgresql      358                 :         563874 : type_cache_syshash(const void *key, Size keysize)
                                359                 :                : {
                                360         [ -  + ]:         563874 :     Assert(keysize == sizeof(Oid));
                                361                 :         563874 :     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 *
 8504 tgl@sss.pgh.pa.us         385                 :         502903 : lookup_type_cache(Oid type_id, int flags)
                                386                 :                : {
                                387                 :                :     TypeCacheEntry *typentry;
                                388                 :                :     bool        found;
                                389                 :                : 
   19 rhaas@postgresql.org      390         [ +  + ]:         502903 :     if (RelIdToTypeIdCacheHash == NULL)
                                391                 :                :     {
                                392                 :                :         /* First time through: initialize the hash table */
                                393                 :                :         HASHCTL     ctl;
                                394                 :                : 
  161 michael@paquier.xyz       395         [ +  - ]:           4149 :         if (TypeCacheHash == NULL)
                                396                 :                :         {
                                397                 :           4149 :             ctl.keysize = sizeof(Oid);
                                398                 :           4149 :             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                 :           4149 :             ctl.hash = type_cache_syshash;
                                406                 :                : 
                                407                 :           4149 :             TypeCacheHash = hash_create("Type information cache", 64,
                                408                 :                :                                         &ctl, HASH_ELEM | HASH_FUNCTION);
                                409                 :                :         }
                                410                 :                : 
                                411                 :                :         /* Also make sure CacheMemoryContext exists */
 6180 tgl@sss.pgh.pa.us         412         [ -  + ]:           4149 :         if (!CacheMemoryContext)
 6180 tgl@sss.pgh.pa.us         413                 :UBC           0 :             CreateCacheMemoryContext();
                                414                 :                : 
   19 rhaas@postgresql.org      415                 :CBC        4149 :         ctl.keysize = sizeof(Oid);
                                416                 :           4149 :         ctl.entrysize = sizeof(RelIdToTypeIdCacheEntry);
                                417                 :           4149 :         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                 :                :          */
  161 michael@paquier.xyz       425                 :           4149 :         CacheRegisterRelcacheCallback(TypeCacheRelCallback, (Datum) 0);
                                426                 :           4149 :         CacheRegisterSyscacheCallback(TYPEOID, TypeCacheTypCallback, (Datum) 0);
                                427                 :           4149 :         CacheRegisterSyscacheCallback(CLAOID, TypeCacheOpcCallback, (Datum) 0);
                                428                 :           4149 :         CacheRegisterSyscacheCallback(CONSTROID, TypeCacheConstrCallback, (Datum) 0);
                                429                 :                :     }
                                430                 :                : 
  765 akorotkov@postgresql      431   [ +  -  -  + ]:         502903 :     Assert(TypeCacheHash != NULL && RelIdToTypeIdCacheHash != NULL);
                                432                 :                : 
                                433                 :                :     /* Try to look up an existing entry */
 8504 tgl@sss.pgh.pa.us         434                 :         502903 :     typentry = (TypeCacheEntry *) hash_search(TypeCacheHash,
                                435                 :                :                                               &type_id,
                                436                 :                :                                               HASH_FIND, NULL);
                                437                 :                : 
                                438         [ +  + ]:         502903 :     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                 :                : 
 6131 rhaas@postgresql.org      450                 :          20409 :         tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
 8276 tgl@sss.pgh.pa.us         451         [ -  + ]:          20409 :         if (!HeapTupleIsValid(tp))
 3732 tgl@sss.pgh.pa.us         452         [ #  # ]:UBC           0 :             ereport(ERROR,
                                453                 :                :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
                                454                 :                :                      errmsg("type with OID %u does not exist", type_id)));
 8276 tgl@sss.pgh.pa.us         455                 :CBC       20409 :         typtup = (Form_pg_type) GETSTRUCT(tp);
                                456         [ -  + ]:          20409 :         if (!typtup->typisdefined)
 8276 tgl@sss.pgh.pa.us         457         [ #  # ]:UBC           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 */
 8504 tgl@sss.pgh.pa.us         463                 :CBC       20409 :         typentry = (TypeCacheEntry *) hash_search(TypeCacheHash,
                                464                 :                :                                                   &type_id,
                                465                 :                :                                                   HASH_ENTER, &found);
                                466         [ -  + ]:          20409 :         Assert(!found);         /* it wasn't there a moment ago */
                                467                 :                : 
                                468   [ +  -  +  -  :        1285767 :         MemSet(typentry, 0, sizeof(TypeCacheEntry));
                                     +  -  +  -  +  
                                                 + ]
                                469                 :                : 
                                470                 :                :         /* These fields can never change, by definition */
                                471                 :          20409 :         typentry->type_id = type_id;
  843 akorotkov@postgresql      472                 :          20409 :         typentry->type_id_hash = get_hash_value(TypeCacheHash, &type_id);
                                473                 :                : 
                                474                 :                :         /* Keep this part in sync with the code below */
 8276 tgl@sss.pgh.pa.us         475                 :          20409 :         typentry->typlen = typtup->typlen;
                                476                 :          20409 :         typentry->typbyval = typtup->typbyval;
                                477                 :          20409 :         typentry->typalign = typtup->typalign;
 5492                           478                 :          20409 :         typentry->typstorage = typtup->typstorage;
 8276                           479                 :          20409 :         typentry->typtype = typtup->typtype;
                                480                 :          20409 :         typentry->typrelid = typtup->typrelid;
 2180                           481                 :          20409 :         typentry->typsubscript = typtup->typsubscript;
 3299                           482                 :          20409 :         typentry->typelem = typtup->typelem;
  606                           483                 :          20409 :         typentry->typarray = typtup->typarray;
 2906                           484                 :          20409 :         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                 :                :          */
   19 rhaas@postgresql.org      491                 :          20409 :         insert_rel_type_cache_if_needed(typentry);
 2458 tgl@sss.pgh.pa.us         492                 :          20408 :         typentry->flags |= TCFLAGS_HAVE_PG_TYPE_DATA;
                                493                 :                : 
                                494                 :                :         /* If it's a domain, immediately thread it into the domain cache list */
 4290                           495         [ +  + ]:          20408 :         if (typentry->typtype == TYPTYPE_DOMAIN)
                                496                 :                :         {
                                497                 :           1141 :             typentry->nextDomain = firstDomainTypeEntry;
                                498                 :           1141 :             firstDomainTypeEntry = typentry;
                                499                 :                :         }
                                500                 :                : 
 8276                           501                 :          20408 :         ReleaseSysCache(tp);
                                502                 :                :     }
 2458                           503         [ +  + ]:         482494 :     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                 :            397 :         tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
                                513         [ -  + ]:            397 :         if (!HeapTupleIsValid(tp))
 2458 tgl@sss.pgh.pa.us         514         [ #  # ]:UBC           0 :             ereport(ERROR,
                                515                 :                :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
                                516                 :                :                      errmsg("type with OID %u does not exist", type_id)));
 2458 tgl@sss.pgh.pa.us         517                 :CBC         397 :         typtup = (Form_pg_type) GETSTRUCT(tp);
                                518         [ -  + ]:            397 :         if (!typtup->typisdefined)
 2458 tgl@sss.pgh.pa.us         519         [ #  # ]:UBC           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                 :                :          */
 2458 tgl@sss.pgh.pa.us         528                 :CBC         397 :         typentry->typlen = typtup->typlen;
                                529                 :            397 :         typentry->typbyval = typtup->typbyval;
                                530                 :            397 :         typentry->typalign = typtup->typalign;
                                531                 :            397 :         typentry->typstorage = typtup->typstorage;
                                532                 :            397 :         typentry->typtype = typtup->typtype;
                                533                 :            397 :         typentry->typrelid = typtup->typrelid;
 2180                           534                 :            397 :         typentry->typsubscript = typtup->typsubscript;
 2458                           535                 :            397 :         typentry->typelem = typtup->typelem;
  606                           536                 :            397 :         typentry->typarray = typtup->typarray;
 2458                           537                 :            397 :         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                 :                :          */
   19 rhaas@postgresql.org      544                 :            397 :         insert_rel_type_cache_if_needed(typentry);
 2458 tgl@sss.pgh.pa.us         545                 :            397 :         typentry->flags |= TCFLAGS_HAVE_PG_TYPE_DATA;
                                546                 :                : 
                                547                 :            397 :         ReleaseSysCache(tp);
                                548                 :                :     }
                                549                 :                : 
                                550                 :                :     /*
                                551                 :                :      * Look up opclasses if we haven't already and any dependent info is
                                552                 :                :      * requested.
                                553                 :                :      */
 8276                           554         [ +  + ]:         502902 :     if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_LT_OPR | TYPECACHE_GT_OPR |
                                555                 :                :                   TYPECACHE_CMP_PROC |
                                556                 :                :                   TYPECACHE_EQ_OPR_FINFO | TYPECACHE_CMP_PROC_FINFO |
 7280                           557                 :         265152 :                   TYPECACHE_BTREE_OPFAMILY)) &&
 4383                           558         [ +  + ]:         265152 :         !(typentry->flags & TCFLAGS_CHECKED_BTREE_OPCLASS))
                                559                 :                :     {
                                560                 :                :         Oid         opclass;
                                561                 :                : 
 7280                           562                 :          16714 :         opclass = GetDefaultOpClass(type_id, BTREE_AM_OID);
                                563         [ +  + ]:          16714 :         if (OidIsValid(opclass))
                                564                 :                :         {
                                565                 :          16061 :             typentry->btree_opf = get_opclass_family(opclass);
                                566                 :          16061 :             typentry->btree_opintype = get_opclass_input_type(opclass);
                                567                 :                :         }
                                568                 :                :         else
                                569                 :                :         {
 4383                           570                 :            653 :             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                 :          16714 :         typentry->flags &= ~(TCFLAGS_CHECKED_EQ_OPR |
                                580                 :                :                              TCFLAGS_CHECKED_LT_OPR |
                                581                 :                :                              TCFLAGS_CHECKED_GT_OPR |
                                582                 :                :                              TCFLAGS_CHECKED_CMP_PROC);
                                583                 :          16714 :         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         [ +  + ]:         502902 :     if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
                                591         [ +  + ]:         233754 :         !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR) &&
                                592         [ +  + ]:          16264 :         typentry->btree_opf == InvalidOid)
                                593                 :            649 :         flags |= TYPECACHE_HASH_OPFAMILY;
                                594                 :                : 
 5873                           595         [ +  + ]:         502902 :     if ((flags & (TYPECACHE_HASH_PROC | TYPECACHE_HASH_PROC_FINFO |
                                596                 :                :                   TYPECACHE_HASH_EXTENDED_PROC |
                                597                 :                :                   TYPECACHE_HASH_EXTENDED_PROC_FINFO |
                                598                 :         131849 :                   TYPECACHE_HASH_OPFAMILY)) &&
 4383                           599         [ +  + ]:         131849 :         !(typentry->flags & TCFLAGS_CHECKED_HASH_OPCLASS))
                                600                 :                :     {
                                601                 :                :         Oid         opclass;
                                602                 :                : 
 5873                           603                 :          11277 :         opclass = GetDefaultOpClass(type_id, HASH_AM_OID);
                                604         [ +  + ]:          11277 :         if (OidIsValid(opclass))
                                605                 :                :         {
                                606                 :          11093 :             typentry->hash_opf = get_opclass_family(opclass);
                                607                 :          11093 :             typentry->hash_opintype = get_opclass_input_type(opclass);
                                608                 :                :         }
                                609                 :                :         else
                                610                 :                :         {
 4383                           611                 :            184 :             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                 :                :          */
 3326                           619                 :          11277 :         typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
                                620                 :                :                              TCFLAGS_CHECKED_HASH_EXTENDED_PROC);
 4383                           621                 :          11277 :         typentry->flags |= TCFLAGS_CHECKED_HASH_OPCLASS;
                                622                 :                :     }
                                623                 :                : 
                                624                 :                :     /*
                                625                 :                :      * Look for requested operators and functions, if we haven't already.
                                626                 :                :      */
 8504                           627         [ +  + ]:         502902 :     if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
 4383                           628         [ +  + ]:         233754 :         !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR))
                                629                 :                :     {
 5651 bruce@momjian.us          630                 :          16264 :         Oid         eq_opr = InvalidOid;
                                631                 :                : 
 7280 tgl@sss.pgh.pa.us         632         [ +  + ]:          16264 :         if (typentry->btree_opf != InvalidOid)
 5657                           633                 :          15615 :             eq_opr = get_opfamily_member(typentry->btree_opf,
                                634                 :                :                                          typentry->btree_opintype,
                                635                 :                :                                          typentry->btree_opintype,
                                636                 :                :                                          BTEqualStrategyNumber);
                                637         [ +  + ]:          16264 :         if (eq_opr == InvalidOid &&
 7280                           638         [ +  + ]:            649 :             typentry->hash_opf != InvalidOid)
 5657                           639                 :            508 :             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         [ +  + ]:          16264 :         if (eq_opr == ARRAY_EQ_OP &&
                                653         [ +  + ]:           2233 :             !array_element_has_equality(typentry))
                                654                 :            330 :             eq_opr = InvalidOid;
                                655         [ +  + ]:          15934 :         else if (eq_opr == RECORD_EQ_OP &&
                                656         [ +  + ]:            349 :                  !record_fields_have_equality(typentry))
                                657                 :            166 :             eq_opr = InvalidOid;
                                658                 :                : 
                                659                 :                :         /* Force update of eq_opr_finfo only if we're changing state */
 4383                           660         [ +  + ]:          16264 :         if (typentry->eq_opr != eq_opr)
                                661                 :          14619 :             typentry->eq_opr_finfo.fn_oid = InvalidOid;
                                662                 :                : 
 5657                           663                 :          16264 :         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                 :                :          */
 3326                           670                 :          16264 :         typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
                                671                 :                :                              TCFLAGS_CHECKED_HASH_EXTENDED_PROC);
 4383                           672                 :          16264 :         typentry->flags |= TCFLAGS_CHECKED_EQ_OPR;
                                673                 :                :     }
                                674         [ +  + ]:         502902 :     if ((flags & TYPECACHE_LT_OPR) &&
                                675         [ +  + ]:         197835 :         !(typentry->flags & TCFLAGS_CHECKED_LT_OPR))
                                676                 :                :     {
 5651 bruce@momjian.us          677                 :          12355 :         Oid         lt_opr = InvalidOid;
                                678                 :                : 
 7280 tgl@sss.pgh.pa.us         679         [ +  + ]:          12355 :         if (typentry->btree_opf != InvalidOid)
 5657                           680                 :          12074 :             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         [ +  + ]:          12355 :         if (lt_opr == ARRAY_LT_OP &&
                                690         [ +  + ]:           1542 :             !array_element_has_compare(typentry))
                                691                 :            385 :             lt_opr = InvalidOid;
                                692         [ +  + ]:          11970 :         else if (lt_opr == RECORD_LT_OP &&
                                693         [ +  + ]:            122 :                  !record_fields_have_compare(typentry))
                                694                 :              8 :             lt_opr = InvalidOid;
                                695                 :                : 
                                696                 :          12355 :         typentry->lt_opr = lt_opr;
 4383                           697                 :          12355 :         typentry->flags |= TCFLAGS_CHECKED_LT_OPR;
                                698                 :                :     }
                                699         [ +  + ]:         502902 :     if ((flags & TYPECACHE_GT_OPR) &&
                                700         [ +  + ]:         191028 :         !(typentry->flags & TCFLAGS_CHECKED_GT_OPR))
                                701                 :                :     {
 5651 bruce@momjian.us          702                 :          12207 :         Oid         gt_opr = InvalidOid;
                                703                 :                : 
 7280 tgl@sss.pgh.pa.us         704         [ +  + ]:          12207 :         if (typentry->btree_opf != InvalidOid)
 5657                           705                 :          11930 :             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         [ +  + ]:          12207 :         if (gt_opr == ARRAY_GT_OP &&
                                715         [ +  + ]:           1535 :             !array_element_has_compare(typentry))
                                716                 :            385 :             gt_opr = InvalidOid;
                                717         [ +  + ]:          11822 :         else if (gt_opr == RECORD_GT_OP &&
                                718         [ +  + ]:            114 :                  !record_fields_have_compare(typentry))
                                719                 :              8 :             gt_opr = InvalidOid;
                                720                 :                : 
                                721                 :          12207 :         typentry->gt_opr = gt_opr;
 4383                           722                 :          12207 :         typentry->flags |= TCFLAGS_CHECKED_GT_OPR;
                                723                 :                :     }
 8504                           724         [ +  + ]:         502902 :     if ((flags & (TYPECACHE_CMP_PROC | TYPECACHE_CMP_PROC_FINFO)) &&
 4383                           725         [ +  + ]:          29157 :         !(typentry->flags & TCFLAGS_CHECKED_CMP_PROC))
                                726                 :                :     {
 5651 bruce@momjian.us          727                 :           3277 :         Oid         cmp_proc = InvalidOid;
                                728                 :                : 
 7280 tgl@sss.pgh.pa.us         729         [ +  + ]:           3277 :         if (typentry->btree_opf != InvalidOid)
 5657                           730                 :           3123 :             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         [ +  + ]:           3277 :         if (cmp_proc == F_BTARRAYCMP &&
                                740         [ +  + ]:            579 :             !array_element_has_compare(typentry))
                                741                 :            154 :             cmp_proc = InvalidOid;
                                742         [ +  + ]:           3123 :         else if (cmp_proc == F_BTRECORDCMP &&
                                743         [ +  + ]:            229 :                  !record_fields_have_compare(typentry))
                                744                 :            154 :             cmp_proc = InvalidOid;
                                745                 :                : 
                                746                 :                :         /* Force update of cmp_proc_finfo only if we're changing state */
 4383                           747         [ +  + ]:           3277 :         if (typentry->cmp_proc != cmp_proc)
                                748                 :           2719 :             typentry->cmp_proc_finfo.fn_oid = InvalidOid;
                                749                 :                : 
 5657                           750                 :           3277 :         typentry->cmp_proc = cmp_proc;
 4383                           751                 :           3277 :         typentry->flags |= TCFLAGS_CHECKED_CMP_PROC;
                                752                 :                :     }
 5873                           753         [ +  + ]:         502902 :     if ((flags & (TYPECACHE_HASH_PROC | TYPECACHE_HASH_PROC_FINFO)) &&
 4383                           754         [ +  + ]:         131337 :         !(typentry->flags & TCFLAGS_CHECKED_HASH_PROC))
                                755                 :                :     {
 5651 bruce@momjian.us          756                 :          10877 :         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                 :                :          */
 5873 tgl@sss.pgh.pa.us         762         [ +  + ]:          10877 :         if (typentry->hash_opf != InvalidOid &&
                                763   [ +  +  +  - ]:          20831 :             (!OidIsValid(typentry->eq_opr) ||
                                764                 :          10074 :              typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
                                765                 :                :                                                      typentry->hash_opintype,
                                766                 :                :                                                      typentry->hash_opintype,
                                767                 :                :                                                      HTEqualStrategyNumber)))
 5657                           768                 :          10757 :             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         [ +  + ]:          10877 :         if (hash_proc == F_HASH_ARRAY &&
                                779         [ +  + ]:           1479 :             !array_element_has_hashing(typentry))
                                780                 :            205 :             hash_proc = InvalidOid;
 2200 peter@eisentraut.org      781         [ +  + ]:          10672 :         else if (hash_proc == F_HASH_RECORD &&
                                782         [ +  + ]:            347 :                  !record_fields_have_hashing(typentry))
                                783                 :            200 :             hash_proc = InvalidOid;
                                784         [ +  + ]:          10472 :         else if (hash_proc == F_HASH_RANGE &&
 2026 tgl@sss.pgh.pa.us         785         [ +  + ]:             69 :                  !range_element_has_hashing(typentry))
 3326                           786                 :              4 :             hash_proc = InvalidOid;
  173                           787         [ +  + ]:          10468 :         else if (hash_proc == F_HASH_MULTIRANGE &&
                                788         [ +  + ]:             16 :                  !multirange_element_has_hashing(typentry))
 2169 akorotkov@postgresql      789                 :              4 :             hash_proc = InvalidOid;
                                790                 :                : 
                                791                 :                :         /* Force update of hash_proc_finfo only if we're changing state */
 4383 tgl@sss.pgh.pa.us         792         [ +  + ]:          10877 :         if (typentry->hash_proc != hash_proc)
                                793                 :           9520 :             typentry->hash_proc_finfo.fn_oid = InvalidOid;
                                794                 :                : 
 5657                           795                 :          10877 :         typentry->hash_proc = hash_proc;
 4383                           796                 :          10877 :         typentry->flags |= TCFLAGS_CHECKED_HASH_PROC;
                                797                 :                :     }
 3376 rhaas@postgresql.org      798         [ +  + ]:         502902 :     if ((flags & (TYPECACHE_HASH_EXTENDED_PROC |
                                799                 :           7061 :                   TYPECACHE_HASH_EXTENDED_PROC_FINFO)) &&
                                800         [ +  + ]:           7061 :         !(typentry->flags & TCFLAGS_CHECKED_HASH_EXTENDED_PROC))
                                801                 :                :     {
                                802                 :           2844 :         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         [ +  + ]:           2844 :         if (typentry->hash_opf != InvalidOid &&
                                809   [ +  +  +  - ]:           5255 :             (!OidIsValid(typentry->eq_opr) ||
                                810                 :           2435 :              typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
                                811                 :                :                                                      typentry->hash_opintype,
                                812                 :                :                                                      typentry->hash_opintype,
                                813                 :                :                                                      HTEqualStrategyNumber)))
                                814                 :           2820 :             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         [ +  + ]:           2844 :         if (hash_extended_proc == F_HASH_ARRAY_EXTENDED &&
 3326 tgl@sss.pgh.pa.us         824         [ +  + ]:            314 :             !array_element_has_extended_hashing(typentry))
 3376 rhaas@postgresql.org      825                 :            154 :             hash_extended_proc = InvalidOid;
 2200 peter@eisentraut.org      826         [ +  + ]:           2690 :         else if (hash_extended_proc == F_HASH_RECORD_EXTENDED &&
 2026 tgl@sss.pgh.pa.us         827         [ +  + ]:            170 :                  !record_fields_have_extended_hashing(typentry))
 2200 peter@eisentraut.org      828                 :            158 :             hash_extended_proc = InvalidOid;
                                829         [ -  + ]:           2532 :         else if (hash_extended_proc == F_HASH_RANGE_EXTENDED &&
 2026 tgl@sss.pgh.pa.us         830         [ #  # ]:UBC           0 :                  !range_element_has_extended_hashing(typentry))
 3326                           831                 :              0 :             hash_extended_proc = InvalidOid;
  173 tgl@sss.pgh.pa.us         832         [ -  + ]:CBC        2532 :         else if (hash_extended_proc == F_HASH_MULTIRANGE_EXTENDED &&
  173 tgl@sss.pgh.pa.us         833         [ #  # ]:UBC           0 :                  !multirange_element_has_extended_hashing(typentry))
 2169 akorotkov@postgresql      834                 :              0 :             hash_extended_proc = InvalidOid;
                                835                 :                : 
                                836                 :                :         /* Force update of proc finfo only if we're changing state */
 3376 rhaas@postgresql.org      837         [ +  + ]:CBC        2844 :         if (typentry->hash_extended_proc != hash_extended_proc)
                                838                 :           2430 :             typentry->hash_extended_proc_finfo.fn_oid = InvalidOid;
                                839                 :                : 
                                840                 :           2844 :         typentry->hash_extended_proc = hash_extended_proc;
                                841                 :           2844 :         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                 :                :      */
 8504 tgl@sss.pgh.pa.us         856         [ +  + ]:         502902 :     if ((flags & TYPECACHE_EQ_OPR_FINFO) &&
                                857         [ +  + ]:           3732 :         typentry->eq_opr_finfo.fn_oid == InvalidOid &&
                                858         [ +  + ]:           1051 :         typentry->eq_opr != InvalidOid)
                                859                 :                :     {
                                860                 :                :         Oid         eq_opr_func;
                                861                 :                : 
                                862                 :           1047 :         eq_opr_func = get_opcode(typentry->eq_opr);
                                863         [ +  - ]:           1047 :         if (eq_opr_func != InvalidOid)
                                864                 :           1047 :             fmgr_info_cxt(eq_opr_func, &typentry->eq_opr_finfo,
                                865                 :                :                           CacheMemoryContext);
                                866                 :                :     }
                                867         [ +  + ]:         502902 :     if ((flags & TYPECACHE_CMP_PROC_FINFO) &&
                                868         [ +  + ]:          20498 :         typentry->cmp_proc_finfo.fn_oid == InvalidOid &&
                                869         [ +  + ]:           2711 :         typentry->cmp_proc != InvalidOid)
                                870                 :                :     {
                                871                 :           1044 :         fmgr_info_cxt(typentry->cmp_proc, &typentry->cmp_proc_finfo,
                                872                 :                :                       CacheMemoryContext);
                                873                 :                :     }
 5873                           874         [ +  + ]:         502902 :     if ((flags & TYPECACHE_HASH_PROC_FINFO) &&
                                875         [ +  + ]:           5528 :         typentry->hash_proc_finfo.fn_oid == InvalidOid &&
                                876         [ +  + ]:           1115 :         typentry->hash_proc != InvalidOid)
                                877                 :                :     {
                                878                 :            984 :         fmgr_info_cxt(typentry->hash_proc, &typentry->hash_proc_finfo,
                                879                 :                :                       CacheMemoryContext);
                                880                 :                :     }
 3376 rhaas@postgresql.org      881         [ +  + ]:         502902 :     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                 :                :      */
 8276 tgl@sss.pgh.pa.us         893         [ +  + ]:         502902 :     if ((flags & TYPECACHE_TUPDESC) &&
                                894         [ +  + ]:         116580 :         typentry->tupDesc == NULL &&
 7180                           895         [ +  + ]:           4167 :         typentry->typtype == TYPTYPE_COMPOSITE)
                                896                 :                :     {
 5657                           897                 :           4062 :         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                 :                :      */
 5492                           906         [ +  + ]:         502902 :     if ((flags & TYPECACHE_RANGE_INFO) &&
                                907         [ +  - ]:          22120 :         typentry->typtype == TYPTYPE_RANGE)
                                908                 :                :     {
 2458                           909         [ +  + ]:          22120 :         if (typentry->rngelemtype == NULL)
                                910                 :            517 :             load_rangetype_info(typentry);
                                911         [ +  + ]:          21603 :         else if (!(typentry->rngelemtype->flags & TCFLAGS_HAVE_PG_TYPE_DATA))
                                912                 :              3 :             (void) lookup_type_cache(typentry->rngelemtype->type_id, 0);
                                913                 :                :     }
                                914                 :                : 
                                915                 :                :     /*
                                916                 :                :      * If requested, get information about a multirange type
                                917                 :                :      */
 2169 akorotkov@postgresql      918         [ +  + ]:         502902 :     if ((flags & TYPECACHE_MULTIRANGE_INFO) &&
                                919         [ +  + ]:           9588 :         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                 :                :      */
 3320 tgl@sss.pgh.pa.us         928         [ +  + ]:         502902 :     if ((flags & TYPECACHE_DOMAIN_BASE_INFO) &&
                                929         [ +  + ]:           9964 :         typentry->domainBaseType == InvalidOid &&
                                930         [ +  + ]:           7893 :         typentry->typtype == TYPTYPE_DOMAIN)
                                931                 :                :     {
                                932                 :            370 :         typentry->domainBaseTypmod = -1;
                                933                 :            370 :         typentry->domainBaseType =
                                934                 :            370 :             getBaseTypeAndTypmod(type_id, &typentry->domainBaseTypmod);
                                935                 :                :     }
                                936         [ +  + ]:         502902 :     if ((flags & TYPECACHE_DOMAIN_CONSTR_INFO) &&
 4290                           937         [ +  + ]:          31069 :         (typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
                                938         [ +  + ]:           5716 :         typentry->typtype == TYPTYPE_DOMAIN)
                                939                 :                :     {
                                940                 :           1963 :         load_domaintype_info(typentry);
                                941                 :                :     }
                                942                 :                : 
 5657                           943                 :         502902 :     return typentry;
                                944                 :                : }
                                945                 :                : 
                                946                 :                : /*
                                947                 :                :  * load_typcache_tupdesc --- helper routine to set up composite type's tupDesc
                                948                 :                :  */
                                949                 :                : static void
                                950                 :           4258 : load_typcache_tupdesc(TypeCacheEntry *typentry)
                                951                 :                : {
                                952                 :                :     Relation    rel;
                                953                 :                : 
 3447                           954         [ -  + ]:           4258 :     if (!OidIsValid(typentry->typrelid)) /* should not happen */
 5657 tgl@sss.pgh.pa.us         955         [ #  # ]:UBC           0 :         elog(ERROR, "invalid typrelid for composite type %u",
                                956                 :                :              typentry->type_id);
 5657 tgl@sss.pgh.pa.us         957                 :CBC        4258 :     rel = relation_open(typentry->typrelid, AccessShareLock);
                                958         [ -  + ]:           4258 :     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                 :           4258 :     typentry->tupDesc = RelationGetDescr(rel);
                                967                 :                : 
                                968         [ -  + ]:           4258 :     Assert(typentry->tupDesc->tdrefcount > 0);
                                969                 :           4258 :     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                 :                :      */
 3210                           975                 :           4258 :     typentry->tupDesc_identifier = ++tupledesc_id_counter;
                                976                 :                : 
 5657                           977                 :           4258 :     relation_close(rel, AccessShareLock);
                                978                 :           4258 : }
                                979                 :                : 
                                980                 :                : /*
                                981                 :                :  * load_rangetype_info --- helper routine to set up range type information
                                982                 :                :  */
                                983                 :                : static void
 5492                           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))
 5492 tgl@sss.pgh.pa.us        1000         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for range type %u",
                               1001                 :                :              typentry->type_id);
 5492 tgl@sss.pgh.pa.us        1002                 :CBC         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);
 1043                          1015                 :            561 :     typentry->rng_opfamily = opfamilyOid;
                               1016                 :                : 
 5492                          1017                 :            561 :     cmpFnOid = get_opfamily_proc(opfamilyOid, opcintype, opcintype,
                               1018                 :                :                                  BTORDER_PROC);
                               1019         [ -  + ]:            561 :     if (!RegProcedureIsValid(cmpFnOid))
 5492 tgl@sss.pgh.pa.us        1020         [ #  # ]:UBC           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 */
 5492 tgl@sss.pgh.pa.us        1024                 :CBC         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
 2169 akorotkov@postgresql     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))
 2169 akorotkov@postgresql     1048         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for multirange type %u",
                               1049                 :                :              typentry->type_id);
                               1050                 :                : 
 2169 akorotkov@postgresql     1051                 :CBC         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
 4290 tgl@sss.pgh.pa.us        1064                 :           1963 : load_domaintype_info(TypeCacheEntry *typentry)
                               1065                 :                : {
                               1066                 :           1963 :     Oid         typeOid = typentry->type_id;
                               1067                 :                :     DomainConstraintCache *dcc;
                               1068                 :           1963 :     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         [ +  + ]:           1963 :     if (typentry->domainData)
                               1079                 :                :     {
                               1080                 :            491 :         dcc = typentry->domainData;
                               1081                 :            491 :         typentry->domainData = NULL;
                               1082                 :            491 :         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                 :           1963 :     dcc = NULL;
 4268                          1091                 :           1963 :     ccons = NULL;
                               1092                 :           1963 :     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                 :                :      */
 2868 andres@anarazel.de       1099                 :           1963 :     conRel = table_open(ConstraintRelationId, AccessShareLock);
                               1100                 :                : 
                               1101                 :                :     for (;;)
 4290 tgl@sss.pgh.pa.us        1102                 :           1985 :     {
                               1103                 :                :         HeapTuple   tup;
                               1104                 :                :         HeapTuple   conTup;
                               1105                 :                :         Form_pg_type typTup;
 4268                          1106                 :           3948 :         int         nccons = 0;
                               1107                 :                :         ScanKeyData key[1];
                               1108                 :                :         SysScanDesc scan;
                               1109                 :                : 
 4290                          1110                 :           3948 :         tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeOid));
                               1111         [ -  + ]:           3948 :         if (!HeapTupleIsValid(tup))
 4290 tgl@sss.pgh.pa.us        1112         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for type %u", typeOid);
 4290 tgl@sss.pgh.pa.us        1113                 :CBC        3948 :         typTup = (Form_pg_type) GETSTRUCT(tup);
                               1114                 :                : 
                               1115         [ +  + ]:           3948 :         if (typTup->typtype != TYPTYPE_DOMAIN)
                               1116                 :                :         {
                               1117                 :                :             /* Not a domain, so done */
                               1118                 :           1963 :             ReleaseSysCache(tup);
                               1119                 :           1963 :             break;
                               1120                 :                :         }
                               1121                 :                : 
                               1122                 :                :         /* Test for NOT NULL Constraint */
                               1123         [ +  + ]:           1985 :         if (typTup->typnotnull)
                               1124                 :            101 :             notNull = true;
                               1125                 :                : 
                               1126                 :                :         /* Look for CHECK Constraints on this domain */
                               1127                 :           1985 :         ScanKeyInit(&key[0],
                               1128                 :                :                     Anum_pg_constraint_contypid,
                               1129                 :                :                     BTEqualStrategyNumber, F_OIDEQ,
                               1130                 :                :                     ObjectIdGetDatum(typeOid));
                               1131                 :                : 
                               1132                 :           1985 :         scan = systable_beginscan(conRel, ConstraintTypidIndexId, true,
                               1133                 :                :                                   NULL, 1, key);
                               1134                 :                : 
                               1135         [ +  + ]:           2997 :         while (HeapTupleIsValid(conTup = systable_getnext(scan)))
                               1136                 :                :         {
                               1137                 :           1012 :             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         [ +  + ]:           1012 :             if (c->contype != CONSTRAINT_CHECK)
                               1146                 :            101 :                 continue;
                               1147                 :                : 
                               1148                 :                :             /* Not expecting conbin to be NULL, but we'll test for it anyway */
                               1149                 :            911 :             val = fastgetattr(conTup, Anum_pg_constraint_conbin,
                               1150                 :                :                               conRel->rd_att, &isNull);
                               1151         [ -  + ]:            911 :             if (isNull)
 4290 tgl@sss.pgh.pa.us        1152         [ #  # ]:UBC           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 */
 4290 tgl@sss.pgh.pa.us        1156         [ +  + ]:CBC         911 :             if (dcc == NULL)
                               1157                 :                :             {
                               1158                 :                :                 MemoryContext cxt;
                               1159                 :                : 
                               1160                 :            894 :                 cxt = AllocSetContextCreate(CurrentMemoryContext,
                               1161                 :                :                                             "Domain constraints",
                               1162                 :                :                                             ALLOCSET_SMALL_SIZES);
                               1163                 :                :                 dcc = (DomainConstraintCache *)
                               1164                 :            894 :                     MemoryContextAlloc(cxt, sizeof(DomainConstraintCache));
                               1165                 :            894 :                 dcc->constraints = NIL;
                               1166                 :            894 :                 dcc->dccContext = cxt;
                               1167                 :            894 :                 dcc->dccRefCount = 0;
                               1168                 :                :             }
                               1169                 :                : 
                               1170                 :                :             /* Convert conbin to a node tree, still in caller's context */
  483                          1171                 :            911 :             constring = TextDatumGetCString(val);
 4290                          1172                 :            911 :             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                 :            911 :             check_expr = expression_planner(check_expr);
                               1185                 :                : 
                               1186                 :                :             /* Create only the minimally needed stuff in dccContext */
  483                          1187                 :            911 :             oldcxt = MemoryContextSwitchTo(dcc->dccContext);
                               1188                 :                : 
 4290                          1189                 :            911 :             r = makeNode(DomainConstraintState);
                               1190                 :            911 :             r->constrainttype = DOM_CONSTRAINT_CHECK;
                               1191                 :            911 :             r->name = pstrdup(NameStr(c->conname));
  483                          1192                 :            911 :             r->check_expr = copyObject(check_expr);
 3546 andres@anarazel.de       1193                 :            911 :             r->check_exprstate = NULL;
                               1194                 :                : 
 4268 tgl@sss.pgh.pa.us        1195                 :            911 :             MemoryContextSwitchTo(oldcxt);
                               1196                 :                : 
                               1197                 :                :             /* Accumulate constraints in an array, for sorting below */
                               1198         [ +  + ]:            911 :             if (ccons == NULL)
                               1199                 :                :             {
                               1200                 :            894 :                 cconslen = 8;
  103 michael@paquier.xyz      1201                 :GNC         894 :                 ccons = palloc_array(DomainConstraintState *, cconslen);
                               1202                 :                :             }
 4268 tgl@sss.pgh.pa.us        1203         [ -  + ]:CBC          17 :             else if (nccons >= cconslen)
                               1204                 :                :             {
 4268 tgl@sss.pgh.pa.us        1205                 :UBC           0 :                 cconslen *= 2;
  103 michael@paquier.xyz      1206                 :UNC           0 :                 ccons = repalloc_array(ccons, DomainConstraintState *, cconslen);
                               1207                 :                :             }
 4268 tgl@sss.pgh.pa.us        1208                 :CBC         911 :             ccons[nccons++] = r;
                               1209                 :                :         }
                               1210                 :                : 
                               1211                 :           1985 :         systable_endscan(scan);
                               1212                 :                : 
                               1213         [ +  + ]:           1985 :         if (nccons > 0)
                               1214                 :                :         {
                               1215                 :                :             /*
                               1216                 :                :              * Sort the items for this domain, so that CHECKs are applied in a
                               1217                 :                :              * deterministic order.
                               1218                 :                :              */
                               1219         [ +  + ]:            905 :             if (nccons > 1)
                               1220                 :              5 :                 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                 :            905 :             oldcxt = MemoryContextSwitchTo(dcc->dccContext);
                               1227         [ +  + ]:           1816 :             while (nccons > 0)
                               1228                 :            911 :                 dcc->constraints = lcons(ccons[--nccons], dcc->constraints);
 4290                          1229                 :            905 :             MemoryContextSwitchTo(oldcxt);
                               1230                 :                :         }
                               1231                 :                : 
                               1232                 :                :         /* loop to next domain in stack */
                               1233                 :           1985 :         typeOid = typTup->typbasetype;
                               1234                 :           1985 :         ReleaseSysCache(tup);
                               1235                 :                :     }
                               1236                 :                : 
 2868 andres@anarazel.de       1237                 :           1963 :     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                 :                :      */
 4290 tgl@sss.pgh.pa.us        1243         [ +  + ]:           1963 :     if (notNull)
                               1244                 :                :     {
                               1245                 :                :         DomainConstraintState *r;
                               1246                 :                : 
                               1247                 :                :         /* Create the DomainConstraintCache object and context if needed */
                               1248         [ +  + ]:            101 :         if (dcc == NULL)
                               1249                 :                :         {
                               1250                 :                :             MemoryContext cxt;
                               1251                 :                : 
                               1252                 :             80 :             cxt = AllocSetContextCreate(CurrentMemoryContext,
                               1253                 :                :                                         "Domain constraints",
                               1254                 :                :                                         ALLOCSET_SMALL_SIZES);
                               1255                 :                :             dcc = (DomainConstraintCache *)
                               1256                 :             80 :                 MemoryContextAlloc(cxt, sizeof(DomainConstraintCache));
                               1257                 :             80 :             dcc->constraints = NIL;
                               1258                 :             80 :             dcc->dccContext = cxt;
                               1259                 :             80 :             dcc->dccRefCount = 0;
                               1260                 :                :         }
                               1261                 :                : 
                               1262                 :                :         /* Create node trees in DomainConstraintCache's context */
                               1263                 :            101 :         oldcxt = MemoryContextSwitchTo(dcc->dccContext);
                               1264                 :                : 
                               1265                 :            101 :         r = makeNode(DomainConstraintState);
                               1266                 :                : 
                               1267                 :            101 :         r->constrainttype = DOM_CONSTRAINT_NOTNULL;
                               1268                 :            101 :         r->name = pstrdup("NOT NULL");
                               1269                 :            101 :         r->check_expr = NULL;
 3546 andres@anarazel.de       1270                 :            101 :         r->check_exprstate = NULL;
                               1271                 :                : 
                               1272                 :                :         /* lcons to apply the nullness check FIRST */
 4290 tgl@sss.pgh.pa.us        1273                 :            101 :         dcc->constraints = lcons(r, dcc->constraints);
                               1274                 :                : 
                               1275                 :            101 :         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         [ +  + ]:           1963 :     if (dcc)
                               1283                 :                :     {
                               1284                 :            974 :         MemoryContextSetParent(dcc->dccContext, CacheMemoryContext);
                               1285                 :            974 :         typentry->domainData = dcc;
                               1286                 :            974 :         dcc->dccRefCount++;      /* count the typcache's reference */
                               1287                 :                :     }
                               1288                 :                : 
                               1289                 :                :     /* Either way, the typcache entry's domain data is now valid. */
                               1290                 :           1963 :     typentry->flags |= TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS;
                               1291                 :           1963 : }
                               1292                 :                : 
                               1293                 :                : /*
                               1294                 :                :  * qsort comparator to sort DomainConstraintState pointers by name
                               1295                 :                :  */
                               1296                 :                : static int
 4268                          1297                 :              6 : dcs_cmp(const void *a, const void *b)
                               1298                 :                : {
 3447                          1299                 :              6 :     const DomainConstraintState *const *ca = (const DomainConstraintState *const *) a;
                               1300                 :              6 :     const DomainConstraintState *const *cb = (const DomainConstraintState *const *) b;
                               1301                 :                : 
 4268                          1302                 :              6 :     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
 4290                          1310                 :           8347 : decr_dcc_refcount(DomainConstraintCache *dcc)
                               1311                 :                : {
                               1312         [ -  + ]:           8347 :     Assert(dcc->dccRefCount > 0);
                               1313         [ +  + ]:           8347 :     if (--(dcc->dccRefCount) <= 0)
                               1314                 :            489 :         MemoryContextDelete(dcc->dccContext);
                               1315                 :           8347 : }
                               1316                 :                : 
                               1317                 :                : /*
                               1318                 :                :  * Context reset/delete callback for a DomainConstraintRef
                               1319                 :                :  */
                               1320                 :                : static void
                               1321                 :           8320 : dccref_deletion_callback(void *arg)
                               1322                 :                : {
                               1323                 :           8320 :     DomainConstraintRef *ref = (DomainConstraintRef *) arg;
                               1324                 :           8320 :     DomainConstraintCache *dcc = ref->dcc;
                               1325                 :                : 
                               1326                 :                :     /* Paranoia --- be sure link is nulled before trying to release */
                               1327         [ +  + ]:           8320 :     if (dcc)
                               1328                 :                :     {
                               1329                 :           7856 :         ref->constraints = NIL;
                               1330                 :           7856 :         ref->dcc = NULL;
                               1331                 :           7856 :         decr_dcc_refcount(dcc);
                               1332                 :                :     }
                               1333                 :           8320 : }
                               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 *
 4017                          1342                 :           1767 : prep_domain_constraints(List *constraints, MemoryContext execctx)
                               1343                 :                : {
                               1344                 :           1767 :     List       *result = NIL;
                               1345                 :                :     MemoryContext oldcxt;
                               1346                 :                :     ListCell   *lc;
                               1347                 :                : 
                               1348                 :           1767 :     oldcxt = MemoryContextSwitchTo(execctx);
                               1349                 :                : 
                               1350   [ +  -  +  +  :           3574 :     foreach(lc, constraints)
                                              +  + ]
                               1351                 :                :     {
                               1352                 :           1807 :         DomainConstraintState *r = (DomainConstraintState *) lfirst(lc);
                               1353                 :                :         DomainConstraintState *newr;
                               1354                 :                : 
                               1355                 :           1807 :         newr = makeNode(DomainConstraintState);
                               1356                 :           1807 :         newr->constrainttype = r->constrainttype;
                               1357                 :           1807 :         newr->name = r->name;
 3546 andres@anarazel.de       1358                 :           1807 :         newr->check_expr = r->check_expr;
                               1359                 :           1807 :         newr->check_exprstate = ExecInitExpr(r->check_expr, NULL);
                               1360                 :                : 
 4017 tgl@sss.pgh.pa.us        1361                 :           1807 :         result = lappend(result, newr);
                               1362                 :                :     }
                               1363                 :                : 
                               1364                 :           1767 :     MemoryContextSwitchTo(oldcxt);
                               1365                 :                : 
                               1366                 :           1767 :     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
 4290                          1380                 :           8334 : InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref,
                               1381                 :                :                         MemoryContext refctx, bool need_exprstate)
                               1382                 :                : {
                               1383                 :                :     /* Look up the typcache entry --- we assume it survives indefinitely */
 3320                          1384                 :           8334 :     ref->tcache = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO);
 3546 andres@anarazel.de       1385                 :           8334 :     ref->need_exprstate = need_exprstate;
                               1386                 :                :     /* For safety, establish the callback before acquiring a refcount */
 4017 tgl@sss.pgh.pa.us        1387                 :           8334 :     ref->refctx = refctx;
 4290                          1388                 :           8334 :     ref->dcc = NULL;
                               1389                 :           8334 :     ref->callback.func = dccref_deletion_callback;
  730 peter@eisentraut.org     1390                 :           8334 :     ref->callback.arg = ref;
 4290 tgl@sss.pgh.pa.us        1391                 :           8334 :     MemoryContextRegisterResetCallback(refctx, &ref->callback);
                               1392                 :                :     /* Acquire refcount if there are constraints, and set up exported list */
                               1393         [ +  + ]:           8334 :     if (ref->tcache->domainData)
                               1394                 :                :     {
                               1395                 :           7870 :         ref->dcc = ref->tcache->domainData;
                               1396                 :           7870 :         ref->dcc->dccRefCount++;
 3546 andres@anarazel.de       1397         [ +  + ]:           7870 :         if (ref->need_exprstate)
                               1398                 :           1767 :             ref->constraints = prep_domain_constraints(ref->dcc->constraints,
                               1399                 :                :                                                        ref->refctx);
                               1400                 :                :         else
                               1401                 :           6103 :             ref->constraints = ref->dcc->constraints;
                               1402                 :                :     }
                               1403                 :                :     else
 4290 tgl@sss.pgh.pa.us        1404                 :            464 :         ref->constraints = NIL;
                               1405                 :           8334 : }
                               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                 :         265355 : UpdateDomainConstraintRef(DomainConstraintRef *ref)
                               1419                 :                : {
                               1420                 :         265355 :     TypeCacheEntry *typentry = ref->tcache;
                               1421                 :                : 
                               1422                 :                :     /* Make sure typcache entry's data is up to date */
                               1423         [ -  + ]:         265355 :     if ((typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
 4290 tgl@sss.pgh.pa.us        1424         [ #  # ]:UBC           0 :         typentry->typtype == TYPTYPE_DOMAIN)
                               1425                 :              0 :         load_domaintype_info(typentry);
                               1426                 :                : 
                               1427                 :                :     /* Transfer to ref object if there's new info, adjusting refcounts */
 4290 tgl@sss.pgh.pa.us        1428         [ -  + ]:CBC      265355 :     if (ref->dcc != typentry->domainData)
                               1429                 :                :     {
                               1430                 :                :         /* Paranoia --- be sure link is nulled before trying to release */
 4290 tgl@sss.pgh.pa.us        1431                 :UBC           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++;
 3546 andres@anarazel.de       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                 :                :     }
 4290 tgl@sss.pgh.pa.us        1459                 :CBC      265355 : }
                               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
  261 andrew@dunslane.net      1471                 :          22735 : 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                 :                :      */
 3320 tgl@sss.pgh.pa.us        1479                 :          22735 :     typentry = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO);
                               1480                 :                : 
  261 andrew@dunslane.net      1481         [ +  + ]:          22735 :     if (typentry->domainData == NULL)
                               1482                 :          16231 :         return false;
                               1483                 :                : 
                               1484         [ -  + ]:           6504 :     if (has_volatile)
                               1485                 :                :     {
  261 andrew@dunslane.net      1486                 :UBC           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                 :                : 
  261 andrew@dunslane.net      1500                 :CBC        6504 :     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
 5657 tgl@sss.pgh.pa.us        1517                 :           2233 : array_element_has_equality(TypeCacheEntry *typentry)
                               1518                 :                : {
                               1519         [ +  + ]:           2233 :     if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
                               1520                 :           2218 :         cache_array_element_properties(typentry);
                               1521                 :           2233 :     return (typentry->flags & TCFLAGS_HAVE_ELEM_EQUALITY) != 0;
                               1522                 :                : }
                               1523                 :                : 
                               1524                 :                : static bool
                               1525                 :           3656 : array_element_has_compare(TypeCacheEntry *typentry)
                               1526                 :                : {
                               1527         [ +  + ]:           3656 :     if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
                               1528                 :            264 :         cache_array_element_properties(typentry);
                               1529                 :           3656 :     return (typentry->flags & TCFLAGS_HAVE_ELEM_COMPARE) != 0;
                               1530                 :                : }
                               1531                 :                : 
                               1532                 :                : static bool
                               1533                 :           1479 : array_element_has_hashing(TypeCacheEntry *typentry)
                               1534                 :                : {
                               1535         [ -  + ]:           1479 :     if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
 5657 tgl@sss.pgh.pa.us        1536                 :UBC           0 :         cache_array_element_properties(typentry);
 5657 tgl@sss.pgh.pa.us        1537                 :CBC        1479 :     return (typentry->flags & TCFLAGS_HAVE_ELEM_HASHING) != 0;
                               1538                 :                : }
                               1539                 :                : 
                               1540                 :                : static bool
 3326                          1541                 :            314 : array_element_has_extended_hashing(TypeCacheEntry *typentry)
                               1542                 :                : {
                               1543         [ -  + ]:            314 :     if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
 3326 tgl@sss.pgh.pa.us        1544                 :UBC           0 :         cache_array_element_properties(typentry);
 3326 tgl@sss.pgh.pa.us        1545                 :CBC         314 :     return (typentry->flags & TCFLAGS_HAVE_ELEM_EXTENDED_HASHING) != 0;
                               1546                 :                : }
                               1547                 :                : 
                               1548                 :                : static void
 5657                          1549                 :           2482 : cache_array_element_properties(TypeCacheEntry *typentry)
                               1550                 :                : {
 5651 bruce@momjian.us         1551                 :           2482 :     Oid         elem_type = get_base_element_type(typentry->type_id);
                               1552                 :                : 
 5657 tgl@sss.pgh.pa.us        1553         [ +  + ]:           2482 :     if (OidIsValid(elem_type))
                               1554                 :                :     {
                               1555                 :                :         TypeCacheEntry *elementry;
                               1556                 :                : 
                               1557                 :           2306 :         elementry = lookup_type_cache(elem_type,
                               1558                 :                :                                       TYPECACHE_EQ_OPR |
                               1559                 :                :                                       TYPECACHE_CMP_PROC |
                               1560                 :                :                                       TYPECACHE_HASH_PROC |
                               1561                 :                :                                       TYPECACHE_HASH_EXTENDED_PROC);
                               1562         [ +  + ]:           2306 :         if (OidIsValid(elementry->eq_opr))
                               1563                 :           2152 :             typentry->flags |= TCFLAGS_HAVE_ELEM_EQUALITY;
                               1564         [ +  + ]:           2306 :         if (OidIsValid(elementry->cmp_proc))
                               1565                 :           2010 :             typentry->flags |= TCFLAGS_HAVE_ELEM_COMPARE;
                               1566         [ +  + ]:           2306 :         if (OidIsValid(elementry->hash_proc))
                               1567                 :           2144 :             typentry->flags |= TCFLAGS_HAVE_ELEM_HASHING;
 3326                          1568         [ +  + ]:           2306 :         if (OidIsValid(elementry->hash_extended_proc))
                               1569                 :           2144 :             typentry->flags |= TCFLAGS_HAVE_ELEM_EXTENDED_HASHING;
                               1570                 :                :     }
 5657                          1571                 :           2482 :     typentry->flags |= TCFLAGS_CHECKED_ELEM_PROPERTIES;
                               1572                 :           2482 : }
                               1573                 :                : 
                               1574                 :                : /*
                               1575                 :                :  * Likewise, some helper functions for composite types.
                               1576                 :                :  */
                               1577                 :                : 
                               1578                 :                : static bool
                               1579                 :            349 : record_fields_have_equality(TypeCacheEntry *typentry)
                               1580                 :                : {
                               1581         [ +  + ]:            349 :     if (!(typentry->flags & TCFLAGS_CHECKED_FIELD_PROPERTIES))
                               1582                 :            319 :         cache_record_field_properties(typentry);
                               1583                 :            349 :     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
 2200 peter@eisentraut.org     1595                 :            347 : record_fields_have_hashing(TypeCacheEntry *typentry)
                               1596                 :                : {
                               1597         [ +  + ]:            347 :     if (!(typentry->flags & TCFLAGS_CHECKED_FIELD_PROPERTIES))
                               1598                 :              8 :         cache_record_field_properties(typentry);
                               1599                 :            347 :     return (typentry->flags & TCFLAGS_HAVE_FIELD_HASHING) != 0;
                               1600                 :                : }
                               1601                 :                : 
                               1602                 :                : static bool
                               1603                 :            170 : record_fields_have_extended_hashing(TypeCacheEntry *typentry)
                               1604                 :                : {
                               1605         [ -  + ]:            170 :     if (!(typentry->flags & TCFLAGS_CHECKED_FIELD_PROPERTIES))
 2200 peter@eisentraut.org     1606                 :UBC           0 :         cache_record_field_properties(typentry);
 2200 peter@eisentraut.org     1607                 :CBC         170 :     return (typentry->flags & TCFLAGS_HAVE_FIELD_EXTENDED_HASHING) != 0;
                               1608                 :                : }
                               1609                 :                : 
                               1610                 :                : static void
 5657 tgl@sss.pgh.pa.us        1611                 :            394 : 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         [ +  + ]:            394 :     if (typentry->type_id == RECORDOID)
                               1624                 :                :     {
                               1625                 :             42 :         typentry->flags |= (TCFLAGS_HAVE_FIELD_EQUALITY |
                               1626                 :                :                             TCFLAGS_HAVE_FIELD_COMPARE);
                               1627                 :                :     }
                               1628         [ +  - ]:            352 :     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         [ +  + ]:            352 :         if (typentry->tupDesc == NULL)
                               1636                 :            196 :             load_typcache_tupdesc(typentry);
                               1637                 :            352 :         tupdesc = typentry->tupDesc;
                               1638                 :                : 
                               1639                 :                :         /* Must bump the refcount while we do additional catalog lookups */
 4918                          1640                 :            352 :         IncrTupleDescRefCount(tupdesc);
                               1641                 :                : 
                               1642                 :                :         /* Have each property if all non-dropped fields have the property */
 5657                          1643                 :            352 :         newflags = (TCFLAGS_HAVE_FIELD_EQUALITY |
                               1644                 :                :                     TCFLAGS_HAVE_FIELD_COMPARE |
                               1645                 :                :                     TCFLAGS_HAVE_FIELD_HASHING |
                               1646                 :                :                     TCFLAGS_HAVE_FIELD_EXTENDED_HASHING);
                               1647         [ +  + ]:           4768 :         for (i = 0; i < tupdesc->natts; i++)
                               1648                 :                :         {
                               1649                 :                :             TypeCacheEntry *fieldentry;
 3387 andres@anarazel.de       1650                 :           4582 :             Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
                               1651                 :                : 
                               1652         [ -  + ]:           4582 :             if (attr->attisdropped)
 5657 tgl@sss.pgh.pa.us        1653                 :UBC           0 :                 continue;
                               1654                 :                : 
 3387 andres@anarazel.de       1655                 :CBC        4582 :             fieldentry = lookup_type_cache(attr->atttypid,
                               1656                 :                :                                            TYPECACHE_EQ_OPR |
                               1657                 :                :                                            TYPECACHE_CMP_PROC |
                               1658                 :                :                                            TYPECACHE_HASH_PROC |
                               1659                 :                :                                            TYPECACHE_HASH_EXTENDED_PROC);
 5657 tgl@sss.pgh.pa.us        1660         [ +  + ]:           4582 :             if (!OidIsValid(fieldentry->eq_opr))
                               1661                 :            166 :                 newflags &= ~TCFLAGS_HAVE_FIELD_EQUALITY;
                               1662         [ +  + ]:           4582 :             if (!OidIsValid(fieldentry->cmp_proc))
                               1663                 :            166 :                 newflags &= ~TCFLAGS_HAVE_FIELD_COMPARE;
 2200 peter@eisentraut.org     1664         [ +  + ]:           4582 :             if (!OidIsValid(fieldentry->hash_proc))
                               1665                 :            170 :                 newflags &= ~TCFLAGS_HAVE_FIELD_HASHING;
                               1666         [ +  + ]:           4582 :             if (!OidIsValid(fieldentry->hash_extended_proc))
                               1667                 :            170 :                 newflags &= ~TCFLAGS_HAVE_FIELD_EXTENDED_HASHING;
                               1668                 :                : 
                               1669                 :                :             /* We can drop out of the loop once we disprove all bits */
 5657 tgl@sss.pgh.pa.us        1670         [ +  + ]:           4582 :             if (newflags == 0)
                               1671                 :            166 :                 break;
                               1672                 :                :         }
                               1673                 :            352 :         typentry->flags |= newflags;
                               1674                 :                : 
 4918                          1675                 :            352 :         DecrTupleDescRefCount(tupdesc);
                               1676                 :                :     }
 3320 tgl@sss.pgh.pa.us        1677         [ #  # ]:UBC           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                 :                :     }
 5657 tgl@sss.pgh.pa.us        1704                 :CBC         394 :     typentry->flags |= TCFLAGS_CHECKED_FIELD_PROPERTIES;
 8504                          1705                 :            394 : }
                               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
 3326                          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
 3326 tgl@sss.pgh.pa.us        1724                 :UBC           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
 3326 tgl@sss.pgh.pa.us        1732                 :CBC          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
 2169 akorotkov@postgresql     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
 2169 akorotkov@postgresql     1764                 :UBC           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
 2169 akorotkov@postgresql     1772                 :CBC          16 : cache_multirange_element_properties(TypeCacheEntry *typentry)
                               1773                 :                : {
                               1774                 :                :     /* load up range link if we didn't already */
                               1775         [ -  + ]:             16 :     if (typentry->rngtype == NULL &&
 2169 akorotkov@postgresql     1776         [ #  # ]:UBC           0 :         typentry->typtype == TYPTYPE_MULTIRANGE)
                               1777                 :              0 :         load_multirangetype_info(typentry);
                               1778                 :                : 
 2169 akorotkov@postgresql     1779   [ +  -  +  - ]:CBC          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
 3362 andres@anarazel.de       1800                 :          10982 : ensure_record_cache_typmod_slot_exists(int32 typmod)
                               1801                 :                : {
                               1802         [ +  + ]:          10982 :     if (RecordCacheArray == NULL)
                               1803                 :                :     {
 1172 tmunro@postgresql.or     1804                 :           4281 :         RecordCacheArray = (RecordCacheArrayEntry *)
                               1805                 :           4281 :             MemoryContextAllocZero(CacheMemoryContext,
                               1806                 :                :                                    64 * sizeof(RecordCacheArrayEntry));
 3362 andres@anarazel.de       1807                 :           4281 :         RecordCacheArrayLen = 64;
                               1808                 :                :     }
                               1809                 :                : 
                               1810         [ +  + ]:          10982 :     if (typmod >= RecordCacheArrayLen)
                               1811                 :                :     {
 1976 drowley@postgresql.o     1812                 :GBC           1 :         int32       newlen = pg_nextpower2_32(typmod + 1);
                               1813                 :                : 
 1172 tmunro@postgresql.or     1814                 :              1 :         RecordCacheArray = repalloc0_array(RecordCacheArray,
                               1815                 :                :                                            RecordCacheArrayEntry,
                               1816                 :                :                                            RecordCacheArrayLen,
                               1817                 :                :                                            newlen);
 3362 andres@anarazel.de       1818                 :              1 :         RecordCacheArrayLen = newlen;
                               1819                 :                :     }
 3362 andres@anarazel.de       1820                 :CBC       10982 : }
                               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
 7470 tgl@sss.pgh.pa.us        1829                 :         159412 : lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
                               1830                 :                : {
 8276                          1831         [ +  + ]:         159412 :     if (type_id != RECORDOID)
                               1832                 :                :     {
                               1833                 :                :         /*
                               1834                 :                :          * It's a named composite type, so use the regular typcache.
                               1835                 :                :          */
                               1836                 :                :         TypeCacheEntry *typentry;
                               1837                 :                : 
                               1838                 :          93621 :         typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
 8211                          1839   [ -  +  -  - ]:          93620 :         if (typentry->tupDesc == NULL && !noError)
 8276 tgl@sss.pgh.pa.us        1840         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1841                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               1842                 :                :                      errmsg("type %s is not composite",
                               1843                 :                :                             format_type_be(type_id))));
 8276 tgl@sss.pgh.pa.us        1844                 :CBC       93620 :         return typentry->tupDesc;
                               1845                 :                :     }
                               1846                 :                :     else
                               1847                 :                :     {
                               1848                 :                :         /*
                               1849                 :                :          * It's a transient record type, so look in our record-type table.
                               1850                 :                :          */
 3362 andres@anarazel.de       1851         [ +  + ]:          65791 :         if (typmod >= 0)
                               1852                 :                :         {
                               1853                 :                :             /* It is already in our local cache? */
                               1854         [ +  + ]:          65782 :             if (typmod < RecordCacheArrayLen &&
 1172 tmunro@postgresql.or     1855         [ +  + ]:          65778 :                 RecordCacheArray[typmod].tupdesc != NULL)
                               1856                 :          65762 :                 return RecordCacheArray[typmod].tupdesc;
                               1857                 :                : 
                               1858                 :                :             /* Are we attached to a shared record typmod registry? */
 3362 andres@anarazel.de       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         [ -  + ]:             20 :                     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                 :                :                      */
 1172 tmunro@postgresql.or     1882                 :             20 :                     RecordCacheArray[typmod].tupdesc = tupdesc;
 3362 andres@anarazel.de       1883         [ -  + ]:             20 :                     Assert(tupdesc->tdrefcount == -1);
                               1884                 :                : 
                               1885                 :                :                     /*
                               1886                 :                :                      * We don't share tupdesc identifiers across processes, so
                               1887                 :                :                      * assign one locally.
                               1888                 :                :                      */
 1172 tmunro@postgresql.or     1889                 :             20 :                     RecordCacheArray[typmod].id = ++tupledesc_id_counter;
                               1890                 :                : 
 3362 andres@anarazel.de       1891                 :             20 :                     dshash_release_lock(CurrentSession->shared_typmod_table,
                               1892                 :                :                                         entry);
                               1893                 :                : 
 1172 tmunro@postgresql.or     1894                 :             20 :                     return RecordCacheArray[typmod].tupdesc;
                               1895                 :                :                 }
                               1896                 :                :             }
                               1897                 :                :         }
                               1898                 :                : 
 3362 andres@anarazel.de       1899         [ +  + ]:              9 :         if (!noError)
 3362 andres@anarazel.de       1900         [ +  - ]:GBC           1 :             ereport(ERROR,
                               1901                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               1902                 :                :                      errmsg("record type has not been registered")));
 3362 andres@anarazel.de       1903                 :CBC           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
 7470 tgl@sss.pgh.pa.us        1923                 :         106532 : lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
                               1924                 :                : {
                               1925                 :                :     TupleDesc   tupDesc;
                               1926                 :                : 
                               1927                 :         106532 :     tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, false);
 3362 andres@anarazel.de       1928         [ +  + ]:         106530 :     PinTupleDesc(tupDesc);
 7470 tgl@sss.pgh.pa.us        1929                 :         106530 :     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)
 3362 andres@anarazel.de       1946         [ +  - ]:             15 :         PinTupleDesc(tupDesc);
 7470 tgl@sss.pgh.pa.us        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                 :          52856 : lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
                               1958                 :                : {
                               1959                 :                :     TupleDesc   tmp;
                               1960                 :                : 
                               1961                 :          52856 :     tmp = lookup_rowtype_tupdesc_internal(type_id, typmod, false);
                               1962                 :          52856 :     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
 3320                          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)
 3320 tgl@sss.pgh.pa.us        1999         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2000                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                               2001                 :                :                      errmsg("type %s is not composite",
                               2002                 :                :                             format_type_be(type_id))));
 3320 tgl@sss.pgh.pa.us        2003                 :CBC        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
 3385 andres@anarazel.de       2016                 :         341257 : record_type_typmod_hash(const void *data, size_t size)
                               2017                 :                : {
  320 peter@eisentraut.org     2018                 :         341257 :     const RecordCacheEntry *entry = data;
                               2019                 :                : 
  986                          2020                 :         341257 :     return hashRowType(entry->tupdesc);
                               2021                 :                : }
                               2022                 :                : 
                               2023                 :                : /*
                               2024                 :                :  * Match function for the hash table of RecordCacheEntry.
                               2025                 :                :  */
                               2026                 :                : static int
 3385 andres@anarazel.de       2027                 :         325577 : record_type_typmod_compare(const void *a, const void *b, size_t size)
                               2028                 :                : {
  320 peter@eisentraut.org     2029                 :         325577 :     const RecordCacheEntry *left = a;
                               2030                 :         325577 :     const RecordCacheEntry *right = b;
                               2031                 :                : 
  986                          2032                 :         325577 :     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
 8276 tgl@sss.pgh.pa.us        2043                 :         330295 : assign_record_type_typmod(TupleDesc tupDesc)
                               2044                 :                : {
                               2045                 :                :     RecordCacheEntry *recentry;
                               2046                 :                :     TupleDesc   entDesc;
                               2047                 :                :     bool        found;
                               2048                 :                :     MemoryContext oldcxt;
                               2049                 :                : 
                               2050         [ -  + ]:         330295 :     Assert(tupDesc->tdtypeid == RECORDOID);
                               2051                 :                : 
                               2052         [ +  + ]:         330295 :     if (RecordCacheHash == NULL)
                               2053                 :                :     {
                               2054                 :                :         /* First time through: initialize the hash table */
                               2055                 :                :         HASHCTL     ctl;
                               2056                 :                : 
 3385 andres@anarazel.de       2057                 :           4281 :         ctl.keysize = sizeof(TupleDesc);    /* just the pointer */
 8276 tgl@sss.pgh.pa.us        2058                 :           4281 :         ctl.entrysize = sizeof(RecordCacheEntry);
 3385 andres@anarazel.de       2059                 :           4281 :         ctl.hash = record_type_typmod_hash;
                               2060                 :           4281 :         ctl.match = record_type_typmod_compare;
 8276 tgl@sss.pgh.pa.us        2061                 :           4281 :         RecordCacheHash = hash_create("Record information cache", 64,
                               2062                 :                :                                       &ctl,
                               2063                 :                :                                       HASH_ELEM | HASH_FUNCTION | HASH_COMPARE);
                               2064                 :                : 
                               2065                 :                :         /* Also make sure CacheMemoryContext exists */
 6180                          2066         [ -  + ]:           4281 :         if (!CacheMemoryContext)
 6180 tgl@sss.pgh.pa.us        2067                 :UBC           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                 :                :      */
 8276 tgl@sss.pgh.pa.us        2075                 :CBC      330295 :     recentry = (RecordCacheEntry *) hash_search(RecordCacheHash,
                               2076                 :                :                                                 &tupDesc,
                               2077                 :                :                                                 HASH_FIND, &found);
 3385 andres@anarazel.de       2078   [ +  +  +  - ]:         330295 :     if (found && recentry->tupdesc != NULL)
                               2079                 :                :     {
                               2080                 :         319333 :         tupDesc->tdtypmod = recentry->tupdesc->tdtypmod;
                               2081                 :         319333 :         return;
                               2082                 :                :     }
                               2083                 :                : 
                               2084                 :                :     /* Not present, so need to manufacture an entry */
 8276 tgl@sss.pgh.pa.us        2085                 :          10962 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
                               2086                 :                : 
                               2087                 :                :     /* Look in the SharedRecordTypmodRegistry, if attached */
 3362 andres@anarazel.de       2088                 :          10962 :     entDesc = find_or_make_matching_shared_tupledesc(tupDesc);
                               2089         [ +  + ]:          10962 :     if (entDesc == NULL)
                               2090                 :                :     {
                               2091                 :                :         /*
                               2092                 :                :          * Make sure we have room before we CreateTupleDescCopy() or advance
                               2093                 :                :          * NextRecordTypmod.
                               2094                 :                :          */
 1968 jdavis@postgresql.or     2095                 :          10885 :         ensure_record_cache_typmod_slot_exists(NextRecordTypmod);
                               2096                 :                : 
                               2097                 :                :         /* Reference-counted local cache only. */
 3362 andres@anarazel.de       2098                 :          10885 :         entDesc = CreateTupleDescCopy(tupDesc);
                               2099                 :          10885 :         entDesc->tdrefcount = 1;
                               2100                 :          10885 :         entDesc->tdtypmod = NextRecordTypmod++;
                               2101                 :                :     }
                               2102                 :                :     else
                               2103                 :                :     {
 1968 jdavis@postgresql.or     2104                 :             77 :         ensure_record_cache_typmod_slot_exists(entDesc->tdtypmod);
                               2105                 :                :     }
                               2106                 :                : 
 1172 tmunro@postgresql.or     2107                 :          10962 :     RecordCacheArray[entDesc->tdtypmod].tupdesc = entDesc;
                               2108                 :                : 
                               2109                 :                :     /* Assign a unique tupdesc identifier, too. */
                               2110                 :          10962 :     RecordCacheArray[entDesc->tdtypmod].id = ++tupledesc_id_counter;
                               2111                 :                : 
                               2112                 :                :     /* Fully initialized; create the hash table entry */
 1968 jdavis@postgresql.or     2113                 :          10962 :     recentry = (RecordCacheEntry *) hash_search(RecordCacheHash,
                               2114                 :                :                                                 &tupDesc,
                               2115                 :                :                                                 HASH_ENTER, NULL);
                               2116                 :          10962 :     recentry->tupdesc = entDesc;
                               2117                 :                : 
                               2118                 :                :     /* Update the caller's tuple descriptor. */
 3362 andres@anarazel.de       2119                 :          10962 :     tupDesc->tdtypmod = entDesc->tdtypmod;
                               2120                 :                : 
                               2121                 :          10962 :     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
 3210 tgl@sss.pgh.pa.us        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                 :                : 
 3210 tgl@sss.pgh.pa.us        2144                 :UBC           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         [ #  # ]:              0 :         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                 :                :          */
 3210 tgl@sss.pgh.pa.us        2158   [ +  +  +  - ]:CBC        3726 :         if (typmod >= 0 && typmod < RecordCacheArrayLen &&
 1172 tmunro@postgresql.or     2159         [ +  - ]:             52 :             RecordCacheArray[typmod].tupdesc != NULL)
                               2160                 :                :         {
                               2161         [ -  + ]:             52 :             Assert(RecordCacheArray[typmod].id != 0);
                               2162                 :             52 :             return RecordCacheArray[typmod].id;
                               2163                 :                :         }
                               2164                 :                : 
                               2165                 :                :         /* For anonymous or unrecognized record type, generate a new ID */
 3210 tgl@sss.pgh.pa.us        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
 3362 andres@anarazel.de       2176                 :            122 : SharedRecordTypmodRegistryEstimate(void)
                               2177                 :                : {
                               2178                 :            122 :     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                 :            122 : 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         [ -  + ]:            122 :     Assert(!IsParallelWorker());
                               2208                 :                : 
                               2209                 :                :     /* We can't already be attached to a shared registry. */
                               2210         [ -  + ]:            122 :     Assert(CurrentSession->shared_typmod_registry == NULL);
                               2211         [ -  + ]:            122 :     Assert(CurrentSession->shared_record_table == NULL);
                               2212         [ -  + ]:            122 :     Assert(CurrentSession->shared_typmod_table == NULL);
                               2213                 :                : 
                               2214                 :            122 :     old_context = MemoryContextSwitchTo(TopMemoryContext);
                               2215                 :                : 
                               2216                 :                :     /* Create the hash table of tuple descriptors indexed by themselves. */
                               2217                 :            122 :     record_table = dshash_create(area, &srtr_record_table_params, area);
                               2218                 :                : 
                               2219                 :                :     /* Create the hash table of tuple descriptors indexed by typmod. */
                               2220                 :            122 :     typmod_table = dshash_create(area, &srtr_typmod_table_params, NULL);
                               2221                 :                : 
                               2222                 :            122 :     MemoryContextSwitchTo(old_context);
                               2223                 :                : 
                               2224                 :                :     /* Initialize the SharedRecordTypmodRegistry. */
                               2225                 :            122 :     registry->record_table_handle = dshash_get_hash_table_handle(record_table);
                               2226                 :            122 :     registry->typmod_table_handle = dshash_get_hash_table_handle(typmod_table);
                               2227                 :            122 :     pg_atomic_init_u32(&registry->next_typmod, NextRecordTypmod);
                               2228                 :                : 
                               2229                 :                :     /*
                               2230                 :                :      * Copy all entries from this backend's private registry into the shared
                               2231                 :                :      * registry.
                               2232                 :                :      */
                               2233         [ +  + ]:            350 :     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                 :                : 
 1172 tmunro@postgresql.or     2242                 :            228 :         tupdesc = RecordCacheArray[typmod].tupdesc;
 3362 andres@anarazel.de       2243         [ -  + ]:            228 :         if (tupdesc == NULL)
 3362 andres@anarazel.de       2244                 :UBC           0 :             continue;
                               2245                 :                : 
                               2246                 :                :         /* Copy the TupleDesc into shared memory. */
 3362 andres@anarazel.de       2247                 :CBC         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)
 3362 andres@anarazel.de       2254         [ #  # ]:UBC           0 :             elog(ERROR, "cannot create duplicate shared record typmod");
 3362 andres@anarazel.de       2255                 :CBC         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;
 3361 tgl@sss.pgh.pa.us        2261                 :            228 :         record_table_key.u.local_tupdesc = tupdesc;
 3362 andres@anarazel.de       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;
 3361 tgl@sss.pgh.pa.us        2268                 :            228 :             record_table_entry->key.u.shared_tupdesc = shared_dp;
                               2269                 :                :         }
 3362 andres@anarazel.de       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                 :            122 :     CurrentSession->shared_record_table = record_table;
                               2278                 :            122 :     CurrentSession->shared_typmod_table = typmod_table;
                               2279                 :            122 :     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                 :            122 :     on_dsm_detach(segment, shared_record_typmod_registry_detach, (Datum) 0);
                               2288                 :            122 : }
                               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         [ -  + ]:           2049 :     Assert(IsParallelWorker());
                               2304                 :                : 
                               2305                 :                :     /* We can't already be attached to a shared registry. */
                               2306         [ -  + ]:           2049 :     Assert(CurrentSession != NULL);
                               2307         [ -  + ]:           2049 :     Assert(CurrentSession->segment != NULL);
                               2308         [ -  + ]:           2049 :     Assert(CurrentSession->area != NULL);
                               2309         [ -  + ]:           2049 :     Assert(CurrentSession->shared_typmod_registry == NULL);
                               2310         [ -  + ]:           2049 :     Assert(CurrentSession->shared_record_table == NULL);
                               2311         [ -  + ]:           2049 :     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         [ -  + ]:           2049 :     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;
 8276 tgl@sss.pgh.pa.us        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
  765 akorotkov@postgresql     2366                 :          20310 : InvalidateCompositeTypeCacheEntry(TypeCacheEntry *typentry)
                               2367                 :                : {
                               2368   [ +  -  -  + ]:          20310 :     Assert(typentry->typtype == TYPTYPE_COMPOSITE &&
                               2369                 :                :            OidIsValid(typentry->typrelid));
                               2370                 :                : 
                               2371                 :                :     /* Delete tupdesc if we have it */
                               2372         [ +  + ]:          20310 :     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         [ -  + ]:           3650 :         Assert(typentry->tupDesc->tdrefcount > 0);
                               2380         [ +  + ]:           3650 :         if (--typentry->tupDesc->tdrefcount == 0)
                               2381                 :           2718 :             FreeTupleDesc(typentry->tupDesc);
                               2382                 :           3650 :         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                 :           3650 :         typentry->tupDesc_identifier = 0;
                               2389                 :                :     }
                               2390                 :                : 
                               2391                 :                :     /* Reset equality/comparison/hashing validity information */
                               2392                 :          20310 :     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                 :          20310 : }
                               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
 5931 tgl@sss.pgh.pa.us        2442                 :        1576920 : 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                 :                :      */
  765 akorotkov@postgresql     2450         [ +  + ]:        1576920 :     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                 :        1576219 :         relentry = (RelIdToTypeIdCacheEntry *) hash_search(RelIdToTypeIdCacheHash,
                               2460                 :                :                                                            &relid,
                               2461                 :                :                                                            HASH_FIND, NULL);
                               2462                 :                : 
                               2463         [ +  + ]:        1576219 :         if (relentry != NULL)
                               2464                 :                :         {
                               2465                 :          20153 :             typentry = (TypeCacheEntry *) hash_search(TypeCacheHash,
                               2466                 :          20153 :                                                       &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                 :                :              */
   19 rhaas@postgresql.org     2474         [ +  - ]:          20153 :             if (typentry != NULL &&
                               2475         [ +  - ]:          20153 :                 typentry->typtype == TYPTYPE_COMPOSITE &&
                               2476         [ +  - ]:          20153 :                 typentry->typrelid == relid)
  765 akorotkov@postgresql     2477                 :          20153 :                 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                 :        1576219 :         for (typentry = firstDomainTypeEntry;
                               2487         [ +  + ]:        2949950 :              typentry != NULL;
                               2488                 :        1373731 :              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                 :                :              */
 3320 tgl@sss.pgh.pa.us        2496         [ -  + ]:        1373731 :             if (typentry->flags & TCFLAGS_DOMAIN_BASE_IS_COMPOSITE)
 2458 tgl@sss.pgh.pa.us        2497                 :UBC           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                 :                :          */
  765 akorotkov@postgresql     2509                 :CBC         701 :         hash_seq_init(&status, TypeCacheHash);
                               2510         [ +  + ]:           4591 :         while ((typentry = (TypeCacheEntry *) hash_seq_search(&status)) != NULL)
                               2511                 :                :         {
                               2512         [ +  + ]:           3890 :             if (typentry->typtype == TYPTYPE_COMPOSITE)
                               2513                 :                :             {
                               2514                 :            157 :                 InvalidateCompositeTypeCacheEntry(typentry);
                               2515                 :                :             }
                               2516         [ +  + ]:           3733 :             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)
  765 akorotkov@postgresql     2526                 :UBC           0 :                     typentry->flags &= ~TCFLAGS_OPERATOR_FLAGS;
                               2527                 :                :             }
                               2528                 :                :         }
                               2529                 :                :     }
 2458 tgl@sss.pgh.pa.us        2530                 :CBC     1576920 : }
                               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
  283 michael@paquier.xyz      2541                 :         533135 : 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                 :                :      */
  843 akorotkov@postgresql     2553         [ +  + ]:         533135 :     if (hashvalue == 0)
                               2554                 :            294 :         hash_seq_init(&status, TypeCacheHash);
                               2555                 :                :     else
                               2556                 :         532841 :         hash_seq_init_with_hash_value(&status, TypeCacheHash, hashvalue);
                               2557                 :                : 
 2458 tgl@sss.pgh.pa.us        2558         [ +  + ]:         539408 :     while ((typentry = (TypeCacheEntry *) hash_seq_search(&status)) != NULL)
                               2559                 :                :     {
  843 akorotkov@postgresql     2560   [ +  +  -  + ]:           6273 :         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                 :           6273 :         typentry->flags &= ~(TCFLAGS_HAVE_PG_TYPE_DATA |
                               2568                 :                :                              TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS);
                               2569                 :                :     }
 4383 tgl@sss.pgh.pa.us        2570                 :         533135 : }
                               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
  283 michael@paquier.xyz      2589                 :           1727 : 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 */
 4383 tgl@sss.pgh.pa.us        2595                 :           1727 :     hash_seq_init(&status, TypeCacheHash);
                               2596         [ +  + ]:          10643 :     while ((typentry = (TypeCacheEntry *) hash_seq_search(&status)) != NULL)
                               2597                 :                :     {
                               2598                 :                :         /* Reset equality/comparison/hashing validity information */
 2458                          2599                 :           8916 :         typentry->flags &= ~TCFLAGS_OPERATOR_FLAGS;
                               2600                 :                :     }
 8276                          2601                 :           1727 : }
                               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
  283 michael@paquier.xyz      2618                 :         141771 : 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                 :                :      */
 4290 tgl@sss.pgh.pa.us        2628                 :         141771 :     for (typentry = firstDomainTypeEntry;
                               2629         [ +  + ]:         294161 :          typentry != NULL;
                               2630                 :         152390 :          typentry = typentry->nextDomain)
                               2631                 :                :     {
                               2632                 :                :         /* Reset domain constraint validity information */
                               2633                 :         152390 :         typentry->flags &= ~TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS;
                               2634                 :                :     }
                               2635                 :         141771 : }
                               2636                 :                : 
                               2637                 :                : 
                               2638                 :                : /*
                               2639                 :                :  * Check if given OID is part of the subset that's sortable by comparisons
                               2640                 :                :  */
                               2641                 :                : static inline bool
 5879                          2642                 :         151650 : enum_known_sorted(TypeCacheEnumData *enumdata, Oid arg)
                               2643                 :                : {
                               2644                 :                :     Oid         offset;
                               2645                 :                : 
                               2646         [ -  + ]:         151650 :     if (arg < enumdata->bitmap_base)
 5879 tgl@sss.pgh.pa.us        2647                 :UBC           0 :         return false;
 5879 tgl@sss.pgh.pa.us        2648                 :CBC      151650 :     offset = arg - enumdata->bitmap_base;
                               2649         [ -  + ]:         151650 :     if (offset > (Oid) INT_MAX)
 5879 tgl@sss.pgh.pa.us        2650                 :UBC           0 :         return false;
 5879 tgl@sss.pgh.pa.us        2651                 :CBC      151650 :     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                 :          76224 : 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         [ -  + ]:          76224 :     if (arg1 == arg2)
 5879 tgl@sss.pgh.pa.us        2682                 :UBC           0 :         return 0;
                               2683                 :                : 
                               2684                 :                :     /* Load up the cache if first time through */
 5879 tgl@sss.pgh.pa.us        2685         [ +  + ]:CBC       76224 :     if (tcache->enumData == NULL)
                               2686                 :              6 :         load_enum_cache_data(tcache);
                               2687                 :          76224 :     enumdata = tcache->enumData;
                               2688                 :                : 
                               2689                 :                :     /*
                               2690                 :                :      * If both OIDs are known-sorted, we can just compare them directly.
                               2691                 :                :      */
                               2692   [ +  +  -  + ]:         151650 :     if (enum_known_sorted(enumdata, arg1) &&
                               2693                 :          75426 :         enum_known_sorted(enumdata, arg2))
                               2694                 :                :     {
 5879 tgl@sss.pgh.pa.us        2695         [ #  # ]:UBC           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                 :                :      */
 5879 tgl@sss.pgh.pa.us        2704                 :CBC       76224 :     item1 = find_enumitem(enumdata, arg1);
                               2705                 :          76224 :     item2 = find_enumitem(enumdata, arg2);
                               2706                 :                : 
                               2707   [ +  -  -  + ]:          76224 :     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                 :                :          */
 5879 tgl@sss.pgh.pa.us        2714                 :UBC           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                 :                : 
 5879 tgl@sss.pgh.pa.us        2732         [ +  + ]:CBC       76224 :     if (item1->sort_order < item2->sort_order)
                               2733                 :          26067 :         return -1;
                               2734         [ +  - ]:          50157 :     else if (item1->sort_order > item2->sort_order)
                               2735                 :          50157 :         return 1;
                               2736                 :                :     else
 5879 tgl@sss.pgh.pa.us        2737                 :UBC           0 :         return 0;
                               2738                 :                : }
                               2739                 :                : 
                               2740                 :                : /*
                               2741                 :                :  * Load (or re-load) the enumData member of the typcache entry.
                               2742                 :                :  */
                               2743                 :                : static void
 5879 tgl@sss.pgh.pa.us        2744                 :CBC           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)
 5879 tgl@sss.pgh.pa.us        2762         [ #  # ]:UBC           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                 :                :      */
 5879 tgl@sss.pgh.pa.us        2774                 :CBC           6 :     maxitems = 64;
  353 michael@paquier.xyz      2775                 :              6 :     items = palloc_array(EnumItem, maxitems);
 5879 tgl@sss.pgh.pa.us        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                 :                : 
 2868 andres@anarazel.de       2784                 :              6 :     enum_rel = table_open(EnumRelationId, AccessShareLock);
 5879 tgl@sss.pgh.pa.us        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                 :                :         {
 5879 tgl@sss.pgh.pa.us        2796                 :UBC           0 :             maxitems *= 2;
  103 michael@paquier.xyz      2797                 :UNC           0 :             items = repalloc_array(items, EnumItem, maxitems);
                               2798                 :                :         }
 2930 andres@anarazel.de       2799                 :CBC          43 :         items[numitems].enum_oid = en->oid;
 5879 tgl@sss.pgh.pa.us        2800                 :             43 :         items[numitems].sort_order = en->enumsortorder;
                               2801                 :             43 :         numitems++;
                               2802                 :                :     }
                               2803                 :                : 
                               2804                 :              6 :     systable_endscan(enum_scan);
 2868 andres@anarazel.de       2805                 :              6 :     table_close(enum_rel, AccessShareLock);
                               2806                 :                : 
                               2807                 :                :     /* Sort the items into OID order */
 5879 tgl@sss.pgh.pa.us        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                 :                :          */
 5711 bruce@momjian.us         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                 :                : 
 5879 tgl@sss.pgh.pa.us        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)
 5879 tgl@sss.pgh.pa.us        2844                 :UBC           0 :                 break;
                               2845                 :                :             /* include the item if it's in-order */
 5879 tgl@sss.pgh.pa.us        2846         [ +  + ]:CBC          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)
 5879 tgl@sss.pgh.pa.us        2891                 :UBC           0 :         pfree(tcache->enumData);
 5879 tgl@sss.pgh.pa.us        2892                 :CBC           6 :     tcache->enumData = enumdata;
                               2893                 :              6 : }
                               2894                 :                : 
                               2895                 :                : /*
                               2896                 :                :  * Locate the EnumItem with the given OID, if present
                               2897                 :                :  */
                               2898                 :                : static EnumItem *
                               2899                 :         152448 : find_enumitem(TypeCacheEnumData *enumdata, Oid arg)
                               2900                 :                : {
                               2901                 :                :     EnumItem    srch;
                               2902                 :                : 
                               2903                 :                :     /* On some versions of Solaris, bsearch of zero items dumps core */
                               2904         [ -  + ]:         152448 :     if (enumdata->num_values <= 0)
 5879 tgl@sss.pgh.pa.us        2905                 :UBC           0 :         return NULL;
                               2906                 :                : 
 5879 tgl@sss.pgh.pa.us        2907                 :CBC      152448 :     srch.enum_oid = arg;
                               2908                 :         152448 :     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                 :         307268 : enum_oid_cmp(const void *left, const void *right)
                               2917                 :                : {
                               2918                 :         307268 :     const EnumItem *l = (const EnumItem *) left;
                               2919                 :         307268 :     const EnumItem *r = (const EnumItem *) right;
                               2920                 :                : 
 1016 nathan@postgresql.or     2921                 :         307268 :     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
 3362 andres@anarazel.de       2929                 :            285 : share_tupledesc(dsa_area *area, TupleDesc tupdesc, uint32 typmod)
                               2930                 :                : {
   19                          2931                 :            285 :     Size        size = TupleDescCopySize(tupdesc);
                               2932                 :                :     dsa_pointer shared_dp;
                               2933                 :                :     TupleDesc   shared;
                               2934                 :                : 
                               2935                 :            285 :     shared_dp = dsa_allocate(area, size);
 3362                          2936                 :            285 :     shared = (TupleDesc) dsa_get_address(area, shared_dp);
   19                          2937                 :            285 :     TupleDescCopySized(shared, size, tupdesc);
 3362                          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                 :          10962 : 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         [ +  + ]:          10962 :     if (CurrentSession->shared_typmod_registry == NULL)
                               2963                 :          10885 :         return NULL;
                               2964                 :                : 
                               2965                 :                :     /* Try to find a matching tuple descriptor in the record table. */
                               2966                 :             77 :     key.shared = false;
 3361 tgl@sss.pgh.pa.us        2967                 :             77 :     key.u.local_tupdesc = tupdesc;
                               2968                 :                :     record_table_entry = (SharedRecordTableEntry *)
 3362 andres@anarazel.de       2969                 :             77 :         dshash_find(CurrentSession->shared_record_table, &key, false);
                               2970         [ +  + ]:             77 :     if (record_table_entry)
                               2971                 :                :     {
                               2972         [ -  + ]:             20 :         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         [ -  + ]:             20 :         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)
 3362 andres@anarazel.de       3001         [ #  # ]:UBC           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                 :                :     }
 3362 andres@anarazel.de       3008         [ -  + ]:CBC          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                 :                :          */
 3362 andres@anarazel.de       3028                 :UBC           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                 :                :          */
   19 rhaas@postgresql.org     3038                 :              0 :         dshash_delete_entry(CurrentSession->shared_typmod_table,
                               3039                 :                :                             typmod_table_entry);
 3362 andres@anarazel.de       3040                 :              0 :         dsa_free(CurrentSession->area, shared_dp);
                               3041                 :                : 
                               3042                 :                :         /* Return the one we found. */
                               3043         [ #  # ]:              0 :         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         [ #  # ]:              0 :         Assert(result->tdrefcount == -1);
                               3048                 :                : 
                               3049                 :              0 :         return result;
                               3050                 :                :     }
                               3051                 :                : 
                               3052                 :                :     /* Store it and return it. */
 3362 andres@anarazel.de       3053                 :CBC          57 :     record_table_entry->key.shared = true;
 3361 tgl@sss.pgh.pa.us        3054                 :             57 :     record_table_entry->key.u.shared_tupdesc = shared_dp;
 3362 andres@anarazel.de       3055                 :             57 :     dshash_release_lock(CurrentSession->shared_record_table,
                               3056                 :                :                         record_table_entry);
   19 rhaas@postgresql.org     3057                 :             57 :     dshash_release_lock(CurrentSession->shared_typmod_table,
                               3058                 :                :                         typmod_table_entry);
                               3059                 :                :     result = (TupleDesc)
 3362 andres@anarazel.de       3060                 :             57 :         dsa_get_address(CurrentSession->area, shared_dp);
                               3061         [ -  + ]:             57 :     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                 :           2171 : shared_record_typmod_registry_detach(dsm_segment *segment, Datum datum)
                               3072                 :                : {
                               3073                 :                :     /* Be cautious here: maybe we didn't finish initializing. */
                               3074         [ +  - ]:           2171 :     if (CurrentSession->shared_record_table != NULL)
                               3075                 :                :     {
                               3076                 :           2171 :         dshash_detach(CurrentSession->shared_record_table);
                               3077                 :           2171 :         CurrentSession->shared_record_table = NULL;
                               3078                 :                :     }
                               3079         [ +  - ]:           2171 :     if (CurrentSession->shared_typmod_table != NULL)
                               3080                 :                :     {
                               3081                 :           2171 :         dshash_detach(CurrentSession->shared_typmod_table);
                               3082                 :           2171 :         CurrentSession->shared_typmod_table = NULL;
                               3083                 :                :     }
                               3084                 :           2171 :     CurrentSession->shared_typmod_registry = NULL;
                               3085                 :           2171 : }
                               3086                 :                : 
                               3087                 :                : /*
                               3088                 :                :  * Insert RelIdToTypeIdCacheHash entry if needed.
                               3089                 :                :  */
                               3090                 :                : static void
  765 akorotkov@postgresql     3091                 :          20806 : insert_rel_type_cache_if_needed(TypeCacheEntry *typentry)
                               3092                 :                : {
                               3093                 :                :     RelIdToTypeIdCacheEntry *relentry;
                               3094                 :                :     bool        found;
                               3095                 :                : 
                               3096                 :                :     /* Immediately quit for non-composite types */
                               3097         [ +  + ]:          20806 :     if (typentry->typtype != TYPTYPE_COMPOSITE)
                               3098                 :          17758 :         return;
                               3099                 :                : 
                               3100                 :                :     /* typrelid should be given for composite types */
                               3101         [ -  + ]:           3048 :     Assert(OidIsValid(typentry->typrelid));
                               3102                 :                : 
   19 rhaas@postgresql.org     3103                 :           3048 :     INJECTION_POINT("typecache-before-rel-type-cache-insert", NULL);
                               3104                 :                : 
                               3105                 :           3047 :     relentry = (RelIdToTypeIdCacheEntry *) hash_search(RelIdToTypeIdCacheHash,
                               3106                 :           3047 :                                                        &typentry->typrelid,
                               3107                 :                :                                                        HASH_ENTER, &found);
                               3108                 :           3047 :     relentry->relid = typentry->typrelid;
                               3109                 :           3047 :     relentry->composite_typid = typentry->type_id;
                               3110                 :                : }
                               3111                 :                : 
                               3112                 :                : void
  765 akorotkov@postgresql     3113                 :         443983 : AtEOXact_TypeCache(void)
                               3114                 :                : {
                               3115                 :         443983 : }
                               3116                 :                : 
                               3117                 :                : void
                               3118                 :          23143 : AtEOSubXact_TypeCache(void)
                               3119                 :                : {
                               3120                 :          23143 : }
        

Generated by: LCOV version 2.0-1