Files
selfpost/internal/store/migrations/0010_send_log_indexes.sql
T
mixandClaude Opus 5.5 508ac96c79
test / test (push) Canceled after 0s
audit 3: every per-message question of the send log has its index
Migration 0010 (DDL only; 0009 went to stage 2b). EXPLAIN QUERY PLAN of
every send_log statement showed, beyond M2 as written (stage 2 had already
moved the by-domain counts onto (domain_id, created_at)):

- the tailer's sweep of open rows (every 5 min) and its count scanned the
  table: partial idx_send_log_open (created_at) WHERE status IN ('queued',
  'deferred'), the statuses literal in the statements, ORDER BY +id (else
  SQLite walks the table in id order to skip a sort);
- AttachRefusalLine's NOT EXISTS (... refusal_line = ?1) scanned the table
  for every refusal line: partial idx_send_log_refusal_line, the query
  carrying the index's WHERE; its row found by idx_send_log_rejected
  (status literal, +queue_id) instead of every refusal without a queue id;
- an application's level-2 count went through its domain's index:
  +domain_id in appByLogin;
- a page of one domain's or one application's log sorted all of it:
  idx_send_log_domain_id, idx_send_log_app_login (id order; several domains
  still sort, top-N). idx_send_log_domain stays for the global filter.

The statements are constants and builders (countMessagesQuery,
peakPerHourQuery, listOpenQuery, countOpenQuery, sendLogPageQuery,
attachRefusalLineQuery, limitRefusalsQuery) and
TestSendLogQueriesUseTheirIndexes explains exactly those: the index each
must use, no SCAN of send_log, no sort where none is wanted; the literal
statuses checked against the constants. It replaces the hand-written plan
check of TestLimitRefusalsSince.

Tested on the test server: go vet + go test of 24 packages, make e2e (all
pass) on this tree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 10:10:52 +03:00

23 lines
1.3 KiB
SQL

-- The send log's questions, each answered from an index (audit 2026-10-12,
-- M2; stage 3). Measured with EXPLAIN QUERY PLAN against the statements the
-- store runs (TestSendLogQueriesUseTheirIndexes); before this file each of
-- them walked the table or sorted a domain's whole log.
-- The rows still open - queued or deferred - older than a cutoff: the log
-- tailer asks every five minutes, and nearly every row is older than the
-- cutoff, so only an index of the open rows alone helps. The queries name
-- the two statuses as literals, which is what lets SQLite use it.
CREATE INDEX idx_send_log_open ON send_log (created_at) WHERE status IN ('queued', 'deferred');
-- A page of the log, newest first, of one domain (a domain administrator's
-- view) or one application (the filter): an index of the one column is
-- ordered by id within it, so a page reads its rows and stops. The indexes
-- with created_at after the column answer the counts over a time window and
-- cannot give that order.
CREATE INDEX idx_send_log_domain_id ON send_log (domain_id);
CREATE INDEX idx_send_log_app_login ON send_log (app_login);
-- Whether a line of Postfix's log is already on a row: asked for every
-- refusal line the tailer reads. Only refused rows carry one.
CREATE INDEX idx_send_log_refusal_line ON send_log (refusal_line) WHERE refusal_line <> '';