Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uinS8-00FwfQ-Au for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Aug 2025 05:09:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uinS7-00FxHT-7z for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Aug 2025 05:09:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uinS6-00FxHL-TI for pgsql-hackers@lists.postgresql.org; Mon, 04 Aug 2025 05:09:38 +0000 Received: from mail-io1-xd30.google.com ([2607:f8b0:4864:20::d30]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uinS4-000dcf-2L for pgsql-hackers@lists.postgresql.org; Mon, 04 Aug 2025 05:09:38 +0000 Received: by mail-io1-xd30.google.com with SMTP id ca18e2360f4ac-8817b8d2d0eso10926239f.0 for ; Sun, 03 Aug 2025 22:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754284175; x=1754888975; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=U4JO51e0JWBbxMDFoLWQynuNowDeS3oAIsyWMG0CUFo=; b=jVg5bQHg9Lj8yjwRSCC6+h+Ygzuq+wmKwnnFYIdfVm/NEV7eZd09I//9rznx2fxGd8 cmDq/UsUMGoK7ZF8Czi1dKZzaCtDfkEQUwHsj25JsnmhyeiKJDtB3YWj71s7kcIzITeE s7XMepWzqtTIG/ZE6N4DiGlVH35a/BJZ2qyRl97fJ9VkSBupT3Kzl3+4tblnxD5IGRKY +NrfrH+gXm8+1lcQScJFtBhzhrrGlOgI/N5s0UlunZdsQ5ZKBRnmEpi2XymYdSRdbqgK 3hgATycYosJgTW/x14DNIDOjliH9XChd9L0Wre3A/C1q10pkewczRWLNQmWRl3zBFs+U 0fOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754284175; x=1754888975; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=U4JO51e0JWBbxMDFoLWQynuNowDeS3oAIsyWMG0CUFo=; b=KDRUSj+FU/gvGUIWIpUHM2Rtlsmu+m+rSOwBw19Kje0jKkriIfGqqefdMtiAJ1nN1b NoCB68jb0Oz6wbCCg/66eBnfxGkXMj3skC1wDVLco1yle2AR4PytWtsARBeqEGRN+0+8 hpXd8B/s+3x6K2mQ0L6cHeOX7A1qMDy7UO83oXFJUzfiykH4hbV/MXbcyCaM6zYaNOI4 8aJ5/JTIE0U/ynhhVtQJpa7kGpTR61VzHMJSYXkoNn0if1Jwum7uobtXXM9dSHx9FlJ7 VfWuMcgOxTfSMhQdEo/DXQig/yM28nRd5+3UXK78asy1KroUPlHmoGpqaf8PkVp8DXtQ Z9hw== X-Forwarded-Encrypted: i=1; AJvYcCUA3QDf3JsrZCQVEszuvmEEtwGMknWpDo7hinakKYGxPG0xamAhJJUYrU2Qtdtlb3c/Doc5a3WKnpHv0zdq@lists.postgresql.org X-Gm-Message-State: AOJu0YxUYYtiMbVjBZXuPcETzoWcH6ASX+8MNGosIXKmQ14Pjdh6z9mQ /b87E5h7yHEliRvohKmR5mH1RAQyDU49ffVRw+9Do3uuy3CqBoZ/RPaUN2kfTOKjoCh+C0yJ7Lk 7MUAK6Ga70uLYUIUw/XVh1oXZe382Y+R15ItR X-Gm-Gg: ASbGnctzFZwEKQ28XnZNGj8cxKTfsWhsf1ZrP3U1Jk6uFH9mgvC2vqBamrv8s9RaSIH w3tTY3NF1awOXICeXSoSN/UXciqMkzfD1/s48OOGkslNPa/B0mx4snaGTnu9yRYpLlbWcbTVoTF iyiJSWQ5AMPjtBuEnboHg4/Ae22KuggPbrjcm2HaJjpU/JVO5e3UHPPonAomm405lUX9Ceo/zIQ 0e1rHaV/ZJuOXWUdA== X-Google-Smtp-Source: AGHT+IFiu8ZZTJM+dARYv9eI6CzdijU1U1tob/pTJbC37RFOP5N4CwiXVHU6vZpLdzQG0iM2ysGk7LLbUKghjwhIMpM= X-Received: by 2002:a05:6e02:3398:b0:3e2:a438:f9b with SMTP id e9e14a558f8ab-3e41638e4f2mr140399855ab.22.1754284174541; Sun, 03 Aug 2025 22:09:34 -0700 (PDT) MIME-Version: 1.0 References: <04afcd1f-ed7d-4c0a-add1-50e3719ccbf9@postgresfriends.org> <762ae707-7fdc-43d8-a77a-3a10d12ce21d@postgresfriends.org> In-Reply-To: From: Corey Huinker Date: Mon, 4 Aug 2025 01:09:21 -0400 X-Gm-Features: Ac12FXxs0aJuraJZb9DO8Eg6OlB6obVIWzMfhIZAamXNnwn5R3DZoD0XuV9ynp0 Message-ID: Subject: Re: CAST(... ON DEFAULT) - WIP build on top of Error-Safe User Functions To: jian he Cc: Vik Fearing , Isaac Morland , pgsql-hackers@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000000f0670063b831dd1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000000f0670063b831dd1 Content-Type: text/plain; charset="UTF-8" > > so we need to handle numeric source types with fractional points with > special care. > currently, this applies only to numeric, float4, and float8. > (hope this is all the corner case we need to catch...) > I'm fairly certain that the committers won't like us special-casing the internal cast functions, as we would have to maintain these special cases as new core types are added, and it still bypasses the defined cast function for user-defined types, which could have similar issues similar to the rounding issue. I think the way forward here is either to: 1. add a second function definition to CAST. The potential syntax forr a second function gets clumsy, but might look something like this: CREATE CAST (source_type AS target_type) WITH FUNCTION function_name [ (argument_type [, ...]) ] [ AS ASSIGNMENT | AS IMPLICIT ] [ WITH SAFE FUNCTION function_name [ (argument_type [, ...]) ] [ AS ASSIGNMENT | AS IMPLICIT ] ] That doesn't seem right to me, it seems easier to: 2. Modify the CAST definition to indicate whether the existing cast function has the regular function signature or a -Safe one. In cases where a CAST has a defined function but the safe flag is turned off, we would have to fail the query with an error like "Defined CAST function from srctype to desttype is not error-safe". This would involve changing the syntax of CREATE CAST by adding an option SAFE, or ERROR SAFE, or similar: CREATE CAST (source_type AS target_type) WITH [SAFE] FUNCTION function_name [ (argument_type [, ...]) ] [ AS ASSIGNMENT | AS IMPLICIT ] We would add a new value to pg_cast.castmethod, 's' for "safe". We could refactor all the numeric types to use the modified functions, so no special-case code there anymore, and it gives extension writers an incentive to (eventually) make their own cast functions error-safe. While method 2 seems a lot cleaner, there may be a performance regression in the now error-safe typecast functions. If so, that might tip the balance to having two functions defined. --0000000000000f0670063b831dd1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
so we need to handle numeric source types with fractional points with
special care.
currently, this applies only to numeric, float4, and float8.
(hope this is all the corner case we need to catch...)

I'm fairly certain that the committers won't like u= s special-casing the internal cast functions, as we would have to maintain = these special cases as new core types are added, and it still bypasses the = defined cast function for user-defined types, which could have similar issu= es similar to the rounding issue.

I think the way forward= here is either to:

1.=C2=A0 add a second func= tion definition to CAST. The potential syntax forr a second function gets c= lumsy, but might look something like this:

CREATE CAST (source_type AS target_type)
=C2=A0 =C2=A0 WITH FUNCT= ION function_name [ (argument_type [, ...]) ]
=C2=A0 =C2=A0 [ AS ASSIGNM= ENT | AS IMPLICIT ]
=C2=A0 =C2=A0[
=C2=A0 =C2=A0 WITH SAFE FUNCTION function_name [ (argument_type [, ...= ]) ]
=C2=A0 =C2=A0 [ AS ASSIGNMENT | AS IMPLICIT ]
=C2=A0 =C2=A0]

That does= n't seem right to me, it seems easier to:
<= div>
2. Modify the CAST definition to indicate whether the ex= isting cast function has the regular function signature or a -Safe one. In = cases where a CAST has a defined function=C2=A0but the safe flag is turned = off, we would have to fail the query with an error like "Defined CAST = function from srctype to desttype is not error-safe".

This would involve changing the syntax of CREATE CAST by adding an= option SAFE, or ERROR SAFE, or similar:

CREATE CAST (source_type AS target_type)
=C2=A0 =C2=A0 WITH = [SAFE] FUNCTION function_name [ (argument_type [, ...]) ]
=C2=A0 =C2=A0 = [ AS ASSIGNMENT | AS IMPLICIT ]

We would= add a new value to pg_cast.castmethod, 's' for "safe".
We could refactor all the numeric types to use the modifie= d functions, so no special-case code there anymore, and it gives extension = writers an incentive to (eventually) make their own cast functions error-sa= fe.

While method 2 seems a lot cleaner, there may = be a performance regression in the now error-safe typecast functions. If so= , that might tip the balance to having two functions defined.
--0000000000000f0670063b831dd1--