Partitioning
About
Partioning is splitting one table into, multiple smaller, logically divided and more manageable tables.
Postgres' query optimizer is able to exclude partitions that, for sure, won't be affected by the data that it is trying to be read or written. (Postgres automatically routes reads and writes to the right partition)
Postgres does not:
Create the partions automatically, you must manually create and define the partion's boundaries.
Manage old partitions.
Indexes on new partitions, you must create indexes on each new partition. (There are no global indexes, indexes are always per-partition)
Constraints on partitions.
Be very careful with multi-level partitioning because the number of partition files can grow very quickly.
You can use pg_partman extension for some more automatic management of partitions.
Types of Partitions
range
The table is partitioned into ranges defined by a key column or set of columns, with no overlap between the ranges of values assigned to different partitions.
Creating partition table
list
The table is partitioned by explicitly listing which key values appear in each partition.
Creating partition table
hash
The table is partitioned by specifying a modulus and a remainder for each partition.
Each partition will hold rows for the remainer, which is the hash value of the partition key divided by the specified modulus.
Creating partition table
default
Default partition tables are the ones that will receive all the data that does not fit in any of the created partitions.
Accessing Data
Almost always you interact with the master (aka parent or partitioned) table, not the individual partitions.
If you try to insert a row that doesn't fit any partition you will get a:
ERROR: no partition of relation "table_name" found for row
Unless you have setup a default partition table.
SELECT
✅ Yes
Postgres uses partition pruning to scan only relevant partitions.
INSERT
✅ Yes
Data is routed to the correct partition automatically.
UPDATE
✅ Yes
Postgres finds which partition the row is in and moves it if necessary.
DELETE
✅ Yes
Postgres finds and deletes from the correct partition(s).
COPY
✅ Yes
Same as insert; routed correctly.
ANALYZE, VACUUM
❌ Run per partition
Not automatic on children unless specifically enabled.
Maintainance
detach
Unlink a partition from the partition group. (It does not delete it)
attach
You can atach an unlinked partition again with:
When you reatach yo specifiy the partition type values again RANGE, LIST or HASH.
To change a partition table range value after created you have to detach and reatach it.
Last updated