Thanks Grzegorz, But work memory is for each process (connection) rt? so if i keep more then 10MB will not affect the overall performance ?
it will. But the memory is only allocated when needed. You can always set it before running that particular query, and than put it back to default value. just use SET work_mem=64MB
Mind you , postgresql requires more memory to sort same set of data on disc than on memory. Your explain analyze indicates, that it used 2912kB , which means your work_mem value is set to some ridiculously low value. Put it up to 8MB or something, and retry.