"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Efficiently Get Total Row Count Before Applying LIMIT in PostgreSQL?

How to Efficiently Get Total Row Count Before Applying LIMIT in PostgreSQL?

Posted on 2025-03-23
Browse:677

How to Efficiently Get Total Row Count Before Applying LIMIT in PostgreSQL?

Best Way to Determine Result Count Before LIMIT Application

In database querying, determining the total number of results before applying LIMIT is crucial for pagination. Currently, a common approach involves running the query twice: once to count all results and again with a limit to retrieve the desired page. However, this method can be inefficient.

Fortunately, PostgreSQL 8.4 introduced a more efficient solution: window functions. By using a window function, you can retrieve both the total result count and the limited results in a single query.

SELECT foo
     , count(*) OVER() AS full_count
FROM   bar
WHERE  
ORDER  BY 
LIMIT  
OFFSET ;

Note that while this method provides the desired information, it can be computationally expensive, as all rows must be processed even if they will be excluded by the LIMIT.

Sequence of Events in a SELECT Query

Understanding the sequence of events in a SELECT query can help comprehend how window functions operate. The order of operations in Postgres is as follows:

  1. Filter rows based on WHERE clause
  2. Apply window functions
  3. Order results based on ORDER BY
  4. Limit results based on LIMIT and OFFSET

Alternative Methods for Count Retrieval

In addition to window functions, there are alternative methods to retrieve the affected row count:

  • plpgsql: GET DIAGNOSTICS integer_var = ROW_COUNT;
  • PHP: pg_num_rows

These methods provide the count of rows affected by the query rather than the full count before LIMIT application.

Related resources:

  • [Optimize query with OFFSET on large table](https://dba.stackexchange.com/questions/128089/optimize-query-with-offset-on-large-table)
  • [Calculate number of rows affected by batch query in PostgreSQL](https://stackoverflow.com/questions/4644316/calculate-number-of-rows-affected-by-batch-query-in-postgresql)
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3