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

Go to the source code of this file.

Data Structures

struct  TypeCacheEntry
 
struct  DomainConstraintRef
 

Macros

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

Typedefs

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

Functions

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

Macro Definition Documentation

◆ INVALID_TUPLEDESC_IDENTIFIER

#define INVALID_TUPLEDESC_IDENTIFIER   ((uint64) 1)

Definition at line 155 of file typcache.h.

◆ TYPECACHE_BTREE_OPFAMILY

#define TYPECACHE_BTREE_OPFAMILY   0x00200

Definition at line 145 of file typcache.h.

◆ TYPECACHE_CMP_PROC

#define TYPECACHE_CMP_PROC   0x00008

Definition at line 139 of file typcache.h.

◆ TYPECACHE_CMP_PROC_FINFO

#define TYPECACHE_CMP_PROC_FINFO   0x00040

Definition at line 142 of file typcache.h.

◆ TYPECACHE_DOMAIN_BASE_INFO

#define TYPECACHE_DOMAIN_BASE_INFO   0x01000

Definition at line 148 of file typcache.h.

◆ TYPECACHE_DOMAIN_CONSTR_INFO

#define TYPECACHE_DOMAIN_CONSTR_INFO   0x02000

Definition at line 149 of file typcache.h.

◆ TYPECACHE_EQ_OPR

#define TYPECACHE_EQ_OPR   0x00001

Definition at line 136 of file typcache.h.

◆ TYPECACHE_EQ_OPR_FINFO

#define TYPECACHE_EQ_OPR_FINFO   0x00020

Definition at line 141 of file typcache.h.

◆ TYPECACHE_GT_OPR

#define TYPECACHE_GT_OPR   0x00004

Definition at line 138 of file typcache.h.

◆ TYPECACHE_HASH_EXTENDED_PROC

#define TYPECACHE_HASH_EXTENDED_PROC   0x04000

Definition at line 150 of file typcache.h.

◆ TYPECACHE_HASH_EXTENDED_PROC_FINFO

#define TYPECACHE_HASH_EXTENDED_PROC_FINFO   0x08000

Definition at line 151 of file typcache.h.

◆ TYPECACHE_HASH_OPFAMILY

#define TYPECACHE_HASH_OPFAMILY   0x00400

Definition at line 146 of file typcache.h.

◆ TYPECACHE_HASH_PROC

#define TYPECACHE_HASH_PROC   0x00010

Definition at line 140 of file typcache.h.

◆ TYPECACHE_HASH_PROC_FINFO

#define TYPECACHE_HASH_PROC_FINFO   0x00080

Definition at line 143 of file typcache.h.

◆ TYPECACHE_LT_OPR

#define TYPECACHE_LT_OPR   0x00002

Definition at line 137 of file typcache.h.

◆ TYPECACHE_MULTIRANGE_INFO

#define TYPECACHE_MULTIRANGE_INFO   0x10000

Definition at line 152 of file typcache.h.

◆ TYPECACHE_RANGE_INFO

#define TYPECACHE_RANGE_INFO   0x00800

Definition at line 147 of file typcache.h.

◆ TYPECACHE_TUPDESC

#define TYPECACHE_TUPDESC   0x00100

Definition at line 144 of file typcache.h.

Typedef Documentation

◆ DomainConstraintCache

Definition at line 1 of file typcache.h.

◆ DomainConstraintRef

◆ SharedRecordTypmodRegistry

Definition at line 1 of file typcache.h.

◆ TypeCacheEntry

Function Documentation

◆ assign_record_type_identifier()

uint64 assign_record_type_identifier ( Oid  type_id,
int32  typmod 
)

Definition at line 2042 of file typcache.c.

2043 {
2044  if (type_id != RECORDOID)
2045  {
2046  /*
2047  * It's a named composite type, so use the regular typcache.
2048  */
2049  TypeCacheEntry *typentry;
2050 
2051  typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
2052  if (typentry->tupDesc == NULL)
2053  ereport(ERROR,
2054  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2055  errmsg("type %s is not composite",
2056  format_type_be(type_id))));
2057  Assert(typentry->tupDesc_identifier != 0);
2058  return typentry->tupDesc_identifier;
2059  }
2060  else
2061  {
2062  /*
2063  * It's a transient record type, so look in our record-type table.
2064  */
2065  if (typmod >= 0 && typmod < RecordCacheArrayLen &&
2066  RecordCacheArray[typmod].tupdesc != NULL)
2067  {
2068  Assert(RecordCacheArray[typmod].id != 0);
2069  return RecordCacheArray[typmod].id;
2070  }
2071 
2072  /* For anonymous or unrecognized record type, generate a new ID */
2073  return ++tupledesc_id_counter;
2074  }
2075 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
Assert(fmt[strlen(fmt) - 1] !='\n')
uint64 tupDesc_identifier
Definition: typcache.h:90
TupleDesc tupDesc
Definition: typcache.h:89
static RecordCacheArrayEntry * RecordCacheArray
Definition: typcache.c:283
static int32 RecordCacheArrayLen
Definition: typcache.c:284
static uint64 tupledesc_id_counter
Definition: typcache.c:292
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:344
#define TYPECACHE_TUPDESC
Definition: typcache.h:144

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

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

◆ assign_record_type_typmod()

void assign_record_type_typmod ( TupleDesc  tupDesc)

Definition at line 1950 of file typcache.c.

