[英]Get count of all materialized views
I am trying to get a count from all materialized views in my database. 我正在尝试从数据库中的所有物化视图中获取数据。 This is what I have so far, but it is not returning the count -
到目前为止,这是我所拥有的,但是它没有返回计数-
DO
$$
DECLARE
rec record;
my_pk_new integer;
BEGIN
FOR rec IN
SELECT matviewname
FROM pg_matviews
limit 2
LOOP
EXECUTE ' SELECT count(*) from' || rec.matviewname
INTO my_pk_new;
END LOOP;
RAISE NOTICE 'Calling (%)', my_pk_new;
END;
$$
LANGUAGE plpgsql;
This is what I would expect to see - 这就是我希望看到的-
Matview A 2432
Matview B 453984
1) After from
a space is missing. 1)
from
空格后丢失。 So you are executing the query 所以你正在执行查询
SELECT count(*) frommy_view
instead of 代替
SELECT count(*) from my_view
So there is an error. 因此有一个错误。 On my system a default int value returned (
1
) 在我的系统上,返回了默认的int值(
1
)
2) Your RAISE NOTICE
is outside the loop. 2)您的
RAISE NOTICE
不在循环之内。 So you are noticing only the very last query result. 因此,您仅注意到最后的查询结果。 Put this into the loop body and it works.
将其放入循环主体即可。
DO $$
DECLARE
rec record;
my_pk_new integer;
BEGIN
FOR rec IN
SELECT matviewname
FROM pg_matviews
limit 2
LOOP
EXECUTE 'SELECT count(*) FROM ' || rec.matviewname
INTO my_pk_new;
RAISE NOTICE 'Calling (%)', my_pk_new;
END LOOP;
END;
$$
LANGUAGE plpgsql;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.