Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id A505AB5DC36 for ; Tue, 2 Aug 2011 04:49:17 -0300 (ADT) 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 92756-02 for ; Tue, 2 Aug 2011 07:49:11 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og112.obsmtp.com (exprod7og112.obsmtp.com [64.18.2.177]) by mail.postgresql.org (Postfix) with ESMTP id 9BA8EB5DC35 for ; Tue, 2 Aug 2011 04:49:10 -0300 (ADT) Received: from mail-fx0-f50.google.com ([209.85.161.50]) (using TLSv1) by exprod7ob112.postini.com ([64.18.6.12]) with SMTP ID DSNKTjer9YvKWyAwvzoD0F6jZ4BsuUlxfWEj@postini.com; Tue, 02 Aug 2011 00:49:11 PDT Received: by mail-fx0-f50.google.com with SMTP id 2so6835457fxh.23 for ; Tue, 02 Aug 2011 00:49:09 -0700 (PDT) Received: by 10.223.64.80 with SMTP id d16mr7714453fai.86.1312271348497; Tue, 02 Aug 2011 00:49:08 -0700 (PDT) Received: from [192.168.1.183] (dsl-hkibrasgw2-ff7ac300-240.dhcp.inet.fi [88.195.122.240]) by mx.google.com with ESMTPS id f27sm1173321fak.31.2011.08.02.00.49.06 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 02 Aug 2011 00:49:07 -0700 (PDT) Message-ID: <4E37ABF1.5080609@enterprisedb.com> Date: Tue, 02 Aug 2011 10:49:05 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.2.18) Gecko/20110626 Icedove/3.1.11 MIME-Version: 1.0 To: Hitoshi Harada CC: pgsql-bugs@postgresql.org Subject: Re: BUG #6139: LIMIT doesn't return correct result when the value is huge References: <201108020625.p726PZr2034481@wwwmaster.postgresql.org> In-Reply-To: <201108020625.p726PZr2034481@wwwmaster.postgresql.org> Content-Type: multipart/mixed; boundary="------------010304070106060407000208" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201108/13 X-Sequence-Number: 30825 This is a multi-part message in MIME format. --------------010304070106060407000208 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 02.08.2011 09:25, Hitoshi Harada wrote: > db1=# select count(*) from test_xy; > count > ------- > 31 > (1 row) > > db1=# select * from test_xy order by x LIMIT 9223372036854775807 OFFSET 6; > gid | x | y > -----+--------------------+-------------------- > 13 | -0.591943957968476 | -0.481611208406305 > (1 row) The bug seems to occur when LIMIT + OFFSET >= 2^63. In ExecLimit function, we check if current position >= offset + limit, and that overflows. I'll commit the attached patch to fix that. Thanks for the report -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com --------------010304070106060407000208 Content-Type: text/x-diff; name="fix-large-limit+offset.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-large-limit+offset.patch" diff --git a/src/backend/executor/nodeLimit.c b/src/backend/executor/nodeLimit.c index 85d1a6e..f2d356d 100644 --- a/src/backend/executor/nodeLimit.c +++ b/src/backend/executor/nodeLimit.c @@ -127,7 +127,7 @@ ExecLimit(LimitState *node) * the state machine state to record having done so. */ if (!node->noCount && - node->position >= node->offset + node->count) + node->position - node->offset >= node->count) { node->lstate = LIMIT_WINDOWEND; return NULL; --------------010304070106060407000208--