Skip to content

alltotrets: one quotes probe per end of the period (WEBBSITE-3G) - #131

Merged
gfrmin merged 1 commit into
masterfrom
triage/steel-legend-20261118-180405
Nov 18, 2026
Merged

gfrmin merged 1 commit into
masterfrom
triage/steel-legend-20261118-180405

Conversation

@gfrmin

@gfrmin gfrmin commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

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() and lastquotedate() twice, because the planner also pushed them into both closing-price subqueries' index conditions, and it probed ccass.quotes again 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 LATERAL probes (NOT noclose, atdate >= d1 ORDER BY atdate LIMIT 1, and the mirror for d2) 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):

old new
warm, 2008-02/03 window 580 ms, 96k buffers 188 ms, 40k buffers
cold window (different month each) 2.8 s, 3.6k reads 1.9 s, 2.4k reads

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.py checks four things: the SQL makes no first/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

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>
@gfrmin gfrmin added the sentry-triage opened by the Sentry triage agent label Nov 18, 2026
@gfrmin
gfrmin merged commit 1a2c20b into master Nov 18, 2026
6 checks passed
@gfrmin
gfrmin deleted the triage/steel-legend-20261118-180405 branch November 18, 2026 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sentry-triage opened by the Sentry triage agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant