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: 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-17 05:51:15
Message-ID: CACJufxH4LrCpL63SRYO3zVk46YdD4--VYQoBL7GmHmCm=NCAJQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Mar 16, 2026 at 11:48 AM Corey Huinker <corey(dot)huinker(at)gmail(dot)com> wrote:
>
> LGTM.

Thanks for the review.
For the context of
v20-0022-CAST-expr-AS-newtype-DEFAULT-expr-ON-CONVERSION-ERROR.patch

+/*
+ * Check type coercion is error safe or not. If not then report error
+ */
+static void
+CoercionErrorSafeCheck(ParseState *pstate, Node *castexpr, Node *source,
+ Oid inputType, Oid targetType)
+{
+ Oid inputBaseType;
+ Oid targetBaseType;
+ Oid inputElementType;
+ Oid inputElementBaseType;
+ Oid targetElementType;
+ Oid targetElementBaseType;
+ inputElementType = get_element_type(inputType);
+
+ if (OidIsValid(inputElementType))
+ inputElementBaseType = getBaseType(inputElementType);
+ else
+ {
+ inputBaseType = getBaseType(inputType);
+ inputElementBaseType = get_element_type(inputBaseType);
+
+ if (!OidIsValid(inputElementBaseType))
+ inputElementBaseType = inputBaseType;
+ }
+
+ targetElementType = get_element_type(targetType);
+
+ if (OidIsValid(targetElementType))
+ targetElementBaseType = getBaseType(targetElementType);
+ else
+ {
+ targetBaseType = getBaseType(targetType);
+ targetElementBaseType = get_element_type(targetBaseType);
+
+ if (!OidIsValid(targetElementBaseType))
+ targetElementBaseType = targetBaseType;
+ }
+
+ if (inputElementBaseType == MONEYOID ||
+ targetElementBaseType == MONEYOID ||
+ (inputElementBaseType == CIRCLEOID &&
+ targetElementBaseType == POLYGONOID))
+ {
+ errorsafe_coercion = false;
+ }
+

Inspired by https://commitfest.postgresql.org/patch/5759/
I found out the above code is wrong, because it failed to imagine
cases like domain over a domain array.
for example:
CREATE DOMAIN d_int42 as int check (value = 42) NOT NULL;
CREATE DOMAIN d_int42arr as d_int42[];
CREATE DOMAIN d_int42arr1 as d_int42arr;
select typname,typtype, typbasetype::regtype, typelem::regtype
from pg_type
where typname in ('d_int42', 'd_int42arr', 'd_int42arr1', '_d_int42');

Therefore we need a little bit of recursive to get the base elements type,
please check new function: CoercionErrorSafe_Internal in v21-0022.

Given the interaction with array and domain coercion, we need to think more
about error-safe type casts for user-defined range, multirange, and composite
types. For now, we should disallow error-safe casting for these types.

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

Attachment Content-Type Size
v21-0003-error-safe-for-casting-character-to-other-types-per-pg_cast.patch text/x-patch 4.7 KB
v21-0022-CAST-expr-AS-newtype-DEFAULT-expr-ON-CONVERSION-ERROR.patch text/x-patch 119.8 KB
v21-0021-error-safe-for-casting-geometry-data-type.patch text/x-patch 12.3 KB
v21-0023-error-safe-for-user-defined-CREATE-CAST.patch text/x-patch 74.5 KB
v21-0015-error-safe-for-casting-timestamptz-to-other-types-per-pg_cast.patch text/x-patch 3.6 KB
v21-0020-refactor-point_dt.patch text/x-patch 8.6 KB
v21-0019-introduce-float8-safe-function.patch text/x-patch 3.8 KB
v21-0017-error-safe-for-casting-jsonb-to-other-types-per-pg_cast.patch text/x-patch 6.3 KB
v21-0016-error-safe-for-casting-timestamp-to-other-types-per-pg_cast.patch text/x-patch 3.1 KB
v21-0018-refactor-float_overflow_error-float_underflow_error-float_zero_d.patch text/x-patch 14.2 KB
v21-0014-error-safe-for-casting-interval-to-other-types-per-pg_cast.patch text/x-patch 2.1 KB
v21-0012-error-safe-for-casting-float8-to-other-types-per-pg_cast.patch text/x-patch 3.6 KB
v21-0013-error-safe-for-casting-date-to-other-types-per-pg_cast.patch text/x-patch 2.2 KB
v21-0011-error-safe-for-casting-float4-to-other-types-per-pg_cast.patch text/x-patch 3.1 KB
v21-0010-error-safe-for-casting-numeric-to-other-types-per-pg_cast.patch text/x-patch 5.2 KB
v21-0009-error-safe-for-casting-bigint-to-other-types-per-pg_cast.patch text/x-patch 3.9 KB
v21-0008-error-safe-for-casting-integer-to-other-types-per-pg_cast.patch text/x-patch 2.9 KB
v21-0005-error-safe-for-casting-character-varying-to-other-types-per-pg_c.patch text/x-patch 2.1 KB
v21-0007-error-safe-for-casting-macaddr8-to-other-types-per-pg_cast.patch text/x-patch 1.5 KB
v21-0006-error-safe-for-casting-inet-to-other-types-per-pg_cast.patch text/x-patch 1.8 KB
v21-0002-error-safe-for-casting-bit-varbit-to-other-types-per-pg_cast.patch text/x-patch 2.6 KB
v21-0004-error-safe-for-casting-text-to-other-types-per-pg_cast.patch text/x-patch 9.4 KB
v21-0001-error-safe-for-casting-bytea-to-other-types-per-pg_cast.patch text/x-patch 2.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Junwang Zhao 2026-03-17 05:54:52 Re: SQL Property Graph Queries (SQL/PGQ)
Previous Message Michael Paquier 2026-03-17 05:51:12 Re: Return pg_control from pg_backup_stop().