How many rows are there in a table?
There is no limit on the number of rows in a table, but the table is limited to available disk space and memory/swap space. If you are storing rows that exceed an aggregated data size of 2 KB, then the maximum number of rows may be limited to 4 billion or fewer.
Counting is one of the easiest SQL statements, so it is also many people’s first experience of a PostgreSQL query.
How to do it…
From any interface, the SQL command used to count rows is as follows:
SELECT count(*) FROM table;
This will return a single integer value as the result.
In psql
, the command looks like the following:
cookbook=# select count(*) from orders;
count
-------
345
(1 row)
How it works...
PostgreSQL can choose between two techniques available to compute the SQL count(*)
function. Both are available in all the currently supported versions:
- The first is called sequential scan. We access every data block in...