Query optimization in postgresql

shape