Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eKuSg-0006kn-5D for pgsql-performance@arkaria.postgresql.org; Fri, 01 Dec 2017 23:11:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eKuSf-0004Bd-CE for pgsql-performance@arkaria.postgresql.org; Fri, 01 Dec 2017 23:11:13 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eKuSf-0004BT-1L for pgsql-performance@lists.postgresql.org; Fri, 01 Dec 2017 23:11:13 +0000 Received: from mail-it0-x236.google.com ([2607:f8b0:4001:c0b::236]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eKuSb-0001Wz-D2 for pgsql-performance@lists.postgresql.org; Fri, 01 Dec 2017 23:11:12 +0000 Received: by mail-it0-x236.google.com with SMTP id p139so4237859itb.1 for ; Fri, 01 Dec 2017 15:11:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=CtCoZn5BQrm8VEQUHrxwu6uuXqzNHWbu+KCVwIqPs7Q=; b=kLeOOdg6fIKGkWMdqXOcFd7676qXC7P8/f1o0kba9sDftn3w5YOZBTVDPd3JNvr/E3 yoYFN74tS8beWKXPBANBwRkG/Wzn4wW9bq3bQAlETyPtSDJRMaB+HdhZg6GucS6xdpJJ EB5dKUBt8kiVcXlDdwOt885FkCB2PY+uBVe9ViUxspg+bdEZLrJn8d0bwPLjg/B8RLfd Wx0Koau3uRtHD9qMTikZvSR+/LXmbsGLfrTEhI2voSq1CPUYitzQ9qV7X/+bGP7i0qLQ /dnpCuJdNoiXE+BdtKRssc6slZVLhlKyX7kx/eRoJRdOGDnytquO8ZdjNF2h07ve31h0 NKmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=CtCoZn5BQrm8VEQUHrxwu6uuXqzNHWbu+KCVwIqPs7Q=; b=bB4Qcd45xBGclTjx3Tq3gL0nrVjBE4IDXv+4rDcH+ifTCyDyhH9sYQt6Q8EEOp3EvF ukmavvS1KiIktQTxRFnXbNzK6nWhPjSATw4Nb5JSlzQGk/K9seenv3kBejJ52SThOl2u fBaQpWtSBU2J+3PhtxN/3CsEP1umrif0nmdAlU8SNy1seoFRIJ3vJEq0GWa1eplqXQqY se99930DIUIkPHW/iYnAd2sEKUcXgzVjHOqdMCFlM38t+iQDOH1PICm2HLE81pCm1laV 8Hu4VkHkaGipPz7NZ4jrTD1tNk6hAZsWmqabVI3WSBZTiAkA3pDuYLAsYxxolPfk71+0 01nw== X-Gm-Message-State: AJaThX5FRsXd7IVE4a2LjUezYoGQe88yLwDOoc9va5B5TZANruuqqcXS XoetlNh4RVbWv1i4XO1ycxlurg== X-Google-Smtp-Source: AGs4zMbyqnE6pJFicrzsMvdtFlIwPoKz2+TCOqjZqheMG4T0p1utscSlfJ+uBfUxjYFCRZ1LwPaNXQ== X-Received: by 10.36.0.73 with SMTP id 70mr4360992ita.8.1512169866288; Fri, 01 Dec 2017 15:11:06 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id d186sm1113196itd.22.2017.12.01.15.11.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Dec 2017 15:11:05 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id 1AD62801058; Fri, 1 Dec 2017 17:11:05 -0600 (CST) Date: Fri, 1 Dec 2017 17:11:05 -0600 From: Justin Pryzby To: Vitaliy Garnashevich Cc: pgsql-performance@lists.postgresql.org Subject: Re: Bitmap scan is undercosted? Message-ID: <20171201231104.GN18413@telsasoft.com> References: <73a4936d-2814-dc08-ed0c-978f76f435b0@gmail.com> <20171201183427.GK18413@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20171201183427.GK18413@telsasoft.com> User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I tried to reproduce this issue and couldn't, under PG95 and 10.1: On Fri, Dec 01, 2017 at 12:34:27PM -0600, Justin Pryzby wrote: > On Fri, Dec 01, 2017 at 07:40:08PM +0200, Vitaliy Garnashevich wrote: > > We recently had an issue in production, where a bitmap scan was chosen > > instead of an index scan. Despite being 30x slower, the bitmap scan had > > about the same cost as the index scan. > > > drop table if exists aaa; > > create table aaa as select (id%100)::int num, (id%10=1)::bool flag from > > generate_series(1, 10000000) id; > > create index i1 on aaa  (num); > > create index i2 on aaa  (flag); > > analyze aaa; What is: effective_io_concurrency max_parallel_workers_per_gather (I gather you don't have this) Note: postgres=# SELECT correlation FROM pg_stats WHERE tablename='aaa' AND attname='num'; correlation | 0.00710112 ..so this is different from the issue corrected by the patch I created while testing. > Note that id%100==1 implies flag='t', so the planner anticipates retrieving > fewer rows than it will ultimately read, probably by 2x. It makes sense that > causes the index scan to be more expensive than expected, but that's only > somewhat important, since there's no joins involved. I changed the query from COUNT(*) TO * for easier to read explain: CREATE TABLE aaa AS SELECT (id%100)::int num, (id%10=1)::bool flag FROM generate_series(1, 10000000) id; CREATE INDEX i1 ON aaa(num); CREATE INDEX i2 ON aaa (flag); ANALYZE VERBOSE aaa; EXPLAIN (analyze,verbose,costs,buffers) SELECT * FROM aaa WHERE num=1 AND flag=true; Bitmap Heap Scan on public.aaa (cost=20652.98..45751.75 rows=10754 width=5) (actual time=85.314..185.107 rows=100000 loops=1) -> BitmapAnd (cost=20652.98..20652.98 rows=10754 width=0) (actual time=163.220..163.220 rows=0 loops=1) -> Bitmap Index Scan on i1 (cost=0.00..1965.93 rows=106333 width=0) (actual time=26.943..26.943 rows=100000 loops=1) -> Bitmap Index Scan on i2 (cost=0.00..18681.42 rows=1011332 width=0) (actual time=133.804..133.804 rows=1000000 loops=1) ..which is what's wanted with no planner hints (PG10.1 here). Same on PG95: postgres=# EXPLAIN (analyze,verbose,costs,buffers) SELECT * FROM aaa WHERE num=1 AND flag=true; Bitmap Heap Scan on public.aaa (cost=19755.64..43640.32 rows=9979 width=5) (actual time=230.017..336.583 rows=100000 loops=1) -> BitmapAnd (cost=19755.64..19755.64 rows=9979 width=0) (actual time=205.242..205.242 rows=0 loops=1) -> Bitmap Index Scan on i1 (cost=0.00..1911.44 rows=103334 width=0) (actual time=24.911..24.911 rows=100000 loops=1) -> Bitmap Index Scan on i2 (cost=0.00..17838.96 rows=965670 width=0) (actual time=154.237..154.237 rows=1000000 loops=1) The rowcount is off, but not a critical issue without a join. Justin