Postgresql:在不返回任何记录的视图上选择查询

Posted

技术标签:

【中文标题】Postgresql:在不返回任何记录的视图上选择查询【英文标题】:Postgresql: Select query on view returning no records 【发布时间】:2017-11-01 16:40:08 【问题描述】:

我的公共架构中有一个名为 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:我还为视图在创建过程中使用的表和架构提供了使用和选择权限。

【问题讨论】:

从视图定义开始,例如where 'postgres' = current_user() 已经导致没有行返回。也可以是行安全。但请从 postgres 版本开始并查看定义 我使用的是 postgres 9.4 并包含了视图定义 【参考方案1】:

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,这将仅将列表剪切到您所在的那些表... - 要么将其从视图中排除,要么改用 left outer join inner join

我说的是JOIN pg_stat_user_tables,但您应该检查您加入的每个表并阅读您在查询中包含的所有视图

【讨论】:

感谢您指出这一点。我以 postgres 用户的身份创建了一个函数,该函数的作用与视图所做的完全相同,并使其所有人都可以执行。所以现在当我的用户 user1 确实选择它时,我现在可以成功地看到我最初期望看到的结果。 没错。我做了功能安全定义器并且它工作了:)

以上是关于Postgresql:在不返回任何记录的视图上选择查询的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server - 在视图上选择列的内部查询是啥

在 Azure SQL 上运行非常缓慢的外部表上选择

在android上选择文件夹对话框?

在 MYSQL 表上选择查询需要很长时间并超时

是否在表格视图单元格上选择没有响应

如何将集合视图上选择的所有单元格列出到另一个视图中