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, 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)
 
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 343 of file domains.c.

345 {
346  DomainIOData *my_extra = NULL;
347 
348  if (mcxt == NULL)
349  mcxt = CurrentMemoryContext;
350 
351  /*
352  * We arrange to look up the needed info just once per series of calls,
353  * assuming the domain type doesn't change underneath us (which really
354  * shouldn't happen, but cope if it does).
355  */
356  if (extra)
357  my_extra = (DomainIOData *) *extra;
358  if (my_extra == NULL || my_extra->domain_type != domainType)
359  {
360  my_extra = domain_state_setup(domainType, true, mcxt);
361  if (extra)
362  *extra = (void *) my_extra;
363  }
364 
365  /*
366  * Do the necessary checks to ensure it's a valid domain value.
367  */
368  domain_check_input(value, isnull, my_extra, NULL);
369 }
static void domain_check_input(Datum value, bool isnull, DomainIOData *my_extra, Node *escontext)
Definition: domains.c:135
static DomainIOData * domain_state_setup(Oid domainType, bool binary, MemoryContext mcxt)
Definition: domains.c:73
static struct @148 value
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
Oid domain_type
Definition: domains.c:49

References CurrentMemoryContext, domain_check_input(), domain_state_setup(), DomainIOData::domain_type, 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(), PLyObject_ToDomain(), populate_composite(), populate_domain(), and populate_recordset_record().

◆ domain_check_input()

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

Definition at line 135 of file domains.c.

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

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

◆ domain_in()

Datum domain_in ( PG_FUNCTION_ARGS  )

Definition at line 224 of file domains.c.

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

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

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 73 of file domains.c.

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 }
#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:2856
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2922
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021
Oid typiofunc
Definition: domains.c:51
int32 domainBaseTypmod
Definition: typcache.h:114
char typtype
Definition: typcache.h:43
Oid domainBaseType
Definition: typcache.h:113
void InitDomainConstraintRef(Oid type_id, DomainConstraintRef *ref, MemoryContext refctx, bool need_exprstate)
Definition: typcache.c:1310
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:344
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:148

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

◆ errdatatype()

int errdatatype ( Oid  datatypeOid)

Definition at line 376 of file domains.c.

377 {
378  HeapTuple tup;
379  Form_pg_type typtup;
380 
381  tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(datatypeOid));
382  if (!HeapTupleIsValid(tup))
383  elog(ERROR, "cache lookup failed for type %u", datatypeOid);
384  typtup = (Form_pg_type) GETSTRUCT(tup);
385 
387  get_namespace_name(typtup->typnamespace));
389 
390  ReleaseSysCache(tup);
391 
392  return 0; /* return value does not matter */
393 }
#define NameStr(name)
Definition: c.h:735
int err_generic_string(int field, const char *str)
Definition: elog.c:1511
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3348
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:868
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:820
@ TYPEOID
Definition: syscache.h:114

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

◆ errdomainconstraint()

int errdomainconstraint ( Oid  datatypeOid,
const char *  conname 
)

Definition at line 400 of file domains.c.

401 {
402  errdatatype(datatypeOid);
404 
405  return 0; /* return value does not matter */
406 }
#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().