Skip to main content

A lightweight, framework-agnostic developer linter and cleanup tool for PostgreSQL indexes.

Project description

PG Index Manager & Janitor 🚀

A lightweight, framework-agnostic Python library designed for developers to audit database queries, surface runtime performance bottlenecks, and safely maintain PostgreSQL indexes.

Unlike heavy Application Performance Monitoring (APM) tools that require invasive database extensions (like HypoPG) or expensive SaaS subscriptions, this library operates entirely inside your application or via a clean CLI using native PostgreSQL capabilities.

The Problem It Solves: "ORM Blindness"

Modern Object-Relational Mappers (ORMs) like Django or SQLAlchemy maximize developer productivity but hide the underlying SQL execution plan. A seemingly innocent line of Python code can silently trigger a Sequential Scan (Full Table Scan) across millions of rows, saturating server CPU and disk I/O.

Since Large Language Models (AI) cannot inspect your production database cardinality, table sizes, or live index catalogs, they cannot reliably predict query performance. This library bridges that gap by running live EXPLAIN ANALYZE inspections directly on the database engine.

Key Features

  1. Agnostic Performance Auditing: Intercepts raw SQL queries, recursively parses the native PostgreSQL execution tree, and catches performance anomalies (Sequential Scans) with exact millisecond runtimes.
  2. Safe Janitor Mode (Interactive CLI): Scans PostgreSQL system catalogs (pg_stat_user_indexes) to discover dead, unused indexes that are slowing down your INSERT/UPDATE mutations. It allows you to drop them interactively using DROP INDEX CONCURRENTLY without locking tables or risking production application downtime.
  3. Zero-Dependency Architecture: Does not require root or superuser privileges on the database server. If you can connect to the database, you can run this library.

Architectural Architecture: Where does it live?

Because the core engine requires only a raw query string and a standard database connection, it is completely independent of your web framework. You can integrate it at the lowest layer of your infrastructure:

  • At the Driver Level (psycopg2): By extending the native database cursor, you can automatically audit 100% of your application queries (Django, FastAPI, Flask, or raw SQL) before they hit the wire.
  • At the ORM Engine Level: Easily hooks into global ORM events (e.g., SQLAlchemy's before_cursor_execute) to catch hidden query costs implicitly across all Services and Repositories with zero business-code modification.

Getting Started & Testing Locally 🧪

This repository includes a fully containerized testing environment to observe performance optimization in real-time.

1. Spin up the isolated test database

docker compose up -d

2. Install the library locally in editable mode

pip install -e .

3. Run the Core Agnostic Test

This script populates the database with 10,000 mock records, executes an unindexed query, catches the Sequential Scan risk, and launches the persistent interactive CLI:

python tests/run_test.py

4. Run the ORM Integration Test

Observe how the library seamlessly intercepts real-time query metrics generated implicitly by SQLAlchemy ORM models:

python tests/test_orm.py

License

MIT License. Free to use and extend.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pg_idx_manager-0.1.3.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pg_idx_manager-0.1.3-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pg_idx_manager-0.1.3.tar.gz.

File metadata

  • Download URL: pg_idx_manager-0.1.3.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for pg_idx_manager-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f9cab6b23c456c021248241e834a7ca1a08235496bfea5dae04a8c76d386efdc
MD5 72b89b0a1fb014a09cac3bdc5aac6ad0
BLAKE2b-256 f69d681bac415b78133a0598fd28d4c6db0232917844b91963160078a4f027f6

See more details on using hashes here.

File details

Details for the file pg_idx_manager-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pg_idx_manager-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for pg_idx_manager-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 01a194df1cd4f682a4d8aa1ba15b5ab5a96bf39ed7c51325b643dbeb86a5b6e4
MD5 e9993e6b49af0a6034d0e35dec3a45a0
BLAKE2b-256 7fcdc47aba94d5074322766e89419326e51a97f3741345527d203f5b3752f6cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page