If you are using a second query to retrieve the exact number of rows, you can set your transaction as ready only to get consistent point in time results and not be affected by inserts/deletes from other sessions. This is if you are not doing any changes to the data. If you are changing data, you can use the serializable transaction isolation level or lock the entire table.