views:

40

answers:

1

Dear Folks,

I am from DW/BI background using SAS for many years now I have task to find out number of records present in tables on the fly for postgresql tables

i.e. In SAS we have meta tables which has details about tables and no of records, column info etc in system meta tables in a same manner is there any meta table available in postgresql to get no of observation on the fly?

I know we can do select count(*) from table but I dont want to do that, I want to know is there any built in meta tables in postgresql to get no of records present in table?

Highly appreciated your help.

Regards, Vijay

+1  A: 

The pg_class system catalogue contains information about each relation (table, index, view, sequence...). For tables, this includes an estimate of the number of tuples (rows) and disk pages taken up by the table. e.g.:

SELECT reltuples, relpages FROM pg_class WHERE oid = 'table_name'::regclass

Note that reltuples is of "real" type and so stores about 6 significant figures.

araqnid
perfect, it shows no of records say SELECT reltuples, relpages,relname FROM pg_class where relname='xyz' --> for xyz table.is there any meta table in pg to show like select schemaname,tablename,noOfrecords from xyz ?
Vijay
if you just restrict `relkind='r'` instead of `relname='xyz'` you'll get all tables. you need to `join pg_namespace on pg_namespace.oid=pg_class.relnamespace` to get schema names (nspname) as well.
araqnid
Please note that he said this is an *estimate*. The accuracy of that estimate will depend on the amount of "churn" in the table, the size of the table relative to the setting of default_statistics_target and how long it has been since the last ANALYZE was run for that table.
Matthew Wood