Introducing Dragonfly Cloud! Learn More

Question: Why is PostgreSQL partition pruning not working?

Answer

Partition pruning is a performance optimization feature in PostgreSQL that helps in improving query performance by allowing the query planner and executor to skip scanning partitions that are not relevant to the query. However, there are instances where partition pruning might not work as expected. Understanding these scenarios can help in diagnosing and fixing related issues.

Reasons Partition Pruning May Not Work

1. Dynamic Partition Pruning Limitations

Dynamic partition pruning happens during query execution time, but its effectiveness can be limited if the query's join conditions or filter criteria do not allow for clear identification of irrelevant partitions early on.

2. Use of Non-Immutable Functions in WHERE Clause

When the WHERE clause involves non-immutable functions, PostgreSQL might not be able to perform partition pruning at plan time since it cannot guarantee the function's return value will remain constant.

SELECT * FROM partitioned_table WHERE to_char(date_column, 'YYYY-MM-DD') = '2024-04-15';

In the above query, to_char is a stable, not immutable, function, possibly affecting pruning.

3. Lack of Statistics or Outdated Statistics

PostgreSQL relies on table statistics for making informed decisions about partition pruning. If the statistics are not up-to-date or missing, it might lead to inefficient pruning decisions.

To update statistics, you can:

ANALYZE partitioned_table;

4. Misconfigured Partitioning Key or Strategy

Incorrect configuration of partition keys or choosing a suboptimal partitioning strategy (e.g., range vs. list partitioning) based on the query patterns can also impact pruning efficiency.

5. The Influence of OR Conditions and Parameterized Queries

Complex OR conditions or parameterized queries, especially those built dynamically without literals, may impede effective partition pruning since the planner may find it challenging to deduce the exact partitions involved.

Ensuring Effective Partition Pruning

  • Use Immutable Functions in Partition Keys: Ensure your partition keys and any functions used on them in queries are immutable.
  • Keep Statistics Updated: Regularly update the table statistics using the ANALYZE command.
  • Simplify Query Conditions: Simplify your query conditions and avoid using non-immutable functions on partition keys within WHERE clauses.
  • Review Partitioning Strategy: Make sure the partitioning strategy aligns with the most common access patterns of your queries.

By understanding these points, you can diagnose why partition pruning might not be working as expected in your PostgreSQL database and take appropriate steps to optimize your partitioned tables for better performance.

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.