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.1.tar.gz (6.8 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.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pg_idx_manager-0.1.1.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

Hashes for pg_idx_manager-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e8544202a8b11b5a0360cbb3af6292bdde454d2fec0668c7ff76a0fb5fe259e5
MD5 f7dd698470993b661368eb5da32bd1f9
BLAKE2b-256 57983eb1f44bebc2969eb7a5d5d715b39add6100589954e26fbd681da07e0f46

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pg_idx_manager-0.1.1-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

Hashes for pg_idx_manager-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7fcb7c3e1888ada2c8fea825efc56b4773c82ba4b09558fd6d3dd3e5786fec65
MD5 fd87509e11d34af05c69437c5601ed7d
BLAKE2b-256 852789fd5fb363f4891dc8cea2f5715b764160052abd92ad782ae6c99dc29a13

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