]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Avoid scribbling on input node tree in CREATE/ALTER DOMAIN.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 18 Jun 2021 16:09:22 +0000 (12:09 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 18 Jun 2021 16:09:22 +0000 (12:09 -0400)
This works fine in the "simple Query" code path; but if the
statement is in the plan cache then it's corrupted for future
re-execution.  Apply copyObject() to protect the original
tree from modification, as we've done elsewhere.

This narrow fix is applied only to the back branches.  In HEAD,
the problem was fixed more generally by commit 7c337b6b5; but
that changed ProcessUtility's API, so it's infeasible to
back-patch.

Per bug #17053 from Charles Samborski.

Discussion: https://postgr.es/m/931771.1623893989@sss.pgh.pa.us
Discussion: https://postgr.es/m/17053-3ca3f501bbc212b4@postgresql.org

src/backend/commands/typecmds.c

index d938d84962ab9df83ca04a9615c48c15657c7675..94521cb2ebe744188a0e7c5be1a3cf6bc90bc5b6 100644 (file)
@@ -906,10 +906,12 @@ DefineDomain(CreateDomainStmt *stmt)
                                        pstate = make_parsestate(NULL);
 
                                        /*
-                                        * Cook the constr->raw_expr into an expression. Note:
-                                        * name is strictly for error message
+                                        * Cook the constr->raw_expr into an expression; copy it
+                                        * in case the input is in plan cache.  Note: name is used
+                                        * only for error messages.
                                         */
-                                       defaultExpr = cookDefault(pstate, constr->raw_expr,
+                                       defaultExpr = cookDefault(pstate,
+                                                                                         copyObject(constr->raw_expr),
                                                                                          basetypeoid,
                                                                                          basetypeMod,
                                                                                          domainName);
@@ -2215,10 +2217,10 @@ AlterDomainDefault(List *names, Node *defaultRaw)
                pstate = make_parsestate(NULL);
 
                /*
-                * Cook the colDef->raw_expr into an expression. Note: Name is
-                * strictly for error message
+                * Cook the raw default into an expression; copy it in case the input
+                * is in plan cache.  Note: name is used only for error messages.
                 */
-               defaultExpr = cookDefault(pstate, defaultRaw,
+               defaultExpr = cookDefault(pstate, copyObject(defaultRaw),
                                                                  typTup->typbasetype,
                                                                  typTup->typtypmod,
                                                                  NameStr(typTup->typname));
@@ -3084,7 +3086,12 @@ domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid,
        pstate->p_pre_columnref_hook = replace_domain_constraint_value;
        pstate->p_ref_hook_state = (void *) domVal;
 
-       expr = transformExpr(pstate, constr->raw_expr, EXPR_KIND_DOMAIN_CHECK);
+       /*
+        * Transform the expression; first we must copy the input, in case it's in
+        * plan cache.
+        */
+       expr = transformExpr(pstate, copyObject(constr->raw_expr),
+                                                EXPR_KIND_DOMAIN_CHECK);
 
        /*
         * Make sure it yields a boolean result.