For any given query, the use of an index depends on the cost of using that index compared to a sequential scan
Frequently developers think that because there is an index, a query should run faster, and if a query runs slow, an index is the solution. This is usually the case when the query will return few tuples. But as the number of tuples in the result increases, the cost of using an index might increase.
You are using postgres. Postgres does not support clustering around a given attribute. That means that postgres, when confronted with a range query (of the type att > a and att < b) needs to compute an estimation of the number of tuples in the result (make sure you vacuum your database frequently) and the cost of using an index compared to doing a sequential scan. it will then decide what method to use.
you can inspect this decision by running
EXPLAIN ANALYZE <query>;
in psql. It will tell you if it uses an index or not.
If you really, really want to use the indexes instead of a sequential scan (sometimes it is needed) and you really really know what you are doing, you can change the cost of a sequential scan in the planner constants or disable sequential scans in favor of any other method. See this page for the details:
http://www.postgresql.org/docs/9.1/static/runtime-config-query.html
Make sure you browse the correct version of the documentation.
--dmg
explain analyze
results and report back. Since you're using a query generator you might need to useauto_explain
or to log queries and re-execute them by hand. – Craig Ringer May 19 '13 at 23:43