PostgreSQL Source Code  git master
typcache.h File Reference
#include "access/tupdesc.h"
#include "fmgr.h"
#include "storage/dsm.h"
#include "utils/dsa.h"
Include dependency graph for typcache.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TypeCacheEntry
 
struct  DomainConstraintRef
 

Macros

#define TYPECACHE_EQ_OPR   0x00001
 
#define TYPECACHE_LT_OPR   0x00002
 
#define TYPECACHE_GT_OPR   0x00004
 
#define TYPECACHE_CMP_PROC   0x00008
 
#define TYPECACHE_HASH_PROC   0x00010
 
#define TYPECACHE_EQ_OPR_FINFO   0x00020
 
#define TYPECACHE_CMP_PROC_FINFO   0x00040
 
#define TYPECACHE_HASH_PROC_FINFO   0x00080
 
#define TYPECACHE_TUPDESC   0x00100
 
#define TYPECACHE_BTREE_OPFAMILY   0x00200
 
#define TYPECACHE_HASH_OPFAMILY   0x00400
 
#define TYPECACHE_RANGE_INFO   0x00800
 
#define TYPECACHE_DOMAIN_BASE_INFO   0x01000
 
#define TYPECACHE_DOMAIN_CONSTR_INFO   0x02000
 
#define TYPECACHE_HASH_EXTENDED_PROC   0x04000
 
#define TYPECACHE_HASH_EXTENDED_PROC_FINFO   0x08000
 
#define TYPECACHE_MULTIRANGE_INFO   0x10000
 
#define INVALID_TUPLEDESC_IDENTIFIER   ((uint64) 1)
 

Typedefs

typedef struct DomainConstraintCache DomainConstraintCache
 
typedef struct TypeCacheEntry TypeCacheEntry
 
typedef struct DomainConstraintRef DomainConstraintRef
 
typedef struct SharedRecordTypmodRegistry SharedRecordTypmodRegistry
 

Functions

TypeCacheEntrylookup_type_cache (Oid type_id, int flags)
 
void InitDomainConstraintRef (Oid type_id, DomainConstraintRef *ref, MemoryContext refctx, bool need_exprstate)
 
void UpdateDomainConstraintRef (DomainConstraintRef *ref)
 
bool DomainHasConstraints (Oid type_id)
 
TupleDesc lookup_rowtype_tupdesc (Oid type_id, int32 typmod)
 
TupleDesc lookup_rowtype_tupdesc_noerror (Oid type_id, int32 typmod, bool noError)
 
TupleDesc lookup_rowtype_tupdesc_copy (Oid type_id, int32 typmod)
 
TupleDesc lookup_rowtype_tupdesc_domain (Oid type_id, int32 typmod, bool noError)
 
void assign_record_type_typmod (TupleDesc tupDesc)
 
uint64 assign_record_type_identifier (Oid type_id, int32 typmod)
 
int compare_values_of_enum (TypeCacheEntry *tcache, Oid arg1, Oid arg2)
 
size_t SharedRecordTypmodRegistryEstimate (void)
 
void SharedRecordTypmodRegistryInit (SharedRecordTypmodRegistry *, dsm_segment *segment, dsa_area *area)
 
void SharedRecordTypmodRegistryAttach (SharedRecordTypmodRegistry *)
 
void AtEOXact_TypeCache (void)
 
void AtEOSubXact_TypeCache (void)
 

Macro Definition Documentation

◆ INVALID_TUPLEDESC_IDENTIFIER

#define INVALID_TUPLEDESC_IDENTIFIER   ((uint64) 1)

Definition at line 156 of file typcache.h.

◆ TYPECACHE_BTREE_OPFAMILY

#define TYPECACHE_BTREE_OPFAMILY   0x00200

Definition at line 146 of file typcache.h.

◆ TYPECACHE_CMP_PROC

#define TYPECACHE_CMP_PROC   0x00008

Definition at line 140 of file typcache.h.

◆ TYPECACHE_CMP_PROC_FINFO

#define TYPECACHE_CMP_PROC_FINFO   0x00040

Definition at line 143 of file typcache.h.

◆ TYPECACHE_DOMAIN_BASE_INFO

#define TYPECACHE_DOMAIN_BASE_INFO   0x01000

Definition at line 149 of file typcache.h.

◆ TYPECACHE_DOMAIN_CONSTR_INFO

#define TYPECACHE_DOMAIN_CONSTR_INFO   0x02000

Definition at line 150 of file typcache.h.

◆ TYPECACHE_EQ_OPR

#define TYPECACHE_EQ_OPR   0x00001

Definition at line 137 of file typcache.h.

◆ TYPECACHE_EQ_OPR_FINFO

#define TYPECACHE_EQ_OPR_FINFO   0x00020

Definition at line 142 of file typcache.h.

◆ TYPECACHE_GT_OPR

#define TYPECACHE_GT_OPR   0x00004

Definition at line 139 of file typcache.h.

◆ TYPECACHE_HASH_EXTENDED_PROC

#define TYPECACHE_HASH_EXTENDED_PROC   0x04000

Definition at line 151 of file typcache.h.

◆ TYPECACHE_HASH_EXTENDED_PROC_FINFO

#define TYPECACHE_HASH_EXTENDED_PROC_FINFO   0x08000

Definition at line 152 of file typcache.h.

◆ TYPECACHE_HASH_OPFAMILY

#define TYPECACHE_HASH_OPFAMILY   0x00400

Definition at line 147 of file typcache.h.

◆ TYPECACHE_HASH_PROC

#define TYPECACHE_HASH_PROC   0x00010

Definition at line 141 of file typcache.h.

◆ TYPECACHE_HASH_PROC_FINFO

#define TYPECACHE_HASH_PROC_FINFO   0x00080

Definition at line 144 of file typcache.h.

◆ TYPECACHE_LT_OPR

#define TYPECACHE_LT_OPR   0x00002

Definition at line 138 of file typcache.h.

◆ TYPECACHE_MULTIRANGE_INFO

#define TYPECACHE_MULTIRANGE_INFO   0x10000

Definition at line 153 of file typcache.h.

◆ TYPECACHE_RANGE_INFO

#define TYPECACHE_RANGE_INFO   0x00800

Definition at line 148 of file typcache.h.

◆ TYPECACHE_TUPDESC

#define TYPECACHE_TUPDESC   0x00100

Definition at line 145 of file typcache.h.

Typedef Documentation

◆ DomainConstraintCache

Definition at line 1 of file typcache.h.

◆ DomainConstraintRef

◆ SharedRecordTypmodRegistry

Definition at line 1 of file typcache.h.

◆ TypeCacheEntry

Function Documentation

◆ assign_record_type_identifier()

uint64 assign_record_type_identifier ( Oid  type_id,
int32  typmod 
)

Definition at line 2132 of file typcache.c.

2133 {
2134  if (type_id != RECORDOID)
2135  {
2136  /*
2137  * It's a named composite type, so use the regular typcache.
2138  */
2139  TypeCacheEntry *typentry;
2140 
2141  typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
2142  if (typentry->tupDesc == NULL)
2143  ereport(ERROR,
2144  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2145  errmsg("type %s is not composite",
2146  format_type_be(type_id))));
2147  Assert(typentry->tupDesc_identifier != 0);
2148  return typentry->tupDesc_identifier;
2149  }
2150  else
2151  {
2152  /*
2153  * It's a transient record type, so look in our record-type table.
2154  */
2155  if (typmod >= 0 && typmod < RecordCacheArrayLen &&
2156  RecordCacheArray[typmod].tupdesc != NULL)
2157  {
2158  Assert(RecordCacheArray[typmod].id != 0);
2159  return RecordCacheArray[typmod].id;
2160  }
2161 
2162  /* For anonymous or unrecognized record type, generate a new ID */
2163  return ++tupledesc_id_counter;
2164  }
2165 }
#define Assert(condition)
Definition: c.h:812
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
uint64 tupDesc_identifier
Definition: typcache.h:90
TupleDesc tupDesc
Definition: typcache.h:89
static RecordCacheArrayEntry * RecordCacheArray
Definition: typcache.c:304
static int32 RecordCacheArrayLen
Definition: typcache.c:305
static uint64 tupledesc_id_counter
Definition: typcache.c:313
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:386
#define TYPECACHE_TUPDESC
Definition: typcache.h:145

