Table Inheritance
Long before PostgreSQL had declarative partitioning, it already had a distinctly PostgreSQL feature called table inheritance: a child table can inherit the columns of a parent table, and querying the parent automatically includes rows from all of its children. It's modeled loosely on object-oriented inheritance, applied to table structure.
Basic Syntax
table-inheritance.sql
CREATE TABLE vehicles (
id SERIAL PRIMARY KEY,
make TEXT NOT NULL,
model TEXT NOT NULL
);
-- 'cars' inherits every column from 'vehicles', plus its own extra columns.
CREATE TABLE cars (
doors INT NOT NULL
) INHERITS (vehicles);
CREATE TABLE motorcycles (
has_sidecar BOOLEAN NOT NULL DEFAULT false
) INHERITS (vehicles);
INSERT INTO cars (make, model, doors) VALUES ('Toyota', 'Corolla', 4);
INSERT INTO motorcycles (make, model, has_sidecar) VALUES ('Honda', 'CB500', false);
-- Querying the parent returns rows from ALL children by default.
SELECT * FROM vehicles;
-- returns both the Corolla and the CB500 rows
-- ONLY restricts the query to the parent table itself.
SELECT * FROM ONLY vehicles;Table inheritance itself is still a real, supported feature — it just isn't the tool of choice for partitioning anymore. It occasionally shows up for genuine "is-a" type hierarchies: modeling a family of related entities that share a common set of columns but each have their own additional attributes, where you still want to query them all together through the parent.