PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
typcache.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/hash.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/nbtree.h"
#include "catalog/indexing.h"
#include "catalog/pg_am.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_enum.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "executor/executor.h"
#include "optimizer/planner.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for typcache.c:

Go to the source code of this file.

Data Structures

struct  DomainConstraintCache
 
struct  EnumItem
 
struct  TypeCacheEnumData
 
struct  RecordCacheEntry
 

Macros

#define TCFLAGS_CHECKED_BTREE_OPCLASS   0x0001
 
#define TCFLAGS_CHECKED_HASH_OPCLASS   0x0002
 
#define TCFLAGS_CHECKED_EQ_OPR   0x0004
 
#define TCFLAGS_CHECKED_LT_OPR   0x0008
 
#define TCFLAGS_CHECKED_GT_OPR   0x0010
 
#define TCFLAGS_CHECKED_CMP_PROC   0x0020
 
#define TCFLAGS_CHECKED_HASH_PROC   0x0040
 
#define TCFLAGS_CHECKED_ELEM_PROPERTIES   0x0080
 
#define TCFLAGS_HAVE_ELEM_EQUALITY   0x0100
 
#define TCFLAGS_HAVE_ELEM_COMPARE   0x0200
 
#define TCFLAGS_HAVE_ELEM_HASHING   0x0400
 
#define TCFLAGS_CHECKED_FIELD_PROPERTIES   0x0800
 
#define TCFLAGS_HAVE_FIELD_EQUALITY   0x1000
 
#define TCFLAGS_HAVE_FIELD_COMPARE   0x2000
 
#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS   0x4000
 
#define REC_HASH_KEYS   16 /* use this many columns in hash key */
 

Typedefs

typedef struct TypeCacheEnumData TypeCacheEnumData
 
typedef struct RecordCacheEntry RecordCacheEntry
 

Functions

static void load_typcache_tupdesc (TypeCacheEntry *typentry)
 
static void load_rangetype_info (TypeCacheEntry *typentry)
 
static void load_domaintype_info (TypeCacheEntry *typentry)
 
static int dcs_cmp (const void *a, const void *b)
 
static void decr_dcc_refcount (DomainConstraintCache *dcc)
 
static void dccref_deletion_callback (void *arg)
 
static Listprep_domain_constraints (List *constraints, MemoryContext execctx)
 
static bool array_element_has_equality (TypeCacheEntry *typentry)
 
static bool array_element_has_compare (TypeCacheEntry *typentry)
 
static bool array_element_has_hashing (TypeCacheEntry *typentry)
 
static void cache_array_element_properties (TypeCacheEntry *typentry)
 
static bool record_fields_have_equality (TypeCacheEntry *typentry)
 
static bool record_fields_have_compare (TypeCacheEntry *typentry)
 
static void cache_record_field_properties (TypeCacheEntry *typentry)
 
static void TypeCacheRelCallback (Datum arg, Oid relid)
 
static void TypeCacheOpcCallback (Datum arg, int cacheid, uint32 hashvalue)
 
static void TypeCacheConstrCallback (Datum arg, int cacheid, uint32 hashvalue)
 
static void load_enum_cache_data (TypeCacheEntry *tcache)
 
static EnumItemfind_enumitem (TypeCacheEnumData *enumdata, Oid arg)
 
static int enum_oid_cmp (const void *left, const void *right)
 
TypeCacheEntrylookup_type_cache (Oid type_id, int flags)
 
void InitDomainConstraintRef (Oid type_id, DomainConstraintRef *ref, MemoryContext refctx)
 
void UpdateDomainConstraintRef (DomainConstraintRef *ref)
 
bool DomainHasConstraints (Oid type_id)
 
static TupleDesc lookup_rowtype_tupdesc_internal (Oid type_id, int32 typmod, bool noError)
 
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)
 
void assign_record_type_typmod (TupleDesc tupDesc)
 
static bool enum_known_sorted (TypeCacheEnumData *enumdata, Oid arg)
 
int compare_values_of_enum (TypeCacheEntry *tcache, Oid arg1, Oid arg2)
 

Variables

static HTABTypeCacheHash = NULL
 
static TypeCacheEntryfirstDomainTypeEntry = NULL
 
static HTABRecordCacheHash = NULL
 
static TupleDescRecordCacheArray = NULL
 
static int32 RecordCacheArrayLen = 0
 
static int32 NextRecordTypmod = 0
 

Macro Definition Documentation

#define REC_HASH_KEYS   16 /* use this many columns in hash key */

Definition at line 140 of file typcache.c.

Referenced by assign_record_type_typmod().

#define TCFLAGS_CHECKED_BTREE_OPCLASS   0x0001

Definition at line 78 of file typcache.c.

Referenced by lookup_type_cache().

#define TCFLAGS_CHECKED_CMP_PROC   0x0020

Definition at line 83 of file typcache.c.

Referenced by lookup_type_cache().

#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS   0x4000
#define TCFLAGS_CHECKED_ELEM_PROPERTIES   0x0080
#define TCFLAGS_CHECKED_EQ_OPR   0x0004

Definition at line 80 of file typcache.c.

Referenced by lookup_type_cache().

#define TCFLAGS_CHECKED_FIELD_PROPERTIES   0x0800
#define TCFLAGS_CHECKED_GT_OPR   0x0010

Definition at line 82 of file typcache.c.

Referenced by lookup_type_cache().

#define TCFLAGS_CHECKED_HASH_OPCLASS   0x0002

Definition at line 79 of file typcache.c.

Referenced by lookup_type_cache().

#define TCFLAGS_CHECKED_HASH_PROC   0x0040

Definition at line 84 of file typcache.c.

Referenced by lookup_type_cache().

#define TCFLAGS_CHECKED_LT_OPR   0x0008

Definition at line 81 of file typcache.c.

Referenced by lookup_type_cache().

#define TCFLAGS_HAVE_ELEM_COMPARE   0x0200

Definition at line 87 of file typcache.c.

Referenced by array_element_has_compare(), and cache_array_element_properties().

#define TCFLAGS_HAVE_ELEM_EQUALITY   0x0100

Definition at line 86 of file typcache.c.

Referenced by array_element_has_equality(), and cache_array_element_properties().

#define TCFLAGS_HAVE_ELEM_HASHING   0x0400

Definition at line 88 of file typcache.c.

Referenced by array_element_has_hashing(), and cache_array_element_properties().

#define TCFLAGS_HAVE_FIELD_COMPARE   0x2000

Definition at line 91 of file typcache.c.

Referenced by cache_record_field_properties(), and record_fields_have_compare().

#define TCFLAGS_HAVE_FIELD_EQUALITY   0x1000

Definition at line 90 of file typcache.c.

Referenced by cache_record_field_properties(), and record_fields_have_equality().

Typedef Documentation

Function Documentation

static bool array_element_has_compare ( TypeCacheEntry typentry)
static

Definition at line 1082 of file typcache.c.

References cache_array_element_properties(), TypeCacheEntry::flags, TCFLAGS_CHECKED_ELEM_PROPERTIES, and TCFLAGS_HAVE_ELEM_COMPARE.

Referenced by lookup_type_cache().

1083 {
1084  if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1086  return (typentry->flags & TCFLAGS_HAVE_ELEM_COMPARE) != 0;
1087 }
#define TCFLAGS_HAVE_ELEM_COMPARE
Definition: typcache.c:87
#define TCFLAGS_CHECKED_ELEM_PROPERTIES
Definition: typcache.c:85
static void cache_array_element_properties(TypeCacheEntry *typentry)
Definition: typcache.c:1098
static bool array_element_has_equality ( TypeCacheEntry typentry)
static

Definition at line 1074 of file typcache.c.

References cache_array_element_properties(), TypeCacheEntry::flags, TCFLAGS_CHECKED_ELEM_PROPERTIES, and TCFLAGS_HAVE_ELEM_EQUALITY.

Referenced by lookup_type_cache().

1075 {
1076  if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1078  return (typentry->flags & TCFLAGS_HAVE_ELEM_EQUALITY) != 0;
1079 }
#define TCFLAGS_CHECKED_ELEM_PROPERTIES
Definition: typcache.c:85
static void cache_array_element_properties(TypeCacheEntry *typentry)
Definition: typcache.c:1098
#define TCFLAGS_HAVE_ELEM_EQUALITY
Definition: typcache.c:86
static bool array_element_has_hashing ( TypeCacheEntry typentry)
static

Definition at line 1090 of file typcache.c.

References cache_array_element_properties(), TypeCacheEntry::flags, TCFLAGS_CHECKED_ELEM_PROPERTIES, and TCFLAGS_HAVE_ELEM_HASHING.

Referenced by lookup_type_cache().

1091 {
1092  if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1094  return (typentry->flags & TCFLAGS_HAVE_ELEM_HASHING) != 0;
1095 }
#define TCFLAGS_CHECKED_ELEM_PROPERTIES
Definition: typcache.c:85
static void cache_array_element_properties(TypeCacheEntry *typentry)
Definition: typcache.c:1098
#define TCFLAGS_HAVE_ELEM_HASHING
Definition: typcache.c:88
void assign_record_type_typmod ( TupleDesc  tupDesc)

Definition at line 1296 of file typcache.c.

References Assert, tupleDesc::attrs, CacheMemoryContext, CreateCacheMemoryContext(), CreateTupleDescCopy(), HASHCTL::entrysize, equalTupleDescs(), HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), i, HASHCTL::keysize, lcons(), lfirst, MemoryContextSwitchTo(), MemSet, tupleDesc::natts, NextRecordTypmod, NIL, NULL, palloc(), REC_HASH_KEYS, RecordCacheArrayLen, RECORDOID, repalloc(), tupleDesc::tdrefcount, tupleDesc::tdtypeid, tupleDesc::tdtypmod, and RecordCacheEntry::tupdescs.

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

