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: 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: 2025-08-11 06:18:44
Message-ID: CACJufxE053=bO3pDUpGba6Yz3VGpU_XCbg4HO6Rew5EJ7k7VnQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Aug 5, 2025 at 12:10 PM Corey Huinker <corey(dot)huinker(at)gmail(dot)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).

Attachment Content-Type Size
v6-0016-error-safe-for-casting-character-varying-to-other-types-p.patch text/x-patch 1.9 KB
v6-0015-error-safe-for-casting-timestamp-to-other-types-per-pg_ca.patch text/x-patch 3.3 KB
v6-0014-error-safe-for-casting-timestamptz-to-other-types-per-pg_.patch text/x-patch 3.7 KB
v6-0017-make-ArrayCoerceExpr-error-safe.patch text/x-patch 2.1 KB
v6-0018-CAST-expr-AS-newtype-DEFAULT-ON-ERROR.patch text/x-patch 131.8 KB
v6-0010-error-safe-for-casting-text-to-other-types-per-pg_cast.patch text/x-patch 10.3 KB
v6-0012-error-safe-for-casting-interval-to-other-types-per-pg_cas.patch text/x-patch 2.0 KB
v6-0013-error-safe-for-casting-macaddr8-to-other-types-per-pg_cas.patch text/x-patch 1.4 KB
v6-0009-error-safe-for-casting-date-to-other-types-per-pg_cast.patch text/x-patch 2.0 KB
v6-0011-error-safe-for-casting-inet-to-other-types-per-pg_cast.patch text/x-patch 1.6 KB
v6-0008-error-safe-for-casting-jsonb-to-other-types-per-pg_cast.patch text/x-patch 6.1 KB
v6-0006-error-safe-for-casting-float4-to-other-types-per-pg_cast.patch text/x-patch 2.5 KB
v6-0007-error-safe-for-casting-float8-to-other-types-per-pg_cast.patch text/x-patch 3.0 KB
v6-0005-error-safe-for-casting-numeric-to-other-types-per-pg_cast.patch text/x-patch 5.1 KB
v6-0004-error-safe-for-casting-bigint-to-other-types-per-pg_cast.patch text/x-patch 3.8 KB
v6-0003-error-safe-for-casting-integer-to-other-types-per-pg_cast.patch text/x-patch 2.8 KB
v6-0001-error-safe-for-casting-bytea-to-other-types-per-pg_cast.patch text/x-patch 2.0 KB
v6-0002-error-safe-for-casting-character-to-other-types-per-pg_ca.patch text/x-patch 4.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2025-08-11 06:44:01 Re: [Patch] add new parameter to pg_replication_origin_session_setup
Previous Message Peter Eisentraut 2025-08-11 06:04:15 Generate GUC tables from .dat file