Repository navigation
alltotrets: one quotes probe per end of the period (WEBBSITE-3G) - #131
Merged
Merged
Conversation
The whole-market total-returns query called enigma.firstquotedate() and lastquotedate() twice per issue (the planner pushed them into both closing-price subqueries as well as the columns), then probed quotes again for each close. A cold month window ran 2-3 s alone and hit the 8 s statement timeout under load. Two LATERAL index probes now fetch each end's date and close together. Production, EXPLAIN ANALYZE: warm 580 -> 188 ms, buffers 96k -> 40k, cold ~2.8 s -> ~1.9 s. Output identical to the old query on four windows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sentry: WEBBSITE-3G (
dbpub_market_data.alltotrets, statement timeout, 2 events; latest?d1=2017-10-31&d2=2017-11-30).Cause. The query, not the box: other routes were fast in the window around the event. The route ran a heavy query. For each of ~2,000 issues it called
enigma.firstquotedate()andlastquotedate()twice, because the planner also pushed them into both closing-price subqueries' index conditions, and it probedccass.quotesagain for each close. A cold month window cost 2–3 s on its own and went past the 8 s timeout when it ran alongside other requests.Fix. Two
JOIN LATERALprobes (NOT noclose,atdate >= d1 ORDER BY atdate LIMIT 1, and the mirror ford2) fetch each end's date and close together. The semantics are those of the two functions. No index or timeout change.Evidence (production,
scripts/pgq.py --explain):Output parity. The new and old queries return identical rows on four windows (2008-02→03, 2017-10→11, 1994→2025-10-10, 2025-09-30→10-10). The route's own generated SQL for the incident URL, old and new, also gives byte-identical output (2049 rows).
Test. Item 23 in
tests/test_sentry_regressions.pychecks four things: the SQL makes nofirst/lastquotedate(calls, it has two LATERAL probes, every placeholder is bound, and d1/d2 are bound to the buy/sell probes. Without the fix it fails (3 checks) and with it it passes. The CI DB-free list passes (198) and ruff is clean.🤖 Generated with Claude Code