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-20 08:15:12
Message-ID: CACJufxGbw9iNT8QVm4QD9cPFKnDnvDBQp7AGxkoqDa-JjzVXmg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Mar 18, 2026 at 11:11 AM Corey Huinker <corey(dot)huinker(at)gmail(dot)com> wrote:
>>
>> + if (inputElementBaseType == MONEYOID ||
>> + targetElementBaseType == MONEYOID ||
>> + (inputElementBaseType == CIRCLEOID &&
>> + targetElementBaseType == POLYGONOID))
>> + {
>> + errorsafe_coercion = false;
>> + }
>
> What if we just reject cast functions with a non-null prosqlbody?
>

src/include/access/transam.h
* OIDs 12000-16383 are reserved for unpinned objects created by initdb's
* post-bootstrap processing. initdb forces the OID generator up to
* 12000 as soon as it's made the pinned objects it's responsible for.
Based on the above comments, I think comparing FirstUnpinnedObjectId
should be enough.

Rebased because of commit:
https://git.postgresql.org/cgit/postgresql.git/commit/?id=ba21f5bf8aff277aa1659a51d26109e0914df182
which add casting between bytea and uuid.

ExecInitFunc now always sets fcinfo->context from state->escontext
```
InitFunctionCallInfoData(*fcinfo, flinfo,
nargs, inputcollid,
(Node *) state->escontext, NULL);
```
This change in `ExecInitFunc` means **all** FuncExpr evaluations within a
SafeTypeCastExpr context will have their `fcinfo->context` set to the
`ErrorSaveContext`. This is by design for cast functions, but could
accidentally affect non-cast functions that appear in the cast expression
tree and happen to check `fcinfo->context`.

FunctionCallInfoBaseData->context is a generic poiner, it canbe other
than ErrorSaveContext.
For example, in ExecCallTriggerFunc we have:
```
InitFunctionCallInfoData(*fcinfo, finfo, 0,
InvalidOid, (Node *) trigdata, NULL);
```
Because of this, we must differentiate our FuncExpr nodes. Soft-error evaluation
should be only for FuncExpr nodes produced by build_coercion_expression.

We can add a boolean (errorsafe) flag directly to the FuncExpr struct.
This flag to indicate whether this specific expression should be evaluated in an
error-safe manner. In the future, we can also use this flag for

CAST(expr AS type FORMAT 'template' DEFAULT defexpr ON CONVERSION ERROR)

In build_coercion_expression, we set FuncExpr->errorsafe if the specific cast
function needs to be evaluated in an error-safe manner.
build_coercion_expression is also
used in other callers, such as coerce_type_typmod, coerce_to_domain,
that means more refactoring in parse_coerce.c.

V21-0022 JIT related change is totally wrong.
Matheus Alcantara <matheusssilv97(at)gmail(dot)com> helped me resolve the JIT issue.
Previously, I wasn't using SET jit_above_cost = 0; to properly
exercise the JIT-related tests.

You mentioned refactoring RangeVarGetRelidExtended in [1],
With a hint from Andrew Dunstan, we can actually reduce the code
duplication, see v22-0004.
[1] https://postgr.es/m/CADkLM=cZ7N+f4Bj-8MiccFcTASjBB2r1_s3BD9OFbbQOwK01cg@mail.gmail.com

As mentioned before, FunctionCallInfoBaseData->context can be other
Node other than ErrorSaveContext.
Therefore for geometry error safe refactoring, we must pass NULL, not
fcinfo->context, for non-cast realted function.
For example, function point_add_point within box_add, the third
parameter should be NULL, not fcinfo->context.

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).

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jelte Fennema-Nio 2026-03-20 08:19:19 Re: meson: Make test output much more useful on failure (both in CI and locally)
Previous Message Alvaro Herrera 2026-03-20 07:59:23 Re: Adding REPACK [concurrently]