LCOV - differential code coverage report
Current view: top level - src/backend/utils/cache - typcache.c (source / functions) Coverage Total Hit LBC UBC GNC CBC DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 88.6 % 1017 901 1 115 3 898 3
Current Date: 2025-09-06 07:49:51 +0900 Functions: 96.8 % 62 60 2 1 59
Baseline: lcov-20250906-005545-baseline Branches: 69.8 % 708 494 1 213 494
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 90.7 % 108 98 10 3 95
(360..) days: 88.3 % 909 803 1 105 803
Function coverage date bins:
(30,360] days: 100.0 % 6 6 6
(360..) days: 96.4 % 56 54 2 1 53
Branch coverage date bins:
(30,360] days: 67.4 % 86 58 28 58
(360..) days: 70.1 % 622 436 1 185 436

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

Generated by: LCOV version 2.4-beta