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

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

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

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

### read 500k: 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 = '86e0e327-f878-4811-9989-1f5f75eeb7c1', $2 = '51'
Limit  (cost=0.42..7.65 rows=51 width=121) (actual time=0.041..0.052 rows=51.00 loops=1)
  Buffers: shared hit=5 read=1
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..70661.70 rows=498780 width=121) (actual time=0.040..0.047 rows=51.00 loops=1)
        Index Cond: (chat_id = '86e0e327-f878-4811-9989-1f5f75eeb7c1'::uuid)
        Index Searches: 1
        Buffers: shared hit=5 read=1

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

duration: 0.056 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 = '86e0e327-f878-4811-9989-1f5f75eeb7c1', $2 = '2026-10-02 01:47:29.422102+00', $3 = '7153325d-aae0-48ce-bfc3-2c57dc095de6', $4 = '51'
Limit  (cost=0.42..11.28 rows=51 width=121) (actual time=0.013..0.045 rows=51.00 loops=1)
  Buffers: shared hit=49
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..52773.70 rows=247911 width=121) (actual time=0.013..0.042 rows=51.00 loops=1)
        Index Cond: ((chat_id = '86e0e327-f878-4811-9989-1f5f75eeb7c1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:29.422102+00'::timestamp with time zone, '7153325d-aae0-48ce-bfc3-2c57dc095de6'::uuid)))
        Index Searches: 1
        Buffers: shared hit=49

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

duration: 0.054 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 = '86e0e327-f878-4811-9989-1f5f75eeb7c1', $2 = '2026-10-02 01:52:46.819436+00', $3 = '1a534be3-0712-478a-96fb-2ffbd6b9c0d7', $4 = '51'
Limit  (cost=0.42..151.27 rows=51 width=121) (actual time=0.013..0.042 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..1331.42 rows=450 width=121) (actual time=0.012..0.038 rows=51.00 loops=1)
        Index Cond: ((chat_id = '86e0e327-f878-4811-9989-1f5f75eeb7c1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:52:46.819436+00'::timestamp with time zone, '1a534be3-0712-478a-96fb-2ffbd6b9c0d7'::uuid)))
        Index Searches: 1
        Buffers: shared hit=54

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