1297 {
1298  RecordCacheEntry *recentry;
1299  TupleDesc entDesc;
1300  Oid hashkey[REC_HASH_KEYS];
1301  bool found;
1302  int i;
1303  ListCell *l;
1304  int32 newtypmod;
1305  MemoryContext oldcxt;
1306 
1307  Assert(tupDesc->tdtypeid == RECORDOID);
1308 
1309  if (RecordCacheHash == NULL)
1310  {
1311  /* First time through: initialize the hash table */
1312  HASHCTL ctl;
1313 
1314  MemSet(&ctl, 0, sizeof(ctl));
1315  ctl.keysize = REC_HASH_KEYS * sizeof(Oid);
1316  ctl.entrysize = sizeof(RecordCacheEntry);
1317  RecordCacheHash = hash_create("Record information cache", 64,
1318  &ctl, HASH_ELEM | HASH_BLOBS);
1319 
1320  /* Also make sure CacheMemoryContext exists */
1321  if (!CacheMemoryContext)
1323  }
1324 
1325  /* Find or create a hashtable entry for this hash class */
1326  MemSet(hashkey, 0, sizeof(hashkey));
1327  for (i = 0; i < tupDesc->natts; i++)
1328  {
1329  if (i >= REC_HASH_KEYS)
1330  break;
1331  hashkey[i] = tupDesc->attrs[i]->atttypid;
1332  }
1334  (void *) hashkey,
1335  HASH_ENTER, &found);
1336  if (!found)
1337  {
1338  /* New entry ... hash_search initialized only the hash key */
1339  recentry->tupdescs = NIL;
1340  }
1341 
1342  /* Look for existing record cache entry */
1343  foreach(l, recentry->tupdescs)
1344  {
1345  entDesc = (TupleDesc) lfirst(l);
1346  if (equalTupleDescs(tupDesc, entDesc))
1347  {
1348  tupDesc->tdtypmod = entDesc->tdtypmod;
1349  return;
1350  }
1351  }
1352 
1353  /* Not present, so need to manufacture an entry */
1355 
1356  if (RecordCacheArray == NULL)
1357  {
1358  RecordCacheArray = (TupleDesc *) palloc(64 * sizeof(TupleDesc));
1359  RecordCacheArrayLen = 64;
1360  }
1362  {
1363  int32 newlen = RecordCacheArrayLen * 2;
1364 
1366  newlen * sizeof(TupleDesc));
1367  RecordCacheArrayLen = newlen;
1368  }
1369 
1370  /* if fail in subrs, no damage except possibly some wasted memory... */
1371  entDesc = CreateTupleDescCopy(tupDesc);
1372  recentry->tupdescs = lcons(entDesc, recentry->tupdescs);
1373  /* mark it as a reference-counted tupdesc */
1374  entDesc->tdrefcount = 1;
1375  /* now it's safe to advance NextRecordTypmod */
1376  newtypmod = NextRecordTypmod++;
1377  entDesc->tdtypmod = newtypmod;
1378  RecordCacheArray[newtypmod] = entDesc;
1379 
1380  /* report to caller as well */
1381  tupDesc->tdtypmod = newtypmod;
1382 
1383  MemoryContextSwitchTo(oldcxt);
1384 }
#define NIL
Definition: pg_list.h:69
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
Oid tdtypeid
Definition: tupdesc.h:77
#define HASH_ELEM
Definition: hsearch.h:87
List * tupdescs
Definition: typcache.c:148
struct RecordCacheEntry RecordCacheEntry
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:852
static HTAB * RecordCacheHash
Definition: typcache.c:151
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
int32 tdtypmod
Definition: tupdesc.h:78
signed int int32
Definition: c.h:253
static TupleDesc * RecordCacheArray
Definition: typcache.c:153
#define REC_HASH_KEYS
Definition: typcache.c:140
#define RECORDOID
Definition: pg_type.h:668
struct tupleDesc * TupleDesc
#define HASH_BLOBS
Definition: hsearch.h:88
static int32 RecordCacheArrayLen
Definition: typcache.c:154
static int32 NextRecordTypmod
Definition: typcache.c:155
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
Size keysize
Definition: hsearch.h:72
List * lcons(void *datum, List *list)
Definition: list.c:259
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
void CreateCacheMemoryContext(void)
Definition: catcache.c:525
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
void * palloc(Size size)
Definition: mcxt.c:891
int tdrefcount
Definition: tupdesc.h:80
int i
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:352
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
static void cache_array_element_properties ( TypeCacheEntry typentry)
static

Definition at line 1098 of file typcache.c.

References TypeCacheEntry::cmp_proc, TypeCacheEntry::eq_opr, TypeCacheEntry::flags, get_base_element_type(), TypeCacheEntry::hash_proc, lookup_type_cache(), OidIsValid, TCFLAGS_CHECKED_ELEM_PROPERTIES, TCFLAGS_HAVE_ELEM_COMPARE, TCFLAGS_HAVE_ELEM_EQUALITY, TCFLAGS_HAVE_ELEM_HASHING, TypeCacheEntry::type_id, TYPECACHE_CMP_PROC, TYPECACHE_EQ_OPR, and TYPECACHE_HASH_PROC.

Referenced by array_element_has_compare(), array_element_has_equality(), and array_element_has_hashing().

