Skip to content

feat: capture EXPLAIN ANALYZE for slow queries and expose via admin e…#127

Open
Tobi-8 wants to merge 1 commit into
Pidoko257:mainfrom
Tobi-8:feat/slow-query-explain-analyze
Open

feat: capture EXPLAIN ANALYZE for slow queries and expose via admin e…#127
Tobi-8 wants to merge 1 commit into
Pidoko257:mainfrom
Tobi-8:feat/slow-query-explain-analyze

Conversation

@Tobi-8

@Tobi-8 Tobi-8 commented Jun 30, 2026

Copy link
Copy Markdown

Summary of changes:

  1. Migration (migrations/20260630_create_slow_query_plans.sql)
  • New slow_query_plans table with columns: id, query, params (JSONB), duration_ms, plan (JSONB), executed_at, created_at
  • Indexes on created_at DESC and duration_ms DESC
  1. Slow query capture (src/config/database.ts)
  • Lowered SLOW_QUERY_THRESHOLD_MS default from 1000ms to 500ms
  • Added captureExplainAnalyze() — runs EXPLAIN (ANALYZE, COSTS, VERBOSE, FORMAT JSON) via originalPoolQuery (bypasses the monkey-patch to avoid recursion), then inserts into slow_query_plans
  • Fired via setImmediate so it's fully async and adds zero latency to the original query
  1. Admin endpoint (src/routes/admin.ts)
  • GET /admin/slow-queries — returns last 50 records with requireAdmin + logAdminAction middleware

closes #120

…ndpoint

- Create slow_query_plans table to store EXPLAIN ANALYZE output
- Run EXPLAIN (ANALYZE, COSTS, VERBOSE, FORMAT JSON) asynchronously for queries >500ms
- Store original query, params, duration, and plan for later analysis
- Add GET /admin/slow-queries endpoint returning last 50 records
- Lower default SLOW_QUERY_THRESHOLD_MS from 1000 to 500
@drips-wave

drips-wave Bot commented Jun 30, 2026

Copy link
Copy Markdown

@Tobi-8 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Database Query Explain Analyze to Slow Query Logging

1 participant