Skip to main content

DB Snooper

PyPI Python

Agent skill: src/db_snooper/skills/SKILL.md

Spec: spec/profiler.md

DB Snooper generates compact, LLM-ready database context for SQL generation, query debugging, and schema exploration. Profiling alone drives state-of-the-art text-to-SQL accuracy (Automatic Metadata Extraction for Text-to-SQL). Supports SQLite, PostgreSQL, MySQL, MariaDB, and DuckDB. Requires Python ≥ 3.10.

It inspects an existing database and produces a SQL profile (<database>/<schema>.sql): DDL, row counts, sampled rows, and per-column summaries. Use --per-table for one .sql per table.

AI agents and text-to-SQL pipelines can read this context instead of guessing table meanings.

Quick Start

Install with pip:

pip install db-snooper

Or run instantly with uvx (no install needed):

uvx db-snooper profile --db-type mysql --user user --password password --database db --schema sch --port 3306

This creates a profile at db/sch.sql.

What The Outputs Contain

The profile .sql file contains:

  • Metadata with db-snooper version, UTC generation timestamp, SQL dialect, database name, and schema.
  • CREATE TABLE DDL, indexes, and constraints.
  • Total row counts.
  • Deterministic sampled rows for small tables.
  • Latest and random sampled rows for larger tables.
  • Per-column null, non-null, distinct, numeric range, median, top-value, and shape summaries for larger tables.
  • Catalog-derived estimates for very large tables (or metrics that are skipped on medium-large tables) from each engine's internal statistics — PostgreSQL pg_stats, MySQL COLUMN_STATISTICS histograms, and MariaDB mysql.column_stats — emitted with a /(from db stats) marker so they are distinguishable from exact values.
  • Top-level key frequencies for JSON/JSONB columns and min/avg/max element counts for ARRAY columns (when row counts allow).
  • Redacted values for sensitive column names containing password, passwd, pwd, hash, salt, secret, or token.
  • A -- skipped technical tables: line naming migration/framework tables excluded from the profile.
  • Empty tables are skipped by default (no DDL, no rows). A -- Skipped N empty table(s): line names them; use --include-empty-tables to emit their CREATE TABLE.
  • For small tables whose rows are all listed, the CREATE TABLE is omitted — the row data already exposes columns, types, and constraints.

Database Examples

SQLite

db-snooper profile --db-type sqlite --database path/to/app.sqlite

PostgreSQL

Profile

db-snooper profile --db-type postgres --database app_db --schema sch --user readonly_user --host localhost --port 5432 --ask-password

MySQL

db-snooper profile --db-type mysql --database app_db --user readonly_user --host localhost --port 3306 --ask-password

MariaDB

db-snooper profile --db-type mariadb --database app_db --user readonly_user --host localhost --port 3306 --ask-password

DuckDB

db-snooper profile --db-type duckdb --database warehouse.duckdb --schema sch

Environment Variables

Connection values can come from environment variables instead of flags:

DB_SNOOPER_DB_TYPE=sqlite \
DB_SNOOPER_DATABASE=eval-dataset/student_club/student_club.sqlite \
db-snooper profile

Supported variables:

  • DB_SNOOPER_DB_TYPE
  • DB_SNOOPER_DATABASE
  • DB_SNOOPER_DB_HOST
  • DB_SNOOPER_DB_PORT
  • DB_SNOOPER_DB_USER
  • DB_SNOOPER_DB_PASSWORD
  • DB_SNOOPER_SCHEMA

For server databases, --host defaults to localhost, --port defaults to the database default, and DB Snooper securely prompts for a password when DB_SNOOPER_DB_PASSWORD is not set.

Help

db-snooper -h
db-snooper profile -h

Table filters:

db-snooper profile --db-type sqlite --database app.sqlite --include-tables users,orders,line_items

Schema filter:

db-snooper profile --db-type postgres --database app_db --schema reporting --user readonly_user --port 5432 --ask-password
DB_SNOOPER_SCHEMA=reporting db-snooper profile --db-type postgres --database app_db --user readonly_user --port 5432 --ask-password

