Re: CAST(... ON DEFAULT) - WIP build on top of Error-Safe User Functions

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
Cc: Amul Sul <sulamul(at)gmail(dot)com>, Kirill Reshke <reshkekirill(at)gmail(dot)com>, Vik Fearing <vik(at)postgresfriends(dot)org>, Isaac Morland <isaac(dot)morland(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: CAST(... ON DEFAULT) - WIP build on top of Error-Safe User Functions
Date: 2026-03-21 08:16:07
Message-ID: CACJufxFkLLuX1VJ-J3fppCr37PHtxkvwyd_e4zNd+VYK0v0gnQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Mar 20, 2026 at 4:15 PM jian he <jian(dot)universality(at)gmail(dot)com> wrote:
>
> Handling SafeTypeCastExpr->castexpr within eval_const_expressions_mutator is
> tricky. However, if it is a FuncExpr node, we can attempt to fold its arguments
> (FuncExpr->args).
> (Not doing anything for SafeTypeCastExpr->castexpr is easier).

Just found out that eval_const_expressions_mutator should either
return a Const or recurse to every node field.
If not, the node field may have a COLLATE expression, and
ExecInitExprRec cannot cope with T_CollateExpr. For example:

SELECT CAST('1' collate "C" || 'h' AS text DEFAULT 'fallback' ON
CONVERSION ERROR);
ERROR: unrecognized node type: 32

Therefore in eval_const_expressions, we cannot skip constant folding
SafeTypeCastExpr->castexpr.
Since eval_const_expressions_mutator is not error-safe, an
ErrorSaveContext pointer was added to eval_const_expressions_context.
It's initialized to NULL, and it will be passed as
``makeNode(ErrorSaveContext)`` inside T_SafeTypeCastExpr.

Previously in [0], I mentioned that SafeTypeCastExpr->castexpr can be FuncExpr,
CollateExpr, CoerceToDomain, ArrayCoerceExpr, or CoerceViaIO or others.

Constant folding in eval_const_expressions for these nodes are
generally processed
through the simplify_function -> evaluate_function -> evaluate_expr.
evaluate_expr can process it in an error-safe manner because the escontext
(ErrorSaveContext) within eval_const_expressions_context is properly populated.

SafeTypeCastExpr->castexpr can also be any Node type if the parse transformed
source expression is the same type as the target type. See
transformTypeCast->coerce_to_target_type_extended->coerce_type_extended:
```
if (targetTypeId == inputTypeId ||
node == NULL)
{
/* no conversion needed */
return node;
}
```
However, if SafeTypeCastExpr->castexpr is the same as
SafeTypeCastExpr->source then
eval_const_expressions premature failure at "source" should be not a
big problem for us.

Since castexpr is evaluated safely in most cases during eval_const_expressions,
I am not too worried about a premature failure occurring with CAST ... DEFAULT."

[0]: https://postgr.es/m/CACJufxHw9Y3fvh+rZj4ukLo=v54Dpafzk7Xvee_wi9zFZ6pOfg@mail.gmail.com

----------------------------------------------------------------
SELECT CAST(1 as date DEFAULT NULL ON CONVERSION ERROR) as a;

transformTypeCast->coerce_to_target_type->coerce_to_target_type_extended
will make SafeTypeCastExpr->castexpr as NULL.
To deparse the above, we need to save the transformed TypeCast->arg.
Therefore, SafeTypeCastExpr->source is necessary.

--
jian
https://www.enterprisedb.com/

Attachment Content-Type Size
v23-0019-introduce-float8-safe-function.patch text/x-patch 4.0 KB
v23-0004-error-safe-for-casting-text-to-other-types-per-pg_cast.patch text/x-patch 9.7 KB
v23-0017-error-safe-for-casting-jsonb-to-other-types-per-pg_cast.patch text/x-patch 6.5 KB
v23-0010-error-safe-for-casting-numeric-to-other-types-per-pg_cast.patch text/x-patch 5.4 KB
v23-0009-error-safe-for-casting-bigint-to-other-types-per-pg_cast.patch text/x-patch 4.1 KB
v23-0008-error-safe-for-casting-integer-to-other-types-per-pg_cast.patch text/x-patch 3.1 KB
v23-0001-error-safe-for-casting-bytea-to-other-types-per-pg_cast.patch text/x-patch 2.7 KB
v23-0005-error-safe-for-casting-character-varying-to-other-types-per-pg_c.patch text/x-patch 2.3 KB
v23-0003-error-safe-for-casting-character-to-other-types-per-pg_cast.patch text/x-patch 4.8 KB
v23-0002-error-safe-for-casting-bit-varbit-to-other-types-per-pg_cast.patch text/x-patch 2.8 KB
v23-0023-error-safe-for-user-defined-CREATE-CAST.patch text/x-patch 75.1 KB
v23-0021-error-safe-for-casting-geometry-data-type.patch text/x-patch 12.5 KB
v23-0020-refactor-point_dt.patch text/x-patch 8.7 KB
v23-0018-refactor-float_overflow_error-float_underflow_error-float_zero_d.patch text/x-patch 14.5 KB
v23-0022-CAST-expr-AS-newtype-DEFAULT-expr-ON-CONVERSION-ERROR.patch text/x-patch 136.9 KB
v23-0016-error-safe-for-casting-timestamp-to-other-types-per-pg_cast.patch text/x-patch 3.3 KB
v23-0013-error-safe-for-casting-date-to-other-types-per-pg_cast.patch text/x-patch 2.4 KB
v23-0015-error-safe-for-casting-timestamptz-to-other-types-per-pg_cast.patch text/x-patch 3.8 KB
v23-0012-error-safe-for-casting-float8-to-other-types-per-pg_cast.patch text/x-patch 3.8 KB
v23-0014-error-safe-for-casting-interval-to-other-types-per-pg_cast.patch text/x-patch 2.3 KB
v23-0011-error-safe-for-casting-float4-to-other-types-per-pg_cast.patch text/x-patch 3.3 KB
v23-0007-error-safe-for-casting-macaddr8-to-other-types-per-pg_cast.patch text/x-patch 1.7 KB
v23-0006-error-safe-for-casting-inet-to-other-types-per-pg_cast.patch text/x-patch 2.0 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message ls7777 2026-03-21 08:59:11 Re: Patch for migration of the pg_commit_ts directory
Previous Message SATYANARAYANA NARLAPURAM 2026-03-21 08:15:54 Re: log XLogPrefetch stats at end of recovery