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

Macro Definition Documentation

◆ INVALID_TUPLEDESC_IDENTIFIER

#define INVALID_TUPLEDESC_IDENTIFIER   ((uint64) 1)

◆ TYPECACHE_BTREE_OPFAMILY

#define TYPECACHE_BTREE_OPFAMILY   0x0200

Definition at line 136 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 140 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 142 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 137 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 134 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 138 of file typcache.h.

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

◆ TYPECACHE_TUPDESC

Typedef Documentation

◆ DomainConstraintCache

Definition at line 26 of file typcache.h.

◆ DomainConstraintRef

◆ SharedRecordTypmodRegistry

◆ TypeCacheEntry

Function Documentation

◆ assign_record_type_identifier()

uint64 assign_record_type_identifier ( Oid  type_id,
int32  typmod 
)

Definition at line 1836 of file typcache.c.

References Assert, ereport, errcode(), errmsg(), ERROR, format_type_be(), lookup_type_cache(), RecordCacheArrayLen, RecordIdentifierArray, 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().

1837 {
1838  if (type_id != RECORDOID)
1839  {
1840  /*
1841  * It's a named composite type, so use the regular typcache.
1842  */
1843  TypeCacheEntry *typentry;
1844 
1845  typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
1846  if (typentry->tupDesc == NULL)
1847  ereport(ERROR,
1848  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1849  errmsg("type %s is not composite",
1850  format_type_be(type_id))));
1851  Assert(typentry->tupDesc_identifier != 0);
1852  return typentry->tupDesc_identifier;
1853  }
1854  else
1855  {
1856  /*
1857  * It's a transient record type, so look in our record-type table.
1858  */
1859  if (typmod >= 0 && typmod < RecordCacheArrayLen &&
1860  RecordCacheArray[typmod] != NULL)
1861  {
1862  Assert(RecordIdentifierArray[typmod] != 0);
1863  return RecordIdentifierArray[typmod];
1864  }
1865 
1866  /* For anonymous or unrecognized record type, generate a new ID */
1867  return ++tupledesc_id_counter;
1868  }
1869 }
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
static uint64 tupledesc_id_counter
Definition: typcache.c:276
static TupleDesc * RecordCacheArray
Definition: typcache.c:266
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static int32 RecordCacheArrayLen
Definition: typcache.c:268
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:321
#define Assert(condition)
Definition: c.h:699
uint64 tupDesc_identifier
Definition: typcache.h:86
int errmsg(const char *fmt,...)
Definition: elog.c:797
TupleDesc tupDesc
Definition: typcache.h:85
#define TYPECACHE_TUPDESC
Definition: typcache.h:135
static uint64 * RecordIdentifierArray
Definition: typcache.c:267

◆ assign_record_type_typmod()

void assign_record_type_typmod ( TupleDesc  tupDesc)

Definition at line 1761 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(), RecordIdentifierArray, tupleDesc::tdrefcount, tupleDesc::tdtypeid, tupleDesc::tdtypmod, RecordCacheEntry::tupdesc, and tupledesc_id_counter.

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

