Skip to content
proto-tapPublic

About

Watch SQL traffic in real-time with a TUI

Resources

Stars

1.6k stars

Watchers

6 watching

Forks

Latest commit

 

History

252 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sql-tap

Sponsor

Real-time SQL traffic viewer — proxy daemon + TUI / Web client.

sql-tap sits between your application and your database (PostgreSQL, MySQL, or TiDB), capturing every query and displaying it in an interactive terminal UI. Inspect queries, view transactions, and run EXPLAIN — all without changing your application code.

tui web

Installation

Homebrew

brew install --cask proto-tap/tap/sql-tap

Note

sql-tap has moved from the mickamy/tap formula to the proto-tap/tap cask. If you installed it from mickamy/tap, switch with:

brew uninstall sql-tap
brew install --cask proto-tap/tap/sql-tap

Go

go install github.com/proto-tap/sql-tap@latest
go install github.com/proto-tap/sql-tap/cmd/sql-tapd@latest

Build from source

git clone https://github.com/proto-tap/sql-tap.git
cd sql-tap
make install

Nix

Using the flake directly

nix profile install github:proto-tap/sql-tap

In a devenv.sh project

Add sql-tap as a flake input in devenv.yaml:

inputs:
  sql-tap:
    url: github:proto-tap/sql-tap

Then reference it in devenv.nix:

{ inputs, pkgs, ... }:
{
  packages = [
    inputs.sql-tap.packages.${pkgs.system}.default
  ];
}

Docker

FROM alpine:3
ARG SQL_TAP_VERSION=0.0.1
ARG TARGETARCH
ADD https://github.com/proto-tap/sql-tap/releases/download/v${SQL_TAP_VERSION}/sql-tap_${SQL_TAP_VERSION}_linux_${TARGETARCH}.tar.gz /tmp/sql-tap.tar.gz
RUN tar -xzf /tmp/sql-tap.tar.gz -C /usr/local/bin sql-tapd && rm /tmp/sql-tap.tar.gz
ENTRYPOINT ["sql-tapd"]

Run as a sidecar alongside your database:

# PostgreSQL
docker run --rm --network=host sql-tap \
  --driver=postgres --listen=:5433 --upstream=localhost:5432 --grpc=:9091

# MySQL
docker run --rm --network=host sql-tap \
  --driver=mysql --listen=:3307 --upstream=localhost:3306 --grpc=:9091

Quick start

1. Start the proxy daemon

# PostgreSQL: proxy listens on :5433, forwards to PostgreSQL on :5432
DATABASE_URL="postgres://user:pass@localhost:5432/db?sslmode=disable" \
  sql-tapd --driver=postgres --listen=:5433 --upstream=localhost:5432

# MySQL: proxy listens on :3307, forwards to MySQL on :3306
DATABASE_URL="user:pass@tcp(localhost:3306)/db" \
  sql-tapd --driver=mysql --listen=:3307 --upstream=localhost:3306

# TiDB: proxy listens on :4001, forwards to TiDB on :4000
DATABASE_URL="user:pass@tcp(localhost:4000)/db" \
  sql-tapd --driver=tidb --listen=:4001 --upstream=localhost:4000

2. Point your application at the proxy

Connect your app to the proxy port instead of the database port. No code changes needed — sql-tapd speaks the native wire protocol.

3. Launch the TUI

sql-tap localhost:9091

All queries flowing through the proxy appear in real-time.

Usage

sql-tapd

sql-tapd — SQL proxy daemon for sql-tap

Usage:
  sql-tapd [flags]

Flags:
  -config    path to config file (default: .sql-tap.yaml if exists)
  -driver    database driver: postgres, mysql, tidb (required)
  -listen    client listen address (required)
  -upstream  upstream database address (required)
  -grpc      gRPC server address for TUI (default: ":9091")
  -http      HTTP server address for web UI (e.g. ":8080")
  -dsn-env   env var holding DSN for EXPLAIN (default: "DATABASE_URL")
  -nplus1-threshold  N+1 detection threshold (default: 5, 0 to disable)
  -nplus1-window     N+1 detection time window (default: 1s)
  -nplus1-cooldown   N+1 alert cooldown per query template (default: 10s)
  -slow-threshold    slow query threshold (default: 100ms, 0 to disable)
  -version   show version and exit

