我的公共模式中有一个名为vw_check_space的视图(使用postgresql 9.4)。当我跑
时select * from public.vw_check_space;
作为postgres用户,我得到一个行列表但是当我用另一个用户'user1'运行相同的查询时,它什么都不返回。
查看:
CREATE OR REPLACE VIEW public.vw_check_space AS
WITH constants AS (
SELECT current_setting('block_size'::text)::numeric AS bs,
23 AS hdr,
8 AS ma
), no_stats AS (
SELECT columns.table_schema,
columns.table_name,
psut.n_live_tup::numeric AS est_rows,
pg_table_size(psut.relid::regclass)::numeric AS table_size
FROM columns
JOIN pg_stat_user_tables psut ON columns.table_schema::name = psut.schemaname AND columns.table_name::name = psut.relname
LEFT JOIN pg_stats ON columns.table_schema::name = pg_stats.schemaname AND columns.table_name::name = pg_stats.tablename AND columns.column_name::name = pg_stats.attname
WHERE pg_stats.attname IS NULL AND (columns.table_schema::text <> ALL (ARRAY['pg_catalog'::character varying, 'information_schema'::character varying]::text[]))
GROUP BY columns.table_schema, columns.table_name, psut.relid, psut.n_live_tup
), null_headers AS (
SELECT constants.hdr + 1 + sum(
CASE
WHEN pg_stats.null_frac <> 0::double precision THEN 1
ELSE 0
END) / 8 AS nullhdr,
sum((1::double precision - pg_stats.null_frac) * pg_stats.avg_width::double precision) AS datawidth,
max(pg_stats.null_frac) AS maxfracsum,
pg_stats.schemaname,
pg_stats.tablename,
constants.hdr,
constants.ma,
constants.bs
FROM pg_stats
CROSS JOIN constants
LEFT JOIN no_stats ON pg_stats.schemaname = no_stats.table_schema::name AND pg_stats.tablename = no_stats.table_name::name
WHERE (pg_stats.schemaname <> ALL (ARRAY['pg_catalog'::name, 'information_schema'::name])) AND no_stats.table_name IS NULL AND (EXISTS ( SELECT 1
FROM columns
WHERE pg_stats.schemaname = columns.table_schema::name AND pg_stats.tablename = columns.table_name::name))
GROUP BY pg_stats.schemaname, pg_stats.tablename, constants.hdr, constants.ma, constants.bs
), data_headers AS (
SELECT null_headers.ma,
null_headers.bs,
null_headers.hdr,
null_headers.schemaname,
null_headers.tablename,
(null_headers.datawidth + (null_headers.hdr + null_headers.ma -
CASE
WHEN (null_headers.hdr % null_headers.ma) = 0 THEN null_headers.ma
ELSE null_headers.hdr % null_headers.ma
END)::double precision)::numeric AS datahdr,
null_headers.maxfracsum * (null_headers.nullhdr + null_headers.ma -
CASE
WHEN (null_headers.nullhdr % null_headers.ma::bigint) = 0 THEN null_headers.ma::bigint
ELSE null_headers.nullhdr % null_headers.ma::bigint
END)::double precision AS nullhdr2
FROM null_headers
), table_estimates AS (
SELECT data_headers.schemaname,
data_headers.tablename,
data_headers.bs,
pg_class.reltuples::numeric AS est_rows,
pg_class.relpages::numeric * data_headers.bs AS table_bytes,
ceil(pg_class.reltuples * (data_headers.datahdr::double precision + data_headers.nullhdr2 + 4::double precision + data_headers.ma::double precision -
CASE
WHEN (data_headers.datahdr % data_headers.ma::numeric) = 0::numeric THEN data_headers.ma::numeric
ELSE data_headers.datahdr % data_headers.ma::numeric
END::double precision) / (data_headers.bs - 20::numeric)::double precision) * data_headers.bs::double precision AS expected_bytes,
pg_class.reltoastrelid
FROM data_headers
JOIN pg_class ON data_headers.tablename = pg_class.relname
JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid AND data_headers.schemaname = pg_namespace.nspname
WHERE pg_class.relkind = 'r'::"char"
), estimates_with_toast AS (
SELECT table_estimates.schemaname,
table_estimates.tablename,
true AS can_estimate,
table_estimates.est_rows,
table_estimates.table_bytes + COALESCE(toast.relpages, 0)::numeric * table_estimates.bs AS table_bytes,
table_estimates.expected_bytes + ceil(COALESCE(toast.reltuples, 0::real) / 4::double precision) * table_estimates.bs::double precision AS expected_bytes
FROM table_estimates
LEFT JOIN pg_class toast ON table_estimates.reltoastrelid = toast.oid AND toast.relkind = 't'::"char"
), table_estimates_plus AS (
SELECT current_database() AS databasename,
estimates_with_toast.schemaname,
estimates_with_toast.tablename,
estimates_with_toast.can_estimate,
estimates_with_toast.est_rows,
CASE
WHEN estimates_with_toast.table_bytes > 0::numeric THEN estimates_with_toast.table_bytes
ELSE NULL::numeric
END AS table_bytes,
CASE
WHEN estimates_with_toast.expected_bytes > 0::double precision THEN estimates_with_toast.expected_bytes::numeric
ELSE NULL::numeric
END AS expected_bytes,
CASE
WHEN estimates_with_toast.expected_bytes > 0::double precision AND estimates_with_toast.table_bytes > 0::numeric AND estimates_with_toast.expected_bytes <= estimates_with_toast.table_bytes::double precision THEN (estimates_with_toast.table_bytes::double precision - estimates_with_toast.expected_bytes)::numeric
ELSE 0::numeric
END AS bloat_bytes
FROM estimates_with_toast
UNION ALL
SELECT current_database() AS databasename,
no_stats.table_schema,
no_stats.table_name,
false AS bool,
no_stats.est_rows,
no_stats.table_size,
NULL::numeric AS "numeric",
NULL::numeric AS "numeric"
FROM no_stats
), bloat_data AS (
SELECT current_database() AS databasename,
table_estimates_plus.schemaname,
table_estimates_plus.tablename,
table_estimates_plus.can_estimate,
table_estimates_plus.table_bytes,
round(table_estimates_plus.table_bytes / (1024::double precision ^ 2::double precision)::numeric, 3) AS table_mb,
table_estimates_plus.expected_bytes,
round(table_estimates_plus.expected_bytes / (1024::double precision ^ 2::double precision)::numeric, 3) AS expected_mb,
round(table_estimates_plus.bloat_bytes * 100::numeric / table_estimates_plus.table_bytes) AS pct_bloat,
round(table_estimates_plus.bloat_bytes / (1024::numeric ^ 2::numeric), 2) AS mb_bloat,
table_estimates_plus.est_rows
FROM table_estimates_plus
)
SELECT bloat_data.databasename,
bloat_data.schemaname,
bloat_data.tablename,
bloat_data.can_estimate,
bloat_data.table_bytes,
bloat_data.table_mb,
bloat_data.expected_bytes,
bloat_data.expected_mb,
bloat_data.pct_bloat,
bloat_data.mb_bloat,
bloat_data.est_rows
FROM bloat_data
ORDER BY bloat_data.pct_bloat DESC;
我为数据库提供了连接权限,并为用户user1授予了使用权和选择权限。我不确定我在这里会缺少什么其他特权。任何帮助将不胜感激。
PS:我还为视图在创建过程中使用的表和模式提供了使用和选择权限。
答案 0 :(得分:0)
https://www.postgresql.org/docs/9.4/static/view-pg-stats.html
视图pg_stats提供对存储在中的信息的访问 pg_statistic目录。此视图仅允许访问行 pg_statistic,对应于用户有权访问的表 读取,因此允许公共读取访问是安全的 图。
https://www.postgresql.org/docs/9.4/static/monitoring-stats.html
pg_stat_user_tables与pg_stat_all_tables相同,但只有用户 表格显示。
所以在您将其他所有者表的读取授予用户之后,您仍然会加入pg_stat_user_tables
,这会将列表仅删除到您所在的那些表... - 要么将其从视图中排除,要么使用{{ 1}}而不是left outer join
我正在谈论inner join
,但您应该检查您加入的每个表,并阅读您在查询中包含的所有观看次数