1762 {
1763  RecordCacheEntry *recentry;
1764  TupleDesc entDesc;
1765  bool found;
1766  MemoryContext oldcxt;
1767 
1768  Assert(tupDesc->tdtypeid == RECORDOID);
1769 
1770  if (RecordCacheHash == NULL)
1771  {
1772  /* First time through: initialize the hash table */
1773  HASHCTL ctl;
1774 
1775  MemSet(&ctl, 0, sizeof(ctl));
1776  ctl.keysize = sizeof(TupleDesc); /* just the pointer */
1777  ctl.entrysize = sizeof(RecordCacheEntry);
1780  RecordCacheHash = hash_create("Record information cache", 64,
1781  &ctl,
1783 
1784  /* Also make sure CacheMemoryContext exists */
1785  if (!CacheMemoryContext)
1787  }
1788 
1789  /* Find or create a hashtable entry for this tuple descriptor */
1791  (void *) &tupDesc,
1792  HASH_ENTER, &found);
1793  if (found && recentry->tupdesc != NULL)
1794  {
1795  tupDesc->tdtypmod = recentry->tupdesc->tdtypmod;
1796  return;
1797  }
1798 
1799  /* Not present, so need to manufacture an entry */
1800  recentry->tupdesc = NULL;
1802 
1803  /* Look in the SharedRecordTypmodRegistry, if attached */
1804  entDesc = find_or_make_matching_shared_tupledesc(tupDesc);
1805  if (entDesc == NULL)
1806  {
1807  /* Reference-counted local cache only. */
1808  entDesc = CreateTupleDescCopy(tupDesc);
1809  entDesc->tdrefcount = 1;
1810  entDesc->tdtypmod = NextRecordTypmod++;
1811  }
1813  RecordCacheArray[entDesc->tdtypmod] = entDesc;
1814  recentry->tupdesc = entDesc;
1815 
1816  /* Assign a unique tupdesc identifier, too. */
1818 
1819  /* Update the caller's tuple descriptor. */
1820  tupDesc->tdtypmod = entDesc->tdtypmod;
1821 
1822  MemoryContextSwitchTo(oldcxt);
1823 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:112
Oid tdtypeid
Definition: tupdesc.h:83
#define HASH_ELEM
Definition: hsearch.h:87
static TupleDesc find_or_make_matching_shared_tupledesc(TupleDesc tupdesc)
Definition: typcache.c:2510
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:908
static uint64 tupledesc_id_counter
Definition: typcache.c:276
static HTAB * RecordCacheHash
Definition: typcache.c:263
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
int32 tdtypmod
Definition: tupdesc.h:84
static TupleDesc * RecordCacheArray
Definition: typcache.c:266
static int32 NextRecordTypmod
Definition: typcache.c:269
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:1513
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:1734
#define Assert(condition)
Definition: c.h:699
#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:1745
int tdrefcount
Definition: tupdesc.h:86
TupleDesc tupdesc
Definition: typcache.c:148
static uint64 * RecordIdentifierArray
Definition: typcache.c:267
HashValueFunc hash
Definition: hsearch.h:74
#define HASH_FUNCTION
Definition: hsearch.h:89
MemoryContext CacheMemoryContext
Definition: mcxt.c:47

◆ compare_values_of_enum()

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

Definition at line 2226 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().

2227 {
2228  TypeCacheEnumData *enumdata;
2229  EnumItem *item1;
2230  EnumItem *item2;
2231 
2232  /*
2233  * Equal OIDs are certainly equal --- this case was probably handled by
2234  * our caller, but we may as well check.
2235  */
2236  if (arg1 == arg2)
2237  return 0;
2238 
2239  /* Load up the cache if first time through */
2240  if (tcache->enumData == NULL)
2241  load_enum_cache_data(tcache);
2242  enumdata = tcache->enumData;
2243 
2244  /*
2245  * If both OIDs are known-sorted, we can just compare them directly.
2246  */
2247  if (enum_known_sorted(enumdata, arg1) &&
2248  enum_known_sorted(enumdata, arg2))
2249  {
2250  if (arg1 < arg2)
2251  return -1;
2252  else
2253  return 1;
2254  }
2255 
2256  /*
2257  * Slow path: we have to identify their actual sort-order positions.
2258  */
2259  item1 = find_enumitem(enumdata, arg1);
2260  item2 = find_enumitem(enumdata, arg2);
2261 
2262  if (item1 == NULL || item2 == NULL)
2263  {
2264  /*
2265  * We couldn't find one or both values. That means the enum has
2266  * changed under us, so re-initialize the cache and try again. We
2267  * don't bother retrying the known-sorted case in this path.
2268  */
2269  load_enum_cache_data(tcache);
2270  enumdata = tcache->enumData;
2271 
2272  item1 = find_enumitem(enumdata, arg1);
2273  item2 = find_enumitem(enumdata, arg2);
2274 
2275  /*
2276  * If we still can't find the values, complain: we must have corrupt
2277  * data.
2278  */
2279  if (item1 == NULL)
2280  elog(ERROR, "enum value %u not found in cache for enum %s",
2281  arg1, format_type_be(tcache->type_id));
2282  if (item2 == NULL)
2283  elog(ERROR, "enum value %u not found in cache for enum %s",
2284  arg2, format_type_be(tcache->type_id));
2285  }
2286 
2287  if (item1->sort_order < item2->sort_order)
2288  return -1;
2289  else if (item1->sort_order > item2->sort_order)
2290  return 1;
2291  else
2292  return 0;
2293 }
struct TypeCacheEnumData * enumData
Definition: typcache.h:120
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
#define ERROR
Definition: elog.h:43
static bool enum_known_sorted(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:2197
static EnumItem * find_enumitem(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:2454
static void load_enum_cache_data(TypeCacheEntry *tcache)
Definition: typcache.c:2299
float4 sort_order
Definition: typcache.c:124
#define elog
Definition: elog.h:219

◆ DomainHasConstraints()

bool DomainHasConstraints ( Oid  type_id)

Definition at line 1278 of file typcache.c.

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

Referenced by ATColumnChangeRequiresRewrite(), and ATExecAddColumn().

1279 {
1280  TypeCacheEntry *typentry;
1281 
1282  /*
1283  * Note: a side effect is to cause the typcache's domain data to become
1284  * valid. This is fine since we'll likely need it soon if there is any.
1285  */
1286  typentry = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO);
1287 
1288  return (typentry->domainData != NULL);
1289 }
DomainConstraintCache * domainData
Definition: typcache.h:111
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:321
#define TYPECACHE_DOMAIN_CONSTR_INFO
Definition: typcache.h:140

◆ InitDomainConstraintRef()

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

Definition at line 1191 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().

1193 {
1194  /* Look up the typcache entry --- we assume it survives indefinitely */
1196  ref->need_exprstate = need_exprstate;
1197  /* For safety, establish the callback before acquiring a refcount */
1198  ref->refctx = refctx;
1199  ref->dcc = NULL;
1201  ref->callback.arg = (void *) ref;
1203  /* Acquire refcount if there are constraints, and set up exported list */
1204  if (ref->tcache->domainData)
1205  {
1206  ref->dcc = ref->tcache->domainData;
1207  ref->dcc->dccRefCount++;
1208  if (ref->need_exprstate)
1210  ref->refctx);
1211  else
1212  ref->constraints = ref->dcc->constraints;
1213  }
1214  else
1215  ref->constraints = NIL;
1216 }
MemoryContextCallback callback
Definition: typcache.h:162
MemoryContextCallbackFunction func
Definition: palloc.h:49
#define NIL
Definition: pg_list.h:69
DomainConstraintCache * dcc
Definition: typcache.h:161
static void dccref_deletion_callback(void *arg)
Definition: typcache.c:1132
DomainConstraintCache * domainData
Definition: typcache.h:111
MemoryContext refctx
Definition: typcache.h:156
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:321
static List * prep_domain_constraints(List *constraints, MemoryContext execctx)
Definition: typcache.c:1153
#define TYPECACHE_DOMAIN_CONSTR_INFO
Definition: typcache.h:140
TypeCacheEntry * tcache
Definition: typcache.h:157
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:285

◆ lookup_rowtype_tupdesc()

◆ lookup_rowtype_tupdesc_copy()

TupleDesc lookup_rowtype_tupdesc_copy ( Oid  type_id,
int32  typmod 
)

Definition at line 1675 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().

1676 {
1677  TupleDesc tmp;
1678 
1679  tmp = lookup_rowtype_tupdesc_internal(type_id, typmod, false);
1680  return CreateTupleDescCopyConstr(tmp);
1681 }
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:151
static TupleDesc lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1550

◆ lookup_rowtype_tupdesc_domain()

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

Definition at line 1697 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, 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().

1698 {
1699  TupleDesc tupDesc;
1700 
1701  if (type_id != RECORDOID)
1702  {
1703  /*
1704  * Check for domain or named composite type. We might as well load
1705  * whichever data is needed.
1706  */
1707  TypeCacheEntry *typentry;
1708 
1709  typentry = lookup_type_cache(type_id,
1712  if (typentry->typtype == TYPTYPE_DOMAIN)
1714  typentry->domainBaseTypmod,
1715  noError);
1716  if (typentry->tupDesc == NULL && !noError)
1717  ereport(ERROR,
1718  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1719  errmsg("type %s is not composite",
1720  format_type_be(type_id))));
1721  tupDesc = typentry->tupDesc;
1722  }
1723  else
1724  tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1725  if (tupDesc != NULL)
1726  PinTupleDesc(tupDesc);
1727  return tupDesc;
1728 }
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
Oid domainBaseType
Definition: typcache.h:104
#define ERROR
Definition: elog.h:43
int32 domainBaseTypmod
Definition: typcache.h:105
TupleDesc lookup_rowtype_tupdesc_noerror(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1658
#define ereport(elevel, rest)
Definition: elog.h:122
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:139
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:321
char typtype
Definition: typcache.h:41
#define PinTupleDesc(tupdesc)
Definition: tupdesc.h:118
int errmsg(const char *fmt,...)
Definition: elog.c:797
TupleDesc tupDesc
Definition: typcache.h:85
static TupleDesc lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1550
#define TYPECACHE_TUPDESC
Definition: typcache.h:135

◆ lookup_rowtype_tupdesc_noerror()

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

Definition at line 1658 of file typcache.c.

References lookup_rowtype_tupdesc_internal(), and PinTupleDesc.

Referenced by lookup_rowtype_tupdesc_domain().

1659 {
1660  TupleDesc tupDesc;
1661 
1662  tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1663  if (tupDesc != NULL)
1664  PinTupleDesc(tupDesc);
1665  return tupDesc;
1666 }
#define PinTupleDesc(tupdesc)
Definition: tupdesc.h:118
static TupleDesc lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1550

◆ lookup_type_cache()

TypeCacheEntry* lookup_type_cache ( Oid  type_id,
int  flags 
)

Definition at line 321 of file typcache.c.

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(), 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_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_fields_have_compare(), record_fields_have_equality(), 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, and TypeCacheEntry::typtype.

Referenced by appendAggOrderBy(), array_cmp(), array_contain_compare(), array_eq(), array_position_common(), array_positions(), array_replace_internal(), array_typanalyze(), assign_record_type_identifier(), 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(), make_expanded_record_from_tupdesc(), make_expanded_record_from_typeid(), ndistinct_for_combination(), op_hashjoinable(), op_mergejoinable(), PLy_input_setup_func(), PLy_output_setup_func(), range_get_typcache(), record_cmp(), record_eq(), scalararraysel(), scalararraysel_containment(), show_sortorder_options(), tuple_equals_slot(), and width_bucket_array().

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

◆ SharedRecordTypmodRegistryAttach()

void SharedRecordTypmodRegistryAttach ( SharedRecordTypmodRegistry )

Definition at line 1998 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().

1999 {
2000  MemoryContext old_context;
2001  dshash_table *record_table;
2002  dshash_table *typmod_table;
2003 
2005 
2006  /* We can't already be attached to a shared registry. */
2007  Assert(CurrentSession != NULL);
2008  Assert(CurrentSession->segment != NULL);
2009  Assert(CurrentSession->area != NULL);
2013 
2014  /*
2015  * We can't already have typmods in our local cache, because they'd clash
2016  * with those imported by SharedRecordTypmodRegistryInit. This should be
2017  * a freshly started parallel worker. If we ever support worker
2018  * recycling, a worker would need to zap its local cache in between
2019  * servicing different queries, in order to be able to call this and
2020  * synchronize typmods with a new leader; but that's problematic because
2021  * we can't be very sure that record-typmod-related state hasn't escaped
2022  * to anywhere else in the process.
2023  */
2024  Assert(NextRecordTypmod == 0);
2025 
2026  old_context = MemoryContextSwitchTo(TopMemoryContext);
2027 
2028  /* Attach to the two hash tables. */
2029  record_table = dshash_attach(CurrentSession->area,
2031  registry->record_table_handle,
2032  CurrentSession->area);
2033  typmod_table = dshash_attach(CurrentSession->area,
2035  registry->typmod_table_handle,
2036  NULL);
2037 
2038  MemoryContextSwitchTo(old_context);
2039 
2040  /*
2041  * Set up detach hook to run at worker exit. Currently this is the same
2042  * as the leader's detach hook, but in future they might need to be
2043  * different.
2044  */
2047  PointerGetDatum(registry));
2048 
2049  /*
2050  * Set up the session state that will tell assign_record_type_typmod and
2051  * lookup_rowtype_tupdesc_internal about the shared registry.
2052  */
2054  CurrentSession->shared_record_table = record_table;
2055  CurrentSession->shared_typmod_table = typmod_table;
2056 }
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:541
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:60
MemoryContext TopMemoryContext
Definition: mcxt.c:44
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:2622
static int32 NextRecordTypmod
Definition: typcache.c:269
struct SharedRecordTypmodRegistry * shared_typmod_registry
Definition: session.h:31
dshash_table * shared_typmod_table
Definition: session.h:33
#define Assert(condition)
Definition: c.h:699
dsm_segment * segment
Definition: session.h:27

