Postgresql – Can postgres make use of both GIN and BTREE indexes in a single query

postgresqlpostgresql-9.4

Let's say I have GIN index on column A that allows me to filter table fast on that column by appropriate WHERE condition which would be slow without that index. However I would like also to order filtered rows by using ORDER BY statement on, let's say column B. Ordering without index is slow, so I use BTREE index on column B. The problem is that pg don't want to use both indexes – if both WHERE cond on column A and ORDER BY B are present in query, pg uses only GIN index. When I remove WHERE cond from query, then it uses BTREE index on col B.

What I can do to force pg to use both indexes, one to filter rows and second one to order them?

Best Answer

To answer your title question, PostgreSQL can make use of multiple indexes for a "bitmap" scan, but only with Boolean logic. It can't use one to order and the other to filter within the same table scan. It is an interesting idea, though.