# План запроса истории: размер 100k Commit 67be41e Читаемый чат — 100000 сообщений; фоновые чаты: 10 × 5k, 100 × 400, 1000 × 10. Страница — `limit` профиля; «после» — сколько сообщений чата до страницы. ## Планы приложения auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план, с которыми его выполнило приложение (prepared statement asyncpg). ### read 100k: first, после 0 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 ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = 'd06769fb-bc27-4de2-924c-9fe5f560b034', $2 = '51' Limit (cost=0.42..11.45 rows=51 width=121) (actual time=0.030..0.051 rows=51.00 loops=1) Buffers: shared hit=19 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..21692.25 rows=100287 width=121) (actual time=0.028..0.047 rows=51.00 loops=1) Index Cond: (chat_id = 'd06769fb-bc27-4de2-924c-9fe5f560b034'::uuid) Index Searches: 1 Buffers: shared hit=19 ### read 100k: middle, после 50000 duration: 0.050 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 = 'd06769fb-bc27-4de2-924c-9fe5f560b034', $2 = '2026-10-02 01:35:07.958295+00', $3 = 'afc784ba-5f6e-4cc2-8d85-f7c8e9a9e20d', $4 = '51' Limit (cost=0.42..27.92 rows=7 width=121) (actual time=0.019..0.039 rows=51.00 loops=1) Buffers: shared hit=46 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.26 rows=73 width=121) (actual time=0.018..0.036 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=46 ### read 100k: last, после 99900 duration: 0.146 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 = 'd06769fb-bc27-4de2-924c-9fe5f560b034', $2 = '2026-10-02 01:36:55.887527+00', $3 = '4b11d4ae-4c4f-470b-b735-1146862ce2c3', $4 = '51' Limit (cost=0.42..27.92 rows=7 width=121) (actual time=0.051..0.127 rows=51.00 loops=1) Buffers: shared hit=54 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.26 rows=73 width=121) (actual time=0.046..0.120 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 5k: first, после 0 duration: 0.074 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 = '885c361c-93d9-44ad-ac7e-9858165e0630', $2 = '51' Limit (cost=0.42..123.13 rows=51 width=121) (actual time=0.018..0.062 rows=51.00 loops=1) Buffers: shared hit=50 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..12030.43 rows=5000 width=121) (actual time=0.017..0.059 rows=51.00 loops=1) Index Cond: (chat_id = '885c361c-93d9-44ad-ac7e-9858165e0630'::uuid) Index Searches: 1 Buffers: shared hit=50 ### bg 5k: middle, после 2500 duration: 0.076 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 = '885c361c-93d9-44ad-ac7e-9858165e0630', $2 = '2026-10-02 01:35:07.94784+00', $3 = 'bd32e5a5-5ebd-4920-8b59-c703f8ca4d4e', $4 = '51' Limit (cost=0.42..27.92 rows=7 width=121) (actual time=0.017..0.063 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.26 rows=73 width=121) (actual time=0.016..0.060 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 5k: last, после 4900 duration: 0.102 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 = '885c361c-93d9-44ad-ac7e-9858165e0630', $2 = '2026-10-02 01:36:52.131294+00', $3 = '4137822c-49c5-476b-8934-a5bf36fe2465', $4 = '51' Limit (cost=0.42..27.92 rows=7 width=121) (actual time=0.021..0.086 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.26 rows=73 width=121) (actual time=0.021..0.083 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 400: first, после 0 duration: 0.149 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 = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526', $2 = '51' Limit (cost=0.42..192.36 rows=51 width=121) (actual time=0.015..0.137 rows=51.00 loops=1) Buffers: shared hit=55 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..1328.93 rows=353 width=121) (actual time=0.014..0.125 rows=51.00 loops=1) Index Cond: (chat_id = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526'::uuid) Index Searches: 1 Buffers: shared hit=55 ### bg 400: middle, после 200 duration: 0.095 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 = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526', $2 = '2026-10-02 01:35:07.657858+00', $3 = '9d22d900-1bbc-4849-9173-cc1a755f21e6', $4 = '51' Limit (cost=0.42..27.92 rows=7 width=121) (actual time=0.013..0.081 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.26 rows=73 width=121) (actual time=0.012..0.079 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: last, после 300 duration: 0.125 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 = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526', $2 = '2026-10-02 01:36:02.410519+00', $3 = 'd3191a48-36d8-4ef8-b8c6-c6262bd03b77', $4 = '51' Limit (cost=0.42..27.92 rows=7 width=121) (actual time=0.020..0.109 rows=51.00 loops=1) Buffers: shared hit=54 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.26 rows=73 width=121) (actual time=0.020..0.106 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.031 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 = 'ec67a0ba-7da8-4526-b87e-336edaabbd01', $2 = '51' Limit (cost=0.42..66.48 rows=16 width=121) (actual time=0.013..0.028 rows=10.00 loops=1) Buffers: shared hit=13 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..66.48 rows=16 width=121) (actual time=0.013..0.027 rows=10.00 loops=1) Index Cond: (chat_id = 'ec67a0ba-7da8-4526-b87e-336edaabbd01'::uuid) Index Searches: 1 Buffers: shared hit=13 ## Страницы сравнения в 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 100k: middle, после 50000 #### psql Limit (cost=0.42..19.03 rows=51 width=121) (actual time=0.007..0.021 rows=51.00 loops=1) Buffers: shared hit=46 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..18423.62 rows=50496 width=121) (actual time=0.007..0.018 rows=51.00 loops=1) Index Cond: ((chat_id = 'd06769fb-bc27-4de2-924c-9fe5f560b034'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.958295+00'::timestamp with time zone, 'afc784ba-5f6e-4cc2-8d85-f7c8e9a9e20d'::uuid))) Index Searches: 1 Buffers: shared hit=46 Planning Time: 0.054 ms Execution Time: 0.029 ms #### generic Limit (cost=0.42..27.92 rows=7 width=121) (actual time=0.006..0.017 rows=51.00 loops=1) Buffers: shared hit=46 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.26 rows=73 width=121) (actual time=0.005..0.014 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=46 Planning Time: 0.008 ms Execution Time: 0.025 ms #### or-cursor Limit (cost=0.42..23.09 rows=51 width=121) (actual time=9.569..9.581 rows=51.00 loops=1) Buffers: shared hit=50416 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..22444.40 rows=50496 width=121) (actual time=9.568..9.578 rows=51.00 loops=1) Index Cond: (chat_id = 'd06769fb-bc27-4de2-924c-9fe5f560b034'::uuid) Filter: ((created_at > '2026-10-02 01:35:07.958295+00'::timestamp with time zone) OR ((created_at = '2026-10-02 01:35:07.958295+00'::timestamp with time zone) AND (id > 'afc784ba-5f6e-4cc2-8d85-f7c8e9a9e20d'::uuid))) Rows Removed by Filter: 50000 Index Searches: 1 Buffers: shared hit=50416 Planning Time: 0.122 ms Execution Time: 9.594 ms #### no-index Limit (cost=7016.30..7022.24 rows=51 width=121) (actual time=7.087..9.472 rows=51.00 loops=1) Buffers: shared hit=3886 -> Gather Merge (cost=7016.30..12897.39 rows=50496 width=121) (actual time=7.086..9.469 rows=51.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3886 -> Sort (cost=6016.27..6068.87 rows=21040 width=121) (actual time=5.522..5.524 rows=43.67 loops=3) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 50kB Buffers: shared hit=3886 Worker 0: Sort Method: top-N heapsort Memory: 47kB Worker 1: Sort Method: top-N heapsort Memory: 47kB -> Parallel Seq Scan on messages (cost=0.00..5314.33 rows=21040 width=121) (actual time=1.746..4.571 rows=16666.67 loops=3) Filter: ((chat_id = 'd06769fb-bc27-4de2-924c-9fe5f560b034'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.958295+00'::timestamp with time zone, 'afc784ba-5f6e-4cc2-8d85-f7c8e9a9e20d'::uuid))) Rows Removed by Filter: 50000 Buffers: shared hit=3856 Planning Time: 0.144 ms Execution Time: 9.490 ms #### chat_only Limit (cost=7016.30..7022.24 rows=51 width=121) (actual time=6.972..9.458 rows=51.00 loops=1) Buffers: shared hit=3886 -> Gather Merge (cost=7016.30..12897.39 rows=50496 width=121) (actual time=6.970..9.453 rows=51.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3886 -> Sort (cost=6016.27..6068.87 rows=21040 width=121) (actual time=5.334..5.336 rows=39.00 loops=3) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 51kB Buffers: shared hit=3886 Worker 0: Sort Method: top-N heapsort Memory: 46kB Worker 1: Sort Method: top-N heapsort Memory: 43kB -> Parallel Seq Scan on messages (cost=0.00..5314.33 rows=21040 width=121) (actual time=1.508..4.383 rows=16666.67 loops=3) Filter: ((chat_id = 'd06769fb-bc27-4de2-924c-9fe5f560b034'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.958295+00'::timestamp with time zone, 'afc784ba-5f6e-4cc2-8d85-f7c8e9a9e20d'::uuid))) Rows Removed by Filter: 50000 Buffers: shared hit=3856 Planning Time: 0.242 ms Execution Time: 9.480 ms #### covering Limit (cost=0.42..5.40 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..4926.34 rows=50496 width=121) (actual time=0.004..0.008 rows=51.00 loops=1) Index Cond: ((chat_id = 'd06769fb-bc27-4de2-924c-9fe5f560b034'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.958295+00'::timestamp with time zone, 'afc784ba-5f6e-4cc2-8d85-f7c8e9a9e20d'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.041 ms Execution Time: 0.017 ms ### bg 5k: middle, после 2500 #### psql Limit (cost=0.42..152.23 rows=51 width=121) (actual time=0.006..0.018 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..7495.56 rows=2518 width=121) (actual time=0.005..0.015 rows=51.00 loops=1) Index Cond: ((chat_id = '885c361c-93d9-44ad-ac7e-9858165e0630'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.94784+00'::timestamp with time zone, 'bd32e5a5-5ebd-4920-8b59-c703f8ca4d4e'::uuid))) Index Searches: 1 Buffers: shared hit=52 Planning Time: 0.067 ms Execution Time: 0.027 ms #### generic Limit (cost=0.42..27.92 rows=7 width=121) (actual time=0.005..0.016 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.26 rows=73 width=121) (actual time=0.004..0.013 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 Planning Time: 0.005 ms Execution Time: 0.021 ms #### or-cursor Limit (cost=0.42..244.84 rows=51 width=121) (actual time=0.416..0.426 rows=51.00 loops=1) Buffers: shared hit=2420 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..12067.93 rows=2518 width=121) (actual time=0.416..0.424 rows=51.00 loops=1) Index Cond: (chat_id = '885c361c-93d9-44ad-ac7e-9858165e0630'::uuid) Filter: ((created_at > '2026-10-02 01:35:07.94784+00'::timestamp with time zone) OR ((created_at = '2026-10-02 01:35:07.94784+00'::timestamp with time zone) AND (id > 'bd32e5a5-5ebd-4920-8b59-c703f8ca4d4e'::uuid))) Rows Removed by Filter: 2500 Index Searches: 1 Buffers: shared hit=2420 Planning Time: 0.037 ms Execution Time: 0.431 ms #### no-index Limit (cost=6349.35..6355.29 rows=51 width=121) (actual time=4.063..5.801 rows=51.00 loops=1) Buffers: shared hit=3886 -> Gather Merge (cost=6349.35..6642.62 rows=2518 width=121) (actual time=4.062..5.795 rows=51.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3886 -> Sort (cost=5349.33..5351.95 rows=1049 width=121) (actual time=2.941..2.942 rows=42.67 loops=3) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 39kB Buffers: shared hit=3886 Worker 0: Sort Method: top-N heapsort Memory: 38kB Worker 1: Sort Method: top-N heapsort Memory: 38kB -> Parallel Seq Scan on messages (cost=0.00..5314.33 rows=1049 width=121) (actual time=1.251..2.873 rows=833.33 loops=3) Filter: ((chat_id = '885c361c-93d9-44ad-ac7e-9858165e0630'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.94784+00'::timestamp with time zone, 'bd32e5a5-5ebd-4920-8b59-c703f8ca4d4e'::uuid))) Rows Removed by Filter: 65833 Buffers: shared hit=3856 Planning Time: 0.066 ms Execution Time: 5.815 ms #### chat_only Limit (cost=4292.29..4292.42 rows=51 width=121) (actual time=2.144..2.148 rows=51.00 loops=1) Buffers: shared hit=2801 -> Sort (cost=4292.29..4298.59 rows=2518 width=121) (actual time=2.144..2.146 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 39kB Buffers: shared hit=2801 -> Bitmap Heap Scan on messages (cost=58.55..4208.28 rows=2518 width=121) (actual time=0.844..2.007 rows=2500.00 loops=1) Recheck Cond: (chat_id = '885c361c-93d9-44ad-ac7e-9858165e0630'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-02 01:35:07.94784+00'::timestamp with time zone, 'bd32e5a5-5ebd-4920-8b59-c703f8ca4d4e'::uuid)) Rows Removed by Filter: 2500 Heap Blocks: exact=2794 Buffers: shared hit=2801 -> Bitmap Index Scan on bench_variant (cost=0.00..57.92 rows=5000 width=0) (actual time=0.169..0.169 rows=5000.00 loops=1) Index Cond: (chat_id = '885c361c-93d9-44ad-ac7e-9858165e0630'::uuid) Index Searches: 1 Buffers: shared hit=7 Planning Time: 0.056 ms Execution Time: 2.158 ms #### covering Limit (cost=0.42..5.41 rows=51 width=121) (actual time=0.005..0.011 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.42..246.78 rows=2518 width=121) (actual time=0.005..0.009 rows=51.00 loops=1) Index Cond: ((chat_id = '885c361c-93d9-44ad-ac7e-9858165e0630'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.94784+00'::timestamp with time zone, 'bd32e5a5-5ebd-4920-8b59-c703f8ca4d4e'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.041 ms Execution Time: 0.018 ms ### bg 400: middle, после 200 #### psql Limit (cost=0.42..196.99 rows=51 width=121) (actual time=0.004..0.020 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..686.48 rows=178 width=121) (actual time=0.004..0.017 rows=51.00 loops=1) Index Cond: ((chat_id = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.657858+00'::timestamp with time zone, '9d22d900-1bbc-4849-9173-cc1a755f21e6'::uuid))) Index Searches: 1 Buffers: shared hit=52 Planning Time: 0.033 ms Execution Time: 0.025 ms #### generic Limit (cost=0.42..27.92 rows=7 width=121) (actual time=0.006..0.024 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.26 rows=73 width=121) (actual time=0.006..0.020 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 Planning Time: 0.007 ms Execution Time: 0.032 ms #### or-cursor Limit (cost=0.42..381.82 rows=51 width=121) (actual time=0.050..0.066 rows=51.00 loops=1) Buffers: shared hit=253 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..1331.57 rows=178 width=121) (actual time=0.050..0.063 rows=51.00 loops=1) Index Cond: (chat_id = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526'::uuid) Filter: ((created_at > '2026-10-02 01:35:07.657858+00'::timestamp with time zone) OR ((created_at = '2026-10-02 01:35:07.657858+00'::timestamp with time zone) AND (id > '9d22d900-1bbc-4849-9173-cc1a755f21e6'::uuid))) Rows Removed by Filter: 200 Index Searches: 1 Buffers: shared hit=253 Planning Time: 0.034 ms Execution Time: 0.071 ms #### no-index Limit (cost=6316.65..6322.59 rows=51 width=121) (actual time=3.711..4.898 rows=51.00 loops=1) Buffers: shared hit=3886 -> Gather Merge (cost=6316.65..6337.39 rows=178 width=121) (actual time=3.711..4.895 rows=51.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3886 -> Sort (cost=5316.63..5316.82 rows=74 width=121) (actual time=2.757..2.758 rows=41.00 loops=3) Sort Key: created_at, id Sort Method: quicksort Memory: 47kB Buffers: shared hit=3886 Worker 0: Sort Method: quicksort Memory: 42kB Worker 1: Sort Method: quicksort Memory: 36kB -> Parallel Seq Scan on messages (cost=0.00..5314.33 rows=74 width=121) (actual time=1.225..2.737 rows=66.67 loops=3) Filter: ((chat_id = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.657858+00'::timestamp with time zone, '9d22d900-1bbc-4849-9173-cc1a755f21e6'::uuid))) Rows Removed by Filter: 66600 Buffers: shared hit=3856 Planning Time: 0.051 ms Execution Time: 4.907 ms #### chat_only Limit (cost=1071.02..1071.14 rows=51 width=121) (actual time=0.148..0.152 rows=51.00 loops=1) Buffers: shared hit=397 -> Sort (cost=1071.02..1071.46 rows=178 width=121) (actual time=0.148..0.149 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 38kB Buffers: shared hit=397 -> Bitmap Heap Scan on messages (cost=7.11..1065.08 rows=178 width=121) (actual time=0.077..0.133 rows=200.00 loops=1) Recheck Cond: (chat_id = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-02 01:35:07.657858+00'::timestamp with time zone, '9d22d900-1bbc-4849-9173-cc1a755f21e6'::uuid)) Rows Removed by Filter: 200 Heap Blocks: exact=394 Buffers: shared hit=397 -> Bitmap Index Scan on bench_variant (cost=0.00..7.07 rows=353 width=0) (actual time=0.019..0.019 rows=400.00 loops=1) Index Cond: (chat_id = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526'::uuid) Index Searches: 1 Buffers: shared hit=3 Planning Time: 0.045 ms Execution Time: 0.159 ms #### covering Limit (cost=0.42..6.02 rows=51 width=121) (actual time=0.005..0.009 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.42..19.98 rows=178 width=121) (actual time=0.004..0.007 rows=51.00 loops=1) Index Cond: ((chat_id = '5c6f25b4-bfa9-46de-b30b-6a6b5693e526'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:35:07.657858+00'::timestamp with time zone, '9d22d900-1bbc-4849-9173-cc1a755f21e6'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.032 ms Execution Time: 0.015 ms ### bg 10: first, после 0 #### psql Limit (cost=0.42..66.48 rows=16 width=121) (actual time=0.004..0.008 rows=10.00 loops=1) Buffers: shared hit=13 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..66.48 rows=16 width=121) (actual time=0.004..0.007 rows=10.00 loops=1) Index Cond: (chat_id = 'ec67a0ba-7da8-4526-b87e-336edaabbd01'::uuid) Index Searches: 1 Buffers: shared hit=13 Planning Time: 0.037 ms Execution Time: 0.013 ms #### generic Limit (cost=0.42..84.85 rows=22 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..837.00 rows=218 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.009 ms Execution Time: 0.009 ms #### no-index Limit (cost=5899.59..5899.63 rows=16 width=121) (actual time=3.391..4.491 rows=10.00 loops=1) Buffers: shared hit=3856 -> Sort (cost=5899.59..5899.63 rows=16 width=121) (actual time=3.390..4.490 rows=10.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 27kB Buffers: shared hit=3856 -> Gather (cost=1000.00..5899.27 rows=16 width=121) (actual time=0.373..4.484 rows=10.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3856 -> Parallel Seq Scan on messages (cost=0.00..4897.67 rows=7 width=121) (actual time=0.367..2.497 rows=3.33 loops=3) Filter: (chat_id = 'ec67a0ba-7da8-4526-b87e-336edaabbd01'::uuid) Rows Removed by Filter: 66663 Buffers: shared hit=3856 Planning Time: 0.059 ms Execution Time: 4.501 ms #### chat_only Limit (cost=65.07..65.11 rows=16 width=121) (actual time=0.009..0.011 rows=10.00 loops=1) Buffers: shared hit=13 -> Sort (cost=65.07..65.11 rows=16 width=121) (actual time=0.009..0.010 rows=10.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 27kB Buffers: shared hit=13 -> Index Scan using bench_variant on messages (cost=0.42..64.75 rows=16 width=121) (actual time=0.003..0.006 rows=10.00 loops=1) Index Cond: (chat_id = 'ec67a0ba-7da8-4526-b87e-336edaabbd01'::uuid) Index Searches: 1 Buffers: shared hit=13 Planning Time: 0.035 ms Execution Time: 0.016 ms #### covering Limit (cost=0.42..4.70 rows=16 width=121) (actual time=0.003..0.005 rows=10.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.42..4.70 rows=16 width=121) (actual time=0.003..0.004 rows=10.00 loops=1) Index Cond: (chat_id = 'ec67a0ba-7da8-4526-b87e-336edaabbd01'::uuid) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.026 ms Execution Time: 0.009 ms