Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gPUak-0002cO-Ui for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Nov 2018 15:39:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gPUai-0004dR-GL for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Nov 2018 15:39:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gPUai-0004dK-5f for pgsql-hackers@lists.postgresql.org; Wed, 21 Nov 2018 15:39:00 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gPUaf-0005Em-2e for pgsql-hackers@postgresql.org; Wed, 21 Nov 2018 15:38:59 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 9552021CA9F0; Wed, 21 Nov 2018 18:38:55 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.182] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 4B3B721CA9EF; Wed, 21 Nov 2018 18:38:55 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1542814735; bh=sJuJBULEA35DSeAKhU7/uSJnoVC94tnX2JaYHJFOjy8=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=Q+wI4LAXw0/xTopQFsEU4LO7zHx+HLvOZKyKhscvYET71wvtwfHVckhLG510D/a4S JqzxBRrjyINYAaUY4sen+TTb0SiOfA8t/FqvCwA+klGcVMyjgyt17B+RnDEJluZKyk OofbIrIsqJ6YbuuTWK/vgo5WlLMeN6Q0G1BtOuO4= Subject: Re: Index Skip Scan To: Dmitry Dolgov <9erthalion6@gmail.com>, Jesper Pedersen Cc: pg@bowt.ie, Thomas Munro , Bhushan Uparkar , PostgreSQL-development , Alexander Korotkov , jtc331@gmail.com References: <707b6f68-16fa-7aa7-96e5-eeb4865e6a30@redhat.com> <89F84E17-D684-4717-B66F-0F05CB00168E@gmail.com> <522092c7-3cb7-a1ec-410b-dfa97ae722f3@postgrespro.ru> <11b7c9d1-ae00-4385-97b2-fcd45f0a1a71@redhat.com> <3cfd317e-2a8b-8681-0833-8f2ead3af4d4@postgrespro.ru> From: Alexander Kuzmenkov Message-ID: Date: Wed, 21 Nov 2018 18:38:55 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/18/18 02:27, Dmitry Dolgov wrote: > > [0001-Index-skip-scan-v4.patch] I ran a couple of tests on this, please see the cases below. As before, I'm setting total_cost = 1 for index skip scan so that it is chosen. Case 1 breaks because we determine the high key incorrectly, it is the second tuple on page or something like that, not the last tuple. Case 2 is backwards scan, I don't understand how it is supposed to work. We call _bt_search(nextKey = ScanDirectionIsForward), so it seems that it just fetches the previous tuple like the regular scan does. case 1: # create table t as select generate_series(1, 1000000) a; # create index ta on t(a); # explain select count(*) from (select distinct a from t) d;                                QUERY PLAN -------------------------------------------------------------------------  Aggregate  (cost=3.50..3.51 rows=1 width=8)    ->  Index Only Scan using ta on t  (cost=0.42..1.00 rows=200 width=4)          Scan mode: Skip scan (3 rows) postgres=# select count(*) from (select distinct a from t) d;  count --------  500000 -- should be 1kk (1 row) case 2: # create table t as select generate_series(1, 1000000) / 2 a; # create index ta on t(a); # explain select count(*) from (select distinct a from t order by a desc) d;                                      QUERY PLAN -------------------------------------------------------------------------------------  Aggregate  (cost=5980.81..5980.82 rows=1 width=8)    ->  Index Only Scan Backward using ta on t  (cost=0.42..1.00 rows=478385 width=4)          Scan mode: Skip scan (3 rows) # select count(*) from (select distinct a from t order by a desc) d;  count --------  502733 -- should be 500k (1 row) -- Alexander Kuzmenkov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company