Skip to content

Latest commit

 

History

History
65 lines (56 loc) · 1.55 KB

get-row-count-for-most-recent-query.md

File metadata and controls

65 lines (56 loc) · 1.55 KB

Get Row Count For Most Recent Query

Anytime you execute a query in psql, there is a row count associated with that query. This is most naturally understood with a select query where a discreet number of rows are returned. We typically see the row count (e.g. (19 rows)) right below the result set.

You can always reference the row count of the most recent query with the :ROW_COUNT variable. Here we use \echo to print it out.

> select generate_series(2,20);
 generate_series
-----------------
               2
               3
               4
               5
               6
               7
               8
               9
              10
              11
              12
              13
              14
              15
              16
              17
              18
              19
              20
(19 rows)

Time: 12.338 ms
> \echo :ROW_COUNT
19

For some queries, like one that induces a pager (e.g. less) to be used, you'll lose track of the row count once the pager closes. This is where being able to reference the row count without rerunning the query is most useful.

> select generate_series(2,2000);
Time: 9.815 ms
> \echo :ROW_COUNT
1999

Notice, we can also get a row count from other kinds of queries like this insert statement.

> insert into users (id) values (50001), (50002), (50003);
INSERT 0 3
Time: 2.804 ms
> \echo :ROW_COUNT
3

source