Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pINq9-0001Xe-B2 for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Jan 2023 05:51:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pINq7-0003Of-LJ for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Jan 2023 05:51:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pINq7-0003MG-Cz for pgsql-bugs@lists.postgresql.org; Thu, 19 Jan 2023 05:51:55 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pINq4-0007d6-R7 for pgsql-bugs@lists.postgresql.org; Thu, 19 Jan 2023 05:51:54 +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 30J5pm8V3998528; Thu, 19 Jan 2023 00:51:48 -0500 From: Tom Lane To: Noah Misch cc: "Sam.Mesh" , Peter Geoghegan , pgsql-bugs@lists.postgresql.org Subject: Re: index not used for bigint without explicit cast In-reply-to: <20230119053047.GA1295127@rfd.leadboat.com> References: <20230119053047.GA1295127@rfd.leadboat.com> Comments: In-reply-to Noah Misch message dated "Wed, 18 Jan 2023 21:30:47 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3998526.1674107508.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 19 Jan 2023 00:51:48 -0500 Message-ID: <3998527.1674107508@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Noah Misch writes: > On Wed, Jan 18, 2023 at 01:43:43PM -0800, Sam.Mesh wrote: >> Could you please double check the following reasoning based on >> https://www.postgresql.org/docs/current/btree-behavior.html? >> - Index search by bigint column requires conversion of limiting >> expressions to bigint type. >> - Conversion from number(19,0) to bigint may cause overflow. >> - So, index search is not possible. > Essentially, yes. This is a query planner limitation, not a fundamental > property of the search problem. "bigintcol =3D '5.1'::numeric" is equiv= alent to > "bigintcol <> bigintcol" or "CASE WHEN bigintcol IS NULL THEN NULL ELSE = false > END". In contexts that don't distinguish "false" from NULL, it's equiva= lent > to constant "false". Those observations apply to any search value such = that > 'search_value'::numeric::bigint::numeric <> 'search_value'::numeric does= not > return true, including overflow-error cases. Like many datatype-specifi= c > tricks, the planner isn't aware at this time. Hmm ... I don't think the planner would be the place to try to change this. The fundamental thing to do if you wanted to improve this case would be to invent the "bigint =3D numeric" operator (and its whole family, such as "numeric =3D bigint", "bigint < numeric", "smallint < numeric", etc etc) and make those part of the integer_ops opfamily. Which'd probably lead to merging integer_ops and numeric_ops into a single opfamily. As far as I can think at the moment, there are not any insurmountable semantic obstacles to doing that. There are good reasons not to try to merge integer and float opfamilies, namely that transitivity of equality would fail because of inexact conversions; but I can't see how that would occur between integer-family types and numeric, with all of those being exact types. Nonetheless, I'm not eager to try to do it. The practical hazards are at least two: integer vs. numeric comparison operators will be unpleasantly slow, and adding dozens more identically-named operators will increase the risks of getting "ambiguous operator" errors in the parser. In the end I'd ask why is this a problem. If you converted your tables from Oracle-ish number(19,0) to bigint, and did not convert your application to use bigint instead of number(19,0), that sounds like self-inflicted damage. Do both or neither. regards, tom lane