Sub topic 2
The Declarative Partitioned-Table Model
This chapter assumes basic SQL, PostgreSQL table definitions, primary keys, indexes, constraints, and simple query plans.
A declarative partitioned table is one logical table whose rows are stored in several physical child tables. The parent table defines the shared structure and the partitioning rule. Each child table, called a partition, stores a subset of the rows.
Applications normally query and modify the parent table. PostgreSQL routes each row to the correct partition. This gives users one table name while allowing administrators to manage smaller physical tables.
The parent table and its partitions
The parent is created with PARTITION BY. This makes it a partitioned table, sometimes called a partitioned parent. It stores the table definition and partitioning metadata. It does not store rows itself.
The following example partitions orders by month:
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');
INSERT INTO orders (order_id, customer_id, order_date, amount)
VALUES (1, 42, DATE '2026-01-15', 19.99);
SELECT tableoid::regclass AS physical_partition, order_id, order_date
FROM orders
WHERE order_id = 1;The INSERT names only orders. PostgreSQL examines order_date, finds the January bounds, and stores the row in orders_2026_01. The SELECT still uses the parent. The tableoid system column reveals the physical table that contains the row.
Partition bounds use an inclusive lower limit and an exclusive upper limit. Therefore, January includes 2026-01-01 but excludes 2026-02-01. This convention lets adjacent partitions meet without overlapping.
How the model fits together
The parent provides a unified interface, while partitions provide separate storage boundaries. This separation supports later operations such as indexing, archiving, detaching, or removing one partition without changing application SQL.
A partitioned table can also have subpartitions. For example, a monthly partition could itself be partitioned by customer region. Each additional level increases administrative work, so use a hierarchy only when it solves a real storage or maintenance problem.
Exercise
Exercise
Where does PostgreSQL store a row inserted into a partitioned parent?
Show answer
It stores the row in the partition whose bounds match the row's partition-key value. The parent supplies the logical table interface and routing rule.