// if the total result count is not cached yet, retrieve it from the db
$result_count = sizeof ($id_ary);
This doesn't take into account the results that are outside the Limit and offset of the query.
Mysql uses FOUND_ROWS() function instead of sizeof($id_ary). Since pgsql doesn't have FOUND_ROWS, only possible solution is to use a COUNT function to calculate the total result count so that it can be cached.