Sub topic 10
Indexes on partitioned tables
This chapter assumes you can create ordinary indexes, read basic EXPLAIN output, and have seen PARTITION BY RANGE and PARTITION OF. It also assumes you know that a primary key or unique constraint on a partitioned table must include the partition key. Here you will see how an index on the parent table relates to the indexes on its partitions, and how to build, check and maintain those indexes without blocking writes for long.
A partitioned index is a template, not a data structure
A partitioned table stores no rows, and an index on it stores no entries. When you run CREATE INDEX on the parent, PostgreSQL does two things:
- It records a partitioned index on the parent. This is a catalog entry with no storage.
- It builds an ordinary index on every existing partition and ties each one to the parent entry.
Partitions created or attached later get a matching index automatically. Every index is therefore local: it covers one partition only. PostgreSQL has no global index that spans partitions. This explains most of the rules in this chapter.
CREATE TABLE readings (
device_id int NOT NULL,
recorded_at timestamptz NOT NULL,
reading numeric
) PARTITION BY RANGE (recorded_at);
CREATE TABLE readings_2026_01 PARTITION OF readings
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE readings_2026_02 PARTITION OF readings
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');
CREATE INDEX ON readings (device_id, recorded_at);
SELECT tablename, indexname
FROM pg_indexes
WHERE tablename LIKE 'readings%'
ORDER BY tablename, indexname;The query lists three indexes: readings_device_id_recorded_at_idx on the parent, and readings_2026_01_device_id_recorded_at_idx and readings_2026_02_device_id_recorded_at_idx on the partitions. PostgreSQL generates the names from the table and column names.
How queries use the indexes
The planner first prunes partitions using the partition key. It then chooses an index scan, bitmap scan or sequential scan separately for each surviving partition. This has two practical consequences.
- A query that filters on
recorded_attouches only the matching partitions. A small index on each one is cheaper than one large index on a single big table. - A query that does not filter on the partition key cannot be pruned. A lookup such as
WHERE device_id = 7probes the index of every partition, so the cost grows with the partition count.
Put the partition key in the index, or at least make sure your common queries filter on it. Time-partitioned tables with append-only data also suit BRIN indexes, which stay tiny inside each partition.
Unique indexes must include the partition key
Because each index is local, PostgreSQL can only guarantee uniqueness across partitions if the partition key is part of the index. That way, equal keys always land in the same partition.
-- Allowed: includes the partition key recorded_at
CREATE UNIQUE INDEX ON readings (device_id, recorded_at);
-- Rejected: PostgreSQL reports that the unique constraint on the
-- partitioned table must include all partitioning columns
-- CREATE UNIQUE INDEX ON readings (device_id);The same rule applies to primary keys and UNIQUE constraints. If you need uniqueness on a column that is not in the partition key, such as a global order_id, the usual options are:
- Add the partition key to the constraint, for example
(order_id, placed_at). This only guarantees uniqueness per combination, so the application must makeorder_idunique, for example by drawing it from a sequence. - Partition by that column, for example by hash.
- Do not partition the table.
Building indexes without blocking writes
Plain CREATE INDEX on the parent takes a lock that blocks writes on the parent and all its partitions until every partition index is built. On a large table that can be a long outage. CREATE INDEX CONCURRENTLY is not supported on a partitioned table, so use this four-step workflow instead:
- Create the parent index with
ON ONLY. This builds no partition indexes, and the parent index is marked invalid. - Build an index on each partition with
CREATE INDEX CONCURRENTLY. - Attach each partition index to the parent index with
ALTER INDEX ... ATTACH PARTITION. - When every partition is attached, the parent index becomes valid automatically.
CREATE INDEX readings_reading_idx ON ONLY readings (reading);
CREATE INDEX CONCURRENTLY readings_2026_01_reading_idx
ON readings_2026_01 (reading);
CREATE INDEX CONCURRENTLY readings_2026_02_reading_idx
ON readings_2026_02 (reading);
ALTER INDEX readings_reading_idx
ATTACH PARTITION readings_2026_01_reading_idx;
ALTER INDEX readings_reading_idx
ATTACH PARTITION readings_2026_02_reading_idx;
SELECT indexrelid::regclass AS index_name, indisvalid
FROM pg_index
WHERE indexrelid = 'readings_reading_idx'::regclass;CREATE INDEX CONCURRENTLY cannot run inside a transaction block, so run these statements one at a time or with autocommit. After the first ATTACH the query reports indisvalid = false. After the last one it reports true.
Checking index state
An invalid parent index is a common surprise. Queries still work, but the planner does not use an invalid index. Check for invalid indexes on the whole partition tree:
SELECT i.indexrelid::regclass AS index_name,
i.indrelid::regclass AS table_name,
i.indisvalid
FROM pg_index i
WHERE i.indrelid IN (SELECT relid FROM pg_partition_tree('readings'))
ORDER BY i.indisvalid, table_name;pg_partition_tree lists the parent and all partitions. You can also pass an index name to it to see the parent and children of a partitioned index. In psql, \d+ readings shows the indexes and marks invalid ones.
Indexes and attaching partitions
When you attach a partition, PostgreSQL has to give it every index the parent has. For each parent index, it looks for an equivalent index on the new partition, with the same columns, expressions, predicate, operator classes and access method. If it finds one, it adopts it. If it does not, it builds one while holding a lock on the new partition.
To keep ATTACH PARTITION fast, create the matching indexes on the standalone table before attaching it:
CREATE TABLE readings_2026_03 (LIKE readings INCLUDING DEFAULTS);
-- load data here, then index and constrain it
CREATE INDEX ON readings_2026_03 (device_id, recorded_at);
CREATE INDEX ON readings_2026_03 (reading);
ALTER TABLE readings_2026_03 ADD CONSTRAINT readings_2026_03_range
CHECK (recorded_at >= '2026-03-01' AND recorded_at < '2026-04-01');
ALTER TABLE readings ATTACH PARTITION readings_2026_03
FOR VALUES FROM ('2026-03-01') TO ('2026-04-01');The CHECK constraint lets PostgreSQL skip the validation scan. The pre-built indexes let it skip index creation.
Dropping, detaching and extra indexes
- You cannot drop a partition's index on its own if it belongs to a partitioned index. PostgreSQL refuses, because the parent index depends on it. Drop the parent index with
DROP INDEX, which removes every partition index too. - A partition can have extra indexes that the parent does not have. Create them directly on the partition. They are local to that partition and nothing manages them for you. This is useful for a hot, recent partition.
- Detaching a partition keeps its indexes. After
DETACH PARTITION, they become ordinary standalone indexes, and dropping the table drops them. - Re-attaching later reuses an equivalent index if one exists.
Rebuilding indexes
REINDEX accepts a partitioned table or partitioned index from PostgreSQL 14 on. It rebuilds each partition's index in turn, and REINDEX ... CONCURRENTLY processes the partitions in separate transactions. If you must rebuild a single partition, run REINDEX INDEX CONCURRENTLY on its partition index directly.
Costs to keep in mind
Every index is multiplied by the partition count. A table with 5 indexes and 500 partitions has 2,500 partition indexes plus 5 parent entries. That increases catalog size, planning time and the number of files on disk. Each insert updates only the indexes of the partition the row lands in. This is a real benefit for write-heavy tables, because those indexes are small and often stay in cache.
| Concern | Behavior on a partitioned table |
|---|---|
| Index scope | Local to one partition |
| Unique index | Must include every partition key column |
| `CREATE INDEX CONCURRENTLY` on parent | Not supported, use `ON ONLY` plus `ATTACH` |
| Drop one partition's index | Refused while it belongs to a parent index |
| New or attached partitions | Receive matching indexes automatically |
| Invalid parent index | Not used by the planner until all partitions are attached |
Exercise
You need a non-blocking index on column reading of a partitioned table readings with partitions readings_2026_01 and readings_2026_02. Write the statements in order, and say how you would confirm that the parent index is usable.
Show answer
Run CREATE INDEX readings_reading_idx ON ONLY readings (reading);, then CREATE INDEX CONCURRENTLY readings_2026_01_reading_idx ON readings_2026_01 (reading); and the same for readings_2026_02. Next run ALTER INDEX readings_reading_idx ATTACH PARTITION readings_2026_01_reading_idx; and the same for the second partition. Confirm with SELECT indisvalid FROM pg_index WHERE indexrelid = 'readings_reading_idx'::regclass;, which must return true once every partition index is attached.
Exercise
A teammate wants CREATE UNIQUE INDEX ON readings (device_id) on a table partitioned by recorded_at. Why does PostgreSQL reject it, and what is the smallest change that makes it valid?
Show answer
Each partition index is local, so PostgreSQL cannot check uniqueness across partitions unless equal keys are guaranteed to be in the same partition. That guarantee exists only when the index includes the partition key. Change the index to (device_id, recorded_at). This makes the pair unique, not device_id alone.