# План запроса истории: размер 100k Commit 67cb4d7 Читаемый чат — 100000 сообщений; фоновые чаты: 10 × 5k, 100 × 400, 1000 × 10. Страница — `limit` профиля; «после» — сколько сообщений чата до страницы. ## Планы приложения auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план, с которыми его выполнило приложение (prepared statement asyncpg). ### read 100k: first, после 0 duration: 0.043 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 = '8168dfa1-b77d-4a1b-b326-3c141abb0288', $2 = '51' Limit (cost=0.42..11.44 rows=51 width=121) (actual time=0.014..0.032 rows=51.00 loops=1) Buffers: shared hit=24 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..21672.76 rows=100333 width=121) (actual time=0.014..0.028 rows=51.00 loops=1) Index Cond: (chat_id = '8168dfa1-b77d-4a1b-b326-3c141abb0288'::uuid) Index Searches: 1 Buffers: shared hit=24 ### read 100k: middle, после 50000 duration: 0.059 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 = '8168dfa1-b77d-4a1b-b326-3c141abb0288', $2 = '2026-10-02 04:29:43.65986+00', $3 = '305dee56-4c6a-4706-9f4f-82b1742495b9', $4 = '51' Limit (cost=0.42..27.91 rows=7 width=121) (actual time=0.018..0.047 rows=51.00 loops=1) Buffers: shared hit=50 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.07 rows=73 width=121) (actual time=0.017..0.044 rows=51.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=50 ### read 100k: last, после 99900 duration: 0.047 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 = '8168dfa1-b77d-4a1b-b326-3c141abb0288', $2 = '2026-10-02 04:31:24.926308+00', $3 = '85b040f0-3a5c-4adf-ad6a-9fb242c6805a', $4 = '51' Limit (cost=0.42..27.91 rows=7 width=121) (actual time=0.014..0.036 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.07 rows=73 width=121) (actual time=0.013..0.032 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.052 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 = 'ce420db9-1add-44f4-ac30-eef132545058', $2 = '51' Limit (cost=0.42..123.87 rows=51 width=121) (actual time=0.010..0.041 rows=51.00 loops=1) Buffers: shared hit=52 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..11878.25 rows=4907 width=121) (actual time=0.009..0.038 rows=51.00 loops=1) Index Cond: (chat_id = 'ce420db9-1add-44f4-ac30-eef132545058'::uuid) Index Searches: 1 Buffers: shared hit=52 ### bg 5k: middle, после 2500 duration: 0.055 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 = 'ce420db9-1add-44f4-ac30-eef132545058', $2 = '2026-10-02 04:29:43.647772+00', $3 = '03e854fd-a86a-47be-a03c-a9f644378e07', $4 = '51' Limit (cost=0.42..27.91 rows=7 width=121) (actual time=0.009..0.043 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.07 rows=73 width=121) (actual time=0.008..0.040 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.061 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 = 'ce420db9-1add-44f4-ac30-eef132545058', $2 = '2026-10-02 04:31:21.278504+00', $3 = 'f9c0a227-3053-4799-895d-a9f6d802fc59', $4 = '51' Limit (cost=0.42..27.91 rows=7 width=121) (actual time=0.011..0.049 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.07 rows=73 width=121) (actual time=0.010..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 ### bg 400: first, после 0 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 ORDER BY messages.created_at, messages.id LIMIT $2::INTEGER Query Parameters: $1 = 'ac906f31-31b9-472c-9416-431b6154b01e', $2 = '51' Limit (cost=0.42..66.44 rows=16 width=121) (actual time=0.009..0.067 rows=51.00 loops=1) Buffers: shared hit=53 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..66.44 rows=16 width=121) (actual time=0.009..0.065 rows=51.00 loops=1) Index Cond: (chat_id = 'ac906f31-31b9-472c-9416-431b6154b01e'::uuid) Index Searches: 1 Buffers: shared hit=53 ### bg 400: middle, после 200 duration: 0.070 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 = 'ac906f31-31b9-472c-9416-431b6154b01e', $2 = '2026-10-02 04:29:43.395209+00', $3 = '8f2c161b-c9ec-40f0-b84c-364d4d091ac0', $4 = '51' Limit (cost=0.42..27.91 rows=7 width=121) (actual time=0.008..0.060 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.07 rows=73 width=121) (actual time=0.008..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=54 ### bg 400: last, после 300 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 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 = 'ac906f31-31b9-472c-9416-431b6154b01e', $2 = '2026-10-02 04:30:33.50557+00', $3 = 'c946cb97-462f-4b37-b062-8e153629a81e', $4 = '51' Limit (cost=0.42..27.91 rows=7 width=121) (actual time=0.007..0.058 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.07 rows=73 width=121) (actual time=0.006..0.056 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 10: first, после 0 duration: 0.022 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 = '5fefd812-e275-4da5-816c-c3316fbad983', $2 = '51' Limit (cost=0.42..66.44 rows=16 width=121) (actual time=0.008..0.019 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.44 rows=16 width=121) (actual time=0.008..0.019 rows=10.00 loops=1) Index Cond: (chat_id = '5fefd812-e275-4da5-816c-c3316fbad983'::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..18.95 rows=51 width=121) (actual time=0.007..0.022 rows=51.00 loops=1) Buffers: shared hit=50 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..18414.68 rows=50688 width=121) (actual time=0.006..0.019 rows=51.00 loops=1) Index Cond: ((chat_id = '8168dfa1-b77d-4a1b-b326-3c141abb0288'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.65986+00'::timestamp with time zone, '305dee56-4c6a-4706-9f4f-82b1742495b9'::uuid))) Index Searches: 1 Buffers: shared hit=50 Planning Time: 0.053 ms Execution Time: 0.032 ms #### generic Limit (cost=0.42..27.91 rows=7 width=121) (actual time=0.008..0.021 rows=51.00 loops=1) Buffers: shared hit=50 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..287.07 rows=73 width=121) (actual time=0.007..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=50 Planning Time: 0.009 ms Execution Time: 0.030 ms #### or-cursor Limit (cost=0.42..22.98 rows=51 width=121) (actual time=7.359..7.370 rows=51.00 loops=1) Buffers: shared hit=50488 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..22425.26 rows=50688 width=121) (actual time=7.359..7.367 rows=51.00 loops=1) Index Cond: (chat_id = '8168dfa1-b77d-4a1b-b326-3c141abb0288'::uuid) Filter: ((created_at > '2026-10-02 04:29:43.65986+00'::timestamp with time zone) OR ((created_at = '2026-10-02 04:29:43.65986+00'::timestamp with time zone) AND (id > '305dee56-4c6a-4706-9f4f-82b1742495b9'::uuid))) Rows Removed by Filter: 50000 Index Searches: 1 Buffers: shared hit=50488 Planning Time: 0.057 ms Execution Time: 7.379 ms #### no-index Limit (cost=7014.97..7020.91 rows=51 width=121) (actual time=5.476..6.776 rows=51.00 loops=1) Buffers: shared hit=3882 -> Gather Merge (cost=7014.97..12918.42 rows=50688 width=121) (actual time=5.476..6.771 rows=51.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3882 -> Sort (cost=6014.94..6067.74 rows=21120 width=121) (actual time=4.424..4.426 rows=42.33 loops=3) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 51kB Buffers: shared hit=3882 Worker 0: Sort Method: top-N heapsort Memory: 50kB Worker 1: Sort Method: top-N heapsort Memory: 49kB -> Parallel Seq Scan on messages (cost=0.00..5310.33 rows=21120 width=121) (actual time=1.482..3.588 rows=16666.67 loops=3) Filter: ((chat_id = '8168dfa1-b77d-4a1b-b326-3c141abb0288'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.65986+00'::timestamp with time zone, '305dee56-4c6a-4706-9f4f-82b1742495b9'::uuid))) Rows Removed by Filter: 50000 Buffers: shared hit=3852 Planning Time: 0.082 ms Execution Time: 6.795 ms #### chat_only Limit (cost=7014.97..7020.91 rows=51 width=121) (actual time=4.719..5.651 rows=51.00 loops=1) Buffers: shared hit=3882 -> Gather Merge (cost=7014.97..12918.42 rows=50688 width=121) (actual time=4.719..5.648 rows=51.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3882 -> Sort (cost=6014.94..6067.74 rows=21120 width=121) (actual time=3.913..3.915 rows=42.33 loops=3) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 48kB Buffers: shared hit=3882 Worker 0: Sort Method: top-N heapsort Memory: 46kB Worker 1: Sort Method: top-N heapsort Memory: 49kB -> Parallel Seq Scan on messages (cost=0.00..5310.33 rows=21120 width=121) (actual time=1.283..3.078 rows=16666.67 loops=3) Filter: ((chat_id = '8168dfa1-b77d-4a1b-b326-3c141abb0288'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.65986+00'::timestamp with time zone, '305dee56-4c6a-4706-9f4f-82b1742495b9'::uuid))) Rows Removed by Filter: 50000 Buffers: shared hit=3852 Planning Time: 0.042 ms Execution Time: 5.659 ms #### covering Limit (cost=0.42..5.39 rows=51 width=121) (actual time=0.006..0.012 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.42..4942.18 rows=50688 width=121) (actual time=0.006..0.009 rows=51.00 loops=1) Index Cond: ((chat_id = '8168dfa1-b77d-4a1b-b326-3c141abb0288'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.65986+00'::timestamp with time zone, '305dee56-4c6a-4706-9f4f-82b1742495b9'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.041 ms Execution Time: 0.019 ms ### bg 5k: middle, после 2500 #### psql Limit (cost=0.42..152.59 rows=51 width=121) (actual time=0.004..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..7397.25 rows=2479 width=121) (actual time=0.004..0.015 rows=51.00 loops=1) Index Cond: ((chat_id = 'ce420db9-1add-44f4-ac30-eef132545058'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.647772+00'::timestamp with time zone, '03e854fd-a86a-47be-a03c-a9f644378e07'::uuid))) Index Searches: 1 Buffers: shared hit=52 Planning Time: 0.044 ms Execution Time: 0.025 ms #### generic Limit (cost=0.42..27.91 rows=7 width=121) (actual time=0.004..0.019 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.07 rows=73 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=52 Planning Time: 0.007 ms Execution Time: 0.026 ms #### or-cursor Limit (cost=0.42..245.54 rows=51 width=121) (actual time=0.401..0.411 rows=51.00 loops=1) Buffers: shared hit=2430 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..11915.05 rows=2479 width=121) (actual time=0.401..0.409 rows=51.00 loops=1) Index Cond: (chat_id = 'ce420db9-1add-44f4-ac30-eef132545058'::uuid) Filter: ((created_at > '2026-10-02 04:29:43.647772+00'::timestamp with time zone) OR ((created_at = '2026-10-02 04:29:43.647772+00'::timestamp with time zone) AND (id > '03e854fd-a86a-47be-a03c-a9f644378e07'::uuid))) Rows Removed by Filter: 2500 Index Searches: 1 Buffers: shared hit=2430 Planning Time: 0.050 ms Execution Time: 0.416 ms #### no-index Limit (cost=6344.82..6350.76 rows=51 width=121) (actual time=3.494..5.159 rows=51.00 loops=1) Buffers: shared hit=3882 -> Gather Merge (cost=6344.82..6633.54 rows=2479 width=121) (actual time=3.494..5.143 rows=51.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3882 -> Sort (cost=5344.80..5347.38 rows=1033 width=121) (actual time=2.613..2.614 rows=40.33 loops=3) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 39kB Buffers: shared hit=3882 Worker 0: Sort Method: top-N heapsort Memory: 38kB Worker 1: Sort Method: top-N heapsort Memory: 39kB -> Parallel Seq Scan on messages (cost=0.00..5310.33 rows=1033 width=121) (actual time=1.096..2.550 rows=833.33 loops=3) Filter: ((chat_id = 'ce420db9-1add-44f4-ac30-eef132545058'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.647772+00'::timestamp with time zone, '03e854fd-a86a-47be-a03c-a9f644378e07'::uuid))) Rows Removed by Filter: 65833 Buffers: shared hit=3852 Planning Time: 0.049 ms Execution Time: 5.168 ms #### chat_only Limit (cost=4283.81..4283.94 rows=51 width=121) (actual time=1.279..1.283 rows=51.00 loops=1) Buffers: shared hit=2825 -> Sort (cost=4283.81..4290.01 rows=2479 width=121) (actual time=1.279..1.280 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 40kB Buffers: shared hit=2825 -> Bitmap Heap Scan on messages (cost=57.84..4201.11 rows=2479 width=121) (actual time=0.691..1.152 rows=2500.00 loops=1) Recheck Cond: (chat_id = 'ce420db9-1add-44f4-ac30-eef132545058'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-02 04:29:43.647772+00'::timestamp with time zone, '03e854fd-a86a-47be-a03c-a9f644378e07'::uuid)) Rows Removed by Filter: 2500 Heap Blocks: exact=2817 Buffers: shared hit=2825 -> Bitmap Index Scan on bench_variant (cost=0.00..57.22 rows=4907 width=0) (actual time=0.186..0.186 rows=5000.00 loops=1) Index Cond: (chat_id = 'ce420db9-1add-44f4-ac30-eef132545058'::uuid) Index Searches: 1 Buffers: shared hit=8 Planning Time: 0.040 ms Execution Time: 1.289 ms #### covering Limit (cost=0.42..5.47 rows=51 width=121) (actual time=0.005..0.012 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.42..246.00 rows=2479 width=121) (actual time=0.005..0.009 rows=51.00 loops=1) Index Cond: ((chat_id = 'ce420db9-1add-44f4-ac30-eef132545058'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.647772+00'::timestamp with time zone, '03e854fd-a86a-47be-a03c-a9f644378e07'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.037 ms Execution Time: 0.019 ms ### bg 400: middle, после 200 #### psql Limit (cost=0.42..35.53 rows=8 width=121) (actual time=0.004..0.021 rows=51.00 loops=1) Buffers: shared hit=54 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..35.53 rows=8 width=121) (actual time=0.004..0.018 rows=51.00 loops=1) Index Cond: ((chat_id = 'ac906f31-31b9-472c-9416-431b6154b01e'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.395209+00'::timestamp with time zone, '8f2c161b-c9ec-40f0-b84c-364d4d091ac0'::uuid))) Index Searches: 1 Buffers: shared hit=54 Planning Time: 0.029 ms Execution Time: 0.027 ms #### generic Limit (cost=0.42..27.91 rows=7 width=121) (actual time=0.004..0.021 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.07 rows=73 width=121) (actual time=0.004..0.018 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 Planning Time: 0.005 ms Execution Time: 0.027 ms #### or-cursor Limit (cost=40.12..40.14 rows=8 width=121) (actual time=0.113..0.117 rows=51.00 loops=1) Buffers: shared hit=201 -> Sort (cost=40.12..40.14 rows=8 width=121) (actual time=0.113..0.114 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 38kB Buffers: shared hit=201 -> Bitmap Heap Scan on messages (cost=8.94..40.00 rows=8 width=121) (actual time=0.033..0.082 rows=200.00 loops=1) Recheck Cond: (((chat_id = 'ac906f31-31b9-472c-9416-431b6154b01e'::uuid) AND (created_at > '2026-10-02 04:29:43.395209+00'::timestamp with time zone)) OR ((chat_id = 'ac906f31-31b9-472c-9416-431b6154b01e'::uuid) AND (created_at = '2026-10-02 04:29:43.395209+00'::timestamp with time zone) AND (id > '8f2c161b-c9ec-40f0-b84c-364d4d091ac0'::uuid))) Heap Blocks: exact=193 Buffers: shared hit=201 -> BitmapOr (cost=8.94..8.94 rows=8 width=0) (actual time=0.011..0.011 rows=0.00 loops=1) Buffers: shared hit=8 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..4.50 rows=8 width=0) (actual time=0.010..0.010 rows=200.00 loops=1) Index Cond: ((chat_id = 'ac906f31-31b9-472c-9416-431b6154b01e'::uuid) AND (created_at > '2026-10-02 04:29:43.395209+00'::timestamp with time zone)) Index Searches: 1 Buffers: shared hit=5 -> Bitmap Index Scan on ix_messages_chat_id_created_at_id (cost=0.00..4.43 rows=1 width=0) (actual time=0.001..0.001 rows=0.00 loops=1) Index Cond: ((chat_id = 'ac906f31-31b9-472c-9416-431b6154b01e'::uuid) AND (created_at = '2026-10-02 04:29:43.395209+00'::timestamp with time zone) AND (id > '8f2c161b-c9ec-40f0-b84c-364d4d091ac0'::uuid)) Index Searches: 1 Buffers: shared hit=3 Planning Time: 0.038 ms Execution Time: 0.124 ms #### no-index Limit (cost=6310.38..6311.20 rows=7 width=121) (actual time=3.357..4.367 rows=51.00 loops=1) Buffers: shared hit=3882 -> Gather Merge (cost=6310.38..6311.20 rows=7 width=121) (actual time=3.357..4.364 rows=51.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3882 -> Sort (cost=5310.36..5310.36 rows=3 width=121) (actual time=2.454..2.455 rows=41.67 loops=3) Sort Key: created_at, id Sort Method: quicksort Memory: 44kB Buffers: shared hit=3882 Worker 0: Sort Method: quicksort Memory: 41kB Worker 1: Sort Method: quicksort Memory: 40kB -> Parallel Seq Scan on messages (cost=0.00..5310.33 rows=3 width=121) (actual time=1.180..2.436 rows=66.67 loops=3) Filter: ((chat_id = 'ac906f31-31b9-472c-9416-431b6154b01e'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.395209+00'::timestamp with time zone, '8f2c161b-c9ec-40f0-b84c-364d4d091ac0'::uuid))) Rows Removed by Filter: 66600 Buffers: shared hit=3852 Planning Time: 0.043 ms Execution Time: 4.377 ms #### chat_only Limit (cost=64.88..64.90 rows=8 width=121) (actual time=0.122..0.126 rows=51.00 loops=1) Buffers: shared hit=390 -> Sort (cost=64.88..64.90 rows=8 width=121) (actual time=0.122..0.124 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 38kB Buffers: shared hit=390 -> Index Scan using bench_variant on messages (cost=0.42..64.76 rows=8 width=121) (actual time=0.067..0.108 rows=200.00 loops=1) Index Cond: (chat_id = 'ac906f31-31b9-472c-9416-431b6154b01e'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-02 04:29:43.395209+00'::timestamp with time zone, '8f2c161b-c9ec-40f0-b84c-364d4d091ac0'::uuid)) Rows Removed by Filter: 200 Index Searches: 1 Buffers: shared hit=390 Planning Time: 0.044 ms Execution Time: 0.132 ms #### covering Limit (cost=0.42..4.58 rows=8 width=121) (actual time=0.011..0.018 rows=51.00 loops=1) Buffers: shared hit=5 -> Index Only Scan using bench_variant on messages (cost=0.42..4.58 rows=8 width=121) (actual time=0.011..0.015 rows=51.00 loops=1) Index Cond: ((chat_id = 'ac906f31-31b9-472c-9416-431b6154b01e'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:29:43.395209+00'::timestamp with time zone, '8f2c161b-c9ec-40f0-b84c-364d4d091ac0'::uuid))) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.037 ms Execution Time: 0.035 ms ### bg 10: first, после 0 #### psql Limit (cost=0.42..66.44 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.44 rows=16 width=121) (actual time=0.004..0.008 rows=10.00 loops=1) Index Cond: (chat_id = '5fefd812-e275-4da5-816c-c3316fbad983'::uuid) Index Searches: 1 Buffers: shared hit=13 Planning Time: 0.035 ms Execution Time: 0.013 ms #### generic Limit (cost=0.42..84.41 rows=22 width=121) (actual time=0.005..0.010 rows=10.00 loops=1) Buffers: shared hit=13 -> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..836.46 rows=219 width=121) (actual time=0.005..0.008 rows=10.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=13 Planning Time: 0.005 ms Execution Time: 0.016 ms #### no-index Limit (cost=5895.59..5895.63 rows=16 width=121) (actual time=3.220..4.197 rows=10.00 loops=1) Buffers: shared hit=3852 -> Sort (cost=5895.59..5895.63 rows=16 width=121) (actual time=3.219..4.196 rows=10.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 27kB Buffers: shared hit=3852 -> Gather (cost=1000.00..5895.27 rows=16 width=121) (actual time=0.356..4.191 rows=10.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3852 -> Parallel Seq Scan on messages (cost=0.00..4893.67 rows=7 width=121) (actual time=0.236..2.321 rows=3.33 loops=3) Filter: (chat_id = '5fefd812-e275-4da5-816c-c3316fbad983'::uuid) Rows Removed by Filter: 66663 Buffers: shared hit=3852 Planning Time: 0.046 ms Execution Time: 4.210 ms #### chat_only Limit (cost=65.00..65.04 rows=16 width=121) (actual time=0.008..0.009 rows=10.00 loops=1) Buffers: shared hit=13 -> Sort (cost=65.00..65.04 rows=16 width=121) (actual time=0.008..0.008 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.68 rows=16 width=121) (actual time=0.003..0.006 rows=10.00 loops=1) Index Cond: (chat_id = '5fefd812-e275-4da5-816c-c3316fbad983'::uuid) Index Searches: 1 Buffers: shared hit=13 Planning Time: 0.024 ms Execution Time: 0.013 ms #### covering Limit (cost=0.42..4.70 rows=16 width=121) (actual time=0.003..0.004 rows=10.00 loops=1) Buffers: shared hit=4 -> 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 = '5fefd812-e275-4da5-816c-c3316fbad983'::uuid) Heap Fetches: 0 Index Searches: 1 Buffers: shared hit=4 Planning Time: 0.026 ms Execution Time: 0.009 ms