Sub topic 2
Partitioned tables, partitions and PARTITION OF
This chapter assumes you can write CREATE TABLE, INSERT and SELECT and that you know what a primary key is. It also assumes you can read a basic EXPLAIN plan. Here you will learn how PostgreSQL models a partitioned table, how to create partitions with PARTITION OF, and how to inspect the result. The next chapters cover each partitioning method in detail.
Two kinds of tables
A partitioned table has two layers.
- The partitioned table (also called the parent) is the table your queries name. It defines the columns and the partition key. It stores no rows.
- The partitions are ordinary tables that hold the rows. Each partition owns one slice of the data, defined by a bound.
Applications read and write the parent. PostgreSQL routes each inserted row to the matching partition, and a query on the parent reads whichever partitions it needs.
Creating the parent with PARTITION BY
The PARTITION BY clause turns a new table into a partitioned parent. It names the method (RANGE, LIST or HASH) and the partition key.
CREATE TABLE measurements (
device_id int NOT NULL,
recorded_at timestamptz NOT NULL,
reading numeric
) PARTITION BY RANGE (recorded_at);The key is one or more columns, or an expression such as PARTITION BY RANGE (date_trunc('day', recorded_at)). List partitioning accepts only one key column. Range and hash partitioning accept several.
At this point the table exists but cannot accept any rows, because no partition covers any value. Inserting fails with this message, followed by a DETAIL line that shows the key value of the failing row:
ERROR: no partition of relation "measurements" found for rowCreating partitions with PARTITION OF
CREATE TABLE ... PARTITION OF parent creates a new table that is already attached to the parent. The FOR VALUES clause gives its bound. You do not repeat the column list, because the partition copies it from the parent.
CREATE TABLE measurements_2026_01 PARTITION OF measurements
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE measurements_2026_02 PARTITION OF measurements
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');In a range bound, FROM is inclusive and TO is exclusive. A row stamped 2026-02-01 00:00:00 therefore goes to measurements_2026_02, not to January. Because each upper bound is the next lower bound, the two partitions leave no gap and do not overlap.
Insert a few rows through the parent:
INSERT INTO measurements (device_id, recorded_at, reading) VALUES
(1, '2026-01-15 10:00+00', 20.1),
(1, '2026-02-15 10:00+00', 21.7),
(2, '2026-02-28 23:59+00', 19.4);What a partition can and cannot change
A partition inherits the parent's columns, their data types and their NOT NULL constraints. Beyond that it has some freedom.
| Aspect | On a partition |
|---|---|
| Column names and types | Must match the parent. You cannot add or drop columns on one partition only. |
| Column defaults | Can differ from the parent. |
| Extra `CHECK` constraints | Allowed in addition to the parent's. |
| Tablespace and storage parameters | Can be set per partition, for example to put old data on cheaper storage. |
| Non-unique indexes | Can be added to a single partition. |
Because partitions are real tables, you can query one directly by name. Doing so skips routing, so the table's bound is still enforced as a constraint:
INSERT INTO measurements_2026_01 (device_id, recorded_at, reading)
VALUES (1, '2026-03-01', 5);ERROR: new row for relation "measurements_2026_01" violates partition constraintThis implicit partition constraint is what guarantees that a partition holds only rows inside its bound.
Reading data back
A query on the parent returns the rows of every partition that it might need to read:
SELECT device_id, recorded_at, reading FROM measurements ORDER BY recorded_at;To see which partition stores each row, use the system column tableoid. Casting it to regclass turns the numeric ID into a table name.
SELECT tableoid::regclass AS partition, count(*)
FROM measurements
GROUP BY 1
ORDER BY 1;With the three rows above, this returns two rows in measurements_2026_02 and one in measurements_2026_01.
The ONLY keyword restricts a statement to the parent itself. A SELECT with ONLY on a partitioned table returns zero rows, because the parent stores nothing.
SELECT count(*) FROM ONLY measurements;Inspecting the structure
In psql, \d+ measurements prints the partition key and lists each partition with its bound. You can also query the catalog. The function pg_partition_tree() returns one row for every table in a partition hierarchy, including the parent:
SELECT relid::regclass AS table_name,
parentrelid::regclass AS parent,
isleaf,
level
FROM pg_partition_tree('measurements');level is 0 for the parent. isleaf is true for tables that store rows and false for tables that are themselves partitioned. A partitioned parent shows relkind = 'p' in pg_class, while an ordinary table shows r:
SELECT relname, relkind
FROM pg_class
WHERE relname LIKE 'measurements%'
ORDER BY relname;How rows are routed and moved
Routing happens on every insert. PostgreSQL evaluates the partition key for the new row, finds the partition whose bound contains it, and writes the row there. If none contains it, the insert fails, unless the table has a default partition. Default partitions get their own chapter.
An UPDATE that changes a key column can move the row to another partition. PostgreSQL performs this as a delete from the old partition and an insert into the new one:
UPDATE measurements
SET recorded_at = '2026-01-20 10:00+00'
WHERE device_id = 2 AND recorded_at = '2026-02-28 23:59+00';The row now lives in measurements_2026_01. This works, but it costs more than an in-place update. Choose a key that rows do not change after they are written.
Parent and partition are not interchangeable
Several rules follow from the two-layer design.
- You cannot insert rows into the parent's own storage, because it has none.
- Table-level settings on the parent do not all apply to partitions. Storage parameters such as
fillfactorare not inherited, so set them on the partitions. - Dropping the parent drops every partition.
DROP TABLE measurements;removes the parent and all of its partitions. A partition can be dropped alone withDROP TABLE measurements_2026_01;. - Some commands act on the parent and recurse. Adding a column to the parent adds it to every partition. You cannot add a column to a single partition.
Summary of the model
The parent defines shape and key, and each partition owns a disjoint slice of the key space. Because the bounds do not overlap, every row belongs to exactly one partition, and the planner can later skip partitions that cannot match a query. Later chapters build on this structure to explain pruning, constraints, indexes, and attaching and detaching partitions.
Exercise
You run CREATE TABLE sales (id bigint, sold_on date, amount numeric) PARTITION BY RANGE (sold_on); and then INSERT INTO sales VALUES (1, '2026-03-10', 99);. The insert fails. Why, and what is the smallest change that makes the insert succeed?
Show answer
The parent sales stores no rows, and no partition has been created, so no partition's bound contains 2026-03-10. PostgreSQL reports no partition of relation "sales" found for row. Create a partition that covers the date, for example CREATE TABLE sales_2026_03 PARTITION OF sales FOR VALUES FROM ('2026-03-01') TO ('2026-04-01');, and run the insert again.
Exercise
A table has partitions p1 FOR VALUES FROM ('2026-01-01') TO ('2026-02-01') and p2 FOR VALUES FROM ('2026-02-01') TO ('2026-03-01'). Which partition receives a row with the value 2026-02-01, and why does p2 not overlap p1?
Show answer
The row goes to p2. In a range bound, FROM is inclusive and TO is exclusive, so 2026-02-01 is outside p1 and inside p2. The two partitions share the value 2026-02-01 as an endpoint but never contain it at the same time, so they do not overlap.
Exercise
Write a query that shows how many rows of measurements are stored in each partition.
Show answer
SELECT tableoid::regclass AS partition, count(*) FROM measurements GROUP BY 1 ORDER BY 1; The system column tableoid identifies the table that holds each row, and ::regclass converts it to a readable name.