Postgres is creating temp files when a query does not fit into the main memory like below: LOG: temporary file: path "base/pgsql_tmp/pgsql_tmp56743.0", size 2308400 STATEMENT: select count(distinct calltimeda1_.CALL_DESTINATION) as col_0_0_ from RHQ_CALLTIME_DATA_VALUE calltimeda0_, RHQ_CALLTIME_DATA_KEY calltimeda1_, RHQ_MEASUREMENT_SCHED measuremen3_, RHQ_MEASUREMENT_DEF measuremen4_ where calltimeda0_.KEY_ID=calltimeda1_.id and calltimeda1_.SCHEDULE_ID=measuremen3_.id and measuremen3_.DEFINITION=measuremen4_.ID and measuremen4_.DATA_TYPE=$1 and (calltimeda0_.id in (select calltimeda5_.id from RHQ_CALLTIME_DATA_VALUE calltimeda5_, RHQ_CALLTIME_DATA_KEY calltimeda6_, RHQ_MEASUREMENT_SCHED measuremen7_ where calltimeda5_.KEY_ID=calltimeda6_.id and calltimeda6_.SCHEDULE_ID=measuremen7_.id and measuremen7_.RESOURCE_ID=$2)) and calltimeda0_.BEGIN_TIME>$3 and calltimeda0_.END_TIME<$4 LOG: temporary file: path "base/pgsql_tmp/pgsql_tmp56743.1", size 12830168 STATEMENT: select count(distinct calltimeda1_.CALL_DESTINATION) as col_0_0_ from RHQ_CALLTIME_DATA_VALUE calltimeda0_, RHQ_CALLTIME_DATA_KEY calltimeda1_, RHQ_MEASUREMENT_SCHED measuremen3_, RHQ_MEASUREMENT_DEF measuremen4_ where calltimeda0_.KEY_ID=calltimeda1_.id and calltimeda1_.SCHEDULE_ID=measuremen3_.id and measuremen3_.DEFINITION=measuremen4_.ID and measuremen4_.DATA_TYPE=$1 and (calltimeda0_.id in (select calltimeda5_.id from RHQ_CALLTIME_DATA_VALUE calltimeda5_, RHQ_CALLTIME_DATA_KEY calltimeda6_, RHQ_MEASUREMENT_SCHED measuremen7_ where calltimeda5_.KEY_ID=calltimeda6_.id and calltimeda6_.SCHEDULE_ID=measuremen7_.id and measuremen7_.RESOURCE_ID=$2)) and calltimeda0_.BEGIN_TIME>$3 and calltimeda0_.END_TIME<$4 LOG: duration: 1242.267 ms execute <unnamed>: select count(distinct calltimeda1_.CALL_DESTINATION) as col_0_0_ from RHQ_CALLTIME_DATA_VALUE calltimeda0_, RHQ_CALLTIME_DATA_KEY calltimeda1_, RHQ_MEASUREMENT_SCHED measuremen3_, RHQ_MEASUREMENT_DEF measuremen4_ where calltimeda0_.KEY_ID=calltimeda1_.id and calltimeda1_.SCHEDULE_ID=measuremen3_.id and measuremen3_.DEFINITION=measuremen4_.ID and measuremen4_.DATA_TYPE=$1 and (calltimeda0_.id in (select calltimeda5_.id from RHQ_CALLTIME_DATA_VALUE calltimeda5_, RHQ_CALLTIME_DATA_KEY calltimeda6_, RHQ_MEASUREMENT_SCHED measuremen7_ where calltimeda5_.KEY_ID=calltimeda6_.id and calltimeda6_.SCHEDULE_ID=measuremen7_.id and measuremen7_.RESOURCE_ID=$2)) and calltimeda0_.BEGIN_TIME>$3 and calltimeda0_.END_TIME<$4 DETAIL: parameters: $1 = '3', $2 = '12791', $3 = '1278158986846', $4 = '1278331786846'