How to Implement Table Inheritance in PostgreSQL
Use PostgreSQL table inheritance and partitioning to model shared columns and query across child tables, with the constraints that apply.
On this page
Introduction
PostgreSQL supports table inheritance, where a child table inherits the columns of a parent and its rows are visible through the parent, and native declarative partitioning, which is built on a related mechanism. Both let you model data that shares a shape but differs in detail, and both are easy to misuse because the constraints that a normal table gives you do not automatically extend to children. This tutorial covers inheritance for shared structure, the constraint caveats, and how declarative partitioning differs.
Inheritance Versus Partitioning
Table inheritance creates a parent with common columns and children that add their own. A query on the parent includes the children’s rows unless you say otherwise. It is useful for modelling a shared supertype, such as a generic event with typed subtypes, where each subtype has extra columns.
Declarative partitioning is a different feature that also produces child tables, but it is designed to split one logical table into pieces by a key, for manageability and performance, and the children together form one logical table with a shared schema. It is the right tool for large time-series or log tables; inheritance is for a type hierarchy. Knowing which you want avoids the confusion of their overlapping syntax.
# two features, similar syntax, different intent
inheritance: parent is a supertype; children ADD columns for subtypes
queries over the parent include children by default
partitioning: parent is ONE logical table; children are pieces by a key
queries route to the right partition; one schema for all
# large time-series table -> partitioning
# payment/invoice/refund sharing a base -> inheritance
Declaring an Inheritance Hierarchy
The parent declares the shared columns, and each child declares INHERITS (parent) and adds its subtype-specific columns. A query on the parent returns rows from the parent and every descendant, with the parent’s columns; extra child columns are not visible unless you query the child table directly.
To include only the parent’s own rows, add ONLY to the query. This distinction matters because a query meant for one table silently includes children, which can be surprising and can make a query slower than the parent table alone. Use ONLY when you mean only, and expect children when you query the parent by default.
CREATE TABLE payment (
id bigint PRIMARY KEY,
amount_cents bigint NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE card_payment (
card_last4 char(4) NOT NULL
) INHERITS (payment);
CREATE TABLE refund (
original_id bigint NOT NULL,
reason text
) INHERITS (payment);
-- Rows of card_payment and refund are visible through payment:
SELECT * FROM payment; -- includes children
SELECT * FROM ONLY payment; -- parent's own rows only
The Constraint Caveats
Inheritance does not propagate most constraints to children. A NOT NULL or CHECK declared on the parent is inherited as part of the column definition, but a UNIQUE or PRIMARY KEY on the parent does not enforce uniqueness across children: each child has its own index, so a duplicate id in two different children is possible.
Foreign keys referencing the parent do not cover the children by default either, and a foreign key from a child to the parent works only if the referenced parent exists. These caveats mean a recovered hierarchy has weaker guarantees than a single table, and you must add the missing constraints where they matter, or prefer partitioning when one logical table with one set of constraints is what you need.
-- A UNIQUE on the parent does NOT span children.
-- To enforce global uniqueness across children, add a unique index
-- on a shared-identity column in the parent AND ensure children do not
-- reuse ids, or use a single partitioned table instead.
-- Verify constraints actually exist on each child:
SELECT conrelid::regclass AS table_name, conname, contype
FROM pg_constraint
WHERE conrelid IN ('payment'::regclass, 'card_payment'::regclass, 'refund'::regclass);
Querying and Routing
A query on the parent scans the parent and all descendants, which can be surprising both semantically and for performance. With proper check constraints or table constraints, the planner can skip children that cannot contain matching rows; without them it scans them all. Declare the constraints that describe each child’s membership so the planner can prune.
Inheritance is not the same as partitioning for pruning: the planner can only skip a child when it can prove from constraints that no rows match. If you rely on the inheritance for query performance, add the discriminating constraint on each child and confirm with EXPLAIN that the unwanted children are not scanned.
-- Constraint enabling the planner to skip a child it can disprove.
ALTER TABLE card_payment ADD CONSTRAINT card_payment_is_card
CHECK (true); -- children usually need a real discriminator, see partitioning
EXPLAIN (COSTS OFF) SELECT * FROM payment WHERE created_at > now() - interval '1 day';
-- with a discriminating constraint, EXPLAIN shows irrelevant children skipped
Declarative Partitioning
When the goal is to split one logical table into manageable pieces, use declarative partitioning: CREATE TABLE … PARTITION BY RANGE (created_at), then create partitions for each range. All partitions share one schema, the parent enforces constraints across them, and the planner prunes partitions by the partition key, which is what makes large time-series tables fast.
Partitioning gives you what inheritance does not: a single logical table with one set of constraints, automated pruning, and the ability to attach and detach partitions cheaply for retention. For retention, detaching an old partition is far faster than deleting rows, which is the main reason large log tables are partitioned.
CREATE TABLE events (
id bigint NOT NULL,
created_at timestamptz NOT NULL,
payload jsonb
) PARTITION BY RANGE (created_at);
CREATE TABLE events_2026_10 PARTITION OF events
FOR VALUES FROM ('2026-10-01') TO ('2026-11-01');
-- prune: a query bounded by created_at scans only matching partitions
EXPLAIN SELECT * FROM events WHERE created_at >= '2026-10-23';
-- retention: detach and drop an old partition instantly
ALTER TABLE events DETACH PARTITION events_2026_10;
Choosing Between Them
Choose declarative partitioning when you have one logical table that is large and you want to split it for performance or retention, and all pieces share the same schema. It gives you the constraints and pruning you expect from a single table.
Choose inheritance only when you genuinely have a type hierarchy: a common supertype with distinct subtypes that add different columns and where querying the supertype across all subtypes is natural. Even then, consider a single table with a type column and nullable subtype columns, which keeps one set of constraints and is often simpler. Reach for inheritance last.
# decision
one logical table, large, split by key -> declarative partitioning
shared columns + distinct subtypes -> consider inheritance
. but first consider: one table + type column + nullable subtype cols
. it keeps a single set of constraints and simpler queries
# most "inheritance" needs are met by a type column; use inheritance rarely
Common Pitfalls and Conclusion
Do not expect a PRIMARY KEY or UNIQUE on the parent to span children; it does not. Do not be surprised that a query on the parent includes descendants, and use ONLY when you mean it. Do not use inheritance for performance where partitioning is what gives pruning and single-table constraints. And do not forget that DDL on the parent can affect children in ways that surprise you.
PostgreSQL inheritance models a supertype with subtypes that add columns, queryable through the parent, at the cost of weaker constraints across children. Declarative partitioning splits one large logical table into pieces with a shared schema and real pruning. Know which intent you have: for large tables use partitioning, for a genuine type hierarchy use inheritance, and for most cases a type column on a single table is simpler than either.
Sources: PostgreSQL: Table inheritance, PostgreSQL: Table partitioning, PostgreSQL: Constraints.
FAQ
- Does a PRIMARY KEY on the parent enforce uniqueness across children?
- No. A primary key or unique constraint on an inherited parent applies per table, not across the hierarchy, so a duplicate id can exist in two different children. Use a single partitioned table if you need one logical table with a single constraint.
- Inheritance or declarative partitioning?
- Partitioning when you have one large logical table to split by a key with a shared schema and want pruning and single-table constraints. Inheritance only for a real type hierarchy with subtypes adding columns; often a type column on one table is simpler.
Report a problem with this articleOpens a pre-filled issue on GitHub.