【问题标题】:Get constraints and indexes on materialized views获取物化视图的约束和索引
【发布时间】:2019-12-22 06:06:49
【问题描述】:

查询以获取 Postgres 中所有物化视图的所有索引和所有约束。下面的查询只返回表的索引。

SELECT indexname, indexdef 
FROM pg_indexes 
WHERE schemaname = 'public' AND tablename = 'table'

【问题讨论】:

    标签: sql postgresql indexing materialized-views


    【解决方案1】:

    视图pg_indexes 提供对数据库中每个索引的有用信息(以及物化视图的索引)的访问。您可以查找pg_class 以仅过滤掉物化视图(relkind = 'm'):

    select i.*
    from pg_indexes i
    join pg_class c
        on schemaname = relnamespace::regnamespace::text 
        and tablename = relname
    where relkind = 'm'
    
     schemaname | tablename |    indexname    | tablespace |                             indexdef                             
    ------------+-----------+-----------------+------------+------------------------------------------------------------------
     public     | my_view   | my_view_id_idx  |            | CREATE UNIQUE INDEX my_view_id_idx ON my_view USING btree (id)
     public     | sen_view  | sen_view_id_idx |            | CREATE UNIQUE INDEX sen_view_id_idx ON sen_view USING btree (id)
    (2 rows)
    

    【讨论】:

    • 这不会给我外键约束。
    • 1.外键不是索引。 2. 不能在物化视图上定义外键。
    猜你喜欢
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    相关资源
    最近更新 更多