duration: 0.104 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 = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71', $2 = '51'
Limit  (cost=0.42..128.93 rows=51 width=121) (actual time=0.011..0.092 rows=51.00 loops=1)
  Buffers: shared hit=52
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..11540.23 rows=4580 width=121) (actual time=0.010..0.088 rows=51.00 loops=1)
        Index Cond: (chat_id = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71'::uuid)
        Index Searches: 1
        Buffers: shared hit=52

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

duration: 0.101 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 = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71', $2 = '2026-10-02 01:47:29.359226+00', $3 = '9813512c-a211-41cb-9268-349d9375f97f', $4 = '51'
Limit  (cost=0.42..140.43 rows=51 width=121) (actual time=0.011..0.089 rows=51.00 loops=1)
  Buffers: shared hit=52
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..6251.16 rows=2277 width=121) (actual time=0.011..0.087 rows=51.00 loops=1)
        Index Cond: ((chat_id = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:29.359226+00'::timestamp with time zone, '9813512c-a211-41cb-9268-349d9375f97f'::uuid)))
        Index Searches: 1
        Buffers: shared hit=52

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

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 = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71', $2 = '2026-10-02 01:52:35.24998+00', $3 = 'e6903195-a106-46d1-87e6-22545f7012f6', $4 = '51'
Limit  (cost=0.42..156.08 rows=51 width=121) (actual time=0.009..0.067 rows=51.00 loops=1)
  Buffers: shared hit=50
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..278.16 rows=91 width=121) (actual time=0.009..0.064 rows=51.00 loops=1)
        Index Cond: ((chat_id = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:52:35.24998+00'::timestamp with time zone, 'e6903195-a106-46d1-87e6-22545f7012f6'::uuid)))
        Index Searches: 1
        Buffers: shared hit=50

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

duration: 0.083 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 = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c', $2 = '51'
Limit  (cost=0.42..96.85 rows=31 width=121) (actual time=0.009..0.074 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.85 rows=31 width=121) (actual time=0.009..0.072 rows=51.00 loops=1)
        Index Cond: (chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid)
        Index Searches: 1
        Buffers: shared hit=54

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

duration: 0.086 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 = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c', $2 = '2026-10-02 01:47:28.595217+00', $3 = '28caa587-eeb1-43b4-b075-c83553239c45', $4 = '51'
Limit  (cost=0.42..52.69 rows=16 width=121) (actual time=0.008..0.076 rows=51.00 loops=1)
  Buffers: shared hit=55
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..52.69 rows=16 width=121) (actual time=0.008..0.074 rows=51.00 loops=1)
        Index Cond: ((chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:28.595217+00'::timestamp with time zone, '28caa587-eeb1-43b4-b075-c83553239c45'::uuid)))
        Index Searches: 1
        Buffers: shared hit=55

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

duration: 0.090 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 = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c', $2 = '2026-10-02 01:50:07.477482+00', $3 = 'ba64255c-72c0-420d-b92c-6eb4de404662', $4 = '51'
Limit  (cost=0.42..29.09 rows=8 width=121) (actual time=0.010..0.080 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.09 rows=8 width=121) (actual time=0.009..0.077 rows=51.00 loops=1)
        Index Cond: ((chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:50:07.477482+00'::timestamp with time zone, 'ba64255c-72c0-420d-b92c-6eb4de404662'::uuid)))
        Index Searches: 1
        Buffers: shared hit=55

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

duration: 0.029 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 = '38209d93-1be0-4a33-a2ac-7816bbf57565', $2 = '51'
Limit  (cost=0.42..96.85 rows=31 width=121) (actual time=0.012..0.026 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.85 rows=31 width=121) (actual time=0.012..0.025 rows=10.00 loops=1)
        Index Cond: (chat_id = '38209d93-1be0-4a33-a2ac-7816bbf57565'::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.28 rows=51 width=121) (actual time=0.004..0.017 rows=51.00 loops=1)
  Buffers: shared hit=49
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..52773.70 rows=247911 width=121) (actual time=0.004..0.014 rows=51.00 loops=1)
        Index Cond: ((chat_id = '86e0e327-f878-4811-9989-1f5f75eeb7c1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:29.422102+00'::timestamp with time zone, '7153325d-aae0-48ce-bfc3-2c57dc095de6'::uuid)))
        Index Searches: 1
        Buffers: shared hit=49
Planning Time: 0.039 ms
Execution Time: 0.023 ms

#### generic

Limit  (cost=0.42..116.38 rows=39 width=121) (actual time=0.007..0.020 rows=51.00 loops=1)
  Buffers: shared hit=49
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..1171.85 rows=394 width=121) (actual time=0.006..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=49
Planning Time: 0.010 ms
Execution Time: 0.029 ms

#### or-cursor

Limit  (cost=0.42..15.73 rows=51 width=121) (actual time=43.995..44.007 rows=51.00 loops=1)
  Buffers: shared hit=252338
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..74402.55 rows=247911 width=121) (actual time=43.994..44.004 rows=51.00 loops=1)
        Index Cond: (chat_id = '86e0e327-f878-4811-9989-1f5f75eeb7c1'::uuid)
        Filter: ((created_at > '2026-10-02 01:47:29.422102+00'::timestamp with time zone) OR ((created_at = '2026-10-02 01:47:29.422102+00'::timestamp with time zone) AND (id > '7153325d-aae0-48ce-bfc3-2c57dc095de6'::uuid)))
        Rows Removed by Filter: 250000
        Index Searches: 1
        Buffers: shared hit=252338
Planning Time: 0.128 ms
Execution Time: 44.023 ms

#### no-index

Limit  (cost=20381.20..20387.14 rows=51 width=121) (actual time=17.788..20.445 rows=51.00 loops=1)
  Buffers: shared hit=11590
  ->  Gather Merge  (cost=20381.20..49254.40 rows=247910 width=121) (actual time=17.787..20.441 rows=51.00 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=11590
        ->  Sort  (cost=19381.17..19639.41 rows=103296 width=121) (actual time=16.407..16.409 rows=40.00 loops=3)
              Sort Key: created_at, id
              Sort Method: top-N heapsort  Memory: 46kB
              Buffers: shared hit=11590
              Worker 0:  Sort Method: top-N heapsort  Memory: 39kB
              Worker 1:  Sort Method: top-N heapsort  Memory: 47kB
              ->  Parallel Seq Scan on messages  (cost=0.00..15935.00 rows=103296 width=121) (actual time=5.217..12.387 rows=83333.33 loops=3)
                    Filter: ((chat_id = '86e0e327-f878-4811-9989-1f5f75eeb7c1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:29.422102+00'::timestamp with time zone, '7153325d-aae0-48ce-bfc3-2c57dc095de6'::uuid)))
                    Rows Removed by Filter: 116667
                    Buffers: shared hit=11560
Planning Time: 0.117 ms
Execution Time: 20.463 ms

#### chat_only

Limit  (cost=20381.20..20387.14 rows=51 width=121) (actual time=15.976..18.388 rows=51.00 loops=1)
  Buffers: shared hit=11590
  ->  Gather Merge  (cost=20381.20..49254.40 rows=247910 width=121) (actual time=15.976..18.382 rows=51.00 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=11590
        ->  Sort  (cost=19381.17..19639.41 rows=103296 width=121) (actual time=14.772..14.774 rows=39.67 loops=3)
              Sort Key: created_at, id
              Sort Method: top-N heapsort  Memory: 39kB
              Buffers: shared hit=11590
              Worker 0:  Sort Method: top-N heapsort  Memory: 47kB
              Worker 1:  Sort Method: top-N heapsort  Memory: 46kB
              ->  Parallel Seq Scan on messages  (cost=0.00..15935.00 rows=103296 width=121) (actual time=4.636..10.751 rows=83333.33 loops=3)
                    Filter: ((chat_id = '86e0e327-f878-4811-9989-1f5f75eeb7c1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:29.422102+00'::timestamp with time zone, '7153325d-aae0-48ce-bfc3-2c57dc095de6'::uuid)))
                    Rows Removed by Filter: 116667
                    Buffers: shared hit=11560
Planning Time: 0.094 ms
Execution Time: 18.405 ms

#### covering

Limit  (cost=0.42..5.39 rows=51 width=121) (actual time=0.007..0.012 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.42..24154.65 rows=247911 width=121) (actual time=0.006..0.009 rows=51.00 loops=1)
        Index Cond: ((chat_id = '86e0e327-f878-4811-9989-1f5f75eeb7c1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:29.422102+00'::timestamp with time zone, '7153325d-aae0-48ce-bfc3-2c57dc095de6'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.043 ms
Execution Time: 0.020 ms

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

#### psql

Limit  (cost=0.42..140.43 rows=51 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..6251.16 rows=2277 width=121) (actual time=0.004..0.016 rows=51.00 loops=1)
        Index Cond: ((chat_id = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:29.359226+00'::timestamp with time zone, '9813512c-a211-41cb-9268-349d9375f97f'::uuid)))
        Index Searches: 1
        Buffers: shared hit=52
Planning Time: 0.032 ms
Execution Time: 0.024 ms

#### generic

Limit  (cost=0.42..116.38 rows=39 width=121) (actual time=0.016..0.029 rows=51.00 loops=1)
  Buffers: shared hit=52
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..1171.85 rows=394 width=121) (actual time=0.016..0.026 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.008 ms
Execution Time: 0.038 ms

#### or-cursor

Limit  (cost=0.42..259.66 rows=51 width=121) (actual time=0.541..0.552 rows=51.00 loops=1)
  Buffers: shared hit=2441
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..11574.58 rows=2277 width=121) (actual time=0.540..0.550 rows=51.00 loops=1)
        Index Cond: (chat_id = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71'::uuid)
        Filter: ((created_at > '2026-10-02 01:47:29.359226+00'::timestamp with time zone) OR ((created_at = '2026-10-02 01:47:29.359226+00'::timestamp with time zone) AND (id > '9813512c-a211-41cb-9268-349d9375f97f'::uuid)))
        Rows Removed by Filter: 2500
        Index Searches: 1
        Buffers: shared hit=2441
Planning Time: 0.047 ms
Execution Time: 0.557 ms

#### no-index

Limit  (cost=16981.95..16987.89 rows=51 width=121) (actual time=8.891..11.063 rows=51.00 loops=1)
  Buffers: shared hit=11590
  ->  Gather Merge  (cost=16981.95..17247.15 rows=2277 width=121) (actual time=8.890..11.060 rows=51.00 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=11590
        ->  Sort  (cost=15981.93..15984.30 rows=949 width=121) (actual time=7.863..7.867 rows=41.33 loops=3)
              Sort Key: created_at, id
              Sort Method: top-N heapsort  Memory: 38kB
              Buffers: shared hit=11590
              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..15935.00 rows=949 width=121) (actual time=3.714..7.795 rows=833.33 loops=3)
                    Filter: ((chat_id = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:29.359226+00'::timestamp with time zone, '9813512c-a211-41cb-9268-349d9375f97f'::uuid)))
                    Rows Removed by Filter: 199167
                    Buffers: shared hit=11560
Planning Time: 0.076 ms
Execution Time: 11.077 ms

#### chat_only

Limit  (cost=8258.83..8258.95 rows=51 width=121) (actual time=1.317..1.320 rows=51.00 loops=1)
  Buffers: shared hit=4176
  ->  Sort  (cost=8258.83..8264.52 rows=2277 width=121) (actual time=1.316..1.318 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 38kB
        Buffers: shared hit=4176
        ->  Index Scan using bench_variant on messages  (cost=0.42..8182.86 rows=2277 width=121) (actual time=0.563..1.180 rows=2500.00 loops=1)
              Index Cond: (chat_id = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-02 01:47:29.359226+00'::timestamp with time zone, '9813512c-a211-41cb-9268-349d9375f97f'::uuid))
              Rows Removed by Filter: 2500
              Index Searches: 1
              Buffers: shared hit=4176
Planning Time: 0.047 ms
Execution Time: 1.328 ms

#### covering

Limit  (cost=0.42..5.48 rows=51 width=121) (actual time=0.002..0.008 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.42..225.97 rows=2277 width=121) (actual time=0.002..0.006 rows=51.00 loops=1)
        Index Cond: ((chat_id = '02c152bc-ecdd-4ebd-8867-2ea5eadcdc71'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:29.359226+00'::timestamp with time zone, '9813512c-a211-41cb-9268-349d9375f97f'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.026 ms
Execution Time: 0.013 ms

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

#### psql

Limit  (cost=0.42..52.69 rows=16 width=121) (actual time=0.005..0.023 rows=51.00 loops=1)
  Buffers: shared hit=55
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.42..52.69 rows=16 width=121) (actual time=0.005..0.021 rows=51.00 loops=1)
        Index Cond: ((chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:28.595217+00'::timestamp with time zone, '28caa587-eeb1-43b4-b075-c83553239c45'::uuid)))
        Index Searches: 1
        Buffers: shared hit=55
Planning Time: 0.044 ms
Execution Time: 0.030 ms

#### generic

Limit  (cost=0.42..116.38 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..1171.85 rows=394 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=71.88..71.92 rows=16 width=121) (actual time=0.121..0.125 rows=51.00 loops=1)
  Buffers: shared hit=208
  ->  Sort  (cost=71.88..71.92 rows=16 width=121) (actual time=0.121..0.123 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.03..71.56 rows=16 width=121) (actual time=0.032..0.096 rows=200.00 loops=1)
              Recheck Cond: (((chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid) AND (created_at > '2026-10-02 01:47:28.595217+00'::timestamp with time zone)) OR ((chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid) AND (created_at = '2026-10-02 01:47:28.595217+00'::timestamp with time zone) AND (id > '28caa587-eeb1-43b4-b075-c83553239c45'::uuid)))
              Heap Blocks: exact=200
              Buffers: shared hit=208
              ->  BitmapOr  (cost=9.03..9.03 rows=16 width=0) (actual time=0.014..0.015 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=16 width=0) (actual time=0.013..0.013 rows=200.00 loops=1)
                          Index Cond: ((chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid) AND (created_at > '2026-10-02 01:47:28.595217+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 = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid) AND (created_at = '2026-10-02 01:47:28.595217+00'::timestamp with time zone) AND (id > '28caa587-eeb1-43b4-b075-c83553239c45'::uuid))
                          Index Searches: 1
                          Buffers: shared hit=3
Planning Time: 0.056 ms
Execution Time: 0.135 ms

#### no-index

Limit  (cost=16936.92..16936.96 rows=16 width=121) (actual time=8.835..10.722 rows=51.00 loops=1)
  Buffers: shared hit=11560
  ->  Sort  (cost=16936.92..16936.96 rows=16 width=121) (actual time=8.834..10.719 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 44kB
        Buffers: shared hit=11560
        ->  Gather  (cost=1000.00..16936.60 rows=16 width=121) (actual time=4.717..10.686 rows=200.00 loops=1)
              Workers Planned: 2
              Workers Launched: 2
              Buffers: shared hit=11560
              ->  Parallel Seq Scan on messages  (cost=0.00..15935.00 rows=7 width=121) (actual time=3.824..7.832 rows=66.67 loops=3)
                    Filter: ((chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:28.595217+00'::timestamp with time zone, '28caa587-eeb1-43b4-b075-c83553239c45'::uuid)))
                    Rows Removed by Filter: 199933
                    Buffers: shared hit=11560
Planning Time: 0.052 ms
Execution Time: 10.731 ms

#### chat_only

Limit  (cost=72.34..72.38 rows=16 width=121) (actual time=0.119..0.123 rows=51.00 loops=1)
  Buffers: shared hit=403
  ->  Sort  (cost=72.34..72.38 rows=16 width=121) (actual time=0.119..0.120 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..72.02 rows=16 width=121) (actual time=0.050..0.102 rows=200.00 loops=1)
              Index Cond: (chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-02 01:47:28.595217+00'::timestamp with time zone, '28caa587-eeb1-43b4-b075-c83553239c45'::uuid))
              Rows Removed by Filter: 200
              Index Searches: 1
              Buffers: shared hit=403
Planning Time: 0.030 ms
Execution Time: 0.129 ms

#### covering

Limit  (cost=0.42..4.75 rows=16 width=121) (actual time=0.005..0.013 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.42..4.75 rows=16 width=121) (actual time=0.004..0.009 rows=51.00 loops=1)
        Index Cond: ((chat_id = '74777cf4-ffe8-4103-8fe8-ca2e6fd40f9c'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:47:28.595217+00'::timestamp with time zone, '28caa587-eeb1-43b4-b075-c83553239c45'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.045 ms
Execution Time: 0.020 ms

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

#### psql

Limit  (cost=0.42..96.85 rows=31 width=121) (actual time=0.003..0.007 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.85 rows=31 width=121) (actual time=0.003..0.006 rows=10.00 loops=1)
        Index Cond: (chat_id = '38209d93-1be0-4a33-a2ac-7816bbf57565'::uuid)
        Index Searches: 1
        Buffers: shared hit=13
Planning Time: 0.032 ms
Execution Time: 0.011 ms

#### generic

Limit  (cost=0.42..338.58 rows=118 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..3390.57 rows=1183 width=121) (actual time=0.003..0.007 rows=10.00 loops=1)
        Index Cond: (chat_id = $1)
        Index Searches: 1
        Buffers: shared hit=13
Planning Time: 0.003 ms
Execution Time: 0.012 ms

#### no-index

Limit  (cost=15685.26..15688.87 rows=31 width=121) (actual time=7.988..9.987 rows=10.00 loops=1)
  Buffers: shared hit=11650
  ->  Gather Merge  (cost=15685.26..15688.87 rows=31 width=121) (actual time=7.987..9.985 rows=10.00 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=11650
        ->  Sort  (cost=14685.24..14685.27 rows=13 width=121) (actual time=7.038..7.039 rows=3.33 loops=3)
              Sort Key: created_at, id
              Sort Method: quicksort  Memory: 25kB
              Buffers: shared hit=11650
              Worker 0:  Sort Method: quicksort  Memory: 26kB
              Worker 1:  Sort Method: quicksort  Memory: 25kB
              ->  Parallel Seq Scan on messages  (cost=0.00..14685.00 rows=13 width=121) (actual time=1.367..7.007 rows=3.33 loops=3)
                    Filter: (chat_id = '38209d93-1be0-4a33-a2ac-7816bbf57565'::uuid)
                    Rows Removed by Filter: 199997
                    Buffers: shared hit=11560
Planning Time: 0.071 ms
Execution Time: 9.996 ms

#### chat_only

Limit  (cost=72.64..72.71 rows=31 width=121) (actual time=0.008..0.009 rows=10.00 loops=1)
  Buffers: shared hit=13
  ->  Sort  (cost=72.64..72.71 rows=31 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..71.87 rows=31 width=121) (actual time=0.003..0.006 rows=10.00 loops=1)
              Index Cond: (chat_id = '38209d93-1be0-4a33-a2ac-7816bbf57565'::uuid)
              Index Searches: 1
              Buffers: shared hit=13
Planning Time: 0.024 ms
Execution Time: 0.013 ms

#### covering

Limit  (cost=0.42..4.97 rows=31 width=121) (actual time=0.002..0.003 rows=10.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.42..4.97 rows=31 width=121) (actual time=0.002..0.003 rows=10.00 loops=1)
        Index Cond: (chat_id = '38209d93-1be0-4a33-a2ac-7816bbf57565'::uuid)
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
Planning Time: 0.020 ms
Execution Time: 0.006 ms
