Re: Bitmap scan is undercosted?

2018-02-24 Thread Vitaliy Garnashevich
Sorry for delay with response, I had to switch to other tasks and didn't have time to run proper tests and write some meaningful response. Recently,  a similar issue happened with another our database, so I decided to write an update. Bitmap scan was preferred to index scan by the planner, bu

Re: Bitmap scan is undercosted? - overestimated correlation and cost_index

2017-12-16 Thread Justin Pryzby
On Fri, Dec 15, 2017 at 02:54:06PM -0600, Justin Pryzby wrote: > SSD: good question. > > Here's an rackspace VM with PG9.6.6, 2GB shared_buffers, 8GB RAM (~4GB of > which > is being used as OS page cache), and 32GB SSD (with random_page_cost=1). The > server is in use by our application. > > I

Re: Bitmap scan is undercosted? - overestimated correlation and cost_index

2017-12-16 Thread Justin Pryzby
On Fri, Dec 15, 2017 at 02:54:06PM -0600, Justin Pryzby wrote: > SSD: good question. > > Here's an rackspace VM with PG9.6.6, 2GB shared_buffers, 8GB RAM (~4GB of > which > is being used as OS page cache), and 32GB SSD (with random_page_cost=1). The > server is in use by our application. > > I

Re: Bitmap scan is undercosted? - overestimated correlation and cost_index

2017-12-15 Thread Justin Pryzby
On Tue, Dec 12, 2017 at 01:29:48AM -0800, Jeff Janes wrote: > On Wed, Dec 6, 2017 at 1:46 PM, Justin Pryzby wrote: > > On Tue, Dec 05, 2017 at 01:50:11PM -0500, Tom Lane wrote: > > > In any case, given that we do this calculation without regard > > > to any specific index, > > > > One solution is

Re: Bitmap scan is undercosted? - overestimated correlation and cost_index

2017-12-12 Thread Jeff Janes
On Wed, Dec 6, 2017 at 1:46 PM, Justin Pryzby wrote: > On Tue, Dec 05, 2017 at 01:50:11PM -0500, Tom Lane wrote: > > Jeff Janes writes: > > > On Dec 3, 2017 15:31, "Tom Lane" wrote: > > >> Jeff Janes writes: > > >>> But I do see that ties within the logical order of the column values > are > >

Re: Bitmap scan is undercosted?

2017-12-11 Thread Jeff Janes
On Tue, Dec 5, 2017 at 11:06 PM, Vitaliy Garnashevich < vgarnashev...@gmail.com> wrote: This is very cool, thanks. > I've tried to create a better test case: > - Increase shared_buffers and effective_cache_size to fit whole database, > including indexes. > - Use random(), to avoid correlation b

Re: Bitmap scan is undercosted? - boolean correlation

2017-12-06 Thread Jeff Janes
On Tue, Dec 5, 2017 at 10:50 AM, Tom Lane wrote: > Jeff Janes writes: > > On Dec 3, 2017 15:31, "Tom Lane" wrote: > >> Jeff Janes writes: > >>> But I do see that ties within the logical order of the column values > are > >>> broken to agree with the physical order. That is wrong, right? Is >

Re: Bitmap scan is undercosted?

2017-12-06 Thread Jeff Janes
On Sun, Dec 3, 2017 at 1:15 PM, Vitaliy Garnashevich < vgarnashev...@gmail.com> wrote: > On 02/12/2017 23:17, Jeff Janes wrote: > > Right, so there is a cpu costing problem (which could only be fixed by > hacking postgresql and recompiling it), but it is much smaller of a problem > than the IO cos

Re: Bitmap scan is undercosted?

