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 1sgSgy-002S6S-4A for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Aug 2024 17:30:48 +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 1sgSgv-0025ln-FI for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Aug 2024 17:30:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sgSgv-0025lf-1P for pgsql-bugs@lists.postgresql.org; Tue, 20 Aug 2024 17:30:45 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sgSgr-000bPR-Lt for pgsql-bugs@postgresql.org; Tue, 20 Aug 2024 17:30:44 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 47KHUdZm1611528; Tue, 20 Aug 2024 13:30:39 -0400 From: Tom Lane To: Peter Eisentraut cc: pgsql-bugs Subject: Re: CREATE CAST allows creation of binary-coercible cast to range over domain In-reply-to: <1537781.1724165175@sss.pgh.pa.us> References: <076968e1-0852-40a9-bc0b-117cd3f0e43c@eisentraut.org> <1537781.1724165175@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 20 Aug 2024 10:46:15 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <1611494.1724175016.0@sss.pgh.pa.us> Date: Tue, 20 Aug 2024 13:30:39 -0400 Message-ID: <1611527.1724175039@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1611494.1724175016.1@sss.pgh.pa.us> I wrote: > I think it's an oversight that we allow binary-coercible casts > involving range types at all. There are no other nesting cases to > worry about, because CreateCast already rejects binary-coercible casts > applied to composites and arrays, explaining > * We know that composite, enum and array types are never binary- > * compatible with each other. They all have OIDs embedded in them. > But range types also embed their own OID, so I don't see why that > concern doesn't apply to them. In short, more or less as attached. (I didn't bother with a regression test, since none of the adjacent error checks are covered either.) I'm unsure whether to back-patch this. If somebody were using such a cast today, it might seem to mostly work, as long as the two kinds of ranges had the same behavior. So they might not appreciate us breaking it in a minor release. Maybe we should put this into 17 but not further back. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="reject-binary-coercions-on-ranges.patch"; charset="us-ascii" Content-ID: <1611494.1724175016.2@sss.pgh.pa.us> Content-Description: reject-binary-coercions-on-ranges.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/fu= nctioncmds.c index 6593fd7d81..149642ab1b 100644 --- a/src/backend/commands/functioncmds.c +++ b/src/backend/commands/functioncmds.c @@ -1689,13 +1689,18 @@ CreateCast(CreateCastStmt *stmt) errmsg("source and target data types are not physically compatible"= ))); = /* - * We know that composite, enum and array types are never binary- - * compatible with each other. They all have OIDs embedded in them. + * We know that composite, array, range and enum types are never + * binary-compatible with each other. They all have OIDs embedded in + * them. * * Theoretically you could build a user-defined base type that is - * binary-compatible with a composite, enum, or array type. But we - * disallow that too, as in practice such a cast is surely a mistake. - * You can always work around that by writing a cast function. + * binary-compatible with such a type. But we disallow that too, as + * in practice such a cast is surely a mistake. You can always work + * around that by writing a cast function. + * + * NOTE: if we didn't exclude every kind of container type for this + * reason, we'd likely need to recursively apply all of these same + * checks to the contained type(s). */ if (sourcetyptype =3D=3D TYPTYPE_COMPOSITE || targettyptype =3D=3D TYPTYPE_COMPOSITE) @@ -1703,6 +1708,14 @@ CreateCast(CreateCastStmt *stmt) (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), errmsg("composite data types are not binary-compatible"))); = + if (sourcetyptype =3D=3D TYPTYPE_RANGE || + targettyptype =3D=3D TYPTYPE_RANGE || + sourcetyptype =3D=3D TYPTYPE_MULTIRANGE || + targettyptype =3D=3D TYPTYPE_MULTIRANGE) + ereport(ERROR, + (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), + errmsg("range data types are not binary-compatible"))); + if (sourcetyptype =3D=3D TYPTYPE_ENUM || targettyptype =3D=3D TYPTYPE_ENUM) ereport(ERROR, ------- =_aaaaaaaaaa0--