Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id ED982B5DBD3 for ; Tue, 24 May 2011 12:48:07 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 58657-03 for ; Tue, 24 May 2011 15:48:00 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from gw.wicourts.gov (gwmta.wicourts.gov [165.219.244.99]) by mail.postgresql.org (Postfix) with ESMTP id 6A243B5DC0A for ; Tue, 24 May 2011 12:47:59 -0300 (ADT) Received: from Courts-MTA by gw.wicourts.gov with Novell_GroupWise; Tue, 24 May 2011 10:47:58 -0500 Message-Id: <4DDB8CD3020000250003DBF2@gw.wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 8.0.1 Date: Tue, 24 May 2011 10:47:47 -0500 From: "Kevin Grittner" To: , "jose soares" Subject: Re: BUG #6036: why a REAL should be queried as a string? References: <201105240714.p4O7EXQW059510@wwwmaster.postgresql.org> In-Reply-To: <201105240714.p4O7EXQW059510@wwwmaster.postgresql.org> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/174 X-Sequence-Number: 30313 "jose soares" wrote: > Description: why a REAL should be queried as a string? > I'd like to to report this strange behavior, that I think is a > bug... > > # \d frazione > Table "public.frazione" > Column | Type | Modifiers > ------------+---------+-------------------- > id | integer | not null > importo | real | not null > > # update frazione set importo=0.833 where id=549; > UPDATE 1 > # select * from frazione where importo=0.833; > id | importo > ----+--------- > (0 rows) > > # select * from frazione where importo='0.833'; > id | importo > -----+---------- > 549 | 0.833 > (1 rows) You're running into multiple issues here. First off, you should understand that "real" is an approximate data type, which is not capable of exactly representing many decimal fractions. "numeric" can exactly represent decimal fractions. So, while it's OK when you compare real to numeric on values for which real happens to be exact, it falls apart when its approximation doesn't exactly match the numeric value: test=# select '0.125'::numeric = '0.125'::real; ?column? ---------- t (1 row) test=# select '0.833'::numeric = '0.833'::real; ?column? ---------- f (1 row) Unadorned, that literal is taken as numeric: test=# select pg_typeof(0.833); pg_typeof ----------- numeric (1 row) The other issue is that in PostgreSQL a quoted literal is not initially a character string literal -- it starts out as type "unknown" to make it easier to specify literals for various types. test=# select pg_typeof('0.833'); pg_typeof ----------- unknown (1 row) In your second example (where the number matches), the unknown value is cast to real when you compare to the real column, so the same approximation is generated. In general it is a bad idea to compare for equality using approximate values. Perhaps you would be better off with that column defined as numeric? -Kevin