CREATE TABLE t_test (x numeric); INSERT INTO t_test SELECT random() FROM generate_series(1, 5000000); ANALYZE; SHOW work_mem; HEAD: postgres=# explain analyze SELECT * FROM t_test ORDER BY x; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- Gather Merge (cost=397084.73..883229.71 rows=4166666 width=11) (actual time=1326.281..2718.040 rows=5000000 loops=1) Workers Planned: 2 Workers Launched: 2 -> Sort (cost=396084.71..401293.04 rows=2083333 width=11) (actual time=1287.168..1520.520 rows=1666667 loops=3) Sort Key: x Sort Method: external merge Disk: 24880kB Worker 0: Sort Method: external merge Disk: 24776kB Worker 1: Sort Method: external merge Disk: 23960kB -> Parallel Seq Scan on t_test (cost=0.00..47861.33 rows=2083333 width=11) (actual time=0.241..135.730 rows=1666667 loops=3) Planning Time: 0.054 ms Execution Time: 2837.789 ms (11 rows) PATCHED: postgres=# explain analyze SELECT * FROM t_test ORDER BY x; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- Gather Merge (cost=397084.73..883229.71 rows=4166666 width=11) (actual time=1283.818..2696.469 rows=5000000 loops=1) Workers Planned: 2 Workers Launched: 2 -> Sort (cost=396084.71..401293.04 rows=2083333 width=11) (actual time=1253.459..1485.734 rows=1666667 loops=3) Sort Key: x Sort Method: external merge Disk: 25136kB Worker 0: Sort Method: external merge Disk: 24096kB Worker 1: Sort Method: external merge Disk: 24304kB -> Parallel Seq Scan on t_test (cost=0.00..47861.33 rows=2083333 width=11) (actual time=0.256..133.065 rows=1666667 loops=3) Planning Time: 0.055 ms Execution Time: 2816.495 ms (11 rows) David's benchmark: set max_parallel_workers_per_gather = 0; create table t (a bigint not null, b bigint not null, c bigint not null, d bigint not null, e bigint not null, f bigint not null); insert into t select x,x,x,x,x,x from generate_Series(1,140247142) x; -- 10GB! vacuum freeze t; select * from t order by a offset 140247142; HEAD: postgres=# select * from t order by a offset 140247142; a | b | c | d | e | f ---+---+---+---+---+--- (0 rows) Time: 229396,551 ms (03:49,397) Time: 231432,750 ms (03:51,433) PATCHED: postgres=# explain analyze select * from t order by a offset 140247142; a | b | c | d | e | f ---+---+---+---+---+--- (0 rows) Time: 220887,346 ms (03:40,887) Time: 222652,487 ms (03:42,652) Ronan's benchmark: Setup 1: single table, 1 000 000 tuples, no index CREATE TABLE tbench ( a int, b int ); INSERT INTO tbench (a, b) SELECT a, b FROM generate_series(1, 100) a, generate_series(1, 10000) b; Test 1: Single-column ordered select (order by b since the table is already sorted by a) select b from tbench order by b; HEAD: postgres=# explain analyze select b from tbench order by b; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Sort (cost=266422.45..271422.45 rows=2000000 width=4) (actual time=425.062..559.464 rows=2000000 loops=1) Sort Key: b Sort Method: external merge Disk: 23528kB -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=4) (actual time=0.056..168.422 rows=2000000 loops=1) Planning Time: 0.058 ms Execution Time: 605.315 ms (6 rows) PATCHED: postgres=# explain analyze select b from tbench order by b; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Sort (cost=266422.45..271422.45 rows=2000000 width=4) (actual time=421.642..557.750 rows=2000000 loops=1) Sort Key: b Sort Method: external merge Disk: 23528kB -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=4) (actual time=0.047..167.848 rows=2000000 loops=1) Planning Time: 0.080 ms Execution Time: 603.583 ms (6 rows) Test 2: Ordered sum (using b so that the input is not presorted) select sum(b order by b) from tbench; HEAD: postgres=# explain analyze select sum(b order by b) from tbench; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=33850.00..33850.01 rows=1 width=8) (actual time=547.316..547.317 rows=1 loops=1) -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=4) (actual time=0.074..134.147 rows=2000000 loops=1) Planning Time: 0.060 ms Execution Time: 547.339 ms (4 rows) PATCHED: postgres=# explain analyze select sum(b order by b) from tbench; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=33850.00..33850.01 rows=1 width=8) (actual time=544.565..544.566 rows=1 loops=1) -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=4) (actual time=0.044..134.305 rows=2000000 loops=1) Planning Time: 0.050 ms Execution Time: 544.633 ms (4 rows) Test 3: Ordered sum + group by select b, sum(a order by a) from tbench GROUP BY b; HEAD: postgres=# explain analyze select b, sum(a order by a) from tbench GROUP BY b; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ GroupAggregate (cost=266422.45..281522.48 rows=10003 width=12) (actual time=740.557..1201.470 rows=10000 loops=1) Group Key: b -> Sort (cost=266422.45..271422.45 rows=2000000 width=8) (actual time=740.490..905.244 rows=2000000 loops=1) Sort Key: b Sort Method: external merge Disk: 35216kB -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=8) (actual time=0.040..192.818 rows=2000000 loops=1) Planning Time: 0.080 ms Execution Time: 1206.248 ms (8 rows) PATCHED: postgres=# explain analyze select b, sum(a order by a) from tbench GROUP BY b; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ GroupAggregate (cost=266422.45..281522.48 rows=10003 width=12) (actual time=744.886..1196.399 rows=10000 loops=1) Group Key: b -> Sort (cost=266422.45..271422.45 rows=2000000 width=8) (actual time=744.822..906.223 rows=2000000 loops=1) Sort Key: b Sort Method: external merge Disk: 35216kB -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=8) (actual time=0.053..198.255 rows=2000000 loops=1) Planning Time: 0.084 ms Execution Time: 1200.807 ms (8 rows) Setup 2: same as before, but adding an index on (b, a) CREATE INDEX ON tbench (b, a); Test 2: Ordered sum: select sum(a order by a) from tbench; HEAD: postgres=# explain analyze select sum(a order by a) from tbench; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=33850.00..33850.01 rows=1 width=8) (actual time=417.679..417.679 rows=1 loops=1) -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=4) (actual time=0.036..134.878 rows=2000000 loops=1) Planning Time: 1.185 ms Execution Time: 417.732 ms (4 rows) PATCHED: postgres=# explain analyze select sum(a order by a) from tbench; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=33850.00..33850.01 rows=1 width=8) (actual time=421.826..421.827 rows=1 loops=1) -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=4) (actual time=0.063..134.843 rows=2000000 loops=1) Planning Time: 0.070 ms Execution Time: 421.885 ms (4 rows) Test 3: Ordered sum + group by: select a, sum(b order by b) from tbench GROUP BY a; HEAD: postgres=# explain analyze select a, sum(b order by b) from tbench GROUP BY a; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ GroupAggregate (cost=199979.56..214980.56 rows=100 width=12) (actual time=502.326..1048.130 rows=100 loops=1) Group Key: a -> Sort (cost=199979.56..204979.56 rows=2000000 width=8) (actual time=496.952..654.468 rows=2000000 loops=1) Sort Key: a Sort Method: external merge Disk: 35216kB -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=8) (actual time=0.064..136.186 rows=2000000 loops=1) Planning Time: 0.137 ms Execution Time: 1052.026 ms (8 rows) PATCHED: postgres=# explain analyze select a, sum(b order by b) from tbench GROUP BY a; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ GroupAggregate (cost=199979.56..214980.56 rows=100 width=12) (actual time=485.994..1033.928 rows=100 loops=1) Group Key: a -> Sort (cost=199979.56..204979.56 rows=2000000 width=8) (actual time=480.617..637.878 rows=2000000 loops=1) Sort Key: a Sort Method: external merge Disk: 35216kB -> Seq Scan on tbench (cost=0.00..28850.00 rows=2000000 width=8) (actual time=0.066..136.371 rows=2000000 loops=1) Planning Time: 0.117 ms Execution Time: 1037.586 ms (8 rows)