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.96) (envelope-from ) id 1vTKIq-00HJGc-1Y for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Dec 2025 13:32:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vTKIp-00BTQe-0a for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Dec 2025 13:32:23 +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.96) (envelope-from ) id 1vTKIo-00BTQW-2n for pgsql-hackers@lists.postgresql.org; Wed, 10 Dec 2025 13:32:23 +0000 Received: from mail-qk1-x732.google.com ([2607:f8b0:4864:20::732]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vTKIm-004Ghy-39 for pgsql-hackers@lists.postgresql.org; Wed, 10 Dec 2025 13:32:22 +0000 Received: by mail-qk1-x732.google.com with SMTP id af79cd13be357-8b2d56eaaceso662332285a.0 for ; Wed, 10 Dec 2025 05:32:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765373539; x=1765978339; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=8sLePneXR6rW2h4kez/P7G1lrPAFXgq7WksRNMyZ3xo=; b=U7FTeL4zFKUHSNB03y2beyHTlWxWAOVn2k4sP2l2ynuYmHIgUjOdTjgYddlmLDk0Pu 0eSTmK1OQafODhvJj5eB8jr3mIr/1IX9QDYREmiJTDdBtaFNtFp1aixKkb7nX1rOwM5s HlVKvGbOSQYo9bljEmcMWaJX+xOjHpp96sYVoY+STH+VoGmFZ3xtkl/Ul0L9slQiGBeG v1umi1Pd6Xwr/O6bOR4VCvhEwp0+T2XRrKpVPXkhffTwpFI+nsckOlI+giY2NU2fogEm KF0v+B4qOxdCM3atcTV7Flo9Q6tMbFNVkfKn3IT+AgDf1tWffNJPz8hz0tq2WT+KokmB 8S5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765373539; x=1765978339; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=8sLePneXR6rW2h4kez/P7G1lrPAFXgq7WksRNMyZ3xo=; b=aWga7lwrFcPDnXwbTHLiXJ4YK8zbx5G5DFShDu7cBEBcrpki51dDLX0EDc0DvGt93/ 3+/+e0mdNqaUFcdRr27hXciQ75xzmrSufWWNz4TAqYbV/11GQ2px27HO9/JndXgnm8XB K0SXLx2Xx6w/rxVmM5LakpS3wD0liGukGHo0SY4nxG2LTSh2E/ON8a3JaS6belAUbOpV 0k1rjlX/Ccjutq8EK48pUzRSYZbeSTmLDT93WIbedB0I4bhV+/WyvI+5oX6E2gz8T94c SdAJpa6MGoG2qdYGWMiTxCky6IPwGOksx5ximvcMvt2zBb2xFDCAZh+ut8dWw/JsXIbg hY4w== X-Forwarded-Encrypted: i=1; AJvYcCVbaqegeP36q1bCMSW2M3K3p+OeqH+G/+cCSpaKRb/WjWwvieOOAdfrVM9brrJ158ypQi7UwbccFhfWgZKp@lists.postgresql.org X-Gm-Message-State: AOJu0YzcEo5S1xLevzG4qJQrfDZXLxIMqkpCJgBC2vK+gU1v0rcqcK3l B/mUVNmV18RPPWCNslGXZeKeujmkWGktsIvgJOLHrUsy54z+Q+kunaWImR0omEBsU6fWv6rExrE OzdXvAGQUMFxZLPaJEhJ4mkYtM3++/8E= X-Gm-Gg: ASbGncu9blL2vym8aYiL0+83/SAvvSct/Ec9X3LCxkVHKtsu5ppyGNFslZC0d9jnns9 i0CQAsrkTs+bINqHz9tpxpOkmnhgKFXLB2hOVqMFFqbqSqSRT+0+ndouA27UY2FBln+BSTj0sDf uGL5Rq8aEfVrkTVQTw3dEpcUpYviWsgOh4NFVKQ90H+ypa3h0mAiiNSBIffRRCTEYJ/j/1b+q8Z YkBerxjPtaQiw/qKHKMmpTJ0+hNtzK63kGofc1X4e/wBN2hBTTuw6Zr5XUxZcEL2J/LjW1t X-Google-Smtp-Source: AGHT+IFVFqKYDqpOsp9iHWNWDiMDIxJ0jgYV/SS5ejSNlwyu+OYt2BD+CAciqkBV1ncJe5z3iUPCUjwo+Sq+ByHvRoQ= X-Received: by 2002:ac8:5703:0:b0:4ee:739:149 with SMTP id d75a77b69052e-4f1b1b0adbfmr31933131cf.46.1765373538904; Wed, 10 Dec 2025 05:32:18 -0800 (PST) MIME-Version: 1.0 References: <04afcd1f-ed7d-4c0a-add1-50e3719ccbf9@postgresfriends.org> <762ae707-7fdc-43d8-a77a-3a10d12ce21d@postgresfriends.org> In-Reply-To: From: Kirill Reshke Date: Wed, 10 Dec 2025 18:32:06 +0500 X-Gm-Features: AQt7F2pnGxr9dqj5GKQ8KDAdBOheX5gagdWDbcG6o1hhNOWAsrC7DpBn0ZipJcQ Message-ID: Subject: Re: CAST(... ON DEFAULT) - WIP build on top of Error-Safe User Functions To: jian he Cc: Corey Huinker , Vik Fearing , Isaac Morland , pgsql-hackers@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 10 Dec 2025 at 13:58, jian he wrote: > > On Tue, Dec 9, 2025 at 11:39=E2=80=AFAM jian he wrote: > > > > On Mon, Dec 1, 2025 at 1:41=E2=80=AFPM Corey Huinker wrote: > > >> > > > No, I meant implementing the syntax for being able to declare a custo= m 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 obvi= ous 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 da= ta type > error-safe, separate from the main patch (v15-0020). I hope to make it ea= sier 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 erro= r 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/ Hi! Overall, I think this patch is doing a good thing. Also, are we holding it until the next SQL standard release, because sql/23 leaks this feature? Below are my 2c. 1) First of all, I would prefer the `Bumps catversion` comment in the commit msg of v15-0022. 2) In v15-0006, if dont understand when memory allocated by `result =3D (macaddr *) palloc0(sizeof(macaddr));` will be freed. Does it persist until the query ends? I tried to get OOM with a query that errors out macaddr8 casts repeatedly, but failed. 3) > * When error_safe set to true, we will evaluate the constant expression = in a > * error safe way. If the evaluation fails, return NULL instead of throwi= ng > * error. Somebody has to say it - s/error_safe set/error_safe is set/, also s/throwing error/throwing an error/ -- Best regards, Kirill Reshke