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   0x0001
 
#define TYPECACHE_LT_OPR   0x0002
 
#define TYPECACHE_GT_OPR   0x0004
 
#define TYPECACHE_CMP_PROC   0x0008
 
#define TYPECACHE_HASH_PROC   0x0010
 
#define TYPECACHE_EQ_OPR_FINFO   0x0020
 
#define TYPECACHE_CMP_PROC_FINFO   0x0040
 
#define TYPECACHE_HASH_PROC_FINFO   0x0080
 
#define TYPECACHE_TUPDESC   0x0100
 
#define TYPECACHE_BTREE_OPFAMILY   0x0200
 
#define TYPECACHE_HASH_OPFAMILY   0x0400
 
#define TYPECACHE_RANGE_INFO   0x0800
 
#define TYPECACHE_DOMAIN_BASE_INFO   0x1000
 
#define TYPECACHE_DOMAIN_CONSTR_INFO   0x2000
 
#define TYPECACHE_HASH_EXTENDED_PROC   0x4000
 
#define TYPECACHE_HASH_EXTENDED_PROC_FINFO   0x8000
 

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)
 
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 *)
 

Macro Definition Documentation

◆ TYPECACHE_BTREE_OPFAMILY

#define TYPECACHE_BTREE_OPFAMILY   0x0200

Definition at line 133 of file typcache.h.

Referenced by lookup_type_cache().

◆ TYPECACHE_CMP_PROC

#define TYPECACHE_CMP_PROC   0x0008

◆ TYPECACHE_CMP_PROC_FINFO

#define TYPECACHE_CMP_PROC_FINFO   0x0040

◆ TYPECACHE_DOMAIN_BASE_INFO

#define TYPECACHE_DOMAIN_BASE_INFO   0x1000

◆ TYPECACHE_DOMAIN_CONSTR_INFO

#define TYPECACHE_DOMAIN_CONSTR_INFO   0x2000

Definition at line 137 of file typcache.h.

Referenced by DomainHasConstraints(), InitDomainConstraintRef(), and lookup_type_cache().

◆ TYPECACHE_EQ_OPR

◆ TYPECACHE_EQ_OPR_FINFO

#define TYPECACHE_EQ_OPR_FINFO   0x0020

◆ TYPECACHE_GT_OPR

#define TYPECACHE_GT_OPR   0x0004

◆ TYPECACHE_HASH_EXTENDED_PROC

#define TYPECACHE_HASH_EXTENDED_PROC   0x4000

◆ TYPECACHE_HASH_EXTENDED_PROC_FINFO

#define TYPECACHE_HASH_EXTENDED_PROC_FINFO   0x8000

Definition at line 139 of file typcache.h.

Referenced by hash_array_extended(), hash_range_extended(), and lookup_type_cache().

◆ TYPECACHE_HASH_OPFAMILY

#define TYPECACHE_HASH_OPFAMILY   0x0400

Definition at line 134 of file typcache.h.

Referenced by lookup_type_cache().

◆ TYPECACHE_HASH_PROC

#define TYPECACHE_HASH_PROC   0x0010

◆ TYPECACHE_HASH_PROC_FINFO

#define TYPECACHE_HASH_PROC_FINFO   0x0080

Definition at line 131 of file typcache.h.

Referenced by array_typanalyze(), hash_array(), hash_range(), and lookup_type_cache().

◆ TYPECACHE_LT_OPR

◆ TYPECACHE_RANGE_INFO

#define TYPECACHE_RANGE_INFO   0x0800

Definition at line 135 of file typcache.h.

Referenced by get_range_io_data(), lookup_type_cache(), and range_get_typcache().

◆ TYPECACHE_TUPDESC

#define TYPECACHE_TUPDESC   0x0100

Typedef Documentation

◆ DomainConstraintCache

Definition at line 26 of file typcache.h.

◆ DomainConstraintRef

◆ SharedRecordTypmodRegistry

◆ TypeCacheEntry

Function Documentation

◆ assign_record_type_typmod()

void assign_record_type_typmod ( TupleDesc  tupDesc)

Definition at line 1738 of file typcache.c.

References Assert, CacheMemoryContext, CreateCacheMemoryContext(), CreateTupleDescCopy(), ensure_record_cache_typmod_slot_exists(), HASHCTL::entrysize, find_or_make_matching_shared_tupledesc(), HASHCTL::hash, HASH_COMPARE, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FUNCTION, hash_search(), HASHCTL::keysize, HASHCTL::match, MemoryContextSwitchTo(), MemSet, NextRecordTypmod, record_type_typmod_compare(), record_type_typmod_hash(), RECORDOID, tupleDesc::tdrefcount, tupleDesc::tdtypeid, tupleDesc::tdtypmod, and RecordCacheEntry::tupdesc.

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