Set DATABASE_URL (or the env var specified by -dsn-env) to enable EXPLAIN support. Without it, the proxy still captures queries but EXPLAIN is disabled.

Config file

Instead of passing flags on every invocation, you can create a .sql-tap.yaml in your project directory:

driver: postgres
listen: ":5433"
upstream: "localhost:5432"
grpc: ":9091"
http: ":8080"
dsn_env: DATABASE_URL
slow_threshold: 100ms
nplus1:
  threshold: 5
  window: 1s
  cooldown: 10s

sql-tapd automatically loads .sql-tap.yaml from the current directory. Use -config to specify a different path. CLI flags override config file values.

Web UI

Add --http=:8080 to serve a browser-based viewer:

DATABASE_URL="postgres://user:pass@localhost:5432/db?sslmode=disable" \
  sql-tapd --driver=postgres --listen=:5433 --upstream=localhost:5432 --http=:8080

Open http://localhost:8080 in your browser to view queries in real-time. The web UI supports:

  • Real-time query stream via SSE
  • Click to inspect query details
  • EXPLAIN / EXPLAIN ANALYZE
  • Text filter
  • Copy query (with or without bound args)
  • N+1 detection (toast + row highlight)
  • Timeline view (Gantt-chart style query visualization)

sql-tap

sql-tap — Watch SQL traffic in real-time

Usage:
  sql-tap [flags] <addr>
  sql-tap run [flags] <addr> -- <command> [args...]
  sql-tap diff [flags] <before.json> <after.json>

Flags:
  -ci       run in CI mode: collect events until SIGTERM/SIGINT or stream ends, then report and exit
  -format   CI report format on stdout: text or json (default: text)
  -out      also write the CI report as JSON to this file
  -version  show version and exit

<addr> is the gRPC address of sql-tapd (e.g. localhost:9091). See Measuring a command for run and Comparing reports for diff.

CI mode

Run sql-tap -ci to detect N+1 and slow queries in your test suite. It connects to a running sql-tapd (see Quick start for setup), collects events, and exits with code 1 if any problems are found.

# Start sql-tap in CI mode (background)
sql-tap -ci localhost:9091 &
CI_PID=$!

# Run your tests through the proxy
DATABASE_URL="postgres://user:pass@localhost:5433/db?sslmode=disable" go test ./...

# Stop sql-tap — prints report and exits 0 (clean) or 1 (problems found)
kill "$CI_PID" 2>/dev/null || true
wait "$CI_PID" 2>/dev/null || true

Example output:

sql-tap CI Report
=================
Captured: 142 queries

Problems found:
  [N+1]  SELECT * FROM comments WHERE post_id = $1  (detected 12 times)
  [SLOW] SELECT * FROM users JOIN ...  (avg 523ms, 3 occurrences)

Exit: 1 (2 problems found)

Add -format=json to print the report as JSON, or -out=report.json to also write it to a file. If no statements are captured, a warning is printed to stderr; check that your application connects through the proxy port, not the database port. A warning is also printed when sql-tapd dropped events because sql-tap did not keep up (the JSON dropped_events field); the exit code is unchanged in both cases.

Measuring a command

sql-tap run measures the SQL traffic of a single command, without managing a background process. It connects to a running sql-tapd, waits until sql-tapd confirms the subscription, runs the command, and keeps receiving for a short while after the command exits so that no in-flight event is lost.

DATABASE_URL="postgres://user:pass@localhost:5433/db?sslmode=disable" \
  sql-tap run -out=report.json localhost:9091 -- go test ./...

The command keeps its stdin / stdout / stderr, and the report is written to stderr. run empties the -out file when it starts (keeping its permissions), so a run that fails before measuring never leaves a report from an earlier run behind; diff rejects the empty file. The JSON report records the command, so diff can tell whether two reports measured the same thing.

The command runs in its own process group, and SIGINT / SIGTERM sent to sql-tap run are forwarded to the whole group, so processes it spawns (such as test binaries under go test) stop as well. Because the group is not the terminal's foreground group, a command that reads from a terminal stdin is stopped; run is meant for non-interactive commands.

