Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4A96E1337B83 for ; Sun, 27 Feb 2011 14:40:09 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 04849-06 for ; Sun, 27 Feb 2011 18:40:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id E1E821337B4E for ; Sun, 27 Feb 2011 14:40:01 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p1RIdf6i018768; Sun, 27 Feb 2011 13:39:41 -0500 (EST) To: Robert Haas cc: Josh Berkus , Laszlo Nagy , pgsql-performance@postgresql.org, Daniel Fekete Subject: Re: Bad query plan when the wrong data type is used In-reply-to: References: <4D514FFF.9060308@shopzeus.com> <4D51BE0B.1060404@agliodbs.com> Comments: In-reply-to Robert Haas message dated "Sun, 27 Feb 2011 13:16:55 -0500" Date: Sun, 27 Feb 2011 13:39:41 -0500 Message-ID: <18767.1298831981@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/509 X-Sequence-Number: 42647 Robert Haas writes: > On Tue, Feb 8, 2011 at 5:04 PM, Josh Berkus wrote: >> I'm not saying that PostgreSQL couldn't do better on this kind of case, >> but that doing better is a major project, not a minor one. > Specifically, the problem is that x = 4.0, where x is an integer, is > defined to mean x::numeric = 4.0, not x = 4.0::integer. If it meant > the latter, then testing x = 3.5 would throw an error, whereas what > actually happens is it just returns false. > We could fix this by adding some special case logic that understands > properties of integers and numeric values and optimizes x = > 4.0::numeric to x = 4::int and x = 3.5::numeric to constant false. > That would be cool, in a way, but I'm not sure it's really worth the > code it would take, unless it falls naturally out of some larger > project in that area. I think that most of the practical problems around this case could be solved without such a hack. What we should do instead is invent cross-type operators "int = numeric" etc and make them members of both the integer and numeric index opclasses. There are reasons why that wouldn't work for integer versus float (read the last section of src/backend/access/nbtree/README) but right offhand it seems like it ought to be safe enough for numeric. Now, it wouldn't be quite as fast as if we somehow downconverted numeric to integer beforehand, but at least you'd only be talking about a slow comparison operator and not a fundamentally stupider plan. That's close enough for me, for what is in the end a stupidly written query. Of course, the above is still not exactly a small project, since you'd be talking about something like 36 new operators to cover all of int2, int4, int8. But it's a straightforward extension. regards, tom lane