aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/domains.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2016-12-22 15:01:27 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2016-12-22 15:01:38 -0500
commit77cd0dc7e05a91be5deaed7af37eb055c1f080ed (patch)
tree4c779f0775012a4f3d784b1d48686a4f1c611969 /src/backend/utils/adt/domains.c
parent17742a05110ee0015f2861e9a4e4b1158c7e69cf (diff)
downloadpostgresql-77cd0dc7e05a91be5deaed7af37eb055c1f080ed.tar.gz
postgresql-77cd0dc7e05a91be5deaed7af37eb055c1f080ed.zip
Fix handling of expanded objects in CoerceToDomain and CASE execution.
When the input value to a CoerceToDomain expression node is a read-write expanded datum, we should pass a read-only pointer to any domain CHECK expressions and then return the original read-write pointer as the expression result. Previously we were blindly passing the same pointer to all the consumers of the value, making it possible for a function in CHECK to modify or even delete the expanded value. (Since a plpgsql function will absorb a passed-in read-write expanded array as a local variable value, it will in fact delete the value on exit.) A similar hazard of passing the same read-write pointer to multiple consumers exists in domain_check() and in ExecEvalCase, so fix those too. The fix requires adding MakeExpandedObjectReadOnly calls at the appropriate places, which is simple enough except that we need to get the data type's typlen from somewhere. For the domain cases, solve this by redefining DomainConstraintRef.tcache as okay for callers to access; there wasn't any reason for the original convention against that, other than not wanting the API of typcache.c to be any wider than it had to be. For CASE, there's no good solution except to add a syscache lookup during executor start. Per bug #14472 from Marcos Castedo. Back-patch to 9.5 where expanded values were introduced. Discussion: https://postgr.es/m/15225.1482431619@sss.pgh.pa.us
Diffstat (limited to 'src/backend/utils/adt/domains.c')
-rw-r--r--src/backend/utils/adt/domains.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/backend/utils/adt/domains.c b/src/backend/utils/adt/domains.c
index 19ee4ce9d16..07f52c55d8d 100644
--- a/src/backend/utils/adt/domains.c
+++ b/src/backend/utils/adt/domains.c
@@ -35,6 +35,7 @@
#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"
@@ -157,9 +158,14 @@ domain_check_input(Datum value, bool isnull, DomainIOData *my_extra)
* Set up value to be returned by CoerceToDomainValue
* nodes. Unlike ExecEvalCoerceToDomain, this econtext
* couldn't be shared with anything else, so no need to
- * save and restore fields.
+ * save and restore fields. But we do need to protect the
+ * passed-in value against being changed by called
+ * functions. (It couldn't be a R/W expanded object for
+ * most uses, but that seems possible for domain_check().)
*/
- econtext->domainValue_datum = value;
+ econtext->domainValue_datum =
+ MakeExpandedObjectReadOnly(value, isnull,
+ my_extra->constraint_ref.tcache->typlen);
econtext->domainValue_isNull = isnull;
conResult = ExecEvalExprSwitchContext(con->check_expr,