Profile options:

  • --small-table-threshold 50: tables with this many rows or fewer are sampled instead of column-profiled.
  • --large-table-threshold 100000000: tables whose catalog row estimate is at/above this count are profiled from internal database stats only. COUNT(*), sampled rows, and per-column queries are skipped because they would be too slow on hundreds of millions of rows. Instead, each column is summarized from the engine's catalog statistics (approximate null fraction, distinct count, numeric min/max, and top values), marked with /(from db stats).
  • --sample-row-limit 50: maximum sampled rows for small tables.
  • --include-tables table_a,table_b: only profile selected tables.
  • --exclude-tables table_c: skip selected tables.
  • --include-technical-tables: profile migration/framework tables (e.g. schema_migrations, alembic_version, flyway_schema_history, django_migrations) that are skipped by default.
  • --include-empty-tables: emit the CREATE TABLE for tables with zero rows. By default empty tables are skipped entirely.
  • --per-table: generate one .sql profile for each table instead of a single schema profile.

Python API

Use the simple helpers when you have a SQLAlchemy URL:

from db_snooper import generate_profile

database_url = "sqlite:///eval-dataset/superhero/superhero.sqlite"

profile_sql = generate_profile(database_url)

Use the lower-level API when you already have a SQLAlchemy engine or need options:

from sqlalchemy import create_engine
from db_snooper import ProfileOptions, profile_database

engine = create_engine("sqlite:///eval-dataset/superhero/superhero.sqlite")

profile_sql = profile_database(
    engine,
    ProfileOptions(
        sample_row_limit=25, include_tables=frozenset({"superhero", "publisher"})
    ),
)

Agent Skill

DB Snooper bundles db-snooper-profile, an agent skill for generating schema and data context before writing or debugging SQL. It runs db-snooper profile and produces <database>/<schema>.sql.

Install it as a Claude Code plugin:

/plugin marketplace add renatyv/db-snooper
/plugin install db-snooper@db-snooper

The skill ships inside the wheel, so you can inspect or install it without cloning this repository:

uvx db-snooper skills list
uvx db-snooper skills install
uvx db-snooper skills install --target all
uvx db-snooper skills install --dir ./.opencode/skills --force

By default, installation targets ~/.config/opencode/skills. Use --target for Claude or agents-compatible directories, or --dir for a custom location.

License

The DB Snooper source code is licensed under the MIT License. See LICENCE.

Third-party Python dependencies remain under their own upstream licenses. See THIRD_PARTY_NOTICES.md for a dependency license summary.

The dataset files included under eval-dataset/ are derived from birdsql by The BIRD Team, and are used and redistributed under the Creative Commons Attribution-ShareAlike 4.0 International License (CC BY-SA 4.0).

These files are not covered by the MIT source-code license. They retain their original CC BY-SA 4.0 terms. Any derivative works that include these files must also be distributed under CC BY-SA 4.0.

Download files

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

Source Distribution

db_snooper-0.0.20.tar.gz (62.0 kB view details)

Uploaded Source

Built Distribution

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

db_snooper-0.0.20-py3-none-any.whl (39.1 kB view details)

Uploaded Python 3

File details

Details for the file db_snooper-0.0.20.tar.gz.

File metadata

  • Download URL: db_snooper-0.0.20.tar.gz
  • Upload date:
  • Size: 62.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for db_snooper-0.0.20.tar.gz
Algorithm Hash digest
SHA256 7a5d04a3cccb7106e8e94fc4881a202b447e1d39a33c9137f77fbb8f5c148b16
MD5 8db35df964d7649e08ee326770c8a7d6
BLAKE2b-256 996564534bacbeb81bf9bfc9007658f3da0cbc108e4dfcfa3105766a758c17a9

See more details on using hashes here.

File details

Details for the file db_snooper-0.0.20-py3-none-any.whl.

File metadata

File hashes

Hashes for db_snooper-0.0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 662603da332da8da7e97cc8d20b6b7913d2991ec7c34571fcf889e918bb9d561
MD5 af59a032a894108e978de750f45e2da8
BLAKE2b-256 401a1f880f792adf3d90105ea40e298b13486cea7451ca20f2c614dea48983bf

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