Partitioned Tables Stack Local Indexes

Create a partitioned index and per-partition partial indexes, then confirm each child carries its own index entry in pg_indexes.

SqlData Integrity

Code

snippet
-- 按月分区:父表上的分区索引会在每个子分区生成本地索引
CREATE TABLE events_2026 (
    event_id bigint GENERATED ALWAYS AS IDENTITY,
    occurred_at timestamptz NOT NULL,
    severity text NOT NULL,
    payload jsonb
) PARTITION BY RANGE (occurred_at);

CREATE TABLE events_2026_q1
    PARTITION OF events_2026
    FOR VALUES FROM ('2026-01-01') TO ('2026-04-01');
CREATE TABLE events_2026_q2
    PARTITION OF events_2026
    FOR VALUES FROM ('2026-04-01') TO ('2026-07-01');

-- 父表级分区索引:每个子分区自动获得同名后缀的本地索引
CREATE INDEX ON events_2026 (occurred_at);

-- 本地部分索引:只索引 error 级事件的 jsonb 路径,裁剪索引体积
CREATE INDEX ON events_2026 (payload ->> 'trace_id')
    WHERE severity = 'error';

-- 验证:父表一条索引声明,pg_indexes 里出现按分区展开的多条
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename LIKE 'events_2026%'
ORDER BY tablename, indexname;

What the code does

What runs here is SELECT / CREATE — pg_indexes are the objects involved.

Breaking the statements into their working parts: a WHERE clause constraining which rows the statement operates on; an ORDER BY that fixes the output order; NOT NULL qualifiers on the required columns; a GENERATED ALWAYS/GENERATED BY clause that computes the column value.

Using it

The pg_indexes table is the working surface here.

Key statements

CREATE TABLE events_2026 (
    event_id bigint GENERATED ALWAYS AS IDENTITY,
    occurred_at timestamptz NOT NULL,
    severity text NOT NULL,
    payload jsonb
) PARTITION BY RANGE (occurred_at);
CREATE TABLE events_2026_q1
    PARTITION OF events_2026
    FOR VALUES FROM ('2026-01-01') TO ('2026-04-01');
CREATE TABLE events_2026_q2
    PARTITION OF events_2026
    FOR VALUES FROM ('2026-04-01') TO ('2026-07-01');

Interactive Tools for This Topic

FAQ

Which tables does the SQL touch?
`pg_indexes` — referenced directly, no join needed.