Description
src/db/index.ts exposes getPoolStats and a basic checkDbHealth probe, but individual queries executed through the shared pool/db handle are not timed, so a slow query on payments, agreementEvents, or escrowEvents is invisible until it manifests as request latency downstream. Add a lightweight wrapper that logs any query exceeding a configurable threshold with duration and a truncated, parameter-redacted SQL preview.
Requirements and context
- Add a
DB_SLOW_QUERY_THRESHOLD_MS env var (default e.g. 500ms) to src/config.ts
- Wrap
pool.query (or the Drizzle query logger hook) in src/db/index.ts to time each call and log a structured warning when the threshold is exceeded, reusing the existing console.warn/JSON logger from src/utils/logger.ts
- Never log raw parameter values that could contain PII or secrets — log parameter count/types only
- Must be secure, tested, and documented
- Should be efficient and easy to review
Suggested execution
Fork the repo and create a branch
git checkout -b feature/db-slow-query-logging
Implement changes
- Update/Write:
src/db/index.ts
- Update/Write:
src/config.ts
- Add/Update tests:
src/db/health.test.ts
- Add documentation:
docs/db/schema.md
- Include clear code comments and TS types
- Validate security assumptions
Test and commit
- Run tests:
pnpm test
- Cover edge cases
- Include test output and security notes
Example commit message
feat: add slow-query logging to the Postgres pool
Guidelines
- Minimum 95 percent test coverage
- Clear documentation
- Timeframe: 96 hours
Description
src/db/index.tsexposesgetPoolStatsand a basiccheckDbHealthprobe, but individual queries executed through the sharedpool/dbhandle are not timed, so a slow query onpayments,agreementEvents, orescrowEventsis invisible until it manifests as request latency downstream. Add a lightweight wrapper that logs any query exceeding a configurable threshold with duration and a truncated, parameter-redacted SQL preview.Requirements and context
DB_SLOW_QUERY_THRESHOLD_MSenv var (default e.g. 500ms) tosrc/config.tspool.query(or the Drizzle query logger hook) insrc/db/index.tsto time each call and log a structured warning when the threshold is exceeded, reusing the existingconsole.warn/JSON logger fromsrc/utils/logger.tsSuggested execution
Fork the repo and create a branch
Implement changes
src/db/index.tssrc/config.tssrc/db/health.test.tsdocs/db/schema.mdTest and commit
pnpm testExample commit message
Guidelines