Flag Default Description
-format text Report format on stderr: text or json
-out Also write the report as JSON to this file
-subscribe-timeout 5s How long to wait for sql-tapd to confirm the subscription
-drain 200ms After the command exits, stop once no event has arrived for this long
-drain-max 5s Upper bound on draining after the command exits

Exit codes:

  • The command's own exit code if it fails (128 + the signal number if it was killed by a signal), so test failures are never hidden
  • 0: no problems
  • 1: N+1 or slow queries found
  • 2: no statements captured, sql-tapd did not confirm the subscription (it may be older than sql-tap), or the measurement may be incomplete: receiving from sql-tapd failed, sql-tapd dropped events because sql-tap did not keep up, sql-tapd ended the stream before the command finished, or events kept arriving until -drain-max (another application may be using the same sql-tapd). The report and -out are still written, marked incomplete

Comparing reports

sql-tap diff compares two JSON reports, e.g. before and after a change, and fails when SQL behavior regresses.

sql-tap run -out=before.json localhost:9091 -- go test ./...
# ...apply the change...
sql-tap run -out=after.json localhost:9091 -- go test ./...

sql-tap diff -format=markdown before.json after.json

A change is a regression when:

  • the number of statements increases (tolerate some with -allow-increase=<percent>)
  • the number of N+1 queries increases, or a query is flagged as N+1 for the first time

Slow queries depend on timing and vary between runs, so their increase is only reported as a warning. Pass -fail-on-slow to treat it as a regression. Total time is shown but never treated as a regression.

diff refuses to compare reports measured with different commands (pass -allow-command-mismatch to compare anyway), reports with no statements captured, and reports marked incomplete or with dropped events. run marks a report incomplete (the JSON incomplete field) when it may have missed events; such a report is still written for diagnosis.

Flag Default Description
-format text Output format: text, markdown (for pull requests) or json
-allow-increase 0 Tolerated statement count increase in percent
-fail-on-slow false Treat slow query increases as regressions
-allow-command-mismatch false Compare reports measured with different commands, with a warning

Exit codes: 0 no regressions, 1 regressions found, 2 error (no statements captured, incomplete report, reports measured with different commands).

Example Markdown output:

### sql-tap diff

| Metric | Before | After | Change |
| --- | ---: | ---: | ---: |
| Statements | 41 | 2 | -39 |
| N+1 | 36 | 0 | -36 |
| Slow | 0 | 0 | +0 |
| Total time (ms) | 41.0 | 3.0 | -38.0 |

No regressions.

Agent skill

skills/verify-sql is an agent skill that teaches coding agents such as Claude Code to measure a change with run, compare with diff, and report what was and was not verified. Install it into your project's .claude/skills/ (or ~/.claude/skills/ for all projects) from the release tag that matches your sql-tap, so that the flags and exit codes it describes match your binary:

# <version> is what `sql-tap -version` prints, e.g. 0.5.0
mkdir -p .claude/skills/verify-sql
curl -fsSL -o .claude/skills/verify-sql/SKILL.md \
  https://raw.githubusercontent.com/proto-tap/sql-tap/v<version>/skills/verify-sql/SKILL.md

The skill is included from the first release after v0.4.0.

Keybindings

List view

Key Action
j / ↓ Move down
k / ↑ Move up
Ctrl+d / PgDn Half-page down
Ctrl+u / PgUp Half-page up
/ Incremental text search
f Structured filter (see below)
s Toggle sort (chronological/duration)
Enter Inspect query / transaction
Space Toggle transaction expand / collapse
Esc Clear search / filter
x EXPLAIN
X EXPLAIN ANALYZE
e Edit query, then EXPLAIN
E Edit query, then EXPLAIN ANALYZE
a Analytics view
t Timeline view
c Copy query
C Copy query with bound args
w Export queries to file (JSON/Markdown)
q Quit

Inspector view

Key Action
j / ↓ Scroll down
k / ↑ Scroll up
x EXPLAIN
X EXPLAIN ANALYZE
e / E Edit and EXPLAIN / ANALYZE
c Copy query
C Copy query with bound args
q Back to list

Analytics view

