# План запроса истории: размер 1k Commit 6ba74b1 Читаемый чат — 1000 сообщений; фоновые чаты: 2 × 1k, 10 × 100, 50 × 10. Страница — `limit` профиля; «после» — сколько сообщений чата до страницы. ## Планы приложения auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план, с которыми его выполнило приложение (prepared statement asyncpg). ### read 1k: first, после 0 duration: 0.156 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id FROM message_attachments WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids FROM messages WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = '46854c14-676a-4932-9061-086bf0a80bf2', $2 = '51' Limit (cost=0.28..24.73 rows=51 width=153) (actual time=0.045..0.125 rows=51.00 loops=1) Buffers: shared hit=27 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..479.69 rows=1000 width=153) (actual time=0.044..0.122 rows=51.00 loops=1) Index Cond: (chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) Index Searches: 1 Buffers: shared hit=27 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) ### read 1k: middle, после 500 duration: 0.160 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id FROM message_attachments WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids FROM messages WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id LIMIT $4::INTEGER Query Parameters: $1 = '46854c14-676a-4932-9061-086bf0a80bf2', $2 = '2026-10-31 20:23:26.500819+00', $3 = '5576746d-8c9e-4881-84fd-1a794cd17f1d', $4 = '51' Limit (cost=0.28..45.28 rows=51 width=153) (actual time=0.036..0.125 rows=51.00 loops=1) Buffers: shared hit=47 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..440.51 rows=499 width=153) (actual time=0.035..0.122 rows=51.00 loops=1) Index Cond: ((chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:26.500819+00'::timestamp with time zone, '5576746d-8c9e-4881-84fd-1a794cd17f1d'::uuid))) Index Searches: 1 Buffers: shared hit=47 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) ### read 1k: last, после 900 duration: 0.279 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id FROM message_attachments WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids FROM messages WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id LIMIT $4::INTEGER Query Parameters: $1 = '46854c14-676a-4932-9061-086bf0a80bf2', $2 = '2026-10-31 20:23:30.080533+00', $3 = '29d6bdf7-735d-47a4-bb3c-aa907842be2e', $4 = '51' Limit (cost=120.17..120.30 rows=51 width=153) (actual time=0.239..0.244 rows=51.00 loops=1) Buffers: shared hit=22 -> Sort (cost=120.17..120.42 rows=100 width=153) (actual time=0.239..0.241 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=22 -> Bitmap Heap Scan on messages (cost=9.31..116.85 rows=100 width=153) (actual time=0.040..0.199 rows=100.00 loops=1) Recheck Cond: ((chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:30.080533+00'::timestamp with time zone, '29d6bdf7-735d-47a4-bb3c-aa907842be2e'::uuid))) Heap Blocks: exact=19 Buffers: shared hit=22 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..9.28 rows=100 width=0) (actual time=0.017..0.017 rows=100.00 loops=1) Index Cond: ((chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:30.080533+00'::timestamp with time zone, '29d6bdf7-735d-47a4-bb3c-aa907842be2e'::uuid))) Index Searches: 1 Buffers: shared hit=3 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=100) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Filter: (message_id = messages.id) ### bg 1k: first, после 0 duration: 0.135 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id FROM message_attachments WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids FROM messages WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = '62be041f-7108-4a7a-becb-28edcdfc01aa', $2 = '51' Limit (cost=0.28..24.73 rows=51 width=153) (actual time=0.027..0.101 rows=51.00 loops=1) Buffers: shared hit=32 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..479.69 rows=1000 width=153) (actual time=0.027..0.098 rows=51.00 loops=1) Index Cond: (chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) Index Searches: 1 Buffers: shared hit=32 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) ### bg 1k: middle, после 500 duration: 0.108 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id FROM message_attachments WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids FROM messages WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id LIMIT $4::INTEGER Query Parameters: $1 = '62be041f-7108-4a7a-becb-28edcdfc01aa', $2 = '2026-10-31 20:23:26.481237+00', $3 = 'd44546d3-6718-4522-9e30-1bb8271f1d93', $4 = '51' Limit (cost=0.28..45.51 rows=51 width=153) (actual time=0.019..0.079 rows=51.00 loops=1) Buffers: shared hit=49 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..444.58 rows=501 width=153) (actual time=0.018..0.076 rows=51.00 loops=1) Index Cond: ((chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:26.481237+00'::timestamp with time zone, 'd44546d3-6718-4522-9e30-1bb8271f1d93'::uuid))) Index Searches: 1 Buffers: shared hit=49 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) ### bg 1k: last, после 900 duration: 0.154 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id FROM message_attachments WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids FROM messages WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id LIMIT $4::INTEGER Query Parameters: $1 = '62be041f-7108-4a7a-becb-28edcdfc01aa', $2 = '2026-10-31 20:23:30.078364+00', $3 = 'd0076703-57f1-4448-86f4-e1b7e93ee8d7', $4 = '51' Limit (cost=120.17..120.30 rows=51 width=153) (actual time=0.129..0.132 rows=51.00 loops=1) Buffers: shared hit=19 -> Sort (cost=120.17..120.42 rows=100 width=153) (actual time=0.128..0.130 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=19 -> Bitmap Heap Scan on messages (cost=9.31..116.85 rows=100 width=153) (actual time=0.026..0.104 rows=100.00 loops=1) Recheck Cond: ((chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:30.078364+00'::timestamp with time zone, 'd0076703-57f1-4448-86f4-e1b7e93ee8d7'::uuid))) Heap Blocks: exact=17 Buffers: shared hit=19 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..9.28 rows=100 width=0) (actual time=0.011..0.011 rows=100.00 loops=1) Index Cond: ((chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:30.078364+00'::timestamp with time zone, 'd0076703-57f1-4448-86f4-e1b7e93ee8d7'::uuid))) Index Searches: 1 Buffers: shared hit=2 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Filter: (message_id = messages.id) ### bg 100: first, после 0 duration: 0.314 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id FROM message_attachments WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids FROM messages WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = 'c29b11ae-9145-41f5-ad26-73f1c05430d8', $2 = '51' Limit (cost=119.42..119.55 rows=51 width=153) (actual time=0.284..0.288 rows=51.00 loops=1) Buffers: shared hit=65 -> Sort (cost=119.42..119.67 rows=100 width=153) (actual time=0.283..0.285 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=65 -> Bitmap Heap Scan on messages (cost=9.06..116.10 rows=100 width=153) (actual time=0.033..0.257 rows=100.00 loops=1) Recheck Cond: (chat_id = 'c29b11ae-9145-41f5-ad26-73f1c05430d8'::uuid) Heap Blocks: exact=63 Buffers: shared hit=65 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..9.03 rows=100 width=0) (actual time=0.009..0.009 rows=100.00 loops=1) Index Cond: (chat_id = 'c29b11ae-9145-41f5-ad26-73f1c05430d8'::uuid) Index Searches: 1 Buffers: shared hit=2 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=100) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Filter: (message_id = messages.id) ### bg 10: first, после 0 duration: 0.043 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id FROM message_attachments WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids FROM messages WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = '6f3adfe6-1e3e-4065-a369-d2cd6e625865', $2 = '51' Limit (cost=35.36..35.39 rows=10 width=153) (actual time=0.039..0.040 rows=10.00 loops=1) Buffers: shared hit=12 -> Sort (cost=35.36..35.39 rows=10 width=153) (actual time=0.039..0.039 rows=10.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 27kB Buffers: shared hit=12 -> Bitmap Heap Scan on messages (cost=4.36..35.20 rows=10 width=153) (actual time=0.015..0.030 rows=10.00 loops=1) Recheck Cond: (chat_id = '6f3adfe6-1e3e-4065-a369-d2cd6e625865'::uuid) Heap Blocks: exact=10 Buffers: shared hit=12 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..4.36 rows=10 width=0) (actual time=0.007..0.007 rows=10.00 loops=1) Index Cond: (chat_id = '6f3adfe6-1e3e-4065-a369-d2cd6e625865'::uuid) Index Searches: 1 Buffers: shared hit=2 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10) Filter: (message_id = messages.id) ## Страницы сравнения в psql: варианты Тот же текст запроса и те же параметры, что у плана приложения этой страницы, в одной транзакции, которая откатывается: индексы базы после неё те же. Каждый вариант выполнен дважды, здесь — второе выполнение. `psql` — индекс приложения, custom plan; `generic` — он же с force_generic_plan; `or-cursor` — условие курсора через OR; `no-index` — без индекса приложения; варианты индекса вместо него: `chat_only` (chat_id), `covering` (chat_id, created_at, id) INCLUDE (sender_id, text). ### read 1k: middle, после 500 #### psql Limit (cost=0.28..45.28 rows=51 width=153) (actual time=0.006..0.063 rows=51.00 loops=1) Buffers: shared hit=47 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..440.51 rows=499 width=153) (actual time=0.006..0.060 rows=51.00 loops=1) Index Cond: ((chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:26.500819+00'::timestamp with time zone, '5576746d-8c9e-4881-84fd-1a794cd17f1d'::uuid))) Index Searches: 1 Buffers: shared hit=47 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.047 ms Execution Time: 0.070 ms #### generic Limit (cost=0.28..7.92 rows=2 width=153) (actual time=0.011..0.061 rows=51.00 loops=1) Buffers: shared hit=47 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..91.99 rows=24 width=153) (actual time=0.011..0.058 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=47 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning Time: 0.006 ms Execution Time: 0.069 ms #### or-cursor Limit (cost=0.28..49.28 rows=51 width=153) (actual time=0.077..0.122 rows=51.00 loops=1) Buffers: shared hit=496 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..479.68 rows=499 width=153) (actual time=0.077..0.120 rows=51.00 loops=1) Index Cond: (chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) Filter: ((created_at > '2026-10-31 20:23:26.500819+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:23:26.500819+00'::timestamp with time zone) AND (id > '5576746d-8c9e-4881-84fd-1a794cd17f1d'::uuid))) Rows Removed by Filter: 500 Index Searches: 1 Buffers: shared hit=496 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.047 ms Execution Time: 0.128 ms #### no-index Limit (cost=203.88..204.01 rows=51 width=153) (actual time=0.566..0.571 rows=51.00 loops=1) Buffers: shared hit=101 -> Sort (cost=203.88..205.13 rows=499 width=153) (actual time=0.566..0.568 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 41kB Buffers: shared hit=101 -> Seq Scan on messages (cost=0.00..187.24 rows=499 width=153) (actual time=0.092..0.507 rows=500.00 loops=1) Filter: ((chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:26.500819+00'::timestamp with time zone, '5576746d-8c9e-4881-84fd-1a794cd17f1d'::uuid))) Rows Removed by Filter: 4000 Buffers: shared hit=101 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.054 ms Execution Time: 0.578 ms #### chat_only Limit (cost=158.54..158.67 rows=51 width=153) (actual time=0.455..0.459 rows=51.00 loops=1) Buffers: shared hit=103 -> Sort (cost=158.54..159.79 rows=499 width=153) (actual time=0.455..0.457 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 41kB Buffers: shared hit=103 -> Bitmap Heap Scan on messages (cost=15.91..141.89 rows=499 width=153) (actual time=0.046..0.401 rows=500.00 loops=1) Recheck Cond: (chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-31 20:23:26.500819+00'::timestamp with time zone, '5576746d-8c9e-4881-84fd-1a794cd17f1d'::uuid)) Rows Removed by Filter: 500 Heap Blocks: exact=101 Buffers: shared hit=103 -> Bitmap Index Scan on bench_variant (cost=0.00..15.78 rows=1000 width=0) (actual time=0.011..0.011 rows=1000.00 loops=1) Index Cond: (chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) Index Searches: 1 Buffers: shared hit=2 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.046 ms Execution Time: 0.466 ms #### covering Limit (cost=0.28..6.16 rows=51 width=153) (actual time=0.005..0.050 rows=51.00 loops=1) Buffers: shared hit=4 -> Index Only Scan using bench_variant on messages (cost=0.28..57.75 rows=499 width=153) (actual time=0.005..0.048 rows=51.00 loops=1) Index Cond: ((chat_id = '46854c14-676a-4932-9061-086bf0a80bf2'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:26.500819+00'::timestamp with time zone, '5576746d-8c9e-4881-84fd-1a794cd17f1d'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=4 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.051 ms Execution Time: 0.057 ms ### bg 1k: middle, после 500 #### psql Limit (cost=0.28..45.51 rows=51 width=153) (actual time=0.014..0.059 rows=51.00 loops=1) Buffers: shared hit=49 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..444.58 rows=501 width=153) (actual time=0.014..0.056 rows=51.00 loops=1) Index Cond: ((chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:26.481237+00'::timestamp with time zone, 'd44546d3-6718-4522-9e30-1bb8271f1d93'::uuid))) Index Searches: 1 Buffers: shared hit=49 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.074 ms Execution Time: 0.068 ms #### generic Limit (cost=0.28..7.92 rows=2 width=153) (actual time=0.011..0.058 rows=51.00 loops=1) Buffers: shared hit=49 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..91.99 rows=24 width=153) (actual time=0.011..0.054 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=49 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning Time: 0.009 ms Execution Time: 0.069 ms #### or-cursor Limit (cost=0.28..49.09 rows=51 width=153) (actual time=0.082..0.117 rows=51.00 loops=1) Buffers: shared hit=487 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..479.71 rows=501 width=153) (actual time=0.081..0.115 rows=51.00 loops=1) Index Cond: (chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) Filter: ((created_at > '2026-10-31 20:23:26.481237+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:23:26.481237+00'::timestamp with time zone) AND (id > 'd44546d3-6718-4522-9e30-1bb8271f1d93'::uuid))) Rows Removed by Filter: 500 Index Searches: 1 Buffers: shared hit=487 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.073 ms Execution Time: 0.125 ms #### no-index Limit (cost=203.98..204.11 rows=51 width=153) (actual time=0.480..0.484 rows=51.00 loops=1) Buffers: shared hit=101 -> Sort (cost=203.98..205.23 rows=501 width=153) (actual time=0.480..0.482 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 40kB Buffers: shared hit=101 -> Seq Scan on messages (cost=0.00..187.27 rows=501 width=153) (actual time=0.069..0.430 rows=500.00 loops=1) Filter: ((chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:26.481237+00'::timestamp with time zone, 'd44546d3-6718-4522-9e30-1bb8271f1d93'::uuid))) Rows Removed by Filter: 4000 Buffers: shared hit=101 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.038 ms Execution Time: 0.489 ms #### chat_only Limit (cost=158.64..158.76 rows=51 width=153) (actual time=0.418..0.421 rows=51.00 loops=1) Buffers: shared hit=101 -> Sort (cost=158.64..159.89 rows=501 width=153) (actual time=0.417..0.419 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 40kB Buffers: shared hit=101 -> Bitmap Heap Scan on messages (cost=15.91..141.92 rows=501 width=153) (actual time=0.062..0.369 rows=500.00 loops=1) Recheck Cond: (chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-31 20:23:26.481237+00'::timestamp with time zone, 'd44546d3-6718-4522-9e30-1bb8271f1d93'::uuid)) Rows Removed by Filter: 500 Heap Blocks: exact=99 Buffers: shared hit=101 -> Bitmap Index Scan on bench_variant (cost=0.00..15.78 rows=1000 width=0) (actual time=0.015..0.015 rows=1000.00 loops=1) Index Cond: (chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) Index Searches: 1 Buffers: shared hit=2 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.068 ms Execution Time: 0.431 ms #### covering Limit (cost=0.28..6.14 rows=51 width=153) (actual time=0.005..0.049 rows=51.00 loops=1) Buffers: shared hit=4 -> Index Only Scan using bench_variant on messages (cost=0.28..57.82 rows=501 width=153) (actual time=0.005..0.047 rows=51.00 loops=1) Index Cond: ((chat_id = '62be041f-7108-4a7a-becb-28edcdfc01aa'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:23:26.481237+00'::timestamp with time zone, 'd44546d3-6718-4522-9e30-1bb8271f1d93'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=4 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.045 ms Execution Time: 0.055 ms ### bg 100: first, после 0 #### psql Limit (cost=119.42..119.55 rows=51 width=153) (actual time=0.122..0.126 rows=51.00 loops=1) Buffers: shared hit=65 -> Sort (cost=119.42..119.67 rows=100 width=153) (actual time=0.121..0.123 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=65 -> Bitmap Heap Scan on messages (cost=9.06..116.10 rows=100 width=153) (actual time=0.017..0.108 rows=100.00 loops=1) Recheck Cond: (chat_id = 'c29b11ae-9145-41f5-ad26-73f1c05430d8'::uuid) Heap Blocks: exact=63 Buffers: shared hit=65 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..9.03 rows=100 width=0) (actual time=0.007..0.007 rows=100.00 loops=1) Index Cond: (chat_id = 'c29b11ae-9145-41f5-ad26-73f1c05430d8'::uuid) Index Searches: 1 Buffers: shared hit=2 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.042 ms Execution Time: 0.138 ms #### generic Limit (cost=0.28..21.53 rows=7 width=153) (actual time=0.016..0.059 rows=51.00 loops=1) Buffers: shared hit=45 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..215.80 rows=71 width=153) (actual time=0.015..0.057 rows=51.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=45 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning Time: 0.003 ms Execution Time: 0.065 ms #### no-index Limit (cost=162.07..162.20 rows=51 width=153) (actual time=0.187..0.191 rows=51.00 loops=1) Buffers: shared hit=101 -> Sort (cost=162.07..162.32 rows=100 width=153) (actual time=0.187..0.188 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=101 -> Seq Scan on messages (cost=0.00..158.75 rows=100 width=153) (actual time=0.005..0.172 rows=100.00 loops=1) Filter: (chat_id = 'c29b11ae-9145-41f5-ad26-73f1c05430d8'::uuid) Rows Removed by Filter: 4400 Buffers: shared hit=101 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.053 ms Execution Time: 0.198 ms #### chat_only Limit (cost=115.42..115.55 rows=51 width=153) (actual time=0.113..0.118 rows=51.00 loops=1) Buffers: shared hit=65 -> Sort (cost=115.42..115.67 rows=100 width=153) (actual time=0.113..0.115 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=65 -> Bitmap Heap Scan on messages (cost=5.06..112.10 rows=100 width=153) (actual time=0.009..0.096 rows=100.00 loops=1) Recheck Cond: (chat_id = 'c29b11ae-9145-41f5-ad26-73f1c05430d8'::uuid) Heap Blocks: exact=63 Buffers: shared hit=65 -> Bitmap Index Scan on bench_variant (cost=0.00..5.03 rows=100 width=0) (actual time=0.004..0.004 rows=100.00 loops=1) Index Cond: (chat_id = 'c29b11ae-9145-41f5-ad26-73f1c05430d8'::uuid) Index Searches: 1 Buffers: shared hit=2 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.036 ms Execution Time: 0.125 ms #### covering Limit (cost=0.28..6.02 rows=51 width=153) (actual time=0.006..0.042 rows=51.00 loops=1) Buffers: shared hit=4 -> Index Only Scan using bench_variant on messages (cost=0.28..11.53 rows=100 width=153) (actual time=0.006..0.040 rows=51.00 loops=1) Index Cond: (chat_id = 'c29b11ae-9145-41f5-ad26-73f1c05430d8'::uuid) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=4 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.040 ms Execution Time: 0.049 ms ### bg 10: first, после 0 #### psql Limit (cost=35.36..35.39 rows=10 width=153) (actual time=0.032..0.033 rows=10.00 loops=1) Buffers: shared hit=12 -> Sort (cost=35.36..35.39 rows=10 width=153) (actual time=0.032..0.033 rows=10.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 27kB Buffers: shared hit=12 -> Bitmap Heap Scan on messages (cost=4.36..35.20 rows=10 width=153) (actual time=0.020..0.030 rows=10.00 loops=1) Recheck Cond: (chat_id = '6f3adfe6-1e3e-4065-a369-d2cd6e625865'::uuid) Heap Blocks: exact=10 Buffers: shared hit=12 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..4.36 rows=10 width=0) (actual time=0.004..0.004 rows=10.00 loops=1) Index Cond: (chat_id = '6f3adfe6-1e3e-4065-a369-d2cd6e625865'::uuid) Index Searches: 1 Buffers: shared hit=2 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.044 ms Execution Time: 0.047 ms #### generic Limit (cost=0.28..21.53 rows=7 width=153) (actual time=0.013..0.023 rows=10.00 loops=1) Buffers: shared hit=12 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..215.80 rows=71 width=153) (actual time=0.012..0.022 rows=10.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=12 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10) Filter: (message_id = messages.id) Planning Time: 0.003 ms Execution Time: 0.029 ms #### no-index Limit (cost=157.57..157.59 rows=10 width=153) (actual time=0.139..0.140 rows=10.00 loops=1) Buffers: shared hit=101 -> Sort (cost=157.57..157.59 rows=10 width=153) (actual time=0.138..0.139 rows=10.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 27kB Buffers: shared hit=101 -> Seq Scan on messages (cost=0.00..157.40 rows=10 width=153) (actual time=0.012..0.135 rows=10.00 loops=1) Filter: (chat_id = '6f3adfe6-1e3e-4065-a369-d2cd6e625865'::uuid) Rows Removed by Filter: 4490 Buffers: shared hit=101 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.050 ms Execution Time: 0.146 ms #### chat_only Limit (cost=35.36..35.39 rows=10 width=153) (actual time=0.022..0.024 rows=10.00 loops=1) Buffers: shared hit=12 -> Sort (cost=35.36..35.39 rows=10 width=153) (actual time=0.022..0.023 rows=10.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 27kB Buffers: shared hit=12 -> Bitmap Heap Scan on messages (cost=4.36..35.20 rows=10 width=153) (actual time=0.008..0.020 rows=10.00 loops=1) Recheck Cond: (chat_id = '6f3adfe6-1e3e-4065-a369-d2cd6e625865'::uuid) Heap Blocks: exact=10 Buffers: shared hit=12 -> Bitmap Index Scan on bench_variant (cost=0.00..4.36 rows=10 width=0) (actual time=0.003..0.003 rows=10.00 loops=1) Index Cond: (chat_id = '6f3adfe6-1e3e-4065-a369-d2cd6e625865'::uuid) Index Searches: 1 Buffers: shared hit=2 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.057 ms Execution Time: 0.033 ms #### covering Limit (cost=0.28..4.61 rows=10 width=153) (actual time=0.004..0.010 rows=10.00 loops=1) Buffers: shared hit=3 -> Index Only Scan using bench_variant on messages (cost=0.28..4.61 rows=10 width=153) (actual time=0.004..0.010 rows=10.00 loops=1) Index Cond: (chat_id = '6f3adfe6-1e3e-4065-a369-d2cd6e625865'::uuid) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=3 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10) Sort Key: message_attachments."position" Sort Method: quicksort Memory: 25kB -> Seq Scan on message_attachments (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.038 ms Execution Time: 0.015 ms