Sub topic 10
Indexes on Partitioned Tables
This chapter assumes basic SQL, PostgreSQL table definitions, indexes, constraints, and EXPLAIN. An index is a data structure that helps PostgreSQL find rows without scanning every row in a table.
One logical index, many physical indexes
A partitioned table is one logical table made from several physical tables. An index created on the partitioned table is also logical. PostgreSQL creates a corresponding physical index for each partition.
For example:
CREATE TABLE orders (
order_id bigint NOT NULL,
customer_id bigint NOT NULL,
order_date date NOT NULL,
amount numeric(12, 2) NOT NULL
) PARTITION BY RANGE (order_date);
CREATE TABLE orders_2026_01
PARTITION OF orders
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE orders_2026_02
PARTITION OF orders
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');
CREATE INDEX orders_customer_id_idx
ON orders (customer_id);The last statement creates a partitioned index on orders and an index on customer_id in each existing partition. Queries can continue to name orders; PostgreSQL uses the relevant child indexes internally.
A partition added later should also have the indexes required by the table's workload. Check the result after attaching or creating a partition rather than assuming that every operational path created the desired index.
Indexing and partition pruning
Partitioning can reduce the number of rows PostgreSQL considers. This is called partition pruning. An index can then reduce the work inside each remaining partition.
EXPLAIN (COSTS OFF)
SELECT order_id, customer_id, amount
FROM orders
WHERE order_date >= DATE '2026-01-10'
AND order_date < DATE '2026-01-15'
AND customer_id = 42;The order_date condition lets PostgreSQL prune unrelated partitions. The customer_id condition can use the local index in the partition that remains. An index on order_date is often unnecessary when that column is the range key and pruning already removes most partitions.
Constraints and unique indexes
A unique constraint on a partitioned table has an important limitation. PostgreSQL must be able to check uniqueness across partitions. Therefore, a unique constraint or unique index on the partitioned table must include every partition-key column.
CREATE UNIQUE INDEX orders_order_id_date_idx
ON orders (order_id, order_date);This can enforce uniqueness for the combined values, but it does not make order_id alone globally unique if the same identifier can occur in different partitions. Design identifiers and constraints with that boundary in mind.
Practical checks
Inspect indexes with the PostgreSQL client:
\d+ orders
\d+ orders_2026_01Compare the parent definition with every partition. Test representative queries with EXPLAIN (ANALYZE, BUFFERS) in a non-production environment, and confirm both pruning and index usage.
Exercise
Why might an index on the partition key provide little benefit for a range-partitioned table?
Show answer
A predicate on the partition key can prune unrelated partitions first, so each remaining partition may already contain a small enough range that another index adds little value.