PostgreSQL query the number of records in all tables

The database used in the project is changed from SQLServer to PostgreSQL. Many SQLs in the project are written for SQLServer, so we have to rewrite SQL. One of the functions in the project is to count the database, including the number of records in all tables. I'm not very familiar with the database. After looking for it for a long time, I still have to start from the system table pg_class.

 

About pg_class field introduction: https://wizardforcel.gitbooks.io/postgresql-doc/content/714.html

 

Query the reltuples in the pg_class table is the number of records in the table:

select relname as TABLE_NAME, reltuples as rowCounts from pg_class where relkind = 'r' order by rowCounts desc

 

There is a problem found in this way, that is, the data of the system table will also be found out, which is obviously not what I want. How to remove system table.

 

The number of records in each table under the schema can be queried

select relname as TABLE_NAME, reltuples as rowCounts from pg_class where relkind = 'r' and relnamespace = (select oid from pg_namespace where nspname='public') order by rowCounts desc;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326859400&siteId=291194637