PostgreSQL Source Code  git master
domains.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "lib/stringinfo.h"
#include "utils/builtins.h"
#include "utils/expandeddatum.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for domains.c:

Go to the source code of this file.

Data Structures

struct  DomainIOData
 

Typedefs

typedef struct DomainIOData DomainIOData
 

Functions

static DomainIODatadomain_state_setup (Oid domainType, bool binary, MemoryContext mcxt)
 
static void domain_check_input (Datum value, bool isnull, DomainIOData *my_extra)
 
Datum domain_in (PG_FUNCTION_ARGS)
 
Datum domain_recv (PG_FUNCTION_ARGS)
 
void domain_check (Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
 
int errdatatype (Oid datatypeOid)
 
int errdomainconstraint (Oid datatypeOid, const char *conname)
 

Typedef Documentation

◆ DomainIOData

Function Documentation

◆ domain_check()

void domain_check ( Datum  value,
bool  isnull,
Oid  domainType,
void **  extra,
MemoryContext  mcxt 
)

Definition at line 327 of file domains.c.

References CurrentMemoryContext, domain_check_input(), domain_state_setup(), and DomainIOData::domain_type.

Referenced by check_domain_for_new_field(), check_domain_for_new_tuple(), expanded_record_set_fields(), hstore_populate_record(), plperl_return_next_internal(), plperl_sv_to_datum(), plpgsql_exec_function(), pltcl_build_tuple_result(), PLyObject_ToDomain(), populate_composite(), populate_domain(), and populate_recordset_record().

329 {
330  DomainIOData *my_extra = NULL;
331 
332  if (mcxt == NULL)
333  mcxt = CurrentMemoryContext;
334 
335  /*
336  * We arrange to look up the needed info just once per series of calls,
337  * assuming the domain type doesn't change underneath us (which really
338  * shouldn't happen, but cope if it does).
339  */
340  if (extra)
341  my_extra = (DomainIOData *) *extra;
342  if (my_extra == NULL || my_extra->domain_type != domainType)
343  {
344  my_extra = domain_state_setup(domainType, true, mcxt);
345  if (extra)
346  *extra = (void *) my_extra;
347  }
348 
349  /*
350  * Do the necessary checks to ensure it's a valid domain value.
351  */
352  domain_check_input(value, isnull, my_extra);
353 }
Oid domain_type
Definition: domains.c:49
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static struct @131 value
static DomainIOData * domain_state_setup(Oid domainType, bool binary, MemoryContext mcxt)
Definition: domains.c:73
static void domain_check_input(Datum value, bool isnull, DomainIOData *my_extra)
Definition: domains.c:131

◆ domain_check_input()

static void domain_check_input ( Datum  value,
bool  isnull,
DomainIOData my_extra 
)
static

Definition at line 131 of file domains.c.

References DomainConstraintState::check_exprstate, DomainIOData::constraint_ref, DomainConstraintRef::constraints, DomainConstraintState::constrainttype, CreateStandaloneExprContext(), DOM_CONSTRAINT_CHECK, DOM_CONSTRAINT_NOTNULL, DomainIOData::domain_type, ExprContext::domainValue_datum, ExprContext::domainValue_isNull, DomainIOData::econtext, elog, ereport, errcode(), errdatatype(), errdomainconstraint(), errmsg(), ERROR, ExecCheck(), format_type_be(), lfirst, MakeExpandedObjectReadOnly, DomainIOData::mcxt, MemoryContextSwitchTo(), DomainConstraintState::name, ReScanExprContext(), DomainConstraintRef::tcache, TypeCacheEntry::typlen, and UpdateDomainConstraintRef().

Referenced by domain_check(), domain_in(), and domain_recv().

132 {
133  ExprContext *econtext = my_extra->econtext;
134  ListCell *l;
135 
136  /* Make sure we have up-to-date constraints */
138 
139  foreach(l, my_extra->constraint_ref.constraints)
140  {
142 
143  switch (con->constrainttype)
144  {
146  if (isnull)
147  ereport(ERROR,
148  (errcode(ERRCODE_NOT_NULL_VIOLATION),
149  errmsg("domain %s does not allow null values",
150  format_type_be(my_extra->domain_type)),
151  errdatatype(my_extra->domain_type)));
152  break;
154  {
155  /* Make the econtext if we didn't already */
156  if (econtext == NULL)
157  {
158  MemoryContext oldcontext;
159 
160  oldcontext = MemoryContextSwitchTo(my_extra->mcxt);
161  econtext = CreateStandaloneExprContext();
162  MemoryContextSwitchTo(oldcontext);
163  my_extra->econtext = econtext;
164  }
165 
166  /*
167  * Set up value to be returned by CoerceToDomainValue
168  * nodes. Unlike in the generic expression case, this
169  * econtext couldn't be shared with anything else, so no
170  * need to save and restore fields. But we do need to
171  * protect the passed-in value against being changed by
172  * called functions. (It couldn't be a R/W expanded
173  * object for most uses, but that seems possible for
174  * domain_check().)
175  */
176  econtext->domainValue_datum =
178  my_extra->constraint_ref.tcache->typlen);
179  econtext->domainValue_isNull = isnull;
180 
181  if (!ExecCheck(con->check_exprstate, econtext))
182  ereport(ERROR,
183  (errcode(ERRCODE_CHECK_VIOLATION),
184  errmsg("value for domain %s violates check constraint \"%s\"",
185  format_type_be(my_extra->domain_type),
186  con->name),
188  con->name)));
189  break;
190  }
191  default:
192  elog(ERROR, "unrecognized constraint type: %d",
193  (int) con->constrainttype);
194  break;
195  }
196  }
197 
198  /*
199  * Before exiting, call any shutdown callbacks and reset econtext's
200  * per-tuple memory. This avoids leaking non-memory resources, if
201  * anything in the expression(s) has any.
202  */
203  if (econtext)
204  ReScanExprContext(econtext);
205 }
ExprContext * CreateStandaloneExprContext(void)
Definition: execUtils.c:300
void UpdateDomainConstraintRef(DomainConstraintRef *ref)
Definition: typcache.c:1229
DomainConstraintType constrainttype
Definition: execnodes.h:876
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid domain_type
Definition: domains.c:49
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
Datum domainValue_datum
Definition: execnodes.h:249
int16 typlen
Definition: typcache.h:37
#define ERROR
Definition: elog.h:43
int errdomainconstraint(Oid datatypeOid, const char *conname)
Definition: domains.c:384
ExprContext * econtext
Definition: domains.c:58
#define MakeExpandedObjectReadOnly(d, isnull, typlen)
ExprState * check_exprstate
Definition: execnodes.h:879
#define ereport(elevel, rest)
Definition: elog.h:122
bool domainValue_isNull
Definition: execnodes.h:251
int errdatatype(Oid datatypeOid)
Definition: domains.c:360
static struct @131 value
DomainConstraintRef constraint_ref
Definition: domains.c:56
#define lfirst(lc)
Definition: pg_list.h:106
TypeCacheEntry * tcache
Definition: typcache.h:157
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:386
MemoryContext mcxt
Definition: domains.c:60
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool ExecCheck(ExprState *state, ExprContext *econtext)
Definition: execExpr.c:595
#define elog
Definition: elog.h:219

