Question: What are PostgreSQL partitioning strategies?


PostgreSQL supports table partitioning, which is an effective way to manage large tables by splitting them into smaller, more manageable pieces called partitions. Partitioning can greatly improve performance for certain types of queries and is also useful for purging old data. There are several partitioning strategies in PostgreSQL, each suitable for different scenarios:

1. Range Partitioning

This strategy involves dividing a table into partitions based on ranges of values of a partition key. It is often used with date or time columns.

CREATE TABLE measurement ( city_id int not null, logdate date not null, peaktemp int, unitsales int ) PARTITION BY RANGE (logdate); CREATE TABLE measurement_y2020 PARTITION OF measurement FOR VALUES FROM ('2020-01-01') TO ('2021-01-01'); CREATE TABLE measurement_y2021 PARTITION OF measurement FOR VALUES FROM ('2021-01-01') TO ('2022-01-01');

2. List Partitioning

List partitioning allows you to map rows to partitions based on the specific values of a partition key.

CREATE TABLE customer ( id int not null, country varchar not null, name text ) PARTITION BY LIST (country); CREATE TABLE customer_usa PARTITION OF customer FOR VALUES IN ('USA'); CREATE TABLE customer_canada PARTITION OF customer FOR VALUES IN ('Canada');

3. Hash Partitioning

Hash partitioning assigns rows to partitions based on a hash value of the partition key. This strategy aims to evenly distribute the rows across partitions.

CREATE TABLE product ( id int not null, name text, price numeric ) PARTITION BY HASH (id); CREATE TABLE product_part1 PARTITION OF product FOR VALUES WITH (MODULUS 4, REMAINDER 0); CREATE TABLE product_part2 PARTITION OF product FOR VALUES WITH (MODULUS 4, REMAINDER 1);

Choosing a Partitioning Strategy

  • Range partitioning is ideal for time-series data where queries often filter by date ranges.
  • List partitioning works well when you frequently query subsets of data based on discrete values, such as country codes or status codes.
  • Hash partitioning is best suited for distributing data uniformly across partitions, especially when no logical range or list distribution exists.


  • Partitioning requires planning and understanding of your data characteristics and access patterns.
  • While partitioning can significantly improve performance for certain queries, it may add complexity to your database schema and operational processes.
  • Not all features and optimizations are available for partitioned tables, such as foreign keys referencing partitioned tables.

Using partitions effectively in PostgreSQL requires careful consideration of your use case, but when implemented correctly, it can offer significant performance improvements and ease of maintenance.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.