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

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

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

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

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

duration: 0.147 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id 
 LIMIT $2::INTEGER
Query Parameters: $1 = 'e89c392b-46c2-4a1d-a798-2ca1f4787398', $2 = '51'
Limit  (cost=0.28..24.74 rows=51 width=153) (actual time=0.040..0.113 rows=51.00 loops=1)
  Buffers: shared hit=32
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..479.84 rows=1000 width=153) (actual time=0.039..0.109 rows=51.00 loops=1)
        Index Cond: (chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid)
        Index Searches: 1
        Buffers: shared hit=32
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

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

duration: 0.121 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
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 = 'e89c392b-46c2-4a1d-a798-2ca1f4787398', $2 = '2026-10-31 20:19:42.570021+00', $3 = 'cb5ad463-2531-42b3-8858-75d9f4d51aa0', $4 = '51'
Limit  (cost=0.28..45.12 rows=51 width=153) (actual time=0.016..0.085 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..440.73 rows=501 width=153) (actual time=0.016..0.082 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:42.570021+00'::timestamp with time zone, 'cb5ad463-2531-42b3-8858-75d9f4d51aa0'::uuid)))
        Index Searches: 1
        Buffers: shared hit=51
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### read 1k: last, после 900

duration: 0.240 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
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 = 'e89c392b-46c2-4a1d-a798-2ca1f4787398', $2 = '2026-10-31 20:19:45.708241+00', $3 = '10ad2de8-b01a-4b52-a049-d90b26b5aa5b', $4 = '51'
Limit  (cost=120.17..120.30 rows=51 width=153) (actual time=0.193..0.198 rows=51.00 loops=1)
  Buffers: shared hit=28
  ->  Sort  (cost=120.17..120.42 rows=100 width=153) (actual time=0.193..0.195 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=28
        ->  Bitmap Heap Scan on messages  (cost=9.31..116.85 rows=100 width=153) (actual time=0.029..0.144 rows=100.00 loops=1)
              Recheck Cond: ((chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:45.708241+00'::timestamp with time zone, '10ad2de8-b01a-4b52-a049-d90b26b5aa5b'::uuid)))
              Heap Blocks: exact=20
              Buffers: shared hit=22
              ->  Bitmap Index Scan on ix_messages_chat_id_created_at_id  (cost=0.00..9.28 rows=100 width=0) (actual time=0.010..0.010 rows=100.00 loops=1)
                    Index Cond: ((chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:45.708241+00'::timestamp with time zone, '10ad2de8-b01a-4b52-a049-d90b26b5aa5b'::uuid)))
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=100)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                            Filter: (message_id = messages.id)

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

duration: 0.106 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id 
 LIMIT $2::INTEGER
Query Parameters: $1 = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3', $2 = '51'
Limit  (cost=0.28..24.74 rows=51 width=153) (actual time=0.010..0.078 rows=51.00 loops=1)
  Buffers: shared hit=37
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..479.84 rows=1000 width=153) (actual time=0.009..0.075 rows=51.00 loops=1)
        Index Cond: (chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid)
        Index Searches: 1
        Buffers: shared hit=37
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### bg 1k: middle, после 500

duration: 0.105 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
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 = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3', $2 = '2026-10-31 20:19:42.593379+00', $3 = '41e10366-ac36-468e-965e-54b7bd0290ce', $4 = '51'
Limit  (cost=0.28..45.38 rows=51 width=153) (actual time=0.012..0.075 rows=51.00 loops=1)
  Buffers: shared hit=53
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..440.63 rows=498 width=153) (actual time=0.011..0.072 rows=51.00 loops=1)
        Index Cond: ((chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:42.593379+00'::timestamp with time zone, '41e10366-ac36-468e-965e-54b7bd0290ce'::uuid)))
        Index Searches: 1
        Buffers: shared hit=53
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)

### bg 1k: last, после 900

duration: 0.156 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
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 = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3', $2 = '2026-10-31 20:19:45.714312+00', $3 = '630019db-1266-44cc-b5d6-fcb3edda0b30', $4 = '51'
Limit  (cost=120.09..120.21 rows=51 width=153) (actual time=0.125..0.130 rows=51.00 loops=1)
  Buffers: shared hit=18
  ->  Sort  (cost=120.09..120.33 rows=99 width=153) (actual time=0.125..0.127 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=18
        ->  Bitmap Heap Scan on messages  (cost=9.30..116.81 rows=99 width=153) (actual time=0.019..0.104 rows=100.00 loops=1)
              Recheck Cond: ((chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:45.714312+00'::timestamp with time zone, '630019db-1266-44cc-b5d6-fcb3edda0b30'::uuid)))
              Heap Blocks: exact=16
              Buffers: shared hit=18
              ->  Bitmap Index Scan on ix_messages_chat_id_created_at_id  (cost=0.00..9.27 rows=99 width=0) (actual time=0.009..0.009 rows=100.00 loops=1)
                    Index Cond: ((chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:45.714312+00'::timestamp with time zone, '630019db-1266-44cc-b5d6-fcb3edda0b30'::uuid)))
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                            Filter: (message_id = messages.id)

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

duration: 0.231 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id 
 LIMIT $2::INTEGER
Query Parameters: $1 = '3694cad0-8739-4553-ba44-604daaaa3382', $2 = '51'
Limit  (cost=119.42..119.55 rows=51 width=153) (actual time=0.197..0.201 rows=51.00 loops=1)
  Buffers: shared hit=64
  ->  Sort  (cost=119.42..119.67 rows=100 width=153) (actual time=0.197..0.199 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=64
        ->  Bitmap Heap Scan on messages  (cost=9.06..116.10 rows=100 width=153) (actual time=0.033..0.175 rows=100.00 loops=1)
              Recheck Cond: (chat_id = '3694cad0-8739-4553-ba44-604daaaa3382'::uuid)
              Heap Blocks: exact=62
              Buffers: shared hit=64
              ->  Bitmap Index Scan on ix_messages_chat_id_created_at_id  (cost=0.00..9.03 rows=100 width=0) (actual time=0.010..0.010 rows=100.00 loops=1)
                    Index Cond: (chat_id = '3694cad0-8739-4553-ba44-604daaaa3382'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=100)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                            Filter: (message_id = messages.id)

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

duration: 0.041 ms  plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at, array((SELECT message_attachments.attachment_id 
FROM message_attachments 
WHERE message_attachments.message_id = messages.id ORDER BY message_attachments.position)) AS attachment_ids 
FROM messages 
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id 
 LIMIT $2::INTEGER
Query Parameters: $1 = '173df049-0e59-461f-afe3-8a15542fa0c5', $2 = '51'
Limit  (cost=35.36..35.39 rows=10 width=153) (actual time=0.036..0.037 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=35.36..35.39 rows=10 width=153) (actual time=0.036..0.036 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=12
        ->  Bitmap Heap Scan on messages  (cost=4.36..35.20 rows=10 width=153) (actual time=0.014..0.032 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '173df049-0e59-461f-afe3-8a15542fa0c5'::uuid)
              Heap Blocks: exact=10
              Buffers: shared hit=12
              ->  Bitmap Index Scan on ix_messages_chat_id_created_at_id  (cost=0.00..4.36 rows=10 width=0) (actual time=0.005..0.005 rows=10.00 loops=1)
                    Index Cond: (chat_id = '173df049-0e59-461f-afe3-8a15542fa0c5'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                            Filter: (message_id = messages.id)

## Страницы сравнения в 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 1k: middle, после 500

#### psql

Limit  (cost=0.28..45.12 rows=51 width=153) (actual time=0.009..0.057 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..440.73 rows=501 width=153) (actual time=0.009..0.054 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:42.570021+00'::timestamp with time zone, 'cb5ad463-2531-42b3-8858-75d9f4d51aa0'::uuid)))
        Index Searches: 1
        Buffers: shared hit=51
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.048 ms
Execution Time: 0.065 ms

#### generic

Limit  (cost=0.28..7.93 rows=2 width=153) (actual time=0.009..0.064 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..92.02 rows=24 width=153) (actual time=0.009..0.061 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
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning Time: 0.006 ms
Execution Time: 0.072 ms

#### or-cursor

Limit  (cost=0.28..49.10 rows=51 width=153) (actual time=0.085..0.131 rows=51.00 loops=1)
  Buffers: shared hit=525
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..479.86 rows=501 width=153) (actual time=0.085..0.128 rows=51.00 loops=1)
        Index Cond: (chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid)
        Filter: ((created_at > '2026-10-31 20:19:42.570021+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:19:42.570021+00'::timestamp with time zone) AND (id > 'cb5ad463-2531-42b3-8858-75d9f4d51aa0'::uuid)))
        Rows Removed by Filter: 500
        Index Searches: 1
        Buffers: shared hit=525
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.046 ms
Execution Time: 0.136 ms

#### no-index

Limit  (cost=203.98..204.11 rows=51 width=153) (actual time=0.545..0.550 rows=51.00 loops=1)
  Buffers: shared hit=101
  ->  Sort  (cost=203.98..205.23 rows=501 width=153) (actual time=0.545..0.547 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 40kB
        Buffers: shared hit=101
        ->  Seq Scan on messages  (cost=0.00..187.27 rows=501 width=153) (actual time=0.077..0.485 rows=500.00 loops=1)
              Filter: ((chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:42.570021+00'::timestamp with time zone, 'cb5ad463-2531-42b3-8858-75d9f4d51aa0'::uuid)))
              Rows Removed by Filter: 4000
              Buffers: shared hit=101
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.073 ms
Execution Time: 0.560 ms

#### chat_only

Limit  (cost=158.64..158.76 rows=51 width=153) (actual time=0.507..0.512 rows=51.00 loops=1)
  Buffers: shared hit=103
  ->  Sort  (cost=158.64..159.89 rows=501 width=153) (actual time=0.507..0.509 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 40kB
        Buffers: shared hit=103
        ->  Bitmap Heap Scan on messages  (cost=15.91..141.92 rows=501 width=153) (actual time=0.053..0.431 rows=500.00 loops=1)
              Recheck Cond: (chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-31 20:19:42.570021+00'::timestamp with time zone, 'cb5ad463-2531-42b3-8858-75d9f4d51aa0'::uuid))
              Rows Removed by Filter: 500
              Heap Blocks: exact=101
              Buffers: shared hit=103
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..15.78 rows=1000 width=0) (actual time=0.011..0.012 rows=1000.00 loops=1)
                    Index Cond: (chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.045 ms
Execution Time: 0.519 ms

#### covering

Limit  (cost=0.28..6.14 rows=51 width=153) (actual time=0.006..0.048 rows=51.00 loops=1)
  Buffers: shared hit=4
  ->  Index Only Scan using bench_variant on messages  (cost=0.28..57.82 rows=501 width=153) (actual time=0.006..0.045 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'e89c392b-46c2-4a1d-a798-2ca1f4787398'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:42.570021+00'::timestamp with time zone, 'cb5ad463-2531-42b3-8858-75d9f4d51aa0'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=4
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.044 ms
Execution Time: 0.055 ms

### bg 1k: middle, после 500

#### psql

Limit  (cost=0.28..45.38 rows=51 width=153) (actual time=0.011..0.072 rows=51.00 loops=1)
  Buffers: shared hit=53
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..440.63 rows=498 width=153) (actual time=0.010..0.070 rows=51.00 loops=1)
        Index Cond: ((chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:42.593379+00'::timestamp with time zone, '41e10366-ac36-468e-965e-54b7bd0290ce'::uuid)))
        Index Searches: 1
        Buffers: shared hit=53
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.067 ms
Execution Time: 0.084 ms

#### generic

Limit  (cost=0.28..7.93 rows=2 width=153) (actual time=0.010..0.060 rows=51.00 loops=1)
  Buffers: shared hit=53
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..92.02 rows=24 width=153) (actual time=0.010..0.057 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
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning Time: 0.007 ms
Execution Time: 0.069 ms

#### or-cursor

Limit  (cost=0.28..49.39 rows=51 width=153) (actual time=0.082..0.139 rows=51.00 loops=1)
  Buffers: shared hit=528
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..479.81 rows=498 width=153) (actual time=0.082..0.137 rows=51.00 loops=1)
        Index Cond: (chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid)
        Filter: ((created_at > '2026-10-31 20:19:42.593379+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:19:42.593379+00'::timestamp with time zone) AND (id > '41e10366-ac36-468e-965e-54b7bd0290ce'::uuid)))
        Rows Removed by Filter: 500
        Index Searches: 1
        Buffers: shared hit=528
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.055 ms
Execution Time: 0.146 ms

#### no-index

Limit  (cost=203.83..203.96 rows=51 width=153) (actual time=0.602..0.605 rows=51.00 loops=1)
  Buffers: shared hit=101
  ->  Sort  (cost=203.83..205.08 rows=498 width=153) (actual time=0.602..0.603 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 40kB
        Buffers: shared hit=101
        ->  Seq Scan on messages  (cost=0.00..187.22 rows=498 width=153) (actual time=0.076..0.544 rows=500.00 loops=1)
              Filter: ((chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:42.593379+00'::timestamp with time zone, '41e10366-ac36-468e-965e-54b7bd0290ce'::uuid)))
              Rows Removed by Filter: 4000
              Buffers: shared hit=101
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=500)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.050 ms
Execution Time: 0.612 ms

#### chat_only

Limit  (cost=158.49..158.62 rows=51 width=153) (actual time=0.442..0.446 rows=51.00 loops=1)
  Buffers: shared hit=101
  ->  Sort  (cost=158.49..159.74 rows=498 width=153) (actual time=0.442..0.443 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 40kB
        Buffers: shared hit=101
        ->  Bitmap Heap Scan on messages  (cost=15.91..141.88 rows=498 width=153) (actual time=0.053..0.390 rows=500.00 loops=1)
              Recheck Cond: (chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-31 20:19:42.593379+00'::timestamp with time zone, '41e10366-ac36-468e-965e-54b7bd0290ce'::uuid))
              Rows Removed by Filter: 500
              Heap Blocks: exact=99
              Buffers: shared hit=101
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..15.78 rows=1000 width=0) (actual time=0.014..0.014 rows=1000.00 loops=1)
                    Index Cond: (chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.037 ms
Execution Time: 0.453 ms

#### covering

Limit  (cost=0.28..6.16 rows=51 width=153) (actual time=0.007..0.042 rows=51.00 loops=1)
  Buffers: shared hit=4
  ->  Index Only Scan using bench_variant on messages  (cost=0.28..57.71 rows=498 width=153) (actual time=0.007..0.040 rows=51.00 loops=1)
        Index Cond: ((chat_id = '1c02e8d5-8fbd-4500-9374-1e4e3f22f2e3'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:19:42.593379+00'::timestamp with time zone, '41e10366-ac36-468e-965e-54b7bd0290ce'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=4
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.056 ms
Execution Time: 0.051 ms

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

#### psql

Limit  (cost=119.42..119.55 rows=51 width=153) (actual time=0.106..0.110 rows=51.00 loops=1)
  Buffers: shared hit=64
  ->  Sort  (cost=119.42..119.67 rows=100 width=153) (actual time=0.106..0.108 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=64
        ->  Bitmap Heap Scan on messages  (cost=9.06..116.10 rows=100 width=153) (actual time=0.014..0.093 rows=100.00 loops=1)
              Recheck Cond: (chat_id = '3694cad0-8739-4553-ba44-604daaaa3382'::uuid)
              Heap Blocks: exact=62
              Buffers: shared hit=64
              ->  Bitmap Index Scan on ix_messages_chat_id_created_at_id  (cost=0.00..9.03 rows=100 width=0) (actual time=0.007..0.007 rows=100.00 loops=1)
                    Index Cond: (chat_id = '3694cad0-8739-4553-ba44-604daaaa3382'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.040 ms
Execution Time: 0.127 ms

#### generic

Limit  (cost=0.28..21.54 rows=7 width=153) (actual time=0.008..0.058 rows=51.00 loops=1)
  Buffers: shared hit=50
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..215.88 rows=71 width=153) (actual time=0.008..0.055 rows=51.00 loops=1)
        Index Cond: (chat_id = $1)
        Index Searches: 1
        Buffers: shared hit=50
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning Time: 0.003 ms
Execution Time: 0.063 ms

#### no-index

Limit  (cost=162.07..162.20 rows=51 width=153) (actual time=0.196..0.200 rows=51.00 loops=1)
  Buffers: shared hit=101
  ->  Sort  (cost=162.07..162.32 rows=100 width=153) (actual time=0.196..0.197 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=101
        ->  Seq Scan on messages  (cost=0.00..158.75 rows=100 width=153) (actual time=0.005..0.182 rows=100.00 loops=1)
              Filter: (chat_id = '3694cad0-8739-4553-ba44-604daaaa3382'::uuid)
              Rows Removed by Filter: 4400
              Buffers: shared hit=101
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.048 ms
Execution Time: 0.207 ms

#### chat_only

Limit  (cost=115.42..115.55 rows=51 width=153) (actual time=0.116..0.120 rows=51.00 loops=1)
  Buffers: shared hit=64
  ->  Sort  (cost=115.42..115.67 rows=100 width=153) (actual time=0.116..0.118 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=64
        ->  Bitmap Heap Scan on messages  (cost=5.06..112.10 rows=100 width=153) (actual time=0.010..0.103 rows=100.00 loops=1)
              Recheck Cond: (chat_id = '3694cad0-8739-4553-ba44-604daaaa3382'::uuid)
              Heap Blocks: exact=62
              Buffers: shared hit=64
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..5.03 rows=100 width=0) (actual time=0.004..0.004 rows=100.00 loops=1)
                    Index Cond: (chat_id = '3694cad0-8739-4553-ba44-604daaaa3382'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=100)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=100)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.038 ms
Execution Time: 0.127 ms

#### covering

Limit  (cost=0.28..6.02 rows=51 width=153) (actual time=0.007..0.042 rows=51.00 loops=1)
  Buffers: shared hit=4
  ->  Index Only Scan using bench_variant on messages  (cost=0.28..11.53 rows=100 width=153) (actual time=0.007..0.040 rows=51.00 loops=1)
        Index Cond: (chat_id = '3694cad0-8739-4553-ba44-604daaaa3382'::uuid)
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=4
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=51)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.053 ms
Execution Time: 0.052 ms

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

#### psql

Limit  (cost=35.36..35.39 rows=10 width=153) (actual time=0.022..0.024 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=35.36..35.39 rows=10 width=153) (actual time=0.022..0.023 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=12
        ->  Bitmap Heap Scan on messages  (cost=4.36..35.20 rows=10 width=153) (actual time=0.009..0.020 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '173df049-0e59-461f-afe3-8a15542fa0c5'::uuid)
              Heap Blocks: exact=10
              Buffers: shared hit=12
              ->  Bitmap Index Scan on ix_messages_chat_id_created_at_id  (cost=0.00..4.36 rows=10 width=0) (actual time=0.003..0.003 rows=10.00 loops=1)
                    Index Cond: (chat_id = '173df049-0e59-461f-afe3-8a15542fa0c5'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.044 ms
Execution Time: 0.032 ms

#### generic

Limit  (cost=0.28..21.54 rows=7 width=153) (actual time=0.007..0.017 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.28..215.88 rows=71 width=153) (actual time=0.007..0.016 rows=10.00 loops=1)
        Index Cond: (chat_id = $1)
        Index Searches: 1
        Buffers: shared hit=12
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                      Filter: (message_id = messages.id)
Planning Time: 0.003 ms
Execution Time: 0.023 ms

#### no-index

Limit  (cost=157.57..157.59 rows=10 width=153) (actual time=0.121..0.123 rows=10.00 loops=1)
  Buffers: shared hit=101
  ->  Sort  (cost=157.57..157.59 rows=10 width=153) (actual time=0.121..0.122 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=101
        ->  Seq Scan on messages  (cost=0.00..157.40 rows=10 width=153) (actual time=0.019..0.118 rows=10.00 loops=1)
              Filter: (chat_id = '173df049-0e59-461f-afe3-8a15542fa0c5'::uuid)
              Rows Removed by Filter: 4490
              Buffers: shared hit=101
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.051 ms
Execution Time: 0.130 ms

#### chat_only

Limit  (cost=35.36..35.39 rows=10 width=153) (actual time=0.020..0.021 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=35.36..35.39 rows=10 width=153) (actual time=0.019..0.020 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=12
        ->  Bitmap Heap Scan on messages  (cost=4.36..35.20 rows=10 width=153) (actual time=0.008..0.018 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '173df049-0e59-461f-afe3-8a15542fa0c5'::uuid)
              Heap Blocks: exact=10
              Buffers: shared hit=12
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..4.36 rows=10 width=0) (actual time=0.003..0.003 rows=10.00 loops=1)
                    Index Cond: (chat_id = '173df049-0e59-461f-afe3-8a15542fa0c5'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=2
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                      Sort Key: message_attachments."position"
                      Sort Method: quicksort  Memory: 25kB
                      ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.036 ms
Execution Time: 0.026 ms

#### covering

Limit  (cost=0.28..4.61 rows=10 width=153) (actual time=0.005..0.011 rows=10.00 loops=1)
  Buffers: shared hit=3
  ->  Index Only Scan using bench_variant on messages  (cost=0.28..4.61 rows=10 width=153) (actual time=0.005..0.011 rows=10.00 loops=1)
        Index Cond: (chat_id = '173df049-0e59-461f-afe3-8a15542fa0c5'::uuid)
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=3
        SubPlan 1
          ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.001..0.001 rows=0.00 loops=10)
                Sort Key: message_attachments."position"
                Sort Method: quicksort  Memory: 25kB
                ->  Seq Scan on message_attachments  (cost=0.00..0.00 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=10)
                      Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.036 ms
Execution Time: 0.017 ms
