diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c index c69cac4015..7d5cdeaa17 100644 --- a/src/backend/parser/parse_coerce.c +++ b/src/backend/parser/parse_coerce.c @@ -739,26 +739,38 @@ coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod, CoercionPathType pathtype; Oid funcId; - /* - * A negative typmod is assumed to mean that no coercion is wanted. Also, - * skip coercion if already done. - */ - if (targetTypMod < 0 || targetTypMod == exprTypmod(node)) + /* Skip coercion if already done */ + if (targetTypMod == exprTypmod(node)) return node; + /* Suppress display of nested coercion steps */ + if (hideInputCoercion) + hide_coercion_node(node); + pathtype = find_typmod_coercion_function(targetTypeId, &funcId); if (pathtype != COERCION_PATH_NONE) { - /* Suppress display of nested coercion steps */ - if (hideInputCoercion) - hide_coercion_node(node); - node = build_coercion_expression(node, pathtype, funcId, targetTypeId, targetTypMod, cformat, location, isExplicit); } + else + { + /* + * We don't need to perform any actual coercion step, but we should + * apply a RelabelType to ensure that the expression exposes the + * intended typmod. + */ + RelabelType *rt; + + rt = makeRelabelType((Expr *) node, targetTypeId, targetTypMod, + exprCollation(node), + cformat); + rt->location = location; + node = (Node *) rt; + } return node; }