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

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

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

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

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

duration: 0.100 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 = '80b7184b-2dce-467f-80f1-54ef9f806be7', $2 = '51'
Limit  (cost=0.29..11.51 rows=51 width=153) (actual time=0.018..0.071 rows=51.00 loops=1)
  Buffers: shared hit=25
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1100.82 rows=5000 width=153) (actual time=0.018..0.068 rows=51.00 loops=1)
        Index Cond: (chat_id = '80b7184b-2dce-467f-80f1-54ef9f806be7'::uuid)
        Index Searches: 1
        Buffers: shared hit=25
        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 5k: middle, после 2500

duration: 0.113 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 = '80b7184b-2dce-467f-80f1-54ef9f806be7', $2 = '2026-10-31 20:20:52.094182+00', $3 = '451aa0da-83f8-4f73-ac9d-8a4efcc8ecd3', $4 = '51'
Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.022..0.080 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.021..0.076 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
        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 5k: last, после 4900

duration: 0.112 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 = '80b7184b-2dce-467f-80f1-54ef9f806be7', $2 = '2026-10-31 20:20:59.127167+00', $3 = 'd29a2234-a752-4b15-97ca-bc3a5fe16f81', $4 = '51'
Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.022..0.079 rows=51.00 loops=1)
  Buffers: shared hit=53
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.021..0.075 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)

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

duration: 0.253 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 = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008', $2 = '51'
Limit  (cost=0.29..39.63 rows=51 width=153) (actual time=0.033..0.158 rows=51.00 loops=1)
  Buffers: shared hit=45
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..771.85 rows=1000 width=153) (actual time=0.032..0.152 rows=51.00 loops=1)
        Index Cond: (chat_id = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008'::uuid)
        Index Searches: 1
        Buffers: shared hit=45
        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)

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

duration: 0.109 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 = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008', $2 = '2026-10-31 20:20:52.084497+00', $3 = '9087eb30-8899-4c5d-8728-7327affbc2a1', $4 = '51'
Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.018..0.078 rows=51.00 loops=1)
  Buffers: shared hit=52
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.017..0.075 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
        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)

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

duration: 0.114 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 = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008', $2 = '2026-10-31 20:20:57.953096+00', $3 = 'aa061a11-d99b-430e-8d57-8956f75927c3', $4 = '51'
Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.019..0.080 rows=51.00 loops=1)
  Buffers: shared hit=51
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.018..0.077 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)

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

duration: 0.124 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 = '6c3ce1eb-20ca-48e7-af3c-920956940be7', $2 = '51'
Limit  (cost=0.29..154.66 rows=51 width=153) (actual time=0.012..0.077 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..302.99 rows=100 width=153) (actual time=0.012..0.074 rows=51.00 loops=1)
        Index Cond: (chat_id = '6c3ce1eb-20ca-48e7-af3c-920956940be7'::uuid)
        Index Searches: 1
        Buffers: shared hit=47
        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 10: first, после 0

duration: 0.042 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 = '90a36fb9-ae42-4016-a663-65307e1e1e19', $2 = '51'
Limit  (cost=37.85..37.87 rows=10 width=153) (actual time=0.037..0.038 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=37.85..37.87 rows=10 width=153) (actual time=0.036..0.037 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..37.68 rows=10 width=153) (actual time=0.017..0.033 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '90a36fb9-ae42-4016-a663-65307e1e1e19'::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.006..0.006 rows=10.00 loops=1)
                    Index Cond: (chat_id = '90a36fb9-ae42-4016-a663-65307e1e1e19'::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 5k: middle, после 2500

#### psql

Limit  (cost=0.29..18.68 rows=51 width=153) (actual time=0.011..0.061 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..901.43 rows=2499 width=153) (actual time=0.011..0.058 rows=51.00 loops=1)
        Index Cond: ((chat_id = '80b7184b-2dce-467f-80f1-54ef9f806be7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:20:52.094182+00'::timestamp with time zone, '451aa0da-83f8-4f73-ac9d-8a4efcc8ecd3'::uuid)))
        Index Searches: 1
        Buffers: shared hit=54
        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.060 ms
Execution Time: 0.070 ms

#### generic

Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.014..0.070 rows=51.00 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.013..0.064 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
        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.011 ms
