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: Corey Huinker <corey.huinker@gmail.com>
Cc: Vik Fearing <vik@postgresfriends.org>,
Isaac Morland <isaac.morland@gmail.com>, pgsql-hackers@lists.postgresql.org
Date: 2025-08-11T06:18:44Z
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
- v6-0016-error-safe-for-casting-character-varying-to-other-types-p.patch (text/x-patch) patch v6-0016
- v6-0015-error-safe-for-casting-timestamp-to-other-types-per-pg_ca.patch (text/x-patch) patch v6-0015
- v6-0014-error-safe-for-casting-timestamptz-to-other-types-per-pg_.patch (text/x-patch) patch v6-0014
- v6-0017-make-ArrayCoerceExpr-error-safe.patch (text/x-patch) patch v6-0017
- v6-0018-CAST-expr-AS-newtype-DEFAULT-ON-ERROR.patch (text/x-patch) patch v6-0018
- v6-0010-error-safe-for-casting-text-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0010
- v6-0012-error-safe-for-casting-interval-to-other-types-per-pg_cas.patch (text/x-patch) patch v6-0012
- v6-0013-error-safe-for-casting-macaddr8-to-other-types-per-pg_cas.patch (text/x-patch) patch v6-0013
- v6-0009-error-safe-for-casting-date-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0009
- v6-0011-error-safe-for-casting-inet-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0011
- v6-0008-error-safe-for-casting-jsonb-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0008
- v6-0006-error-safe-for-casting-float4-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0006
- v6-0007-error-safe-for-casting-float8-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0007
- v6-0005-error-safe-for-casting-numeric-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0005
- v6-0004-error-safe-for-casting-bigint-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0004
- v6-0003-error-safe-for-casting-integer-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0003
- v6-0001-error-safe-for-casting-bytea-to-other-types-per-pg_cast.patch (text/x-patch) patch v6-0001
- v6-0002-error-safe-for-casting-character-to-other-types-per-pg_ca.patch (text/x-patch) patch v6-0002
On Tue, Aug 5, 2025 at 12:10 PM Corey Huinker <corey.huinker@gmail.com> wrote: >> >> In the end, it seems we need to make all these functions in the below >> query error safe. >> select castsource::regtype, casttarget::regtype, castfunc, >> castcontext,castmethod, pp.prosrc, pp.proname from pg_cast pc join pg_proc pp on >> pp.oid = pc.castfunc and pc.castfunc > 0 >> order by castsource::regtype; >> It's a lot of work, but seems doable, after playing around with it. > > It is do-able. But that's just the cast functions that are part of core postgres. > hi. it's doable for most of the data types. but I found geometric type related cast function refactoring to error safe is quite challenging, so I skip that part refactoring. >> >> I don't think we need to change the pg_cast catalog entry, >> we just need to make these function (pg_cast.castmethod) errors safe. > > That would break any user-defined cast functions that were not also error safe, which is to say all of them. > > We need a way for user-defined cast functions to indicate whether or not they are error safe, and handle both situations accordingly (i.e. fail a CAST ON DEFAULT when the user-defined cast is not error-safe). > I understand what you mean now. CREATE CAST can use built-in functions too, we have no way to check whether these CREATE CAST associated functions are error safe or not. for example: CREATE CAST (jsonpath AS bytea) WITH FUNCTION jsonpath_send (jsonpath ) AS ASSIGNMENT; select '$'::jsonpath::bytea; To avoid this situation, we have to add a new column to pg_cast to indicate whether a cast function is error-safe. It's unlikely a pg_cast entry has two functions, one is error safe, one is not, adding pg_cast.casterrorsafefunc would not be that appropriate. so I choose pg_cast.casterrorsafe would be fine. pg_cast.casterrorsafe true means castfunc function is error safe, we can use it as safe cast evaluation (CAST... DEFAULT defexpr ON CONVERSION ERROR) please check the attached V6 script: v6-0001 to v6-0016 is about making existing pg_cast.castfunc function error safe. (commit message have associated query to explain the refactoring, as mentioned above, geometric and money associated type not refactored yet) v6-0017-make-ArrayCoerceExpr-error-safe.patch v6-0018-CAST-expr-AS-newtype-DEFAULT-ON-ERROR.patch is about (CAST... DEFAULT defexpr ON CONVERSION ERROR).