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 bool domain_check_internal (Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt, Node *escontext)
 
static DomainIODatadomain_state_setup (Oid domainType, bool binary, MemoryContext mcxt)
 
static void domain_check_input (Datum value, bool isnull, DomainIOData *my_extra, Node *escontext)
 
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)
 
bool domain_check_safe (Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt, Node *escontext)
 
int errdatatype (Oid datatypeOid)
 
int errdomainconstraint (Oid datatypeOid, const char *conname)
 

Typedef Documentation

◆ DomainIOData

typedef struct DomainIOData DomainIOData

Function Documentation

◆ domain_check()

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

Definition at line 346 of file domains.c.

348 {
349  (void) domain_check_internal(value, isnull, domainType, extra, mcxt,
350  NULL);
351 }
static bool domain_check_internal(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt, Node *escontext)
Definition: domains.c:371
static struct @155 value

References domain_check_internal(), and value.

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(), and PLyObject_ToDomain().

◆ domain_check_input()

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

Definition at line 138 of file domains.c.

140 {
141  ExprContext *econtext = my_extra->econtext;
142  ListCell *l;
143 
144  /* Make sure we have up-to-date constraints */
146 
147  foreach(l, my_extra->constraint_ref.constraints)
148  {
150 
151  switch (con->constrainttype)
152  {
154  if (isnull)
155  {
156  errsave(escontext,
157  (errcode(ERRCODE_NOT_NULL_VIOLATION),
158  errmsg("domain %s does not allow null values",
159  format_type_be(my_extra->domain_type)),
160  errdatatype(my_extra->domain_type)));
161  goto fail;
162  }
163  break;
165  {
166  /* Make the econtext if we didn't already */
167  if (econtext == NULL)
168  {
169  MemoryContext oldcontext;
170 
171  oldcontext = MemoryContextSwitchTo(my_extra->mcxt);
172  econtext = CreateStandaloneExprContext();
173  MemoryContextSwitchTo(oldcontext);
174  my_extra->econtext = econtext;
175  }
176 
177  /*
178  * Set up value to be returned by CoerceToDomainValue
179  * nodes. Unlike in the generic expression case, this
180  * econtext couldn't be shared with anything else, so no
181  * need to save and restore fields. But we do need to
182  * protect the passed-in value against being changed by
183  * called functions. (It couldn't be a R/W expanded
184  * object for most uses, but that seems possible for
185  * domain_check().)
186  */
187  econtext->domainValue_datum =
189  my_extra->constraint_ref.tcache->typlen);
190  econtext->domainValue_isNull = isnull;
191 
192  if (!ExecCheck(con->check_exprstate, econtext))
193  {
194  errsave(escontext,
195  (errcode(ERRCODE_CHECK_VIOLATION),
196  errmsg("value for domain %s violates check constraint \"%s\"",
197  format_type_be(my_extra->domain_type),
198  con->name),
200  con->name)));
201  goto fail;
202  }
203  break;
204  }
205  default:
206  elog(ERROR, "unrecognized constraint type: %d",
207  (int) con->constrainttype);
208  break;
209  }
210  }
211 
212  /*
213  * Before exiting, call any shutdown callbacks and reset econtext's
214  * per-tuple memory. This avoids leaking non-memory resources, if
215  * anything in the expression(s) has any.
216  */
217 fail:
218  if (econtext)
219  ReScanExprContext(econtext);
220 }
int errdatatype(Oid datatypeOid)
Definition: domains.c:407
int errdomainconstraint(Oid datatypeOid, const char *conname)
Definition: domains.c:431
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define errsave(context,...)
Definition: elog.h:260
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
bool ExecCheck(ExprState *state, ExprContext *econtext)
Definition: execExpr.c:846
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:441
ExprContext * CreateStandaloneExprContext(void)
Definition: execUtils.c:355
@ DOM_CONSTRAINT_CHECK
Definition: execnodes.h:1001
@ DOM_CONSTRAINT_NOTNULL
Definition: execnodes.h:1000
#define MakeExpandedObjectReadOnly(d, isnull, typlen)
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define lfirst(lc)
Definition: pg_list.h:172
MemoryContextSwitchTo(old_ctx)
TypeCacheEntry * tcache
Definition: typcache.h:168
DomainConstraintType constrainttype
Definition: execnodes.h:1007
ExprState * check_exprstate
Definition: execnodes.h:1010
Oid domain_type
Definition: domains.c:52
DomainConstraintRef constraint_ref
Definition: domains.c:59
ExprContext * econtext
Definition: domains.c:61
MemoryContext mcxt
Definition: domains.c:63
Datum domainValue_datum
Definition: execnodes.h:286
bool domainValue_isNull
Definition: execnodes.h:288
int16 typlen
Definition: typcache.h:39
void UpdateDomainConstraintRef(DomainConstraintRef *ref)
Definition: typcache.c:1351

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, errcode(), errdatatype(), errdomainconstraint(), errmsg(), ERROR, errsave, ExecCheck(), format_type_be(), lfirst, MakeExpandedObjectReadOnly, DomainIOData::mcxt, MemoryContextSwitchTo(), DomainConstraintState::name, ReScanExprContext(), DomainConstraintRef::tcache, TypeCacheEntry::typlen, UpdateDomainConstraintRef(), and value.

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