Execution Time: 0.081 ms

#### or-cursor

Limit  (cost=0.29..22.74 rows=51 width=153) (actual time=0.376..0.420 rows=51.00 loops=1)
  Buffers: shared hit=2541
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..1100.80 rows=2499 width=153) (actual time=0.376..0.417 rows=51.00 loops=1)
        Index Cond: (chat_id = '80b7184b-2dce-467f-80f1-54ef9f806be7'::uuid)
        Filter: ((created_at > '2026-10-31 20:20:52.094182+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:20:52.094182+00'::timestamp with time zone) AND (id > '451aa0da-83f8-4f73-ac9d-8a4efcc8ecd3'::uuid)))
        Rows Removed by Filter: 2500
        Index Searches: 1
        Buffers: shared hit=2541
        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.083 ms
Execution Time: 0.431 ms

#### no-index

Limit  (cost=455.61..455.73 rows=51 width=153) (actual time=2.173..2.177 rows=51.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=455.61..461.85 rows=2499 width=153) (actual time=2.173..2.175 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 43kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..372.24 rows=2499 width=153) (actual time=0.165..1.896 rows=2500.00 loops=1)
              Filter: ((chat_id = '80b7184b-2dce-467f-80f1-54ef9f806be7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:20:52.094182+00'::timestamp with time zone, '451aa0da-83f8-4f73-ac9d-8a4efcc8ecd3'::uuid)))
              Rows Removed by Filter: 6000
              Buffers: shared hit=186
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=2500)
                      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=2500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.061 ms
Execution Time: 2.186 ms

#### chat_only

Limit  (cost=455.61..455.73 rows=51 width=153) (actual time=2.048..2.052 rows=51.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=455.61..461.85 rows=2499 width=153) (actual time=2.047..2.049 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 43kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..372.24 rows=2499 width=153) (actual time=0.140..1.778 rows=2500.00 loops=1)
              Filter: ((chat_id = '80b7184b-2dce-467f-80f1-54ef9f806be7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:20:52.094182+00'::timestamp with time zone, '451aa0da-83f8-4f73-ac9d-8a4efcc8ecd3'::uuid)))
              Rows Removed by Filter: 6000
              Buffers: shared hit=186
              SubPlan 1
                ->  Sort  (cost=0.01..0.02 rows=1 width=18) (actual time=0.000..0.000 rows=0.00 loops=2500)
                      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=2500)
                            Filter: (message_id = messages.id)
Planning:
  Buffers: shared hit=3
Planning Time: 0.037 ms
Execution Time: 2.057 ms

#### covering