◆ domain_in()

Datum domain_in ( PG_FUNCTION_ARGS  )

Definition at line 212 of file domains.c.

References domain_check_input(), domain_state_setup(), DomainIOData::domain_type, InputFunctionCall(), PG_ARGISNULL, PG_GETARG_CSTRING, PG_GETARG_OID, PG_RETURN_DATUM, PG_RETURN_NULL, DomainIOData::proc, DomainIOData::typioparam, DomainIOData::typtypmod, and value.

213 {
214  char *string;
215  Oid domainType;
216  DomainIOData *my_extra;
217  Datum value;
218 
219  /*
220  * Since domain_in is not strict, we have to check for null inputs. The
221  * typioparam argument should never be null in normal system usage, but it
222  * could be null in a manual invocation --- if so, just return null.
223  */
224  if (PG_ARGISNULL(0))
225  string = NULL;
226  else
227  string = PG_GETARG_CSTRING(0);
228  if (PG_ARGISNULL(1))
229  PG_RETURN_NULL();
230  domainType = PG_GETARG_OID(1);
231 
232  /*
233  * We arrange to look up the needed info just once per series of calls,
234  * assuming the domain type doesn't change underneath us (which really
235  * shouldn't happen, but cope if it does).
236  */
237  my_extra = (DomainIOData *) fcinfo->flinfo->fn_extra;
238  if (my_extra == NULL || my_extra->domain_type != domainType)
239  {
240  my_extra = domain_state_setup(domainType, false,
241  fcinfo->flinfo->fn_mcxt);
242  fcinfo->flinfo->fn_extra = (void *) my_extra;
243  }
244 
245  /*
246  * Invoke the base type's typinput procedure to convert the data.
247  */
248  value = InputFunctionCall(&my_extra->proc,
249  string,
250  my_extra->typioparam,
251  my_extra->typtypmod);
252 
253  /*
254  * Do the necessary checks to ensure it's a valid domain value.
255  */
256  domain_check_input(value, (string == NULL), my_extra);
257 
258  if (string == NULL)
259  PG_RETURN_NULL();
260  else
261  PG_RETURN_DATUM(value);
262 }
Oid domain_type
Definition: domains.c:49
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo proc
Definition: domains.c:54
int32 typtypmod
Definition: domains.c:53
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
char string[11]
Definition: preproc-type.c:46
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:318
static struct @131 value
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1709
Oid typioparam
Definition: domains.c:52
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
static DomainIOData * domain_state_setup(Oid domainType, bool binary, MemoryContext mcxt)
Definition: domains.c:73
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:247
#define PG_RETURN_NULL()
Definition: fmgr.h:310
static void domain_check_input(Datum value, bool isnull, DomainIOData *my_extra)
Definition: domains.c:131

