Sub topic 18
Limitations and Operational Risks
Assumptions
This chapter assumes that you know basic SQL, PostgreSQL table definitions, indexes, constraints, and the basics of query planning. Partitioning divides one logical table into several physical tables. It can improve maintenance and some queries, but it also adds operational work.
Partitioning does not guarantee faster queries
PostgreSQL can use partition pruning to skip partitions when a query has a usable condition on the partition key. Without that condition, PostgreSQL may inspect every partition. A query such as WHERE customer_id = 42 may scan all date partitions if customer_id is not the partition key.
Pruning also depends on the query shape, prepared statements, data types, and PostgreSQL version. Always inspect the actual plan.
EXPLAIN (COSTS OFF)
SELECT *
FROM orders
WHERE order_date >= DATE '2026-01-10'
AND order_date < DATE '2026-01-15';A partitioned table with many partitions can make planning slower, even when execution scans only a few partitions. Partitioning is usually most useful when it supports a clear access or retention pattern.
More objects mean more maintenance
Each partition is a real table. Indexes defined on the parent create corresponding indexes on the partitions, but each index still consumes storage and requires maintenance. Vacuum, analyze, backups, monitoring, permissions, and schema changes must work correctly across the entire hierarchy.
Creating thousands of small partitions can increase planning time and administrative effort. Choose partition sizes that match your data volume, query patterns, and retention schedule.
Constraints and keys have restrictions
A primary key or unique constraint on a partitioned table must include every column in the partition key. PostgreSQL enforces uniqueness separately in each partition unless the partition key makes cross-partition duplicates impossible.
For example, a globally unique order_id alone may not be allowed when the table is partitioned by order_date. You may need a key such as (order_id, order_date), or you may need to enforce global identity elsewhere.
Foreign keys, triggers, generated columns, and application assumptions also need testing against the partitioned parent and its children.
Writes can fail or move rows
An insert fails when no partition accepts its partition-key value. A default partition prevents that error, but it can hide unexpected values and later block attachment of a correctly bounded partition. An update that changes the partition key moves the row between partitions. That operation can have higher cost and may interact with triggers, foreign keys, and locks.
flowchart TD
A[Application write] --> B{Matching partition?}
B -->|Yes| C[Route row]
B -->|No| D{Default partition?}
D -->|Yes| E[Store in default]
D -->|No| F[Reject statement]
E --> G[Review unexpected values]DDL and lifecycle operations need planning
Attaching a partition may scan the table to verify its bounds and can require strong locks. Add a matching CHECK constraint before attachment to reduce that validation work. Detaching is useful for archiving, but applications and foreign keys may depend on the partition remaining attached.
Before production changes, check lock behavior, replication lag, backup coverage, and rollback steps.
Exercise
What two problems can result from creating too many partitions?
Show answer
Query planning can become slower, and maintenance such as vacuum, analyze, indexing, backups, and monitoring becomes more complex.