1951 {
1952  RecordCacheEntry *recentry;
1953  TupleDesc entDesc;
1954  bool found;
1955  MemoryContext oldcxt;
1956 
1957  Assert(tupDesc->tdtypeid == RECORDOID);
1958 
1959  if (RecordCacheHash == NULL)
1960  {
1961  /* First time through: initialize the hash table */
1962  HASHCTL ctl;
1963 
1964  ctl.keysize = sizeof(TupleDesc); /* just the pointer */
1965  ctl.entrysize = sizeof(RecordCacheEntry);
1968  RecordCacheHash = hash_create("Record information cache", 64,
1969  &ctl,
1971 
1972  /* Also make sure CacheMemoryContext exists */
1973  if (!CacheMemoryContext)
1975  }
1976 
1977  /*
1978  * Find a hashtable entry for this tuple descriptor. We don't use
1979  * HASH_ENTER yet, because if it's missing, we need to make sure that all
1980  * the allocations succeed before we create the new entry.
1981  */
1983  &tupDesc,
1984  HASH_FIND, &found);
1985  if (found && recentry->tupdesc != NULL)
1986  {
1987  tupDesc->tdtypmod = recentry->tupdesc->tdtypmod;
1988  return;
1989  }
1990 
1991  /* Not present, so need to manufacture an entry */
1993 
1994  /* Look in the SharedRecordTypmodRegistry, if attached */
1995  entDesc = find_or_make_matching_shared_tupledesc(tupDesc);
1996  if (entDesc == NULL)
1997  {
1998  /*
1999  * Make sure we have room before we CreateTupleDescCopy() or advance
2000  * NextRecordTypmod.
2001  */
2003 
2004  /* Reference-counted local cache only. */
2005  entDesc = CreateTupleDescCopy(tupDesc);
2006  entDesc->tdrefcount = 1;
2007  entDesc->tdtypmod = NextRecordTypmod++;
2008  }
2009  else
2010  {
2012  }
2013 
2014  RecordCacheArray[entDesc->tdtypmod].tupdesc = entDesc;
2015 
2016  /* Assign a unique tupdesc identifier, too. */
2018 
2019  /* Fully initialized; create the hash table entry */
2021  &tupDesc,
2022  HASH_ENTER, NULL);
2023  recentry->tupdesc = entDesc;
2024 
2025  /* Update the caller's tuple descriptor. */
2026  tupDesc->tdtypmod = entDesc->tdtypmod;
2027 
2028  MemoryContextSwitchTo(oldcxt);
2029 }
void CreateCacheMemoryContext(void)
Definition: catcache.c:667
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_COMPARE
Definition: hsearch.h:99
#define HASH_FUNCTION
Definition: hsearch.h:98
MemoryContext CacheMemoryContext
Definition: mcxt.c:144
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
Size keysize
Definition: hsearch.h:75
HashValueFunc hash
Definition: hsearch.h:78
Size entrysize
Definition: hsearch.h:76
HashCompareFunc match
Definition: hsearch.h:80
TupleDesc tupdesc
Definition: typcache.c:159
int tdrefcount
Definition: tupdesc.h:84
int32 tdtypmod
Definition: tupdesc.h:83
Oid tdtypeid
Definition: tupdesc.h:82
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:133
struct TupleDescData * TupleDesc
Definition: tupdesc.h:89
static TupleDesc find_or_make_matching_shared_tupledesc(TupleDesc tupdesc)
Definition: typcache.c:2758
static int32 NextRecordTypmod
Definition: typcache.c:285
static HTAB * RecordCacheHash
Definition: typcache.c:274
struct RecordCacheEntry RecordCacheEntry
static int record_type_typmod_compare(const void *a, const void *b, size_t size)
Definition: typcache.c:1934
static void ensure_record_cache_typmod_slot_exists(int32 typmod)
Definition: typcache.c:1707
static uint32 record_type_typmod_hash(const void *data, size_t size)
Definition: typcache.c:1923

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_FIND, HASH_FUNCTION, hash_search(), RecordCacheArrayEntry::id, HASHCTL::keysize, HASHCTL::match, MemoryContextSwitchTo(), NextRecordTypmod, record_type_typmod_compare(), record_type_typmod_hash(), RecordCacheArray, RecordCacheHash, TupleDescData::tdrefcount, TupleDescData::tdtypeid, TupleDescData::tdtypmod, RecordCacheEntry::tupdesc, RecordCacheArrayEntry::tupdesc, and tupledesc_id_counter.

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

◆ compare_values_of_enum()

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

Definition at line 2474 of file typcache.c.

2475 {
2476  TypeCacheEnumData *enumdata;
2477  EnumItem *item1;
2478  EnumItem *item2;
2479 
2480  /*
2481  * Equal OIDs are certainly equal --- this case was probably handled by
2482  * our caller, but we may as well check.
2483  */
2484  if (arg1 == arg2)
2485  return 0;
2486 
2487  /* Load up the cache if first time through */
2488  if (tcache->enumData == NULL)
2489  load_enum_cache_data(tcache);
2490  enumdata = tcache->enumData;
2491 
2492  /*
2493  * If both OIDs are known-sorted, we can just compare them directly.
2494  */
2495  if (enum_known_sorted(enumdata, arg1) &&
2496  enum_known_sorted(enumdata, arg2))
2497  {
2498  if (arg1 < arg2)
2499  return -1;
2500  else
2501  return 1;
2502  }
2503 
2504  /*
2505  * Slow path: we have to identify their actual sort-order positions.
2506  */
2507  item1 = find_enumitem(enumdata, arg1);
2508  item2 = find_enumitem(enumdata, arg2);
2509 
2510  if (item1 == NULL || item2 == NULL)
2511  {
2512  /*
2513  * We couldn't find one or both values. That means the enum has
2514  * changed under us, so re-initialize the cache and try again. We
2515  * don't bother retrying the known-sorted case in this path.
2516  */
2517  load_enum_cache_data(tcache);
2518  enumdata = tcache->enumData;
2519 
2520  item1 = find_enumitem(enumdata, arg1);
2521  item2 = find_enumitem(enumdata, arg2);
2522 
2523  /*
2524  * If we still can't find the values, complain: we must have corrupt
2525  * data.
2526  */
2527  if (item1 == NULL)
2528  elog(ERROR, "enum value %u not found in cache for enum %s",
2529  arg1, format_type_be(tcache->type_id));
2530  if (item2 == NULL)
2531  elog(ERROR, "enum value %u not found in cache for enum %s",
2532  arg2, format_type_be(tcache->type_id));
2533  }
2534 
2535  if (item1->sort_order < item2->sort_order)
2536  return -1;
2537  else if (item1->sort_order > item2->sort_order)
2538  return 1;
2539  else
2540  return 0;
2541 }
float4 sort_order
Definition: typcache.c:135
struct TypeCacheEnumData * enumData
Definition: typcache.h:129
static void load_enum_cache_data(TypeCacheEntry *tcache)
Definition: typcache.c:2547
static EnumItem * find_enumitem(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:2702
static bool enum_known_sorted(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:2445

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

Referenced by enum_cmp_internal().

◆ DomainHasConstraints()

bool DomainHasConstraints ( Oid  type_id)

Definition at line 1397 of file typcache.c.

1398 {
1399  TypeCacheEntry *typentry;
1400 
1401  /*
1402  * Note: a side effect is to cause the typcache's domain data to become
1403  * valid. This is fine since we'll likely need it soon if there is any.
1404  */
1405  typentry = lookup_type_cache(type_id, TYPECACHE_DOMAIN_CONSTR_INFO);
1406 
1407  return (typentry->domainData != NULL);
1408 }
DomainConstraintCache * domainData
Definition: typcache.h:120
#define TYPECACHE_DOMAIN_CONSTR_INFO
Definition: typcache.h:149

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

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

◆ InitDomainConstraintRef()

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

Definition at line 1310 of file typcache.c.

1312 {
1313  /* Look up the typcache entry --- we assume it survives indefinitely */
1315  ref->need_exprstate = need_exprstate;
1316  /* For safety, establish the callback before acquiring a refcount */
1317  ref->refctx = refctx;
1318  ref->dcc = NULL;
1320  ref->callback.arg = (void *) ref;
1322  /* Acquire refcount if there are constraints, and set up exported list */
1323  if (ref->tcache->domainData)
1324  {
1325  ref->dcc = ref->tcache->domainData;
1326  ref->dcc->dccRefCount++;
1327  if (ref->need_exprstate)
1329  ref->refctx);
1330  else
1331  ref->constraints = ref->dcc->constraints;
1332  }
1333  else
1334  ref->constraints = NIL;
1335 }
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:477
#define NIL
Definition: pg_list.h:68
DomainConstraintCache * dcc
Definition: typcache.h:171
MemoryContext refctx
Definition: typcache.h:166
MemoryContextCallback callback
Definition: typcache.h:172
TypeCacheEntry * tcache
Definition: typcache.h:167
MemoryContextCallbackFunction func
Definition: palloc.h:49
static List * prep_domain_constraints(List *constraints, MemoryContext execctx)
Definition: typcache.c:1272
static void dccref_deletion_callback(void *arg)
Definition: typcache.c:1251

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

Referenced by domain_state_setup(), and ExecInitCoerceToDomain().

◆ lookup_rowtype_tupdesc()

◆ lookup_rowtype_tupdesc_copy()

TupleDesc lookup_rowtype_tupdesc_copy ( Oid  type_id,
int32  typmod 
)

Definition at line 1864 of file typcache.c.

1865 {
1866  TupleDesc tmp;
1867 
1868  tmp = lookup_rowtype_tupdesc_internal(type_id, typmod, false);
1869  return CreateTupleDescCopyConstr(tmp);
1870 }
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:173

References CreateTupleDescCopyConstr(), and lookup_rowtype_tupdesc_internal().

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

◆ lookup_rowtype_tupdesc_domain()

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

Definition at line 1886 of file typcache.c.

1887 {
1888  TupleDesc tupDesc;
1889 
1890  if (type_id != RECORDOID)
1891  {
1892  /*
1893  * Check for domain or named composite type. We might as well load
1894  * whichever data is needed.
1895  */
1896  TypeCacheEntry *typentry;
1897 
1898  typentry = lookup_type_cache(type_id,
1901  if (typentry->typtype == TYPTYPE_DOMAIN)
1903  typentry->domainBaseTypmod,
1904  noError);
1905  if (typentry->tupDesc == NULL && !noError)
1906  ereport(ERROR,
1907  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1908  errmsg("type %s is not composite",
1909  format_type_be(type_id))));
1910  tupDesc = typentry->tupDesc;
1911  }
1912  else
1913  tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1914  if (tupDesc != NULL)
1915  PinTupleDesc(tupDesc);
1916  return tupDesc;
1917 }
int32 domainBaseTypmod
Definition: typcache.h:114
char typtype
Definition: typcache.h:43
Oid domainBaseType
Definition: typcache.h:113
TupleDesc lookup_rowtype_tupdesc_noerror(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1847
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:148

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

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

◆ lookup_rowtype_tupdesc_noerror()

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

Definition at line 1847 of file typcache.c.

1848 {
1849  TupleDesc tupDesc;
1850 
1851  tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1852  if (tupDesc != NULL)
1853  PinTupleDesc(tupDesc);
1854  return tupDesc;
1855 }

References lookup_rowtype_tupdesc_internal(), and PinTupleDesc.

Referenced by lookup_rowtype_tupdesc_domain().

◆ lookup_type_cache()

TypeCacheEntry* lookup_type_cache ( Oid  type_id,
int  flags 
)

Definition at line 344 of file typcache.c.

345 {
346  TypeCacheEntry *typentry;
347  bool found;
348 
349  if (TypeCacheHash == NULL)
350  {
351  /* First time through: initialize the hash table */
352  HASHCTL ctl;
353 
354  ctl.keysize = sizeof(Oid);
355  ctl.entrysize = sizeof(TypeCacheEntry);
356  TypeCacheHash = hash_create("Type information cache", 64,
357  &ctl, HASH_ELEM | HASH_BLOBS);
358 
359  /* Also set up callbacks for SI invalidations */
364 
365  /* Also make sure CacheMemoryContext exists */
366  if (!CacheMemoryContext)
368  }
369 
370  /* Try to look up an existing entry */
372  &type_id,
373  HASH_FIND, NULL);
374  if (typentry == NULL)
375  {
376  /*
377  * If we didn't find one, we want to make one. But first look up the
378  * pg_type row, just to make sure we don't make a cache entry for an
379  * invalid type OID. If the type OID is not valid, present a
380  * user-facing error, since some code paths such as domain_in() allow
381  * this function to be reached with a user-supplied OID.
382  */
383  HeapTuple tp;
384  Form_pg_type typtup;
385 
386  tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
387  if (!HeapTupleIsValid(tp))
388  ereport(ERROR,
389  (errcode(ERRCODE_UNDEFINED_OBJECT),
390  errmsg("type with OID %u does not exist", type_id)));
391  typtup = (Form_pg_type) GETSTRUCT(tp);
392  if (!typtup->typisdefined)
393  ereport(ERROR,
394  (errcode(ERRCODE_UNDEFINED_OBJECT),
395  errmsg("type \"%s\" is only a shell",
396  NameStr(typtup->typname))));
397 
398  /* Now make the typcache entry */
400  &type_id,
401  HASH_ENTER, &found);
402  Assert(!found); /* it wasn't there a moment ago */
403 
404  MemSet(typentry, 0, sizeof(TypeCacheEntry));
405 
406  /* These fields can never change, by definition */
407  typentry->type_id = type_id;
409  ObjectIdGetDatum(type_id));
410 
411  /* Keep this part in sync with the code below */
412  typentry->typlen = typtup->typlen;
413  typentry->typbyval = typtup->typbyval;
414  typentry->typalign = typtup->typalign;
415  typentry->typstorage = typtup->typstorage;
416  typentry->typtype = typtup->typtype;
417  typentry->typrelid = typtup->typrelid;
418  typentry->typsubscript = typtup->typsubscript;
419  typentry->typelem = typtup->typelem;
420  typentry->typcollation = typtup->typcollation;
421  typentry->flags |= TCFLAGS_HAVE_PG_TYPE_DATA;
422 
423  /* If it's a domain, immediately thread it into the domain cache list */
424  if (typentry->typtype == TYPTYPE_DOMAIN)
425  {
426  typentry->nextDomain = firstDomainTypeEntry;
427  firstDomainTypeEntry = typentry;
428  }
429 
430  ReleaseSysCache(tp);
431  }
432  else if (!(typentry->flags & TCFLAGS_HAVE_PG_TYPE_DATA))
433  {
434  /*
435  * We have an entry, but its pg_type row got changed, so reload the
436  * data obtained directly from pg_type.
437  */
438  HeapTuple tp;
439  Form_pg_type typtup;
440 
441  tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
442  if (!HeapTupleIsValid(tp))
443  ereport(ERROR,
444  (errcode(ERRCODE_UNDEFINED_OBJECT),
445  errmsg("type with OID %u does not exist", type_id)));
446  typtup = (Form_pg_type) GETSTRUCT(tp);
447  if (!typtup->typisdefined)
448  ereport(ERROR,
449  (errcode(ERRCODE_UNDEFINED_OBJECT),
450  errmsg("type \"%s\" is only a shell",
451  NameStr(typtup->typname))));
452 
453  /*
454  * Keep this part in sync with the code above. Many of these fields
455  * shouldn't ever change, particularly typtype, but copy 'em anyway.
456  */
457  typentry->typlen = typtup->typlen;
458  typentry->typbyval = typtup->typbyval;
459  typentry->typalign = typtup->typalign;
460  typentry->typstorage = typtup->typstorage;
461  typentry->typtype = typtup->typtype;
462  typentry->typrelid = typtup->typrelid;
463  typentry->typsubscript = typtup->typsubscript;
464  typentry->typelem = typtup->typelem;
465  typentry->typcollation = typtup->typcollation;
466  typentry->flags |= TCFLAGS_HAVE_PG_TYPE_DATA;
467 
468  ReleaseSysCache(tp);
469  }
470 
471  /*
472  * Look up opclasses if we haven't already and any dependent info is
473  * requested.
474  */
479  !(typentry->flags & TCFLAGS_CHECKED_BTREE_OPCLASS))
480  {
481  Oid opclass;
482 
483  opclass = GetDefaultOpClass(type_id, BTREE_AM_OID);
484  if (OidIsValid(opclass))
485  {
486  typentry->btree_opf = get_opclass_family(opclass);
487  typentry->btree_opintype = get_opclass_input_type(opclass);
488  }
489  else
490  {
491  typentry->btree_opf = typentry->btree_opintype = InvalidOid;
492  }
493 
494  /*
495  * Reset information derived from btree opclass. Note in particular
496  * that we'll redetermine the eq_opr even if we previously found one;
497  * this matters in case a btree opclass has been added to a type that
498  * previously had only a hash opclass.
499  */
500  typentry->flags &= ~(TCFLAGS_CHECKED_EQ_OPR |
505  }
506 
507  /*
508  * If we need to look up equality operator, and there's no btree opclass,
509  * force lookup of hash opclass.
510  */
511  if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
512  !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR) &&
513  typentry->btree_opf == InvalidOid)
514  flags |= TYPECACHE_HASH_OPFAMILY;
515 
520  !(typentry->flags & TCFLAGS_CHECKED_HASH_OPCLASS))
521  {
522  Oid opclass;
523 
524  opclass = GetDefaultOpClass(type_id, HASH_AM_OID);
525  if (OidIsValid(opclass))
526  {
527  typentry->hash_opf = get_opclass_family(opclass);
528  typentry->hash_opintype = get_opclass_input_type(opclass);
529  }
530  else
531  {
532  typentry->hash_opf = typentry->hash_opintype = InvalidOid;
533  }
534 
535  /*
536  * Reset information derived from hash opclass. We do *not* reset the
537  * eq_opr; if we already found one from the btree opclass, that
538  * decision is still good.
539  */
540  typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
542  typentry->flags |= TCFLAGS_CHECKED_HASH_OPCLASS;
543  }
544 
545  /*
546  * Look for requested operators and functions, if we haven't already.
547  */
548  if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
549  !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR))
550  {
551  Oid eq_opr = InvalidOid;
552 
553  if (typentry->btree_opf != InvalidOid)
554  eq_opr = get_opfamily_member(typentry->btree_opf,
555  typentry->btree_opintype,
556  typentry->btree_opintype,
558  if (eq_opr == InvalidOid &&
559  typentry->hash_opf != InvalidOid)
560  eq_opr = get_opfamily_member(typentry->hash_opf,
561  typentry->hash_opintype,
562  typentry->hash_opintype,
564 
565  /*
566  * If the proposed equality operator is array_eq or record_eq, check
567  * to see if the element type or column types support equality. If
568  * not, array_eq or record_eq would fail at runtime, so we don't want
569  * to report that the type has equality. (We can omit similar
570  * checking for ranges and multiranges because ranges can't be created
571  * in the first place unless their subtypes support equality.)
572  */
573  if (eq_opr == ARRAY_EQ_OP &&
574  !array_element_has_equality(typentry))
575  eq_opr = InvalidOid;
576  else if (eq_opr == RECORD_EQ_OP &&
577  !record_fields_have_equality(typentry))
578  eq_opr = InvalidOid;
579 
580  /* Force update of eq_opr_finfo only if we're changing state */
581  if (typentry->eq_opr != eq_opr)
582  typentry->eq_opr_finfo.fn_oid = InvalidOid;
583 
584  typentry->eq_opr = eq_opr;
585 
586  /*
587  * Reset info about hash functions whenever we pick up new info about
588  * equality operator. This is so we can ensure that the hash
589  * functions match the operator.
590  */
591  typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC |
593  typentry->flags |= TCFLAGS_CHECKED_EQ_OPR;
594  }
595  if ((flags & TYPECACHE_LT_OPR) &&
596  !(typentry->flags & TCFLAGS_CHECKED_LT_OPR))
597  {
598  Oid lt_opr = InvalidOid;
599 
600  if (typentry->btree_opf != InvalidOid)
601  lt_opr = get_opfamily_member(typentry->btree_opf,
602  typentry->btree_opintype,
603  typentry->btree_opintype,
605 
606  /*
607  * As above, make sure array_cmp or record_cmp will succeed; but again
608  * we need no special check for ranges or multiranges.
609  */
610  if (lt_opr == ARRAY_LT_OP &&
611  !array_element_has_compare(typentry))
612  lt_opr = InvalidOid;
613  else if (lt_opr == RECORD_LT_OP &&
614  !record_fields_have_compare(typentry))
615  lt_opr = InvalidOid;
616 
617  typentry->lt_opr = lt_opr;
618  typentry->flags |= TCFLAGS_CHECKED_LT_OPR;
619  }
620  if ((flags & TYPECACHE_GT_OPR) &&
621  !(typentry->flags & TCFLAGS_CHECKED_GT_OPR))
622  {
623  Oid gt_opr = InvalidOid;
624 
625  if (typentry->btree_opf != InvalidOid)
626  gt_opr = get_opfamily_member(typentry->btree_opf,
627  typentry->btree_opintype,
628  typentry->btree_opintype,
630 
631  /*
632  * As above, make sure array_cmp or record_cmp will succeed; but again
633  * we need no special check for ranges or multiranges.
634  */
635  if (gt_opr == ARRAY_GT_OP &&
636  !array_element_has_compare(typentry))
637  gt_opr = InvalidOid;
638  else if (gt_opr == RECORD_GT_OP &&
639  !record_fields_have_compare(typentry))
640  gt_opr = InvalidOid;
641 
642  typentry->gt_opr = gt_opr;
643  typentry->flags |= TCFLAGS_CHECKED_GT_OPR;
644  }
645  if ((flags & (TYPECACHE_CMP_PROC | TYPECACHE_CMP_PROC_FINFO)) &&
646  !(typentry->flags & TCFLAGS_CHECKED_CMP_PROC))
647  {
648  Oid cmp_proc = InvalidOid;
649 
650  if (typentry->btree_opf != InvalidOid)
651  cmp_proc = get_opfamily_proc(typentry->btree_opf,
652  typentry->btree_opintype,
653  typentry->btree_opintype,
654  BTORDER_PROC);
655 
656  /*
657  * As above, make sure array_cmp or record_cmp will succeed; but again
658  * we need no special check for ranges or multiranges.
659  */
660  if (cmp_proc == F_BTARRAYCMP &&
661  !array_element_has_compare(typentry))
662  cmp_proc = InvalidOid;
663  else if (cmp_proc == F_BTRECORDCMP &&
664  !record_fields_have_compare(typentry))
665  cmp_proc = InvalidOid;
666 
667  /* Force update of cmp_proc_finfo only if we're changing state */
668  if (typentry->cmp_proc != cmp_proc)
669  typentry->cmp_proc_finfo.fn_oid = InvalidOid;
670 
671  typentry->cmp_proc = cmp_proc;
672  typentry->flags |= TCFLAGS_CHECKED_CMP_PROC;
673  }
675  !(typentry->flags & TCFLAGS_CHECKED_HASH_PROC))
676  {
677  Oid hash_proc = InvalidOid;
678 
679  /*
680  * We insist that the eq_opr, if one has been determined, match the
681  * hash opclass; else report there is no hash function.
682  */
683  if (typentry->hash_opf != InvalidOid &&
684  (!OidIsValid(typentry->eq_opr) ||
685  typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
686  typentry->hash_opintype,
687  typentry->hash_opintype,
689  hash_proc = get_opfamily_proc(typentry->hash_opf,
690  typentry->hash_opintype,
691  typentry->hash_opintype,
693 
694  /*
695  * As above, make sure hash_array, hash_record, or hash_range will
696  * succeed.
697  */
698  if (hash_proc == F_HASH_ARRAY &&
699  !array_element_has_hashing(typentry))
700  hash_proc = InvalidOid;
701  else if (hash_proc == F_HASH_RECORD &&
702  !record_fields_have_hashing(typentry))
703  hash_proc = InvalidOid;
704  else if (hash_proc == F_HASH_RANGE &&
705  !range_element_has_hashing(typentry))
706  hash_proc = InvalidOid;
707 
708  /*
709  * Likewise for hash_multirange.
710  */
711  if (hash_proc == F_HASH_MULTIRANGE &&
713  hash_proc = InvalidOid;
714 
715  /* Force update of hash_proc_finfo only if we're changing state */
716  if (typentry->hash_proc != hash_proc)
717  typentry->hash_proc_finfo.fn_oid = InvalidOid;
718 
719  typentry->hash_proc = hash_proc;
720  typentry->flags |= TCFLAGS_CHECKED_HASH_PROC;
721  }
722  if ((flags & (TYPECACHE_HASH_EXTENDED_PROC |
725  {
726  Oid hash_extended_proc = InvalidOid;
727 
728  /*
729  * We insist that the eq_opr, if one has been determined, match the
730  * hash opclass; else report there is no hash function.
731  */
732  if (typentry->hash_opf != InvalidOid &&
733  (!OidIsValid(typentry->eq_opr) ||
734  typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
735  typentry->hash_opintype,
736  typentry->hash_opintype,
738  hash_extended_proc = get_opfamily_proc(typentry->hash_opf,
739  typentry->hash_opintype,
740  typentry->hash_opintype,
742 
743  /*
744  * As above, make sure hash_array_extended, hash_record_extended, or
745  * hash_range_extended will succeed.
746  */
747  if (hash_extended_proc == F_HASH_ARRAY_EXTENDED &&
749  hash_extended_proc = InvalidOid;
750  else if (hash_extended_proc == F_HASH_RECORD_EXTENDED &&
752  hash_extended_proc = InvalidOid;
753  else if (hash_extended_proc == F_HASH_RANGE_EXTENDED &&
755  hash_extended_proc = InvalidOid;
756 
757  /*
758  * Likewise for hash_multirange_extended.
759  */
760  if (hash_extended_proc == F_HASH_MULTIRANGE_EXTENDED &&
762  hash_extended_proc = InvalidOid;
763 
764  /* Force update of proc finfo only if we're changing state */
765  if (typentry->hash_extended_proc != hash_extended_proc)
767 
768  typentry->hash_extended_proc = hash_extended_proc;
770  }
771 
772  /*
773  * Set up fmgr lookup info as requested
774  *
775  * Note: we tell fmgr the finfo structures live in CacheMemoryContext,
776  * which is not quite right (they're really in the hash table's private
777  * memory context) but this will do for our purposes.
778  *
779  * Note: the code above avoids invalidating the finfo structs unless the
780  * referenced operator/function OID actually changes. This is to prevent
781  * unnecessary leakage of any subsidiary data attached to an finfo, since
782  * that would cause session-lifespan memory leaks.
783  */
784  if ((flags & TYPECACHE_EQ_OPR_FINFO) &&
785  typentry->eq_opr_finfo.fn_oid == InvalidOid &&
786  typentry->eq_opr != InvalidOid)
787  {
788  Oid eq_opr_func;
789 
790  eq_opr_func = get_opcode(typentry->eq_opr);
791  if (eq_opr_func != InvalidOid)
792  fmgr_info_cxt(eq_opr_func, &typentry->eq_opr_finfo,
794  }
795  if ((flags & TYPECACHE_CMP_PROC_FINFO) &&
796  typentry->cmp_proc_finfo.fn_oid == InvalidOid &&
797  typentry->cmp_proc != InvalidOid)
798  {
799  fmgr_info_cxt(typentry->cmp_proc, &typentry->cmp_proc_finfo,
801  }
802  if ((flags & TYPECACHE_HASH_PROC_FINFO) &&
803  typentry->hash_proc_finfo.fn_oid == InvalidOid &&
804  typentry->hash_proc != InvalidOid)
805  {
806  fmgr_info_cxt(typentry->hash_proc, &typentry->hash_proc_finfo,
808  }
809  if ((flags & TYPECACHE_HASH_EXTENDED_PROC_FINFO) &&
811  typentry->hash_extended_proc != InvalidOid)
812  {
814  &typentry->hash_extended_proc_finfo,
816  }
817 
818  /*
819  * If it's a composite type (row type), get tupdesc if requested
820  */
821  if ((flags & TYPECACHE_TUPDESC) &&
822  typentry->tupDesc == NULL &&
823  typentry->typtype == TYPTYPE_COMPOSITE)
824  {
825  load_typcache_tupdesc(typentry);
826  }
827 
828  /*
829  * If requested, get information about a range type
830  *
831  * This includes making sure that the basic info about the range element
832  * type is up-to-date.
833  */
834  if ((flags & TYPECACHE_RANGE_INFO) &&
835  typentry->typtype == TYPTYPE_RANGE)
836  {
837  if (typentry->rngelemtype == NULL)
838  load_rangetype_info(typentry);
839  else if (!(typentry->rngelemtype->flags & TCFLAGS_HAVE_PG_TYPE_DATA))
840  (void) lookup_type_cache(typentry->rngelemtype->type_id, 0);
841  }
842 
843  /*
844  * If requested, get information about a multirange type
845  */
846  if ((flags & TYPECACHE_MULTIRANGE_INFO) &&
847  typentry->rngtype == NULL &&
848  typentry->typtype == TYPTYPE_MULTIRANGE)
849  {
850  load_multirangetype_info(typentry);
851  }
852 
853  /*
854  * If requested, get information about a domain type
855  */
856  if ((flags & TYPECACHE_DOMAIN_BASE_INFO) &&
857  typentry->domainBaseType == InvalidOid &&
858  typentry->typtype == TYPTYPE_DOMAIN)
859  {
860  typentry->domainBaseTypmod = -1;
861  typentry->domainBaseType =
862  getBaseTypeAndTypmod(type_id, &typentry->domainBaseTypmod);
863  }
864  if ((flags & TYPECACHE_DOMAIN_CONSTR_INFO) &&
865  (typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
866  typentry->typtype == TYPTYPE_DOMAIN)
867  {
868  load_domaintype_info(typentry);
869  }
870 
871  return typentry;
872 }
#define NameStr(name)
Definition: c.h:735
#define MemSet(start, val, len)
Definition: c.h:1009
#define OidIsValid(objectId)
Definition: c.h:764
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
#define HASHSTANDARD_PROC
Definition: hash.h:355
#define HASHEXTENDED_PROC
Definition: hash.h:356
#define HASH_BLOBS
Definition: hsearch.h:97
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
Definition: indexcmds.c:2296
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1560
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1518
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1216
Oid get_opclass_family(Oid opclass)
Definition: lsyscache.c:1194
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:795
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1289
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:165
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2520
#define BTORDER_PROC
Definition: nbtree.h:707
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define HTEqualStrategyNumber
Definition: stratnum.h:41
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Oid fn_oid
Definition: fmgr.h:59
uint32 type_id_hash
Definition: typcache.h:36
FmgrInfo hash_proc_finfo
Definition: typcache.h:77
Oid hash_extended_proc
Definition: typcache.h:66
Oid typsubscript
Definition: typcache.h:45
FmgrInfo cmp_proc_finfo
Definition: typcache.h:76
char typalign
Definition: typcache.h:41
struct TypeCacheEntry * rngelemtype
Definition: typcache.h:98
FmgrInfo hash_extended_proc_finfo
Definition: typcache.h:78
struct TypeCacheEntry * rngtype
Definition: typcache.h:107
FmgrInfo eq_opr_finfo
Definition: typcache.h:75
Oid btree_opintype
Definition: typcache.h:58
struct TypeCacheEntry * nextDomain
Definition: typcache.h:132
bool typbyval
Definition: typcache.h:40
int16 typlen
Definition: typcache.h:39
Oid hash_opintype
Definition: typcache.h:60
Oid typcollation
Definition: typcache.h:47
char typstorage
Definition: typcache.h:42
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:868
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:820
@ TYPEOID
Definition: syscache.h:114
@ CLAOID
Definition: syscache.h:48
@ CONSTROID
Definition: syscache.h:53
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:209
#define TCFLAGS_CHECKED_BTREE_OPCLASS
Definition: typcache.c:85
#define TCFLAGS_CHECKED_HASH_OPCLASS
Definition: typcache.c:86
static bool range_element_has_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1623
static bool record_fields_have_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1502
static bool record_fields_have_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1510
#define TCFLAGS_CHECKED_GT_OPR
Definition: typcache.c:89
static bool multirange_element_has_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1663
static bool record_fields_have_equality(TypeCacheEntry *typentry)
Definition: typcache.c:1486
#define TCFLAGS_CHECKED_LT_OPR
Definition: typcache.c:88
#define TCFLAGS_CHECKED_HASH_PROC
Definition: typcache.c:91
static bool array_element_has_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1448
static bool array_element_has_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1440
static void load_multirangetype_info(TypeCacheEntry *typentry)
Definition: typcache.c:969
#define TCFLAGS_CHECKED_CMP_PROC
Definition: typcache.c:90
static bool multirange_element_has_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1671
static bool array_element_has_equality(TypeCacheEntry *typentry)
Definition: typcache.c:1424
static void load_rangetype_info(TypeCacheEntry *typentry)
Definition: typcache.c:912
static bool range_element_has_extended_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1631
static TypeCacheEntry * firstDomainTypeEntry
Definition: typcache.c:81
#define TCFLAGS_CHECKED_HASH_EXTENDED_PROC
Definition: typcache.c:92
static void TypeCacheTypCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:2352
static void TypeCacheConstrCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:2421
static void TypeCacheOpcCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:2392
static void load_domaintype_info(TypeCacheEntry *typentry)
Definition: typcache.c:991
static void TypeCacheRelCallback(Datum arg, Oid relid)
Definition: typcache.c:2287
static bool array_element_has_compare(TypeCacheEntry *typentry)
Definition: typcache.c:1432
#define TCFLAGS_HAVE_PG_TYPE_DATA
Definition: typcache.c:84
#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS
Definition: typcache.c:103
static HTAB * TypeCacheHash
Definition: typcache.c:78
static bool record_fields_have_compare(TypeCacheEntry *typentry)
Definition: typcache.c:1494
#define TCFLAGS_CHECKED_EQ_OPR
Definition: typcache.c:87
static void load_typcache_tupdesc(TypeCacheEntry *typentry)
Definition: typcache.c:878
#define TYPECACHE_HASH_PROC_FINFO
Definition: typcache.h:143
#define TYPECACHE_EQ_OPR
Definition: typcache.h:136
#define TYPECACHE_HASH_OPFAMILY
Definition: typcache.h:146
#define TYPECACHE_MULTIRANGE_INFO
Definition: typcache.h:152
#define TYPECACHE_EQ_OPR_FINFO
Definition: typcache.h:141
#define TYPECACHE_HASH_EXTENDED_PROC
Definition: typcache.h:150
#define TYPECACHE_BTREE_OPFAMILY
Definition: typcache.h:145
#define TYPECACHE_RANGE_INFO
Definition: typcache.h:147
#define TYPECACHE_GT_OPR
Definition: typcache.h:138
#define TYPECACHE_CMP_PROC
Definition: typcache.h:139
struct TypeCacheEntry TypeCacheEntry
#define TYPECACHE_LT_OPR
Definition: typcache.h:137
#define TYPECACHE_HASH_EXTENDED_PROC_FINFO
Definition: typcache.h:151
#define TYPECACHE_CMP_PROC_FINFO
Definition: typcache.h:142
#define TYPECACHE_HASH_PROC
Definition: typcache.h:140

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, GetSysCacheHashValue1, 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_multirangetype_info(), load_rangetype_info(), load_typcache_tupdesc(), TypeCacheEntry::lt_opr, MemSet, multirange_element_has_extended_hashing(), multirange_element_has_hashing(), NameStr, TypeCacheEntry::nextDomain, ObjectIdGetDatum(), OidIsValid, range_element_has_extended_hashing(), range_element_has_hashing(), record_fields_have_compare(), record_fields_have_equality(), record_fields_have_extended_hashing(), record_fields_have_hashing(), ReleaseSysCache(), TypeCacheEntry::rngelemtype, TypeCacheEntry::rngtype, SearchSysCache1(), TCFLAGS_CHECKED_BTREE_OPCLASS, TCFLAGS_CHECKED_CMP_PROC, TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS, TCFLAGS_CHECKED_EQ_OPR, TCFLAGS_CHECKED_GT_OPR, TCFLAGS_CHECKED_HASH_EXTENDED_PROC, TCFLAGS_CHECKED_HASH_OPCLASS, TCFLAGS_CHECKED_HASH_PROC, TCFLAGS_CHECKED_LT_OPR, TCFLAGS_HAVE_PG_TYPE_DATA, TypeCacheEntry::tupDesc, TypeCacheEntry::typalign, TypeCacheEntry::typbyval, TypeCacheEntry::typcollation, TypeCacheEntry::type_id, TypeCacheEntry::type_id_hash, TYPECACHE_BTREE_OPFAMILY, TYPECACHE_CMP_PROC, TYPECACHE_CMP_PROC_FINFO, TYPECACHE_DOMAIN_BASE_INFO, TYPECACHE_DOMAIN_CONSTR_INFO, TYPECACHE_EQ_OPR, TYPECACHE_EQ_OPR_FINFO, TYPECACHE_GT_OPR, TYPECACHE_HASH_EXTENDED_PROC, TYPECACHE_HASH_EXTENDED_PROC_FINFO, TYPECACHE_HASH_OPFAMILY, TYPECACHE_HASH_PROC, TYPECACHE_HASH_PROC_FINFO, TYPECACHE_LT_OPR, TYPECACHE_MULTIRANGE_INFO, TYPECACHE_RANGE_INFO, TYPECACHE_TUPDESC, TypeCacheConstrCallback(), TypeCacheHash, TypeCacheOpcCallback(), TypeCacheRelCallback(), TypeCacheTypCallback(), TypeCacheEntry::typelem, TYPEOID, TypeCacheEntry::typlen, TypeCacheEntry::typrelid, TypeCacheEntry::typstorage, TypeCacheEntry::typsubscript, and TypeCacheEntry::typtype.

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

◆ SharedRecordTypmodRegistryAttach()

void SharedRecordTypmodRegistryAttach ( SharedRecordTypmodRegistry registry)

Definition at line 2204 of file typcache.c.

2205 {
2206  MemoryContext old_context;
2207  dshash_table *record_table;
2208  dshash_table *typmod_table;
2209 
2211 
2212  /* We can't already be attached to a shared registry. */
2213  Assert(CurrentSession != NULL);
2214  Assert(CurrentSession->segment != NULL);
2215  Assert(CurrentSession->area != NULL);
2219 
2220  /*
2221  * We can't already have typmods in our local cache, because they'd clash
2222  * with those imported by SharedRecordTypmodRegistryInit. This should be
2223  * a freshly started parallel worker. If we ever support worker
2224  * recycling, a worker would need to zap its local cache in between
2225  * servicing different queries, in order to be able to call this and
2226  * synchronize typmods with a new leader; but that's problematic because
2227  * we can't be very sure that record-typmod-related state hasn't escaped
2228  * to anywhere else in the process.
2229  */
2230  Assert(NextRecordTypmod == 0);
2231 
2232  old_context = MemoryContextSwitchTo(TopMemoryContext);
2233 
2234  /* Attach to the two hash tables. */
2235  record_table = dshash_attach(CurrentSession->area,
2237  registry->record_table_handle,
2238  CurrentSession->area);
2239  typmod_table = dshash_attach(CurrentSession->area,
2241  registry->typmod_table_handle,
2242  NULL);
2243 
2244  MemoryContextSwitchTo(old_context);
2245 
2246  /*
2247  * Set up detach hook to run at worker exit. Currently this is the same
2248  * as the leader's detach hook, but in future they might need to be
2249  * different.
2250  */
2253  PointerGetDatum(registry));
2254 
2255  /*
2256  * Set up the session state that will tell assign_record_type_typmod and
2257  * lookup_rowtype_tupdesc_internal about the shared registry.
2258  */
2260  CurrentSession->shared_record_table = record_table;
2261  CurrentSession->shared_typmod_table = typmod_table;
2262 }
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1133
#define IsParallelWorker()
Definition: parallel.h:61
MemoryContext TopMemoryContext
Definition: mcxt.c:141
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
Session * CurrentSession
Definition: session.c:48
dsm_segment * segment
Definition: session.h:27
dshash_table * shared_record_table
Definition: session.h:32
struct SharedRecordTypmodRegistry * shared_typmod_registry
Definition: session.h:31
dsa_area * area
Definition: session.h:28
dshash_table * shared_typmod_table
Definition: session.h:33
dshash_table_handle typmod_table_handle
Definition: typcache.c:171
dshash_table_handle record_table_handle
Definition: typcache.c:169
static const dshash_parameters srtr_typmod_table_params
Definition: typcache.c:265
static void shared_record_typmod_registry_detach(dsm_segment *segment, Datum datum)
Definition: typcache.c:2870
static const dshash_parameters srtr_record_table_params
Definition: typcache.c:256

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

Referenced by AttachSession().

◆ SharedRecordTypmodRegistryEstimate()

size_t SharedRecordTypmodRegistryEstimate ( void  )

Definition at line 2083 of file typcache.c.

2084 {
2085  return sizeof(SharedRecordTypmodRegistry);
2086 }
struct SharedRecordTypmodRegistry SharedRecordTypmodRegistry
Definition: typcache.h:175

Referenced by GetSessionDsmHandle().

◆ SharedRecordTypmodRegistryInit()

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

Definition at line 2105 of file typcache.c.

2108 {
2109  MemoryContext old_context;
2110  dshash_table *record_table;
2111  dshash_table *typmod_table;
2112  int32 typmod;
2113 
2115 
2116  /* We can't already be attached to a shared registry. */
2120 
2121  old_context = MemoryContextSwitchTo(TopMemoryContext);
2122 
2123  /* Create the hash table of tuple descriptors indexed by themselves. */
2124  record_table = dshash_create(area, &srtr_record_table_params, area);
2125 
2126  /* Create the hash table of tuple descriptors indexed by typmod. */
2127  typmod_table = dshash_create(area, &srtr_typmod_table_params, NULL);
2128 
2129  MemoryContextSwitchTo(old_context);
2130 
2131  /* Initialize the SharedRecordTypmodRegistry. */
2132  registry->record_table_handle = dshash_get_hash_table_handle(record_table);
2133  registry->typmod_table_handle = dshash_get_hash_table_handle(typmod_table);
2135 
2136  /*
2137  * Copy all entries from this backend's private registry into the shared
2138  * registry.
2139  */
2140  for (typmod = 0; typmod < NextRecordTypmod; ++typmod)
2141  {
2142  SharedTypmodTableEntry *typmod_table_entry;
2143  SharedRecordTableEntry *record_table_entry;
2144  SharedRecordTableKey record_table_key;
2145  dsa_pointer shared_dp;
2146  TupleDesc tupdesc;
2147  bool found;
2148 
2149  tupdesc = RecordCacheArray[typmod].tupdesc;
2150  if (tupdesc == NULL)
2151  continue;
2152 
2153  /* Copy the TupleDesc into shared memory. */
2154  shared_dp = share_tupledesc(area, tupdesc, typmod);
2155 
2156  /* Insert into the typmod table. */
2157  typmod_table_entry = dshash_find_or_insert(typmod_table,
2158  &tupdesc->tdtypmod,
2159  &found);
2160  if (found)
2161  elog(ERROR, "cannot create duplicate shared record typmod");
2162  typmod_table_entry->typmod = tupdesc->tdtypmod;
2163  typmod_table_entry->shared_tupdesc = shared_dp;
2164  dshash_release_lock(typmod_table, typmod_table_entry);
2165 
2166  /* Insert into the record table. */
2167  record_table_key.shared = false;
2168  record_table_key.u.local_tupdesc = tupdesc;
2169  record_table_entry = dshash_find_or_insert(record_table,
2170  &record_table_key,
2171  &found);
2172  if (!found)
2173  {
2174  record_table_entry->key.shared = true;
2175  record_table_entry->key.u.shared_tupdesc = shared_dp;
2176  }
2177  dshash_release_lock(record_table, record_table_entry);
2178  }
2179 
2180  /*
2181  * Set up the global state that will tell assign_record_type_typmod and
2182  * lookup_rowtype_tupdesc_internal about the shared registry.
2183  */
2184  CurrentSession->shared_record_table = record_table;
2185  CurrentSession->shared_typmod_table = typmod_table;
2187 
2188  /*
2189  * We install a detach hook in the leader, but only to handle cleanup on
2190  * failure during GetSessionDsmHandle(). Once GetSessionDsmHandle() pins
2191  * the memory, the leader process will use a shared registry until it
2192  * exits.
2193  */
2195 }
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:218
signed int int32
Definition: c.h:483
uint64 dsa_pointer
Definition: dsa.h:62
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:367
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:206
SharedRecordTableKey key
Definition: typcache.c:198
TupleDesc local_tupdesc
Definition: typcache.c:186
union SharedRecordTableKey::@30 u
dsa_pointer shared_tupdesc
Definition: typcache.c:187
pg_atomic_uint32 next_typmod
Definition: typcache.c:173
dsa_pointer shared_tupdesc
Definition: typcache.c:208
static dsa_pointer share_tupledesc(dsa_area *area, TupleDesc tupdesc, uint32 typmod)
Definition: typcache.c:2737

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

Referenced by GetSessionDsmHandle().

◆ UpdateDomainConstraintRef()

void UpdateDomainConstraintRef ( DomainConstraintRef ref)

Definition at line 1348 of file typcache.c.

1349 {
1350  TypeCacheEntry *typentry = ref->tcache;
1351 
1352  /* Make sure typcache entry's data is up to date */
1353  if ((typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
1354  typentry->typtype == TYPTYPE_DOMAIN)
1355  load_domaintype_info(typentry);
1356 
1357  /* Transfer to ref object if there's new info, adjusting refcounts */
1358  if (ref->dcc != typentry->domainData)
1359  {
1360  /* Paranoia --- be sure link is nulled before trying to release */
1361  DomainConstraintCache *dcc = ref->dcc;
1362 
1363  if (dcc)
1364  {
1365  /*
1366  * Note: we just leak the previous list of executable domain
1367  * constraints. Alternatively, we could keep those in a child
1368  * context of ref->refctx and free that context at this point.
1369  * However, in practice this code path will be taken so seldom
1370  * that the extra bookkeeping for a child context doesn't seem
1371  * worthwhile; we'll just allow a leak for the lifespan of refctx.
1372  */
1373  ref->constraints = NIL;
1374  ref->dcc = NULL;
1375  decr_dcc_refcount(dcc);
1376  }
1377  dcc = typentry->domainData;
1378  if (dcc)
1379  {
1380  ref->dcc = dcc;
1381  dcc->dccRefCount++;
1382  if (ref->need_exprstate)
1384  ref->refctx);
1385  else
1386  ref->constraints = dcc->constraints;
1387  }
1388  }
1389 }
static void decr_dcc_refcount(DomainConstraintCache *dcc)
Definition: typcache.c:1240

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