1739 {
1740  RecordCacheEntry *recentry;
1741  TupleDesc entDesc;
1742  bool found;
1743  MemoryContext oldcxt;
1744 
1745  Assert(tupDesc->tdtypeid == RECORDOID);
1746 
1747  if (RecordCacheHash == NULL)
1748  {
1749  /* First time through: initialize the hash table */
1750  HASHCTL ctl;
1751 
1752  MemSet(&ctl, 0, sizeof(ctl));
1753  ctl.keysize = sizeof(TupleDesc); /* just the pointer */
1754  ctl.entrysize = sizeof(RecordCacheEntry);
1757  RecordCacheHash = hash_create("Record information cache", 64,
1758  &ctl,
1760 
1761  /* Also make sure CacheMemoryContext exists */
1762  if (!CacheMemoryContext)
1764  }
1765 
1766  /* Find or create a hashtable entry for this tuple descriptor */
1768  (void *) &tupDesc,
1769  HASH_ENTER, &found);
1770  if (found && recentry->tupdesc != NULL)
1771  {
1772  tupDesc->tdtypmod = recentry->tupdesc->tdtypmod;
1773  return;
1774  }
1775 
1776  /* Not present, so need to manufacture an entry */
1777  recentry->tupdesc = NULL;
1779 
1780  /* Look in the SharedRecordTypmodRegistry, if attached */
1781  entDesc = find_or_make_matching_shared_tupledesc(tupDesc);
1782  if (entDesc == NULL)
1783  {
1784  /* Reference-counted local cache only. */
1785  entDesc = CreateTupleDescCopy(tupDesc);
1786  entDesc->tdrefcount = 1;
1787  entDesc->tdtypmod = NextRecordTypmod++;
1788  }
1790  RecordCacheArray[entDesc->tdtypmod] = entDesc;
1791  recentry->tupdesc = entDesc;
1792 
1793  /* Update the caller's tuple descriptor. */
1794  tupDesc->tdtypmod = entDesc->tdtypmod;
1795 
1796  MemoryContextSwitchTo(oldcxt);
1797 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
Oid tdtypeid
Definition: tupdesc.h:80
#define HASH_ELEM
Definition: hsearch.h:87
static TupleDesc find_or_make_matching_shared_tupledesc(TupleDesc tupdesc)
Definition: typcache.c:2438
struct RecordCacheEntry RecordCacheEntry
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:877
static HTAB * RecordCacheHash
Definition: typcache.c:262
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:904
int32 tdtypmod
Definition: tupdesc.h:81
static TupleDesc * RecordCacheArray
Definition: typcache.c:264
#define RECORDOID
Definition: pg_type.h:680
static int32 NextRecordTypmod
Definition: typcache.c:266
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
static void ensure_record_cache_typmod_slot_exists(int32 typmod)
Definition: typcache.c:1502
Size keysize
Definition: hsearch.h:72
HashCompareFunc match
Definition: hsearch.h:75
struct tupleDesc * TupleDesc
static uint32 record_type_typmod_hash(const void *data, size_t size)
Definition: typcache.c:1711
#define Assert(condition)
Definition: c.h:680
#define HASH_COMPARE
Definition: hsearch.h:90
void CreateCacheMemoryContext(void)
Definition: catcache.c:636
static int record_type_typmod_compare(const void *a, const void *b, size_t size)
Definition: typcache.c:1722
int tdrefcount
Definition: tupdesc.h:83
TupleDesc tupdesc
Definition: typcache.c:148
HashValueFunc hash
Definition: hsearch.h:74
#define HASH_FUNCTION
Definition: hsearch.h:89
MemoryContext CacheMemoryContext
Definition: mcxt.c:46

◆ compare_values_of_enum()

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

Definition at line 2154 of file typcache.c.

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().

2155 {
2156  TypeCacheEnumData *enumdata;
2157  EnumItem *item1;
2158  EnumItem *item2;
2159 
2160  /*
2161  * Equal OIDs are certainly equal --- this case was probably handled by
2162  * our caller, but we may as well check.
2163  */
2164  if (arg1 == arg2)
2165  return 0;
2166 
2167  /* Load up the cache if first time through */
2168  if (tcache->enumData == NULL)
2169  load_enum_cache_data(tcache);
2170  enumdata = tcache->enumData;
2171 
2172  /*
2173  * If both OIDs are known-sorted, we can just compare them directly.
2174  */
2175  if (enum_known_sorted(enumdata, arg1) &&
2176  enum_known_sorted(enumdata, arg2))
2177  {
2178  if (arg1 < arg2)
2179  return -1;
2180  else
2181  return 1;
2182  }
2183 
2184  /*
2185  * Slow path: we have to identify their actual sort-order positions.
2186  */
2187  item1 = find_enumitem(enumdata, arg1);
2188  item2 = find_enumitem(enumdata, arg2);
2189 
2190  if (item1 == NULL || item2 == NULL)
2191  {
2192  /*
2193  * We couldn't find one or both values. That means the enum has
2194  * changed under us, so re-initialize the cache and try again. We
2195  * don't bother retrying the known-sorted case in this path.
2196  */
2197  load_enum_cache_data(tcache);
2198  enumdata = tcache->enumData;
2199 
2200  item1 = find_enumitem(enumdata, arg1);
2201  item2 = find_enumitem(enumdata, arg2);
2202 
2203  /*
2204  * If we still can't find the values, complain: we must have corrupt
2205  * data.
2206  */
2207  if (item1 == NULL)
2208  elog(ERROR, "enum value %u not found in cache for enum %s",
2209  arg1, format_type_be(tcache->type_id));
2210  if (item2 == NULL)
2211  elog(ERROR, "enum value %u not found in cache for enum %s",
2212  arg2, format_type_be(tcache->type_id));
2213  }
2214 
2215  if (item1->sort_order < item2->sort_order)
2216  return -1;
2217  else if (item1->sort_order > item2->sort_order)
2218  return 1;
2219  else
2220  return 0;
2221 }
struct TypeCacheEnumData * enumData
Definition: typcache.h:117
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
#define ERROR
Definition: elog.h:43
static bool enum_known_sorted(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:2125
static EnumItem * find_enumitem(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:2382
static void load_enum_cache_data(TypeCacheEntry *tcache)
Definition: typcache.c:2227
float4 sort_order
Definition: typcache.c:124
#define elog
Definition: elog.h:219

◆ DomainHasConstraints()

bool DomainHasConstraints ( Oid  type_id)

Definition at line 1268 of file typcache.c.

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

Referenced by ATColumnChangeRequiresRewrite(), and ATExecAddColumn().

1269 {
1270  TypeCacheEntry *typentry;
1271 
1272  /*
1273  * Note: a side effect is to cause the typcache's domain data to become
1274  * valid. This is fine since we'll likely need it soon if there is any.
1275  */
1276  typentry = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO);
1277 
1278  return (typentry->domainData != NULL);
1279 }
DomainConstraintCache * domainData
Definition: typcache.h:108
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:311
#define TYPECACHE_DOMAIN_CONSTR_INFO
Definition: typcache.h:137

◆ InitDomainConstraintRef()

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

Definition at line 1181 of file typcache.c.

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().

1183 {
1184  /* Look up the typcache entry --- we assume it survives indefinitely */
1186  ref->need_exprstate = need_exprstate;
1187  /* For safety, establish the callback before acquiring a refcount */
1188  ref->refctx = refctx;
1189  ref->dcc = NULL;
1191  ref->callback.arg = (void *) ref;
1193  /* Acquire refcount if there are constraints, and set up exported list */
1194  if (ref->tcache->domainData)
1195  {
1196  ref->dcc = ref->tcache->domainData;
1197  ref->dcc->dccRefCount++;
1198  if (ref->need_exprstate)
1200  ref->refctx);
1201  else
1202  ref->constraints = ref->dcc->constraints;
1203  }
1204  else
1205  ref->constraints = NIL;
1206 }
MemoryContextCallback callback
Definition: typcache.h:156
MemoryContextCallbackFunction func
Definition: palloc.h:49
#define NIL
Definition: pg_list.h:69
DomainConstraintCache * dcc
Definition: typcache.h:155
static void dccref_deletion_callback(void *arg)
Definition: typcache.c:1122
DomainConstraintCache * domainData
Definition: typcache.h:108
MemoryContext refctx
Definition: typcache.h:150
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:311
static List * prep_domain_constraints(List *constraints, MemoryContext execctx)
Definition: typcache.c:1143
#define TYPECACHE_DOMAIN_CONSTR_INFO
Definition: typcache.h:137
TypeCacheEntry * tcache
Definition: typcache.h:151
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:265

◆ lookup_rowtype_tupdesc()

◆ lookup_rowtype_tupdesc_copy()

TupleDesc lookup_rowtype_tupdesc_copy ( Oid  type_id,
int32  typmod 
)

Definition at line 1652 of file typcache.c.

References CreateTupleDescCopyConstr(), and lookup_rowtype_tupdesc_internal().

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

1653 {
1654  TupleDesc tmp;
1655 
1656  tmp = lookup_rowtype_tupdesc_internal(type_id, typmod, false);
1657  return CreateTupleDescCopyConstr(tmp);
1658 }
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:148
static TupleDesc lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1533

◆ lookup_rowtype_tupdesc_domain()

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

Definition at line 1674 of file typcache.c.

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

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

1675 {
1676  TupleDesc tupDesc;
1677 
1678  if (type_id != RECORDOID)
1679  {
1680  /*
1681  * Check for domain or named composite type. We might as well load
1682  * whichever data is needed.
1683  */
1684  TypeCacheEntry *typentry;
1685 
1686  typentry = lookup_type_cache(type_id,
1689  if (typentry->typtype == TYPTYPE_DOMAIN)
1691  typentry->domainBaseTypmod,
1692  noError);
1693  if (typentry->tupDesc == NULL && !noError)
1694  ereport(ERROR,
1695  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1696  errmsg("type %s is not composite",
1697  format_type_be(type_id))));
1698  tupDesc = typentry->tupDesc;
1699  }
1700  else
1701  tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1702  if (tupDesc != NULL)
1703  PinTupleDesc(tupDesc);
1704  return tupDesc;
1705 }
#define TYPTYPE_DOMAIN
Definition: pg_type.h:722
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
Oid domainBaseType
Definition: typcache.h:101
#define ERROR
Definition: elog.h:43
int32 domainBaseTypmod
Definition: typcache.h:102
#define RECORDOID
Definition: pg_type.h:680
TupleDesc lookup_rowtype_tupdesc_noerror(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1635
#define ereport(elevel, rest)
Definition: elog.h:122
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:136
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:311
char typtype
Definition: typcache.h:41
#define PinTupleDesc(tupdesc)
Definition: tupdesc.h:115
int errmsg(const char *fmt,...)
Definition: elog.c:797
TupleDesc tupDesc
Definition: typcache.h:82
static TupleDesc lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1533
#define TYPECACHE_TUPDESC
Definition: typcache.h:132

◆ lookup_rowtype_tupdesc_noerror()

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

Definition at line 1635 of file typcache.c.

References lookup_rowtype_tupdesc_internal(), and PinTupleDesc.

Referenced by lookup_rowtype_tupdesc_domain().

1636 {
1637  TupleDesc tupDesc;
1638 
1639  tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1640  if (tupDesc != NULL)
1641  PinTupleDesc(tupDesc);
1642  return tupDesc;
1643 }
#define PinTupleDesc(tupdesc)
Definition: tupdesc.h:115
static TupleDesc lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1533

◆ lookup_type_cache()

TypeCacheEntry* lookup_type_cache ( Oid  type_id,
int  flags 
)

Definition at line 311 of file typcache.c.

References array_element_has_compare(), array_element_has_equality(), array_element_has_extended_hashing(), array_element_has_hashing(), ARRAY_EQ_OP, ARRAY_GT_OP, ARRAY_LT_OP, Assert, BTEqualStrategyNumber, BTGreaterStrategyNumber, BTLessStrategyNumber, BTORDER_PROC, BTREE_AM_OID, TypeCacheEntry::btree_opf, TypeCacheEntry::btree_opintype, CacheMemoryContext, CacheRegisterRelcacheCallback(), CacheRegisterSyscacheCallback(), CLAOID, TypeCacheEntry::cmp_proc, TypeCacheEntry::cmp_proc_finfo, CONSTROID, CreateCacheMemoryContext(), TypeCacheEntry::domainBaseType, TypeCacheEntry::domainBaseTypmod, HASHCTL::entrysize, TypeCacheEntry::eq_opr, TypeCacheEntry::eq_opr_finfo, ereport, errcode(), errmsg(), ERROR, firstDomainTypeEntry, TypeCacheEntry::flags, fmgr_info_cxt(), FmgrInfo::fn_oid, get_opclass_family(), get_opclass_input_type(), get_opcode(), get_opfamily_member(), get_opfamily_proc(), getBaseTypeAndTypmod(), GetDefaultOpClass(), GETSTRUCT, TypeCacheEntry::gt_opr, HASH_AM_OID, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, TypeCacheEntry::hash_extended_proc, TypeCacheEntry::hash_extended_proc_finfo, HASH_FIND, TypeCacheEntry::hash_opf, TypeCacheEntry::hash_opintype, TypeCacheEntry::hash_proc, TypeCacheEntry::hash_proc_finfo, hash_search(), HASHEXTENDED_PROC, HASHSTANDARD_PROC, HeapTupleIsValid, HTEqualStrategyNumber, InvalidOid, HASHCTL::keysize, load_domaintype_info(), load_rangetype_info(), load_typcache_tupdesc(), TypeCacheEntry::lt_opr, MemSet, NameStr, TypeCacheEntry::nextDomain, ObjectIdGetDatum, OidIsValid, range_element_has_extended_hashing(), range_element_has_hashing(), RECORD_EQ_OP, record_fields_have_compare(), record_fields_have_equality(), RECORD_GT_OP, RECORD_LT_OP, ReleaseSysCache(), TypeCacheEntry::rngelemtype, 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, TypeCacheEntry::tupDesc, TypeCacheEntry::typalign, TypeCacheEntry::typbyval, TypeCacheEntry::type_id, 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_RANGE_INFO, TYPECACHE_TUPDESC, TypeCacheConstrCallback(), TypeCacheOpcCallback(), TypeCacheRelCallback(), TypeCacheEntry::typelem, TYPEOID, TypeCacheEntry::typlen, TypeCacheEntry::typrelid, TypeCacheEntry::typstorage, TypeCacheEntry::typtype, TYPTYPE_COMPOSITE, TYPTYPE_DOMAIN, and TYPTYPE_RANGE.

Referenced by appendAggOrderBy(), array_cmp(), array_contain_compare(), array_eq(), array_position_common(), array_positions(), array_replace_internal(), array_typanalyze(), brin_inclusion_opcinfo(), brin_minmax_opcinfo(), cache_array_element_properties(), cache_range_element_properties(), cache_record_field_properties(), calc_arraycontsel(), CreateStatistics(), dependency_degree(), domain_state_setup(), DomainHasConstraints(), enum_cmp_internal(), ExecInitExprRec(), foreign_expr_walker(), get_range_io_data(), get_rule_orderby(), get_sort_group_operators(), hash_array(), hash_array_extended(), hash_range(), hash_range_extended(), InitDomainConstraintRef(), initGinState(), load_rangetype_info(), lookup_rowtype_tupdesc_domain(), lookup_rowtype_tupdesc_internal(), ndistinct_for_combination(), op_hashjoinable(), op_mergejoinable(), PLy_input_setup_func(), PLy_output_setup_func(), range_get_typcache(), record_cmp(), record_eq(), refresh_by_match_merge(), scalararraysel(), scalararraysel_containment(), show_sortorder_options(), tuple_equals_slot(), and width_bucket_array().

312 {
313  TypeCacheEntry *typentry;
314  bool found;
315 
316  if (TypeCacheHash == NULL)
317  {
318  /* First time through: initialize the hash table */
319  HASHCTL ctl;
320 
321  MemSet(&ctl, 0, sizeof(ctl));
322  ctl.keysize = sizeof(Oid);
323  ctl.entrysize = sizeof(TypeCacheEntry);
324  TypeCacheHash = hash_create("Type information cache", 64,
325  &ctl, HASH_ELEM | HASH_BLOBS);
326 
327  /* Also set up callbacks for SI invalidations */
332 
333  /* Also make sure CacheMemoryContext exists */
334  if (!CacheMemoryContext)
336  }
337 
338  /* Try to look up an existing entry */
340  (void *) &type_id,
341  HASH_FIND, NULL);
342  if (typentry == NULL)
343  {
344  /*
345  * If we didn't find one, we want to make one. But first look up the
346  * pg_type row, just to make sure we don't make a cache entry for an
347  * invalid type OID. If the type OID is not valid, present a
348  * user-facing error, since some code paths such as domain_in() allow
349  * this function to be reached with a user-supplied OID.
350  */
351  HeapTuple tp;
352  Form_pg_type typtup;
353 
354  tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
355  if (!HeapTupleIsValid(tp))
356  ereport(ERROR,
357  (errcode(ERRCODE_UNDEFINED_OBJECT),
358  errmsg("type with OID %u does not exist", type_id)));
359  typtup = (Form_pg_type) GETSTRUCT(tp);
360  if (!typtup->typisdefined)
361  ereport(ERROR,
362  (errcode(ERRCODE_UNDEFINED_OBJECT),
363  errmsg("type \"%s\" is only a shell",
364  NameStr(typtup->typname))));
365 
366  /* Now make the typcache entry */
368  (void *) &type_id,
369  HASH_ENTER, &found);
370  Assert(!found); /* it wasn't there a moment ago */
371 
372  MemSet(typentry, 0, sizeof(TypeCacheEntry));
373  typentry->type_id = type_id;
374  typentry->typlen = typtup->typlen;
375  typentry->typbyval = typtup->typbyval;
376  typentry->typalign = typtup->typalign;
377  typentry->typstorage = typtup->typstorage;
378  typentry->typtype = typtup->typtype;
379  typentry->typrelid = typtup->typrelid;
380  typentry->typelem = typtup->typelem;
381 
382  /* If it's a domain, immediately thread it into the domain cache list */
383  if (typentry->typtype == TYPTYPE_DOMAIN)
384  {
385  typentry->nextDomain = firstDomainTypeEntry;
386  firstDomainTypeEntry = typentry;
387  }
388 
389  ReleaseSysCache(tp);
390  }
391 
392  /*
393  * Look up opclasses if we haven't already and any dependent info is
394  * requested.
395  */
400  !(typentry->flags & TCFLAGS_CHECKED_BTREE_OPCLASS))
401  {
402  Oid opclass;
403 
404  opclass = GetDefaultOpClass(type_id, BTREE_AM_OID);
405  if (OidIsValid(opclass))
406  {
407  typentry->btree_opf = get_opclass_family(opclass);
408  typentry->btree_opintype = get_opclass_input_type(opclass);
409  }
410  else
411  {
412  typentry->btree_opf = typentry->btree_opintype = InvalidOid;
413  }
414 
415  /*
416  * Reset information derived from btree opclass. Note in particular
417  * that we'll redetermine the eq_opr even if we previously found one;
418  * this matters in case a btree opclass has been added to a type that
419  * previously had only a hash opclass.
420  */
421  typentry->flags &= ~(TCFLAGS_CHECKED_EQ_OPR |
426  }
427 
428  /*
429  * If we need to look up equality operator, and there's no btree opclass,
430  * force lookup of hash opclass.
431  */
432  if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
433  !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR) &&
434  typentry->btree_opf == InvalidOid)
435  flags |= TYPECACHE_HASH_OPFAMILY;
436 
441  !(typentry->flags & TCFLAGS_CHECKED_HASH_OPCLASS))
442  {
443  Oid opclass;
444 
445  opclass = GetDefaultOpClass(type_id, HASH_AM_OID);
446  if (OidIsValid(opclass))
447  {
448  typentry->hash_opf = get_opclass_family(opclass);
449  typentry->hash_opintype = get_opclass_input_type(opclass);
450  }
451  else
452  {
453  typentry->hash_opf = typentry->hash_opintype = InvalidOid;
454  }
455 
456  /*
457  * Reset information derived from hash opclass. We do *not* reset the
458  * eq_opr; if we already found one from the btree opclass, that
459  * decision is still good.
460  */
461  typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
463  typentry->flags |= TCFLAGS_CHECKED_HASH_OPCLASS;
464  }
465 
466  /*
467  * Look for requested operators and functions, if we haven't already.
468  */
469  if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
470  !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR))
471  {
472  Oid eq_opr = InvalidOid;
473 
474  if (typentry->btree_opf != InvalidOid)
475  eq_opr = get_opfamily_member(typentry->btree_opf,
476  typentry->btree_opintype,
477  typentry->btree_opintype,
479  if (eq_opr == InvalidOid &&
480  typentry->hash_opf != InvalidOid)
481  eq_opr = get_opfamily_member(typentry->hash_opf,
482  typentry->hash_opintype,
483  typentry->hash_opintype,
485 
486  /*
487  * If the proposed equality operator is array_eq or record_eq, check
488  * to see if the element type or column types support equality. If
489  * not, array_eq or record_eq would fail at runtime, so we don't want
490  * to report that the type has equality. (We can omit similar
491  * checking for ranges because ranges can't be created in the first
492  * place unless their subtypes support equality.)
493  */
494  if (eq_opr == ARRAY_EQ_OP &&
495  !array_element_has_equality(typentry))
496  eq_opr = InvalidOid;
497  else if (eq_opr == RECORD_EQ_OP &&
498  !record_fields_have_equality(typentry))
499  eq_opr = InvalidOid;
500 
501  /* Force update of eq_opr_finfo only if we're changing state */
502  if (typentry->eq_opr != eq_opr)
503  typentry->eq_opr_finfo.fn_oid = InvalidOid;
504 
505  typentry->eq_opr = eq_opr;
506 
507  /*
508  * Reset info about hash functions whenever we pick up new info about
509  * equality operator. This is so we can ensure that the hash
510  * functions match the operator.
511  */
512  typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
514  typentry->flags |= TCFLAGS_CHECKED_EQ_OPR;
515  }
516  if ((flags & TYPECACHE_LT_OPR) &&
517  !(typentry->flags & TCFLAGS_CHECKED_LT_OPR))
518  {
519  Oid lt_opr = InvalidOid;
520 
521  if (typentry->btree_opf != InvalidOid)
522  lt_opr = get_opfamily_member(typentry->btree_opf,
523  typentry->btree_opintype,
524  typentry->btree_opintype,
526 
527  /*
528  * As above, make sure array_cmp or record_cmp will succeed; but again
529  * we need no special check for ranges.
530  */
531  if (lt_opr == ARRAY_LT_OP &&
532  !array_element_has_compare(typentry))
533  lt_opr = InvalidOid;
534  else if (lt_opr == RECORD_LT_OP &&
535  !record_fields_have_compare(typentry))
536  lt_opr = InvalidOid;
537 
538  typentry->lt_opr = lt_opr;
539  typentry->flags |= TCFLAGS_CHECKED_LT_OPR;
540  }
541  if ((flags & TYPECACHE_GT_OPR) &&
542  !(typentry->flags & TCFLAGS_CHECKED_GT_OPR))
543  {
544  Oid gt_opr = InvalidOid;
545 
546  if (typentry->btree_opf != InvalidOid)
547  gt_opr = get_opfamily_member(typentry->btree_opf,
548  typentry->btree_opintype,
549  typentry->btree_opintype,
551 
552  /*
553  * As above, make sure array_cmp or record_cmp will succeed; but again
554  * we need no special check for ranges.
555  */
556  if (gt_opr == ARRAY_GT_OP &&
557  !array_element_has_compare(typentry))
558  gt_opr = InvalidOid;
559  else if (gt_opr == RECORD_GT_OP &&
560  !record_fields_have_compare(typentry))
561  gt_opr = InvalidOid;
562 
563  typentry->gt_opr = gt_opr;
564  typentry->flags |= TCFLAGS_CHECKED_GT_OPR;
565  }
566  if ((flags & (TYPECACHE_CMP_PROC | TYPECACHE_CMP_PROC_FINFO)) &&
567  !(typentry->flags & TCFLAGS_CHECKED_CMP_PROC))
568  {
569  Oid cmp_proc = InvalidOid;
570 
571  if (typentry->btree_opf != InvalidOid)
572  cmp_proc = get_opfamily_proc(typentry->btree_opf,
573  typentry->btree_opintype,
574  typentry->btree_opintype,
575  BTORDER_PROC);
576 
577  /*
578  * As above, make sure array_cmp or record_cmp will succeed; but again
579  * we need no special check for ranges.
580  */
581  if (cmp_proc == F_BTARRAYCMP &&
582  !array_element_has_compare(typentry))
583  cmp_proc = InvalidOid;
584  else if (cmp_proc == F_BTRECORDCMP &&
585  !record_fields_have_compare(typentry))
586  cmp_proc = InvalidOid;
587 
588  /* Force update of cmp_proc_finfo only if we're changing state */
589  if (typentry->cmp_proc != cmp_proc)
590  typentry->cmp_proc_finfo.fn_oid = InvalidOid;
591 
592  typentry->cmp_proc = cmp_proc;
593  typentry->flags |= TCFLAGS_CHECKED_CMP_PROC;
594  }
596  !(typentry->flags & TCFLAGS_CHECKED_HASH_PROC))
597  {
598  Oid hash_proc = InvalidOid;
599 
600  /*
601  * We insist that the eq_opr, if one has been determined, match the
602  * hash opclass; else report there is no hash function.
603  */
604  if (typentry->hash_opf != InvalidOid &&
605  (!OidIsValid(typentry->eq_opr) ||
606  typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
607  typentry->hash_opintype,
608  typentry->hash_opintype,
610  hash_proc = get_opfamily_proc(typentry->hash_opf,
611  typentry->hash_opintype,
612  typentry->hash_opintype,
614 
615  /*
616  * As above, make sure hash_array will succeed. We don't currently
617  * support hashing for composite types, but when we do, we'll need
618  * more logic here to check that case too.
619  */
620  if (hash_proc == F_HASH_ARRAY &&
621  !array_element_has_hashing(typentry))
622  hash_proc = InvalidOid;
623 
624  /*
625  * Likewise for hash_range.
626  */
627  if (hash_proc == F_HASH_RANGE &&
628  !range_element_has_hashing(typentry))
629  hash_proc = InvalidOid;
630 
631  /* Force update of hash_proc_finfo only if we're changing state */
632  if (typentry->hash_proc != hash_proc)
633  typentry->hash_proc_finfo.fn_oid = InvalidOid;
634 
635  typentry->hash_proc = hash_proc;
636  typentry->flags |= TCFLAGS_CHECKED_HASH_PROC;
637  }
638  if ((flags & (TYPECACHE_HASH_EXTENDED_PROC |
641  {
642  Oid hash_extended_proc = InvalidOid;
643 
644  /*
645  * We insist that the eq_opr, if one has been determined, match the
646  * hash opclass; else report there is no hash function.
647  */
648  if (typentry->hash_opf != InvalidOid &&
649  (!OidIsValid(typentry->eq_opr) ||
650  typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
651  typentry->hash_opintype,
652  typentry->hash_opintype,
654  hash_extended_proc = get_opfamily_proc(typentry->hash_opf,
655  typentry->hash_opintype,
656  typentry->hash_opintype,
658 
659  /*
660  * As above, make sure hash_array_extended will succeed. We don't
661  * currently support hashing for composite types, but when we do,
662  * we'll need more logic here to check that case too.
663  */
664  if (hash_extended_proc == F_HASH_ARRAY_EXTENDED &&
666  hash_extended_proc = InvalidOid;
667 
668  /*
669  * Likewise for hash_range_extended.
670  */
671  if (hash_extended_proc == F_HASH_RANGE_EXTENDED &&
673  hash_extended_proc = InvalidOid;
674 
675  /* Force update of proc finfo only if we're changing state */
676  if (typentry->hash_extended_proc != hash_extended_proc)
678 
679  typentry->hash_extended_proc = hash_extended_proc;
681  }
682 
683  /*
684  * Set up fmgr lookup info as requested
685  *
686  * Note: we tell fmgr the finfo structures live in CacheMemoryContext,
687  * which is not quite right (they're really in the hash table's private
688  * memory context) but this will do for our purposes.
689  *
690  * Note: the code above avoids invalidating the finfo structs unless the
691  * referenced operator/function OID actually changes. This is to prevent
692  * unnecessary leakage of any subsidiary data attached to an finfo, since
693  * that would cause session-lifespan memory leaks.
694  */
695  if ((flags & TYPECACHE_EQ_OPR_FINFO) &&
696  typentry->eq_opr_finfo.fn_oid == InvalidOid &&
697  typentry->eq_opr != InvalidOid)
698  {
699  Oid eq_opr_func;
700 
701  eq_opr_func = get_opcode(typentry->eq_opr);
702  if (eq_opr_func != InvalidOid)
703  fmgr_info_cxt(eq_opr_func, &typentry->eq_opr_finfo,
705  }
706  if ((flags & TYPECACHE_CMP_PROC_FINFO) &&
707  typentry->cmp_proc_finfo.fn_oid == InvalidOid &&
708  typentry->cmp_proc != InvalidOid)
709  {
710  fmgr_info_cxt(typentry->cmp_proc, &typentry->cmp_proc_finfo,
712  }
713  if ((flags & TYPECACHE_HASH_PROC_FINFO) &&
714  typentry->hash_proc_finfo.fn_oid == InvalidOid &&
715  typentry->hash_proc != InvalidOid)
716  {
717  fmgr_info_cxt(typentry->hash_proc, &typentry->hash_proc_finfo,
719  }
720  if ((flags & TYPECACHE_HASH_EXTENDED_PROC_FINFO) &&
722  typentry->hash_extended_proc != InvalidOid)
723  {
725  &typentry->hash_extended_proc_finfo,
727  }
728 
729  /*
730  * If it's a composite type (row type), get tupdesc if requested
731  */
732  if ((flags & TYPECACHE_TUPDESC) &&
733  typentry->tupDesc == NULL &&
734  typentry->typtype == TYPTYPE_COMPOSITE)
735  {
736  load_typcache_tupdesc(typentry);
737  }
738 
739  /*
740  * If requested, get information about a range type
741  */
742  if ((flags & TYPECACHE_RANGE_INFO) &&
743  typentry->rngelemtype == NULL &&
744  typentry->typtype == TYPTYPE_RANGE)
745  {
746  load_rangetype_info(typentry);
747  }
748 
749  /*
750  * If requested, get information about a domain type
751  */
752  if ((flags & TYPECACHE_DOMAIN_BASE_INFO) &&
753  typentry->domainBaseType == InvalidOid &&
754  typentry->typtype == TYPTYPE_DOMAIN)
755  {
756  typentry->domainBaseTypmod = -1;
757  typentry->domainBaseType =
758  getBaseTypeAndTypmod(type_id, &typentry->domainBaseTypmod);
759  }
760  if ((flags & TYPECACHE_DOMAIN_CONSTR_INFO) &&
761  (typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
762  typentry->typtype == TYPTYPE_DOMAIN)
763  {
764  load_domaintype_info(typentry);
765  }
766 
767  return typentry;
768 }
static bool array_element_has_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1311
#define TYPTYPE_DOMAIN
Definition: pg_type.h:722
static void load_typcache_tupdesc(TypeCacheEntry *typentry)
Definition: typcache.c:774
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2307
#define BTORDER_PROC
Definition: nbtree.h:229
#define TYPECACHE_RANGE_INFO
Definition: typcache.h:135
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
Definition: indexcmds.c:1410
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
Oid hash_opintype
Definition: typcache.h:56
#define TCFLAGS_CHECKED_EQ_OPR
Definition: typcache.c:84
#define HASH_ELEM
Definition: hsearch.h:87
static TypeCacheEntry * firstDomainTypeEntry
Definition: typcache.c:79
#define TYPECACHE_HASH_EXTENDED_PROC_FINFO
Definition: typcache.h:139
#define TCFLAGS_CHECKED_HASH_EXTENDED_PROC
Definition: typcache.c:89
#define TYPTYPE_COMPOSITE
Definition: pg_type.h:721
struct TypeCacheEntry TypeCacheEntry
#define HTEqualStrategyNumber
Definition: hash.h:336
#define TYPECACHE_EQ_OPR_FINFO
Definition: typcache.h:129
#define BTREE_AM_OID
Definition: pg_am.h:70
#define TYPECACHE_HASH_PROC_FINFO
Definition: typcache.h:131
Size entrysize
Definition: hsearch.h:73
#define TYPECACHE_EQ_OPR
Definition: typcache.h:124
int errcode(int sqlerrcode)
Definition: elog.c:575
#define HASHEXTENDED_PROC
Definition: hash.h:352
#define MemSet(start, val, len)
Definition: c.h:877
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:904
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
#define RECORD_EQ_OP
Definition: pg_operator.h:1720
int16 typlen
Definition: typcache.h:37
#define OidIsValid(objectId)
Definition: c.h:586
bool typbyval
Definition: typcache.h:38
#define HASH_AM_OID
Definition: pg_am.h:73
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1431
Oid domainBaseType
Definition: typcache.h:101
FmgrInfo cmp_proc_finfo
Definition: typcache.h:72
struct TypeCacheEntry * nextDomain
Definition: typcache.h:120
#define TCFLAGS_CHECKED_GT_OPR
Definition: typcache.c:86
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ARRAY_LT_OP
Definition: pg_operator.h:781
char typstorage
Definition: typcache.h:40
static void TypeCacheRelCallback(Datum arg, Oid relid)
Definition: typcache.c:2009
static bool array_element_has_compare(TypeCacheEntry *typentry)
Definition: typcache.c:1303
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
int32 domainBaseTypmod
Definition: typcache.h:102
Oid hash_extended_proc
Definition: typcache.h:62
FmgrInfo hash_proc_finfo
Definition: typcache.h:73
#define RECORD_LT_OP
Definition: pg_operator.h:1725
#define TYPECACHE_GT_OPR
Definition: typcache.h:126
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:132
#define HASHSTANDARD_PROC
Definition: hash.h:351
#define ereport(elevel, rest)
Definition: elog.h:122
#define TYPECACHE_BTREE_OPFAMILY
Definition: typcache.h:133
struct TypeCacheEntry * rngelemtype
Definition: typcache.h:91
#define RECORD_GT_OP
Definition: pg_operator.h:1728
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:136
#define TYPTYPE_RANGE
Definition: pg_type.h:725
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
static bool range_element_has_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1459
#define HASH_BLOBS
Definition: hsearch.h:88
FmgrInfo hash_extended_proc_finfo
Definition: typcache.h:74
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
#define TYPECACHE_HASH_EXTENDED_PROC
Definition: typcache.h:138
uintptr_t Datum
Definition: postgres.h:372
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Oid btree_opintype
Definition: typcache.h:54
Size keysize
Definition: hsearch.h:72
FmgrInfo eq_opr_finfo
Definition: typcache.h:71
#define InvalidOid
Definition: postgres_ext.h:36
static void load_rangetype_info(TypeCacheEntry *typentry)
Definition: typcache.c:808
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1094
Oid fn_oid
Definition: fmgr.h:59
#define TYPECACHE_CMP_PROC
Definition: typcache.h:127
char typtype
Definition: typcache.h:41
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:680
#define TYPECACHE_DOMAIN_CONSTR_INFO
Definition: typcache.h:137
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
#define ARRAY_GT_OP
Definition: pg_operator.h:784
void CreateCacheMemoryContext(void)
Definition: catcache.c:636
#define TCFLAGS_CHECKED_BTREE_OPCLASS
Definition: typcache.c:82
#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS
Definition: typcache.c:98
static bool record_fields_have_compare(TypeCacheEntry *typentry)
Definition: typcache.c:1365
#define ARRAY_EQ_OP
Definition: pg_operator.h:776
Oid get_opclass_family(Oid opclass)
Definition: lsyscache.c:1047
static bool array_element_has_equality(TypeCacheEntry *typentry)
Definition: typcache.c:1295
static void load_domaintype_info(TypeCacheEntry *typentry)
Definition: typcache.c:871
#define TCFLAGS_CHECKED_HASH_PROC
Definition: typcache.c:88
char typalign
Definition: typcache.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TYPECACHE_LT_OPR
Definition: typcache.h:125
#define TCFLAGS_CHECKED_LT_OPR
Definition: typcache.c:85
#define NameStr(name)
Definition: c.h:557
#define TYPECACHE_CMP_PROC_FINFO
Definition: typcache.h:130
#define TCFLAGS_CHECKED_HASH_OPCLASS
Definition: typcache.c:83
static bool range_element_has_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1467
#define TYPECACHE_HASH_OPFAMILY
Definition: typcache.h:134
static bool record_fields_have_equality(TypeCacheEntry *typentry)
Definition: typcache.c:1357
#define TCFLAGS_CHECKED_CMP_PROC
Definition: typcache.c:87
#define BTLessStrategyNumber
Definition: stratnum.h:29
static bool array_element_has_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1319
TupleDesc tupDesc
Definition: typcache.h:82
static void TypeCacheConstrCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:2101
static HTAB * TypeCacheHash
Definition: typcache.c:76
#define TYPECACHE_HASH_PROC
Definition: typcache.h:128
#define TYPECACHE_TUPDESC
Definition: typcache.h:132
#define BTEqualStrategyNumber
Definition: stratnum.h:31
static void TypeCacheOpcCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:2072
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1069

◆ SharedRecordTypmodRegistryAttach()

void SharedRecordTypmodRegistryAttach ( SharedRecordTypmodRegistry )

Definition at line 1926 of file typcache.c.

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, TopMemoryContext, and SharedRecordTypmodRegistry::typmod_table_handle.

Referenced by AttachSession().

1927 {
1928  MemoryContext old_context;
1929  dshash_table *record_table;
1930  dshash_table *typmod_table;
1931 
1933 
1934  /* We can't already be attached to a shared registry. */
1935  Assert(CurrentSession != NULL);
1936  Assert(CurrentSession->segment != NULL);
1937  Assert(CurrentSession->area != NULL);
1941 
1942  /*
1943  * We can't already have typmods in our local cache, because they'd clash
1944  * with those imported by SharedRecordTypmodRegistryInit. This should be
1945  * a freshly started parallel worker. If we ever support worker
1946  * recycling, a worker would need to zap its local cache in between
1947  * servicing different queries, in order to be able to call this and
1948  * synchronize typmods with a new leader; but that's problematic because
1949  * we can't be very sure that record-typmod-related state hasn't escaped
1950  * to anywhere else in the process.
1951  */
1952  Assert(NextRecordTypmod == 0);
1953 
1954  old_context = MemoryContextSwitchTo(TopMemoryContext);
1955 
1956  /* Attach to the two hash tables. */
1957  record_table = dshash_attach(CurrentSession->area,
1959  registry->record_table_handle,
1960  CurrentSession->area);
1961  typmod_table = dshash_attach(CurrentSession->area,
1963  registry->typmod_table_handle,
1964  NULL);
1965 
1966  MemoryContextSwitchTo(old_context);
1967 
1968  /*
1969  * Set up detach hook to run at worker exit. Currently this is the same
1970  * as the leader's detach hook, but in future they might need to be
1971  * different.
1972  */
1975  PointerGetDatum(registry));
1976 
1977  /*
1978  * Set up the session state that will tell assign_record_type_typmod and
1979  * lookup_rowtype_tupdesc_internal about the shared registry.
1980  */
1982  CurrentSession->shared_record_table = record_table;
1983  CurrentSession->shared_typmod_table = typmod_table;
1984 }
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:263
#define PointerGetDatum(X)
Definition: postgres.h:562
Session * CurrentSession
Definition: session.c:48
static const dshash_parameters srtr_record_table_params
Definition: typcache.c:245
dshash_table * shared_record_table
Definition: session.h:32
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1024
dsa_area * area
Definition: session.h:28
#define IsParallelWorker()
Definition: parallel.h:58
MemoryContext TopMemoryContext
Definition: mcxt.c:43
static const dshash_parameters srtr_typmod_table_params
Definition: typcache.c:254
static void shared_record_typmod_registry_detach(dsm_segment *segment, Datum datum)
Definition: typcache.c:2550
static int32 NextRecordTypmod
Definition: typcache.c:266
struct SharedRecordTypmodRegistry * shared_typmod_registry
Definition: session.h:31
dshash_table * shared_typmod_table
Definition: session.h:33
#define Assert(condition)
Definition: c.h:680
dsm_segment * segment
Definition: session.h:27

◆ SharedRecordTypmodRegistryEstimate()

size_t SharedRecordTypmodRegistryEstimate ( void  )

Definition at line 1805 of file typcache.c.

Referenced by GetSessionDsmHandle().

1806 {
1807  return sizeof(SharedRecordTypmodRegistry);
1808 }
struct SharedRecordTypmodRegistry SharedRecordTypmodRegistry
Definition: typcache.h:159

◆ SharedRecordTypmodRegistryInit()

void SharedRecordTypmodRegistryInit ( SharedRecordTypmodRegistry ,
dsm_segment segment,
dsa_area area 
)

Definition at line 1827 of file typcache.c.

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, 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, tupleDesc::tdtypmod, TopMemoryContext, SharedTypmodTableEntry::typmod, SharedRecordTypmodRegistry::typmod_table_handle, and SharedRecordTableKey::u.

Referenced by GetSessionDsmHandle().

1830 {
1831  MemoryContext old_context;
1832  dshash_table *record_table;
1833  dshash_table *typmod_table;
1834  int32 typmod;
1835 
1837 
1838  /* We can't already be attached to a shared registry. */
1842 
1843  old_context = MemoryContextSwitchTo(TopMemoryContext);
1844 
1845  /* Create the hash table of tuple descriptors indexed by themselves. */
1846  record_table = dshash_create(area, &srtr_record_table_params, area);
1847 
1848  /* Create the hash table of tuple descriptors indexed by typmod. */
1849  typmod_table = dshash_create(area, &srtr_typmod_table_params, NULL);
1850 
1851  MemoryContextSwitchTo(old_context);
1852 
1853  /* Initialize the SharedRecordTypmodRegistry. */
1854  registry->record_table_handle = dshash_get_hash_table_handle(record_table);
1855  registry->typmod_table_handle = dshash_get_hash_table_handle(typmod_table);
1856  pg_atomic_init_u32(&registry->next_typmod, NextRecordTypmod);
1857 
1858  /*
1859  * Copy all entries from this backend's private registry into the shared
1860  * registry.
1861  */
1862  for (typmod = 0; typmod < NextRecordTypmod; ++typmod)
1863  {
1864  SharedTypmodTableEntry *typmod_table_entry;
1865  SharedRecordTableEntry *record_table_entry;
1866  SharedRecordTableKey record_table_key;
1867  dsa_pointer shared_dp;
1868  TupleDesc tupdesc;
1869  bool found;
1870 
1871  tupdesc = RecordCacheArray[typmod];
1872  if (tupdesc == NULL)
1873  continue;
1874 
1875  /* Copy the TupleDesc into shared memory. */
1876  shared_dp = share_tupledesc(area, tupdesc, typmod);
1877 
1878  /* Insert into the typmod table. */
1879  typmod_table_entry = dshash_find_or_insert(typmod_table,
1880  &tupdesc->tdtypmod,
1881  &found);
1882  if (found)
1883  elog(ERROR, "cannot create duplicate shared record typmod");
1884  typmod_table_entry->typmod = tupdesc->tdtypmod;
1885  typmod_table_entry->shared_tupdesc = shared_dp;
1886  dshash_release_lock(typmod_table, typmod_table_entry);
1887 
1888  /* Insert into the record table. */
1889  record_table_key.shared = false;
1890  record_table_key.u.local_tupdesc = tupdesc;
1891  record_table_entry = dshash_find_or_insert(record_table,
1892  &record_table_key,
1893  &found);
1894  if (!found)
1895  {
1896  record_table_entry->key.shared = true;
1897  record_table_entry->key.u.shared_tupdesc = shared_dp;
1898  }
1899  dshash_release_lock(record_table, record_table_entry);
1900  }
1901 
1902  /*
1903  * Set up the global state that will tell assign_record_type_typmod and
1904  * lookup_rowtype_tupdesc_internal about the shared registry.
1905  */
1906  CurrentSession->shared_record_table = record_table;
1907  CurrentSession->shared_typmod_table = typmod_table;
1909 
1910  /*
1911  * We install a detach hook in the leader, but only to handle cleanup on
1912  * failure during GetSessionDsmHandle(). Once GetSessionDsmHandle() pins
1913  * the memory, the leader process will use a shared registry until it
1914  * exits.
1915  */
1917 }
Session * CurrentSession
Definition: session.c:48
static const dshash_parameters srtr_record_table_params
Definition: typcache.c:245
dshash_table * shared_record_table
Definition: session.h:32
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SharedRecordTableKey key
Definition: typcache.c:187
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1024
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:561
uint64 dsa_pointer
Definition: dsa.h:62
int32 tdtypmod
Definition: tupdesc.h:81
signed int int32
Definition: c.h:294
static TupleDesc * RecordCacheArray
Definition: typcache.c:264
dsa_pointer shared_tupdesc
Definition: typcache.c:197
#define ERROR
Definition: elog.h:43
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:362
dsa_pointer shared_tupdesc
Definition: typcache.c:176
#define IsParallelWorker()
Definition: parallel.h:58
MemoryContext TopMemoryContext
Definition: mcxt.c:43
static const dshash_parameters srtr_typmod_table_params
Definition: typcache.c:254
static void shared_record_typmod_registry_detach(dsm_segment *segment, Datum datum)
Definition: typcache.c:2550
static int32 NextRecordTypmod
Definition: typcache.c:266
struct SharedRecordTypmodRegistry * shared_typmod_registry
Definition: session.h:31
uintptr_t Datum
Definition: postgres.h:372
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:196
dshash_table * shared_typmod_table
Definition: session.h:33
union SharedRecordTableKey::@36 u
#define Assert(condition)
Definition: c.h:680
static dsa_pointer share_tupledesc(dsa_area *area, TupleDesc tupdesc, uint32 typmod)
Definition: typcache.c:2417
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:430
TupleDesc local_tupdesc
Definition: typcache.c:175
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:234
#define elog
Definition: elog.h:219

◆ UpdateDomainConstraintRef()

void UpdateDomainConstraintRef ( DomainConstraintRef ref)

Definition at line 1219 of file typcache.c.

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, TypeCacheEntry::typtype, and TYPTYPE_DOMAIN.

Referenced by domain_check_input().

1220 {
1221  TypeCacheEntry *typentry = ref->tcache;
1222 
1223  /* Make sure typcache entry's data is up to date */
1224  if ((typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
1225  typentry->typtype == TYPTYPE_DOMAIN)
1226  load_domaintype_info(typentry);
1227 
1228  /* Transfer to ref object if there's new info, adjusting refcounts */
1229  if (ref->dcc != typentry->domainData)
1230  {
1231  /* Paranoia --- be sure link is nulled before trying to release */
1232  DomainConstraintCache *dcc = ref->dcc;
1233 
1234  if (dcc)
1235  {
1236  /*
1237  * Note: we just leak the previous list of executable domain
1238  * constraints. Alternatively, we could keep those in a child
1239  * context of ref->refctx and free that context at this point.
1240  * However, in practice this code path will be taken so seldom
1241  * that the extra bookkeeping for a child context doesn't seem
1242  * worthwhile; we'll just allow a leak for the lifespan of refctx.
1243  */
1244  ref->constraints = NIL;
1245  ref->dcc = NULL;
1246  decr_dcc_refcount(dcc);
1247  }
1248  dcc = typentry->domainData;
1249  if (dcc)
1250  {
1251  ref->dcc = dcc;
1252  dcc->dccRefCount++;
1253  if (ref->need_exprstate)
1255  ref->refctx);
1256  else
1257  ref->constraints = dcc->constraints;
1258  }
1259  }
1260 }
#define NIL
Definition: pg_list.h:69
#define TYPTYPE_DOMAIN
Definition: pg_type.h:722
DomainConstraintCache * dcc
Definition: typcache.h:155
DomainConstraintCache * domainData
Definition: typcache.h:108
MemoryContext refctx
Definition: typcache.h:150
char typtype
Definition: typcache.h:41
static List * prep_domain_constraints(List *constraints, MemoryContext execctx)
Definition: typcache.c:1143
TypeCacheEntry * tcache
Definition: typcache.h:151
#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS
Definition: typcache.c:98
static void load_domaintype_info(TypeCacheEntry *typentry)
Definition: typcache.c:871
static void decr_dcc_refcount(DomainConstraintCache *dcc)
Definition: typcache.c:1111