npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@piyushmbm45/pg-lens

v0.1.2

Published

A CLI + API toolkit to analyze and optimize PostgreSQL performance

Downloads

281

Readme

pg-lens

A CLI + API toolkit to analyze and optimize PostgreSQL performance.

Find slow queries, missing indexes, table bloat, and unused indexes — with actionable fix suggestions.


Features

| Command | What it does | | ----------------- | ----------------------------------------------------------- | | slow-queries | Top N slowest queries from pg_stat_statements | | missing-indexes | Tables with high sequential scans pg_stat_user_indexes | | bloat | Table and index bloat analysis (Week 2) | | unused-indexes | Indexes that are never used (Week 2) | | explain | Run and parse EXPLAIN ANALYZE with suggestions (Week 3) | | benchmark | Before/after query performance comparison (Week 5) |


Installation

npm install -g pg-lens

Or run directly with npx:

npx pg-lens slow-queries --host localhost --db mydb --user postgres

Quick Start

1. Set up environment variables:

cp .env.example .env
# Edit .env with your database credentials

2. Run your first analysis:

# Find the 10 slowest queries
pg-lens slow-queries

# Find top 20, minimum 5 calls, output as JSON
pg-lens slow-queries --limit 20 --min-calls 5 --format json

# Sort by total time (most expensive overall)
pg-lens slow-queries --sort-total

# Export to CSV
pg-lens slow-queries --format csv > slow_queries.csv

3. Connect with flags instead of .env:

pg-lens slow-queries \
  --host localhost \
  --port 5432 \
  --db mydb \
  --user postgres \
  --password secret

Development Setup

git clone https://github.com/yourusername/pg-lens
cd pg-lens
npm install

# Start a test PostgreSQL database (with sample data)
docker-compose up -d

# Copy env and point to test DB
cp .env.example .env
# PG_DATABASE=pg_lens_test, PG_USER=pguser, PG_PASSWORD=pgpassword

# Run CLI in dev mode
npm run dev -- slow-queries
npm run dev -- slow-queries --limit 5 --format json

Requirements

  • Node.js 18+
  • PostgreSQL 12+
  • pg_stat_statements extension enabled

Enabling pg_stat_statements:

-- Run as superuser
CREATE EXTENSION pg_stat_statements;

Add to postgresql.conf:

shared_preload_libraries = 'pg_stat_statements'

Then restart PostgreSQL.


Output Example

  ┌─────────────────────────────┐
  │       pg-lens v0.1.0     │
  │  PostgreSQL Performance CLI  │
  └─────────────────────────────┘

  ✔ Connected to PostgreSQL

  Top 10 Slowest Queries (avg ms)
  ────────────────────────────────
┌──────────────────────────┬───────┬─────────────┬──────────────┬───────────────┐
│ query                    │ calls │ avg_time_ms │ total_tim... │ cache_hit_... │
├──────────────────────────┼───────┼─────────────┼──────────────┼───────────────┤
│ SELECT * FROM trans...   │  1240 │      245.30 │   304,172.00 │         72.3% │
│ SELECT u.*, t.amount...  │   890 │      187.50 │   166,875.00 │         95.1% │
└──────────────────────────┴───────┴─────────────┴──────────────┴───────────────┘

  Tips:
  • Low cache_hit_ratio (<90%) = add more shared_buffers or check index usage
  • High avg_time_ms with low calls = optimize the query itself
  • Run  pg-lens explain "<query>"  for a detailed query plan

Roadmap

  • [x] slow-queries — pg_stat_statements analysis
  • [ ] missing-indexes — sequential scan detection
  • [ ] bloat — table and index bloat
  • [ ] unused-indexes — dead index finder
  • [ ] explain — EXPLAIN ANALYZE parser with suggestions
  • [ ] benchmark — before/after query comparison
  • [ ] REST API mode
  • [ ] HTML report export

Author

Piyush Jainlinkedin.com/in/piyush-jain-mbm

Built from real-world experience optimizing PostgreSQL systems handling 500K+ daily transactions and 24M+ record datasets.


License

MIT