Quick revision
The short answer
We create the parent table with PARTITION BY RANGE (created_at), make the primary key include created_at, and then add one partition per month with FOR VALUES FROM ('2026-01-01') TO ('2026-02-01') plus a DEFAULT partition for rows that fit no other. Partition pruning is the planner or executor skipping every partition whose bounds cannot match the WHERE clause, so a query for one week of January reads only the January partition.
What to remember
In this answer, we wrote the DDL for a monthly range-partitioned events table: a parent with PARTITION BY RANGE (created_at), a primary key that includes created_at because unique indexes are per partition, a January 2026 partition whose FROM bound is inclusive and TO bound exclusive, and a default partition for everything else. We saw that partition pruning skips partitions that cannot match the WHERE clause, at plan time for constants and at execution time for parameters, and that it only works when queries filter on the partition key itself.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We create the parent table with PARTITION BY RANGE (created_at), make the primary key include created_at, and then add one partition per month with FOR VALUES FROM ('2026-01-01') TO ('2026-02-01') plus a DEFAULT partition for rows that fit no other. Partition pruning is the planner or executor skipping every partition whose bounds cannot match the WHERE clause, so a query for one week of January reads only the January partition.
Why this question is asked
Partitioning is one of the first tools we reach for when a table such as events grows to hundreds of millions of rows. It makes deleting old data cheap and keeps each piece small enough to vacuum and index comfortably. Interviewers ask for the DDL because the small details, the inclusive and exclusive bounds and the rule about primary keys, are exactly where people make mistakes, and they ask about pruning because it is the reason queries stay fast. In this answer, we will write the DDL step by step, see why the primary key must include created_at, insert rows to see where they land, and then look at how pruning works at plan time and at execution time.
1A Quick Recap of Declarative Partitioning
A partitioned table in PostgreSQL is a parent that holds no rows itself. It only describes how rows are split. The rows live in child tables called partitions, and each partition owns a slice of the values of the partition key, here created_at. When we insert into the parent, PostgreSQL routes each row to the right partition. When we query the parent, PostgreSQL reads the partitions it needs.
With range partitioning, each partition has a lower bound and an upper bound. The lower bound is inclusive and the upper bound is exclusive. That rule is what lets monthly partitions sit next to each other without gaps or overlaps: January runs from midnight on 1 January up to, but not including, midnight on 1 February.
2Writing the DDL
First the parent table. The PARTITION BY clause names the method and the key.
Notice the primary key is (id, created_at), not just id. PostgreSQL enforces a unique constraint on a partitioned table by building a unique index in each partition. Each index can only see its own partition, so the only uniqueness PostgreSQL can guarantee across the whole table is one that includes the partition key: two rows with the same created_at always land in the same partition, where the local index can compare them. If we write PRIMARY KEY (id) alone, CREATE TABLE fails with an error saying the unique constraint must include all partitioning columns.
The default partition catches any row that matches no other partition, for example an event from March before we have created a March partition. Without it, such an insert fails with an error that no partition of relation events was found for the row.
3Seeing Where Rows Land
We can check the routing with tableoid, a hidden column that tells which table a row is physically stored in.
| Partition | Bounds | Holds |
|---|---|---|
| events_2026_01 | from 2026-01-01 (included) to 2026-02-01 (excluded) | every row in January 2026 |
| events_default | DEFAULT | rows that fit no other partition |
The row at exactly 2026-02-01 00:00 went to the default partition, because TO is exclusive and there is no February partition yet. One more detail: because created_at is timestamptz, the bound '2026-01-01' is read in the session's time zone. Teams usually set the bounds in UTC on purpose, for example '2026-01-01 00:00+00', so a partition does not move with a server setting.
4What Partition Pruning Does
Partition pruning means PostgreSQL skips partitions whose bounds prove they cannot hold any matching row. If a query asks for created_at in the second week of January, PostgreSQL compares that range with each partition's bounds and only reads events_2026_01. With 36 monthly partitions, that is one partition read instead of 36.
Pruning happens at two moments. At plan time, when the WHERE clause has constants, the planner removes partitions before the plan is even built, and EXPLAIN does not show them at all. At execution time, when the value is only known later, for example a parameter of a prepared statement using a generic plan or a value coming from the other side of a nested loop join, the executor removes partitions when it starts or while it runs, and EXPLAIN ANALYZE reports a line such as Subplans Removed: 35.
So pruning only helps when the query filters on the partition key in a form the planner can compare with the bounds. Wrapping the column in a function, such as date_trunc('day', created_at) = '2026-01-10', hides the key and turns pruning off for that condition. The setting enable_partition_pruning controls the feature and is on by default.
5Why a Large Default Partition Slows Down New Partitions
When we create a new partition, PostgreSQL must make sure that no existing row in the default partition belongs in it, because that row would then be in the wrong place. So CREATE TABLE ... PARTITION OF, or ATTACH PARTITION, scans the whole default partition while holding a lock on it. If the default has collected 50 million rows because nobody created partitions for a few months, that scan takes minutes and blocks writes into the default partition the whole time.
The fix is to keep the default small: create future partitions ahead of time with a scheduled job or pg_partman, and alert when rows appear in the default. If the default already holds rows for the new range, we must move them out first, because the new partition cannot be created while matching rows remain there.
6Follow-up questions
Why can a large default partition make adding a new partition slow?
Because PostgreSQL scans the default partition to check that none of its rows fall inside the new partition's bounds, and holds a lock on the default while it does. A big default turns a quick DDL statement into a long, blocking scan. Keeping future partitions created in advance keeps the default nearly empty.
Can we still look up an event by id alone?
Yes, but the query cannot be pruned, because id says nothing about created_at, so PostgreSQL checks the index of every partition. With dozens of partitions that is usually fine for single lookups. If id-only lookups are the main access path, we should include created_at in the lookup, for example by carrying it in the API or encoding it in the id.
What happens to an index created on the parent?
CREATE INDEX on the partitioned table creates a matching index on every existing partition and on every partition added later. The parent's index is only a definition; the real indexes live in the partitions.
7Conclusion
In this answer, we wrote the DDL for a monthly range-partitioned events table: a parent with PARTITION BY RANGE (created_at), a primary key that includes created_at because unique indexes are per partition, a January 2026 partition whose FROM bound is inclusive and TO bound exclusive, and a default partition for everything else. We saw that partition pruning skips partitions that cannot match the WHERE clause, at plan time for constants and at execution time for parameters, and that it only works when queries filter on the partition key itself.
Open with
I would create the parent as CREATE TABLE events with id bigint, created_at timestamptz not null and body jsonb, with PRIMARY KEY (id, created_at), and PARTITION BY RANGE (created_at).
Then, if they want more
The primary key has to include created_at because PostgreSQL enforces uniqueness with a local index in each partition, so it can only guarantee uniqueness that includes the partition key. Then CREATE TABLE events_2026_01 PARTITION OF events FOR VALUES FROM '2026-01-01' TO '2026-02-01', and CREATE TABLE events_default PARTITION OF events DEFAULT.
The lower bound is inclusive and the upper bound exclusive, so a row at exactly midnight on 1 February goes to February, or to the default if February does not exist yet, and I would write the bounds in UTC so they do not depend on the session time zone.
Partition pruning is PostgreSQL skipping partitions whose bounds cannot match the WHERE clause. With constants it happens at plan time and the partitions do not even appear in the plan; with parameters, such as a generic prepared statement, it happens at execution time and EXPLAIN ANALYZE shows Subplans Removed. It only works when the query filters on created_at directly.
I would also create future partitions ahead of time, because adding a partition scans the default partition, and a large default makes that slow.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write DDL for an events table partitioned by month on created_at, with a partition for...": https://scrivio-interview-library.netlify.app/databases/postgresql/a2fdb051ab/