$ python scripts/explain_history.py --output evidence/explain-history.txt messages_total | messages_in_target_chat ----------------+------------------------- 50146 | 20000 (1 row) === 1. first page, app query HISTORY_FIRST, generic plan QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=0.41..122.63 rows=119 width=55) (actual time=0.019..0.030 rows=51.00 loops=1) Buffers: shared hit=4 -> Index Scan using messages_chat_keyset on messages (cost=0.41..1226.64 rows=1194 width=55) (actual time=0.017..0.025 rows=51.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=4 Planning: Buffers: shared hit=10 Planning Time: 0.157 ms Execution Time: 0.067 ms (10 rows) === 2. late page (cursor at 19950), app query HISTORY_AFTER, generic plan QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.41..59.95 rows=40 width=55) (actual time=0.022..0.036 rows=50.00 loops=1) Buffers: shared hit=5 -> Index Scan using messages_chat_keyset on messages (cost=0.41..592.75 rows=398 width=55) (actual time=0.020..0.030 rows=50.00 loops=1) Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) Index Searches: 1 Buffers: shared hit=5 Planning Time: 0.163 ms Execution Time: 0.112 ms (8 rows) === 3. REJECTED: one query with ($2 IS NULL OR ...), same late page, generic plan QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.41..123.02 rows=40 width=55) (actual time=3.242..3.257 rows=50.00 loops=1) Buffers: shared hit=526 -> Index Scan using messages_chat_keyset on messages (cost=0.41..1232.61 rows=402 width=55) (actual time=3.239..3.251 rows=50.00 loops=1) Index Cond: (chat_id = $1) Filter: (($2 IS NULL) OR (ROW(created_at, id) > ROW($2, $3))) Rows Removed by Filter: 19950 Index Searches: 1 Buffers: shared hit=526 Planning Time: 0.125 ms Execution Time: 3.282 ms (10 rows) === 4. REJECTED: OFFSET pagination, same late page QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=122.63..244.84 rows=119 width=55) (actual time=4.404..4.418 rows=50.00 loops=1) Buffers: shared hit=526 -> Index Scan using messages_chat_keyset on messages (cost=0.41..1226.64 rows=1194 width=55) (actual time=0.015..3.790 rows=20000.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=526 Planning Time: 0.091 ms Execution Time: 4.438 ms (8 rows)