Re: [BUGS] Query planner skipping index depending on DISTINCTparameter order (2)

Поиск
Список
Период
Сортировка
От Tomas Vondra
Тема Re: [BUGS] Query planner skipping index depending on DISTINCTparameter order (2)
Дата
Msg-id cd89d79f-96df-1a1e-5821-ef111950eccf@2ndquadrant.com
обсуждение исходный текст
Ответ на [BUGS] Query planner skipping index depending on DISTINCT parameter order(2)  (Дилян Палаузов<dpa-postgres@aegee.org>)
Ответы Re: [BUGS] Query planner skipping index depending on DISTINCTparameter order (2)  (Dilyan Palauzov <dilyan.palauzov@aegee.org>)
Список pgsql-bugs
Hello Dilyan,

You're right - we're currently not able to use the index if it's not
consistent with the DISTINCT ordering. That is, if you have index on
(a,b) and DISTINCT ON (b,a) we fail to leverage the index.

The reason for this simple - if you look at create_distinct_paths [1],
which is where the Unique path comes from, you'll see it iterates over
all paths and compares the ordering using pathkeys_is_contained [2].

That however only ensures the path matches the expected Unique ordering
(determined by the column list in DISTINCT ON clause), we don't try to
re-shuffle the columns in any way at this point.

So this is more a missing optimization than a bug, I'd guess. But it
seems worthwhile and possibly not extremely difficult to implement, so I
may look into it - but that's PG11 at the earliest.

But, looking at the code in create_distinct_paths, ISTM you can easily
convince the planner to use the index by simply adding a matching ORDER
BY clause. That is
   SELECT DISTINCT ON(token, id) token  FROM bayes_token   ORDER BY id, token;

should be able to use the index on (id,token).


[1]
https://github.com/postgres/postgres/blob/master/src/backend/optimizer/plan/planner.c#L4725

[2]
https://github.com/postgres/postgres/blob/master/src/backend/optimizer/plan/planner.c#L4811


regards

-- 
Tomas Vondra                  http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

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

Предыдущее
От: Tomas Vondra
Дата:
Сообщение: Re: [BUGS] 】
Следующее
От: Tom Lane
Дата:
Сообщение: Re: [BUGS] BUG #14815: event trigger in extension