References Assert, ereport, errcode(), errmsg(), ERROR, format_type_be(), RecordCacheArrayEntry::id, lookup_type_cache(), RecordCacheArray, RecordCacheArrayLen, TypeCacheEntry::tupDesc, TypeCacheEntry::tupDesc_identifier, tupledesc_id_counter, and TYPECACHE_TUPDESC.

Referenced by expanded_record_fetch_tupdesc(), make_expanded_record_from_tupdesc(), and make_expanded_record_from_typeid().

◆ assign_record_type_typmod()

void assign_record_type_typmod ( TupleDesc  tupDesc)

Definition at line 2040 of file typcache.c.

2041 {
2042  RecordCacheEntry *recentry;
2043  TupleDesc entDesc;
2044  bool found;
2045  MemoryContext oldcxt;
2046 
2047  Assert(tupDesc->tdtypeid == RECORDOID);
2048 
2049  if (RecordCacheHash == NULL)
2050  {
2051  /* First time through: initialize the hash table */
2052  HASHCTL ctl;
2053 
2054  ctl.keysize = sizeof(TupleDesc); /* just the pointer */
2055  ctl.entrysize = sizeof(RecordCacheEntry);
2058  RecordCacheHash = hash_create("Record information cache", 64,
2059  &ctl,
2061 
2062  /* Also make sure CacheMemoryContext exists */
2063  if (!CacheMemoryContext)
2065  }
2066 
2067  /*
2068  * Find a hashtable entry for this tuple descriptor. We don't use
2069  * HASH_ENTER yet, because if it's missing, we need to make sure that all
2070  * the allocations succeed before we create the new entry.
2071  */
2073  &tupDesc,
2074  HASH_FIND, &found);
2075  if (found && recentry->tupdesc != NULL)
2076  {
2077  tupDesc->tdtypmod = recentry->tupdesc->tdtypmod;
2078  return;
2079  }
2080 
2081  /* Not present, so need to manufacture an entry */
2083 
2084  /* Look in the SharedRecordTypmodRegistry, if attached */
2085  entDesc = find_or_make_matching_shared_tupledesc(tupDesc);
2086  if (entDesc == NULL)
2087  {
2088  /*
2089  * Make sure we have room before we CreateTupleDescCopy() or advance
2090  * NextRecordTypmod.
2091  */
2093 
2094  /* Reference-counted local cache only. */
2095  entDesc = CreateTupleDescCopy(tupDesc);
2096  entDesc->tdrefcount = 1;
2097  entDesc->tdtypmod = NextRecordTypmod++;
2098  }
2099  else
2100  {
2102  }
2103 
2104  RecordCacheArray[entDesc->tdtypmod].tupdesc = entDesc;
2105 
2106  /* Assign a unique tupdesc identifier, too. */
2108 
2109  /* Fully initialized; create the hash table entry */
2111  &tupDesc,
2112  HASH_ENTER, NULL);
2113  recentry->tupdesc = entDesc;
2114 
2115  /* Update the caller's tuple descriptor. */
2116  tupDesc->tdtypmod = entDesc->tdtypmod;
2117 
2118  MemoryContextSwitchTo(oldcxt);
2119 }
void CreateCacheMemoryContext(void)
Definition: catcache.c:680
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_COMPARE
Definition: hsearch.h:99
#define HASH_FUNCTION
Definition: hsearch.h:98
MemoryContext CacheMemoryContext
Definition: mcxt.c:152
MemoryContextSwitchTo(old_ctx)
tree ctl
Definition: radixtree.h:1853
TupleDesc tupdesc
Definition: typcache.c:174
int tdrefcount
Definition: tupdesc.h:84
int32 tdtypmod
Definition: tupdesc.h:83
Oid tdtypeid
Definition: tupdesc.h:82
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:133
struct TupleDescData * TupleDesc
Definition: tupdesc.h:89
static TupleDesc find_or_make_matching_shared_tupledesc(TupleDesc tupdesc)
Definition: typcache.c:2929
static int32 NextRecordTypmod
Definition: typcache.c:306
static HTAB * RecordCacheHash
Definition: typcache.c:295
struct RecordCacheEntry RecordCacheEntry
static int record_type_typmod_compare(const void *a, const void *b, size_t size)
Definition: typcache.c:2024
static void ensure_record_cache_typmod_slot_exists(int32 typmod)
Definition: typcache.c:1797
static uint32 record_type_typmod_hash(const void *data, size_t size)
Definition: typcache.c:2013

References Assert, CacheMemoryContext, CreateCacheMemoryContext(), CreateTupleDescCopy(), ctl, ensure_record_cache_typmod_slot_exists(), find_or_make_matching_shared_tupledesc(), HASH_COMPARE, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FIND, HASH_FUNCTION, hash_search(), RecordCacheArrayEntry::id, MemoryContextSwitchTo(), NextRecordTypmod, record_type_typmod_compare(), record_type_typmod_hash(), RecordCacheArray, RecordCacheHash, TupleDescData::tdrefcount, TupleDescData::tdtypeid, TupleDescData::tdtypmod, RecordCacheEntry::tupdesc, RecordCacheArrayEntry::tupdesc, and tupledesc_id_counter.

Referenced by BlessTupleDesc(), ER_get_flat_size(), internal_get_result_type(), and SPI_returntuple().

◆ AtEOSubXact_TypeCache()

void AtEOSubXact_TypeCache ( void  )

Definition at line 3184 of file typcache.c.

3185 {
3187 }
static void finalize_in_progress_typentries(void)
Definition: typcache.c:3159

References finalize_in_progress_typentries().

Referenced by AbortSubTransaction(), and CommitSubTransaction().

◆ AtEOXact_TypeCache()

void AtEOXact_TypeCache ( void  )

Definition at line 3178 of file typcache.c.

3179 {
3181 }

References finalize_in_progress_typentries().

Referenced by AbortTransaction(), CommitTransaction(), and PrepareTransaction().

◆ compare_values_of_enum()

int compare_values_of_enum ( TypeCacheEntry tcache,
Oid  arg1,
Oid  arg2 
)

Definition at line 2650 of file typcache.c.

