是否可以在“创建物化视图为”形式的物化视图上创建新索引?

Dou*_*Fir 7 postgresql

(show server_version; 11.5)
Run Code Online (Sandbox Code Playgroud)

我有一个物化视图:

drop materialized view if exists reporting.ecom_channel;
create materialized view reporting.ecom_channel  as


select 
    s.date,
    s.user_type,
    s.channel_grouping,
    s.device_category,
    sum(s.sessions) as sessions,
    count(distinct s.dimension2) as daily_users,
    sum(s.transactions) as transactions,
    sum(s.transaction_revenue) as revenue
from ecom.sessions s
group by 1,2,3,4
Run Code Online (Sandbox Code Playgroud)

该查询中没有 id 字段。对于我的常规表,这些表“传统上”指定为'create table tablename .... id serial, field1 int, etc'

由于我根据查询结果创建了物化视图,而不是在创建期间定义每个字段名称,因此如何添加 id?

我这样做的原因是因为当我尝试时:

refresh materialized view concurrently reporting.ecom_channel;
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

错误:无法同时刷新物化视图“reporting.channel”。提示:在物化视图的一列或多列上创建不带 WHERE 子句的唯一索引

如何在上述上下文中添加 ID?和

create materialized view reporting.ecom_channel  as
select 
    s.date,
    s.user_type,
    ...
Run Code Online (Sandbox Code Playgroud)

Lau*_*lbe 9

该唯一索引可以是

CREATE UNIQUE INDEX ON reporting.ecom_channel (
   date,
   user_type,
   channel_grouping,
   device_category
);
Run Code Online (Sandbox Code Playgroud)

根据定义视图的查询,此组合必须是唯一的。