# План запроса истории: размер 10k

Commit 67cb4d7
Читаемый чат — 10000 сообщений; фоновые чаты: 10 × 5k, 100 × 400, 1000 × 10.
Страница — `limit` профиля; «после» — сколько сообщений чата до
страницы.

## Планы приложения

auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
с которыми его выполнило приложение (prepared statement asyncpg).

### read 10k: 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 = '16247948-069a-41c4-ac39-4960d74af26b', $2 = '51'
Limit  (cost=0.42..46.21 rows=51 width=121) (actual time=0.015..0.050 rows=51.00 loops=1)
  Buffers: shared hit=46
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..9185.54 rows=10230 width=121) (actual time=0.015..0.046 rows=51.00 loops=1)
        Index Cond: (chat_id = '16247948-069a-41c4-ac39-4960d74af26b'::uuid)
        Index Searches: 1
        Buffers: shared hit=46

### read 10k: middle, после 5000

duration: 0.065 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 = '16247948-069a-41c4-ac39-4960d74af26b', $2 = '2026-10-02 04:26:36.494259+00', $3 = 'bb1d6b63-a42f-456c-904f-517fdec74674', $4 = '51'
Limit  (cost=0.42..12.82 rows=3 width=121) (actual time=0.020..0.052 rows=51.00 loops=1)
  Buffers: shared hit=55
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..141.00 rows=34 width=121) (actual time=0.019..0.048 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=55

### read 10k: last, после 9900

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 = '16247948-069a-41c4-ac39-4960d74af26b', $2 = '2026-10-02 04:27:31.500672+00', $3 = '058c8820-b7bc-44d2-999b-b327b1229e5a', $4 = '51'
Limit  (cost=0.42..12.82 rows=3 width=121) (actual time=0.012..0.045 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.00 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.049 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 = '68a617de-3d8f-4b0f-955a-fc36aacd783d', $2 = '51'
Limit  (cost=0.42..90.54 rows=51 width=121) (actual time=0.012..0.038 rows=51.00 loops=1)
  Buffers: shared hit=46
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..8837.96 rows=5001 width=121) (actual time=0.011..0.036 rows=51.00 loops=1)
        Index Cond: (chat_id = '68a617de-3d8f-4b0f-955a-fc36aacd783d'::uuid)
        Index Searches: 1
        Buffers: shared hit=46

### bg 5k: middle, после 2500

duration: 0.051 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 = '68a617de-3d8f-4b0f-955a-fc36aacd783d', $2 = '2026-10-02 04:26:36.485784+00', $3 = 'c5916956-6a49-4619-b177-d9d2b5ef9dc9', $4 = '51'
Limit  (cost=0.42..12.82 rows=3 width=121) (actual time=0.014..0.040 rows=51.00 loops=1)
  Buffers: shared hit=50
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..141.00 rows=34 width=121) (actual time=0.014..0.037 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

### bg 5k: last, после 4900

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 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 = '68a617de-3d8f-4b0f-955a-fc36aacd783d', $2 = '2026-10-02 04:27:30.486163+00', $3 = '655cb466-1486-474f-8e8a-eebbef614b66', $4 = '51'
Limit  (cost=0.42..12.82 rows=3 width=121) (actual time=0.010..0.041 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.00 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=53

### 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 = 'e4673889-7eb9-410f-92ad-9bfa26894ef3', $2 = '51'
Limit  (cost=0.42..189.27 rows=51 width=121) (actual time=0.010..0.070 rows=51.00 loops=1)
  Buffers: shared hit=53
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..1614.93 rows=436 width=121) (actual time=0.010..0.068 rows=51.00 loops=1)
        Index Cond: (chat_id = 'e4673889-7eb9-410f-92ad-9bfa26894ef3'::uuid)
        Index Searches: 1
        Buffers: shared hit=53

### bg 400: middle, после 200

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 = 'e4673889-7eb9-410f-92ad-9bfa26894ef3', $2 = '2026-10-02 04:26:36.332907+00', $3 = '687d11cd-352d-41fe-a556-2ff16f09650b', $4 = '51'
Limit  (cost=0.42..12.82 rows=3 width=121) (actual time=0.009..0.065 rows=51.00 loops=1)
  Buffers: shared hit=49
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..141.00 rows=34 width=121) (actual time=0.008..0.062 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=49

### bg 400: last, после 300

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 = 'e4673889-7eb9-410f-92ad-9bfa26894ef3', $2 = '2026-10-02 04:27:04.035066+00', $3 = 'd453b3c9-25ba-4060-8bb9-64a65b53b231', $4 = '51'
Limit  (cost=0.42..12.82 rows=3 width=121) (actual time=0.008..0.058 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.00 rows=34 width=121) (actual time=0.008..0.055 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 = '0efbcbe9-fc38-4fdd-b0d4-fee054e19463', $2 = '51'
Limit  (cost=57.56..57.60 rows=14 width=121) (actual time=0.027..0.028 rows=10.00 loops=1)
  Buffers: shared hit=13
  ->  Sort  (cost=57.56..57.60 rows=14 width=121) (actual time=0.027..0.027 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.29 rows=14 width=121) (actual time=0.011..0.023 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '0efbcbe9-fc38-4fdd-b0d4-fee054e19463'::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.006..0.006 rows=10.00 loops=1)
                    Index Cond: (chat_id = '0efbcbe9-fc38-4fdd-b0d4-fee054e19463'::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..88.87 rows=51 width=121) (actual time=0.004..0.018 rows=51.00 loops=1)
  Buffers: shared hit=55
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..8856.56 rows=5106 width=121) (actual time=0.004..0.016 rows=51.00 loops=1)
        Index Cond: ((chat_id = '16247948-069a-41c4-ac39-4960d74af26b'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:26:36.494259+00'::timestamp with time zone, 'bb1d6b63-a42f-456c-904f-517fdec74674'::uuid)))
        Index Searches: 1
        Buffers: shared hit=55
Planning Time: 0.030 ms
Execution Time: 0.024 ms

#### generic

Limit  (cost=0.42..12.82 rows=3 width=121) (actual time=0.004..0.018 rows=51.00 loops=1)
  Buffers: shared hit=55
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..141.00 rows=34 width=121) (actual time=0.003..0.015 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=55
Planning Time: 0.004 ms
Execution Time: 0.022 ms

#### or-cursor

Limit  (cost=0.42..92.93 rows=51 width=121) (actual time=0.699..0.711 rows=51.00 loops=1)
  Buffers: shared hit=4973
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..9262.26 rows=5106 width=121) (actual time=0.699..0.708 rows=51.00 loops=1)
        Index Cond: (chat_id = '16247948-069a-41c4-ac39-4960d74af26b'::uuid)
        Filter: ((created_at > '2026-10-02 04:26:36.494259+00'::timestamp with time zone) OR ((created_at = '2026-10-02 04:26:36.494259+00'::timestamp with time zone) AND (id > 'bb1d6b63-a42f-456c-904f-517fdec74674'::uuid)))
        Rows Removed by Filter: 5000
        Index Searches: 1
        Buffers: shared hit=4973
Planning Time: 0.035 ms
Execution Time: 0.715 ms

#### no-index

Limit  (cost=4223.35..4223.47 rows=51 width=121) (actual time=3.298..3.303 rows=51.00 loops=1)
  Buffers: shared hit=2128
  ->  Sort  (cost=4223.35..4236.11 rows=5106 width=121) (actual time=3.298..3.301 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 49kB
        Buffers: shared hit=2128
        ->  Seq Scan on messages  (cost=0.00..4053.00 rows=5106 width=121) (actual time=0.684..3.042 rows=5000.00 loops=1)
              Filter: ((chat_id = '16247948-069a-41c4-ac39-4960d74af26b'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:26:36.494259+00'::timestamp with time zone, 'bb1d6b63-a42f-456c-904f-517fdec74674'::uuid)))
              Rows Removed by Filter: 105000
              Buffers: shared hit=2128
Planning Time: 0.025 ms
Execution Time: 3.310 ms

#### chat_only

Limit  (cost=2595.67..2595.79 rows=51 width=121) (actual time=1.426..1.430 rows=51.00 loops=1)
  Buffers: shared hit=2031
  ->  Sort  (cost=2595.67..2608.43 rows=5106 width=121) (actual time=1.425..1.427 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 49kB
        Buffers: shared hit=2031
        ->  Bitmap Heap Scan on messages  (cost=118.29..2425.32 rows=5106 width=121) (actual time=0.525..1.201 rows=5000.00 loops=1)
              Recheck Cond: (chat_id = '16247948-069a-41c4-ac39-4960d74af26b'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-02 04:26:36.494259+00'::timestamp with time zone, 'bb1d6b63-a42f-456c-904f-517fdec74674'::uuid))
              Rows Removed by Filter: 5000
              Heap Blocks: exact=2021
              Buffers: shared hit=2031
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..117.02 rows=10230 width=0) (actual time=0.186..0.186 rows=10000.00 loops=1)
                    Index Cond: (chat_id = '16247948-069a-41c4-ac39-4960d74af26b'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=10
Planning Time: 0.050 ms
Execution Time: 1.438 ms

#### covering

Limit  (cost=0.42..5.39 rows=51 width=121) (actual time=0.003..0.009 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.42..498.54 rows=5106 width=121) (actual time=0.003..0.006 rows=51.00 loops=1)
        Index Cond: ((chat_id = '16247948-069a-41c4-ac39-4960d74af26b'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:26:36.494259+00'::timestamp with time zone, 'bb1d6b63-a42f-456c-904f-517fdec74674'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.026 ms
Execution Time: 0.013 ms

### bg 5k: middle, после 2500

#### psql

Limit  (cost=0.42..132.47 rows=51 width=121) (actual time=0.005..0.019 rows=51.00 loops=1)
  Buffers: shared hit=50
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..6465.92 rows=2497 width=121) (actual time=0.005..0.015 rows=51.00 loops=1)
        Index Cond: ((chat_id = '68a617de-3d8f-4b0f-955a-fc36aacd783d'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:26:36.485784+00'::timestamp with time zone, 'c5916956-6a49-4619-b177-d9d2b5ef9dc9'::uuid)))
        Index Searches: 1
        Buffers: shared hit=50
Planning Time: 0.042 ms
Execution Time: 0.025 ms

#### generic

Limit  (cost=0.42..12.82 rows=3 width=121) (actual time=0.006..0.018 rows=51.00 loops=1)
  Buffers: shared hit=50
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..141.00 rows=34 width=121) (actual time=0.006..0.015 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.007 ms
Execution Time: 0.025 ms

#### or-cursor

Limit  (cost=0.42..181.69 rows=51 width=121) (actual time=0.346..0.355 rows=51.00 loops=1)
  Buffers: shared hit=2431
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..8875.46 rows=2497 width=121) (actual time=0.345..0.352 rows=51.00 loops=1)
        Index Cond: (chat_id = '68a617de-3d8f-4b0f-955a-fc36aacd783d'::uuid)
        Filter: ((created_at > '2026-10-02 04:26:36.485784+00'::timestamp with time zone) OR ((created_at = '2026-10-02 04:26:36.485784+00'::timestamp with time zone) AND (id > 'c5916956-6a49-4619-b177-d9d2b5ef9dc9'::uuid)))
        Rows Removed by Filter: 2500
        Index Searches: 1
        Buffers: shared hit=2431
Planning Time: 0.041 ms
Execution Time: 0.360 ms

#### no-index

Limit  (cost=4136.31..4136.43 rows=51 width=121) (actual time=2.927..2.930 rows=51.00 loops=1)
  Buffers: shared hit=2128
  ->  Sort  (cost=4136.31..4142.55 rows=2497 width=121) (actual time=2.926..2.928 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 44kB
        Buffers: shared hit=2128
        ->  Seq Scan on messages  (cost=0.00..4053.00 rows=2497 width=121) (actual time=0.615..2.805 rows=2500.00 loops=1)
              Filter: ((chat_id = '68a617de-3d8f-4b0f-955a-fc36aacd783d'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:26:36.485784+00'::timestamp with time zone, 'c5916956-6a49-4619-b177-d9d2b5ef9dc9'::uuid)))
              Rows Removed by Filter: 107500
              Buffers: shared hit=2128
Planning Time: 0.032 ms
Execution Time: 2.936 ms

#### chat_only

Limit  (cost=2357.25..2357.37 rows=51 width=121) (actual time=0.894..0.898 rows=51.00 loops=1)
  Buffers: shared hit=1867
  ->  Sort  (cost=2357.25..2363.49 rows=2497 width=121) (actual time=0.894..0.896 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 44kB
        Buffers: shared hit=1867
        ->  Bitmap Heap Scan on messages  (cost=58.42..2273.94 rows=2497 width=121) (actual time=0.364..0.783 rows=2500.00 loops=1)
              Recheck Cond: (chat_id = '68a617de-3d8f-4b0f-955a-fc36aacd783d'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-02 04:26:36.485784+00'::timestamp with time zone, 'c5916956-6a49-4619-b177-d9d2b5ef9dc9'::uuid))
              Rows Removed by Filter: 2500
              Heap Blocks: exact=1861
              Buffers: shared hit=1867
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..57.80 rows=5001 width=0) (actual time=0.144..0.144 rows=5000.00 loops=1)
                    Index Cond: (chat_id = '68a617de-3d8f-4b0f-955a-fc36aacd783d'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=6
Planning Time: 0.028 ms
Execution Time: 0.903 ms

#### covering

Limit  (cost=0.42..5.44 rows=51 width=121) (actual time=0.003..0.009 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.42..246.36 rows=2497 width=121) (actual time=0.003..0.006 rows=51.00 loops=1)
        Index Cond: ((chat_id = '68a617de-3d8f-4b0f-955a-fc36aacd783d'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:26:36.485784+00'::timestamp with time zone, 'c5916956-6a49-4619-b177-d9d2b5ef9dc9'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.022 ms
Execution Time: 0.013 ms

### bg 400: middle, после 200

#### psql

Limit  (cost=0.42..198.75 rows=51 width=121) (actual time=0.004..0.021 rows=51.00 loops=1)
  Buffers: shared hit=49
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..848.19 rows=218 width=121) (actual time=0.004..0.018 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'e4673889-7eb9-410f-92ad-9bfa26894ef3'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:26:36.332907+00'::timestamp with time zone, '687d11cd-352d-41fe-a556-2ff16f09650b'::uuid)))
        Index Searches: 1
        Buffers: shared hit=49
Planning Time: 0.045 ms
Execution Time: 0.028 ms

#### generic

Limit  (cost=0.42..12.82 rows=3 width=121) (actual time=0.014..0.025 rows=51.00 loops=1)
  Buffers: shared hit=49
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..141.00 rows=34 width=121) (actual time=0.014..0.022 rows=51.00 loops=1)
        Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
        Index Searches: 1
        Buffers: shared hit=49
Planning Time: 0.003 ms
Execution Time: 0.030 ms

#### or-cursor

Limit  (cost=0.42..378.89 rows=51 width=121) (actual time=0.047..0.061 rows=51.00 loops=1)
  Buffers: shared hit=238
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..1618.20 rows=218 width=121) (actual time=0.047..0.058 rows=51.00 loops=1)
        Index Cond: (chat_id = 'e4673889-7eb9-410f-92ad-9bfa26894ef3'::uuid)
        Filter: ((created_at > '2026-10-02 04:26:36.332907+00'::timestamp with time zone) OR ((created_at = '2026-10-02 04:26:36.332907+00'::timestamp with time zone) AND (id > '687d11cd-352d-41fe-a556-2ff16f09650b'::uuid)))
        Rows Removed by Filter: 200
        Index Searches: 1
        Buffers: shared hit=238
Planning Time: 0.040 ms
Execution Time: 0.066 ms

#### no-index

Limit  (cost=4060.27..4060.40 rows=51 width=121) (actual time=2.583..2.587 rows=51.00 loops=1)
  Buffers: shared hit=2128
  ->  Sort  (cost=4060.27..4060.82 rows=218 width=121) (actual time=2.583..2.585 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 38kB
        Buffers: shared hit=2128
        ->  Seq Scan on messages  (cost=0.00..4053.00 rows=218 width=121) (actual time=0.621..2.569 rows=200.00 loops=1)
              Filter: ((chat_id = 'e4673889-7eb9-410f-92ad-9bfa26894ef3'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:26:36.332907+00'::timestamp with time zone, '687d11cd-352d-41fe-a556-2ff16f09650b'::uuid)))
              Rows Removed by Filter: 109800
              Buffers: shared hit=2128
Planning Time: 0.042 ms
Execution Time: 2.595 ms

#### chat_only

Limit  (cost=1094.04..1094.17 rows=51 width=121) (actual time=0.148..0.152 rows=51.00 loops=1)
  Buffers: shared hit=365
  ->  Sort  (cost=1094.04..1094.58 rows=218 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=365
        ->  Bitmap Heap Scan on messages  (cost=7.62..1086.77 rows=218 width=121) (actual time=0.062..0.132 rows=200.00 loops=1)
              Recheck Cond: (chat_id = 'e4673889-7eb9-410f-92ad-9bfa26894ef3'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-02 04:26:36.332907+00'::timestamp with time zone, '687d11cd-352d-41fe-a556-2ff16f09650b'::uuid))
              Rows Removed by Filter: 200
              Heap Blocks: exact=363
              Buffers: shared hit=365
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..7.56 rows=436 width=0) (actual time=0.016..0.016 rows=400.00 loops=1)
                    Index Cond: (chat_id = 'e4673889-7eb9-410f-92ad-9bfa26894ef3'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
Planning Time: 0.030 ms
Execution Time: 0.158 ms

#### covering

Limit  (cost=0.42..6.12 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..24.78 rows=218 width=121) (actual time=0.005..0.010 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'e4673889-7eb9-410f-92ad-9bfa26894ef3'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:26:36.332907+00'::timestamp with time zone, '687d11cd-352d-41fe-a556-2ff16f09650b'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.037 ms
Execution Time: 0.020 ms

### bg 10: first, после 0

#### psql

Limit  (cost=57.56..57.60 rows=14 width=121) (actual time=0.013..0.013 rows=10.00 loops=1)
  Buffers: shared hit=13
  ->  Sort  (cost=57.56..57.60 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=13
        ->  Bitmap Heap Scan on messages  (cost=4.53..57.29 rows=14 width=121) (actual time=0.007..0.010 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '0efbcbe9-fc38-4fdd-b0d4-fee054e19463'::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.003..0.004 rows=10.00 loops=1)
                    Index Cond: (chat_id = '0efbcbe9-fc38-4fdd-b0d4-fee054e19463'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=3
Planning Time: 0.037 ms
Execution Time: 0.020 ms

#### generic

Limit  (cost=0.42..40.56 rows=10 width=121) (actual time=0.002..0.004 rows=10.00 loops=1)
  Buffers: shared hit=13
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..409.92 rows=102 width=121) (actual time=0.002..0.003 rows=10.00 loops=1)
        Index Cond: (chat_id = $1)
        Index Searches: 1
        Buffers: shared hit=13
Planning Time: 0.002 ms
Execution Time: 0.007 ms

#### no-index

Limit  (cost=3503.27..3503.30 rows=14 width=121) (actual time=2.440..2.441 rows=10.00 loops=1)
  Buffers: shared hit=2128
  ->  Sort  (cost=3503.27..3503.30 rows=14 width=121) (actual time=2.439..2.440 rows=10.00 loops=1)
        Sort Key: created_at, id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=2128
        ->  Seq Scan on messages  (cost=0.00..3503.00 rows=14 width=121) (actual time=0.136..2.437 rows=10.00 loops=1)
              Filter: (chat_id = '0efbcbe9-fc38-4fdd-b0d4-fee054e19463'::uuid)
              Rows Removed by Filter: 109990
              Buffers: shared hit=2128
Planning Time: 0.029 ms
Execution Time: 2.445 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 = '0efbcbe9-fc38-4fdd-b0d4-fee054e19463'::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 = '0efbcbe9-fc38-4fdd-b0d4-fee054e19463'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
Planning Time: 0.025 ms
Execution Time: 0.019 ms

#### covering

Limit  (cost=0.42..4.66 rows=14 width=121) (actual time=0.004..0.005 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.003..0.004 rows=10.00 loops=1)
        Index Cond: (chat_id = '0efbcbe9-fc38-4fdd-b0d4-fee054e19463'::uuid)
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=4
Planning Time: 0.021 ms
Execution Time: 0.009 ms
