TimescaleDB – How to Quickly Return Row Count in Distributed Hypertable

timescaledb

In a 'vanilla' PostgreSQL 12.7 database, I generally run the following query to learn the estimated number of rows in tables with 100+ million rows:

----------------------------------------------------------
-- Return the estimated number of rows for the given table
----------------------------------------------------------
SELECT reltuples::bigint AS estimate_number_of_rows
FROM   pg_class
WHERE  oid = to_regclass('name_of_some_big_table');

This type of query doesn't work for a distributed hypertable on our multi-node TimescaleDB installation.

I checked the TimescaleDB API Reference but couldn't find what I'm looking for.

Is there such a straightforward query to quickly return the estimated number of rows in a distributed hypertable?

Best Answer

TimescaleDB provides approximate_row_count function, which is described in the documentation here. The accuracy depends when ANALYZE or VACUUM was run last time.

For your table the query will be:

SELECT approximate_row_count('name_of_some_big_table');