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

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

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

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

### read 500k: first, после 0

duration: 0.035 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 = '9b702935-3c99-43a2-9afc-2350ca1d0614', $2 = '51'
Limit  (cost=0.42..7.64 rows=51 width=121) (actual time=0.015..0.024 rows=51.00 loops=1)
  Buffers: shared hit=6
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..70707.85 rows=499660 width=121) (actual time=0.015..0.021 rows=51.00 loops=1)
        Index Cond: (chat_id = '9b702935-3c99-43a2-9afc-2350ca1d0614'::uuid)
        Index Searches: 1
        Buffers: shared hit=6

### read 500k: middle, после 250000

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 = '9b702935-3c99-43a2-9afc-2350ca1d0614', $2 = '2026-10-02 04:37:10.959452+00', $3 = '9ce86e25-cc9e-4e5b-9f1b-2132fd7c5a10', $4 = '51'
Limit  (cost=0.42..11.23 rows=51 width=121) (actual time=0.014..0.038 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..52856.91 rows=249375 width=121) (actual time=0.013..0.034 rows=51.00 loops=1)
        Index Cond: ((chat_id = '9b702935-3c99-43a2-9afc-2350ca1d0614'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.959452+00'::timestamp with time zone, '9ce86e25-cc9e-4e5b-9f1b-2132fd7c5a10'::uuid)))
        Index Searches: 1
        Buffers: shared hit=54

### read 500k: last, после 499500

duration: 0.045 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 = '9b702935-3c99-43a2-9afc-2350ca1d0614', $2 = '2026-10-02 04:42:24.521415+00', $3 = '10a1dab9-c7cc-47c5-ab8a-47d9f76c53fb', $4 = '51'
Limit  (cost=0.42..151.87 rows=51 width=121) (actual time=0.012..0.034 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..1188.24 rows=400 width=121) (actual time=0.012..0.031 rows=51.00 loops=1)
        Index Cond: ((chat_id = '9b702935-3c99-43a2-9afc-2350ca1d0614'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:42:24.521415+00'::timestamp with time zone, '10a1dab9-c7cc-47c5-ab8a-47d9f76c53fb'::uuid)))
        Index Searches: 1
        Buffers: shared hit=54

### bg 5k: first, после 0

duration: 0.071 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 = 'a3bff6e1-722f-4dec-b574-e93133a8f406', $2 = '51'
Limit  (cost=0.42..127.21 rows=51 width=121) (actual time=0.009..0.060 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..12281.36 rows=4940 width=121) (actual time=0.008..0.057 rows=51.00 loops=1)
        Index Cond: (chat_id = 'a3bff6e1-722f-4dec-b574-e93133a8f406'::uuid)
        Index Searches: 1
        Buffers: shared hit=51

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

duration: 0.058 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 = 'a3bff6e1-722f-4dec-b574-e93133a8f406', $2 = '2026-10-02 04:37:10.912231+00', $3 = 'd69181f0-9119-4169-bb77-f952d8d0b8e7', $4 = '51'
Limit  (cost=0.42..139.26 rows=51 width=121) (actual time=0.011..0.047 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..6713.58 rows=2466 width=121) (actual time=0.011..0.044 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'a3bff6e1-722f-4dec-b574-e93133a8f406'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.912231+00'::timestamp with time zone, 'd69181f0-9119-4169-bb77-f952d8d0b8e7'::uuid)))
        Index Searches: 1
        Buffers: shared hit=51

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

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 = 'a3bff6e1-722f-4dec-b574-e93133a8f406', $2 = '2026-10-02 04:42:12.801083+00', $3 = 'bbbd14be-b675-4e71-9e1e-e95b4e6911d0', $4 = '51'
Limit  (cost=0.42..155.44 rows=51 width=121) (actual time=0.009..0.054 rows=51.00 loops=1)
  Buffers: shared hit=52
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..304.37 rows=100 width=121) (actual time=0.009..0.052 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'a3bff6e1-722f-4dec-b574-e93133a8f406'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:42:12.801083+00'::timestamp with time zone, 'bbbd14be-b675-4e71-9e1e-e95b4e6911d0'::uuid)))
        Index Searches: 1
        Buffers: shared hit=52

### bg 400: 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 = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd', $2 = '51'
Limit  (cost=0.42..96.75 rows=31 width=121) (actual time=0.009..0.056 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..96.75 rows=31 width=121) (actual time=0.009..0.053 rows=51.00 loops=1)
        Index Cond: (chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid)
        Index Searches: 1
        Buffers: shared hit=54

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

duration: 0.067 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 = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd', $2 = '2026-10-02 04:37:10.147031+00', $3 = 'ba892acc-1849-4f46-85ca-b1d38eeead45', $4 = '51'
Limit  (cost=0.42..49.69 rows=15 width=121) (actual time=0.008..0.058 rows=51.00 loops=1)
  Buffers: shared hit=55
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..49.69 rows=15 width=121) (actual time=0.008..0.055 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.147031+00'::timestamp with time zone, 'ba892acc-1849-4f46-85ca-b1d38eeead45'::uuid)))
        Index Searches: 1
        Buffers: shared hit=55

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

duration: 0.079 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 = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd', $2 = '2026-10-02 04:39:46.657452+00', $3 = 'a2abe6e2-7dec-4c64-ad68-c403deac0bec', $4 = '51'
Limit  (cost=0.42..29.07 rows=8 width=121) (actual time=0.010..0.067 rows=51.00 loops=1)
  Buffers: shared hit=55
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..29.07 rows=8 width=121) (actual time=0.010..0.065 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:39:46.657452+00'::timestamp with time zone, 'a2abe6e2-7dec-4c64-ad68-c403deac0bec'::uuid)))
        Index Searches: 1
        Buffers: shared hit=55

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

duration: 0.023 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 = '2a2c17bc-34c9-4624-ace4-ca499a0d93d1', $2 = '51'
Limit  (cost=0.42..96.75 rows=31 width=121) (actual time=0.010..0.020 rows=10.00 loops=1)
  Buffers: shared hit=13
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..96.75 rows=31 width=121) (actual time=0.010..0.019 rows=10.00 loops=1)
        Index Cond: (chat_id = '2a2c17bc-34c9-4624-ace4-ca499a0d93d1'::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 500k: middle, после 250000

#### psql

Limit  (cost=0.42..11.23 rows=51 width=121) (actual time=0.004..0.018 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..52856.91 rows=249375 width=121) (actual time=0.004..0.015 rows=51.00 loops=1)
        Index Cond: ((chat_id = '9b702935-3c99-43a2-9afc-2350ca1d0614'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.959452+00'::timestamp with time zone, '9ce86e25-cc9e-4e5b-9f1b-2132fd7c5a10'::uuid)))
        Index Searches: 1
        Buffers: shared hit=54
Planning Time: 0.035 ms
Execution Time: 0.023 ms

#### generic

Limit  (cost=0.42..115.86 rows=39 width=121) (actual time=0.005..0.020 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..1157.72 rows=391 width=121) (actual time=0.004..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=54
Planning Time: 0.005 ms
Execution Time: 0.027 ms

#### or-cursor

Limit  (cost=0.42..15.65 rows=51 width=121) (actual time=36.107..36.117 rows=51.00 loops=1)
  Buffers: shared hit=252418
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..74455.30 rows=249375 width=121) (actual time=36.106..36.114 rows=51.00 loops=1)
        Index Cond: (chat_id = '9b702935-3c99-43a2-9afc-2350ca1d0614'::uuid)
        Filter: ((created_at > '2026-10-02 04:37:10.959452+00'::timestamp with time zone) OR ((created_at = '2026-10-02 04:37:10.959452+00'::timestamp with time zone) AND (id > '9ce86e25-cc9e-4e5b-9f1b-2132fd7c5a10'::uuid)))
        Rows Removed by Filter: 250000
        Index Searches: 1
        Buffers: shared hit=252418
Planning Time: 0.089 ms
Execution Time: 36.126 ms

#### no-index

Limit  (cost=20400.55..20406.49 rows=51 width=121) (actual time=14.688..16.845 rows=51.00 loops=1)
  Buffers: shared hit=11589
  ->  Gather Merge  (cost=20400.55..49444.26 rows=249374 width=121) (actual time=14.687..16.840 rows=51.00 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=11589
        ->  Sort  (cost=19400.53..19660.29 rows=103906 width=121) (actual time=13.732..13.734 rows=40.33 loops=3)
              Sort Key: created_at, id
              Sort Method: top-N heapsort  Memory: 51kB
              Buffers: shared hit=11589
              Worker 0:  Sort Method: top-N heapsort  Memory: 46kB
              Worker 1:  Sort Method: top-N heapsort  Memory: 45kB
              ->  Parallel Seq Scan on messages  (cost=0.00..15934.00 rows=103906 width=121) (actual time=3.379..9.705 rows=83333.33 loops=3)
                    Filter: ((chat_id = '9b702935-3c99-43a2-9afc-2350ca1d0614'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.959452+00'::timestamp with time zone, '9ce86e25-cc9e-4e5b-9f1b-2132fd7c5a10'::uuid)))
                    Rows Removed by Filter: 116667
                    Buffers: shared hit=11559
Planning Time: 0.054 ms
Execution Time: 16.862 ms

#### chat_only

Limit  (cost=20400.55..20406.49 rows=51 width=121) (actual time=14.511..16.220 rows=51.00 loops=1)
  Buffers: shared hit=11589
  ->  Gather Merge  (cost=20400.55..49444.26 rows=249374 width=121) (actual time=14.511..16.216 rows=51.00 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=11589
        ->  Sort  (cost=19400.53..19660.29 rows=103906 width=121) (actual time=13.568..13.570 rows=40.33 loops=3)
              Sort Key: created_at, id
              Sort Method: top-N heapsort  Memory: 51kB
              Buffers: shared hit=11589
              Worker 0:  Sort Method: top-N heapsort  Memory: 46kB
              Worker 1:  Sort Method: top-N heapsort  Memory: 45kB
              ->  Parallel Seq Scan on messages  (cost=0.00..15934.00 rows=103906 width=121) (actual time=3.283..9.515 rows=83333.33 loops=3)
                    Filter: ((chat_id = '9b702935-3c99-43a2-9afc-2350ca1d0614'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.959452+00'::timestamp with time zone, '9ce86e25-cc9e-4e5b-9f1b-2132fd7c5a10'::uuid)))
                    Rows Removed by Filter: 116667
                    Buffers: shared hit=11559
Planning Time: 0.069 ms
Execution Time: 16.230 ms

#### covering

Limit  (cost=0.42..5.39 rows=51 width=121) (actual time=0.006..0.013 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.42..24299.92 rows=249375 width=121) (actual time=0.005..0.010 rows=51.00 loops=1)
        Index Cond: ((chat_id = '9b702935-3c99-43a2-9afc-2350ca1d0614'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.959452+00'::timestamp with time zone, '9ce86e25-cc9e-4e5b-9f1b-2132fd7c5a10'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.048 ms
Execution Time: 0.021 ms

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

#### psql

Limit  (cost=0.42..139.26 rows=51 width=121) (actual time=0.006..0.020 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..6713.58 rows=2466 width=121) (actual time=0.006..0.017 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'a3bff6e1-722f-4dec-b574-e93133a8f406'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.912231+00'::timestamp with time zone, 'd69181f0-9119-4169-bb77-f952d8d0b8e7'::uuid)))
        Index Searches: 1
        Buffers: shared hit=51
Planning Time: 0.040 ms
Execution Time: 0.027 ms

#### generic

Limit  (cost=0.42..115.86 rows=39 width=121) (actual time=0.008..0.023 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..1157.72 rows=391 width=121) (actual time=0.007..0.019 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.008 ms
Execution Time: 0.030 ms

#### or-cursor

Limit  (cost=0.42..255.18 rows=51 width=121) (actual time=0.487..0.500 rows=51.00 loops=1)
  Buffers: shared hit=2448
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..12318.41 rows=2466 width=121) (actual time=0.487..0.497 rows=51.00 loops=1)
        Index Cond: (chat_id = 'a3bff6e1-722f-4dec-b574-e93133a8f406'::uuid)
        Filter: ((created_at > '2026-10-02 04:37:10.912231+00'::timestamp with time zone) OR ((created_at = '2026-10-02 04:37:10.912231+00'::timestamp with time zone) AND (id > 'd69181f0-9119-4169-bb77-f952d8d0b8e7'::uuid)))
        Rows Removed by Filter: 2500
        Index Searches: 1
        Buffers: shared hit=2448
Planning Time: 0.044 ms
Execution Time: 0.506 ms

#### no-index

Limit  (cost=16985.45..16991.39 rows=51 width=121) (actual time=8.282..10.272 rows=51.00 loops=1)
  Buffers: shared hit=11589
  ->  Gather Merge  (cost=16985.45..17272.66 rows=2466 width=121) (actual time=8.281..10.267 rows=51.00 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=11589
        ->  Sort  (cost=15985.43..15988.00 rows=1028 width=121) (actual time=7.307..7.309 rows=40.33 loops=3)
              Sort Key: created_at, id
              Sort Method: top-N heapsort  Memory: 38kB
              Buffers: shared hit=11589
              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..15934.00 rows=1028 width=121) (actual time=3.122..7.242 rows=833.33 loops=3)
                    Filter: ((chat_id = 'a3bff6e1-722f-4dec-b574-e93133a8f406'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.912231+00'::timestamp with time zone, 'd69181f0-9119-4169-bb77-f952d8d0b8e7'::uuid)))
                    Rows Removed by Filter: 199167
                    Buffers: shared hit=11559
Planning Time: 0.065 ms
Execution Time: 10.282 ms

#### chat_only

Limit  (cost=8772.49..8772.62 rows=51 width=121) (actual time=1.335..1.338 rows=51.00 loops=1)
  Buffers: shared hit=4168
  ->  Sort  (cost=8772.49..8778.65 rows=2466 width=121) (actual time=1.334..1.336 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 39kB
        Buffers: shared hit=4168
        ->  Index Scan using bench_variant on messages  (cost=0.42..8690.22 rows=2466 width=121) (actual time=0.561..1.199 rows=2500.00 loops=1)
              Index Cond: (chat_id = 'a3bff6e1-722f-4dec-b574-e93133a8f406'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-02 04:37:10.912231+00'::timestamp with time zone, 'd69181f0-9119-4169-bb77-f952d8d0b8e7'::uuid))
              Rows Removed by Filter: 2500
              Index Searches: 1
              Buffers: shared hit=4168
Planning Time: 0.047 ms
Execution Time: 1.345 ms

#### covering

Limit  (cost=0.42..5.42 rows=51 width=121) (actual time=0.007..0.013 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.42..241.75 rows=2466 width=121) (actual time=0.006..0.010 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'a3bff6e1-722f-4dec-b574-e93133a8f406'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.912231+00'::timestamp with time zone, 'd69181f0-9119-4169-bb77-f952d8d0b8e7'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.045 ms
Execution Time: 0.021 ms

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

#### psql

Limit  (cost=0.42..49.69 rows=15 width=121) (actual time=0.005..0.026 rows=51.00 loops=1)
  Buffers: shared hit=55
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..49.69 rows=15 width=121) (actual time=0.005..0.022 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.147031+00'::timestamp with time zone, 'ba892acc-1849-4f46-85ca-b1d38eeead45'::uuid)))
        Index Searches: 1
        Buffers: shared hit=55
Planning Time: 0.037 ms
Execution Time: 0.032 ms

#### generic

Limit  (cost=0.42..115.86 rows=39 width=121) (actual time=0.005..0.024 rows=51.00 loops=1)
  Buffers: shared hit=55
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..1157.72 rows=391 width=121) (actual time=0.004..0.021 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.005 ms
Execution Time: 0.030 ms

#### or-cursor

Limit  (cost=67.99..68.03 rows=15 width=121) (actual time=0.105..0.109 rows=51.00 loops=1)
  Buffers: shared hit=208
  ->  Sort  (cost=67.99..68.03 rows=15 width=121) (actual time=0.105..0.107 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 38kB
        Buffers: shared hit=208
        ->  Bitmap Heap Scan on messages  (cost=9.02..67.70 rows=15 width=121) (actual time=0.028..0.088 rows=200.00 loops=1)
              Recheck Cond: (((chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid) AND (created_at > '2026-10-02 04:37:10.147031+00'::timestamp with time zone)) OR ((chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid) AND (created_at = '2026-10-02 04:37:10.147031+00'::timestamp with time zone) AND (id > 'ba892acc-1849-4f46-85ca-b1d38eeead45'::uuid)))
              Heap Blocks: exact=200
              Buffers: shared hit=208
              ->  BitmapOr  (cost=9.02..9.02 rows=15 width=0) (actual time=0.014..0.014 rows=0.00 loops=1)
                    Buffers: shared hit=8
                    ->  Bitmap Index Scan on ix_messages_chat_id_created_at_id  (cost=0.00..4.58 rows=15 width=0) (actual time=0.013..0.013 rows=200.00 loops=1)
                          Index Cond: ((chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid) AND (created_at > '2026-10-02 04:37:10.147031+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.44 rows=1 width=0) (actual time=0.001..0.001 rows=0.00 loops=1)
                          Index Cond: ((chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid) AND (created_at = '2026-10-02 04:37:10.147031+00'::timestamp with time zone) AND (id > 'ba892acc-1849-4f46-85ca-b1d38eeead45'::uuid))
                          Index Searches: 1
                          Buffers: shared hit=3
Planning Time: 0.044 ms
Execution Time: 0.118 ms

#### no-index

Limit  (cost=16934.10..16935.73 rows=14 width=121) (actual time=8.152..9.433 rows=51.00 loops=1)
  Buffers: shared hit=11589
  ->  Gather Merge  (cost=16934.10..16935.73 rows=14 width=121) (actual time=8.152..9.431 rows=51.00 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=11589
        ->  Sort  (cost=15934.08..15934.09 rows=6 width=121) (actual time=7.162..7.163 rows=39.33 loops=3)
              Sort Key: created_at, id
              Sort Method: quicksort  Memory: 41kB
              Buffers: shared hit=11589
              Worker 0:  Sort Method: quicksort  Memory: 42kB
              Worker 1:  Sort Method: quicksort  Memory: 43kB
              ->  Parallel Seq Scan on messages  (cost=0.00..15934.00 rows=6 width=121) (actual time=3.554..7.141 rows=66.67 loops=3)
                    Filter: ((chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.147031+00'::timestamp with time zone, 'ba892acc-1849-4f46-85ca-b1d38eeead45'::uuid)))
                    Rows Removed by Filter: 199933
                    Buffers: shared hit=11559
Planning Time: 0.058 ms
Execution Time: 9.445 ms

#### chat_only

Limit  (cost=72.14..72.18 rows=15 width=121) (actual time=0.124..0.127 rows=51.00 loops=1)
  Buffers: shared hit=403
  ->  Sort  (cost=72.14..72.18 rows=15 width=121) (actual time=0.124..0.125 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 38kB
        Buffers: shared hit=403
        ->  Index Scan using bench_variant on messages  (cost=0.42..71.84 rows=15 width=121) (actual time=0.054..0.107 rows=200.00 loops=1)
              Index Cond: (chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-02 04:37:10.147031+00'::timestamp with time zone, 'ba892acc-1849-4f46-85ca-b1d38eeead45'::uuid))
              Rows Removed by Filter: 200
              Index Searches: 1
              Buffers: shared hit=403
Planning Time: 0.045 ms
Execution Time: 0.134 ms

#### covering

Limit  (cost=0.42..4.73 rows=15 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..4.73 rows=15 width=121) (actual time=0.004..0.008 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'e36b0a2c-5c95-44b1-9fac-08d2302a80cd'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 04:37:10.147031+00'::timestamp with time zone, 'ba892acc-1849-4f46-85ca-b1d38eeead45'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.036 ms
Execution Time: 0.018 ms

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

#### psql

Limit  (cost=0.42..96.75 rows=31 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..96.75 rows=31 width=121) (actual time=0.004..0.007 rows=10.00 loops=1)
        Index Cond: (chat_id = '2a2c17bc-34c9-4624-ace4-ca499a0d93d1'::uuid)
        Index Searches: 1
        Buffers: shared hit=13
Planning Time: 0.039 ms
Execution Time: 0.012 ms

#### generic

Limit  (cost=0.42..335.15 rows=117 width=121) (actual time=0.005..0.009 rows=10.00 loops=1)
  Buffers: shared hit=13
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..3353.38 rows=1172 width=121) (actual time=0.005..0.007 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.015 ms

#### no-index

Limit  (cost=15684.26..15687.87 rows=31 width=121) (actual time=7.798..9.758 rows=10.00 loops=1)
  Buffers: shared hit=11649
  ->  Gather Merge  (cost=15684.26..15687.87 rows=31 width=121) (actual time=7.798..9.757 rows=10.00 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=11649
        ->  Sort  (cost=14684.24..14684.27 rows=13 width=121) (actual time=6.819..6.819 rows=3.33 loops=3)
              Sort Key: created_at, id
              Sort Method: quicksort  Memory: 27kB
              Buffers: shared hit=11649
              Worker 0:  Sort Method: quicksort  Memory: 25kB
              Worker 1:  Sort Method: quicksort  Memory: 25kB
              ->  Parallel Seq Scan on messages  (cost=0.00..14684.00 rows=13 width=121) (actual time=3.495..6.791 rows=3.33 loops=3)
                    Filter: (chat_id = '2a2c17bc-34c9-4624-ace4-ca499a0d93d1'::uuid)
                    Rows Removed by Filter: 199997
                    Buffers: shared hit=11559
Planning Time: 0.054 ms
Execution Time: 9.766 ms

#### chat_only

Limit  (cost=72.46..72.53 rows=31 width=121) (actual time=0.008..0.009 rows=10.00 loops=1)
  Buffers: shared hit=13
  ->  Sort  (cost=72.46..72.53 rows=31 width=121) (actual time=0.008..0.009 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..71.69 rows=31 width=121) (actual time=0.003..0.007 rows=10.00 loops=1)
              Index Cond: (chat_id = '2a2c17bc-34c9-4624-ace4-ca499a0d93d1'::uuid)
              Index Searches: 1
              Buffers: shared hit=13
Planning Time: 0.023 ms
Execution Time: 0.013 ms

#### covering

Limit  (cost=0.42..4.97 rows=31 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.97 rows=31 width=121) (actual time=0.003..0.004 rows=10.00 loops=1)
        Index Cond: (chat_id = '2a2c17bc-34c9-4624-ace4-ca499a0d93d1'::uuid)
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=4
Planning Time: 0.023 ms
Execution Time: 0.008 ms
