Re: CAST(... ON DEFAULT) - WIP build on top of Error-Safe User Functions
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: Amul Sul <sulamul@gmail.com>
Cc: Corey Huinker <corey.huinker@gmail.com>,
Vik Fearing <vik@postgresfriends.org>, Isaac Morland <isaac.morland@gmail.com>, pgsql-hackers@lists.postgresql.org
Date: 2025-12-08T14:58:00Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Make cast functions to type money error safe
- b36b95640487 19 (unreleased) landed
-
Make cast function from circle to polygon error safe
- 26f9012beecf 19 (unreleased) landed
-
Make geometry cast functions error safe
- 45cdaf3665be 19 (unreleased) landed
-
Make cast functions from jsonb error safe
- 10e4d8aaf46f 19 (unreleased) landed
-
Make many cast functions error safe
- e2f289e5b9b8 19 (unreleased) landed
-
Add SQL/JSON query functions
- 6185c9737cf4 17.0 cited
-
Add soft error handling to some expression nodes
- aaaf9449ec6b 17.0 cited
Attachments
- v14-0016-error-safe-for-casting-timestamp-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0016
- v14-0019-CAST-expr-AS-newtype-DEFAULT-ON-ERROR.patch (text/x-patch) patch v14-0019
- v14-0017-error-safe-for-casting-jsonb-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0017
- v14-0018-error-safe-for-casting-geometry-data-type.patch (text/x-patch) patch v14-0018
- v14-0015-error-safe-for-casting-timestamptz-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0015
- v14-0014-error-safe-for-casting-interval-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0014
- v14-0013-error-safe-for-casting-date-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0013
- v14-0012-error-safe-for-casting-float8-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0012
- v14-0010-error-safe-for-casting-numeric-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0010
- v14-0011-error-safe-for-casting-float4-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0011
- v14-0009-error-safe-for-casting-bigint-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0009
- v14-0008-error-safe-for-casting-integer-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0008
- v14-0007-error-safe-for-casting-macaddr8-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0007
- v14-0005-error-safe-for-casting-character-varying-to-other-types-per-pg_c.patch (text/x-patch) patch v14-0005
- v14-0006-error-safe-for-casting-inet-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0006
- v14-0004-error-safe-for-casting-character-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0004
- v14-0003-error-safe-for-casting-character-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0003
- v14-0001-error-safe-for-casting-bytea-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0001
- v14-0002-error-safe-for-casting-bit-varbit-to-other-types-per-pg_cast.patch (text/x-patch) patch v14-0002
On Thu, Dec 4, 2025 at 8:47 PM Amul Sul <sulamul@gmail.com> wrote:
>
> Regarding v13-0019 and v13-0020 patches, I have a bunch of comments
> for the code, but I'd like to understand the implemented design first.
> I have some basic questions regarding the commit message and code
> comment, as follows:
>
> "
> We cannot simply prohibit user-defined functions in pg_cast for safe cast
> evaluation because CREATE CAST can also utilize built-in functions. So, to
> completely disallow custom casts created via CREATE CAST used in safe cast
> evaluation, a new field in pg_cast would unfortunately be necessary.
> "
>
> I might not have understood the implementation completely -- can't we
> use fmgr_last_builtin_oid to detect built-in functions?
> --
hi, Amul.
I have removed the pg_cast.casterrorsafe field.
But in the future, to make the below (Examples) CASTs associated with
built-in function
error safe, I think we need pg_cast.casterrorsafe. Otherwise,
how can we know if the pg_cast.castfunc (built-in function) is error
safe or not.
CREATE CAST (interval AS uuid ) WITH FUNCTION uuidv7(interval);
CREATE CAST (jsonb AS text) WITH FUNCTION jsonb_array_element_text(jsonb, int);
CREATE CAST (json AS text) WITH FUNCTION json_array_element_text(json, int);
I’ve added a dedicated function: CoercionErrorSafeCheck, which
checks whether the cast can be evaluated safely. If not, we throw an error.
SELECT CAST('A' AS DATE DEFAULT NULL ON CONVERSION ERROR)
Here, 'A' is an Unknown Const. As mentioned earlier, Unknown Const Node must be
coerced error safe, otherwise, it will raise an error instead of returning NULL.
The coercion of Unknown Const happens inside coerce_to_target_type, which means
part of that function must run in an error safe. coerce_to_target_type is used
widely, adding another parameter to it seems no good, so I introduced
coerce_to_target_type_extended, coerce_type_extend.
CoerceUnknownConstSafe is being removed.
stringTypeDatumSafe, OidInputFunctionCallSafe functions are needed for coercing
Unknown Const to the target type. Refactoring existing function seems not
ideal, so i invented these two functions.
--
jian
https://www.enterprisedb.com