◆ SharedRecordTypmodRegistryEstimate()

size_t SharedRecordTypmodRegistryEstimate ( void  )

Definition at line 1877 of file typcache.c.

Referenced by GetSessionDsmHandle().

1878 {
1879  return sizeof(SharedRecordTypmodRegistry);
1880 }
struct SharedRecordTypmodRegistry SharedRecordTypmodRegistry
Definition: typcache.h:165

◆ SharedRecordTypmodRegistryInit()

void SharedRecordTypmodRegistryInit ( SharedRecordTypmodRegistry ,
dsm_segment segment,
dsa_area area 
)

Definition at line 1899 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().

1902 {
1903  MemoryContext old_context;
1904  dshash_table *record_table;
1905  dshash_table *typmod_table;
1906  int32 typmod;
1907 
1909 
1910  /* We can't already be attached to a shared registry. */
1914 
1915  old_context = MemoryContextSwitchTo(TopMemoryContext);
1916 
1917  /* Create the hash table of tuple descriptors indexed by themselves. */
1918  record_table = dshash_create(area, &srtr_record_table_params, area);
1919 
1920  /* Create the hash table of tuple descriptors indexed by typmod. */
1921  typmod_table = dshash_create(area, &srtr_typmod_table_params, NULL);
1922 
1923  MemoryContextSwitchTo(old_context);
1924 
1925  /* Initialize the SharedRecordTypmodRegistry. */
1926  registry->record_table_handle = dshash_get_hash_table_handle(record_table);
1927  registry->typmod_table_handle = dshash_get_hash_table_handle(typmod_table);
1928  pg_atomic_init_u32(&registry->next_typmod, NextRecordTypmod);
1929 
1930  /*
1931  * Copy all entries from this backend's private registry into the shared
1932  * registry.
1933  */
1934  for (typmod = 0; typmod < NextRecordTypmod; ++typmod)
1935  {
1936  SharedTypmodTableEntry *typmod_table_entry;
1937  SharedRecordTableEntry *record_table_entry;
1938  SharedRecordTableKey record_table_key;
1939  dsa_pointer shared_dp;
1940  TupleDesc tupdesc;
1941  bool found;
1942 
1943  tupdesc = RecordCacheArray[typmod];
1944  if (tupdesc == NULL)
1945  continue;
1946 
1947  /* Copy the TupleDesc into shared memory. */
1948  shared_dp = share_tupledesc(area, tupdesc, typmod);
1949 
1950  /* Insert into the typmod table. */
1951  typmod_table_entry = dshash_find_or_insert(typmod_table,
1952  &tupdesc->tdtypmod,
1953  &found);
1954  if (found)
1955  elog(ERROR, "cannot create duplicate shared record typmod");
1956  typmod_table_entry->typmod = tupdesc->tdtypmod;
1957  typmod_table_entry->shared_tupdesc = shared_dp;
1958  dshash_release_lock(typmod_table, typmod_table_entry);
1959 
1960  /* Insert into the record table. */
1961  record_table_key.shared = false;
1962  record_table_key.u.local_tupdesc = tupdesc;
1963  record_table_entry = dshash_find_or_insert(record_table,
1964  &record_table_key,
1965  &found);
1966  if (!found)
1967  {
1968  record_table_entry->key.shared = true;
1969  record_table_entry->key.u.shared_tupdesc = shared_dp;
1970  }
1971  dshash_release_lock(record_table, record_table_entry);
1972  }
1973 
1974  /*
1975  * Set up the global state that will tell assign_record_type_typmod and
1976  * lookup_rowtype_tupdesc_internal about the shared registry.
1977  */
1978  CurrentSession->shared_record_table = record_table;
1979  CurrentSession->shared_typmod_table = typmod_table;
1981 
1982  /*
1983  * We install a detach hook in the leader, but only to handle cleanup on
1984  * failure during GetSessionDsmHandle(). Once GetSessionDsmHandle() pins
1985  * the memory, the leader process will use a shared registry until it
1986  * exits.
1987  */
1989 }
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:84
signed int int32
Definition: c.h:313
static TupleDesc * RecordCacheArray
Definition: typcache.c:266
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:60
MemoryContext TopMemoryContext
Definition: mcxt.c:44
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:2622
static int32 NextRecordTypmod
Definition: typcache.c:269
struct SharedRecordTypmodRegistry * shared_typmod_registry
Definition: session.h:31
uintptr_t Datum
Definition: postgres.h:367
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:699
static dsa_pointer share_tupledesc(dsa_area *area, TupleDesc tupdesc, uint32 typmod)
Definition: typcache.c:2489
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:226
#define elog
Definition: elog.h:219

◆ UpdateDomainConstraintRef()

void UpdateDomainConstraintRef ( DomainConstraintRef ref)

Definition at line 1229 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, and TypeCacheEntry::typtype.

Referenced by domain_check_input().

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