Key Action
j / ↓ Move down
k / ↑ Move up
Ctrl+d Half-page down
Ctrl+u Half-page up
h / ← Scroll left
l / → Scroll right
s Cycle sort (total/count/avg)
c Copy query
q Back to list

Timeline view

Key Action
j / ↓ Scroll down
k / ↑ Scroll up
Ctrl+d / PgDn Half-page down
Ctrl+u / PgUp Half-page up
q Back to list

Explain view

Key Action
j / ↓ Scroll down
k / ↑ Scroll up
h / ← Scroll left
l / → Scroll right
c Copy explain plan
e / E Edit and re-explain / re-analyze
q Back to list

Filter syntax

Press f in the list view to enter filter mode. Filters support structured conditions that go beyond simple text search.

Syntax Meaning Example
d>100ms Duration greater than d>1s, d>500us
d<10ms Duration less than d<50ms
error Events with errors only
n+1 N+1 flagged queries alias: nplus1
slow Slow queries only
op:select SQL keyword prefix op:insert, op:update, op:delete
op:begin Protocol operation op:commit, op:rollback
(other) Text substring match users, WHERE id

Multiple tokens are separated by spaces and combined with AND logic:

op:select d>100ms

This shows only SELECT queries that took longer than 100ms.

Both / (text search) and f (filter) can be active simultaneously — the filter is applied first, then the text search narrows the results further.

N+1 query detection

sql-tap automatically detects N+1 query patterns — when the same SELECT template is executed many times in a short time window.

Detection is enabled by default and runs server-side, so both TUI and Web UI benefit:

  • TUI: alert overlay on first detection + N+1 marker in the Status column for every flagged query
  • Web UI: toast notification on first detection + yellow row highlight + N+1 in the Status column

Configuration

Flag Default Description
--nplus1-threshold 5 Number of executions to trigger detection (0 to disable)
--nplus1-window 1s Sliding time window for counting
--nplus1-cooldown 10s Minimum interval between alert notifications for the same query

Only SELECT queries are monitored. INSERT, UPDATE, DELETE, and transaction lifecycle commands (BEGIN, COMMIT, etc.) are excluded. Metadata queries — SELECT statements without a FROM clause, such as SELECT database(), SELECT @@version, or SELECT 1 — are also excluded, as they are typically driver health checks or system introspection calls rather than application data queries.

Once the threshold is crossed, all subsequent executions of the same template within the window are flagged. The cooldown only affects the notification frequency — the Status column marker always appears.

To disable detection entirely:

sql-tapd --nplus1-threshold=0 ...

Known limitations

Arrow key input in search / filter mode

Due to a limitation in the terminal input parser used by Bubble Tea v1, multi-byte escape sequences (such as arrow keys: ESC [ A/B/C/D) can occasionally be split across OS-level read() calls. When this happens, the remaining bytes ([A, [B, [C, [D, [F, [H) would appear as garbage text in the input field.

sql-tap includes a workaround that detects and discards these split sequences. As a side effect, the literal two-character strings [A, [B, [C, [D, [F, and [H cannot be typed in search or filter input. This is unlikely to affect real-world usage since these patterns rarely appear in SQL queries.

How it works

┌─────────────┐      ┌───────────────────────┐      ┌───────────────────────────┐
│ Application │─────▶│  sql-tapd (proxy)     │─────▶│ PostgreSQL / MySQL / TiDB │
└─────────────┘      │                       │      └───────────────────────────┘
                     │  captures queries     │
                     │  via wire protocol    │
                     └───────────┬───────────┘
                                 │ gRPC stream / SSE
                     ┌───────────▼───────────┐
                     │  sql-tap (TUI)        │
                     │  Browser  (Web UI)    │
                     └───────────────────────┘

sql-tapd parses the database wire protocol (PostgreSQL, MySQL, or TiDB) to intercept queries transparently. It tracks prepared statements, parameter bindings, transactions, execution time, rows affected, and errors. Events are streamed to connected TUI clients via gRPC.

See also

  • grpc-tap — Same concept for gRPC. Transparent HTTP/2 reverse proxy that captures every gRPC / gRPC-Web / Connect call with TUI + Web UI.

License

MIT

About

Watch SQL traffic in real-time with a TUI

Resources

Stars

1.6k stars

Watchers

6 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages