Skip to main content

pgplan

GitHub Release npm PyPI Go Reference ci go version License

A command-line tool for analyzing and comparing PostgreSQL query execution plans. Get optimization insights and track performance regressions without leaving your terminal.

Features

  • Plan Analysis - Run 15+ intelligent rules against a query plan to surface performance issues with actionable fix suggestions
  • Plan Comparison - Semantically diff two plans side-by-side to understand what changed and whether it got better or worse
  • Flexible Input - Accept JSON EXPLAIN output, raw SQL files, stdin, or paste plans interactively
  • Connection Profiles - Save and manage named PostgreSQL connection strings for quick reuse
  • Multiple Output Formats - Human-readable colored terminal output or structured JSON for tooling integration

Installation

PyPI

pip install pgplan

npm

npm i -g pgplan

Go

go install github.com/jacobarthurs/pgplan@latest

Binary

Download the latest release for your platform from the releases page.

Quick Start

# Analyze a query plan from a JSON EXPLAIN output
pgplan analyze plan.json

# Analyze by running a SQL file against a database
pgplan analyze query.sql --db postgres://localhost:5432/mydb

# Compare two plans
pgplan compare before.json after.json

# Interactive mode - paste plans or queries directly into the terminal
pgplan analyze
pgplan compare

Commands

pgplan analyze [file]

Analyzes a single query plan and returns optimization findings sorted by severity.

Arguments:

Argument Description
file Path to a .json (EXPLAIN output) or .sql file. Use - for stdin. Omit for interactive mode.

Flags:

Flag Description
-d, --db PostgreSQL connection string (required for SQL input)
-p, --profile Named connection profile to use
-f, --format Output format: text (default) or json

Example:

pgplan analyze slow-query.sql --profile prod

pgplan compare [file1] [file2]

Compares two query plans and reports on cost, time, row estimate, and buffer differences across every node in the plan tree.

Arguments:

Argument Description
file1 The "before" plan. .json, .sql, - for stdin, or omit for interactive.
file2 The "after" plan. Same input options as file1.

Flags:

Flag Description
-d, --db PostgreSQL connection string (required for SQL input)
-p, --profile Named connection profile to use
-f, --format Output format: text (default) or json
-t, --threshold Percent change threshold for significance (default: 5)

Example:

pgplan compare before.json after.json --threshold 10

pgplan profile <subcommand>

Manages saved PostgreSQL connection profiles stored in ~/.config/pgplan/profiles.yaml.

Subcommand Description
list [--show] List saved profiles. Pass --show to display connection strings.
add <name> <conn_str> Add or update a named profile.
remove <name> Remove a profile.
default <name> Set a profile as the default.
clear-default Clear the default profile.

Example:

pgplan profile add prod postgres://user:pass@host:5432/mydb
pgplan profile default prod

# Now use it with analyze or compare
pgplan analyze query.sql --profile prod

Analysis Rules

The analyze command applies the following rules to identify performance issues. Each finding includes a severity level and an actionable suggestion.

Severity Rule Description
Critical Sort Spill to Disk Sort operation exceeded work_mem and spilled to disk
Warning Hash Spill to Disk Hash table exceeded work_mem
Warning Temp Block I/O Plan is reading/writing temporary blocks
Warning Seq Scan in Join Sequential scan used inside a join against a smaller set
Warning Seq Scan with Filter Standalone sequential scan filtering a large number of rows
Warning Index Scan Filter Inefficiency Index scan is fetching many rows then discarding most via filter
Warning Bitmap Heap Recheck Lossy bitmap scan rechecking conditions (bitmap exceeded work_mem)
Warning Nested Loop High Loops Nested loop executing 1,000+ iterations
Warning Correlated Subplan Subplan re-executing on every outer row
Warning Worker Launch Mismatch Fewer parallel workers launched than planned
Warning Parallel Overhead Parallel execution is slower than the serial estimate
Warning Large Join Filter Removal Join filter is discarding a large percentage of rows
Warning Excessive Materialization Materialize node looping many times
Info Low Selectivity Index Scan Index scan is returning most of the table
Info Wide Row Output Query is selecting more columns than necessary

Comparison Output

The compare command produces a structured diff of two plans including:

  • Summary - Overall cost, execution time, and buffer changes with directional indicators
  • Node Details - Per-node breakdown of metric changes (cost, rows, loops, buffers, filters, indexes)
  • Verdict - A final assessment such as "faster and cheaper" or "slower but cheaper"

Changes below the significance threshold (default 5%) are filtered out to reduce noise.

Output Formats

Text (default)

Colored terminal output with severity-coded findings and directional change indicators. Designed for quick human review.

JSON

Structured output suitable for piping into other tools, CI systems, or dashboards. Includes all metrics, findings, and comparison deltas.

pgplan analyze plan.json --format json | jq '.findings[] | select(.severity == "critical")'

Configuration

Connection Profiles

Profiles are stored in a YAML configuration file at the platform-appropriate config directory:

  • Linux/macOS: ~/.config/pgplan/profiles.yaml
  • Windows: %APPDATA%\pgplan\profiles.yaml
default: prod
profiles:
  - name: prod
    conn_str: postgres://user:pass@host:5432/production
  - name: dev
    conn_str: postgres://localhost:5432/development

Use --profile <name> with any command, or set a default to skip the flag entirely. The --db and --profile flags are mutually exclusive.

Contributing

Contributions are welcome! To get started:

  1. Fork the repository
  2. Create a feature branch (git checkout -b my-new-feature)
  3. Open a pull request

CI will automatically run tests and linting on your PR.

License

This project is licensed under the MIT License.

Metadata

Release files for pgplan 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for pgplan 1.2.0
File
pgplan-1.2.0-py3-none-win_arm64.whl Python 3 none Windows ARM64 Details
pgplan-1.2.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details
pgplan-1.2.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details
pgplan-1.2.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details
pgplan-1.2.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details
pgplan-1.2.0-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details

Total release size: 58.9 MB

Release files / pgplan-1.2.0-py3-none-win_arm64.whl

Download URL pgplan-1.2.0-py3-none-win_arm64.whl
Size 9.5 MB
Tags Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
c58279eea1f3ecfb5fce68408b832aa7bd8e66a0e3f67a6d11244e88049e6c8b
BLAKE2b-256 checksum
How to use checksums
577d605c82c3e9995fd8d41173d6c7ef5d3075d5874ab11fa871b403136eb444
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via Go-http-client/2.0

Release files / pgplan-1.2.0-py3-none-win_amd64.whl

Download URL pgplan-1.2.0-py3-none-win_amd64.whl
Size 10.3 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
bfbc62b4bae6e88904d87e0ab3166834ee9a73bf675bb0f0614b84e843b05254
BLAKE2b-256 checksum
How to use checksums
0304c1f2b4a4858790bf7817fc2bb8d9adeedb040c35b3e8bfc98cd60d5029ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via Go-http-client/2.0

Release files / pgplan-1.2.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL pgplan-1.2.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 9.9 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
3542b9bd898d661d611473aeccbefca41f4efa20b7aa584d24a7dbd7c30008a2
BLAKE2b-256 checksum
How to use checksums
1828a88ae12c85fde425a4df3ff8e3118fea4fb998f50a807e83a2fd1024f6d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via Go-http-client/2.0

Release files / pgplan-1.2.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL pgplan-1.2.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 9.4 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
f3f52aead4764f3fd5420cf0f3dca51580571ee2ca630fbc5de4ab5423dc7aeb
BLAKE2b-256 checksum
How to use checksums
2b7c3e8d0f42ea713a7525bbe51f861f437eaff01ad432c4e0e3f22153d70cf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via Go-http-client/2.0

Release files / pgplan-1.2.0-py3-none-macosx_11_0_arm64.whl

Download URL pgplan-1.2.0-py3-none-macosx_11_0_arm64.whl
Size 9.6 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0ad569d50ce769dc9da99ddbaebe0798f5264b42744fe423a9788b78753e9b71
BLAKE2b-256 checksum
How to use checksums
c4fd5447bd3f102bfd97e2086631fbf36c8d0263a408add93df0af321aad54de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via Go-http-client/2.0

Release files / pgplan-1.2.0-py3-none-macosx_10_12_x86_64.whl

Download URL pgplan-1.2.0-py3-none-macosx_10_12_x86_64.whl
Size 10.2 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
07324e33adaccd246f404f0c554edc0cd15c47666d617a67ad82f4a2ba472a22
BLAKE2b-256 checksum
How to use checksums
c49a04d3f0aed48a059a836e73756efa5dd3d8ac5d20f3bbabd954ec13f02b0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via Go-http-client/2.0

Release history Release notifications | RSS feed

This release

1.2.0 This release

6 release files

1.1.1

6 release files

1.1.0

6 release files

1.0.0

5 release files

0.5.0

5 release files

0.4.2

5 release files

0.4.1

5 release files

0.4.0

5 release files

0.3.0

5 release files

0.0.2

5 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page