drop table if exists tbl; create table tbl(a serial, b int not null, c text not null); insert into tbl (b,c) select i, random()::text from generate_series(1, 100000000) i; alter table tbl add primary key (a); analyze tbl; checkpoint; \timing -- new code --\copy (delete from tbl where a <= 5000000 returning *) to foo.dat -- old code --SET work_mem TO '1GB'; --\copy (with t as (delete from tbl where a <= 5000000 returning *) select * from t) to foo.dat