How do you find the row count for all your tables in Postgres

前端 未结 15 2088
无人及你
无人及你 2020-11-22 12:31

I\'m looking for a way to find the row count for all my tables in Postgres. I know I can do this one table at a time with:

SELECT count(*) FROM table_name;
         


        
15条回答
  •  伪装坚强ぢ
    2020-11-22 13:01

    I usually don't rely on statistics, especially in PostgreSQL.

    SELECT table_name, dsql2('select count(*) from '||table_name) as rownum
    FROM information_schema.tables
    WHERE table_type='BASE TABLE'
        AND table_schema='livescreen'
    ORDER BY 2 DESC;
    
    CREATE OR REPLACE FUNCTION dsql2(i_text text)
      RETURNS int AS
    $BODY$
    Declare
      v_val int;
    BEGIN
      execute i_text into v_val;
      return v_val;
    END; 
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;
    

提交回复
热议问题