PostgreSQLQuestion 10 of 11
A huge log table — how do you partition it?
Answer out loud or in your head first, then compare.
Answer
I choose the key to match the queries and retention. For logs, a time range is the natural candidate; region can be a second level if reads and data distribution justify it.
SQL
CREATE TABLE logs (
created_at timestamptz NOT NULL,
region text,
message text
) PARTITION BY RANGE (created_at);
CREATE TABLE logs_2026_01 PARTITION OF logs
FOR VALUES FROM ('2026-01-01 00:00:00+00')
TO ('2026-02-01 00:00:00+00');I check: whether a time filter lets the planner skip partitions, and how we create future partitions and drop old ones.
Pitfall: partitioning within one database is not sharding across many servers. Indexes and the number of partitions still need design. The example has no partitions for other months.
How did it go? Be honest — “Not yet” puts the question on your review list.