◆ domain_recv()

Datum domain_recv ( PG_FUNCTION_ARGS  )

Definition at line 268 of file domains.c.

References buf, domain_check_input(), domain_state_setup(), DomainIOData::domain_type, PG_ARGISNULL, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_DATUM, PG_RETURN_NULL, DomainIOData::proc, ReceiveFunctionCall(), DomainIOData::typioparam, DomainIOData::typtypmod, and value.

269 {
270  StringInfo buf;
271  Oid domainType;
272  DomainIOData *my_extra;
273  Datum value;
274 
275  /*
276  * Since domain_recv is not strict, we have to check for null inputs. The
277  * typioparam argument should never be null in normal system usage, but it
278  * could be null in a manual invocation --- if so, just return null.
279  */
280  if (PG_ARGISNULL(0))
281  buf = NULL;
282  else
283  buf = (StringInfo) PG_GETARG_POINTER(0);
284  if (PG_ARGISNULL(1))
285  PG_RETURN_NULL();
286  domainType = PG_GETARG_OID(1);
287 
288  /*
289  * We arrange to look up the needed info just once per series of calls,
290  * assuming the domain type doesn't change underneath us (which really
291  * shouldn't happen, but cope if it does).
292  */
293  my_extra = (DomainIOData *) fcinfo->flinfo->fn_extra;
294  if (my_extra == NULL || my_extra->domain_type != domainType)
295  {
296  my_extra = domain_state_setup(domainType, true,
297  fcinfo->flinfo->fn_mcxt);
298  fcinfo->flinfo->fn_extra = (void *) my_extra;
299  }
300 
301  /*
302  * Invoke the base type's typreceive procedure to convert the data.
303  */
304  value = ReceiveFunctionCall(&my_extra->proc,
305  buf,
306  my_extra->typioparam,
307  my_extra->typtypmod);
308 
309  /*
310  * Do the necessary checks to ensure it's a valid domain value.
311  */
312  domain_check_input(value, (buf == NULL), my_extra);
313 
314  if (buf == NULL)
315  PG_RETURN_NULL();
316  else
317  PG_RETURN_DATUM(value);
318 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
Oid domain_type
Definition: domains.c:49
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo proc
Definition: domains.c:54
int32 typtypmod
Definition: domains.c:53
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1767
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:318
static struct @131 value
Oid typioparam
Definition: domains.c:52
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
static DomainIOData * domain_state_setup(Oid domainType, bool binary, MemoryContext mcxt)
Definition: domains.c:73
#define PG_RETURN_NULL()
Definition: fmgr.h:310
static void domain_check_input(Datum value, bool isnull, DomainIOData *my_extra)
Definition: domains.c:131

◆ domain_state_setup()

static DomainIOData* domain_state_setup ( Oid  domainType,
bool  binary,
MemoryContext  mcxt 
)
static

Definition at line 73 of file domains.c.

References DomainIOData::constraint_ref, DomainIOData::domain_type, TypeCacheEntry::domainBaseType, TypeCacheEntry::domainBaseTypmod, DomainIOData::econtext, ereport, errcode(), errmsg(), ERROR, fmgr_info_cxt(), format_type_be(), getTypeBinaryInputInfo(), getTypeInputInfo(), InitDomainConstraintRef(), lookup_type_cache(), DomainIOData::mcxt, MemoryContextAlloc(), DomainIOData::proc, TYPECACHE_DOMAIN_BASE_INFO, DomainIOData::typiofunc, DomainIOData::typioparam, TypeCacheEntry::typtype, and DomainIOData::typtypmod.

Referenced by domain_check(), domain_in(), and domain_recv().

74 {
75  DomainIOData *my_extra;
76  TypeCacheEntry *typentry;
77  Oid baseType;
78 
79  my_extra = (DomainIOData *) MemoryContextAlloc(mcxt, sizeof(DomainIOData));
80 
81  /*
82  * Verify that domainType represents a valid domain type. We need to be
83  * careful here because domain_in and domain_recv can be called from SQL,
84  * possibly with incorrect arguments. We use lookup_type_cache mainly
85  * because it will throw a clean user-facing error for a bad OID; but also
86  * it can cache the underlying base type info.
87  */
88  typentry = lookup_type_cache(domainType, TYPECACHE_DOMAIN_BASE_INFO);
89  if (typentry->typtype != TYPTYPE_DOMAIN)
90  ereport(ERROR,
91  (errcode(ERRCODE_DATATYPE_MISMATCH),
92  errmsg("type %s is not a domain",
93  format_type_be(domainType))));
94 
95  /* Find out the base type */
96  baseType = typentry->domainBaseType;
97  my_extra->typtypmod = typentry->domainBaseTypmod;
98 
99  /* Look up underlying I/O function */
100  if (binary)
101  getTypeBinaryInputInfo(baseType,
102  &my_extra->typiofunc,
103  &my_extra->typioparam);
104  else
105  getTypeInputInfo(baseType,
106  &my_extra->typiofunc,
107  &my_extra->typioparam);
108  fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc, mcxt);
109 
110  /* Look up constraints for domain */
111  InitDomainConstraintRef(domainType, &my_extra->constraint_ref, mcxt, true);
112 
113  /* We don't make an ExprContext until needed */
114  my_extra->econtext = NULL;
115  my_extra->mcxt = mcxt;
116 
117  /* Mark cache valid */
118  my_extra->domain_type = domainType;
119 
120  return my_extra;
121 }
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid domain_type
Definition: domains.c:49
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo proc
Definition: domains.c:54
Oid domainBaseType
Definition: typcache.h:104
int32 typtypmod
Definition: domains.c:53
#define ERROR
Definition: elog.h:43
ExprContext * econtext
Definition: domains.c:58
int32 domainBaseTypmod
Definition: typcache.h:105
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2683
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2617
#define ereport(elevel, rest)
Definition: elog.h:122
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:139
Oid typiofunc
Definition: domains.c:51
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:321
DomainConstraintRef constraint_ref
Definition: domains.c:56
char typtype
Definition: typcache.h:41
Oid typioparam
Definition: domains.c:52
void InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref, MemoryContext refctx, bool need_exprstate)
Definition: typcache.c:1191
MemoryContext mcxt
Definition: domains.c:60
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771

