1 /*-------------------------------------------------------------------------
4 * I/O functions for domain types.
6 * The output functions for a domain type are just the same ones provided
7 * by its underlying base type. The input functions, however, must be
8 * prepared to apply any constraints defined by the type. So, we create
9 * special input functions that invoke the base type's input function
10 * and then check the constraints.
12 * The overhead required for constraint checking can be high, since examining
13 * the catalogs to discover the constraints for a given domain is not cheap.
14 * We have three mechanisms for minimizing this cost:
15 * 1. In a nest of domains, we flatten the checking of all the levels
16 * into just one operation.
17 * 2. We cache the list of constraint items in the FmgrInfo struct
18 * passed by the caller.
19 * 3. If there are CHECK constraints, we cache a standalone ExprContext
20 * to evaluate them in.
23 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
28 * $PostgreSQL: pgsql/src/backend/utils/adt/domains.c,v 1.4 2006/10/04 00:29:58 momjian Exp $
30 *-------------------------------------------------------------------------
34 #include "commands/typecmds.h"
35 #include "executor/executor.h"
36 #include "utils/builtins.h"
37 #include "utils/lsyscache.h"
41 * structure to cache state across multiple calls
43 typedef struct DomainIOData
46 /* Data needed to call base type's input function */
51 /* List of constraint items to check */
52 List *constraint_list;
53 /* Context for evaluating CHECK constraints in */
54 ExprContext *econtext;
55 /* Memory context this cache is in */
61 * domain_state_setup - initialize the cache for a new domain type.
64 domain_state_setup(DomainIOData *my_extra, Oid domainType, bool binary,
68 MemoryContext oldcontext;
70 /* Mark cache invalid */
71 my_extra->domain_type = InvalidOid;
73 /* Find out the base type */
74 my_extra->typtypmod = -1;
75 baseType = getBaseTypeAndTypmod(domainType, &my_extra->typtypmod);
76 if (baseType == domainType)
78 (errcode(ERRCODE_DATATYPE_MISMATCH),
79 errmsg("type %s is not a domain",
80 format_type_be(domainType))));
82 /* Look up underlying I/O function */
84 getTypeBinaryInputInfo(baseType,
86 &my_extra->typioparam);
88 getTypeInputInfo(baseType,
90 &my_extra->typioparam);
91 fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc, mcxt);
93 /* Look up constraints for domain */
94 oldcontext = MemoryContextSwitchTo(mcxt);
95 my_extra->constraint_list = GetDomainConstraints(domainType);
96 MemoryContextSwitchTo(oldcontext);
98 /* We don't make an ExprContext until needed */
99 my_extra->econtext = NULL;
100 my_extra->mcxt = mcxt;
102 /* Mark cache valid */
103 my_extra->domain_type = domainType;
107 * domain_check_input - apply the cached checks.
109 * This is extremely similar to ExecEvalCoerceToDomain in execQual.c.
112 domain_check_input(Datum value, bool isnull, DomainIOData *my_extra)
114 ExprContext *econtext = my_extra->econtext;
117 foreach(l, my_extra->constraint_list)
119 DomainConstraintState *con = (DomainConstraintState *) lfirst(l);
121 switch (con->constrainttype)
123 case DOM_CONSTRAINT_NOTNULL:
126 (errcode(ERRCODE_NOT_NULL_VIOLATION),
127 errmsg("domain %s does not allow null values",
128 format_type_be(my_extra->domain_type))));
130 case DOM_CONSTRAINT_CHECK:
135 /* Make the econtext if we didn't already */
136 if (econtext == NULL)
138 MemoryContext oldcontext;
140 oldcontext = MemoryContextSwitchTo(my_extra->mcxt);
141 econtext = CreateStandaloneExprContext();
142 MemoryContextSwitchTo(oldcontext);
143 my_extra->econtext = econtext;
147 * Set up value to be returned by CoerceToDomainValue
148 * nodes. Unlike ExecEvalCoerceToDomain, this econtext
149 * couldn't be shared with anything else, so no need to
150 * save and restore fields.
152 econtext->domainValue_datum = value;
153 econtext->domainValue_isNull = isnull;
155 conResult = ExecEvalExprSwitchContext(con->check_expr,
160 !DatumGetBool(conResult))
162 (errcode(ERRCODE_CHECK_VIOLATION),
163 errmsg("value for domain %s violates check constraint \"%s\"",
164 format_type_be(my_extra->domain_type),
169 elog(ERROR, "unrecognized constraint type: %d",
170 (int) con->constrainttype);
176 * Before exiting, call any shutdown callbacks and reset econtext's
177 * per-tuple memory. This avoids leaking non-memory resources, if
178 * anything in the expression(s) has any.
181 ReScanExprContext(econtext);
186 * domain_in - input routine for any domain type.
189 domain_in(PG_FUNCTION_ARGS)
193 DomainIOData *my_extra;
197 * Since domain_in is not strict, we have to check for null inputs. The
198 * typioparam argument should never be null in normal system usage, but it
199 * could be null in a manual invocation --- if so, just return null.
204 string = PG_GETARG_CSTRING(0);
207 domainType = PG_GETARG_OID(1);
210 * We arrange to look up the needed info just once per series of calls,
211 * assuming the domain type doesn't change underneath us.
213 my_extra = (DomainIOData *) fcinfo->flinfo->fn_extra;
214 if (my_extra == NULL)
216 my_extra = (DomainIOData *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
217 sizeof(DomainIOData));
218 domain_state_setup(my_extra, domainType, false,
219 fcinfo->flinfo->fn_mcxt);
220 fcinfo->flinfo->fn_extra = (void *) my_extra;
222 else if (my_extra->domain_type != domainType)
223 domain_state_setup(my_extra, domainType, false,
224 fcinfo->flinfo->fn_mcxt);
227 * Invoke the base type's typinput procedure to convert the data.
229 value = InputFunctionCall(&my_extra->proc,
231 my_extra->typioparam,
232 my_extra->typtypmod);
235 * Do the necessary checks to ensure it's a valid domain value.
237 domain_check_input(value, (string == NULL), my_extra);
242 PG_RETURN_DATUM(value);
246 * domain_recv - binary input routine for any domain type.
249 domain_recv(PG_FUNCTION_ARGS)
253 DomainIOData *my_extra;
257 * Since domain_recv is not strict, we have to check for null inputs. The
258 * typioparam argument should never be null in normal system usage, but it
259 * could be null in a manual invocation --- if so, just return null.
264 buf = (StringInfo) PG_GETARG_POINTER(0);
267 domainType = PG_GETARG_OID(1);
270 * We arrange to look up the needed info just once per series of calls,
271 * assuming the domain type doesn't change underneath us.
273 my_extra = (DomainIOData *) fcinfo->flinfo->fn_extra;
274 if (my_extra == NULL)
276 my_extra = (DomainIOData *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
277 sizeof(DomainIOData));
278 domain_state_setup(my_extra, domainType, true,
279 fcinfo->flinfo->fn_mcxt);
280 fcinfo->flinfo->fn_extra = (void *) my_extra;
282 else if (my_extra->domain_type != domainType)
283 domain_state_setup(my_extra, domainType, true,
284 fcinfo->flinfo->fn_mcxt);
287 * Invoke the base type's typreceive procedure to convert the data.
289 value = ReceiveFunctionCall(&my_extra->proc,
291 my_extra->typioparam,
292 my_extra->typtypmod);
295 * Do the necessary checks to ensure it's a valid domain value.
297 domain_check_input(value, (buf == NULL), my_extra);
302 PG_RETURN_DATUM(value);