Re: Count for pagination

From: Michael C Rosenstein <mcr(at)mdibl(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Count for pagination
Date: 2011-04-11 12:26:09
Message-ID: 4DA2F361.7040900@mdibl.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Any suggestions on how to get the count of all records that could be
> returned

We use a window function to get the total # of records within each of
our paginated queries:

SELECT
...
,COUNT(*) OVER() fullRowCount
FROM ...
WHERE ...
ORDER BY ...
LIMIT ... OFFSET ...;

While there is a cost to using the window function, it's faster (for
us) than two separate queries, and, more importantly, it's flexible
enough to work in the 100s of different query contexts we have.

/mcr

In response to

Browse pgsql-general by date

  From Date Subject
Next Message George Woodring 2011-04-11 15:07:20 Re: autovacuum issue after upgrade to 9.0.1
Previous Message Nick Raj 2011-04-11 10:53:47 Global Variables in plpgsql