# План запроса истории: размер 10k Commit 67be41e Читаемый чат — 10000 сообщений; фоновые чаты: 10 × 5k, 100 × 400, 1000 × 10. Страница — `limit` профиля; «после» — сколько сообщений чата до страницы. ## Планы приложения auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план, с которыми его выполнило приложение (prepared statement asyncpg). ### read 10k: first, после 0 duration: 0.068 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at FROM messages WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = '23ca9f41-d045-47cd-91af-e5a122c973fb', $2 = '51' Limit (cost=0.42..48.05 rows=51 width=121) (actual time=0.017..0.053 rows=51.00 loops=1) Buffers: shared hit=46 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..9164.44 rows=9812 width=121) (actual time=0.017..0.049 rows=51.00 loops=1) Index Cond: (chat_id = '23ca9f41-d045-47cd-91af-e5a122c973fb'::uuid) Index Searches: 1 Buffers: shared hit=46 ### read 10k: middle, после 5000 duration: 0.063 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at 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 = '23ca9f41-d045-47cd-91af-e5a122c973fb', $2 = '2026-10-02 01:27:07.615518+00', $3 = 'adbc37b6-306b-46cf-b1c8-ee85bed999a6', $4 = '51' Limit (cost=0.42..12.83 rows=3 width=121) (actual time=0.017..0.050 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..141.04 rows=34 width=121) (actual time=0.017..0.046 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 ### read 10k: last, после 9900 duration: 0.057 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at 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 = '23ca9f41-d045-47cd-91af-e5a122c973fb', $2 = '2026-10-02 01:28:06.131929+00', $3 = '2648fddc-da75-4218-bf26-b984c758a0c0', $4 = '51' Limit (cost=0.42..12.83 rows=3 width=121) (actual time=0.011..0.044 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..141.04 rows=34 width=121) (actual time=0.011..0.041 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 ### bg 5k: first, после 0 duration: 0.066 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at FROM messages WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = '5a34e263-dc69-4528-a6cb-b920d0f636a6', $2 = '51' Limit (cost=0.42..90.18 rows=51 width=121) (actual time=0.013..0.053 rows=51.00 loops=1) Buffers: shared hit=50 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..8848.66 rows=5027 width=121) (actual time=0.012..0.050 rows=51.00 loops=1) Index Cond: (chat_id = '5a34e263-dc69-4528-a6cb-b920d0f636a6'::uuid) Index Searches: 1 Buffers: shared hit=50 ### bg 5k: middle, после 2500 duration: 0.053 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at 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 = '5a34e263-dc69-4528-a6cb-b920d0f636a6', $2 = '2026-10-02 01:27:07.604183+00', $3 = '5e7a7fa4-7226-4560-b538-a15fefcc5218', $4 = '51' Limit (cost=0.42..12.83 rows=3 width=121) (actual time=0.010..0.041 rows=51.00 loops=1) Buffers: shared hit=51 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..141.04 rows=34 width=121) (actual time=0.010..0.038 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=51 ### bg 5k: last, после 4900 duration: 0.069 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at 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 = '5a34e263-dc69-4528-a6cb-b920d0f636a6', $2 = '2026-10-02 01:28:05.078473+00', $3 = '2c62b01e-3ea0-4d96-81ee-30fabb06c725', $4 = '51' Limit (cost=0.42..12.83 rows=3 width=121) (actual time=0.010..0.055 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..141.04 rows=34 width=121) (actual time=0.010..0.052 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 ### bg 400: first, после 0 duration: 0.081 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at FROM messages WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = '95e22f46-dc69-4c05-b02d-127fd0abb8fd', $2 = '51' Limit (cost=0.42..192.30 rows=51 width=121) (actual time=0.014..0.071 rows=51.00 loops=1) Buffers: shared hit=54 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..1366.18 rows=363 width=121) (actual time=0.014..0.068 rows=51.00 loops=1) Index Cond: (chat_id = '95e22f46-dc69-4c05-b02d-127fd0abb8fd'::uuid) Index Searches: 1 Buffers: shared hit=54 ### bg 400: middle, после 200 duration: 0.077 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at 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 = '95e22f46-dc69-4c05-b02d-127fd0abb8fd', $2 = '2026-10-02 01:27:07.406365+00', $3 = 'f15df963-0361-4789-9e7e-47767c6064fe', $4 = '51' Limit (cost=0.42..12.83 rows=3 width=121) (actual time=0.014..0.065 rows=51.00 loops=1) Buffers: shared hit=51 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..141.04 rows=34 width=121) (actual time=0.013..0.061 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=51 ### bg 400: last, после 300 duration: 0.081 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at 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 = '95e22f46-dc69-4c05-b02d-127fd0abb8fd', $2 = '2026-10-02 01:27:36.432901+00', $3 = 'e450612d-cb2f-4d0c-85fa-43ad892afaad', $4 = '51' Limit (cost=0.42..12.83 rows=3 width=121) (actual time=0.013..0.068 rows=51.00 loops=1) Buffers: shared hit=54 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..141.04 rows=34 width=121) (actual time=0.012..0.065 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 ### bg 10: first, после 0 duration: 0.034 ms plan: Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at FROM messages WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = '65833eb1-1e98-47e6-8955-8098e7434a4f', $2 = '51' Limit (cost=57.56..57.60 rows=14 width=121) (actual time=0.030..0.031 rows=10.00 loops=1) Buffers: shared hit=13 -> Sort (cost=57.56..57.60 rows=14 width=121) (actual time=0.030..0.030 rows=10.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 27kB Buffers: shared hit=13 -> Bitmap Heap Scan on messages (cost=4.53..57.30 rows=14 width=121) (actual time=0.013..0.025 rows=10.00 loops=1) Recheck Cond: (chat_id = '65833eb1-1e98-47e6-8955-8098e7434a4f'::uuid) Heap Blocks: exact=10 Buffers: shared hit=13 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..4.52 rows=14 width=0) (actual time=0.008..0.008 rows=10.00 loops=1) Index Cond: (chat_id = '65833eb1-1e98-47e6-8955-8098e7434a4f'::uuid) Index Searches: 1 Buffers: shared hit=3 ## Страницы сравнения в 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.42..92.66 rows=51 width=121) (actual time=0.005..0.020 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..8850.54 rows=4893 width=121) (actual time=0.005..0.017 rows=51.00 loops=1) Index Cond: ((chat_id = '23ca9f41-d045-47cd-91af-e5a122c973fb'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:07.615518+00'::timestamp with time zone, 'adbc37b6-306b-46cf-b1c8-ee85bed999a6'::uuid))) Index Searches: 1 Buffers: shared hit=53 Planning Time: 0.034 ms Execution Time: 0.027 ms #### generic Limit (cost=0.42..12.83 rows=3 width=121) (actual time=0.006..0.020 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..141.04 rows=34 width=121) (actual time=0.005..0.017 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 Planning Time: 0.005 ms Execution Time: 0.026 ms #### or-cursor Limit (cost=0.42..96.70 rows=51 width=121) (actual time=0.826..0.840 rows=51.00 loops=1) Buffers: shared hit=4979 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..9238.03 rows=4893 width=121) (actual time=0.826..0.836 rows=51.00 loops=1) Index Cond: (chat_id = '23ca9f41-d045-47cd-91af-e5a122c973fb'::uuid) Filter: ((created_at > '2026-10-02 01:27:07.615518+00'::timestamp with time zone) OR ((created_at = '2026-10-02 01:27:07.615518+00'::timestamp with time zone) AND (id > 'adbc37b6-306b-46cf-b1c8-ee85bed999a6'::uuid))) Rows Removed by Filter: 5000 Index Searches: 1 Buffers: shared hit=4979 Planning Time: 0.058 ms Execution Time: 0.847 ms #### no-index Limit (cost=4217.24..4217.37 rows=51 width=121) (actual time=4.010..4.015 rows=51.00 loops=1) Buffers: shared hit=2129 -> Sort (cost=4217.24..4229.47 rows=4893 width=121) (actual time=4.009..4.012 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 51kB Buffers: shared hit=2129 -> Seq Scan on messages (cost=0.00..4054.00 rows=4893 width=121) (actual time=0.785..3.686 rows=5000.00 loops=1) Filter: ((chat_id = '23ca9f41-d045-47cd-91af-e5a122c973fb'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:07.615518+00'::timestamp with time zone, 'adbc37b6-306b-46cf-b1c8-ee85bed999a6'::uuid))) Rows Removed by Filter: 105000 Buffers: shared hit=2129 Planning Time: 0.043 ms Execution Time: 4.023 ms #### chat_only Limit (cost=2579.06..2579.18 rows=51 width=121) (actual time=2.005..2.009 rows=51.00 loops=1) Buffers: shared hit=2036 -> Sort (cost=2579.06..2591.29 rows=4893 width=121) (actual time=2.004..2.006 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 51kB Buffers: shared hit=2036 -> Bitmap Heap Scan on messages (cost=115.11..2415.82 rows=4893 width=121) (actual time=0.711..1.723 rows=5000.00 loops=1) Recheck Cond: (chat_id = '23ca9f41-d045-47cd-91af-e5a122c973fb'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-02 01:27:07.615518+00'::timestamp with time zone, 'adbc37b6-306b-46cf-b1c8-ee85bed999a6'::uuid)) Rows Removed by Filter: 5000 Heap Blocks: exact=2025 Buffers: shared hit=2036 -> Bitmap Index Scan on bench_variant (cost=0.00..113.88 rows=9812 width=0) (actual time=0.268..0.268 rows=10000.00 loops=1) Index Cond: (chat_id = '23ca9f41-d045-47cd-91af-e5a122c973fb'::uuid) Index Searches: 1 Buffers: shared hit=11 Planning Time: 0.059 ms Execution Time: 2.017 ms #### covering Limit (cost=0.42..5.40 rows=51 width=121) (actual time=0.004..0.010 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.42..478.28 rows=4893 width=121) (actual time=0.004..0.007 rows=51.00 loops=1) Index Cond: ((chat_id = '23ca9f41-d045-47cd-91af-e5a122c973fb'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:07.615518+00'::timestamp with time zone, 'adbc37b6-306b-46cf-b1c8-ee85bed999a6'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.045 ms Execution Time: 0.016 ms ### bg 5k: middle, после 2500 #### psql Limit (cost=0.42..132.31 rows=51 width=121) (actual time=0.004..0.018 rows=51.00 loops=1) Buffers: shared hit=51 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..6483.78 rows=2507 width=121) (actual time=0.004..0.015 rows=51.00 loops=1) Index Cond: ((chat_id = '5a34e263-dc69-4528-a6cb-b920d0f636a6'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:07.604183+00'::timestamp with time zone, '5e7a7fa4-7226-4560-b538-a15fefcc5218'::uuid))) Index Searches: 1 Buffers: shared hit=51 Planning Time: 0.044 ms Execution Time: 0.025 ms #### generic Limit (cost=0.42..12.83 rows=3 width=121) (actual time=0.006..0.019 rows=51.00 loops=1) Buffers: shared hit=51 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..141.04 rows=34 width=121) (actual time=0.005..0.017 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=51 Planning Time: 0.007 ms Execution Time: 0.027 ms #### or-cursor Limit (cost=0.42..181.18 rows=51 width=121) (actual time=0.345..0.356 rows=51.00 loops=1) Buffers: shared hit=2427 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..8886.36 rows=2507 width=121) (actual time=0.345..0.352 rows=51.00 loops=1) Index Cond: (chat_id = '5a34e263-dc69-4528-a6cb-b920d0f636a6'::uuid) Filter: ((created_at > '2026-10-02 01:27:07.604183+00'::timestamp with time zone) OR ((created_at = '2026-10-02 01:27:07.604183+00'::timestamp with time zone) AND (id > '5e7a7fa4-7226-4560-b538-a15fefcc5218'::uuid))) Rows Removed by Filter: 2500 Index Searches: 1 Buffers: shared hit=2427 Planning Time: 0.034 ms Execution Time: 0.359 ms #### no-index Limit (cost=4137.64..4137.77 rows=51 width=121) (actual time=3.155..3.160 rows=51.00 loops=1) Buffers: shared hit=2129 -> Sort (cost=4137.64..4143.91 rows=2507 width=121) (actual time=3.155..3.157 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 46kB Buffers: shared hit=2129 -> Seq Scan on messages (cost=0.00..4054.00 rows=2507 width=121) (actual time=0.648..3.021 rows=2500.00 loops=1) Filter: ((chat_id = '5a34e263-dc69-4528-a6cb-b920d0f636a6'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:07.604183+00'::timestamp with time zone, '5e7a7fa4-7226-4560-b538-a15fefcc5218'::uuid))) Rows Removed by Filter: 107500 Buffers: shared hit=2129 Planning Time: 0.028 ms Execution Time: 3.167 ms #### chat_only Limit (cost=2359.23..2359.36 rows=51 width=121) (actual time=1.080..1.084 rows=51.00 loops=1) Buffers: shared hit=1876 -> Sort (cost=2359.23..2365.50 rows=2507 width=121) (actual time=1.080..1.082 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 46kB Buffers: shared hit=1876 -> Bitmap Heap Scan on messages (cost=58.62..2275.59 rows=2507 width=121) (actual time=0.405..0.940 rows=2500.00 loops=1) Recheck Cond: (chat_id = '5a34e263-dc69-4528-a6cb-b920d0f636a6'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-02 01:27:07.604183+00'::timestamp with time zone, '5e7a7fa4-7226-4560-b538-a15fefcc5218'::uuid)) Rows Removed by Filter: 2500 Heap Blocks: exact=1870 Buffers: shared hit=1876 -> Bitmap Index Scan on bench_variant (cost=0.00..57.99 rows=5027 width=0) (actual time=0.149..0.149 rows=5000.00 loops=1) Index Cond: (chat_id = '5a34e263-dc69-4528-a6cb-b920d0f636a6'::uuid) Index Searches: 1 Buffers: shared hit=6 Planning Time: 0.054 ms Execution Time: 1.092 ms #### covering Limit (cost=0.42..5.42 rows=51 width=121) (actual time=0.004..0.011 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.42..246.56 rows=2507 width=121) (actual time=0.004..0.008 rows=51.00 loops=1) Index Cond: ((chat_id = '5a34e263-dc69-4528-a6cb-b920d0f636a6'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:07.604183+00'::timestamp with time zone, '5e7a7fa4-7226-4560-b538-a15fefcc5218'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.053 ms Execution Time: 0.017 ms ### bg 400: middle, после 200 #### psql Limit (cost=0.42..200.87 rows=51 width=121) (actual time=0.004..0.016 rows=51.00 loops=1) Buffers: shared hit=51 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..715.75 rows=182 width=121) (actual time=0.003..0.014 rows=51.00 loops=1) Index Cond: ((chat_id = '95e22f46-dc69-4c05-b02d-127fd0abb8fd'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:07.406365+00'::timestamp with time zone, 'f15df963-0361-4789-9e7e-47767c6064fe'::uuid))) Index Searches: 1 Buffers: shared hit=51 Planning Time: 0.032 ms Execution Time: 0.021 ms #### generic Limit (cost=0.42..12.83 rows=3 width=121) (actual time=0.004..0.019 rows=51.00 loops=1) Buffers: shared hit=51 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..141.04 rows=34 width=121) (actual time=0.004..0.016 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=51 Planning Time: 0.005 ms Execution Time: 0.024 ms #### or-cursor Limit (cost=0.42..383.89 rows=51 width=121) (actual time=0.048..0.060 rows=51.00 loops=1) Buffers: shared hit=249 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..1368.90 rows=182 width=121) (actual time=0.048..0.057 rows=51.00 loops=1) Index Cond: (chat_id = '95e22f46-dc69-4c05-b02d-127fd0abb8fd'::uuid) Filter: ((created_at > '2026-10-02 01:27:07.406365+00'::timestamp with time zone) OR ((created_at = '2026-10-02 01:27:07.406365+00'::timestamp with time zone) AND (id > 'f15df963-0361-4789-9e7e-47767c6064fe'::uuid))) Rows Removed by Filter: 200 Index Searches: 1 Buffers: shared hit=249 Planning Time: 0.034 ms Execution Time: 0.065 ms #### no-index Limit (cost=4060.07..4060.20 rows=51 width=121) (actual time=2.826..2.830 rows=51.00 loops=1) Buffers: shared hit=2129 -> Sort (cost=4060.07..4060.53 rows=182 width=121) (actual time=2.825..2.827 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 38kB Buffers: shared hit=2129 -> Seq Scan on messages (cost=0.00..4054.00 rows=182 width=121) (actual time=1.272..2.809 rows=200.00 loops=1) Filter: ((chat_id = '95e22f46-dc69-4c05-b02d-127fd0abb8fd'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:07.406365+00'::timestamp with time zone, 'f15df963-0361-4789-9e7e-47767c6064fe'::uuid))) Rows Removed by Filter: 109800 Buffers: shared hit=2129 Planning Time: 0.031 ms Execution Time: 2.835 ms #### chat_only Limit (cost=960.83..960.95 rows=51 width=121) (actual time=0.142..0.145 rows=51.00 loops=1) Buffers: shared hit=378 -> Sort (cost=960.83..961.28 rows=182 width=121) (actual time=0.142..0.143 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 38kB Buffers: shared hit=378 -> Bitmap Heap Scan on messages (cost=7.06..954.75 rows=182 width=121) (actual time=0.074..0.127 rows=200.00 loops=1) Recheck Cond: (chat_id = '95e22f46-dc69-4c05-b02d-127fd0abb8fd'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-02 01:27:07.406365+00'::timestamp with time zone, 'f15df963-0361-4789-9e7e-47767c6064fe'::uuid)) Rows Removed by Filter: 200 Heap Blocks: exact=376 Buffers: shared hit=378 -> Bitmap Index Scan on bench_variant (cost=0.00..7.02 rows=363 width=0) (actual time=0.013..0.013 rows=400.00 loops=1) Index Cond: (chat_id = '95e22f46-dc69-4c05-b02d-127fd0abb8fd'::uuid) Index Searches: 1 Buffers: shared hit=2 Planning Time: 0.029 ms Execution Time: 0.151 ms #### covering Limit (cost=0.42..5.92 rows=51 width=121) (actual time=0.003..0.011 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.42..20.06 rows=182 width=121) (actual time=0.003..0.008 rows=51.00 loops=1) Index Cond: ((chat_id = '95e22f46-dc69-4c05-b02d-127fd0abb8fd'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:07.406365+00'::timestamp with time zone, 'f15df963-0361-4789-9e7e-47767c6064fe'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.033 ms Execution Time: 0.015 ms ### bg 10: first, после 0 #### psql Limit (cost=57.56..57.60 rows=14 width=121) (actual time=0.014..0.015 rows=10.00 loops=1) Buffers: shared hit=13 -> Sort (cost=57.56..57.60 rows=14 width=121) (actual time=0.013..0.014 rows=10.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 27kB Buffers: shared hit=13 -> Bitmap Heap Scan on messages (cost=4.53..57.30 rows=14 width=121) (actual time=0.007..0.011 rows=10.00 loops=1) Recheck Cond: (chat_id = '65833eb1-1e98-47e6-8955-8098e7434a4f'::uuid) Heap Blocks: exact=10 Buffers: shared hit=13 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..4.52 rows=14 width=0) (actual time=0.004..0.004 rows=10.00 loops=1) Index Cond: (chat_id = '65833eb1-1e98-47e6-8955-8098e7434a4f'::uuid) Index Searches: 1 Buffers: shared hit=3 Planning Time: 0.040 ms Execution Time: 0.022 ms #### generic Limit (cost=0.42..40.58 rows=10 width=121) (actual time=0.003..0.006 rows=10.00 loops=1) Buffers: shared hit=13 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..410.03 rows=102 width=121) (actual time=0.003..0.005 rows=10.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=13 Planning Time: 0.003 ms Execution Time: 0.009 ms #### no-index Limit (cost=3504.27..3504.30 rows=14 width=121) (actual time=2.520..2.521 rows=10.00 loops=1) Buffers: shared hit=2129 -> Sort (cost=3504.27..3504.30 rows=14 width=121) (actual time=2.519..2.520 rows=10.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 27kB Buffers: shared hit=2129 -> Seq Scan on messages (cost=0.00..3504.00 rows=14 width=121) (actual time=0.141..2.517 rows=10.00 loops=1) Filter: (chat_id = '65833eb1-1e98-47e6-8955-8098e7434a4f'::uuid) Rows Removed by Filter: 109990 Buffers: shared hit=2129 Planning Time: 0.031 ms Execution Time: 2.526 ms #### chat_only Limit (cost=57.44..57.47 rows=14 width=121) (actual time=0.013..0.014 rows=10.00 loops=1) Buffers: shared hit=12 -> Sort (cost=57.44..57.47 rows=14 width=121) (actual time=0.012..0.013 rows=10.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 27kB Buffers: shared hit=12 -> Bitmap Heap Scan on messages (cost=4.40..57.17 rows=14 width=121) (actual time=0.006..0.010 rows=10.00 loops=1) Recheck Cond: (chat_id = '65833eb1-1e98-47e6-8955-8098e7434a4f'::uuid) Heap Blocks: exact=10 Buffers: shared hit=12 -> Bitmap Index Scan on bench_variant (cost=0.00..4.40 rows=14 width=0) (actual time=0.003..0.003 rows=10.00 loops=1) Index Cond: (chat_id = '65833eb1-1e98-47e6-8955-8098e7434a4f'::uuid) Index Searches: 1 Buffers: shared hit=2 Planning Time: 0.036 ms Execution Time: 0.020 ms #### covering Limit (cost=0.42..4.66 rows=14 width=121) (actual time=0.004..0.006 rows=10.00 loops=1) Buffers: shared hit=4 -> Index Only Scan using bench_variant on messages (cost=0.42..4.66 rows=14 width=121) (actual time=0.004..0.005 rows=10.00 loops=1) Index Cond: (chat_id = '65833eb1-1e98-47e6-8955-8098e7434a4f'::uuid) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=4 Planning Time: 0.033 ms Execution Time: 0.011 ms