Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 79F051337B83 for ; Wed, 9 Feb 2011 05:52:55 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 49114-08 for ; Wed, 9 Feb 2011 09:52:49 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f46.google.com (mail-bw0-f46.google.com [209.85.214.46]) by mail.postgresql.org (Postfix) with ESMTP id 7D7A71337B84 for ; Wed, 9 Feb 2011 05:52:47 -0400 (AST) Received: by bwz15 with SMTP id 15so755107bwz.19 for ; Wed, 09 Feb 2011 01:52:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type; bh=L5OggTlyH+BdpRkkKF2GxalVJuF4xDasclq8GB5qrqE=; b=ivyAD99rhu0EiQN5fUSCsOwJeg2E3gXPHIOCjycAj2A22VAFDiVIXp6PtozLJPgE2y zqqmm7fkS44UczKS/z4sNpmYwNIsjrQ/jxIp0+m9I8OZPjs3oPI7A1MnWiUrYWt+Guwx Us2CaH4yK5NieXguih5uu+wUoF/gjQjeAy22U= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type; b=oUWuVE0os7be2X02GUgPXOrWcYKovnQhje92CwTdHk+N1QwXAC9CvF4vT4Z+JWWISU K7Pa2dkHddaXl5t+47nQCz8dK351jkcCYtJVf0RvfJerR7xpGAwEah1Q8BCpNKiIEJ7e Q2q0S4Q3RzShls2YaBHP/Dz4KoY6m8mEUBugQ= Received: by 10.204.72.20 with SMTP id k20mr18472602bkj.184.1297245165971; Wed, 09 Feb 2011 01:52:45 -0800 (PST) Received: from [10.1.1.57] (skuns.zoral.com.ua [91.193.166.194]) by mx.google.com with ESMTPS id j11sm84125bka.0.2011.02.09.01.52.42 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 09 Feb 2011 01:52:43 -0800 (PST) Message-ID: <4D5263EC.9000205@gmail.com> Date: Wed, 09 Feb 2011 11:52:44 +0200 From: Vitalii Tymchyshyn User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7 MIME-Version: 1.0 To: Dave Crooke CC: Josh Berkus , Laszlo Nagy , pgsql-performance@postgresql.org, Daniel Fekete Subject: Re: Bad query plan when the wrong data type is used References: <4D514FFF.9060308@shopzeus.com> <4D51BE0B.1060404@agliodbs.com> In-Reply-To: Content-Type: multipart/alternative; boundary="------------060906000504060400040808" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.045 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_ENVFROM_END_DIGIT=1.553, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201102/324 X-Sequence-Number: 42462 This is a multi-part message in MIME format. --------------060906000504060400040808 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 09.02.11 01:14, Dave Crooke написав(ла): > You will get the same behaviour from any database product where the > query as written requires type coercion - the coercion has to go in > the direction of the "wider" type. I have seen the exact same scenario > with Oracle, and I view it as a problem with the way the query is > written, not with the database server. > > Whoever coded the application which is making this query presumably > knows that the visa.id field is an integer type in > the schema they designed, so why are they passing a float? Convert the > 4.0 to 4 on the application side instead, it's one function call or cast. Actually the problem may be in layers, and the problem may even be not noticed until it's late enough. As far as I remember from this list there are problems with column being integer and parameter prepared as bigint or number. Same for number vs double vs float. As for me it would be great for optimizer to consider the next: 1) val1::narrow = val2::wide as (val1::narrow = val2::narrow and val2::narrow = val2::wide) 2) val1::narrow < val2::wide as (val1::narrow < val2::narrow and val1::wide < val2::wide) 3) val1::narrow > val2::wide as (val1::narrow + 1 > val2::narrow and val1::wide > val2::wide) Of course it should use additional check it this allows to use an index. Surely, this is not an easy thing to implement, but as for me similar question are raised quite often in this list. Best regards, Vitalii Tymchyshyn --------------060906000504060400040808 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit 09.02.11 01:14, Dave Crooke написав(ла):
You will get the same behaviour from any database product where the query as written requires type coercion - the coercion has to go in the direction of the "wider" type. I have seen the exact same scenario with Oracle, and I view it as a problem with the way the query is written, not with the database server.

Whoever coded the application which is making this query presumably knows that the visa.id field is an integer type in the schema they designed, so why are they passing a float? Convert the 4.0 to 4 on the application side instead, it's one function call or cast.
Actually the problem may be in layers, and the  problem may even be not noticed until it's late enough. As far as I remember from this list there are problems with column being integer and parameter prepared as bigint or number. Same for number vs double vs float.
As for me it would be great for optimizer to consider the next:
1) val1::narrow = val2::wide as (val1::narrow = val2::narrow and val2::narrow = val2::wide)
2) val1::narrow < val2::wide as (val1::narrow < val2::narrow and val1::wide < val2::wide)
3) val1::narrow > val2::wide as (val1::narrow + 1 > val2::narrow and val1::wide > val2::wide)
Of course it should use additional check it this allows to use an index.
Surely, this is not an easy thing to implement, but as for me similar question are raised quite often in this list.

Best regards, Vitalii Tymchyshyn

--------------060906000504060400040808--