◆ domain_check_internal()

static bool domain_check_internal ( Datum  value,
bool  isnull,
Oid  domainType,
void **  extra,
MemoryContext  mcxt,
Node escontext 
)
static

Definition at line 371 of file domains.c.

374 {
375  DomainIOData *my_extra = NULL;
376 
377  if (mcxt == NULL)
378  mcxt = CurrentMemoryContext;
379 
380  /*
381  * We arrange to look up the needed info just once per series of calls,
382  * assuming the domain type doesn't change underneath us (which really
383  * shouldn't happen, but cope if it does).
384  */
385  if (extra)
386  my_extra = (DomainIOData *) *extra;
387  if (my_extra == NULL || my_extra->domain_type != domainType)
388  {
389  my_extra = domain_state_setup(domainType, true, mcxt);
390  if (extra)
391  *extra = (void *) my_extra;
392  }
393 
394  /*
395  * Do the necessary checks to ensure it's a valid domain value.
396  */
397  domain_check_input(value, isnull, my_extra, escontext);
398 
399  return !SOFT_ERROR_OCCURRED(escontext);
400 }
static void domain_check_input(Datum value, bool isnull, DomainIOData *my_extra, Node *escontext)
Definition: domains.c:138
static DomainIOData * domain_state_setup(Oid domainType, bool binary, MemoryContext mcxt)
Definition: domains.c:76
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
#define SOFT_ERROR_OCCURRED(escontext)
Definition: miscnodes.h:52

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

Referenced by domain_check(), and domain_check_safe().

◆ domain_check_safe()

bool domain_check_safe ( Datum  value,
bool  isnull,
Oid  domainType,
void **  extra,
MemoryContext  mcxt,
Node escontext 
)

Definition at line 355 of file domains.c.

358 {
359  return domain_check_internal(value, isnull, domainType, extra, mcxt,
360  escontext);
361 }

References domain_check_internal(), and value.

Referenced by populate_composite(), populate_domain(), and populate_recordset_record().

◆ domain_in()

Datum domain_in ( PG_FUNCTION_ARGS  )

Definition at line 227 of file domains.c.

228 {
229  char *string;
230  Oid domainType;
231  Node *escontext = fcinfo->context;
232  DomainIOData *my_extra;
233  Datum value;
234 
235  /*
236  * Since domain_in is not strict, we have to check for null inputs. The
237  * typioparam argument should never be null in normal system usage, but it
238  * could be null in a manual invocation --- if so, just return null.
239  */
240  if (PG_ARGISNULL(0))
241  string = NULL;
242  else
243  string = PG_GETARG_CSTRING(0);
244  if (PG_ARGISNULL(1))
245  PG_RETURN_NULL();
246  domainType = PG_GETARG_OID(1);
247 
248  /*
249  * We arrange to look up the needed info just once per series of calls,
250  * assuming the domain type doesn't change underneath us (which really
251  * shouldn't happen, but cope if it does).
252  */
253  my_extra = (DomainIOData *) fcinfo->flinfo->fn_extra;
254  if (my_extra == NULL || my_extra->domain_type != domainType)
255  {
256  my_extra = domain_state_setup(domainType, false,
257  fcinfo->flinfo->fn_mcxt);
258  fcinfo->flinfo->fn_extra = (void *) my_extra;
259  }
260 
261  /*
262  * Invoke the base type's typinput procedure to convert the data.
263  */
264  if (!InputFunctionCallSafe(&my_extra->proc,
265  string,
266  my_extra->typioparam,
267  my_extra->typtypmod,
268  escontext,
269  &value))
270  PG_RETURN_NULL();
271 
272  /*
273  * Do the necessary checks to ensure it's a valid domain value.
274  */
275  domain_check_input(value, (string == NULL), my_extra, escontext);
276 
277  if (string == NULL)
278  PG_RETURN_NULL();
279  else
281 }
bool InputFunctionCallSafe(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod, fmNodePtr escontext, Datum *result)
Definition: fmgr.c:1585
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
char string[11]
Definition: preproc-type.c:52
int32 typtypmod
Definition: domains.c:56
FmgrInfo proc
Definition: domains.c:57
Oid typioparam
Definition: domains.c:55
Definition: nodes.h:129

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