◆ errdatatype()

int errdatatype ( Oid  datatypeOid)

Definition at line 360 of file domains.c.

References elog, err_generic_string(), ERROR, get_namespace_name(), GETSTRUCT, HeapTupleIsValid, NameStr, ObjectIdGetDatum, PG_DIAG_DATATYPE_NAME, PG_DIAG_SCHEMA_NAME, ReleaseSysCache(), SearchSysCache1(), and TYPEOID.

Referenced by domain_check_input(), errdomainconstraint(), and ExecEvalConstraintNotNull().

361 {
362  HeapTuple tup;
363  Form_pg_type typtup;
364 
365  tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(datatypeOid));
366  if (!HeapTupleIsValid(tup))
367  elog(ERROR, "cache lookup failed for type %u", datatypeOid);
368  typtup = (Form_pg_type) GETSTRUCT(tup);
369 
371  get_namespace_name(typtup->typnamespace));
373 
374  ReleaseSysCache(tup);
375 
376  return 0; /* return value does not matter */
377 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
#define PG_DIAG_SCHEMA_NAME
Definition: postgres_ext.h:65
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3051
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define PG_DIAG_DATATYPE_NAME
Definition: postgres_ext.h:68
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_type * Form_pg_type
Definition: pg_type.h:247
#define NameStr(name)
Definition: c.h:576
#define elog
Definition: elog.h:219
int err_generic_string(int field, const char *str)
Definition: elog.c:1191

◆ errdomainconstraint()

int errdomainconstraint ( Oid  datatypeOid,
const char *  conname 
)

Definition at line 384 of file domains.c.

References err_generic_string(), errdatatype(), and PG_DIAG_CONSTRAINT_NAME.

Referenced by domain_check_input(), and ExecEvalConstraintCheck().

385 {
386  errdatatype(datatypeOid);
388 
389  return 0; /* return value does not matter */
390 }
int errdatatype(Oid datatypeOid)
Definition: domains.c:360
#define PG_DIAG_CONSTRAINT_NAME
Definition: postgres_ext.h:69
int err_generic_string(int field, const char *str)
Definition: elog.c:1191