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 1geSxZ-0000LO-Ia for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Jan 2019 22:56:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1geSxX-0001aK-FY for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Jan 2019 22:56:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1geSxX-0001a8-0T for pgsql-hackers@lists.postgresql.org; Tue, 01 Jan 2019 22:56:27 +0000 Received: from mail-io1-xd31.google.com ([2607:f8b0:4864:20::d31]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1geSxO-0001fJ-PN for pgsql-hackers@postgresql.org; Tue, 01 Jan 2019 22:56:25 +0000 Received: by mail-io1-xd31.google.com with SMTP id c2so1205115iom.12 for ; Tue, 01 Jan 2019 14:56:18 -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:mime-version:content-disposition :user-agent; bh=firwuCaqcBjnAoQvJnCAjj/S2s4fukhkdbRI6E3R/64=; b=ijH3XuCo2qe1a6cm6hHNq+g8Ao0AEqb1k16lzttHOWOI62tg3jcl+Hj5WMXwx6fIwi EMmshqj9S7qp6z/I9fxHICsdvQ1EUuB6+ElCg2K7O6zZtk8qYmBN3g8yrI21N+JlFQEZ rwCg3slWAWdskbZENknZBtVc01D0HXjNgRYZYOtkyWZS5bSum+anvEnw9O4vpNuWqVhr +h9za2ggE3ARdq7dsc1YZJK7Flp8RngRnN1obETIRx7IdC8OCxlQX00/FueSdbt6AN4g 8kE2o5HVoz7ZhvG7pty1WHXC1LoAoS+U5rFbwOl31K9pIBB3b3X7TaZ9r4DdU7nJ0Uv8 5o6w== 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:mime-version :content-disposition:user-agent; bh=firwuCaqcBjnAoQvJnCAjj/S2s4fukhkdbRI6E3R/64=; b=n8qbj/h8kUsv7SleRvmU5nvqxyNR2ZEorFma+Irei+3HYrMg/kwrtWBRJsjQa35ldj lQezcyPxP/XyezbIGjAOMjmzOhZSZh1kcHX5EpRmxIZ2W7roXoBVmZ52G/xOhlirOSHF YqUUjeS/i77VTbDfQBCsqzpNnnEIBFgB4vXDlK3qwE+rF2yeEmIBA6JJHVZMRye6rMqk GGCT9cqzvswwJehBzDFievmpWLHwWIr8CVn+0epqX2MoyLvrE9ZEM4qm9eSxsl5EB/2a +NhdJMGbBaU2GOboheMc8pNEBIZ7+SDQUzP13RxWF6f4kyue8qZ27Li0GzYJLYwGjZxx DUNQ== X-Gm-Message-State: AJcUukcQCGtbRi3W7/ZiSuy1I+MNYy2dox1N9InsP28Hp79HAdNTyJen 4AqKPdszQae73hRcXcEmcPtWBQ== X-Google-Smtp-Source: ALg8bN7Ebiu7Boi5L7GzvnCSSLaRCTxoHTwcpyjyOoidEn8+ZV/GOJdEoibM5L2MetHVG2zD4IlRfA== X-Received: by 2002:a6b:b443:: with SMTP id d64mr29334877iof.227.1546383377188; Tue, 01 Jan 2019 14:56:17 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id u3sm20048676ita.18.2019.01.01.14.56.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Jan 2019 14:56:16 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id EC9DB8054BB; Tue, 1 Jan 2019 16:56:15 -0600 (CST) Date: Tue, 1 Jan 2019 16:56:15 -0600 From: Justin Pryzby To: Jeff Janes Cc: pgsql-hackers , Claudio Freire Subject: bitmaps and correlation Message-ID: <20190101225615.GF25379@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk It's a new year and I'm getting reflective, so resuming a portion of conversation we had here: https://www.postgresql.org/message-id/CAMkU%3D1yVbwEAugaCmKWxjaX15ZduWee45%2B_DqCw--d_3N_O_%3DQ%40mail.gmail.com Find attached patch which implements use of correlation statistic in costing for bitmap scans. An opened question in my mind is how to combine the correlation statistic with existing cost_per_page: . sqrt(a^2+b^2) . MIN() . multiply existing computation by new correlation component On Wed, Dec 20, 2017 at 09:55:40PM -0800, Jeff Janes wrote: > On Tue, Dec 19, 2017 at 7:25 PM, Justin Pryzby wrote: > > > I started playing with this weeks ago (probably during Vitaliy's problem > > report). Is there any reason cost_bitmap_heap_scan shouldn't interpolate > > based on correlation from seq_page_cost to rand_page_cost, same as > > cost_index ? > > I think that doing something like that is a good idea in general, but someone > has to implement the code, and so far no one seems enthused to do so. You > seem pretty interested in the topic, so.... I tested patch using CDR data which was causing issues for us years ago: https://www.postgresql.org/message-id/20160524173914.GA11880%40telsasoft.com Note: since that original problem report: . the SAN is backed by SSD rather than rotational storage; . we're using relkind=p partitioned tables; . PG12 uses pread() rather than lstat()+read(), so overhead of seek()+read() is avoided (but probably wasn't a substantial component of the problem); Unpatched, I'm unable to get bitmap scan without disabling indexscan and seqscan. | Bitmap Heap Scan on cdrs_huawei_pgwrecord_2018_12_25 (cost=55764.07..1974230.46 rows=2295379 width=1375) | Recheck Cond: ((recordopeningtime >= '2018-12-25 05:00:00-06'::timestamp with time zone) AND (recordopeningtime <= '2018-12-25 10:00:00-06'::timestamp with time zone)) | -> Bitmap Index Scan on cdrs_huawei_pgwrecord_2018_12_25_recordopeningtime_idx (cost=0.00..55190.22 rows=2295379 width=0) | Index Cond: ((recordopeningtime >= '2018-12-25 05:00:00-06'::timestamp with time zone) AND (recordopeningtime <= '2018-12-25 10:00:00-06'::timestamp with time zone)) Patched, I get bitmap scan when random_page_cost is reduced enough that startup cost (index scan component) is not prohibitive. But for simplicity, this just forces bitmap by setting enable_indexscan=off; | Bitmap Heap Scan on cdrs_huawei_pgwrecord_2018_12_25 (cost=55764.07..527057.45 rows=2295379 width=1375) | Recheck Cond: ((recordopeningtime >= '2018-12-25 05:00:00-06'::timestamp with time zone) AND (recordopeningtime <= '2018-12-25 10:00:00-06'::timestamp with time zone)) | -> Bitmap Index Scan on cdrs_huawei_pgwrecord_2018_12_25_recordopeningtime_idx (cost=0.00..55190.22 rows=2295379 width=0) | Index Cond: ((recordopeningtime >= '2018-12-25 05:00:00-06'::timestamp with time zone) AND (recordopeningtime <= '2018-12-25 10:00:00-06'::timestamp with time zone)) That addresses the issue we had in part. A remaining problem is that correlation fails to distinguish between "fresh" index and fragmented index, and so heap access of a correlated index may looks cheaper than it is. Which is why I still have to set random_page_cost to get bitmap scan. Justin