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-12-10T08:57:09Z
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
- v15-0022-error-safe-for-user-defined-CREATE-CAST.patch (text/x-patch) patch v15-0022
- v15-0019-refactor-point_dt.patch (text/x-patch) patch v15-0019
- v15-0020-error-safe-for-casting-geometry-data-type.patch (text/x-patch) patch v15-0020
- v15-0018-introduce-float8-safe-function.patch (text/x-patch) patch v15-0018
- v15-0021-CAST-expr-AS-newtype-DEFAULT-ON-ERROR.patch (text/x-patch) patch v15-0021
- v15-0016-error-safe-for-casting-jsonb-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0016
- v15-0017-refactor-float_overflow_error-float_underflow_error-float_zero_d.patch (text/x-patch) patch v15-0017
- v15-0015-error-safe-for-casting-timestamp-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0015
- v15-0014-error-safe-for-casting-timestamptz-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0014
- v15-0013-error-safe-for-casting-interval-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0013
- v15-0012-error-safe-for-casting-date-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0012
- v15-0011-error-safe-for-casting-float8-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0011
- v15-0010-error-safe-for-casting-float4-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0010
- v15-0009-error-safe-for-casting-numeric-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0009
- v15-0007-error-safe-for-casting-integer-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0007
- v15-0008-error-safe-for-casting-bigint-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0008
- v15-0005-error-safe-for-casting-inet-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0005
- v15-0006-error-safe-for-casting-macaddr8-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0006
- v15-0004-error-safe-for-casting-character-varying-to-other-types-per-pg_c.patch (text/x-patch) patch v15-0004
- v15-0002-error-safe-for-casting-character-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0002
- v15-0001-error-safe-for-casting-bit-varbit-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0001
- v15-0003-error-safe-for-casting-character-to-other-types-per-pg_cast.patch (text/x-patch) patch v15-0003
On Tue, Dec 9, 2025 at 11:39 AM jian he <jian.universality@gmail.com> wrote:
>
> On Mon, Dec 1, 2025 at 1:41 PM Corey Huinker <corey.huinker@gmail.com> wrote:
> >>
> > No, I meant implementing the syntax for being able to declare a custom CAST function as safe (or not). Basically adding the [SAFE] to
> >
> > CREATE CAST (source_type AS target_type)
> > WITH [SAFE] FUNCTION function_name [ (argument_type [, ...]) ]
> >
> > I'm not tied to this syntax choice, but this one seemed the most obvious and least invasive.
> >
hi.
please see the attached v15.
the primary implementation of CAST DEFAULT is contained in V15-0021.
changes compared to v14.
1. separate patch (v15-0017) for float error.
-pg_noreturn extern void float_overflow_error(void);
-pg_noreturn extern void float_underflow_error(void);
-pg_noreturn extern void float_zero_divide_error(void);
+extern void float_overflow_error(struct Node *escontext);
+extern void float_underflow_error(struct Node *escontext);
+extern void float_zero_divide_error(struct Node *escontext);
2. separate patch (v15-0018) for newly added float8 functions:
float8_pl_safe
float8_mi_safe
float8_mul_safe
float8_div_safe
refactoring existing functions is too invasive, I choose not to.
3. refactor point_dt (v15-0019). This is necessary for making geometry data type
error-safe, separate from the main patch (v15-0020). I hope to make it easier to
review.
-static inline float8 point_dt(Point *pt1, Point *pt2);
+static inline float8 point_dt(Point *pt1, Point *pt2, Node *escontext);
4. skip compile DEFAULT expression (ExecInitExprRec) for binary coercion cast,
as mentioned before. See ExecInitSafeTypeCastExpr.
5. Support user-defined type cast error-safe, see v15-0022.
user-defined error-safe cast syntax:
CREATE CAST (source_type AS target_type)
WITH [SAFE] FUNCTION function_name [ (argument_type [, ...]) ]
[ AS ASSIGNMENT | AS IMPLICIT ]
this only adds a new keyword SAFE.
This works for C and internal language functions only now.
To make it really usable, I have made citext, hstore module castfunc error safe.
A new column: pg_cast.casterrorsafe was added, this is needed for
CREATE CAST WITH SAFE FUNCTION.
+select CAST(ARRAY['a','g','b','h',null,'i'] AS hstore
+ DEFAULT NULL ON CONVERSION ERROR);
+ array
+-------
+
+(1 row)
+
6. slightly polished the doc.
--
jian
https://www.enterprisedb.com/