← Back to Pull requests

perf(database-watt): cut the database worker's cost per request #1

Closed
unknown closed this pull request without merging on Nov 24, 2026 10:15 UTC
View on GitHub
unknown unknown commented on Nov 22, 2026 10:41 UTC
Author

This builds on supabase/storage#1230 and goes after the database worker's thread, which was the bottleneck: it saturated with about two storage workers.

  • c84e76fd sends a whole transaction as one message instead of four (database.batch: BEGIN, the settings and the scope ride on the first query), and runs single-query operations as one message that also commits. The database worker returns raw rows, which the storage worker parses with the same pg type parsers a direct pool uses, so the database thread no longer builds row objects, dates or JSON. With DATABASE_ENABLE_PIPELINING=true a batch is a single socket write.

  • 5e619e1f moves to Platformatic 3.80.1, which includes platformatic/platformatic#5339 and lowers the messaging cost of every round trip.

  • ab6e7547 reads the bucket and the object for object GET and info in one batch instead of two transactions. Each read keeps its permissions: the bucket as the super user, the object with RLS unless the bucket is public. The old and new builds returned the same status and body for 182 request cases covering those routes, roles, and public, private and missing buckets.

  • 2935cff2 moves to Platformatic 3.80.2 and marks the database messages as plain data (sanitize: false, from platformatic/platformatic#5354), so neither side walks every request and result before posting it. CPU profiles of both builds under the same load show that the walk cost the database thread 0.8 to 1 µs per GET or info request (1.2 to 1.6% of its time) and 6.3 µs per list of 100 objects (3.4%), plus about 1 µs per request on each storage worker. Now it costs nothing, and no message fell back to being sanitized. The change in req/s is smaller than the noise between runs.
  • Storage workers and the database application have to be deployed together, because query responses now carry raw rows.

    Measured on Linux (Docker on an M4 Max, 16 vCPUs) with 4 storage workers, wrk with 128 connections, and Postgres and the S3 store in Docker. Both builds ran back to back with DATABASE_ENABLE_PIPELINING=true, which makes no difference on #1230 alone. Absolute numbers move by about 10% between runs.

    | Route | req/s, #1230 → this PR | DB thread µs per request | DB thread busy | p99 |
    |---|---|---|---|---|
    | GET 1 KB | 3,871 → 9,241 | 251 → 68 | 97% → 63% | 216 → 20 ms |
    | object info | 3,999 → 13,228 | 242 → 73 | 97% → 97% | 212 → 16 ms |
    | list 100 | 2,005 → 4,436 | 487 → 179 | 98% → 80% | 100 → 50 ms |
    | sign URL | 7,262 → 13,751 | 135 → 58 | 98% → 79% | 80 → 20 ms |
    | upload 1 KB | 835 → 908 | 1,007 → 833 | 84% → 76% | 349 → 185 ms |

    With 4 workers the database thread now has headroom on GET, list, sign and upload. Object info still saturates it, because info does very little work on the storage side. brianc/node-postgres#3871 (draft) removes two more per-query costs on that thread.

    Diff: 356 lines (+267 / -89) across 4 files Reviewer: ShogunPanda Review duration: 2853m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 356 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.352
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.5% (MEDIUM) · Higher = larger blast radius
    42.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.8% · Safe to merge
    16.8%