From: | Marco Carlo Moriggi <marco(dot)moriggi(at)zucchetti(dot)it> |
---|---|
To: | "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org> |
Cc: | Simone Reccagni <Simone(dot)Reccagni(at)zucchetti(dot)it>, Paolo Ravizza <Paolo(dot)Ravizza(at)zucchetti(dot)it> |
Subject: | Planner using wrong composite index with date interval statically calculated |
Date: | 2020-07-29 10:37:39 |
Message-ID: | b91bf7b86f3f4e09b40994a8f73fddf6@zucchetti.it |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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
Attachment | Content-Type | Size |
---|---|---|
test.sql | application/octet-stream | 2.9 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2020-07-29 14:10:21 | Re: pg_dump seems to be broken in regards to the "--exclude-table-data" option on Windows. |
Previous Message | PG Bug reporting form | 2020-07-29 09:11:54 | BUG #16559: ERROR: canceling autovacuum task CONTEXT: automatic analyze of table pg_catalog.pg_attribute |