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

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Peter Eisentraut <peter(at)eisentraut(dot)org>
Cc: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>, 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-25 13:13:49
Message-ID: CACJufxHx-UfprE6P4_ZB_cOYktHd4pLMNx=jWJFOGGGFj2YZWQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Mar 24, 2026 at 9:13 PM Peter Eisentraut <peter(at)eisentraut(dot)org> wrote:
>
> In patch 0012, I did not apply the changes involving
> float_*flow_error() in dtof(). These should be considered together
> with the error handling changes in patch 0018 (about which below).
>
> Patch 0004 is probably ok, I just need to study a bit more, since it's
> a bit different from the other ones.
>
> In the comment for patch 0008 you write that supporting soft errors in
> int4_cash() is not easy. I suppose this is because of the external
> function call to int8mul()? Maybe that was necessary in ancient
> times, but int8mul() is nowadays just a fmgr wrapper around
> pg_mul_s64_overflow(), which you can call directly in int4_cash(), and
> then it should be easy.
>

cash_numeric is not easy to refactor safely,
To do this, we have to refactor (numeric_round, numeric_div), which
will require refactoring more functions.
So CoercionErrorSafe_Internal still checks whether the source
element/base type is MONEYOID.

> About patch 0017 (jsonb): Calling ereturn() in a function that returns
> void, well, you make that work, but it seems a bit weird. Maybe
> cannotCastJsonbValue() could be changed to return a dummy datum, and
> then you could just call it like
>
> return cannotCastJsonbValue(...);
>
ok.

> Similarly, about patch 0018 (refactor float error): I think this is a
> fragile API. float_overflow_error() now will either ereport or just
> errsave. But nothing enforces that callers do the right thing. For
> example, in patch 0019, you change float_zero_divide_error(NULL) to
> float_zero_divide_error(escontext), but don't insert a return, so the
> code will continue to perform the division. Similar to patch 0017,
> maybe these functions should be changed to have a non-void return
> value. Or maybe make separate functions to be used in places where we
> want soft error handling.
>

Add argument to float_overflow_error, extern void
float_underflow_error, float_zero_divide_error
requires extensive refactoring.

So I have added
+extern float8 float_overflow_error_ext(struct Node *escontext);
+extern float8 float_underflow_error_ext(struct Node *escontext);
+extern float8 float_zero_divide_error_ext(struct Node *escontext);

> In the comment for patch 0021 you write that the function casting type
> circle to type polygon cannot be error safe, because it's a SQL
> language function. I suggest to convert this to a C function and make
> the required changes there.
>
Please see 007.
Now with this entire v24 patchset, the only built-in cast function
that is not error safe is numeric_cash.

I'll think about CREATE CAST ... WITH SAFE FUNCTION syntax, later.
Right now, I am still using CoercionErrorSafe_Internal to check if CAST DEFAULT
can be applied to this combination of source type and target type.

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

Attachment Content-Type Size
v24-0008-error-safe-for-casting-money-data-type.patch text/x-patch 3.7 KB
v24-0005-refactor-point_dt.patch text/x-patch 8.7 KB
v24-0002-error-safe-for-casting-jsonb-to-other-types-per-pg_cast.patch text/x-patch 6.4 KB
v24-0006-error-safe-for-casting-geometry-data-type.patch text/x-patch 12.5 KB
v24-0009-CAST-expr-AS-newtype-DEFAULT-expr-ON-CONVERSION-ERROR.patch text/x-patch 136.3 KB
v24-0004-introduce-float8-safe-function.patch text/x-patch 3.9 KB
v24-0007-error-safe-for-casting-circle-to-polygon.patch text/x-patch 5.2 KB
v24-0003-extended-function-for-float-overflow-underflow-zero_divide.patch text/x-patch 2.3 KB
v24-0001-error-safe-for-casting-text-to-other-types-per-pg_cast.patch text/x-patch 9.7 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Viktor Holmberg 2026-03-25 13:48:54 Re: [PATCH] no table rewrite when set column type to constrained domain
Previous Message Fujii Masao 2026-03-25 12:39:00 Re: Exit walsender before confirming remote flush in logical replication