2651 {
2652  TypeCacheEnumData *enumdata;
2653  EnumItem *item1;
2654  EnumItem *item2;
2655 
2656  /*
2657  * Equal OIDs are certainly equal --- this case was probably handled by
2658  * our caller, but we may as well check.
2659  */
2660  if (arg1 == arg2)
2661  return 0;
2662 
2663  /* Load up the cache if first time through */
2664  if (tcache->enumData == NULL)
2665  load_enum_cache_data(tcache);
2666  enumdata = tcache->enumData;
2667 
2668  /*
2669  * If both OIDs are known-sorted, we can just compare them directly.
2670  */
2671  if (enum_known_sorted(enumdata, arg1) &&
2672  enum_known_sorted(enumdata, arg2))
2673  {
2674  if (arg1 < arg2)
2675  return -1;
2676  else
2677  return 1;
2678  }
2679 
2680  /*
2681  * Slow path: we have to identify their actual sort-order positions.
2682  */
2683  item1 = find_enumitem(enumdata, arg1);
2684  item2 = find_enumitem(enumdata, arg2);
2685 
2686  if (item1 == NULL || item2 == NULL)
2687  {
2688  /*
2689  * We couldn't find one or both values. That means the enum has
2690  * changed under us, so re-initialize the cache and try again. We
2691  * don't bother retrying the known-sorted case in this path.
2692  */
2693  load_enum_cache_data(tcache);
2694  enumdata = tcache->enumData;
2695 
2696  item1 = find_enumitem(enumdata, arg1);
2697  item2 = find_enumitem(enumdata, arg2);
2698 
2699  /*
2700  * If we still can't find the values, complain: we must have corrupt
2701  * data.
2702  */
2703  if (item1 == NULL)
2704  elog(ERROR, "enum value %u not found in cache for enum %s",
2705  arg1, format_type_be(tcache->type_id));
2706  if (item2 == NULL)
2707  elog(ERROR, "enum value %u not found in cache for enum %s",
2708  arg2, format_type_be(tcache->type_id));
2709  }
2710 
2711  if (item1->sort_order < item2->sort_order)
2712  return -1;
2713  else if (item1->sort_order > item2->sort_order)
2714  return 1;
2715  else
2716  return 0;
2717 }
#define elog(elevel,...)
Definition: elog.h:225
float4 sort_order
Definition: typcache.c:150
struct TypeCacheEnumData * enumData
Definition: typcache.h:130
static void load_enum_cache_data(TypeCacheEntry *tcache)
Definition: typcache.c:2723
static EnumItem * find_enumitem(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:2878
static bool enum_known_sorted(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:2621

References elog, enum_known_sorted(), TypeCacheEntry::enumData, ERROR, find_enumitem(), format_type_be(), load_enum_cache_data(), EnumItem::sort_order, and TypeCacheEntry::type_id.

Referenced by enum_cmp_internal().

◆ DomainHasConstraints()

bool DomainHasConstraints ( Oid  type_id)

Definition at line 1487 of file typcache.c.

1488 {
1489  TypeCacheEntry *typentry;
1490 
1491  /*
1492  * Note: a side effect is to cause the typcache's domain data to become
1493  * valid. This is fine since we'll likely need it soon if there is any.
1494  */
1495  typentry = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO);
1496 
1497  return (typentry->domainData != NULL);
1498 }
DomainConstraintCache * domainData
Definition: typcache.h:121
#define TYPECACHE_DOMAIN_CONSTR_INFO
Definition: typcache.h:150

References TypeCacheEntry::domainData, lookup_type_cache(), and TYPECACHE_DOMAIN_CONSTR_INFO.

Referenced by ATColumnChangeRequiresRewrite(), ATExecAddColumn(), eval_const_expressions_mutator(), ExecInitJsonCoercion(), and transformJsonFuncExpr().

◆ InitDomainConstraintRef()

void InitDomainConstraintRef ( Oid  type_id,
DomainConstraintRef ref,
MemoryContext  refctx,
bool  need_exprstate 
)

Definition at line 1400 of file typcache.c.

1402 {
1403  /* Look up the typcache entry --- we assume it survives indefinitely */
1405  ref->need_exprstate = need_exprstate;
1406  /* For safety, establish the callback before acquiring a refcount */
1407  ref->refctx = refctx;
1408  ref->dcc = NULL;
1410  ref->callback.arg = ref;
1412  /* Acquire refcount if there are constraints, and set up exported list */
1413  if (ref->tcache->domainData)
1414  {
1415  ref->dcc = ref->tcache->domainData;
1416  ref->dcc->dccRefCount++;
1417  if (ref->need_exprstate)
1419  ref->refctx);
1420  else
1421  ref->constraints = ref->dcc->constraints;
1422  }
1423  else
1424  ref->constraints = NIL;
1425 }
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:568
#define NIL
Definition: pg_list.h:68
DomainConstraintCache * dcc
Definition: typcache.h:172
MemoryContext refctx
Definition: typcache.h:167
MemoryContextCallback callback
Definition: typcache.h:173
TypeCacheEntry * tcache
Definition: typcache.h:168
MemoryContextCallbackFunction func
Definition: palloc.h:49
static List * prep_domain_constraints(List *constraints, MemoryContext execctx)
Definition: typcache.c:1362
static void dccref_deletion_callback(void *arg)
Definition: typcache.c:1341

References MemoryContextCallback::arg, DomainConstraintRef::callback, DomainConstraintCache::constraints, DomainConstraintRef::constraints, DomainConstraintRef::dcc, dccref_deletion_callback(), DomainConstraintCache::dccRefCount, TypeCacheEntry::domainData, MemoryContextCallback::func, lookup_type_cache(), MemoryContextRegisterResetCallback(), DomainConstraintRef::need_exprstate, NIL, prep_domain_constraints(), DomainConstraintRef::refctx, DomainConstraintRef::tcache, and TYPECACHE_DOMAIN_CONSTR_INFO.

Referenced by domain_state_setup(), and ExecInitCoerceToDomain().

◆ lookup_rowtype_tupdesc()

◆ lookup_rowtype_tupdesc_copy()

TupleDesc lookup_rowtype_tupdesc_copy ( Oid  type_id,
int32  typmod 
)

Definition at line 1954 of file typcache.c.

1955 {
1956  TupleDesc tmp;
1957 
1958  tmp = lookup_rowtype_tupdesc_internal(type_id, typmod, false);
1959  return CreateTupleDescCopyConstr(tmp);
1960 }
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:173

References CreateTupleDescCopyConstr(), and lookup_rowtype_tupdesc_internal().

Referenced by ExecInitExprRec(), ExecMakeTableFunctionResult(), get_expr_result_type(), internal_get_result_type(), and TypeGetTupleDesc().

◆ lookup_rowtype_tupdesc_domain()

TupleDesc lookup_rowtype_tupdesc_domain ( Oid  type_id,
int32  typmod,
bool  noError 
)

Definition at line 1976 of file typcache.c.