◆ domain_recv()

Datum domain_recv ( PG_FUNCTION_ARGS  )

Definition at line 287 of file domains.c.

288 {
289  StringInfo buf;
290  Oid domainType;
291  DomainIOData *my_extra;
292  Datum value;
293 
294  /*
295  * Since domain_recv is not strict, we have to check for null inputs. The
296  * typioparam argument should never be null in normal system usage, but it
297  * could be null in a manual invocation --- if so, just return null.
298  */
299  if (PG_ARGISNULL(0))
300  buf = NULL;
301  else
303  if (PG_ARGISNULL(1))
304  PG_RETURN_NULL();
305  domainType = PG_GETARG_OID(1);
306 
307  /*
308  * We arrange to look up the needed info just once per series of calls,
309  * assuming the domain type doesn't change underneath us (which really
310  * shouldn't happen, but cope if it does).
311  */
312  my_extra = (DomainIOData *) fcinfo->flinfo->fn_extra;
313  if (my_extra == NULL || my_extra->domain_type != domainType)
314  {
315  my_extra = domain_state_setup(domainType, true,
316  fcinfo->flinfo->fn_mcxt);
317  fcinfo->flinfo->fn_extra = (void *) my_extra;
318  }
319 
320  /*
321  * Invoke the base type's typreceive procedure to convert the data.
322  */
323  value = ReceiveFunctionCall(&my_extra->proc,
324  buf,
325  my_extra->typioparam,
326  my_extra->typtypmod);
327 
328  /*
329  * Do the necessary checks to ensure it's a valid domain value.
330  */
331  domain_check_input(value, (buf == NULL), my_extra, NULL);
332 
333  if (buf == NULL)
334  PG_RETURN_NULL();
335  else
337 }
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1697
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static char * buf
Definition: pg_test_fsync.c:73
StringInfoData * StringInfo
Definition: stringinfo.h:54

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

◆ domain_state_setup()

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

Definition at line 76 of file domains.c.

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

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_internal(), domain_in(), and domain_recv().

◆ errdatatype()

int errdatatype ( Oid  datatypeOid)

Definition at line 407 of file domains.c.

408 {
409  HeapTuple tup;
410  Form_pg_type typtup;
411 
412  tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(datatypeOid));
413  if (!HeapTupleIsValid(tup))
414  elog(ERROR, "cache lookup failed for type %u", datatypeOid);
415  typtup = (Form_pg_type) GETSTRUCT(tup);
416 
418  get_namespace_name(typtup->typnamespace));
420 
421  ReleaseSysCache(tup);
422 
423  return 0; /* return value does not matter */
424 }
#define NameStr(name)
Definition: c.h:746
int err_generic_string(int field, const char *str)
Definition: elog.c:1514
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define PG_DIAG_SCHEMA_NAME
Definition: postgres_ext.h:64
#define PG_DIAG_DATATYPE_NAME
Definition: postgres_ext.h:67
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:218

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

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

◆ errdomainconstraint()

int errdomainconstraint ( Oid  datatypeOid,
const char *  conname 
)

Definition at line 431 of file domains.c.

432 {
433  errdatatype(datatypeOid);
435 
436  return 0; /* return value does not matter */
437 }
#define PG_DIAG_CONSTRAINT_NAME
Definition: postgres_ext.h:68

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

Referenced by domain_check_input(), and ExecEvalConstraintCheck().