2017-12-06 Thread Vitaliy Garnashevich
What seems odd to me is that in different kinds of tests (with different frequency of column values): i1 Rows Removed by Filter = 900156, 179792, 89762 (decreased a lot) i1 buffers = 46983, 44373, 39928 (decreased, but not a lot) i1 best case time = 756.045, 127.814, 79.492 (decreased a lot, a

Re: Bitmap scan is undercosted? - overestimated correlation and cost_index

2017-12-06 Thread Justin Pryzby
On Tue, Dec 05, 2017 at 01:50:11PM -0500, Tom Lane wrote: > Jeff Janes writes: > > On Dec 3, 2017 15:31, "Tom Lane" wrote: > >> Jeff Janes writes: > >>> But I do see that ties within the logical order of the column values are > >>> broken to agree with the physical order. That is wrong, right?

Re: Bitmap scan is undercosted? - boolean correlation

2017-12-05 Thread Tom Lane
Jeff Janes writes: > On Dec 3, 2017 15:31, "Tom Lane" wrote: >> Jeff Janes writes: >>> But I do see that ties within the logical order of the column values are >>> broken to agree with the physical order. That is wrong, right? Is there >>> any argument that this is desirable? >> Uh ... what d

Re: Bitmap scan is undercosted?

2017-12-03 Thread Tom Lane
I wrote: > I tried creating multiple-column statistics using the v10 facility for > that: > regression=# create statistics s1 on num, flag from aaa; > CREATE STATISTICS > regression=# analyze aaa; > ANALYZE > but that changed the estimate not at all, which surprised me because > dependency statisti

Re: Bitmap scan is undercosted? - boolean correlation

2017-12-03 Thread Jeff Janes
On Dec 3, 2017 15:31, "Tom Lane" wrote: Jeff Janes writes: > On Sat, Dec 2, 2017 at 8:04 PM, Justin Pryzby wrote: >> It thinks there's somewhat-high correlation since it gets a list of x >> and y values (integer positions by logical and physical sort order) and >> 90% of the x list (logical val

Re: Bitmap scan is undercosted? - boolean correlation

2017-12-03 Thread Tom Lane
Jeff Janes writes: > On Sat, Dec 2, 2017 at 8:04 PM, Justin Pryzby wrote: >> It thinks there's somewhat-high correlation since it gets a list of x >> and y values (integer positions by logical and physical sort order) and >> 90% of the x list (logical value) are the same value ('t'), and the >> C

Re: Bitmap scan is undercosted? - boolean correlation

2017-12-03 Thread Jeff Janes
On Sat, Dec 2, 2017 at 8:04 PM, Justin Pryzby wrote: > On Sat, Dec 02, 2017 at 05:27:51PM -0800, Jeff Janes wrote: > > I think the non-extended stats code also has trouble with booleans. > > pg_stats gives me a correlation of 0.8 or higher for the flag column. > > It's not due to the boolean tho

Re: Bitmap scan is undercosted?

2017-12-03 Thread Vitaliy Garnashevich
On 03/12/2017 03:27, Jeff Janes wrote: Due to that, when I disable bitmapscans and seqscans, I start getting slow index scans on the wrong index, i2 rather than i1.  I don't know why he doesn't see that in his example. When I increase effective_cache_size to 1024MB, I start getting the plan wit

Re: Bitmap scan is undercosted?

2017-12-03 Thread Vitaliy Garnashevich
On 03/12/2017 01:44, Tom Lane wrote: I think it'd be a serious error to screw around with your cost settings on the basis of a single case in which the rowcount estimates are so far off. It's really those estimates that are the problem AFAICS. The core issue in this example is that, the way the

Re: Bitmap scan is undercosted?

2017-12-03 Thread Vitaliy Garnashevich
On 02/12/2017 23:17, Jeff Janes wrote: Right, so there is a cpu costing problem (which could only be fixed by hacking postgresql and recompiling it), but it is much smaller of a problem than the IO cost not being accurate due to the high hit rate. Fixing the CPU costing problem is unlikely to m

Re: Bitmap scan is undercosted? - boolean correlation

2017-12-02 Thread Justin Pryzby
On Sat, Dec 02, 2017 at 05:27:51PM -0800, Jeff Janes wrote: > I think the non-extended stats code also has trouble with booleans. > pg_stats gives me a correlation of 0.8 or higher for the flag column. It's not due to the boolean though; you see the same thing if you do: CREATE INDEX aaa_f ON aaa

Re: Bitmap scan is undercosted?

2017-12-02 Thread Jeff Janes
On Sat, Dec 2, 2017 at 3:44 PM, Tom Lane wrote: > Jeff Janes writes: > > On Fri, Dec 1, 2017 at 11:08 PM, Vitaliy Garnashevich < > > vgarnashev...@gmail.com> wrote: > >> # x4 tuple/operator costs - bitmap scan still a bit cheaper > >> set seq_page_cost = 1.0; > >> set random_page_cost = 1.0; > >

Re: Bitmap scan is undercosted?

2017-12-02 Thread Tom Lane
Jeff Janes writes: > On Fri, Dec 1, 2017 at 11:08 PM, Vitaliy Garnashevich < > vgarnashev...@gmail.com> wrote: >> # x4 tuple/operator costs - bitmap scan still a bit cheaper >> set seq_page_cost = 1.0; >> set random_page_cost = 1.0; >> set cpu_tuple_cost = 0.04; >> set cpu_index_tuple_cost = 0.02;

Re: Bitmap scan is undercosted?

2017-12-02 Thread Jeff Janes
On Fri, Dec 1, 2017 at 11:08 PM, Vitaliy Garnashevich < vgarnashev...@gmail.com> wrote: > > > seq_page_cost = 0.0 > random_page_cost = 0.0 > explain analyze select * from aaa where num = 2 and flag = true; > > Bitmap Heap Scan on aaa (cost=753.00..2003.00 rows=10257 width=5) (actual > time=82.212

Re: Bitmap scan is undercosted?

2017-12-01 Thread Vitaliy Garnashevich
On 02/12/2017 07:51, Jeff Janes wrote: On Fri, Dec 1, 2017 at 3:54 PM, Vitaliy Garnashevich mailto:vgarnashev...@gmail.com>> wrote: On 02/12/2017 01:11, Justin Pryzby wrote: I tried to reproduce this issue and couldn't, under PG95 and 10.1: On Fri, Dec 01, 2017 at 12:34:27

Re: Bitmap scan is undercosted?

2017-12-01 Thread Justin Pryzby
On Sat, Dec 02, 2017 at 01:54:09AM +0200, Vitaliy Garnashevich wrote: > On 02/12/2017 01:11, Justin Pryzby wrote: > >..which is what's wanted with no planner hints (PG10.1 here). > Yes, that's what you get without planner hints, but it's strange to get this > plan, when there is another one, which

Re: Bitmap scan is undercosted?

2017-12-01 Thread Jeff Janes
On Fri, Dec 1, 2017 at 3:54 PM, Vitaliy Garnashevich < vgarnashev...@gmail.com> wrote: > On 02/12/2017 01:11, Justin Pryzby wrote: > >> 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, 201

Re: Bitmap scan is undercosted?

2017-12-01 Thread Justin Pryzby
On Fri, Dec 01, 2017 at 05:11:04PM -0600, Justin Pryzby wrote: > I tried to reproduce this issue and couldn't, under PG95 and 10.1: I'm embarassed to say that I mis-read your message, despite you're amply clear subject. You're getting a bitmap scan but you'd prefer to get an index scan. I anticip

Re: Bitmap scan is undercosted?

2017-12-01 Thread Vitaliy Garnashevich
On 02/12/2017 01:11, Justin Pryzby wrote: 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 bitm

Re: Bitmap scan is undercosted?

2017-12-01 Thread Justin Pryzby
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 ind

Re: Bitmap scan is undercosted?

2017-12-01 Thread Vitaliy Garnashevich
On 01/12/2017 20:34, 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. M

Re: Bitmap scan is undercosted?

2017-12-01 Thread Justin Pryzby
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. Me too, see also: https://www.postgresql

Bitmap scan is undercosted?

2017-12-01 Thread Vitaliy Garnashevich
Hi, 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. I've found some cases where similar issues with bitmap scans were reported before: https://www.postgresq