← Back to PostgreSQL, page 2

postgresql-0037 · Medium mid

Use a partial index for a small, frequently queried subset of rows.

A partial index (CREATE INDEX ... WHERE condition) indexes only rows matching a predicate, such as open tickets. It is smaller and cheaper to maintain than a full index, and the planner uses it when the query's WHERE clause implies the index predicate. Partial unique indexes also enforce rules such as one active row per user.

Full answer

Tables and rows used here:

sql
CREATE TABLE tickets (id int, status text, assignee text);
INSERT INTO tickets SELECT g, CASE WHEN g % 50 = 0 THEN 'open' ELSE 'closed' END, 'agent' || (g % 7) FROM generate_series(1, 20000) g;
CREATE INDEX tickets_open_assignee ON tickets (assignee) WHERE status = 'open';
CREATE INDEX tickets_all_assignee ON tickets (assignee);
ANALYZE tickets;
sql
SELECT indexrelid::regclass::text AS index_name, pg_relation_size(indexrelid) < pg_relation_size('tickets_all_assignee') AS smaller_than_full FROM pg_index WHERE indrelid = 'tickets'::regclass ORDER BY 1;

Output:

text
       index_name      | smaller_than_full
-----------------------+-------------------
 tickets_all_assignee  | f
 tickets_open_assignee | t
(2 rows)
sql
EXPLAIN (COSTS OFF) SELECT id FROM tickets WHERE status = 'open' AND assignee = 'agent3';

PostgreSQL returns:

text
Bitmap Heap Scan on tickets
  Recheck Cond: ((assignee = 'agent3'::text) AND (status = 'open'::text))
  ->  Bitmap Index Scan on tickets_open_assignee
        Index Cond: (assignee = 'agent3'::text)
sql
CREATE UNIQUE INDEX one_open_per_assignee ON tickets (assignee) WHERE status = 'open' AND id > 20000; INSERT INTO tickets VALUES (20001, 'open', 'agent1');

Result on PostgreSQL 17:

text
INSERT 0 1
sql
INSERT INTO tickets VALUES (20002, 'open', 'agent1');

Result:

text
ERROR:  duplicate key value violates unique constraint "one_open_per_assignee"

The partial index holds only the 400 open tickets, so it is a fraction of the full index's size. The query that filters on status = 'open' uses it. The planner can only choose it when the query states the same condition; a query on assignee alone could not use it. The partial unique index, here restricted to new rows so the demo data does not conflict, allows one open ticket per assignee and rejects a second with a duplicate key error.

sql
CREATE TABLE tickets (id int, status text, assignee text);
INSERT INTO tickets SELECT g, CASE WHEN g % 50 = 0 THEN 'open' ELSE 'closed' END, 'agent' || (g % 7) FROM generate_series(1, 20000) g;
CREATE INDEX tickets_open_assignee ON tickets (assignee) WHERE status = 'open';
CREATE INDEX tickets_all_assignee ON tickets (assignee);
ANALYZE tickets;

SELECT indexrelid::regclass::text AS index_name, pg_relation_size(indexrelid) < pg_relation_size('tickets_all_assignee') AS smaller_than_full FROM pg_index WHERE indrelid = 'tickets'::regclass ORDER BY 1;

EXPLAIN (COSTS OFF) SELECT id FROM tickets WHERE status = 'open' AND assignee = 'agent3';

CREATE UNIQUE INDEX one_open_per_assignee ON tickets (assignee) WHERE status = 'open' AND id > 20000; INSERT INTO tickets VALUES (20001, 'open', 'agent1');

INSERT INTO tickets VALUES (20002, 'open', 'agent1');

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Partial indexes cover a subset
  • Smaller and cheaper to maintain
  • Queries must imply the predicate
  • Partial unique indexes enforce conditional rules
  • Good for hot subsets such as open items

Common mistakes

  • Writing a query condition that differs from the index predicate
  • Indexing every row when queries touch 2%

Follow-ups

  • How would you enforce one default address per user?
  • Do parameterised queries use partial indexes?

Tips

  • Match the predicate