Optimizing Queries with PostgreSQL 16: A Deep Dive into Enhanced EXPLAIN Output

Photo by Casey Allen on Unsplash

Optimizing Queries with PostgreSQL 16: A Deep Dive into Enhanced EXPLAIN Output

Understanding PostgreSQL 16's Enhanced EXPLAIN Output for Better Query Optimization


3 min read

The EXPLAIN command in PostgreSQL is a powerful tool for understanding how queries are executed by the PostgreSQL query planner. PostgreSQL 16 brings several enhancements to the EXPLAIN output, making it more informative and useful for developers and database administrators. These enhancements help in better diagnosing performance issues and optimizing queries.

Key Enhancements in EXPLAIN Output in PostgreSQL 16

  1. More Detailed Execution Plans:

    • Increased Verbosity: PostgreSQL 16 includes more detailed information in the execution plans, providing deeper insights into how queries are processed.

    • Additional Metrics: New metrics such as memory usage, cache hits, and disk I/O statistics are included, offering a comprehensive view of query performance.

  2. Adaptive Query Execution Information:

    • Incremental Sort: The EXPLAIN output now includes details about incremental sorting, showing how the planner optimizes sorting operations.

    • Memoize Node: Information about the use of Memoize nodes is included, indicating when and how results are being cached to avoid redundant computations.

  3. Parallel Query Enhancements:

    • Parallel Append: The EXPLAIN output shows details about parallel append operations, helping to understand how data is distributed and processed in parallel.

    • Parallel Workers: Improved visibility into how parallel workers are utilized, including the number of workers and their contribution to the query execution.

  4. Enhanced Cost and Timing Information:

    • Detailed Cost Breakdown: More granular cost estimates are provided, including separate costs for I/O operations, CPU usage, and network latency.

    • Timing Statistics: Enhanced timing statistics, such as planning time and execution time, are included to give a complete picture of query performance.

  5. Improved Formatting and Readability:

    • Clearer Hierarchical Structure: The hierarchical structure of execution plans is made clearer, making it easier to understand the flow of query execution.

    • Formatted Output: Better formatting of the EXPLAIN output for improved readability, especially for complex queries.

Example of Enhanced EXPLAIN Output

Here’s an example demonstrating some of the enhancements in the EXPLAIN output in PostgreSQL 16:

Query Example


Enhanced EXPLAIN Output

Gather Merge  (cost=1000.00..1020.00 rows=10 width=64) (actual time=1.234..2.345 rows=10 loops=1)
  Output: id, name, department_id, salary
  Workers Planned: 2
  Workers Launched: 2
  ->  Sort  (cost=1000.00..1010.00 rows=5 width=64) (actual time=1.230..2.340 rows=10 loops=3)
        Output: id, name, department_id, salary
        Sort Key: employees.salary DESC
        Sort Method: quicksort  Memory: 25kB
        Worker 0:  actual time=1.200..2.300 rows=10 loops=1
        Worker 1:  actual time=1.220..2.320 rows=10 loops=1
        Buffers: shared hit=100 read=50
        ->  Parallel Seq Scan on public.employees  (cost=0.00..900.00 rows=100 width=64) (actual time=0.012..0.912 rows=105 loops=3)
              Output: id, name, department_id, salary
              Filter: (employees.department_id = 1)
              Rows Removed by Filter: 95
              Buffers: shared hit=100 read=50
Planning Time: 0.123 ms
Execution Time: 2.456 ms

Key Components Explained

  • Gather Merge: Indicates a parallel query execution with merging of results from multiple workers.

  • Workers Planned/Launched: Shows the number of parallel workers planned and actually used.

  • Sort Method and Memory: Details about the sorting method used (e.g., quicksort) and the memory consumed for sorting.

  • Buffers: Buffer usage statistics, indicating how many pages were hit in the shared buffer cache and how many were read from disk.

  • Parallel Seq Scan: Details of the parallel sequential scan, including actual rows processed by each worker.

  • Planning and Execution Time: Separate timing statistics for query planning and execution phases.


The enhancements to the EXPLAIN output in PostgreSQL 16 provide deeper insights into query execution, making it easier to diagnose performance issues and optimize queries. By leveraging these detailed execution plans, developers and database administrators can make more informed decisions to improve the efficiency and performance of their PostgreSQL databases.

More PostgreSQL Blogs