fix-slow-queries
Fix Slow Queries
Deliver a measured improvement with preserved behavior and a reviewable PR whose
latest commit passes checks. A list of suspicious SQL or an unverified rewrite
is an intermediate result. Respect a narrower request for diagnosis only.
1. Establish the target and finish line
- Resolve the reported URL, environment, company/workspace, deployed revision,
owning repository, and local checkout. Read its AGENTS.md and scoped rules.
Paperclip and the private paperclip-cloud control plane have different rules.
- Work in a dedicated branch/worktree. Reuse suitable existing work; keep the
checkout running a live server out of branch switching and benchmark setup.
- Select a bounded batch, usually the top two or three query families. Record
deferred candidates so discovery does not turn into an unbounded rewrite.
- The finish line is: reproducible baseline, faster treatment, output/behavior
proof, relevant regression coverage, and green checks on each final PR head.
A concrete access or infrastructure blocker must be reported as incomplete.
2. Find the actual database and rank the work
Read Cloud discovery when starting from a hosted
workspace. Follow the chain from Paperclip Cloud fleet admin to the stack's Neon
project, branch, endpoint, database, and runtime role. Record the corresponding
application deployment and time window. Do not reuse IDs from a previous run.
Capture query text/fingerprint, calls, total execution time, mean time, rows,
and available I/O, temporary-file, and lock-wait evidence. Rank both expensive
individual calls and frequent work with high aggregate cost. Group variants by
their actual caller; do not add unrelated query means into an endpoint latency.
Keep three claims separate:
| Claim | Evidence needed |
| --- | --- |
| SQL is expensive | Query statistics and a reproducible execution plan |
| The database path dominates a slow request | Correlated SQL, connection wait, and application timings |
| More database capacity would help | Resource/queue pressure in the same window and a capacity-specific experiment |
Cumulative query statistics are not a recent-window trace. Capture reset times
or two snapshots and calculate deltas. A large database-time total alone does
not justify resizing Neon. Application CPU headroom also does not rule out a
busy Node main thread.
3. Trace each candidate to its behavior
Use rg on distinctive SQL fragments, table names, JSON keys, and predicates.
Trace the full path: route or scheduler → service → generated SQL → indexes.
Record frequency, fan-out, page limits, redaction, authorization, and any second
query the caller performs. Compare the deployed implementation with local code.
State a falsifiable cause and the contract the fix must preserve. Typical causes
include casts on indexed columns, repeated detoasting of large JSON, broad
candidate scans, unbounded result sets, duplicate reads, and unnecessary polling.
Use plans and caller evidence to choose among them.
Inspect existing indexes and recent related fixes before adding another index.
For advisory locks, identify the caller, key, transaction duration, and wait
chain first. Preserve checkout, leases, ownership, recovery, and no-replay rules;
removing a lock or polling loop is not a query optimization by itself.
4. Reproduce the baseline locally
Read Local proof before building or changing the
benchmark. Use disposable real PostgreSQL and synthetic data with representative
row counts, selectivity, and payload sizes. Record the PostgreSQL version and
any difference from the hosted server.
Pin the pre-fix commit. Capture the complete SQL and parameters the real service
generates, including projections, joins, ordering, limits, and relevant imported
query helpers. Prove the baseline exhibits the suspected cost before editing.
If it does not, improve the fixture or revise the hypothesis.
Save baseline results, timings, and EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON).
Define the performance acceptance criterion for this fixture before measuring
the treatment. Avoid a brittle universal millisecond threshold.
5. Implement and prove both kinds of green
Make the smallest change supported by the measurements. Author the fix on our
branch from source and evidence. Related community reports can supply leads;
respect any instruction to keep the implementation independent of their code.
Run baseline and treatment against equivalent fixtures under the same settings:
- Performance: use warmups and repeated samples, report medians and spread,
and examine plan/buffer changes. Alternate order or repeat in reverse order
when caching could explain the gain. Report neutral or slower paths honestly.
- Correctness: compare every returned SQL column and the externally visible
behavior. Preserve ordering, duplicates, nulls, tenant scope, and redaction.
A faster wrong result fails even when row counts match.
When intentionally bounding history, compare query speed at the same result
limit separately from the contract change. Test default/max limits and older
record access. Do not call truncated output equivalent to the former full list.
Reject or revise the change if either proof fails. Keep the reproducible harness
with the source where repository conventions allow it; keep bulky evidence out
of git. Exhaust viable query/caller changes and existing indexes before proposing
an exceptional migration. Document the serious reason and why migration-free
alternatives are insufficient. Do not add startup scripts, boot-time DDL, or
recur
