Index only the rows you search: PostgreSQL partial indexes
A partial index covers part of a table, set by a WHERE condition, so it holds only the rows that match. But the query's condition must imply the index condition, and parameterized values do not work with it.

Your orders table has millions of rows, almost all of them billed, and the page you open every day looks for the few that are still unbilled. A normal index on the whole table carries all those rows, even the ones you will never look for. By the end of this article you will know how to index only the rows that matter to you, and the mistake that makes a query ignore that index.
What is a partial index?
A partial index is an index built over part of a table, and that part is set by a condition called the predicate. The index holds only the rows that match the condition.
That is how the PostgreSQL documentation on partial indexes defines it. The condition is the WHERE at the end of a CREATE INDEX command, like any WHERE you write in a query.
When does it help?
It helps when the queries you care about always look at the same small part of the table, like orders that are not billed yet.
The same documentation gives this exact example:
CREATE INDEX orders_unbilled_index ON orders (order_nr)
WHERE billed IS NOT TRUE;The index here holds only orders where billed is not true. A query that looks for an unbilled order can use it.
How do you make a unique constraint on some rows only?
You make it with a unique index that has a WHERE. The index then enforces uniqueness among the rows that match the condition, and leaves the other rows unconstrained.
The documentation's example: a table of test results, where for each subject and target you want only one successful result, and as many failed ones as there are:
CREATE UNIQUE INDEX tests_success_constraint ON tests (subject, target)
WHERE success;Why can a query ignore the index?
A query ignores a partial index when PostgreSQL cannot recognize that the query's WHERE condition mathematically implies the index condition.
The documentation says this matching happens at query planning time, not at run time. So parameterized query clauses do not work with a partial index: a prepared query with x < ? will never imply x < 2 for every value that may take the place of the question mark.
That is the human mistake. The index is right, the query in your ORM is right, but the value in the condition is passed as a parameter, so the plan never touches the index. Check the plan with EXPLAIN ANALYZE before you assume the index works, and keep the part that matches the index condition written in the query itself, not passed in. That is for the fixed condition only: values that come from the user stay parameters, because they are your protection from SQL injection.
When should you not use it?
You should not use it in place of partitioning. The documentation warns against making one partial index per value of a column, like one index for category = 1, one for category = 2 and so on.
It calls this a bad idea, and says you will almost certainly be better off with a single normal index:
CREATE INDEX mytable_cat_data ON mytable (category, data);And if the table is so large that a single index really is a bad idea, its answer is partitioning, not many partial indexes.
Where do you start now?
Open the slowest query that runs every day in your app, and look at its WHERE: if it always looks for a small, fixed part of the table, try a partial index on it, and check the plan before and after. For more of the everyday tools, see the developer tools articles. Which table of yours always looks at the same small part?
FAQ
Should I make a partial index for each value in a category column?
No. The PostgreSQL documentation calls it a bad idea; a single normal index on both columns is better, or partitioning if the table is very large.
Can I make a unique constraint on only some rows?
Yes. A unique index with WHERE enforces uniqueness among the rows that match the condition, and leaves the rest unconstrained.
How do I know if a query uses the index?
Run the query with EXPLAIN and read the plan: if the index name is in it, the query is using it.


