PosgreSQL: setting high work_mem does not avoid disk merge
This is somewhat speculative but Depesz (Hubert Lubaczewski) has this to say on the subject:
You might wonder, though, why PostgreSQL switched to Disk, when it used only 448kB? After all, work_mem is 1MB. Answer is pretty simple – as I understand – disk is used when work_mem is not enough, so it means it's already been filled. So, sort with “Disk: 448kB" would mean that more or less whole work_mem has been used plus 448kB of disk.
So in your case the used work_mem might be in the 6 MB range. Also, try reset work_mem
first, maybe there's stuff in there from a previous query.
The reported amount reported by Sort Method: external merge Disk: 2072kB
is not the amount quicksort would need. I ran into this issue and saw Sort Method: external merge Disk: 28408kB
i started raising work_mem to see what happened and when I had raised it enough it reported Sort Method: quicksort Memory: 66629kB
. So quicksort needs more then two times of what a disk merge needs.