# План запроса истории: размер 1k Commit b2fb88f Читаемый чат — 1000 сообщений; фоновые чаты: 2 × 1k, 10 × 100, 50 × 10. Страница — `limit` профиля; «после» — сколько сообщений чата до страницы. ## Планы приложения auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план, с которыми его выполнило приложение (prepared statement asyncpg). ### read 1k: first, после 0 duration: 0.112 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 = '86fab098-90d6-41f8-8651-e1a1d894b47f', $2 = '51' Limit (cost=0.28..24.54 rows=51 width=153) (actual time=0.021..0.080 rows=51.00 loops=1) Buffers: shared hit=38 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..475.91 rows=1000 width=153) (actual time=0.020..0.076 rows=51.00 loops=1) Index Cond: (chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::uuid) Index Searches: 1 Buffers: shared hit=38 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.109 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 = '86fab098-90d6-41f8-8651-e1a1d894b47f', $2 = '2026-10-31 20:32:35.560579+00', $3 = '56b675e9-b197-40df-b5bf-70da08443b02', $4 = '51' Limit (cost=0.28..44.89 rows=51 width=153) (actual time=0.012..0.073 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..436.74 rows=499 width=153) (actual time=0.011..0.070 rows=51.00 loops=1) Index Cond: ((chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:35.560579+00'::timestamp with time zone, '56b675e9-b197-40df-b5bf-70da08443b02'::uuid))) 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) ### read 1k: last, после 900 duration: 0.190 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 = '86fab098-90d6-41f8-8651-e1a1d894b47f', $2 = '2026-10-31 20:32:38.6944+00', $3 = '15f6b21b-4c28-40a8-b298-28397cbf0a2e', $4 = '51' Limit (cost=119.82..119.95 rows=51 width=153) (actual time=0.153..0.157 rows=51.00 loops=1) Buffers: shared hit=27 -> Sort (cost=119.82..120.08 rows=102 width=153) (actual time=0.152..0.154 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=27 -> Bitmap Heap Scan on messages (cost=9.33..116.42 rows=102 width=153) (actual time=0.019..0.115 rows=100.00 loops=1) Recheck Cond: ((chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:38.6944+00'::timestamp with time zone, '15f6b21b-4c28-40a8-b298-28397cbf0a2e'::uuid))) Heap Blocks: exact=18 Buffers: shared hit=21 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..9.30 rows=102 width=0) (actual time=0.009..0.009 rows=100.00 loops=1) Index Cond: ((chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:38.6944+00'::timestamp with time zone, '15f6b21b-4c28-40a8-b298-28397cbf0a2e'::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=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.097 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 = '4a92e036-81de-4387-8577-bca03fd549a7', $2 = '51' Limit (cost=0.28..24.54 rows=51 width=153) (actual time=0.010..0.068 rows=51.00 loops=1) Buffers: shared hit=43 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..475.91 rows=1000 width=153) (actual time=0.010..0.066 rows=51.00 loops=1) Index Cond: (chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::uuid) Index Searches: 1 Buffers: shared hit=43 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.096 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 = '4a92e036-81de-4387-8577-bca03fd549a7', $2 = '2026-10-31 20:32:35.544798+00', $3 = 'a727e0c7-e3cb-4ce8-bff7-59e9c11b7c6f', $4 = '51' Limit (cost=0.28..44.72 rows=51 width=153) (actual time=0.011..0.066 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..436.81 rows=501 width=153) (actual time=0.010..0.063 rows=51.00 loops=1) Index Cond: ((chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:35.544798+00'::timestamp with time zone, 'a727e0c7-e3cb-4ce8-bff7-59e9c11b7c6f'::uuid))) Index Searches: 1 Buffers: shared hit=53 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) ### bg 1k: last, после 900 duration: 0.145 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 = '4a92e036-81de-4387-8577-bca03fd549a7', $2 = '2026-10-31 20:32:38.721445+00', $3 = 'a1f7ba6d-d70d-485c-ba73-68124a4f887b', $4 = '51' Limit (cost=118.86..118.98 rows=51 width=153) (actual time=0.112..0.116 rows=51.00 loops=1) Buffers: shared hit=19 -> Sort (cost=118.86..119.10 rows=98 width=153) (actual time=0.112..0.114 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.29..115.62 rows=98 width=153) (actual time=0.013..0.093 rows=100.00 loops=1) Recheck Cond: ((chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:38.721445+00'::timestamp with time zone, 'a1f7ba6d-d70d-485c-ba73-68124a4f887b'::uuid))) Heap Blocks: exact=17 Buffers: shared hit=19 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..9.26 rows=98 width=0) (actual time=0.007..0.007 rows=100.00 loops=1) Index Cond: ((chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:38.721445+00'::timestamp with time zone, 'a1f7ba6d-d70d-485c-ba73-68124a4f887b'::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.197 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 = '20a79c04-8e44-4239-98e4-6e42867bb00b', $2 = '51' Limit (cost=118.60..118.73 rows=51 width=153) (actual time=0.163..0.167 rows=51.00 loops=1) Buffers: shared hit=73 -> Sort (cost=118.60..118.85 rows=100 width=153) (actual time=0.162..0.164 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=73 -> Bitmap Heap Scan on messages (cost=9.06..115.28 rows=100 width=153) (actual time=0.023..0.141 rows=100.00 loops=1) Recheck Cond: (chat_id = '20a79c04-8e44-4239-98e4-6e42867bb00b'::uuid) Heap Blocks: exact=70 Buffers: shared hit=73 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..9.03 rows=100 width=0) (actual time=0.010..0.010 rows=100.00 loops=1) Index Cond: (chat_id = '20a79c04-8e44-4239-98e4-6e42867bb00b'::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=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.036 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 = 'ed5c39bf-15e3-48b5-bb93-a93544b7da4a', $2 = '51' Limit (cost=35.31..35.34 rows=10 width=153) (actual time=0.031..0.032 rows=10.00 loops=1) Buffers: shared hit=12 -> Sort (cost=35.31..35.34 rows=10 width=153) (actual time=0.031..0.031 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.15 rows=10 width=153) (actual time=0.013..0.028 rows=10.00 loops=1) Recheck Cond: (chat_id = 'ed5c39bf-15e3-48b5-bb93-a93544b7da4a'::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.005..0.005 rows=10.00 loops=1) Index Cond: (chat_id = 'ed5c39bf-15e3-48b5-bb93-a93544b7da4a'::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..44.89 rows=51 width=153) (actual time=0.007..0.060 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..436.74 rows=499 width=153) (actual time=0.007..0.056 rows=51.00 loops=1) Index Cond: ((chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:35.560579+00'::timestamp with time zone, '56b675e9-b197-40df-b5bf-70da08443b02'::uuid))) 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: Buffers: shared hit=3 Planning Time: 0.057 ms Execution Time: 0.067 ms #### generic Limit (cost=0.28..7.93 rows=2 width=153) (actual time=0.015..0.066 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..92.03 rows=24 width=153) (actual time=0.014..0.063 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.010 ms Execution Time: 0.079 ms #### or-cursor Limit (cost=0.28..48.89 rows=51 width=153) (actual time=0.082..0.126 rows=51.00 loops=1) Buffers: shared hit=533 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..475.90 rows=499 width=153) (actual time=0.081..0.123 rows=51.00 loops=1) Index Cond: (chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::uuid) Filter: ((created_at > '2026-10-31 20:32:35.560579+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:32:35.560579+00'::timestamp with time zone) AND (id > '56b675e9-b197-40df-b5bf-70da08443b02'::uuid))) Rows Removed by Filter: 500 Index Searches: 1 Buffers: shared hit=533 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.049 ms Execution Time: 0.132 ms #### no-index Limit (cost=202.88..203.01 rows=51 width=153) (actual time=0.572..0.576 rows=51.00 loops=1) Buffers: shared hit=100 -> Sort (cost=202.88..204.13 rows=499 width=153) (actual time=0.572..0.574 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 42kB Buffers: shared hit=100 -> Seq Scan on messages (cost=0.00..186.24 rows=499 width=153) (actual time=0.075..0.510 rows=500.00 loops=1) Filter: ((chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:35.560579+00'::timestamp with time zone, '56b675e9-b197-40df-b5bf-70da08443b02'::uuid))) Rows Removed by Filter: 4000 Buffers: shared hit=100 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.040 ms Execution Time: 0.582 ms #### chat_only Limit (cost=157.54..157.67 rows=51 width=153) (actual time=0.493..0.497 rows=51.00 loops=1) Buffers: shared hit=103 -> Sort (cost=157.54..158.79 rows=499 width=153) (actual time=0.493..0.495 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 42kB Buffers: shared hit=103 -> Bitmap Heap Scan on messages (cost=15.91..140.89 rows=499 width=153) (actual time=0.063..0.432 rows=500.00 loops=1) Recheck Cond: (chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-31 20:32:35.560579+00'::timestamp with time zone, '56b675e9-b197-40df-b5bf-70da08443b02'::uuid)) Rows Removed by Filter: 500 Heap Blocks: exact=100 Buffers: shared hit=103 -> Bitmap Index Scan on bench_variant (cost=0.00..15.78 rows=1000 width=0) (actual time=0.019..0.019 rows=1000.00 loops=1) Index Cond: (chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::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.037 ms Execution Time: 0.503 ms #### covering Limit (cost=0.28..6.16 rows=51 width=153) (actual time=0.005..0.045 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.042 rows=51.00 loops=1) Index Cond: ((chat_id = '86fab098-90d6-41f8-8651-e1a1d894b47f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:35.560579+00'::timestamp with time zone, '56b675e9-b197-40df-b5bf-70da08443b02'::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.042 ms Execution Time: 0.051 ms ### bg 1k: middle, после 500 #### psql Limit (cost=0.28..44.72 rows=51 width=153) (actual time=0.012..0.062 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..436.81 rows=501 width=153) (actual time=0.012..0.058 rows=51.00 loops=1) Index Cond: ((chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:35.544798+00'::timestamp with time zone, 'a727e0c7-e3cb-4ce8-bff7-59e9c11b7c6f'::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.058 ms Execution Time: 0.072 ms #### generic Limit (cost=0.28..7.93 rows=2 width=153) (actual time=0.010..0.056 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..92.03 rows=24 width=153) (actual time=0.009..0.053 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.006 ms Execution Time: 0.063 ms #### or-cursor Limit (cost=0.28..48.70 rows=51 width=153) (actual time=0.076..0.116 rows=51.00 loops=1) Buffers: shared hit=540 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..475.93 rows=501 width=153) (actual time=0.076..0.114 rows=51.00 loops=1) Index Cond: (chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::uuid) Filter: ((created_at > '2026-10-31 20:32:35.544798+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:32:35.544798+00'::timestamp with time zone) AND (id > 'a727e0c7-e3cb-4ce8-bff7-59e9c11b7c6f'::uuid))) Rows Removed by Filter: 500 Index Searches: 1 Buffers: shared hit=540 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.121 ms #### no-index Limit (cost=202.98..203.11 rows=51 width=153) (actual time=0.486..0.490 rows=51.00 loops=1) Buffers: shared hit=100 -> Sort (cost=202.98..204.23 rows=501 width=153) (actual time=0.486..0.488 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 43kB Buffers: shared hit=100 -> Seq Scan on messages (cost=0.00..186.27 rows=501 width=153) (actual time=0.070..0.431 rows=500.00 loops=1) Filter: ((chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:35.544798+00'::timestamp with time zone, 'a727e0c7-e3cb-4ce8-bff7-59e9c11b7c6f'::uuid))) Rows Removed by Filter: 4000 Buffers: shared hit=100 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.043 ms Execution Time: 0.497 ms #### chat_only Limit (cost=157.64..157.76 rows=51 width=153) (actual time=0.436..0.440 rows=51.00 loops=1) Buffers: shared hit=103 -> Sort (cost=157.64..158.89 rows=501 width=153) (actual time=0.436..0.437 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: top-N heapsort Memory: 43kB Buffers: shared hit=103 -> Bitmap Heap Scan on messages (cost=15.91..140.92 rows=501 width=153) (actual time=0.053..0.382 rows=500.00 loops=1) Recheck Cond: (chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-31 20:32:35.544798+00'::timestamp with time zone, 'a727e0c7-e3cb-4ce8-bff7-59e9c11b7c6f'::uuid)) Rows Removed by Filter: 500 Heap Blocks: exact=100 Buffers: shared hit=103 -> Bitmap Index Scan on bench_variant (cost=0.00..15.78 rows=1000 width=0) (actual time=0.012..0.012 rows=1000.00 loops=1) Index Cond: (chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::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.045 ms Execution Time: 0.446 ms #### covering Limit (cost=0.28..6.14 rows=51 width=153) (actual time=0.006..0.045 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.006..0.043 rows=51.00 loops=1) Index Cond: ((chat_id = '4a92e036-81de-4387-8577-bca03fd549a7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:32:35.544798+00'::timestamp with time zone, 'a727e0c7-e3cb-4ce8-bff7-59e9c11b7c6f'::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.052 ms ### bg 100: first, после 0 #### psql Limit (cost=118.60..118.73 rows=51 width=153) (actual time=0.118..0.123 rows=51.00 loops=1) Buffers: shared hit=73 -> Sort (cost=118.60..118.85 rows=100 width=153) (actual time=0.118..0.120 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=73 -> Bitmap Heap Scan on messages (cost=9.06..115.28 rows=100 width=153) (actual time=0.022..0.103 rows=100.00 loops=1) Recheck Cond: (chat_id = '20a79c04-8e44-4239-98e4-6e42867bb00b'::uuid) Heap Blocks: exact=70 Buffers: shared hit=73 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..9.03 rows=100 width=0) (actual time=0.010..0.010 rows=100.00 loops=1) Index Cond: (chat_id = '20a79c04-8e44-4239-98e4-6e42867bb00b'::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=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.064 ms Execution Time: 0.136 ms #### generic Limit (cost=0.28..21.54 rows=7 width=153) (actual time=0.009..0.050 rows=51.00 loops=1) Buffers: shared hit=47 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.28..215.90 rows=71 width=153) (actual time=0.008..0.046 rows=51.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=47 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 Time: 0.004 ms Execution Time: 0.057 ms #### no-index Limit (cost=161.07..161.20 rows=51 width=153) (actual time=0.196..0.200 rows=51.00 loops=1) Buffers: shared hit=100 -> Sort (cost=161.07..161.32 rows=100 width=153) (actual time=0.196..0.197 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=100 -> Seq Scan on messages (cost=0.00..157.75 rows=100 width=153) (actual time=0.004..0.182 rows=100.00 loops=1) Filter: (chat_id = '20a79c04-8e44-4239-98e4-6e42867bb00b'::uuid) Rows Removed by Filter: 4400 Buffers: shared hit=100 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.038 ms Execution Time: 0.206 ms #### chat_only Limit (cost=114.60..114.73 rows=51 width=153) (actual time=0.107..0.111 rows=51.00 loops=1) Buffers: shared hit=72 -> Sort (cost=114.60..114.85 rows=100 width=153) (actual time=0.107..0.109 rows=51.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 50kB Buffers: shared hit=72 -> Bitmap Heap Scan on messages (cost=5.06..111.28 rows=100 width=153) (actual time=0.014..0.093 rows=100.00 loops=1) Recheck Cond: (chat_id = '20a79c04-8e44-4239-98e4-6e42867bb00b'::uuid) Heap Blocks: exact=70 Buffers: shared hit=72 -> Bitmap Index Scan on bench_variant (cost=0.00..5.03 rows=100 width=0) (actual time=0.005..0.005 rows=100.00 loops=1) Index Cond: (chat_id = '20a79c04-8e44-4239-98e4-6e42867bb00b'::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.054 ms Execution Time: 0.119 ms #### covering Limit (cost=0.28..6.02 rows=51 width=153) (actual time=0.005..0.040 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.005..0.038 rows=51.00 loops=1) Index Cond: (chat_id = '20a79c04-8e44-4239-98e4-6e42867bb00b'::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.038 ms Execution Time: 0.047 ms ### bg 10: first, после 0 #### psql Limit (cost=35.31..35.34 rows=10 width=153) (actual time=0.020..0.021 rows=10.00 loops=1) Buffers: shared hit=12 -> Sort (cost=35.31..35.34 rows=10 width=153) (actual time=0.020..0.020 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.15 rows=10 width=153) (actual time=0.009..0.018 rows=10.00 loops=1) Recheck Cond: (chat_id = 'ed5c39bf-15e3-48b5-bb93-a93544b7da4a'::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 = 'ed5c39bf-15e3-48b5-bb93-a93544b7da4a'::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=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.042 ms Execution Time: 0.031 ms #### generic Limit (cost=0.28..21.54 rows=7 width=153) (actual time=0.009..0.020 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.90 rows=71 width=153) (actual time=0.008..0.019 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.004 ms Execution Time: 0.026 ms #### no-index Limit (cost=156.57..156.59 rows=10 width=153) (actual time=0.127..0.128 rows=10.00 loops=1) Buffers: shared hit=100 -> Sort (cost=156.57..156.59 rows=10 width=153) (actual time=0.126..0.127 rows=10.00 loops=1) Sort Key: messages.created_at, messages.id Sort Method: quicksort Memory: 27kB Buffers: shared hit=100 -> Seq Scan on messages (cost=0.00..156.40 rows=10 width=153) (actual time=0.008..0.124 rows=10.00 loops=1) Filter: (chat_id = 'ed5c39bf-15e3-48b5-bb93-a93544b7da4a'::uuid) Rows Removed by Filter: 4490 Buffers: shared hit=100 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.033 ms Execution Time: 0.132 ms #### chat_only Limit (cost=35.31..35.34 rows=10 width=153) (actual time=0.021..0.022 rows=10.00 loops=1) Buffers: shared hit=12 -> Sort (cost=35.31..35.34 rows=10 width=153) (actual time=0.020..0.021 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.15 rows=10 width=153) (actual time=0.009..0.018 rows=10.00 loops=1) Recheck Cond: (chat_id = 'ed5c39bf-15e3-48b5-bb93-a93544b7da4a'::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 = 'ed5c39bf-15e3-48b5-bb93-a93544b7da4a'::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.050 ms Execution Time: 0.030 ms #### covering Limit (cost=0.28..4.61 rows=10 width=153) (actual time=0.005..0.013 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.012 rows=10.00 loops=1) Index Cond: (chat_id = 'ed5c39bf-15e3-48b5-bb93-a93544b7da4a'::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.035 ms Execution Time: 0.018 ms