# План снимка очереди relay (change `job-trace-propagation`, задача 12.3) - Основа: commit `72cfa9a` (feat: propagate trace context through outbox and add pipeline saturation metrics), рабочее дерево чистое; файлы `src/` и `migrations/` в контейнерах `api`, `relay`, `worker` и `cleaner` побайтно совпадают с деревом commit (SHA-256 каждого файла). - Дата: 2026-11-11, ~16:57Z; рабочий стенд `mtusi-lab6`, PostgreSQL 18.6, ревизия схемы `e4b8c2d17f90`. - Запрос — `SqlAlchemyAttachmentJobRepository.queue_snapshot` (design, решение 9): текст получен компиляцией выражения repo диалектом asyncpg (`render_postcompile`), состояния — константы, время прохода — параметры `$1`, `$2`. Выполнен в транзакции `READ ONLY` с откатом, `PREPARE` и `plan_cache_mode = force_generic_plan` — общий план, который PostgreSQL может выбрать для подготовленного выражения asyncpg после пяти выполнений. - Проверка: ни одного `Seq Scan` по `attachment_jobs`; каждый из семи подзапросов идёт по частичному индексу своей стадии (`ix_attachment_jobs_outbox` — срок по выражению, `ix_attachment_jobs_published`, `ix_attachment_jobs_lease`), завершённые задания (`succeeded`, `failed`) не читаются. Итог — 7 буферов на 2,2 тыс. строк таблицы, 0,15 мс. - Ограничение: на рабочей базе незавершённых заданий в момент снимка нет, строки узлов — 0; число незавершённых ограничено пределом приёма (`[uploads] max_unfinished_jobs`, 250), а частичные индексы содержат только их, поэтому стоимость снимка от числа завершённых не зависит. ```text jobs by state: failed=33, succeeded=2198 table: reltuples=2227 relpages=45 PREPARE QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------- Result (actual time=0.029..0.030 rows=1.00 loops=1) Buffers: shared hit=7 InitPlan 1 -> Aggregate (actual time=0.003..0.003 rows=1.00 loops=1) Buffers: shared hit=1 -> Index Scan using ix_attachment_jobs_outbox on attachment_jobs (actual time=0.002..0.002 rows=0.00 loops=1) Index Cond: (COALESCE(next_attempt_at, created_at) > $1) Index Searches: 1 Buffers: shared hit=1 InitPlan 2 -> Aggregate (actual time=0.001..0.001 rows=1.00 loops=1) Buffers: shared hit=1 -> Index Scan using ix_attachment_jobs_outbox on attachment_jobs attachment_jobs_1 (actual time=0.000..0.000 rows=0.00 loops=1) Index Cond: (COALESCE(next_attempt_at, created_at) <= $2) Index Searches: 1 Buffers: shared hit=1 InitPlan 3 -> Aggregate (actual time=0.005..0.005 rows=1.00 loops=1) Buffers: shared hit=1 -> Index Only Scan using ix_attachment_jobs_published on attachment_jobs attachment_jobs_2 (actual time=0.005..0.005 rows=0.00 loops=1) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=1 InitPlan 4 -> Aggregate (actual time=0.006..0.006 rows=1.00 loops=1) Buffers: shared hit=1 -> Index Only Scan using ix_attachment_jobs_lease on attachment_jobs attachment_jobs_3 (actual time=0.006..0.006 rows=0.00 loops=1) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=1 InitPlan 5 -> Aggregate (actual time=0.003..0.003 rows=1.00 loops=1) Buffers: shared hit=1 -> Index Scan using ix_attachment_jobs_outbox on attachment_jobs attachment_jobs_4 (actual time=0.002..0.002 rows=0.00 loops=1) Index Searches: 1 Buffers: shared hit=1 InitPlan 6 -> Aggregate (actual time=0.003..0.003 rows=1.00 loops=1) Buffers: shared hit=1 -> Index Scan using ix_attachment_jobs_published on attachment_jobs attachment_jobs_5 (actual time=0.002..0.002 rows=0.00 loops=1) Index Searches: 1 Buffers: shared hit=1 InitPlan 7 -> Aggregate (actual time=0.005..0.005 rows=1.00 loops=1) Buffers: shared hit=1 -> Index Scan using ix_attachment_jobs_lease on attachment_jobs attachment_jobs_6 (actual time=0.005..0.005 rows=0.00 loops=1) Index Searches: 1 Buffers: shared hit=1 Planning: Buffers: shared hit=44 read=2 Planning Time: 1.043 ms Execution Time: 0.154 ms (52 rows) ROLLBACK ``` Текст запроса (общий план, `$1` и `$2` — время прохода relay): ```sql SELECT (SELECT count(*) FROM attachment_jobs WHERE state = 'queued' AND published_at IS NULL AND coalesce(next_attempt_at, created_at) > $1) AS scheduled, (SELECT count(*) FROM attachment_jobs WHERE state = 'queued' AND published_at IS NULL AND coalesce(next_attempt_at, created_at) <= $2) AS outbox, (SELECT count(*) FROM attachment_jobs WHERE state = 'queued' AND published_at IS NOT NULL) AS published, (SELECT count(*) FROM attachment_jobs WHERE state = 'processing') AS processing, least((SELECT min(created_at) FROM attachment_jobs WHERE state = 'queued' AND published_at IS NULL), (SELECT min(created_at) FROM attachment_jobs WHERE state = 'queued' AND published_at IS NOT NULL), (SELECT min(created_at) FROM attachment_jobs WHERE state = 'processing')) AS oldest_created_at; ```