1099 {
1100  Oid elem_type = get_base_element_type(typentry->type_id);
1101 
1102  if (OidIsValid(elem_type))
1103  {
1104  TypeCacheEntry *elementry;
1105 
1106  elementry = lookup_type_cache(elem_type,
1110  if (OidIsValid(elementry->eq_opr))
1111  typentry->flags |= TCFLAGS_HAVE_ELEM_EQUALITY;
1112  if (OidIsValid(elementry->cmp_proc))
1113  typentry->flags |= TCFLAGS_HAVE_ELEM_COMPARE;
1114  if (OidIsValid(elementry->hash_proc))
1115  typentry->flags |= TCFLAGS_HAVE_ELEM_HASHING;
1116  }
1118 }
#define TCFLAGS_HAVE_ELEM_COMPARE
Definition: typcache.c:87
#define TYPECACHE_EQ_OPR
Definition: typcache.h:110
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:533
#define TCFLAGS_CHECKED_ELEM_PROPERTIES
Definition: typcache.c:85
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define TYPECACHE_CMP_PROC
Definition: typcache.h:113
#define TCFLAGS_HAVE_ELEM_EQUALITY
Definition: typcache.c:86
#define TCFLAGS_HAVE_ELEM_HASHING
Definition: typcache.c:88
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2525
#define TYPECACHE_HASH_PROC
Definition: typcache.h:114
static void cache_record_field_properties ( TypeCacheEntry typentry)
static

Definition at line 1137 of file typcache.c.

References tupleDesc::attrs, TypeCacheEntry::cmp_proc, DecrTupleDescRefCount(), TypeCacheEntry::eq_opr, TypeCacheEntry::flags, i, IncrTupleDescRefCount(), load_typcache_tupdesc(), lookup_type_cache(), tupleDesc::natts, NULL, OidIsValid, RECORDOID, TCFLAGS_CHECKED_FIELD_PROPERTIES, TCFLAGS_HAVE_FIELD_COMPARE, TCFLAGS_HAVE_FIELD_EQUALITY, TypeCacheEntry::tupDesc, TypeCacheEntry::type_id, TYPECACHE_CMP_PROC, TYPECACHE_EQ_OPR, TypeCacheEntry::typtype, and TYPTYPE_COMPOSITE.

Referenced by record_fields_have_compare(), and record_fields_have_equality().

1138 {
1139  /*
1140  * For type RECORD, we can't really tell what will work, since we don't
1141  * have access here to the specific anonymous type. Just assume that
1142  * everything will (we may get a failure at runtime ...)
1143  */
1144  if (typentry->type_id == RECORDOID)
1145  typentry->flags |= (TCFLAGS_HAVE_FIELD_EQUALITY |
1147  else if (typentry->typtype == TYPTYPE_COMPOSITE)
1148  {
1149  TupleDesc tupdesc;
1150  int newflags;
1151  int i;
1152 
1153  /* Fetch composite type's tupdesc if we don't have it already */
1154  if (typentry->tupDesc == NULL)
1155  load_typcache_tupdesc(typentry);
1156  tupdesc = typentry->tupDesc;
1157 
1158  /* Must bump the refcount while we do additional catalog lookups */
1159  IncrTupleDescRefCount(tupdesc);
1160 
1161  /* Have each property if all non-dropped fields have the property */
1162  newflags = (TCFLAGS_HAVE_FIELD_EQUALITY |
1164  for (i = 0; i < tupdesc->natts; i++)
1165  {
1166  TypeCacheEntry *fieldentry;
1167 
1168  if (tupdesc->attrs[i]->attisdropped)
1169  continue;
1170 
1171  fieldentry = lookup_type_cache(tupdesc->attrs[i]->atttypid,
1174  if (!OidIsValid(fieldentry->eq_opr))
1175  newflags &= ~TCFLAGS_HAVE_FIELD_EQUALITY;
1176  if (!OidIsValid(fieldentry->cmp_proc))
1177  newflags &= ~TCFLAGS_HAVE_FIELD_COMPARE;
1178 
1179  /* We can drop out of the loop once we disprove all bits */
1180  if (newflags == 0)
1181  break;
1182  }
1183  typentry->flags |= newflags;
1184 
1185  DecrTupleDescRefCount(tupdesc);
1186  }
1188 }
static void load_typcache_tupdesc(TypeCacheEntry *typentry)
Definition: typcache.c:571
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:316
#define TCFLAGS_CHECKED_FIELD_PROPERTIES
Definition: typcache.c:89
#define TYPTYPE_COMPOSITE
Definition: pg_type.h:709
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define TYPECACHE_EQ_OPR
Definition: typcache.h:110
#define OidIsValid(objectId)
Definition: c.h:533
int natts
Definition: tupdesc.h:73
#define TCFLAGS_HAVE_FIELD_COMPARE
Definition: typcache.c:91
#define RECORDOID
Definition: pg_type.h:668
#define TCFLAGS_HAVE_FIELD_EQUALITY
Definition: typcache.c:90
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define TYPECACHE_CMP_PROC
Definition: typcache.h:113
char typtype
Definition: typcache.h:39
#define NULL
Definition: c.h:226
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:334
int i
TupleDesc tupDesc
Definition: typcache.h:76
int compare_values_of_enum ( TypeCacheEntry tcache,
Oid  arg1,
Oid  arg2 
)

Definition at line 1543 of file typcache.c.

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

Referenced by enum_cmp_internal().

1544 {
1545  TypeCacheEnumData *enumdata;
1546  EnumItem *item1;
1547  EnumItem *item2;
1548 
1549  /*
1550  * Equal OIDs are certainly equal --- this case was probably handled by
1551  * our caller, but we may as well check.
1552  */
1553  if (arg1 == arg2)
1554  return 0;
1555 
1556  /* Load up the cache if first time through */
1557  if (tcache->enumData == NULL)
1558  load_enum_cache_data(tcache);
1559  enumdata = tcache->enumData;
1560 
1561  /*
1562  * If both OIDs are known-sorted, we can just compare them directly.
1563  */
1564  if (enum_known_sorted(enumdata, arg1) &&
1565  enum_known_sorted(enumdata, arg2))
1566  {
1567  if (arg1 < arg2)
1568  return -1;
1569  else
1570  return 1;
1571  }
1572 
1573  /*
1574  * Slow path: we have to identify their actual sort-order positions.
1575  */
1576  item1 = find_enumitem(enumdata, arg1);
1577  item2 = find_enumitem(enumdata, arg2);
1578 
1579  if (item1 == NULL || item2 == NULL)
1580  {
1581  /*
1582  * We couldn't find one or both values. That means the enum has
1583  * changed under us, so re-initialize the cache and try again. We
1584  * don't bother retrying the known-sorted case in this path.
1585  */
1586  load_enum_cache_data(tcache);
1587  enumdata = tcache->enumData;
1588 
1589  item1 = find_enumitem(enumdata, arg1);
1590  item2 = find_enumitem(enumdata, arg2);
1591 
1592  /*
1593  * If we still can't find the values, complain: we must have corrupt
1594  * data.
1595  */
1596  if (item1 == NULL)
1597  elog(ERROR, "enum value %u not found in cache for enum %s",
1598  arg1, format_type_be(tcache->type_id));
1599  if (item2 == NULL)
1600  elog(ERROR, "enum value %u not found in cache for enum %s",
1601  arg2, format_type_be(tcache->type_id));
1602  }
1603 
1604  if (item1->sort_order < item2->sort_order)
1605  return -1;
1606  else if (item1->sort_order > item2->sort_order)
1607  return 1;
1608  else
1609  return 0;
1610 }
struct TypeCacheEnumData * enumData
Definition: typcache.h:103
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
#define ERROR
Definition: elog.h:43
static bool enum_known_sorted(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:1514
static EnumItem * find_enumitem(TypeCacheEnumData *enumdata, Oid arg)
Definition: typcache.c:1771
#define NULL
Definition: c.h:226
static void load_enum_cache_data(TypeCacheEntry *tcache)
Definition: typcache.c:1616
float4 sort_order
Definition: typcache.c:117
#define elog
Definition: elog.h:219
static void dccref_deletion_callback ( void *  arg)
static

Definition at line 912 of file typcache.c.

References DomainConstraintRef::constraints, DomainConstraintRef::dcc, decr_dcc_refcount(), NIL, and NULL.

Referenced by InitDomainConstraintRef().

913 {
915  DomainConstraintCache *dcc = ref->dcc;
916 
917  /* Paranoia --- be sure link is nulled before trying to release */
918  if (dcc)
919  {
920  ref->constraints = NIL;
921  ref->dcc = NULL;
922  decr_dcc_refcount(dcc);
923  }
924 }
#define NIL
Definition: pg_list.h:69
DomainConstraintCache * dcc
Definition: typcache.h:137
#define NULL
Definition: c.h:226
void * arg
static void decr_dcc_refcount(DomainConstraintCache *dcc)
Definition: typcache.c:901
static int dcs_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 888 of file typcache.c.

Referenced by load_domaintype_info().

889 {
890  const DomainConstraintState *const * ca = (const DomainConstraintState *const *) a;
891  const DomainConstraintState *const * cb = (const DomainConstraintState *const *) b;
892 
893  return strcmp((*ca)->name, (*cb)->name);
894 }
static void decr_dcc_refcount ( DomainConstraintCache dcc)
static

Definition at line 901 of file typcache.c.

References Assert, DomainConstraintCache::dccContext, DomainConstraintCache::dccRefCount, and MemoryContextDelete().

Referenced by dccref_deletion_callback(), load_domaintype_info(), and UpdateDomainConstraintRef().

902 {
903  Assert(dcc->dccRefCount > 0);
904  if (--(dcc->dccRefCount) <= 0)
906 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
MemoryContext dccContext
Definition: typcache.c:109
#define Assert(condition)
Definition: c.h:670
bool DomainHasConstraints ( Oid  type_id)

Definition at line 1047 of file typcache.c.

References TypeCacheEntry::domainData, lookup_type_cache(), NULL, and TYPECACHE_DOMAIN_INFO.

Referenced by ATColumnChangeRequiresRewrite(), and ATExecAddColumn().

1048 {
1049  TypeCacheEntry *typentry;
1050 
1051  /*
1052  * Note: a side effect is to cause the typcache's domain data to become
1053  * valid. This is fine since we'll likely need it soon if there is any.
1054  */
1055  typentry = lookup_type_cache(type_id, TYPECACHE_DOMAIN_INFO);
1056 
1057  return (typentry->domainData != NULL);
1058 }
DomainConstraintCache * domainData
Definition: typcache.h:94
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define NULL
Definition: c.h:226
#define TYPECACHE_DOMAIN_INFO
Definition: typcache.h:122
static bool enum_known_sorted ( TypeCacheEnumData enumdata,
Oid  arg 
)
inlinestatic

Definition at line 1514 of file typcache.c.

References TypeCacheEnumData::bitmap_base, bms_is_member(), and TypeCacheEnumData::sorted_values.

Referenced by compare_values_of_enum().

1515 {
1516  Oid offset;
1517 
1518  if (arg < enumdata->bitmap_base)
1519  return false;
1520  offset = arg - enumdata->bitmap_base;
1521  if (offset > (Oid) INT_MAX)
1522  return false;
1523  return bms_is_member((int) offset, enumdata->sorted_values);
1524 }
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * sorted_values
Definition: typcache.c:123
void * arg
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:419
static int enum_oid_cmp ( const void *  left,
const void *  right 
)
static

Definition at line 1788 of file typcache.c.

References EnumItem::enum_oid.

Referenced by find_enumitem(), and load_enum_cache_data().

1789 {
1790  const EnumItem *l = (const EnumItem *) left;
1791  const EnumItem *r = (const EnumItem *) right;
1792 
1793  if (l->enum_oid < r->enum_oid)
1794  return -1;
1795  else if (l->enum_oid > r->enum_oid)
1796  return 1;
1797  else
1798  return 0;
1799 }
Oid enum_oid
Definition: typcache.c:116
static EnumItem * find_enumitem ( TypeCacheEnumData enumdata,
Oid  arg 
)
static

Definition at line 1771 of file typcache.c.

References arg, EnumItem::enum_oid, enum_oid_cmp(), TypeCacheEnumData::enum_values, NULL, and TypeCacheEnumData::num_values.

Referenced by compare_values_of_enum().

1772 {
1773  EnumItem srch;
1774 
1775  /* On some versions of Solaris, bsearch of zero items dumps core */
1776  if (enumdata->num_values <= 0)
1777  return NULL;
1778 
1779  srch.enum_oid = arg;
1780  return bsearch(&srch, enumdata->enum_values, enumdata->num_values,
1781  sizeof(EnumItem), enum_oid_cmp);
1782 }
EnumItem enum_values[FLEXIBLE_ARRAY_MEMBER]
Definition: typcache.c:125
Oid enum_oid
Definition: typcache.c:116
#define NULL
Definition: c.h:226
static int enum_oid_cmp(const void *left, const void *right)
Definition: typcache.c:1788
void * arg
void InitDomainConstraintRef ( Oid  type_id,
DomainConstraintRef ref,
MemoryContext  refctx 
)

Definition at line 967 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(), NIL, NULL, prep_domain_constraints(), DomainConstraintRef::refctx, DomainConstraintRef::tcache, and TYPECACHE_DOMAIN_INFO.

Referenced by domain_state_setup(), and ExecInitExpr().

969 {
970  /* Look up the typcache entry --- we assume it survives indefinitely */
972  /* For safety, establish the callback before acquiring a refcount */
973  ref->refctx = refctx;
974  ref->dcc = NULL;
976  ref->callback.arg = (void *) ref;
978  /* Acquire refcount if there are constraints, and set up exported list */
979  if (ref->tcache->domainData)
980  {
981  ref->dcc = ref->tcache->domainData;
982  ref->dcc->dccRefCount++;
984  ref->refctx);
985  }
986  else
987  ref->constraints = NIL;
988 }
MemoryContextCallback callback
Definition: typcache.h:138
MemoryContextCallbackFunction func
Definition: palloc.h:49
#define NIL
Definition: pg_list.h:69
DomainConstraintCache * dcc
Definition: typcache.h:137
static void dccref_deletion_callback(void *arg)
Definition: typcache.c:912
DomainConstraintCache * domainData
Definition: typcache.h:94
MemoryContext refctx
Definition: typcache.h:133
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define NULL
Definition: c.h:226
static List * prep_domain_constraints(List *constraints, MemoryContext execctx)
Definition: typcache.c:933
TypeCacheEntry * tcache
Definition: typcache.h:134
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:265
#define TYPECACHE_DOMAIN_INFO
Definition: typcache.h:122
static void load_domaintype_info ( TypeCacheEntry typentry)
static

Definition at line 662 of file typcache.c.

References AccessShareLock, ALLOCSET_SMALL_SIZES, AllocSetContextCreate(), Anum_pg_constraint_conbin, Anum_pg_constraint_contypid, BTEqualStrategyNumber, CacheMemoryContext, DomainConstraintState::check_expr, CONSTRAINT_CHECK, ConstraintRelationId, DomainConstraintCache::constraints, DomainConstraintState::constrainttype, ConstraintTypidIndexId, CurrentMemoryContext, DomainConstraintCache::dccContext, DomainConstraintCache::dccRefCount, dcs_cmp(), decr_dcc_refcount(), DOM_CONSTRAINT_CHECK, DOM_CONSTRAINT_NOTNULL, TypeCacheEntry::domainData, elog, ERROR, expression_planner(), fastgetattr, TypeCacheEntry::flags, GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, lcons(), makeNode, MemoryContextAlloc(), MemoryContextSetParent(), MemoryContextSwitchTo(), DomainConstraintState::name, NameStr, NIL, NULL, ObjectIdGetDatum, palloc(), pstrdup(), qsort, RelationData::rd_att, ReleaseSysCache(), repalloc(), ScanKeyInit(), SearchSysCache1, stringToNode(), systable_beginscan(), systable_endscan(), systable_getnext(), TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS, TextDatumGetCString, TypeCacheEntry::type_id, TYPEOID, TYPTYPE_DOMAIN, and val.

Referenced by lookup_type_cache(), and UpdateDomainConstraintRef().

663 {
664  Oid typeOid = typentry->type_id;
666  bool notNull = false;
667  DomainConstraintState **ccons;
668  int cconslen;
669  Relation conRel;
670  MemoryContext oldcxt;
671 
672  /*
673  * If we're here, any existing constraint info is stale, so release it.
674  * For safety, be sure to null the link before trying to delete the data.
675  */
676  if (typentry->domainData)
677  {
678  dcc = typentry->domainData;
679  typentry->domainData = NULL;
680  decr_dcc_refcount(dcc);
681  }
682 
683  /*
684  * We try to optimize the common case of no domain constraints, so don't
685  * create the dcc object and context until we find a constraint. Likewise
686  * for the temp sorting array.
687  */
688  dcc = NULL;
689  ccons = NULL;
690  cconslen = 0;
691 
692  /*
693  * Scan pg_constraint for relevant constraints. We want to find
694  * constraints for not just this domain, but any ancestor domains, so the
695  * outer loop crawls up the domain stack.
696  */
698 
699  for (;;)
700  {
701  HeapTuple tup;
702  HeapTuple conTup;
703  Form_pg_type typTup;
704  int nccons = 0;
705  ScanKeyData key[1];
706  SysScanDesc scan;
707 
708  tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeOid));
709  if (!HeapTupleIsValid(tup))
710  elog(ERROR, "cache lookup failed for type %u", typeOid);
711  typTup = (Form_pg_type) GETSTRUCT(tup);
712 
713  if (typTup->typtype != TYPTYPE_DOMAIN)
714  {
715  /* Not a domain, so done */
716  ReleaseSysCache(tup);
717  break;
718  }
719 
720  /* Test for NOT NULL Constraint */
721  if (typTup->typnotnull)
722  notNull = true;
723 
724  /* Look for CHECK Constraints on this domain */
725  ScanKeyInit(&key[0],
727  BTEqualStrategyNumber, F_OIDEQ,
728  ObjectIdGetDatum(typeOid));
729 
730  scan = systable_beginscan(conRel, ConstraintTypidIndexId, true,
731  NULL, 1, key);
732 
733  while (HeapTupleIsValid(conTup = systable_getnext(scan)))
734  {
736  Datum val;
737  bool isNull;
738  char *constring;
739  Expr *check_expr;
741 
742  /* Ignore non-CHECK constraints (presently, shouldn't be any) */
743  if (c->contype != CONSTRAINT_CHECK)
744  continue;
745 
746  /* Not expecting conbin to be NULL, but we'll test for it anyway */
748  conRel->rd_att, &isNull);
749  if (isNull)
750  elog(ERROR, "domain \"%s\" constraint \"%s\" has NULL conbin",
751  NameStr(typTup->typname), NameStr(c->conname));
752 
753  /* Convert conbin to C string in caller context */
754  constring = TextDatumGetCString(val);
755 
756  /* Create the DomainConstraintCache object and context if needed */
757  if (dcc == NULL)
758  {
759  MemoryContext cxt;
760 
762  "Domain constraints",
764  dcc = (DomainConstraintCache *)
766  dcc->constraints = NIL;
767  dcc->dccContext = cxt;
768  dcc->dccRefCount = 0;
769  }
770 
771  /* Create node trees in DomainConstraintCache's context */
772  oldcxt = MemoryContextSwitchTo(dcc->dccContext);
773 
774  check_expr = (Expr *) stringToNode(constring);
775 
776  /* ExecInitExpr will assume we've planned the expression */
777  check_expr = expression_planner(check_expr);
778 
781  r->name = pstrdup(NameStr(c->conname));
782  /* Must cast here because we're not storing an expr state node */
783  r->check_expr = (ExprState *) check_expr;
784 
785  MemoryContextSwitchTo(oldcxt);
786 
787  /* Accumulate constraints in an array, for sorting below */
788  if (ccons == NULL)
789  {
790  cconslen = 8;
791  ccons = (DomainConstraintState **)
792  palloc(cconslen * sizeof(DomainConstraintState *));
793  }
794  else if (nccons >= cconslen)
795  {
796  cconslen *= 2;
797  ccons = (DomainConstraintState **)
798  repalloc(ccons, cconslen * sizeof(DomainConstraintState *));
799  }
800  ccons[nccons++] = r;
801  }
802 
803  systable_endscan(scan);
804 
805  if (nccons > 0)
806  {
807  /*
808  * Sort the items for this domain, so that CHECKs are applied in a
809  * deterministic order.
810  */
811  if (nccons > 1)
812  qsort(ccons, nccons, sizeof(DomainConstraintState *), dcs_cmp);
813 
814  /*
815  * Now attach them to the overall list. Use lcons() here because
816  * constraints of parent domains should be applied earlier.
817  */
818  oldcxt = MemoryContextSwitchTo(dcc->dccContext);
819  while (nccons > 0)
820  dcc->constraints = lcons(ccons[--nccons], dcc->constraints);
821  MemoryContextSwitchTo(oldcxt);
822  }
823 
824  /* loop to next domain in stack */
825  typeOid = typTup->typbasetype;
826  ReleaseSysCache(tup);
827  }
828 
829  heap_close(conRel, AccessShareLock);
830 
831  /*
832  * Only need to add one NOT NULL check regardless of how many domains in
833  * the stack request it.
834  */
835  if (notNull)
836  {
838 
839  /* Create the DomainConstraintCache object and context if needed */
840  if (dcc == NULL)
841  {
842  MemoryContext cxt;
843 
845  "Domain constraints",
847  dcc = (DomainConstraintCache *)
849  dcc->constraints = NIL;
850  dcc->dccContext = cxt;
851  dcc->dccRefCount = 0;
852  }
853 
854  /* Create node trees in DomainConstraintCache's context */
855  oldcxt = MemoryContextSwitchTo(dcc->dccContext);
856 
858 
860  r->name = pstrdup("NOT NULL");
861  r->check_expr = NULL;
862 
863  /* lcons to apply the nullness check FIRST */
864  dcc->constraints = lcons(r, dcc->constraints);
865 
866  MemoryContextSwitchTo(oldcxt);
867  }
868 
869  /*
870  * If we made a constraint object, move it into CacheMemoryContext and
871  * attach it to the typcache entry.
872  */
873  if (dcc)
874  {
876  typentry->domainData = dcc;
877  dcc->dccRefCount++; /* count the typcache's reference */
878  }
879 
880  /* Either way, the typcache entry's domain data is now valid. */
882 }
#define NIL
Definition: pg_list.h:69
#define TYPTYPE_DOMAIN
Definition: pg_type.h:710
void * stringToNode(char *str)
Definition: read.c:38
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:493
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define fastgetattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:719
MemoryContext dccContext
Definition: typcache.c:109
DomainConstraintType constrainttype
Definition: execnodes.h:1022
DomainConstraintCache * domainData
Definition: typcache.h:94
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:317
char * pstrdup(const char *in)
Definition: mcxt.c:1165
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:155
Expr * expression_planner(Expr *expr)
Definition: planner.c:5211
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ExprState * check_expr
Definition: execnodes.h:1024
#define AccessShareLock
Definition: lockdefs.h:36
static int dcs_cmp(const void *a, const void *b)
Definition: typcache.c:888
#define heap_close(r, l)
Definition: heapam.h:97
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
#define Anum_pg_constraint_conbin
#define ConstraintTypidIndexId
Definition: indexing.h:128
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:322
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
#define CONSTRAINT_CHECK
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:410
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
char * c
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define Anum_pg_constraint_contypid
#define TextDatumGetCString(d)
Definition: builtins.h:91
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
uintptr_t Datum
Definition: postgres.h:374
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
TupleDesc rd_att
Definition: rel.h:114
List * lcons(void *datum, List *list)
Definition: list.c:259
#define makeNode(_type_)
Definition: nodes.h:556
FormData_pg_constraint * Form_pg_constraint
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS
Definition: typcache.c:92
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
void * palloc(Size size)
Definition: mcxt.c:891
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
#define NameStr(name)
Definition: c.h:494
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define ConstraintRelationId
Definition: pg_constraint.h:29
#define elog
Definition: elog.h:219
#define qsort(a, b, c, d)
Definition: port.h:440
static void decr_dcc_refcount(DomainConstraintCache *dcc)
Definition: typcache.c:901
long val
Definition: informix.c:689
#define BTEqualStrategyNumber
Definition: stratnum.h:31
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
static void load_enum_cache_data ( TypeCacheEntry tcache)
static

Definition at line 1616 of file typcache.c.

References AccessShareLock, Anum_pg_enum_enumtypid, TypeCacheEnumData::bitmap_base, bms_add_member(), bms_copy(), bms_free(), bms_make_singleton(), BTEqualStrategyNumber, CacheMemoryContext, EnumItem::enum_oid, enum_oid_cmp(), TypeCacheEnumData::enum_values, TypeCacheEntry::enumData, EnumRelationId, EnumTypIdLabelIndexId, ereport, errcode(), errmsg(), ERROR, format_type_be(), GETSTRUCT, heap_close, heap_open(), HeapTupleGetOid, HeapTupleIsValid, i, InvalidOid, MemoryContextSwitchTo(), NULL, TypeCacheEnumData::num_values, ObjectIdGetDatum, offsetof, palloc(), pfree(), qsort, repalloc(), ScanKeyInit(), EnumItem::sort_order, TypeCacheEnumData::sorted_values, systable_beginscan(), systable_endscan(), systable_getnext(), TypeCacheEntry::type_id, TypeCacheEntry::typtype, and TYPTYPE_ENUM.

Referenced by compare_values_of_enum().

1617 {
1618  TypeCacheEnumData *enumdata;
1619  Relation enum_rel;
1620  SysScanDesc enum_scan;
1621  HeapTuple enum_tuple;
1622  ScanKeyData skey;
1623  EnumItem *items;
1624  int numitems;
1625  int maxitems;
1626  Oid bitmap_base;
1627  Bitmapset *bitmap;
1628  MemoryContext oldcxt;
1629  int bm_size,
1630  start_pos;
1631 
1632  /* Check that this is actually an enum */
1633  if (tcache->typtype != TYPTYPE_ENUM)
1634  ereport(ERROR,
1635  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1636  errmsg("%s is not an enum",
1637  format_type_be(tcache->type_id))));
1638 
1639  /*
1640  * Read all the information for members of the enum type. We collect the
1641  * info in working memory in the caller's context, and then transfer it to
1642  * permanent memory in CacheMemoryContext. This minimizes the risk of
1643  * leaking memory from CacheMemoryContext in the event of an error partway
1644  * through.
1645  */
1646  maxitems = 64;
1647  items = (EnumItem *) palloc(sizeof(EnumItem) * maxitems);
1648  numitems = 0;
1649 
1650  /* Scan pg_enum for the members of the target enum type. */
1651  ScanKeyInit(&skey,
1653  BTEqualStrategyNumber, F_OIDEQ,
1654  ObjectIdGetDatum(tcache->type_id));
1655 
1657  enum_scan = systable_beginscan(enum_rel,
1659  true, NULL,
1660  1, &skey);
1661 
1662  while (HeapTupleIsValid(enum_tuple = systable_getnext(enum_scan)))
1663  {
1664  Form_pg_enum en = (Form_pg_enum) GETSTRUCT(enum_tuple);
1665 
1666  if (numitems >= maxitems)
1667  {
1668  maxitems *= 2;
1669  items = (EnumItem *) repalloc(items, sizeof(EnumItem) * maxitems);
1670  }
1671  items[numitems].enum_oid = HeapTupleGetOid(enum_tuple);
1672  items[numitems].sort_order = en->enumsortorder;
1673  numitems++;
1674  }
1675 
1676  systable_endscan(enum_scan);
1677  heap_close(enum_rel, AccessShareLock);
1678 
1679  /* Sort the items into OID order */
1680  qsort(items, numitems, sizeof(EnumItem), enum_oid_cmp);
1681 
1682  /*
1683  * Here, we create a bitmap listing a subset of the enum's OIDs that are
1684  * known to be in order and can thus be compared with just OID comparison.
1685  *
1686  * The point of this is that the enum's initial OIDs were certainly in
1687  * order, so there is some subset that can be compared via OID comparison;
1688  * and we'd rather not do binary searches unnecessarily.
1689  *
1690  * This is somewhat heuristic, and might identify a subset of OIDs that
1691  * isn't exactly what the type started with. That's okay as long as the
1692  * subset is correctly sorted.
1693  */
1694  bitmap_base = InvalidOid;
1695  bitmap = NULL;
1696  bm_size = 1; /* only save sets of at least 2 OIDs */
1697 
1698  for (start_pos = 0; start_pos < numitems - 1; start_pos++)
1699  {
1700  /*
1701  * Identify longest sorted subsequence starting at start_pos
1702  */
1703  Bitmapset *this_bitmap = bms_make_singleton(0);
1704  int this_bm_size = 1;
1705  Oid start_oid = items[start_pos].enum_oid;
1706  float4 prev_order = items[start_pos].sort_order;
1707  int i;
1708 
1709  for (i = start_pos + 1; i < numitems; i++)
1710  {
1711  Oid offset;
1712 
1713  offset = items[i].enum_oid - start_oid;
1714  /* quit if bitmap would be too large; cutoff is arbitrary */
1715  if (offset >= 8192)
1716  break;
1717  /* include the item if it's in-order */
1718  if (items[i].sort_order > prev_order)
1719  {
1720  prev_order = items[i].sort_order;
1721  this_bitmap = bms_add_member(this_bitmap, (int) offset);
1722  this_bm_size++;
1723  }
1724  }
1725 
1726  /* Remember it if larger than previous best */
1727  if (this_bm_size > bm_size)
1728  {
1729  bms_free(bitmap);
1730  bitmap_base = start_oid;
1731  bitmap = this_bitmap;
1732  bm_size = this_bm_size;
1733  }
1734  else
1735  bms_free(this_bitmap);
1736 
1737  /*
1738  * Done if it's not possible to find a longer sequence in the rest of
1739  * the list. In typical cases this will happen on the first
1740  * iteration, which is why we create the bitmaps on the fly instead of
1741  * doing a second pass over the list.
1742  */
1743  if (bm_size >= (numitems - start_pos - 1))
1744  break;
1745  }
1746 
1747  /* OK, copy the data into CacheMemoryContext */
1749  enumdata = (TypeCacheEnumData *)
1750  palloc(offsetof(TypeCacheEnumData, enum_values) +
1751  numitems * sizeof(EnumItem));
1752  enumdata->bitmap_base = bitmap_base;
1753  enumdata->sorted_values = bms_copy(bitmap);
1754  enumdata->num_values = numitems;
1755  memcpy(enumdata->enum_values, items, numitems * sizeof(EnumItem));
1756  MemoryContextSwitchTo(oldcxt);
1757 
1758  pfree(items);
1759  bms_free(bitmap);
1760 
1761  /* And link the finished cache struct into the typcache */
1762  if (tcache->enumData != NULL)
1763  pfree(tcache->enumData);
1764  tcache->enumData = enumdata;
1765 }
struct TypeCacheEnumData * enumData
Definition: typcache.h:103
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:493
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:110
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
#define EnumTypIdLabelIndexId
Definition: indexing.h:157
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:322
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:410
Bitmapset * sorted_values
Definition: typcache.c:123
void pfree(void *pointer)
Definition: mcxt.c:992
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define EnumRelationId
Definition: pg_enum.h:32
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:178
FormData_pg_enum * Form_pg_enum
Definition: pg_enum.h:46
#define ereport(elevel, rest)
Definition: elog.h:122
EnumItem enum_values[FLEXIBLE_ARRAY_MEMBER]
Definition: typcache.c:125
float float4
Definition: c.h:376
Oid enum_oid
Definition: typcache.c:116
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define InvalidOid
Definition: postgres_ext.h:36
char typtype
Definition: typcache.h:39
void bms_free(Bitmapset *a)
Definition: bitmapset.c:200
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
#define Anum_pg_enum_enumtypid
Definition: pg_enum.h:53
float4 sort_order
Definition: typcache.c:117
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
static int enum_oid_cmp(const void *left, const void *right)
Definition: typcache.c:1788
int i
#define TYPTYPE_ENUM
Definition: pg_type.h:711
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define qsort(a, b, c, d)
Definition: port.h:440
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define offsetof(type, field)
Definition: c.h:550
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
static void load_rangetype_info ( TypeCacheEntry typentry)
static

Definition at line 599 of file typcache.c.

References BTORDER_PROC, CacheMemoryContext, elog, ERROR, fmgr_info_cxt(), get_opclass_family(), get_opclass_input_type(), get_opfamily_proc(), GETSTRUCT, HeapTupleIsValid, lookup_type_cache(), ObjectIdGetDatum, OidIsValid, RANGETYPE, RegProcedureIsValid, ReleaseSysCache(), TypeCacheEntry::rng_canonical_finfo, TypeCacheEntry::rng_cmp_proc_finfo, TypeCacheEntry::rng_collation, TypeCacheEntry::rng_subdiff_finfo, TypeCacheEntry::rngelemtype, SearchSysCache1, and TypeCacheEntry::type_id.

Referenced by lookup_type_cache().

600 {
601  Form_pg_range pg_range;
602  HeapTuple tup;
603  Oid subtypeOid;
604  Oid opclassOid;
605  Oid canonicalOid;
606  Oid subdiffOid;
607  Oid opfamilyOid;
608  Oid opcintype;
609  Oid cmpFnOid;
610 
611  /* get information from pg_range */
613  /* should not fail, since we already checked typtype ... */
614  if (!HeapTupleIsValid(tup))
615  elog(ERROR, "cache lookup failed for range type %u",
616  typentry->type_id);
617  pg_range = (Form_pg_range) GETSTRUCT(tup);
618 
619  subtypeOid = pg_range->rngsubtype;
620  typentry->rng_collation = pg_range->rngcollation;
621  opclassOid = pg_range->rngsubopc;
622  canonicalOid = pg_range->rngcanonical;
623  subdiffOid = pg_range->rngsubdiff;
624 
625  ReleaseSysCache(tup);
626 
627  /* get opclass properties and look up the comparison function */
628  opfamilyOid = get_opclass_family(opclassOid);
629  opcintype = get_opclass_input_type(opclassOid);
630 
631  cmpFnOid = get_opfamily_proc(opfamilyOid, opcintype, opcintype,
632  BTORDER_PROC);
633  if (!RegProcedureIsValid(cmpFnOid))
634  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
635  BTORDER_PROC, opcintype, opcintype, opfamilyOid);
636 
637  /* set up cached fmgrinfo structs */
638  fmgr_info_cxt(cmpFnOid, &typentry->rng_cmp_proc_finfo,
640  if (OidIsValid(canonicalOid))
641  fmgr_info_cxt(canonicalOid, &typentry->rng_canonical_finfo,
643  if (OidIsValid(subdiffOid))
644  fmgr_info_cxt(subdiffOid, &typentry->rng_subdiff_finfo,
646 
647  /* Lastly, set up link to the element type --- this marks data valid */
648  typentry->rngelemtype = lookup_type_cache(subtypeOid, 0);
649 }
FormData_pg_range * Form_pg_range
Definition: pg_range.h:49
FmgrInfo rng_cmp_proc_finfo
Definition: typcache.h:86
#define BTORDER_PROC
Definition: nbtree.h:229
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:533
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
FmgrInfo rng_subdiff_finfo
Definition: typcache.h:88
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define RegProcedureIsValid(p)
Definition: c.h:535
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:169
FmgrInfo rng_canonical_finfo
Definition: typcache.h:87
struct TypeCacheEntry * rngelemtype
Definition: typcache.h:84
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
Oid rng_collation
Definition: typcache.h:85
Oid get_opclass_family(Oid opclass)
Definition: lsyscache.c:1015
#define elog
Definition: elog.h:219
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1037
static void load_typcache_tupdesc ( TypeCacheEntry typentry)
static

Definition at line 571 of file typcache.c.

References AccessShareLock, Assert, elog, ERROR, OidIsValid, RelationData::rd_rel, relation_close(), relation_open(), RelationGetDescr, tupleDesc::tdrefcount, TypeCacheEntry::tupDesc, TypeCacheEntry::type_id, and TypeCacheEntry::typrelid.

Referenced by cache_record_field_properties(), and lookup_type_cache().

572 {
573  Relation rel;
574 
575  if (!OidIsValid(typentry->typrelid)) /* should not happen */
576  elog(ERROR, "invalid typrelid for composite type %u",
577  typentry->type_id);
578  rel = relation_open(typentry->typrelid, AccessShareLock);
579  Assert(rel->rd_rel->reltype == typentry->type_id);
580 
581  /*
582  * Link to the tupdesc and increment its refcount (we assert it's a
583  * refcounted descriptor). We don't use IncrTupleDescRefCount() for this,
584  * because the reference mustn't be entered in the current resource owner;
585  * it can outlive the current query.
586  */
587  typentry->tupDesc = RelationGetDescr(rel);
588 
589  Assert(typentry->tupDesc->tdrefcount > 0);
590  typentry->tupDesc->tdrefcount++;
591 
593 }
#define RelationGetDescr(relation)
Definition: rel.h:425
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
Form_pg_class rd_rel
Definition: rel.h:113
#define OidIsValid(objectId)
Definition: c.h:533
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:670
int tdrefcount
Definition: tupdesc.h:80
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1117
#define elog
Definition: elog.h:219
TupleDesc tupDesc
Definition: typcache.h:76
TupleDesc lookup_rowtype_tupdesc_copy ( Oid  type_id,
int32  typmod 
)

Definition at line 1279 of file typcache.c.

References CreateTupleDescCopyConstr(), and lookup_rowtype_tupdesc_internal().

Referenced by ExecInitExpr(), ExecMakeTableFunctionResult(), expandRecordVariable(), ExpandRowReference(), get_expr_result_type(), get_name_for_var_field(), internal_get_result_type(), and TypeGetTupleDesc().

1280 {
1281  TupleDesc tmp;
1282 
1283  tmp = lookup_rowtype_tupdesc_internal(type_id, typmod, false);
1284  return CreateTupleDescCopyConstr(tmp);
1285 }
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:167
static TupleDesc lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1198
static TupleDesc lookup_rowtype_tupdesc_internal ( Oid  type_id,
int32  typmod,
bool  noError 
)
static

Definition at line 1198 of file typcache.c.

References ereport, errcode(), errmsg(), ERROR, format_type_be(), lookup_type_cache(), NextRecordTypmod, NULL, RECORDOID, TypeCacheEntry::tupDesc, and TYPECACHE_TUPDESC.

Referenced by lookup_rowtype_tupdesc(), lookup_rowtype_tupdesc_copy(), and lookup_rowtype_tupdesc_noerror().

1199 {
1200  if (type_id != RECORDOID)
1201  {
1202  /*
1203  * It's a named composite type, so use the regular typcache.
1204  */
1205  TypeCacheEntry *typentry;
1206 
1207  typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
1208  if (typentry->tupDesc == NULL && !noError)
1209  ereport(ERROR,
1210  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1211  errmsg("type %s is not composite",
1212  format_type_be(type_id))));
1213  return typentry->tupDesc;
1214  }
1215  else
1216  {
1217  /*
1218  * It's a transient record type, so look in our record-type table.
1219  */
1220  if (typmod < 0 || typmod >= NextRecordTypmod)
1221  {
1222  if (!noError)
1223  ereport(ERROR,
1224  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1225  errmsg("record type has not been registered")));
1226  return NULL;
1227  }
1228  return RecordCacheArray[typmod];
1229  }
1230 }
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
static TupleDesc * RecordCacheArray
Definition: typcache.c:153
#define ERROR
Definition: elog.h:43
#define RECORDOID
Definition: pg_type.h:668
#define ereport(elevel, rest)
Definition: elog.h:122
static int32 NextRecordTypmod
Definition: typcache.c:155
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
TupleDesc tupDesc
Definition: typcache.h:76
#define TYPECACHE_TUPDESC
Definition: typcache.h:118
TupleDesc lookup_rowtype_tupdesc_noerror ( Oid  type_id,
int32  typmod,
bool  noError 
)

Definition at line 1262 of file typcache.c.

References IncrTupleDescRefCount(), lookup_rowtype_tupdesc_internal(), and NULL.

Referenced by plperl_sv_to_datum().

1263 {
1264  TupleDesc tupDesc;
1265 
1266  tupDesc = lookup_rowtype_tupdesc_internal(type_id, typmod, noError);
1267  if (tupDesc != NULL)
1268  IncrTupleDescRefCount(tupDesc);
1269  return tupDesc;
1270 }
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:316
#define NULL
Definition: c.h:226
static TupleDesc lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1198
TypeCacheEntry* lookup_type_cache ( Oid  type_id,
int  flags 
)

Definition at line 191 of file typcache.c.

References array_element_has_compare(), array_element_has_equality(), array_element_has_hashing(), ARRAY_EQ_OP, ARRAY_GT_OP, ARRAY_LT_OP, Assert, BTEqualStrategyNumber, BTGreaterStrategyNumber, BTLessStrategyNumber, BTORDER_PROC, BTREE_AM_OID, TypeCacheEntry::btree_opf, TypeCacheEntry::btree_opintype, CacheMemoryContext, CacheRegisterRelcacheCallback(), CacheRegisterSyscacheCallback(), CLAOID, TypeCacheEntry::cmp_proc, TypeCacheEntry::cmp_proc_finfo, CONSTROID, CreateCacheMemoryContext(), 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(), GetDefaultOpClass(), GETSTRUCT, TypeCacheEntry::gt_opr, HASH_AM_OID, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, HASH_FIND, TypeCacheEntry::hash_opf, TypeCacheEntry::hash_opintype, TypeCacheEntry::hash_proc, TypeCacheEntry::hash_proc_finfo, hash_search(), HASHPROC, HeapTupleIsValid, HTEqualStrategyNumber, InvalidOid, HASHCTL::keysize, load_domaintype_info(), load_rangetype_info(), load_typcache_tupdesc(), TypeCacheEntry::lt_opr, MemSet, NameStr, TypeCacheEntry::nextDomain, NULL, ObjectIdGetDatum, OidIsValid, RECORD_EQ_OP, record_fields_have_compare(), record_fields_have_equality(), RECORD_GT_OP, RECORD_LT_OP, ReleaseSysCache(), TypeCacheEntry::rngelemtype, SearchSysCache1, TCFLAGS_CHECKED_BTREE_OPCLASS, TCFLAGS_CHECKED_CMP_PROC, TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS, TCFLAGS_CHECKED_EQ_OPR, TCFLAGS_CHECKED_GT_OPR, TCFLAGS_CHECKED_HASH_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_INFO, TYPECACHE_EQ_OPR, TYPECACHE_EQ_OPR_FINFO, TYPECACHE_GT_OPR, TYPECACHE_HASH_OPFAMILY, TYPECACHE_HASH_PROC, TYPECACHE_HASH_PROC_FINFO, TYPECACHE_LT_OPR, TYPECACHE_RANGE_INFO, TYPECACHE_TUPDESC, TypeCacheConstrCallback(), TypeCacheOpcCallback(), TypeCacheRelCallback(), TYPEOID, TypeCacheEntry::typlen, TypeCacheEntry::typrelid, TypeCacheEntry::typstorage, TypeCacheEntry::typtype, TYPTYPE_COMPOSITE, TYPTYPE_DOMAIN, and TYPTYPE_RANGE.

Referenced by appendAggOrderBy(), array_cmp(), array_contain_compare(), array_eq(), array_position_common(), array_positions(), array_replace_internal(), array_typanalyze(), brin_inclusion_opcinfo(), brin_minmax_opcinfo(), BuildRangeRemapInfo(), cache_array_element_properties(), cache_record_field_properties(), calc_arraycontsel(), domain_state_setup(), DomainHasConstraints(), enum_cmp_internal(), ExecInitExpr(), foreign_expr_walker(), get_range_io_data(), get_rule_orderby(), get_sort_group_operators(), hash_array(), hash_range(), InitDomainConstraintRef(), initGinState(), load_rangetype_info(), lookup_rowtype_tupdesc_internal(), op_hashjoinable(), op_mergejoinable(), range_get_typcache(), record_cmp(), record_eq(), refresh_by_match_merge(), scalararraysel(), scalararraysel_containment(), show_sortorder_options(), and width_bucket_array().

192 {
193  TypeCacheEntry *typentry;
194  bool found;
195 
196  if (TypeCacheHash == NULL)
197  {
198  /* First time through: initialize the hash table */
199  HASHCTL ctl;
200 
201  MemSet(&ctl, 0, sizeof(ctl));
202  ctl.keysize = sizeof(Oid);
203  ctl.entrysize = sizeof(TypeCacheEntry);
204  TypeCacheHash = hash_create("Type information cache", 64,
205  &ctl, HASH_ELEM | HASH_BLOBS);
206 
207  /* Also set up callbacks for SI invalidations */
212 
213  /* Also make sure CacheMemoryContext exists */
214  if (!CacheMemoryContext)
216  }
217 
218  /* Try to look up an existing entry */
220  (void *) &type_id,
221  HASH_FIND, NULL);
222  if (typentry == NULL)
223  {
224  /*
225  * If we didn't find one, we want to make one. But first look up the
226  * pg_type row, just to make sure we don't make a cache entry for an
227  * invalid type OID. If the type OID is not valid, present a
228  * user-facing error, since some code paths such as domain_in() allow
229  * this function to be reached with a user-supplied OID.
230  */
231  HeapTuple tp;
232  Form_pg_type typtup;
233 
234  tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
235  if (!HeapTupleIsValid(tp))
236  ereport(ERROR,
237  (errcode(ERRCODE_UNDEFINED_OBJECT),
238  errmsg("type with OID %u does not exist", type_id)));
239  typtup = (Form_pg_type) GETSTRUCT(tp);
240  if (!typtup->typisdefined)
241  ereport(ERROR,
242  (errcode(ERRCODE_UNDEFINED_OBJECT),
243  errmsg("type \"%s\" is only a shell",
244  NameStr(typtup->typname))));
245 
246  /* Now make the typcache entry */
248  (void *) &type_id,
249  HASH_ENTER, &found);
250  Assert(!found); /* it wasn't there a moment ago */
251 
252  MemSet(typentry, 0, sizeof(TypeCacheEntry));
253  typentry->type_id = type_id;
254  typentry->typlen = typtup->typlen;
255  typentry->typbyval = typtup->typbyval;
256  typentry->typalign = typtup->typalign;
257  typentry->typstorage = typtup->typstorage;
258  typentry->typtype = typtup->typtype;
259  typentry->typrelid = typtup->typrelid;
260 
261  /* If it's a domain, immediately thread it into the domain cache list */
262  if (typentry->typtype == TYPTYPE_DOMAIN)
263  {
264  typentry->nextDomain = firstDomainTypeEntry;
265  firstDomainTypeEntry = typentry;
266  }
267 
268  ReleaseSysCache(tp);
269  }
270 
271  /*
272  * Look up opclasses if we haven't already and any dependent info is
273  * requested.
274  */
279  !(typentry->flags & TCFLAGS_CHECKED_BTREE_OPCLASS))
280  {
281  Oid opclass;
282 
283  opclass = GetDefaultOpClass(type_id, BTREE_AM_OID);
284  if (OidIsValid(opclass))
285  {
286  typentry->btree_opf = get_opclass_family(opclass);
287  typentry->btree_opintype = get_opclass_input_type(opclass);
288  }
289  else
290  {
291  typentry->btree_opf = typentry->btree_opintype = InvalidOid;
292  }
293 
294  /*
295  * Reset information derived from btree opclass. Note in particular
296  * that we'll redetermine the eq_opr even if we previously found one;
297  * this matters in case a btree opclass has been added to a type that
298  * previously had only a hash opclass.
299  */
300  typentry->flags &= ~(TCFLAGS_CHECKED_EQ_OPR |
305  }
306 
307  /*
308  * If we need to look up equality operator, and there's no btree opclass,
309  * force lookup of hash opclass.
310  */
311  if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
312  !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR) &&
313  typentry->btree_opf == InvalidOid)
314  flags |= TYPECACHE_HASH_OPFAMILY;
315 
318  !(typentry->flags & TCFLAGS_CHECKED_HASH_OPCLASS))
319  {
320  Oid opclass;
321 
322  opclass = GetDefaultOpClass(type_id, HASH_AM_OID);
323  if (OidIsValid(opclass))
324  {
325  typentry->hash_opf = get_opclass_family(opclass);
326  typentry->hash_opintype = get_opclass_input_type(opclass);
327  }
328  else
329  {
330  typentry->hash_opf = typentry->hash_opintype = InvalidOid;
331  }
332 
333  /*
334  * Reset information derived from hash opclass. We do *not* reset the
335  * eq_opr; if we already found one from the btree opclass, that
336  * decision is still good.
337  */
338  typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC);
339  typentry->flags |= TCFLAGS_CHECKED_HASH_OPCLASS;
340  }
341 
342  /*
343  * Look for requested operators and functions, if we haven't already.
344  */
345  if ((flags & (TYPECACHE_EQ_OPR | TYPECACHE_EQ_OPR_FINFO)) &&
346  !(typentry->flags & TCFLAGS_CHECKED_EQ_OPR))
347  {
348  Oid eq_opr = InvalidOid;
349 
350  if (typentry->btree_opf != InvalidOid)
351  eq_opr = get_opfamily_member(typentry->btree_opf,
352  typentry->btree_opintype,
353  typentry->btree_opintype,
355  if (eq_opr == InvalidOid &&
356  typentry->hash_opf != InvalidOid)
357  eq_opr = get_opfamily_member(typentry->hash_opf,
358  typentry->hash_opintype,
359  typentry->hash_opintype,
361 
362  /*
363  * If the proposed equality operator is array_eq or record_eq, check
364  * to see if the element type or column types support equality. If
365  * not, array_eq or record_eq would fail at runtime, so we don't want
366  * to report that the type has equality.
367  */
368  if (eq_opr == ARRAY_EQ_OP &&
369  !array_element_has_equality(typentry))
370  eq_opr = InvalidOid;
371  else if (eq_opr == RECORD_EQ_OP &&
372  !record_fields_have_equality(typentry))
373  eq_opr = InvalidOid;
374 
375  /* Force update of eq_opr_finfo only if we're changing state */
376  if (typentry->eq_opr != eq_opr)
377  typentry->eq_opr_finfo.fn_oid = InvalidOid;
378 
379  typentry->eq_opr = eq_opr;
380 
381  /*
382  * Reset info about hash function whenever we pick up new info about
383  * equality operator. This is so we can ensure that the hash function
384  * matches the operator.
385  */
386  typentry->flags &= ~(TCFLAGS_CHECKED_HASH_PROC);
387  typentry->flags |= TCFLAGS_CHECKED_EQ_OPR;
388  }
389  if ((flags & TYPECACHE_LT_OPR) &&
390  !(typentry->flags & TCFLAGS_CHECKED_LT_OPR))
391  {
392  Oid lt_opr = InvalidOid;
393 
394  if (typentry->btree_opf != InvalidOid)
395  lt_opr = get_opfamily_member(typentry->btree_opf,
396  typentry->btree_opintype,
397  typentry->btree_opintype,
399 
400  /* As above, make sure array_cmp or record_cmp will succeed */
401  if (lt_opr == ARRAY_LT_OP &&
402  !array_element_has_compare(typentry))
403  lt_opr = InvalidOid;
404  else if (lt_opr == RECORD_LT_OP &&
405  !record_fields_have_compare(typentry))
406  lt_opr = InvalidOid;
407 
408  typentry->lt_opr = lt_opr;
409  typentry->flags |= TCFLAGS_CHECKED_LT_OPR;
410  }
411  if ((flags & TYPECACHE_GT_OPR) &&
412  !(typentry->flags & TCFLAGS_CHECKED_GT_OPR))
413  {
414  Oid gt_opr = InvalidOid;
415 
416  if (typentry->btree_opf != InvalidOid)
417  gt_opr = get_opfamily_member(typentry->btree_opf,
418  typentry->btree_opintype,
419  typentry->btree_opintype,
421 
422  /* As above, make sure array_cmp or record_cmp will succeed */
423  if (gt_opr == ARRAY_GT_OP &&
424  !array_element_has_compare(typentry))
425  gt_opr = InvalidOid;
426  else if (gt_opr == RECORD_GT_OP &&
427  !record_fields_have_compare(typentry))
428  gt_opr = InvalidOid;
429 
430  typentry->gt_opr = gt_opr;
431  typentry->flags |= TCFLAGS_CHECKED_GT_OPR;
432  }
433  if ((flags & (TYPECACHE_CMP_PROC | TYPECACHE_CMP_PROC_FINFO)) &&
434  !(typentry->flags & TCFLAGS_CHECKED_CMP_PROC))
435  {
436  Oid cmp_proc = InvalidOid;
437 
438  if (typentry->btree_opf != InvalidOid)
439  cmp_proc = get_opfamily_proc(typentry->btree_opf,
440  typentry->btree_opintype,
441  typentry->btree_opintype,
442  BTORDER_PROC);
443 
444  /* As above, make sure array_cmp or record_cmp will succeed */
445  if (cmp_proc == F_BTARRAYCMP &&
446  !array_element_has_compare(typentry))
447  cmp_proc = InvalidOid;
448  else if (cmp_proc == F_BTRECORDCMP &&
449  !record_fields_have_compare(typentry))
450  cmp_proc = InvalidOid;
451 
452  /* Force update of cmp_proc_finfo only if we're changing state */
453  if (typentry->cmp_proc != cmp_proc)
454  typentry->cmp_proc_finfo.fn_oid = InvalidOid;
455 
456  typentry->cmp_proc = cmp_proc;
457  typentry->flags |= TCFLAGS_CHECKED_CMP_PROC;
458  }
460  !(typentry->flags & TCFLAGS_CHECKED_HASH_PROC))
461  {
462  Oid hash_proc = InvalidOid;
463 
464  /*
465  * We insist that the eq_opr, if one has been determined, match the
466  * hash opclass; else report there is no hash function.
467  */
468  if (typentry->hash_opf != InvalidOid &&
469  (!OidIsValid(typentry->eq_opr) ||
470  typentry->eq_opr == get_opfamily_member(typentry->hash_opf,
471  typentry->hash_opintype,
472  typentry->hash_opintype,
474  hash_proc = get_opfamily_proc(typentry->hash_opf,
475  typentry->hash_opintype,
476  typentry->hash_opintype,
477  HASHPROC);
478 
479  /*
480  * As above, make sure hash_array will succeed. We don't currently
481  * support hashing for composite types, but when we do, we'll need
482  * more logic here to check that case too.
483  */
484  if (hash_proc == F_HASH_ARRAY &&
485  !array_element_has_hashing(typentry))
486  hash_proc = InvalidOid;
487 
488  /* Force update of hash_proc_finfo only if we're changing state */
489  if (typentry->hash_proc != hash_proc)
490  typentry->hash_proc_finfo.fn_oid = InvalidOid;
491 
492  typentry->hash_proc = hash_proc;
493  typentry->flags |= TCFLAGS_CHECKED_HASH_PROC;
494  }
495 
496  /*
497  * Set up fmgr lookup info as requested
498  *
499  * Note: we tell fmgr the finfo structures live in CacheMemoryContext,
500  * which is not quite right (they're really in the hash table's private
501  * memory context) but this will do for our purposes.
502  *
503  * Note: the code above avoids invalidating the finfo structs unless the
504  * referenced operator/function OID actually changes. This is to prevent
505  * unnecessary leakage of any subsidiary data attached to an finfo, since
506  * that would cause session-lifespan memory leaks.
507  */
508  if ((flags & TYPECACHE_EQ_OPR_FINFO) &&
509  typentry->eq_opr_finfo.fn_oid == InvalidOid &&
510  typentry->eq_opr != InvalidOid)
511  {
512  Oid eq_opr_func;
513 
514  eq_opr_func = get_opcode(typentry->eq_opr);
515  if (eq_opr_func != InvalidOid)
516  fmgr_info_cxt(eq_opr_func, &typentry->eq_opr_finfo,
518  }
519  if ((flags & TYPECACHE_CMP_PROC_FINFO) &&
520  typentry->cmp_proc_finfo.fn_oid == InvalidOid &&
521  typentry->cmp_proc != InvalidOid)
522  {
523  fmgr_info_cxt(typentry->cmp_proc, &typentry->cmp_proc_finfo,
525  }
526  if ((flags & TYPECACHE_HASH_PROC_FINFO) &&
527  typentry->hash_proc_finfo.fn_oid == InvalidOid &&
528  typentry->hash_proc != InvalidOid)
529  {
530  fmgr_info_cxt(typentry->hash_proc, &typentry->hash_proc_finfo,
532  }
533 
534  /*
535  * If it's a composite type (row type), get tupdesc if requested
536  */
537  if ((flags & TYPECACHE_TUPDESC) &&
538  typentry->tupDesc == NULL &&
539  typentry->typtype == TYPTYPE_COMPOSITE)
540  {
541  load_typcache_tupdesc(typentry);
542  }
543 
544  /*
545  * If requested, get information about a range type
546  */
547  if ((flags & TYPECACHE_RANGE_INFO) &&
548  typentry->rngelemtype == NULL &&
549  typentry->typtype == TYPTYPE_RANGE)
550  {
551  load_rangetype_info(typentry);
552  }
553 
554  /*
555  * If requested, get information about a domain type
556  */
557  if ((flags & TYPECACHE_DOMAIN_INFO) &&
558  (typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
559  typentry->typtype == TYPTYPE_DOMAIN)
560  {
561  load_domaintype_info(typentry);
562  }
563 
564  return typentry;
565 }
static bool array_element_has_hashing(TypeCacheEntry *typentry)
Definition: typcache.c:1090
#define TYPTYPE_DOMAIN
Definition: pg_type.h:710
static void load_typcache_tupdesc(TypeCacheEntry *typentry)
Definition: typcache.c:571
#define BTORDER_PROC
Definition: nbtree.h:229
#define TYPECACHE_RANGE_INFO
Definition: typcache.h:121
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
Definition: indexcmds.c:1383
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid hash_opintype
Definition: typcache.h:53
#define TCFLAGS_CHECKED_EQ_OPR
Definition: typcache.c:80
#define HASH_ELEM
Definition: hsearch.h:87
static TypeCacheEntry * firstDomainTypeEntry
Definition: typcache.c:75
#define TYPTYPE_COMPOSITE
Definition: pg_type.h:709
struct TypeCacheEntry TypeCacheEntry
#define HTEqualStrategyNumber
Definition: hash.h:261
#define TYPECACHE_EQ_OPR_FINFO
Definition: typcache.h:115
#define BTREE_AM_OID
Definition: pg_am.h:70
#define TYPECACHE_HASH_PROC_FINFO
Definition: typcache.h:117
Size entrysize
Definition: hsearch.h:73
#define TYPECACHE_EQ_OPR
Definition: typcache.h:110
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:852
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
#define RECORD_EQ_OP
Definition: pg_operator.h:1699
int16 typlen
Definition: typcache.h:35
#define OidIsValid(objectId)
Definition: c.h:533
bool typbyval
Definition: typcache.h:36
#define HASH_AM_OID
Definition: pg_am.h:73
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1403
FmgrInfo cmp_proc_finfo
Definition: typcache.h:68
struct TypeCacheEntry * nextDomain
Definition: typcache.h:106
#define TCFLAGS_CHECKED_GT_OPR
Definition: typcache.c:82
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define ARRAY_LT_OP
Definition: pg_operator.h:781
char typstorage
Definition: typcache.h:38
static void TypeCacheRelCallback(Datum arg, Oid relid)
Definition: typcache.c:1409
static bool array_element_has_compare(TypeCacheEntry *typentry)
Definition: typcache.c:1082
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:163
FmgrInfo hash_proc_finfo
Definition: typcache.h:69
#define RECORD_LT_OP
Definition: pg_operator.h:1704
#define TYPECACHE_GT_OPR
Definition: typcache.h:112
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:169
#define ereport(elevel, rest)
Definition: elog.h:122
#define TYPECACHE_BTREE_OPFAMILY
Definition: typcache.h:119
struct TypeCacheEntry * rngelemtype
Definition: typcache.h:84
#define RECORD_GT_OP
Definition: pg_operator.h:1707
#define TYPTYPE_RANGE
Definition: pg_type.h:713
#define HASH_BLOBS
Definition: hsearch.h:88
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1379
uintptr_t Datum
Definition: postgres.h:374
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Oid btree_opintype
Definition: typcache.h:51
Size keysize
Definition: hsearch.h:72
FmgrInfo eq_opr_finfo
Definition: typcache.h:67
#define InvalidOid
Definition: postgres_ext.h:36
static void load_rangetype_info(TypeCacheEntry *typentry)
Definition: typcache.c:599
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
Oid fn_oid
Definition: fmgr.h:56
#define TYPECACHE_CMP_PROC
Definition: typcache.h:113
char typtype
Definition: typcache.h:39
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
#define ARRAY_GT_OP
Definition: pg_operator.h:784
void CreateCacheMemoryContext(void)
Definition: catcache.c:525
#define TCFLAGS_CHECKED_BTREE_OPCLASS
Definition: typcache.c:78
#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS
Definition: typcache.c:92
static bool record_fields_have_compare(TypeCacheEntry *typentry)
Definition: typcache.c:1129
#define ARRAY_EQ_OP
Definition: pg_operator.h:776
Oid get_opclass_family(Oid opclass)
Definition: lsyscache.c:1015
static bool array_element_has_equality(TypeCacheEntry *typentry)
Definition: typcache.c:1074
static void load_domaintype_info(TypeCacheEntry *typentry)
Definition: typcache.c:662
#define TCFLAGS_CHECKED_HASH_PROC
Definition: typcache.c:84
char typalign
Definition: typcache.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HASHPROC
Definition: hash.h:269
#define TYPECACHE_LT_OPR
Definition: typcache.h:111
#define TCFLAGS_CHECKED_LT_OPR
Definition: typcache.c:81
#define NameStr(name)
Definition: c.h:494
#define TYPECACHE_CMP_PROC_FINFO
Definition: typcache.h:116
#define TCFLAGS_CHECKED_HASH_OPCLASS
Definition: typcache.c:79
#define TYPECACHE_HASH_OPFAMILY
Definition: typcache.h:120
#define TYPECACHE_DOMAIN_INFO
Definition: typcache.h:122
static bool record_fields_have_equality(TypeCacheEntry *typentry)
Definition: typcache.c:1121
#define TCFLAGS_CHECKED_CMP_PROC
Definition: typcache.c:83
#define BTLessStrategyNumber
Definition: stratnum.h:29
TupleDesc tupDesc
Definition: typcache.h:76
static void TypeCacheConstrCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:1490
static HTAB * TypeCacheHash
Definition: typcache.c:72
#define TYPECACHE_HASH_PROC
Definition: typcache.h:114
#define TYPECACHE_TUPDESC
Definition: typcache.h:118
#define BTEqualStrategyNumber
Definition: stratnum.h:31
static void TypeCacheOpcCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: typcache.c:1461
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1037
static List * prep_domain_constraints ( List constraints,
MemoryContext  execctx 
)
static

Definition at line 933 of file typcache.c.

References DomainConstraintState::check_expr, DomainConstraintState::constrainttype, ExecInitExpr(), lappend(), lfirst, makeNode, MemoryContextSwitchTo(), DomainConstraintState::name, NIL, and NULL.

Referenced by InitDomainConstraintRef(), and UpdateDomainConstraintRef().

934 {
935  List *result = NIL;
936  MemoryContext oldcxt;
937  ListCell *lc;
938 
939  oldcxt = MemoryContextSwitchTo(execctx);
940 
941  foreach(lc, constraints)
942  {
944  DomainConstraintState *newr;
945 
947  newr->constrainttype = r->constrainttype;
948  newr->name = r->name;
949  /* Must cast here because cache items contain expr plan trees */
950  newr->check_expr = ExecInitExpr((Expr *) r->check_expr, NULL);
951 
952  result = lappend(result, newr);
953  }
954 
955  MemoryContextSwitchTo(oldcxt);
956 
957  return result;
958 }
#define NIL
Definition: pg_list.h:69
DomainConstraintType constrainttype
Definition: execnodes.h:1022
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ExprState * check_expr
Definition: execnodes.h:1024
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4266
List * lappend(List *list, void *datum)
Definition: list.c:128
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45
static bool record_fields_have_compare ( TypeCacheEntry typentry)
static

Definition at line 1129 of file typcache.c.

References cache_record_field_properties(), TypeCacheEntry::flags, TCFLAGS_CHECKED_FIELD_PROPERTIES, and TCFLAGS_HAVE_FIELD_COMPARE.

Referenced by lookup_type_cache().

1130 {
1131  if (!(typentry->flags & TCFLAGS_CHECKED_FIELD_PROPERTIES))
1133  return (typentry->flags & TCFLAGS_HAVE_FIELD_COMPARE) != 0;
1134 }
#define TCFLAGS_CHECKED_FIELD_PROPERTIES
Definition: typcache.c:89
static void cache_record_field_properties(TypeCacheEntry *typentry)
Definition: typcache.c:1137
#define TCFLAGS_HAVE_FIELD_COMPARE
Definition: typcache.c:91
static bool record_fields_have_equality ( TypeCacheEntry typentry)
static

Definition at line 1121 of file typcache.c.

References cache_record_field_properties(), TypeCacheEntry::flags, TCFLAGS_CHECKED_FIELD_PROPERTIES, and TCFLAGS_HAVE_FIELD_EQUALITY.

Referenced by lookup_type_cache().

1122 {
1123  if (!(typentry->flags & TCFLAGS_CHECKED_FIELD_PROPERTIES))
1125  return (typentry->flags & TCFLAGS_HAVE_FIELD_EQUALITY) != 0;
1126 }
#define TCFLAGS_CHECKED_FIELD_PROPERTIES
Definition: typcache.c:89
static void cache_record_field_properties(TypeCacheEntry *typentry)
Definition: typcache.c:1137
#define TCFLAGS_HAVE_FIELD_EQUALITY
Definition: typcache.c:90
static void TypeCacheConstrCallback ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 1490 of file typcache.c.

References TypeCacheEntry::flags, TypeCacheEntry::nextDomain, NULL, and TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS.

Referenced by lookup_type_cache().

1491 {
1492  TypeCacheEntry *typentry;
1493 
1494  /*
1495  * Because this is called very frequently, and typically very few of the
1496  * typcache entries are for domains, we don't use hash_seq_search here.
1497  * Instead we thread all the domain-type entries together so that we can
1498  * visit them cheaply.
1499  */
1500  for (typentry = firstDomainTypeEntry;
1501  typentry != NULL;
1502  typentry = typentry->nextDomain)
1503  {
1504  /* Reset domain constraint validity information */
1506  }
1507 }
static TypeCacheEntry * firstDomainTypeEntry
Definition: typcache.c:75
struct TypeCacheEntry * nextDomain
Definition: typcache.h:106
#define NULL
Definition: c.h:226
#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS
Definition: typcache.c:92
static void TypeCacheOpcCallback ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 1461 of file typcache.c.

References TypeCacheEntry::flags, hash_seq_init(), hash_seq_search(), NULL, and status().

Referenced by lookup_type_cache().

1462 {
1464  TypeCacheEntry *typentry;
1465 
1466  /* TypeCacheHash must exist, else this callback wouldn't be registered */
1467  hash_seq_init(&status, TypeCacheHash);
1468  while ((typentry = (TypeCacheEntry *) hash_seq_search(&status)) != NULL)
1469  {
1470  /* Reset equality/comparison/hashing validity information */
1471  typentry->flags = 0;
1472  }
1473 }
#define NULL
Definition: c.h:226
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1353
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1343
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
static HTAB * TypeCacheHash
Definition: typcache.c:72
static void TypeCacheRelCallback ( Datum  arg,
Oid  relid 
)
static

Definition at line 1409 of file typcache.c.

References Assert, TypeCacheEntry::flags, FreeTupleDesc(), hash_seq_init(), hash_seq_search(), InvalidOid, NULL, status(), tupleDesc::tdrefcount, TypeCacheEntry::tupDesc, TypeCacheEntry::typrelid, TypeCacheEntry::typtype, and TYPTYPE_COMPOSITE.

Referenced by lookup_type_cache().

1410 {
1412  TypeCacheEntry *typentry;
1413 
1414  /* TypeCacheHash must exist, else this callback wouldn't be registered */
1415  hash_seq_init(&status, TypeCacheHash);
1416  while ((typentry = (TypeCacheEntry *) hash_seq_search(&status)) != NULL)
1417  {
1418  if (typentry->typtype != TYPTYPE_COMPOSITE)
1419  continue; /* skip non-composites */
1420 
1421  /* Skip if no match, unless we're zapping all composite types */
1422  if (relid != typentry->typrelid && relid != InvalidOid)
1423  continue;
1424 
1425  /* Delete tupdesc if we have it */
1426  if (typentry->tupDesc != NULL)
1427  {
1428  /*
1429  * Release our refcount, and free the tupdesc if none remain.
1430  * (Can't use DecrTupleDescRefCount because this reference is not
1431  * logged in current resource owner.)
1432  */
1433  Assert(typentry->tupDesc->tdrefcount > 0);
1434  if (--typentry->tupDesc->tdrefcount == 0)
1435  FreeTupleDesc(typentry->tupDesc);
1436  typentry->tupDesc = NULL;
1437  }
1438 
1439  /* Reset equality/comparison/hashing validity information */
1440  typentry->flags = 0;
1441  }
1442 }
#define TYPTYPE_COMPOSITE
Definition: pg_type.h:709
#define InvalidOid
Definition: postgres_ext.h:36
char typtype
Definition: typcache.h:39
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1353
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1343
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:266
int tdrefcount
Definition: tupdesc.h:80
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
TupleDesc tupDesc
Definition: typcache.h:76
static HTAB * TypeCacheHash
Definition: typcache.c:72
void UpdateDomainConstraintRef ( DomainConstraintRef ref)

Definition at line 1001 of file typcache.c.

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

Referenced by domain_check_input(), and ExecEvalCoerceToDomain().

1002 {
1003  TypeCacheEntry *typentry = ref->tcache;
1004 
1005  /* Make sure typcache entry's data is up to date */
1006  if ((typentry->flags & TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS) == 0 &&
1007  typentry->typtype == TYPTYPE_DOMAIN)
1008  load_domaintype_info(typentry);
1009 
1010  /* Transfer to ref object if there's new info, adjusting refcounts */
1011  if (ref->dcc != typentry->domainData)
1012  {
1013  /* Paranoia --- be sure link is nulled before trying to release */
1014  DomainConstraintCache *dcc = ref->dcc;
1015 
1016  if (dcc)
1017  {
1018  /*
1019  * Note: we just leak the previous list of executable domain
1020  * constraints. Alternatively, we could keep those in a child
1021  * context of ref->refctx and free that context at this point.
1022  * However, in practice this code path will be taken so seldom
1023  * that the extra bookkeeping for a child context doesn't seem
1024  * worthwhile; we'll just allow a leak for the lifespan of refctx.
1025  */
1026  ref->constraints = NIL;
1027  ref->dcc = NULL;
1028  decr_dcc_refcount(dcc);
1029  }
1030  dcc = typentry->domainData;
1031  if (dcc)
1032  {
1033  ref->dcc = dcc;
1034  dcc->dccRefCount++;
1036  ref->refctx);
1037  }
1038  }
1039 }
#define NIL
Definition: pg_list.h:69
#define TYPTYPE_DOMAIN
Definition: pg_type.h:710
DomainConstraintCache * dcc
Definition: typcache.h:137
DomainConstraintCache * domainData
Definition: typcache.h:94
MemoryContext refctx
Definition: typcache.h:133
char typtype
Definition: typcache.h:39
#define NULL
Definition: c.h:226
static List * prep_domain_constraints(List *constraints, MemoryContext execctx)
Definition: typcache.c:933
TypeCacheEntry * tcache
Definition: typcache.h:134
#define TCFLAGS_CHECKED_DOMAIN_CONSTRAINTS
Definition: typcache.c:92
static void load_domaintype_info(TypeCacheEntry *typentry)
Definition: typcache.c:662
static void decr_dcc_refcount(DomainConstraintCache *dcc)
Definition: typcache.c:901

Variable Documentation

TypeCacheEntry* firstDomainTypeEntry = NULL
static

Definition at line 75 of file typcache.c.

Referenced by lookup_type_cache().

int32 NextRecordTypmod = 0
static

Definition at line 155 of file typcache.c.

Referenced by assign_record_type_typmod(), and lookup_rowtype_tupdesc_internal().

TupleDesc* RecordCacheArray = NULL
static

Definition at line 153 of file typcache.c.

int32 RecordCacheArrayLen = 0
static

Definition at line 154 of file typcache.c.

Referenced by assign_record_type_typmod().

HTAB* RecordCacheHash = NULL
static

Definition at line 151 of file typcache.c.

HTAB* TypeCacheHash = NULL
static

Definition at line 72 of file typcache.c.