HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Getting the exact count of rows in Postgres database

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
exactrowsthepostgresgettingdatabasecount

Problem

Is there any way to get the exact row count of all tables in Postgres in fast way?

Count(*) seems to run very slowly for larger tables.

Is there a way to get an exact count from Postgres statistics tables without running vacuum, which is also a costly operation?

Solution

It seems there is currently no built-in way to do what you require in PostgreSQL.


People are working toward such capabilities. While nobody can say
with any certainty when such features will make it into a
PostgreSQL release, I think it's safe to predict that it will not
be before late 2017, and most probably later than that.

Meanwhile, you could manually implement a solution using triggers; for example as described in Postgresql General Bits by A. Elein Mustain.

The idea is to maintain an always-current row count in a separate table using triggers. Be aware that this may add significant overhead to data modifications.

Context

StackExchange Database Administrators Q#198882, answer score: 4

Revisions (0)

No revisions yet.