Planner using wrong composite index with date interval statically calculated

Поиск
Список
Период
Сортировка
От Marco Carlo Moriggi
Тема Planner using wrong composite index with date interval statically calculated
Дата
Msg-id b91bf7b86f3f4e09b40994a8f73fddf6@zucchetti.it
обсуждение исходный текст
Список pgsql-bugs

Hi,

I’m writing for Zucchetti S.p.A.

 

We have a situation where a table has a small composite index e.g. (field1 varchar(10), field2 varchar(10)), and a wider index e.g. (field2 varchar(10), field1 varchar(10), field3 varchar(50), field4 date).

 

We added the second index in our production database to tune a query on a table with millions or records, checking that, when used like

select id from tbl where field1=’fixed_val1’ and field2=’fixed_val2’, and field3 = ‘fixed_val3’ and field4 between fixed_date1 and fixed_date2

The planner was using this new index.

 

In the practice we noticed that the planner was choosing the old one, anyway.

 

A closer look to the generated query showed that the date interval was not written as we expected by the application, but in this way:

select id from tbl where field1=’fixed_val1’ and field2=’fixed_val2’, and field3 = ‘fixed_val3’ and field4 between (substr(‘fixed_date1’, 1, 4)||’-01-01’)::date and (substr(‘fixed_date2’, 1, 4)|| ‘-12-31’)::date

 

Running explain analyse on both queries finally revealed that in this way the planner was using a first index scan on the old index, and then a sequential scan to filter the result set. (running for 3.5s instead of 0.496ms in our production database)

 

Anyway, the result of the two substring can be calculated once and then used in the exact same way of our first query. We placed a patch to generate the query in the right way, but I think it should be corrected also on the DB server query planner.

 

Attached to this email there’s a test sequence of operation to reproduce the problem with random data.

 

Thanks in advance,

Marco

Вложения

В списке pgsql-bugs по дате отправления:

Предыдущее
От: PG Bug reporting form
Дата:
Сообщение: BUG #16559: ERROR: canceling autovacuum task CONTEXT: automatic analyze of table pg_catalog.pg_attribute
Следующее
От: Tom Lane
Дата:
Сообщение: Re: pg_dump seems to be broken in regards to the "--exclude-table-data" option on Windows.