Sub topic 8
How PostgreSQL Routes Inserts and Updates
This chapter assumes you know basic SQL, PostgreSQL table definitions, constraints, and the idea of a query plan.
The partition key controls routing
A partitioned table is one logical table with several physical child tables. The partition key is the column, or columns, PostgreSQL examines to choose the child table.
For a range-partitioned table, PostgreSQL compares the inserted value with each partition's lower-inclusive and upper-exclusive bounds:
CREATE TABLE orders (
order_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, order_date, amount)
VALUES (1, DATE '2026-01-15', 29.95);The insert targets orders, but PostgreSQL stores the row in orders_2026_01. The application can continue using the parent table without naming the physical partition.
A row whose key equals 2026-02-01 belongs to orders_2026_02, not orders_2026_01. The first partition ends immediately before that date.
What happens when no partition matches?
Without a matching partition, PostgreSQL rejects the row:
ERROR: no partition of relation "orders" found for row
DETAIL: Partition key of the failing row contains (order_date) = (2026-04-15).A default partition catches values that match no explicit partition:
CREATE TABLE orders_other
PARTITION OF orders DEFAULT;Use a default partition deliberately. It prevents write failures, but it can hide missing future partitions. Before attaching a new partition, rows in the default partition must not violate the new partition's bounds.
> callout warning > Treat a missing partition as an operational event. Create future partitions before they are needed, or monitor the default partition and alert when it receives rows.
Updates can move rows
PostgreSQL routes an ordinary update to the partition that currently stores the row. If an update changes the partition key so that the row belongs elsewhere, PostgreSQL moves the row. Internally, this behaves like deleting the old row and inserting a new row in the destination partition.
UPDATE orders
SET order_date = DATE '2026-02-10'
WHERE order_id = 1;This update moves the row from orders_2026_01 to orders_2026_02. Foreign keys, triggers, indexes, and permissions can make such moves more expensive or operationally important. Applications should also account for the fact that row identity and locking behavior may differ from an update that stays within one partition.
Inspect the destination partition
The system column tableoid identifies the physical table holding a row:
SELECT tableoid::regclass AS physical_partition,
order_id,
order_date
FROM orders
WHERE order_id = 1;This is useful for tests and operational checks. It verifies that the partition bounds and routing behavior match your design.
Exercise
Which partition receives a row with order_date = DATE '2026-02-01' when the January partition ends at TO ('2026-02-01')?
Show answer
The February partition receives it, because range upper bounds are exclusive.