Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 6A9AE1337C1D for ; Tue, 12 Apr 2011 21:30:58 -0300 (ADT) 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 04923-01-4 for ; Wed, 13 Apr 2011 00:30:59 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by mail.postgresql.org (Postfix) with ESMTP id 0624813364DE for ; Tue, 12 Apr 2011 21:30:12 -0300 (ADT) Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by wwwmaster.postgresql.org (8.14.3/8.14.3) with ESMTP id p3D0UC4w076615 for ; Wed, 13 Apr 2011 00:30:12 GMT (envelope-from www@wwwmaster.postgresql.org) Received: (from www@localhost) by wwwmaster.postgresql.org (8.14.3/8.14.3/Submit) id p3D0UCSZ076614; Wed, 13 Apr 2011 00:30:12 GMT (envelope-from www) Date: Wed, 13 Apr 2011 00:30:12 GMT Message-Id: <201104130030.p3D0UCSZ076614@wwwmaster.postgresql.org> To: pgsql-bugs@postgresql.org Subject: BUG #5975: Incorrect result from mod function with cast From: "John Dickson" Content-Type: text/plain; charset=utf-8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 201104/67 X-Sequence-Number: 29986 The following bug has been logged online: Bug reference: 5975 Logged by: John Dickson Email address: jdickson@tnsi.com PostgreSQL version: 8.3.11 Operating system: RedHat EL 5.6 Description: Incorrect result from mod function with cast Details: The mod function (and the % operator) can return an incorrect result when combined with a cast(). The following SQL (executed on 8.3.11) shows a result one higher than the maximum permissible remainder when combined with a cast: ----- select 1129590 % 66, cast(1129590 as numeric(21, 0)) % 66; ?column? | ?column? ----------+---------- 0 | 66 (1 row) ----- When executed on Postgres 8.2.5 (on Centos 4.6), the results are: ----- ?column? | ?column? ----------+---------- 0 | 0 (1 row) ----- We're using the cast here to demonstrate the bug, but we actually strike the problem when operating on values from a numeric(21, 0) column rather than using cast(). We haven't tested this on later releases of 8.3, but didn't see any likely fixes in the changelog.