Skip to content

Add configurable slow-query logging around Postgres pool queries in src/db/index.ts #658

Description

@Jagadeeshftw

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSGrantFox open-source programMaybe RewardedGrantFox: potentially rewarded contributionOfficial Campaign | FWC26GrantFox official campaign issuebackendBackend / API workdatabaseDatabase / schema / indexing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions