# План запроса истории: размер 10k Commit b2fb88f Читаемый чат — 10000 сообщений; фоновые чаты: 2 × 1k, 10 × 100, 50 × 10. Страница — `limit` профиля; «после» — сколько сообщений чата до страницы. ## Планы приложения auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план, с которыми его выполнило приложение (prepared statement asyncpg). ### read 10k: first, после 0 duration: 0.184 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 = 'e7f0a7ce-371e-41c8-8b8c-182d863d4eee', $2 = '51' Limit (cost=0.29..9.59 rows=51 width=153) (actual time=0.042..0.140 rows=51.00 loops=1) Buffers: shared hit=17 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..1825.36 rows=10000 width=153) (actual time=0.040..0.135 rows=51.00 loops=1) Index Cond: (chat_id = 'e7f0a7ce-371e-41c8-8b8c-182d863d4eee'::uuid) Index Searches: 1 Buffers: shared hit=17 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 10k: middle, после 5000 duration: 0.146 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 = 'e7f0a7ce-371e-41c8-8b8c-182d863d4eee', $2 = '2026-10-31 20:22:09.184908+00', $3 = 'e12757c1-c860-44ed-8d49-05bd9ef9ed8a', $4 = '51' Limit (cost=0.29..21.79 rows=7 width=153) (actual time=0.032..0.107 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..218.45 rows=71 width=153) (actual time=0.030..0.103 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=53 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 10k: last, после 9900 duration: 0.144 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 = 'e7f0a7ce-371e-41c8-8b8c-182d863d4eee', $2 = '2026-10-31 20:22:20.445692+00', $3 = 'ff794cf6-13c4-4816-a97e-ef0fc4ef4326', $4 = '51' Limit (cost=0.29..21.79 rows=7 width=153) (actual time=0.031..0.113 rows=51.00 loops=1) Buffers: shared hit=54 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..218.45 rows=71 width=153) (actual time=0.030..0.109 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=54 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: first, после 0 duration: 0.150 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 = '8b619ecf-de9e-4043-8b20-40346656f7ba', $2 = '51' Limit (cost=0.29..54.04 rows=51 width=153) (actual time=0.033..0.117 rows=51.00 loops=1) Buffers: shared hit=46 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..1054.25 rows=1000 width=153) (actual time=0.033..0.114 rows=51.00 loops=1) Index Cond: (chat_id = '8b619ecf-de9e-4043-8b20-40346656f7ba'::uuid) Index Searches: 1 Buffers: shared hit=46 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.185 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 = '8b619ecf-de9e-4043-8b20-40346656f7ba', $2 = '2026-10-31 20:22:09.183192+00', $3 = '6c079c72-956b-4d22-902d-50525cd17715', $4 = '51' Limit (cost=0.29..21.79 rows=7 width=153) (actual time=0.023..0.140 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..218.45 rows=71 width=153) (actual time=0.022..0.137 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=52 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.126 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 = '8b619ecf-de9e-4043-8b20-40346656f7ba', $2 = '2026-10-31 20:22:18.306084+00', $3 = '20861ffc-3d7f-413e-a417-26cec0b4dcb3', $4 = '51' Limit (cost=0.29..21.79 rows=7 width=153) (actual time=0.016..0.096 rows=51.00 loops=1) Buffers: shared hit=47 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..218.45 rows=71 width=153) (actual time=0.016..0.093 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) ### bg 100: first, после 0 duration: 0.165 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 = '2c89c970-25a2-4db8-be56-14a7e9aa8283', $2 = '51' Limit (cost=0.29..151.02 rows=51 width=153) (actual time=0.013..0.134 rows=51.00 loops=1) Buffers: shared hit=48 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..295.85 rows=100 width=153) (actual time=0.013..0.131 rows=51.00 loops=1) Index Cond: (chat_id = '2c89c970-25a2-4db8-be56-14a7e9aa8283'::uuid) Index Searches: 1 Buffers: shared hit=48 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 10: first, после 0 duration: 0.073 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 = 'd96f0cdc-62d0-4949-b4e8-9f94e470ece5', $2 = '51' Limit (cost=0.29..38.25 rows=10 width=153) (actual time=0.028..0.065 rows=10.00 loops=1) Buffers: shared hit=12 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..38.25 rows=10 width=153) (actual time=0.027..0.064 rows=10.00 loops=1) Index Cond: (chat_id = 'd96f0cdc-62d0-4949-b4e8-9f94e470ece5'::uuid) Index Searches: 1 Buffers: shared hit=12 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.002..0.002 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 10k: middle, после 5000 #### psql Limit (cost=0.29..14.64 rows=51 width=153) (actual time=0.006..0.057 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..1408.11 rows=5000 width=153) (actual time=0.005..0.053 rows=51.00 loops=1) Index Cond: ((chat_id = 'e7f0a7ce-371e-41c8-8b8c-182d863d4eee'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:22:09.184908+00'::timestamp with time zone, 'e12757c1-c860-44ed-8d49-05bd9ef9ed8a'::uuid))) Index Searches: 1 Buffers: shared hit=53 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.044 ms Execution Time: 0.065 ms #### generic Limit (cost=0.29..21.79 rows=7 width=153) (actual time=0.014..0.065 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..218.45 rows=71 width=153) (actual time=0.014..0.062 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=53 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.011 ms Execution Time: 0.077 ms #### or-cursor Limit (cost=0.29..18.90 rows=51 width=153) (actual time=0.720..0.763 rows=51.00 loops=1) Buffers: shared hit=5063 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..1825.36 rows=5000 width=153) (actual time=0.720..0.760 rows=51.00 loops=1) Index Cond: (chat_id = 'e7f0a7ce-371e-41c8-8b8c-182d863d4eee'::uuid) Filter: ((created_at > '2026-10-31 20:22:09.184908+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:22:09.184908+00'::timestamp with time zone) AND (id > 'e12757c1-c860-44ed-8d49-05bd9ef9ed8a'::uuid))) Rows Removed by Filter: 5000 Index Searches: 1 Buffers: shared hit=5063 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.061 ms Execution Time: 0.779 ms #### no-index Limit (cost=771.06..771.19 rows=51 width=153) (actual time=3.982..3.986 rows=51.00 loops=1) Buffers: shared hit=293 -> Sort (cost=771.06..783.56 rows=5000 width=153) (actual time=3.982..3.984 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 44kB Buffers: shared hit=293 -> Seq Scan on messages (cost=0.00..604.25 rows=5000 width=153) (actual time=0.224..3.444 rows=5000.00 loops=1) Filter: ((chat_id = 'e7f0a7ce-371e-41c8-8b8c-182d863d4eee'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:22:09.184908+00'::timestamp with time zone, 'e12757c1-c860-44ed-8d49-05bd9ef9ed8a'::uuid))) Rows Removed by Filter: 8500 Buffers: shared hit=293 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=5000) 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=5000) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.069 ms Execution Time: 3.995 ms #### chat_only Limit (cost=771.06..771.19 rows=51 width=153) (actual time=3.710..3.713 rows=51.00 loops=1) Buffers: shared hit=293 -> Sort (cost=771.06..783.56 rows=5000 width=153) (actual time=3.710..3.711 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 44kB Buffers: shared hit=293 -> Seq Scan on messages (cost=0.00..604.25 rows=5000 width=153) (actual time=0.208..3.168 rows=5000.00 loops=1) Filter: ((chat_id = 'e7f0a7ce-371e-41c8-8b8c-182d863d4eee'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:22:09.184908+00'::timestamp with time zone, 'e12757c1-c860-44ed-8d49-05bd9ef9ed8a'::uuid))) Rows Removed by Filter: 8500 Buffers: shared hit=293 SubPlan 1 -> Sort (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=5000) 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=5000) Filter: (message_id = messages.id) Planning: Buffers: shared hit=3 Planning Time: 0.037 ms Execution Time: 3.719 ms #### covering Limit (cost=0.41..6.19 rows=51 width=153) (actual time=0.007..0.044 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.41..567.41 rows=5000 width=153) (actual time=0.006..0.041 rows=51.00 loops=1) Index Cond: ((chat_id = 'e7f0a7ce-371e-41c8-8b8c-182d863d4eee'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:22:09.184908+00'::timestamp with time zone, 'e12757c1-c860-44ed-8d49-05bd9ef9ed8a'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 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.048 ms Execution Time: 0.052 ms ### bg 1k: middle, после 500 #### psql Limit (cost=0.29..95.89 rows=51 width=153) (actual time=0.006..0.049 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..937.55 rows=500 width=153) (actual time=0.006..0.046 rows=51.00 loops=1) Index Cond: ((chat_id = '8b619ecf-de9e-4043-8b20-40346656f7ba'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:22:09.183192+00'::timestamp with time zone, '6c079c72-956b-4d22-902d-50525cd17715'::uuid))) Index Searches: 1 Buffers: shared hit=52 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.041 ms Execution Time: 0.057 ms #### generic Limit (cost=0.29..21.79 rows=7 width=153) (actual time=0.012..0.061 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..218.45 rows=71 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=52 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.072 ms #### or-cursor Limit (cost=0.29..107.79 rows=51 width=153) (actual time=0.083..0.121 rows=51.00 loops=1) Buffers: shared hit=502 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..1054.25 rows=500 width=153) (actual time=0.083..0.119 rows=51.00 loops=1) Index Cond: (chat_id = '8b619ecf-de9e-4043-8b20-40346656f7ba'::uuid) Filter: ((created_at > '2026-10-31 20:22:09.183192+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:22:09.183192+00'::timestamp with time zone) AND (id > '6c079c72-956b-4d22-902d-50525cd17715'::uuid))) Rows Removed by Filter: 500 Index Searches: 1 Buffers: shared hit=502 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.076 ms Execution Time: 0.129 ms #### no-index Limit (cost=553.43..553.56 rows=51 width=153) (actual time=0.666..0.669 rows=51.00 loops=1) Buffers: shared hit=293 -> Sort (cost=553.43..554.68 rows=500 width=153) (actual time=0.666..0.667 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 39kB Buffers: shared hit=293 -> Seq Scan on messages (cost=0.00..536.75 rows=500 width=153) (actual time=0.192..0.613 rows=500.00 loops=1) Filter: ((chat_id = '8b619ecf-de9e-4043-8b20-40346656f7ba'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:22:09.183192+00'::timestamp with time zone, '6c079c72-956b-4d22-902d-50525cd17715'::uuid))) Rows Removed by Filter: 13000 Buffers: shared hit=293 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.066 ms Execution Time: 0.676 ms #### chat_only Limit (cost=350.59..350.72 rows=51 width=153) (actual time=0.507..0.511 rows=51.00 loops=1) Buffers: shared hit=281 -> Sort (cost=350.59..351.84 rows=500 width=153) (actual time=0.507..0.509 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 39kB Buffers: shared hit=281 -> Bitmap Heap Scan on messages (cost=15.91..333.91 rows=500 width=153) (actual time=0.094..0.442 rows=500.00 loops=1) Recheck Cond: (chat_id = '8b619ecf-de9e-4043-8b20-40346656f7ba'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-31 20:22:09.183192+00'::timestamp with time zone, '6c079c72-956b-4d22-902d-50525cd17715'::uuid)) Rows Removed by Filter: 500 Heap Blocks: exact=278 Buffers: shared hit=281 -> Bitmap Index Scan on bench_variant (cost=0.00..15.79 rows=1000 width=0) (actual time=0.022..0.022 rows=1000.00 loops=1) Index Cond: (chat_id = '8b619ecf-de9e-4043-8b20-40346656f7ba'::uuid) Index Searches: 1 Buffers: shared hit=3 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.042 ms Execution Time: 0.517 ms #### covering Limit (cost=0.41..6.28 rows=51 width=153) (actual time=0.007..0.046 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.41..57.91 rows=500 width=153) (actual time=0.006..0.044 rows=51.00 loops=1) Index Cond: ((chat_id = '8b619ecf-de9e-4043-8b20-40346656f7ba'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:22:09.183192+00'::timestamp with time zone, '6c079c72-956b-4d22-902d-50525cd17715'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 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.063 ms Execution Time: 0.054 ms ### bg 100: first, после 0 #### psql Limit (cost=0.29..151.02 rows=51 width=153) (actual time=0.005..0.050 rows=51.00 loops=1) Buffers: shared hit=48 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..295.85 rows=100 width=153) (actual time=0.005..0.047 rows=51.00 loops=1) Index Cond: (chat_id = '2c89c970-25a2-4db8-be56-14a7e9aa8283'::uuid) Index Searches: 1 Buffers: shared hit=48 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.044 ms Execution Time: 0.055 ms #### generic Limit (cost=0.29..53.03 rows=21 width=153) (actual time=0.012..0.061 rows=51.00 loops=1) Buffers: shared hit=48 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..537.76 rows=214 width=153) (actual time=0.011..0.058 rows=51.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=48 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.005 ms Execution Time: 0.069 ms #### no-index Limit (cost=466.57..466.70 rows=51 width=153) (actual time=0.394..0.397 rows=51.00 loops=1) Buffers: shared hit=293 -> Sort (cost=466.57..466.82 rows=100 width=153) (actual time=0.393..0.395 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=293 -> Seq Scan on messages (cost=0.00..463.25 rows=100 width=153) (actual time=0.007..0.376 rows=100.00 loops=1) Filter: (chat_id = '2c89c970-25a2-4db8-be56-14a7e9aa8283'::uuid) Rows Removed by Filter: 13400 Buffers: shared hit=293 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.037 ms Execution Time: 0.402 ms #### chat_only Limit (cost=215.36..215.48 rows=51 width=153) (actual time=0.110..0.114 rows=51.00 loops=1) Buffers: shared hit=91 -> Sort (cost=215.36..215.61 rows=100 width=153) (actual time=0.110..0.112 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=91 -> Bitmap Heap Scan on messages (cost=5.06..212.03 rows=100 width=153) (actual time=0.011..0.097 rows=100.00 loops=1) Recheck Cond: (chat_id = '2c89c970-25a2-4db8-be56-14a7e9aa8283'::uuid) Heap Blocks: exact=89 Buffers: shared hit=91 -> Bitmap Index Scan on bench_variant (cost=0.00..5.04 rows=100 width=0) (actual time=0.004..0.004 rows=100.00 loops=1) Index Cond: (chat_id = '2c89c970-25a2-4db8-be56-14a7e9aa8283'::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.120 ms #### covering Limit (cost=0.41..6.15 rows=51 width=153) (actual time=0.005..0.042 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.41..11.66 rows=100 width=153) (actual time=0.005..0.040 rows=51.00 loops=1) Index Cond: (chat_id = '2c89c970-25a2-4db8-be56-14a7e9aa8283'::uuid) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 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.038 ms Execution Time: 0.049 ms ### bg 10: first, после 0 #### psql Limit (cost=0.29..38.25 rows=10 width=153) (actual time=0.008..0.018 rows=10.00 loops=1) Buffers: shared hit=12 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..38.25 rows=10 width=153) (actual time=0.008..0.017 rows=10.00 loops=1) Index Cond: (chat_id = 'd96f0cdc-62d0-4949-b4e8-9f94e470ece5'::uuid) 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: Buffers: shared hit=3 Planning Time: 0.061 ms Execution Time: 0.026 ms #### generic Limit (cost=0.29..53.03 rows=21 width=153) (actual time=0.013..0.026 rows=10.00 loops=1) Buffers: shared hit=12 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..537.76 rows=214 width=153) (actual time=0.013..0.024 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.006 ms Execution Time: 0.035 ms #### no-index Limit (cost=462.07..462.09 rows=10 width=153) (actual time=0.307..0.308 rows=10.00 loops=1) Buffers: shared hit=293 -> Sort (cost=462.07..462.09 rows=10 width=153) (actual time=0.307..0.307 rows=10.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 27kB Buffers: shared hit=293 -> Seq Scan on messages (cost=0.00..461.90 rows=10 width=153) (actual time=0.017..0.294 rows=10.00 loops=1) Filter: (chat_id = 'd96f0cdc-62d0-4949-b4e8-9f94e470ece5'::uuid) Rows Removed by Filter: 13490 Buffers: shared hit=293 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.313 ms #### chat_only Limit (cost=33.48..33.50 rows=10 width=153) (actual time=0.019..0.021 rows=10.00 loops=1) Buffers: shared hit=12 -> Sort (cost=33.48..33.50 rows=10 width=153) (actual time=0.019..0.020 rows=10.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 27kB Buffers: shared hit=12 -> Index Scan using bench_variant on messages (cost=0.29..33.31 rows=10 width=153) (actual time=0.006..0.017 rows=10.00 loops=1) Index Cond: (chat_id = 'd96f0cdc-62d0-4949-b4e8-9f94e470ece5'::uuid) 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: Buffers: shared hit=3 Planning Time: 0.066 ms Execution Time: 0.030 ms #### covering Limit (cost=0.41..4.74 rows=10 width=153) (actual time=0.005..0.012 rows=10.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.41..4.74 rows=10 width=153) (actual time=0.005..0.012 rows=10.00 loops=1) Index Cond: (chat_id = 'd96f0cdc-62d0-4949-b4e8-9f94e470ece5'::uuid) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 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.040 ms Execution Time: 0.017 ms