Limit  (cost=0.41..6.28 rows=51 width=153) (actual time=0.009..0.097 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..287.88 rows=2499 width=153) (actual time=0.009..0.093 rows=51.00 loops=1)
        Index Cond: ((chat_id = '80b7184b-2dce-467f-80f1-54ef9f806be7'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:20:52.094182+00'::timestamp with time zone, '451aa0da-83f8-4f73-ac9d-8a4efcc8ecd3'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
        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.086 ms
Execution Time: 0.108 ms

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

#### psql

Limit  (cost=0.29..74.92 rows=51 width=153) (actual time=0.009..0.069 rows=51.00 loops=1)
  Buffers: shared hit=52
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..731.98 rows=500 width=153) (actual time=0.008..0.066 rows=51.00 loops=1)
        Index Cond: ((chat_id = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:20:52.084497+00'::timestamp with time zone, '9087eb30-8899-4c5d-8728-7327affbc2a1'::uuid)))
        Index Searches: 1
        Buffers: shared hit=52
        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.050 ms
Execution Time: 0.076 ms

#### generic

Limit  (cost=0.29..14.23 rows=4 width=153) (actual time=0.010..0.066 rows=51.00 loops=1)
  Buffers: shared hit=52
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..157.19 rows=45 width=153) (actual time=0.009..0.064 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
        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.076 ms

#### or-cursor

Limit  (cost=0.29..78.98 rows=51 width=153) (actual time=0.100..0.138 rows=51.00 loops=1)
  Buffers: shared hit=530
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..771.85 rows=500 width=153) (actual time=0.100..0.136 rows=51.00 loops=1)
        Index Cond: (chat_id = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008'::uuid)
        Filter: ((created_at > '2026-10-31 20:20:52.084497+00'::timestamp with time zone) OR ((created_at = '2026-10-31 20:20:52.084497+00'::timestamp with time zone) AND (id > '9087eb30-8899-4c5d-8728-7327affbc2a1'::uuid)))
        Rows Removed by Filter: 500
        Index Searches: 1
        Buffers: shared hit=530
        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.048 ms
Execution Time: 0.143 ms

#### no-index

Limit  (cost=358.93..359.06 rows=51 width=153) (actual time=0.633..0.637 rows=51.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=358.93..360.18 rows=500 width=153) (actual time=0.633..0.635 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 40kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..342.25 rows=500 width=153) (actual time=0.143..0.576 rows=500.00 loops=1)
              Filter: ((chat_id = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:20:52.084497+00'::timestamp with time zone, '9087eb30-8899-4c5d-8728-7327affbc2a1'::uuid)))
              Rows Removed by Filter: 8000
              Buffers: shared hit=186
              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.041 ms
Execution Time: 0.643 ms

#### chat_only

Limit  (cost=243.59..243.72 rows=51 width=153) (actual time=0.472..0.476 rows=51.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=243.59..244.84 rows=500 width=153) (actual time=0.472..0.474 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: top-N heapsort  Memory: 40kB
        Buffers: shared hit=186
        ->  Bitmap Heap Scan on messages  (cost=15.91..226.91 rows=500 width=153) (actual time=0.092..0.416 rows=500.00 loops=1)
              Recheck Cond: (chat_id = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-31 20:20:52.084497+00'::timestamp with time zone, '9087eb30-8899-4c5d-8728-7327affbc2a1'::uuid))
              Rows Removed by Filter: 500
              Heap Blocks: exact=183
              Buffers: shared hit=186
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..15.79 rows=1000 width=0) (actual time=0.019..0.019 rows=1000.00 loops=1)
                    Index Cond: (chat_id = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=3
              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.044 ms
Execution Time: 0.483 ms

#### covering

Limit  (cost=0.41..6.28 rows=51 width=153) (actual time=0.004..0.052 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..57.91 rows=500 width=153) (actual time=0.004..0.050 rows=51.00 loops=1)
        Index Cond: ((chat_id = '6ae81a0d-d05a-4b3b-b7f6-2013260aa008'::uuid) AND (ROW(created_at, id) > ROW('2026-10-31 20:20:52.084497+00'::timestamp with time zone, '9087eb30-8899-4c5d-8728-7327affbc2a1'::uuid)))
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
        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.028 ms
Execution Time: 0.058 ms

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

#### psql

Limit  (cost=0.29..154.66 rows=51 width=153) (actual time=0.007..0.064 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..302.99 rows=100 width=153) (actual time=0.007..0.062 rows=51.00 loops=1)
        Index Cond: (chat_id = '6c3ce1eb-20ca-48e7-af3c-920956940be7'::uuid)
        Index Searches: 1
        Buffers: shared hit=47
        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.037 ms
Execution Time: 0.072 ms

#### generic

Limit  (cost=0.29..39.82 rows=14 width=153) (actual time=0.009..0.062 rows=51.00 loops=1)
  Buffers: shared hit=47
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..381.47 rows=135 width=153) (actual time=0.009..0.059 rows=51.00 loops=1)
        Index Cond: (chat_id = $1)
        Index Searches: 1
        Buffers: shared hit=47
        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 Time: 0.003 ms
Execution Time: 0.067 ms

#### no-index

Limit  (cost=297.07..297.20 rows=51 width=153) (actual time=0.281..0.284 rows=51.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=297.07..297.32 rows=100 width=153) (actual time=0.281..0.282 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..293.75 rows=100 width=153) (actual time=0.010..0.266 rows=100.00 loops=1)
              Filter: (chat_id = '6c3ce1eb-20ca-48e7-af3c-920956940be7'::uuid)
              Rows Removed by Filter: 8400
              Buffers: shared hit=186
              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.035 ms
Execution Time: 0.290 ms

#### chat_only

Limit  (cost=172.68..172.80 rows=51 width=153) (actual time=0.133..0.136 rows=51.00 loops=1)
  Buffers: shared hit=79
  ->  Sort  (cost=172.68..172.93 rows=100 width=153) (actual time=0.133..0.134 rows=51.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 50kB
        Buffers: shared hit=79
        ->  Bitmap Heap Scan on messages  (cost=5.06..169.35 rows=100 width=153) (actual time=0.013..0.120 rows=100.00 loops=1)
              Recheck Cond: (chat_id = '6c3ce1eb-20ca-48e7-af3c-920956940be7'::uuid)
              Heap Blocks: exact=77
              Buffers: shared hit=79
              ->  Bitmap Index Scan on bench_variant  (cost=0.00..5.04 rows=100 width=0) (actual time=0.004..0.004 rows=100.00 loops=1)
                    Index Cond: (chat_id = '6c3ce1eb-20ca-48e7-af3c-920956940be7'::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.040 ms
Execution Time: 0.143 ms

#### covering

Limit  (cost=0.41..6.15 rows=51 width=153) (actual time=0.005..0.055 rows=51.00 loops=1)
  Buffers: shared hit=5
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..11.66 rows=100 width=153) (actual time=0.005..0.052 rows=51.00 loops=1)
        Index Cond: (chat_id = '6c3ce1eb-20ca-48e7-af3c-920956940be7'::uuid)
        Heap Fetches: 0
        Index Searches: 1
        Buffers: shared hit=5
        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.035 ms
Execution Time: 0.062 ms

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

#### psql

Limit  (cost=37.85..37.87 rows=10 width=153) (actual time=0.026..0.027 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=37.85..37.87 rows=10 width=153) (actual time=0.026..0.026 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..37.68 rows=10 width=153) (actual time=0.013..0.023 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '90a36fb9-ae42-4016-a663-65307e1e1e19'::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 = '90a36fb9-ae42-4016-a663-65307e1e1e19'::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.039 ms
Execution Time: 0.040 ms

#### generic

Limit  (cost=0.29..39.82 rows=14 width=153) (actual time=0.011..0.023 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Index Scan using ix_messages_chat_id_created_at_id on messages  (cost=0.29..381.47 rows=135 width=153) (actual time=0.011..0.022 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.005 ms
Execution Time: 0.032 ms

#### no-index

Limit  (cost=292.57..292.59 rows=10 width=153) (actual time=0.214..0.215 rows=10.00 loops=1)
  Buffers: shared hit=186
  ->  Sort  (cost=292.57..292.59 rows=10 width=153) (actual time=0.214..0.214 rows=10.00 loops=1)
        Sort Key: messages.created_at, messages.id
        Sort Method: quicksort  Memory: 27kB
        Buffers: shared hit=186
        ->  Seq Scan on messages  (cost=0.00..292.40 rows=10 width=153) (actual time=0.016..0.212 rows=10.00 loops=1)
              Filter: (chat_id = '90a36fb9-ae42-4016-a663-65307e1e1e19'::uuid)
              Rows Removed by Filter: 8490
              Buffers: shared hit=186
              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.032 ms
Execution Time: 0.220 ms

#### chat_only

Limit  (cost=37.85..37.87 rows=10 width=153) (actual time=0.023..0.025 rows=10.00 loops=1)
  Buffers: shared hit=12
  ->  Sort  (cost=37.85..37.87 rows=10 width=153) (actual time=0.023..0.024 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..37.68 rows=10 width=153) (actual time=0.009..0.020 rows=10.00 loops=1)
              Recheck Cond: (chat_id = '90a36fb9-ae42-4016-a663-65307e1e1e19'::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 = '90a36fb9-ae42-4016-a663-65307e1e1e19'::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.052 ms
Execution Time: 0.033 ms

#### covering

Limit  (cost=0.41..4.74 rows=10 width=153) (actual time=0.006..0.015 rows=10.00 loops=1)
  Buffers: shared hit=4
  ->  Index Only Scan using bench_variant on messages  (cost=0.41..4.74 rows=10 width=153) (actual time=0.006..0.014 rows=10.00 loops=1)
        Index Cond: (chat_id = '90a36fb9-ae42-4016-a663-65307e1e1e19'::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=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.038 ms
Execution Time: 0.021 ms