1977 {
1978  TupleDesc tupDesc;
1979 
1980  if (type_id != RECORDOID)
1981  {
1982  /*
1983  * Check for domain or named composite type. We might as well load
1984  * whichever data is needed.
1985  */
1986  TypeCacheEntry *typentry;
1987 
1988  typentry = lookup_type_cache(type_id,
1991  if (typentry->typtype == TYPTYPE_DOMAIN)
1993  typentry->domainBaseTypmod,
1994  noError);
1995  if (typentry->tupDesc == NULL && !noError)
1996  ereport(ERROR,
1997  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1998  errmsg("type %s is not composite",
1999  format_type_be(type_id))));
2000  tupDesc = typentry->tupDesc;
2001  }
2002  else
2003  tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
2004  if (tupDesc != NULL)
2005  PinTupleDesc(tupDesc);
2006  return tupDesc;
2007 }
int32 domainBaseTypmod
Definition: typcache.h:115
char typtype
Definition: typcache.h:43
Oid domainBaseType
Definition: typcache.h:114
TupleDesc lookup_rowtype_tupdesc_noerror(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1937
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:149

References TypeCacheEntry::domainBaseType, TypeCacheEntry::domainBaseTypmod, ereport, errcode(), errmsg(), ERROR, format_type_be(), lookup_rowtype_tupdesc_internal(), lookup_rowtype_tupdesc_noerror(), lookup_type_cache(), PinTupleDesc, TypeCacheEntry::tupDesc, TYPECACHE_DOMAIN_BASE_INFO, TYPECACHE_TUPDESC, and TypeCacheEntry::typtype.

Referenced by ExecEvalWholeRowVar(), hstore_from_record(), hstore_populate_record(), plperl_sv_to_datum(), and rowtype_field_matches().

◆ lookup_rowtype_tupdesc_noerror()

TupleDesc lookup_rowtype_tupdesc_noerror ( Oid  type_id,
int32  typmod,
bool  noError 
)

Definition at line 1937 of file typcache.c.

1938 {
1939  TupleDesc tupDesc;
1940 
1941  tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1942  if (tupDesc != NULL)
1943  PinTupleDesc(tupDesc);
1944  return tupDesc;
1945 }

References lookup_rowtype_tupdesc_internal(), and PinTupleDesc.

Referenced by lookup_rowtype_tupdesc_domain().

◆ lookup_type_cache()

TypeCacheEntry* lookup_type_cache ( Oid  type_id,
int  flags 
)

Definition at line 386 of file typcache.c.

387 {
388  TypeCacheEntry *typentry;
389  bool found;
390  int in_progress_offset;
391 
392  if (TypeCacheHash == NULL)
393  {
394  /* First time through: initialize the hash table */
395  HASHCTL ctl;
396  int allocsize;
397 
398  ctl.keysize = sizeof(Oid);
399  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  */
406  ctl.hash = type_cache_syshash;
407 
408  TypeCacheHash = hash_create("Type information cache", 64,
410 
412 
413  ctl.keysize = sizeof(Oid);
414  ctl.entrysize = sizeof(RelIdToTypeIdCacheEntry);
415  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 */
423 
424  /* Also make sure CacheMemoryContext exists */
425  if (!CacheMemoryContext)
427 
428  /*
429  * reserve enough in_progress_list slots for many cases
430  */
431  allocsize = 4;
434  allocsize * sizeof(*in_progress_list));
435  in_progress_list_maxlen = allocsize;
436  }
437 
438  Assert(TypeCacheHash != NULL && RelIdToTypeIdCacheHash != NULL);
439 
440  /* Register to catch invalidation messages */
442  {
443  int allocsize;
444 
445  allocsize = in_progress_list_maxlen * 2;
447  allocsize * sizeof(*in_progress_list));
448  in_progress_list_maxlen = allocsize;
449  }
450  in_progress_offset = in_progress_list_len++;
451  in_progress_list[in_progress_offset] = type_id;
452 
453  /* Try to look up an existing entry */
455  &type_id,
456  HASH_FIND, NULL);
457  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 
469  tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
470  if (!HeapTupleIsValid(tp))
471  ereport(ERROR,
472  (errcode(ERRCODE_UNDEFINED_OBJECT),
473  errmsg("type with OID %u does not exist", type_id)));
474  typtup = (Form_pg_type) GETSTRUCT(tp);
475  if (!typtup->typisdefined)
476  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 */
483  &type_id,
484  HASH_ENTER, &found);
485  Assert(!found); /* it wasn't there a moment ago */
486 
487  MemSet(typentry, 0, sizeof(TypeCacheEntry));
488 
489  /* These fields can never change, by definition */
490  typentry->type_id = type_id;
491  typentry->type_id_hash = get_hash_value(TypeCacheHash, &type_id);
492 
493  /* Keep this part in sync with the code below */
494  typentry->typlen = typtup->typlen;
495  typentry->typbyval = typtup->typbyval;
496  typentry->typalign = typtup->typalign;
497  typentry->typstorage = typtup->typstorage;
498  typentry->typtype = typtup->typtype;
499  typentry->typrelid = typtup->typrelid;
500  typentry->typsubscript = typtup->typsubscript;
501  typentry->typelem = typtup->typelem;
502  typentry->typcollation = typtup->typcollation;
503  typentry->flags |= TCFLAGS_HAVE_PG_TYPE_DATA;
504 
505  /* If it's a domain, immediately thread it into the domain cache list */
506  if (typentry->typtype == TYPTYPE_DOMAIN)
507  {
508  typentry->nextDomain = firstDomainTypeEntry;
509  firstDomainTypeEntry = typentry;
510  }
511 
512  ReleaseSysCache(tp);
513  }
514  else if (!(typentry->flags & TCFLAGS_HAVE_PG_TYPE_DATA))
515  {
516  /*
517  * We have an entry, but its pg_type row got changed, so reload the
518  * data obtained directly from pg_type.
519  */
520  HeapTuple tp;
521  Form_pg_type typtup;
522 
523  tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
524  if (!HeapTupleIsValid(tp))
525  ereport(ERROR,
526  (errcode(ERRCODE_UNDEFINED_OBJECT),
527  errmsg("type with OID %u does not exist", type_id)));
528  typtup = (Form_pg_type) GETSTRUCT(tp);
529  if (!typtup->typisdefined)
530  ereport(ERROR,
531  (errcode(ERRCODE_UNDEFINED_OBJECT),
532  errmsg("type \"%s\" is only a shell",
533  NameStr(typtup->typname))));
534 
535  /*
536  * Keep this part in sync with the code above. Many of these fields
537  * shouldn't ever change, particularly typtype, but copy 'em anyway.
538  */
539  typentry->typlen = typtup->typlen;
540  typentry->typbyval = typtup->typbyval;
541  typentry->typalign = typtup->typalign;
542  typentry->typstorage = typtup->typstorage;
543  typentry->typtype = typtup->typtype;
544  typentry->typrelid = typtup->typrelid;
545  typentry->typsubscript = typtup->typsubscript;
546  typentry->typelem = typtup->typelem;
547  typentry->typcollation = typtup->typcollation;
548  typentry->flags |= TCFLAGS_HAVE_PG_TYPE_DATA;
549 
550  ReleaseSysCache(tp);
551  }
552 
553  /*
554  * Look up opclasses if we haven't already and any dependent info is
555  * requested.
556  */
561  !(typentry->flags & TCFLAGS_CHECKED_BTREE_OPCLASS))
562  {
563  Oid opclass;
564 
565  opclass = GetDefaultOpClass(type_id, BTREE_AM_OID);
566  if (OidIsValid(opclass))
567  {
568  typentry->btree_opf = get_opclass_family(opclass);
569  typentry->btree_opintype = get_opclass_input_type(opclass);
570  }
571  else
572  {
573  typentry->btree_opf = typentry->btree_opintype = InvalidOid;
574  }
575 
576  /*
577  * Reset information derived from btree opclass. Note in particular
578  * that we'll redetermine the eq_opr even if we previously found one;
579  * this matters in case a btree opclass has been added to a type that
580  * previously had only a hash opclass.
581  */
582  typentry->flags &= ~(TCFLAGS_CHECKED_EQ_OPR |
587  }
588 
589  /*
590  * If we need to look up equality operator, and there's no btree opclass,
591  * force lookup of hash opclass.
592  */
593  if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
594  !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR) &&
595  typentry->btree_opf == InvalidOid)
596  flags |= TYPECACHE_HASH_OPFAMILY;
597 
602  !(typentry->flags & TCFLAGS_CHECKED_HASH_OPCLASS))
603  {
604  Oid opclass;
605 
606  opclass = GetDefaultOpClass(type_id, HASH_AM_OID);
607  if (OidIsValid(opclass))
608  {
609  typentry->hash_opf = get_opclass_family(opclass);
610  typentry->hash_opintype = get_opclass_input_type(opclass);
611  }
612  else
613  {
614  typentry->hash_opf = typentry->hash_opintype = InvalidOid;
615  }
616 
617  /*
618  * Reset information derived from hash opclass. We do *not* reset the
619  * eq_opr; if we already found one from the btree opclass, that
620  * decision is still good.
621  */
622  typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
624  typentry->flags |= TCFLAGS_CHECKED_HASH_OPCLASS;
625  }
626 
627  /*
628  * Look for requested operators and functions, if we haven't already.
629  */
630  if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
631  !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR))
632  {
633  Oid eq_opr = InvalidOid;
634 
635  if (typentry->btree_opf != InvalidOid)
636  eq_opr = get_opfamily_member(typentry->btree_opf,
637  typentry->btree_opintype,
638  typentry->btree_opintype,
640  if (eq_opr == InvalidOid &&
641  typentry->hash_opf != InvalidOid)
642  eq_opr = get_opfamily_member(typentry->hash_opf,
643  typentry->hash_opintype,
644  typentry->hash_opintype,
646 
647  /*
648  * If the proposed equality operator is array_eq or record_eq, check
649  * to see if the element type or column types support equality. If
650  * not, array_eq or record_eq would fail at runtime, so we don't want
651  * to report that the type has equality. (We can omit similar
652  * checking for ranges and multiranges because ranges can't be created
653  * in the first place unless their subtypes support equality.)
654  */
655  if (eq_opr == ARRAY_EQ_OP &&
656  !array_element_has_equality(typentry))
657  eq_opr = InvalidOid;
658  else if (eq_opr == RECORD_EQ_OP &&
659  !record_fields_have_equality(typentry))
660  eq_opr = InvalidOid;
661 
662  /* Force update of eq_opr_finfo only if we're changing state */
663  if (typentry->eq_opr != eq_opr)
664  typentry->eq_opr_finfo.fn_oid = InvalidOid;
665 
666  typentry->eq_opr = eq_opr;
667 
668  /*
669  * Reset info about hash functions whenever we pick up new info about
670  * equality operator. This is so we can ensure that the hash
671  * functions match the operator.
672  */
673  typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
675  typentry->flags |= TCFLAGS_CHECKED_EQ_OPR;
676  }
677  if ((flags & TYPECACHE_LT_OPR) &&
678  !(typentry->flags & TCFLAGS_CHECKED_LT_OPR))
679  {
680  Oid lt_opr = InvalidOid;
681 
682  if (typentry->btree_opf != InvalidOid)
683  lt_opr = get_opfamily_member(typentry->btree_opf,
684  typentry->btree_opintype,
685  typentry->btree_opintype,
687 
688  /*
689  * As above, make sure array_cmp or record_cmp will succeed; but again
690  * we need no special check for ranges or multiranges.
691  */
692  if (lt_opr == ARRAY_LT_OP &&
693  !array_element_has_compare(typentry))
694  lt_opr = InvalidOid;
695  else if (lt_opr == RECORD_LT_OP &&
696  !record_fields_have_compare(typentry))
697  lt_opr = InvalidOid;
698 
699  typentry->lt_opr = lt_opr;
700  typentry->flags |= TCFLAGS_CHECKED_LT_OPR;
701  }
702  if ((flags & TYPECACHE_GT_OPR) &&
703  !(typentry->flags & TCFLAGS_CHECKED_GT_OPR))
704  {
705  Oid gt_opr = InvalidOid;
706 
707  if (typentry->btree_opf != InvalidOid)
708  gt_opr = get_opfamily_member(typentry->btree_opf,
709  typentry->btree_opintype,
710  typentry->btree_opintype,
712 
713  /*
714  * As above, make sure array_cmp or record_cmp will succeed; but again
715  * we need no special check for ranges or multiranges.
716  */
717  if (gt_opr == ARRAY_GT_OP &&
718  !array_element_has_compare(typentry))
719  gt_opr = InvalidOid;
720  else if (gt_opr == RECORD_GT_OP &&
721  !record_fields_have_compare(typentry))
722  gt_opr = InvalidOid;
723 
724  typentry->gt_opr = gt_opr;
725  typentry->flags |= TCFLAGS_CHECKED_GT_OPR;
726  }
727  if ((flags & (TYPECACHE_CMP_PROC | TYPECACHE_CMP_PROC_FINFO)) &&
728  !(typentry->flags & TCFLAGS_CHECKED_CMP_PROC))
729  {
730  Oid cmp_proc = InvalidOid;
731 
732  if (typentry->btree_opf != InvalidOid)
733  cmp_proc = get_opfamily_proc(typentry->btree_opf,
734  typentry->btree_opintype,
735  typentry->btree_opintype,
736  BTORDER_PROC);
737 
738  /*
739  * As above, make sure array_cmp or record_cmp will succeed; but again
740  * we need no special check for ranges or multiranges.
741  */
742  if (cmp_proc == F_BTARRAYCMP &&
743  !array_element_has_compare(typentry))
744  cmp_proc = InvalidOid;
745  else if (cmp_proc == F_BTRECORDCMP &&
746  !record_fields_have_compare(typentry))
747  cmp_proc = InvalidOid;
748 
749  /* Force update of cmp_proc_finfo only if we're changing state */
750  if (typentry->cmp_proc != cmp_proc)
751  typentry->cmp_proc_finfo.fn_oid = InvalidOid;
752 
753  typentry->cmp_proc = cmp_proc;
754  typentry->flags |= TCFLAGS_CHECKED_CMP_PROC;
755  }
757  !(typentry->flags & TCFLAGS_CHECKED_HASH_PROC))
758  {
759  Oid hash_proc = InvalidOid;
760 
761  /*
762  * We insist that the eq_opr, if one has been determined, match the
763  * hash opclass; else report there is no hash function.
764  */
765  if (typentry->hash_opf != InvalidOid &&
766  (!OidIsValid(typentry->eq_opr) ||
767  typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
768  typentry->hash_opintype,
769  typentry->hash_opintype,
771  hash_proc = get_opfamily_proc(typentry->hash_opf,
772  typentry->hash_opintype,
773  typentry->hash_opintype,
775 
776  /*
777  * As above, make sure hash_array, hash_record, or hash_range will
778  * succeed.
779  */
780  if (hash_proc == F_HASH_ARRAY &&
781  !array_element_has_hashing(typentry))
782  hash_proc = InvalidOid;
783  else if (hash_proc == F_HASH_RECORD &&
784  !record_fields_have_hashing(typentry))
785  hash_proc = InvalidOid;
786  else if (hash_proc == F_HASH_RANGE &&
787  !range_element_has_hashing(typentry))
788  hash_proc = InvalidOid;
789 
790  /*
791  * Likewise for hash_multirange.
792  */
793  if (hash_proc == F_HASH_MULTIRANGE &&
795  hash_proc = InvalidOid;
796 
797  /* Force update of hash_proc_finfo only if we're changing state */
798  if (typentry->hash_proc != hash_proc)
799  typentry->hash_proc_finfo.fn_oid = InvalidOid;
800 
801  typentry->hash_proc = hash_proc;
802  typentry->flags |= TCFLAGS_CHECKED_HASH_PROC;
803  }
804  if ((flags & (TYPECACHE_HASH_EXTENDED_PROC |
807  {
808  Oid hash_extended_proc = InvalidOid;
809 
810  /*
811  * We insist that the eq_opr, if one has been determined, match the
812  * hash opclass; else report there is no hash function.
813  */
814  if (typentry->hash_opf != InvalidOid &&
815  (!OidIsValid(typentry->eq_opr) ||
816  typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
817  typentry->hash_opintype,
818  typentry->hash_opintype,
820  hash_extended_proc = get_opfamily_proc(typentry->hash_opf,
821  typentry->hash_opintype,
822  typentry->hash_opintype,
824 
825  /*
826  * As above, make sure hash_array_extended, hash_record_extended, or
827  * hash_range_extended will succeed.
828  */
829  if (hash_extended_proc == F_HASH_ARRAY_EXTENDED &&
831  hash_extended_proc = InvalidOid;
832  else if (hash_extended_proc == F_HASH_RECORD_EXTENDED &&
834  hash_extended_proc = InvalidOid;
835  else if (hash_extended_proc == F_HASH_RANGE_EXTENDED &&
837  hash_extended_proc = InvalidOid;
838 
839  /*
840  * Likewise for hash_multirange_extended.
841  */
842  if (hash_extended_proc == F_HASH_MULTIRANGE_EXTENDED &&
844  hash_extended_proc = InvalidOid;
845 
846  /* Force update of proc finfo only if we're changing state */
847  if (typentry->hash_extended_proc != hash_extended_proc)
849 
850  typentry->hash_extended_proc = hash_extended_proc;
852  }
853 
854  /*
855  * Set up fmgr lookup info as requested
856  *
857  * Note: we tell fmgr the finfo structures live in CacheMemoryContext,
858  * which is not quite right (they're really in the hash table's private
859  * memory context) but this will do for our purposes.
860  *
861  * Note: the code above avoids invalidating the finfo structs unless the
862  * referenced operator/function OID actually changes. This is to prevent
863  * unnecessary leakage of any subsidiary data attached to an finfo, since
864  * that would cause session-lifespan memory leaks.
865  */
866  if ((flags & TYPECACHE_EQ_OPR_FINFO) &&
867  typentry->eq_opr_finfo.fn_oid == InvalidOid &&
868  typentry->eq_opr != InvalidOid)
869  {
870  Oid eq_opr_func;
871 
872  eq_opr_func = get_opcode(typentry->eq_opr);
873  if (eq_opr_func != InvalidOid)
874  fmgr_info_cxt(eq_opr_func, &typentry->eq_opr_finfo,
876  }
877  if ((flags & TYPECACHE_CMP_PROC_FINFO) &&
878  typentry->cmp_proc_finfo.fn_oid == InvalidOid &&
879  typentry->cmp_proc != InvalidOid)
880  {
881  fmgr_info_cxt(typentry->cmp_proc, &typentry->cmp_proc_finfo,
883  }
884  if ((flags & TYPECACHE_HASH_PROC_FINFO) &&
885  typentry->hash_proc_finfo.fn_oid == InvalidOid &&
886  typentry->hash_proc != InvalidOid)
887  {
888  fmgr_info_cxt(typentry->hash_proc, &typentry->hash_proc_finfo,
890  }
891  if ((flags & TYPECACHE_HASH_EXTENDED_PROC_FINFO) &&
893  typentry->hash_extended_proc != InvalidOid)
894  {
896  &typentry->hash_extended_proc_finfo,
898  }
899 
900  /*
901  * If it's a composite type (row type), get tupdesc if requested
902  */
903  if ((flags & TYPECACHE_TUPDESC) &&
904  typentry->tupDesc == NULL &&
905  typentry->typtype == TYPTYPE_COMPOSITE)
906  {
907  load_typcache_tupdesc(typentry);
908  }
909 
910  /*
911  * If requested, get information about a range type
912  *
913  * This includes making sure that the basic info about the range element
914  * type is up-to-date.
915  */
916  if ((flags & TYPECACHE_RANGE_INFO) &&
917  typentry->typtype == TYPTYPE_RANGE)
918  {
919  if (typentry->rngelemtype == NULL)
920  load_rangetype_info(typentry);
921  else if (!(typentry->rngelemtype->flags & TCFLAGS_HAVE_PG_TYPE_DATA))
922  (void) lookup_type_cache(typentry->rngelemtype->type_id, 0);
923  }
924 
925  /*
926  * If requested, get information about a multirange type
927  */
928  if ((flags & TYPECACHE_MULTIRANGE_INFO) &&
929  typentry->rngtype == NULL &&
930  typentry->typtype == TYPTYPE_MULTIRANGE)
931  {
932  load_multirangetype_info(typentry);
933  }
934 
935  /*
936  * If requested, get information about a domain type
937  */
938  if ((flags & TYPECACHE_DOMAIN_BASE_INFO) &&
939  typentry->domainBaseType == InvalidOid &&
940  typentry->typtype == TYPTYPE_DOMAIN)
941  {
942  typentry->domainBaseTypmod = -1;
943  typentry->domainBaseType =
944  getBaseTypeAndTypmod(type_id, &typentry->domainBaseTypmod);
945  }
946  if ((flags & TYPECACHE_DOMAIN_CONSTR_INFO) &&
947  (typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
948  typentry->typtype == TYPTYPE_DOMAIN)
949  {
950  load_domaintype_info(typentry);
951  }
952 
953  INJECTION_POINT("typecache-before-rel-type-cache-insert");
954 
955  Assert(in_progress_offset + 1 == in_progress_list_len);
957 
959 
960  return typentry;
961 }
#define NameStr(name)
Definition: c.h:700
#define MemSet(start, val, len)
Definition: c.h:974
#define OidIsValid(objectId)
Definition: c.h:729
uint32 get_hash_value(HTAB *hashp, const void *keyPtr)
Definition: dynahash.c:911
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
#define HASHSTANDARD_PROC
Definition: hash.h:355
#define HASHEXTENDED_PROC
Definition: hash.h:356
#define HASH_BLOBS
Definition: hsearch.h:97
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
Definition: indexcmds.c:2338
#define INJECTION_POINT(name)
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1746
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1704
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1212
Oid get_opclass_family(Oid opclass)
Definition: lsyscache.c:1190
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:796
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1285
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:166
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2538
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1541
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
#define BTORDER_PROC
Definition: nbtree.h:707
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define HTEqualStrategyNumber
Definition: stratnum.h:41
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Oid fn_oid
Definition: fmgr.h:59
uint32 type_id_hash
Definition: typcache.h:36
FmgrInfo hash_proc_finfo
Definition: typcache.h:77
Oid hash_extended_proc
Definition: typcache.h:66
Oid typsubscript
Definition: typcache.h:45
FmgrInfo cmp_proc_finfo
Definition: typcache.h:76
char typalign
Definition: typcache.h:41
struct TypeCacheEntry * rngelemtype
Definition: typcache.h:98
FmgrInfo hash_extended_proc_finfo
Definition: typcache.h:78
struct TypeCacheEntry * rngtype
Definition: typcache.h:108
FmgrInfo eq_opr_finfo
Definition: typcache.h:75
Oid btree_opintype
Definition: typcache.h:58
struct TypeCacheEntry * nextDomain
Definition: typcache.h:133
bool typbyval
Definition: typcache.h:40
int16 typlen
Definition: typcache.h:39
Oid hash_opintype
Definition: typcache.h:60
Oid typcollation
Definition: typcache.h:47
char typstorage
Definition: typcache.h:42
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
#define TCFLAGS_CHECKED_BTREE_OPCLASS
Definition: typcache.c:100
#define TCFLAGS_CHECKED_HASH_OPCLASS
Definition: typcache.c:101
static bool range_element_has_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1713
static void insert_rel_type_cache_if_needed(TypeCacheEntry *typentry)
Definition: typcache.c:3061
static bool record_fields_have_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1592
static HTAB * RelIdToTypeIdCacheHash
Definition: typcache.c:87
static bool record_fields_have_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1600
static int in_progress_list_maxlen
Definition: typcache.c:228
static Oid * in_progress_list
Definition: typcache.c:226
#define TCFLAGS_CHECKED_GT_OPR
Definition: typcache.c:104
static bool multirange_element_has_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1753
static bool record_fields_have_equality(TypeCacheEntry *typentry)
Definition: typcache.c:1576
#define TCFLAGS_CHECKED_LT_OPR
Definition: typcache.c:103
#define TCFLAGS_CHECKED_HASH_PROC
Definition: typcache.c:106
static bool array_element_has_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1538
static bool array_element_has_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1530
static void load_multirangetype_info(TypeCacheEntry *typentry)
Definition: typcache.c:1059
static uint32 type_cache_syshash(const void *key, Size keysize)
Definition: typcache.c:359
#define TCFLAGS_CHECKED_CMP_PROC
Definition: typcache.c:105
static bool multirange_element_has_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1761
static int in_progress_list_len
Definition: typcache.c:227
static bool array_element_has_equality(TypeCacheEntry *typentry)
Definition: typcache.c:1514
static void load_rangetype_info(TypeCacheEntry *typentry)
Definition: typcache.c:1001
static bool range_element_has_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1721
static TypeCacheEntry * firstDomainTypeEntry
Definition: typcache.c:96
struct RelIdToTypeIdCacheEntry RelIdToTypeIdCacheEntry
#define TCFLAGS_CHECKED_HASH_EXTENDED_PROC
Definition: typcache.c:107
static void TypeCacheTypCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:2511
static void TypeCacheConstrCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:2597
static void TypeCacheOpcCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:2568
static void load_domaintype_info(TypeCacheEntry *typentry)
Definition: typcache.c:1081
static void TypeCacheRelCallback(Datum arg, Oid relid)
Definition: typcache.c:2415
static bool array_element_has_compare(TypeCacheEntry *typentry)
Definition: typcache.c:1522
#define TCFLAGS_HAVE_PG_TYPE_DATA
Definition: typcache.c:99
#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS
Definition: typcache.c:118
static HTAB * TypeCacheHash
Definition: typcache.c:79
static bool record_fields_have_compare(TypeCacheEntry *typentry)
Definition: typcache.c:1584
#define TCFLAGS_CHECKED_EQ_OPR
Definition: typcache.c:102
static void load_typcache_tupdesc(TypeCacheEntry *typentry)
Definition: typcache.c:967
#define TYPECACHE_HASH_PROC_FINFO
Definition: typcache.h:144
#define TYPECACHE_EQ_OPR
Definition: typcache.h:137
#define TYPECACHE_HASH_OPFAMILY
Definition: typcache.h:147
#define TYPECACHE_MULTIRANGE_INFO
Definition: typcache.h:153
#define TYPECACHE_EQ_OPR_FINFO
Definition: typcache.h:142
#define TYPECACHE_HASH_EXTENDED_PROC
Definition: typcache.h:151
#define TYPECACHE_BTREE_OPFAMILY
Definition: typcache.h:146
#define TYPECACHE_RANGE_INFO
Definition: typcache.h:148
#define TYPECACHE_GT_OPR
Definition: typcache.h:139
#define TYPECACHE_CMP_PROC
Definition: typcache.h:140
struct TypeCacheEntry TypeCacheEntry
#define TYPECACHE_LT_OPR
Definition: typcache.h:138
#define TYPECACHE_HASH_EXTENDED_PROC_FINFO
Definition: typcache.h:152
#define TYPECACHE_CMP_PROC_FINFO
Definition: typcache.h:143
#define TYPECACHE_HASH_PROC
Definition: typcache.h:141

References array_element_has_compare(), array_element_has_equality(), array_element_has_extended_hashing(), array_element_has_hashing(), Assert, BTEqualStrategyNumber, BTGreaterStrategyNumber, BTLessStrategyNumber, BTORDER_PROC, TypeCacheEntry::btree_opf, TypeCacheEntry::btree_opintype, CacheMemoryContext, CacheRegisterRelcacheCallback(), CacheRegisterSyscacheCallback(), TypeCacheEntry::cmp_proc, TypeCacheEntry::cmp_proc_finfo, CreateCacheMemoryContext(), ctl, TypeCacheEntry::domainBaseType, TypeCacheEntry::domainBaseTypmod, TypeCacheEntry::eq_opr, TypeCacheEntry::eq_opr_finfo, ereport, errcode(), errmsg(), ERROR, firstDomainTypeEntry, TypeCacheEntry::flags, fmgr_info_cxt(), FmgrInfo::fn_oid, get_hash_value(), get_opclass_family(), get_opclass_input_type(), get_opcode(), get_opfamily_member(), get_opfamily_proc(), getBaseTypeAndTypmod(), GetDefaultOpClass(), GETSTRUCT, TypeCacheEntry::gt_opr, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, TypeCacheEntry::hash_extended_proc, TypeCacheEntry::hash_extended_proc_finfo, HASH_FIND, HASH_FUNCTION, TypeCacheEntry::hash_opf, TypeCacheEntry::hash_opintype, TypeCacheEntry::hash_proc, TypeCacheEntry::hash_proc_finfo, hash_search(), HASHEXTENDED_PROC, HASHSTANDARD_PROC, HeapTupleIsValid, HTEqualStrategyNumber, in_progress_list, in_progress_list_len, in_progress_list_maxlen, INJECTION_POINT, insert_rel_type_cache_if_needed(), InvalidOid, load_domaintype_info(), load_multirangetype_info(), load_rangetype_info(), load_typcache_tupdesc(), TypeCacheEntry::lt_opr, MemoryContextAlloc(), MemSet, multirange_element_has_extended_hashing(), multirange_element_has_hashing(), NameStr, TypeCacheEntry::nextDomain, ObjectIdGetDatum(), OidIsValid, range_element_has_extended_hashing(), range_element_has_hashing(), record_fields_have_compare(), record_fields_have_equality(), record_fields_have_extended_hashing(), record_fields_have_hashing(), ReleaseSysCache(), RelIdToTypeIdCacheHash, repalloc(), TypeCacheEntry::rngelemtype, TypeCacheEntry::rngtype, SearchSysCache1(), TCFLAGS_CHECKED_BTREE_OPCLASS, TCFLAGS_CHECKED_CMP_PROC, TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS, TCFLAGS_CHECKED_EQ_OPR, TCFLAGS_CHECKED_GT_OPR, TCFLAGS_CHECKED_HASH_EXTENDED_PROC, TCFLAGS_CHECKED_HASH_OPCLASS, TCFLAGS_CHECKED_HASH_PROC, TCFLAGS_CHECKED_LT_OPR, TCFLAGS_HAVE_PG_TYPE_DATA, TypeCacheEntry::tupDesc, TypeCacheEntry::typalign, TypeCacheEntry::typbyval, TypeCacheEntry::typcollation, type_cache_syshash(), TypeCacheEntry::type_id, TypeCacheEntry::type_id_hash, TYPECACHE_BTREE_OPFAMILY, TYPECACHE_CMP_PROC, TYPECACHE_CMP_PROC_FINFO, TYPECACHE_DOMAIN_BASE_INFO, TYPECACHE_DOMAIN_CONSTR_INFO, TYPECACHE_EQ_OPR, TYPECACHE_EQ_OPR_FINFO, TYPECACHE_GT_OPR, TYPECACHE_HASH_EXTENDED_PROC, TYPECACHE_HASH_EXTENDED_PROC_FINFO, TYPECACHE_HASH_OPFAMILY, TYPECACHE_HASH_PROC, TYPECACHE_HASH_PROC_FINFO, TYPECACHE_LT_OPR, TYPECACHE_MULTIRANGE_INFO, TYPECACHE_RANGE_INFO, TYPECACHE_TUPDESC, TypeCacheConstrCallback(), TypeCacheHash, TypeCacheOpcCallback(), TypeCacheRelCallback(), TypeCacheTypCallback(), TypeCacheEntry::typelem, TypeCacheEntry::typlen, TypeCacheEntry::typrelid, TypeCacheEntry::typstorage, TypeCacheEntry::typsubscript, and TypeCacheEntry::typtype.

Referenced by analyzeCTE(), appendOrderBySuffix(), array_cmp(), array_contain_compare(), array_eq(), array_position_common(), array_positions(), array_replace_internal(), array_reverse(), array_sample(), array_shuffle(), array_typanalyze(), assign_record_type_identifier(), brin_bloom_opcinfo(), brin_inclusion_opcinfo(), brin_minmax_multi_opcinfo(), brin_minmax_opcinfo(), build_datatype(), build_mss(), cache_array_element_properties(), cache_multirange_element_properties(), cache_range_element_properties(), cache_record_field_properties(), calc_arraycontsel(), check_exclusion_or_unique_constraint(), check_memoizable(), contain_leaked_vars_walker(), CreateStatistics(), dependency_degree(), domain_state_setup(), DomainHasConstraints(), enum_cmp_internal(), ExecInitExprRec(), find_simplified_clause(), foreign_expr_walker(), get_attr_stat_type(), get_cached_rowtype(), get_elem_stat_type(), get_multirange_io_data(), get_range_io_data(), get_rule_orderby(), get_sort_group_operators(), hash_array(), hash_array_extended(), hash_multirange(), hash_multirange_extended(), hash_range(), hash_range_extended(), hash_record(), hash_record_extended(), InitDomainConstraintRef(), initGinState(), load_multirangetype_info(), load_rangetype_info(), lookup_rowtype_tupdesc_domain(), lookup_rowtype_tupdesc_internal(), make_expanded_record_from_tupdesc(), make_expanded_record_from_typeid(), multirange_get_typcache(), multirange_unnest(), ndistinct_for_combination(), op_hashjoinable(), op_mergejoinable(), paraminfo_get_equal_hashops(), PLy_input_setup_func(), PLy_output_setup_func(), range_get_typcache(), record_cmp(), record_eq(), revalidate_rectypeid(), scalararraysel(), scalararraysel_containment(), show_sortorder_options(), statext_mcv_serialize(), tuples_equal(), and width_bucket_array().

◆ SharedRecordTypmodRegistryAttach()

void SharedRecordTypmodRegistryAttach ( SharedRecordTypmodRegistry registry)

Definition at line 2294 of file typcache.c.

2295 {
2296  MemoryContext old_context;
2297  dshash_table *record_table;
2298  dshash_table *typmod_table;
2299 
2301 
2302  /* We can't already be attached to a shared registry. */
2303  Assert(CurrentSession != NULL);
2304  Assert(CurrentSession->segment != NULL);
2305  Assert(CurrentSession->area != NULL);
2309 
2310  /*
2311  * We can't already have typmods in our local cache, because they'd clash
2312  * with those imported by SharedRecordTypmodRegistryInit. This should be
2313  * a freshly started parallel worker. If we ever support worker
2314  * recycling, a worker would need to zap its local cache in between
2315  * servicing different queries, in order to be able to call this and
2316  * synchronize typmods with a new leader; but that's problematic because
2317  * we can't be very sure that record-typmod-related state hasn't escaped
2318  * to anywhere else in the process.
2319  */
2320  Assert(NextRecordTypmod == 0);
2321 
2322  old_context = MemoryContextSwitchTo(TopMemoryContext);
2323 
2324  /* Attach to the two hash tables. */
2325  record_table = dshash_attach(CurrentSession->area,
2327  registry->record_table_handle,
2328  CurrentSession->area);
2329  typmod_table = dshash_attach(CurrentSession->area,
2331  registry->typmod_table_handle,
2332  NULL);
2333 
2334  MemoryContextSwitchTo(old_context);
2335 
2336  /*
2337  * Set up detach hook to run at worker exit. Currently this is the same
2338  * as the leader's detach hook, but in future they might need to be
2339  * different.
2340  */
2343  PointerGetDatum(registry));
2344 
2345  /*
2346  * Set up the session state that will tell assign_record_type_typmod and
2347  * lookup_rowtype_tupdesc_internal about the shared registry.
2348  */
2350  CurrentSession->shared_record_table = record_table;
2351  CurrentSession->shared_typmod_table = typmod_table;
2352 }
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1132
#define IsParallelWorker()
Definition: parallel.h:60
MemoryContext TopMemoryContext
Definition: mcxt.c:149
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
Session * CurrentSession
Definition: session.c:48
dsm_segment * segment
Definition: session.h:27
dshash_table * shared_record_table
Definition: session.h:32
struct SharedRecordTypmodRegistry * shared_typmod_registry
Definition: session.h:31
dsa_area * area
Definition: session.h:28
dshash_table * shared_typmod_table
Definition: session.h:33
dshash_table_handle typmod_table_handle
Definition: typcache.c:186
dshash_table_handle record_table_handle
Definition: typcache.c:184
static const dshash_parameters srtr_typmod_table_params
Definition: typcache.c:285
static void shared_record_typmod_registry_detach(dsm_segment *segment, Datum datum)
Definition: typcache.c:3041
static const dshash_parameters srtr_record_table_params
Definition: typcache.c:275

References Session::area, Assert, CurrentSession, dshash_attach(), IsParallelWorker, MemoryContextSwitchTo(), NextRecordTypmod, on_dsm_detach(), PointerGetDatum(), SharedRecordTypmodRegistry::record_table_handle, Session::segment, Session::shared_record_table, shared_record_typmod_registry_detach(), Session::shared_typmod_registry, Session::shared_typmod_table, srtr_record_table_params, srtr_typmod_table_params, TopMemoryContext, and SharedRecordTypmodRegistry::typmod_table_handle.

Referenced by AttachSession().

◆ SharedRecordTypmodRegistryEstimate()

size_t SharedRecordTypmodRegistryEstimate ( void  )

Definition at line 2173 of file typcache.c.

2174 {
2175  return sizeof(SharedRecordTypmodRegistry);
2176 }
struct SharedRecordTypmodRegistry SharedRecordTypmodRegistry
Definition: typcache.h:176

Referenced by GetSessionDsmHandle().

◆ SharedRecordTypmodRegistryInit()

void SharedRecordTypmodRegistryInit ( SharedRecordTypmodRegistry registry,
dsm_segment segment,
dsa_area area 
)

Definition at line 2195 of file typcache.c.

2198 {
2199  MemoryContext old_context;
2200  dshash_table *record_table;
2201  dshash_table *typmod_table;
2202  int32 typmod;
2203 
2205 
2206  /* We can't already be attached to a shared registry. */
2210 
2211  old_context = MemoryContextSwitchTo(TopMemoryContext);
2212 
2213  /* Create the hash table of tuple descriptors indexed by themselves. */
2214  record_table = dshash_create(area, &srtr_record_table_params, area);
2215 
2216  /* Create the hash table of tuple descriptors indexed by typmod. */
2217  typmod_table = dshash_create(area, &srtr_typmod_table_params, NULL);
2218 
2219  MemoryContextSwitchTo(old_context);
2220 
2221  /* Initialize the SharedRecordTypmodRegistry. */
2222  registry->record_table_handle = dshash_get_hash_table_handle(record_table);
2223  registry->typmod_table_handle = dshash_get_hash_table_handle(typmod_table);
2225 
2226  /*
2227  * Copy all entries from this backend's private registry into the shared
2228  * registry.
2229  */
2230  for (typmod = 0; typmod < NextRecordTypmod; ++typmod)
2231  {
2232  SharedTypmodTableEntry *typmod_table_entry;
2233  SharedRecordTableEntry *record_table_entry;
2234  SharedRecordTableKey record_table_key;
2235  dsa_pointer shared_dp;
2236  TupleDesc tupdesc;
2237  bool found;
2238 
2239  tupdesc = RecordCacheArray[typmod].tupdesc;
2240  if (tupdesc == NULL)
2241  continue;
2242 
2243  /* Copy the TupleDesc into shared memory. */
2244  shared_dp = share_tupledesc(area, tupdesc, typmod);
2245 
2246  /* Insert into the typmod table. */
2247  typmod_table_entry = dshash_find_or_insert(typmod_table,
2248  &tupdesc->tdtypmod,
2249  &found);
2250  if (found)
2251  elog(ERROR, "cannot create duplicate shared record typmod");
2252  typmod_table_entry->typmod = tupdesc->tdtypmod;
2253  typmod_table_entry->shared_tupdesc = shared_dp;
2254  dshash_release_lock(typmod_table, typmod_table_entry);
2255 
2256  /* Insert into the record table. */
2257  record_table_key.shared = false;
2258  record_table_key.u.local_tupdesc = tupdesc;
2259  record_table_entry = dshash_find_or_insert(record_table,
2260  &record_table_key,
2261  &found);
2262  if (!found)
2263  {
2264  record_table_entry->key.shared = true;
2265  record_table_entry->key.u.shared_tupdesc = shared_dp;
2266  }
2267  dshash_release_lock(record_table, record_table_entry);
2268  }
2269 
2270  /*
2271  * Set up the global state that will tell assign_record_type_typmod and
2272  * lookup_rowtype_tupdesc_internal about the shared registry.
2273  */
2274  CurrentSession->shared_record_table = record_table;
2275  CurrentSession->shared_typmod_table = typmod_table;
2277 
2278  /*
2279  * We install a detach hook in the leader, but only to handle cleanup on
2280  * failure during GetSessionDsmHandle(). Once GetSessionDsmHandle() pins
2281  * the memory, the leader process will use a shared registry until it
2282  * exits.
2283  */
2285 }
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:221
int32_t int32
Definition: c.h:481
uint64 dsa_pointer
Definition: dsa.h:62
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:367
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:206
SharedRecordTableKey key
Definition: typcache.c:213
TupleDesc local_tupdesc
Definition: typcache.c:201
dsa_pointer shared_tupdesc
Definition: typcache.c:202
union SharedRecordTableKey::@31 u
pg_atomic_uint32 next_typmod
Definition: typcache.c:188
dsa_pointer shared_tupdesc
Definition: typcache.c:223
static dsa_pointer share_tupledesc(dsa_area *area, TupleDesc tupdesc, uint32 typmod)
Definition: typcache.c:2908

References Assert, CurrentSession, dshash_create(), dshash_find_or_insert(), dshash_get_hash_table_handle(), dshash_release_lock(), elog, ERROR, IsParallelWorker, SharedRecordTableEntry::key, SharedRecordTableKey::local_tupdesc, MemoryContextSwitchTo(), SharedRecordTypmodRegistry::next_typmod, NextRecordTypmod, on_dsm_detach(), pg_atomic_init_u32(), SharedRecordTypmodRegistry::record_table_handle, RecordCacheArray, share_tupledesc(), SharedRecordTableKey::shared, Session::shared_record_table, shared_record_typmod_registry_detach(), SharedRecordTableKey::shared_tupdesc, SharedTypmodTableEntry::shared_tupdesc, Session::shared_typmod_registry, Session::shared_typmod_table, srtr_record_table_params, srtr_typmod_table_params, TupleDescData::tdtypmod, TopMemoryContext, RecordCacheArrayEntry::tupdesc, SharedTypmodTableEntry::typmod, SharedRecordTypmodRegistry::typmod_table_handle, and SharedRecordTableKey::u.

Referenced by GetSessionDsmHandle().

◆ UpdateDomainConstraintRef()

void UpdateDomainConstraintRef ( DomainConstraintRef ref)

Definition at line 1438 of file typcache.c.

1439 {
1440  TypeCacheEntry *typentry = ref->tcache;
1441 
1442  /* Make sure typcache entry's data is up to date */
1443  if ((typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
1444  typentry->typtype == TYPTYPE_DOMAIN)
1445  load_domaintype_info(typentry);
1446 
1447  /* Transfer to ref object if there's new info, adjusting refcounts */
1448  if (ref->dcc != typentry->domainData)
1449  {
1450  /* Paranoia --- be sure link is nulled before trying to release */
1451  DomainConstraintCache *dcc = ref->dcc;
1452 
1453  if (dcc)
1454  {
1455  /*
1456  * Note: we just leak the previous list of executable domain
1457  * constraints. Alternatively, we could keep those in a child
1458  * context of ref->refctx and free that context at this point.
1459  * However, in practice this code path will be taken so seldom
1460  * that the extra bookkeeping for a child context doesn't seem
1461  * worthwhile; we'll just allow a leak for the lifespan of refctx.
1462  */
1463  ref->constraints = NIL;
1464  ref->dcc = NULL;
1465  decr_dcc_refcount(dcc);
1466  }
1467  dcc = typentry->domainData;
1468  if (dcc)
1469  {
1470  ref->dcc = dcc;
1471  dcc->dccRefCount++;
1472  if (ref->need_exprstate)
1474  ref->refctx);
1475  else
1476  ref->constraints = dcc->constraints;
1477  }
1478  }
1479 }
static void decr_dcc_refcount(DomainConstraintCache *dcc)
Definition: typcache.c:1330

References DomainConstraintCache::constraints, DomainConstraintRef::constraints, DomainConstraintRef::dcc, DomainConstraintCache::dccRefCount, decr_dcc_refcount(), TypeCacheEntry::domainData, TypeCacheEntry::flags, load_domaintype_info(), DomainConstraintRef::need_exprstate, NIL, prep_domain_constraints(), DomainConstraintRef::refctx, DomainConstraintRef::tcache, TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS, and TypeCacheEntry::typtype.

Referenced by domain_check_input().