2016-08-05 39 views
2

我可以很容易地通過下面的查詢找到形式_%_jobs的所有表查找所有表的名稱:比x行更

select * from pg_tables where tablename like '_%_jobs' 

我想過濾該列表,從而爲僅僅是表有超過200,000行。


我曾嘗試:

select * from pg_tables where tablename like '_%_jobs' having count(*) > 200000 

但這會導致一個錯誤:

ERROR: column "pg_tables.schemaname" must appear in the GROUP BY clause or be used in an aggregate function

我怎樣才能做到這一點?

+0

您要檢查,如果該查詢返回的表的數量> 200000您需要使用動態SQL從每個表格返回得到計數。 –

+0

@vkp有道理。我之前沒有使用動態sql,所以我不完全確定如何去做那個 –

+0

你可以使用那個真空分析統計 –

回答

3

嘗試

SELECT relname, reltuples from pg_class where relname like '_%_jobs' 

這裏和實際報告的數字之間有可能略有差異。從手動節上reltuples

Number of rows in the table. This is only an estimate used by the planner. It is updated by VACUUM, ANALYZE, and a few DDL commands such as CREATE INDEX.

+0

爲我的目的,這是完美的,具體回答我添加了'WHERE reltuples> 200000' –

+0

很高興得到了幫助的問題 – e4c5