Skip to Content
Zurück zu: Redis as More Than a Cache: Queues, Sessions, and Pub/Sub
Databases & Search 8 min. read

PostgreSQL Partitioning: Querying Billions of Rows Efficiently

When tables grow to billions of rows, even indexed queries become slow. Table partitioning is the solution, but only when implemented correctly.

devRocks Engineering · 26. February 2026 · Aktualisiert: 31. March 2026
PostgreSQL Partitioning Performance Datenbank
PostgreSQL Partitioning: Querying Billions of Rows Efficiently

When Partitioning Becomes Necessary

Beyond a certain table size, even B-Tree indexes become inefficient. VACUUM takes hours, backups become problematic, and queries hit unnecessarily large amounts of data. Partitioning divides a large table into smaller, manageable parts.

Partitioning Strategies

  • Range Partitioning: Ideal for time-series data. Partition by month or quarter, queries that target a time range scan only the relevant partitions.
  • List Partitioning: For categorical data such as region, tenant, or status. Each partition contains rows with specific values.
  • Hash Partitioning: Even distribution across a fixed number of partitions, well suited for write-heavy workloads without a natural partition key.

Best Practices

  • Partition Key in Query: Queries must include the partition key in the WHERE clause for partition pruning to take effect.
  • Not Too Many Partitions: Hundreds of partitions slow down the query planner. 12-36 active partitions is a good guideline.
  • Automatic Partition Creation: Use pg_partman for automatic creation and management of time-series partitions.
  • Indexes per Partition: Each partition has its own indexes, this keeps index maintenance fast.

Results

In an e-commerce project, we split a 2.4 billion row order table using range partitioning by month. The average query time dropped from 12 seconds to 80 milliseconds, a factor of 150x.

Questions About This Topic?

We are happy to advise you on the technologies and solutions described in this article.

Get in Touch

Seit über 25 Jahren realisieren wir Engineering-Projekte für Mittelstand und Enterprise.

Weitere Artikel aus „Databases & Search“

Frequently Asked Questions

PostgreSQL Partitioning divides large tables into smaller, more manageable pieces. It becomes necessary when tables grow so large that indexes become inefficient, VACUUM takes a long time, or backups become problematic.
There are three main strategies: Range Partitioning for time-series data, List Partitioning for categorical data, and Hash Partitioning for even distribution in write-heavy workloads. The choice of strategy depends on the nature of the data and the specific query requirements.
Key best practices include using the partition key in the WHERE clause, keeping the number of partitions within 12-36, and utilizing automatic partition creation with tools like pg_partman. Additionally, each partition should have its own indexes to maintain quick accessibility.
Partitioning allows only relevant data partitions to be considered in queries, significantly improving performance. In one example, the average query time was reduced from 12 seconds to 80 milliseconds, which represents an improvement factor of 150.
Creating too many partitions can slow down the query planner, negatively affecting query performance. It is advisable to keep the number of active partitions within a reasonable range to avoid efficiency losses.

Didn't find an answer?

Get in touch