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
- Agnostic Performance Auditing: Intercepts raw SQL queries, recursively parses the native PostgreSQL execution tree, and catches performance anomalies (Sequential Scans) with exact millisecond runtimes.
- Safe Janitor Mode (Interactive CLI): Scans PostgreSQL system catalogs (
pg_stat_user_indexes) to discover dead, unused indexes that are slowing down yourINSERT/UPDATEmutations. It allows you to drop them interactively usingDROP INDEX CONCURRENTLYwithout locking tables or risking production application downtime. - 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pg_idx_manager-0.1.0.tar.gz.
File metadata
- Download URL: pg_idx_manager-0.1.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77e8cbebb45280ef1d634122f3e1be70844d561f977080e7e7083bc0083efe08
|
|
| MD5 |
09babb2a432b24d70b09b73283e35ceb
|
|
| BLAKE2b-256 |
a58b3830cfce182217d28221a5cf7e400d424c4a47e6c92b93c92cfe6536e140
|
File details
Details for the file pg_idx_manager-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pg_idx_manager-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6525994a8e8764b07dd5f3ee2e8a68eb18911540c3b3df8de1d09bdd10dfeb63
|
|
| MD5 |
2c386c999de4de00f0230c08f6ed8b96
|
|
| BLAKE2b-256 |
62493550a11cdf102edb263133b859959fa187c7031901a4384526d512f49732
|