如何在创建物化视图时添加具有不同值的列?

How to add column with different values while CREATING MATERIALIZED VIEW?

我正在从几个不同的 table 创建实体化视图,但我缺少 class 值,因为它不在原点 table 中。如何在创建物化视图时添加列并设置值?我想要一个名为 class 的新列,并用它的名称为每个 SELECT 填充它,以便以后能够过滤 MV。我知道您可以将列直接添加到原点 table,但由于 table 总是被删除并从不同的人那里重新填充,我想直接在 MV 中进行。

CREATE MATERIALIZED VIEW vt_views.poi
TABLESPACE pg_default
AS
SELECT tim_bergname.name,
       tim_bergname.info1 AS hoehe,
       NULL AS art,
       NULL AS name_alternative,
       tim_bergname.info2 AS region,
       NULL AS text_hoehe,
       NULL AS name_lang,
       tim_bergname.geoloc,
       tim_bergname.symbolnummer
FROM tim_bergname

UNION ALL

SELECT tim_haus_huette.name,
       NULL,
       tim_haus_huette.info1 AS art,
       NULL,
       tim_haus_huette.info2 AS region,
       NULL,
       NULL,
       tim_haus_huette.geoloc,
       tim_haus_huette.symbolnummer
FROM tim_haus_huette

UNION ALL

... severall other SELECTS

UNION ALL

SELECT tim_wohnmobilstellplatz.name,
       NULL,
       NULL,
       NULL,
       NULL,
       NULL,
       NULL,
       tim_wohnmobilstellplatz.geoloc,
       tim_wohnmobilstellplatz.symbolnummer
FROM tim_wohnmobilstellplatz
WITH DATA;

和索引:

CREATE INDEX poi_geoloc_idx
    ON vt_views.poi USING gist (geoloc)
TABLESPACE pg_default;

有两种方法可以做到这一点:

使用tableoid系统栏:

SELECT tableoid::regclass::text as class,
       tim_bergname.name,
       tim_bergname.info1 AS hoehe,
       NULL AS art,
       NULL AS name_alternative,
       tim_bergname.info2 AS region,
       NULL AS text_hoehe,
       NULL AS name_lang,
       tim_bergname.geoloc,
       tim_bergname.symbolnummer
FROM tim_bergname

或者简单地添加一个常量值:

SELECT 'tim_bergname' as class,
       tim_bergname.name,
       tim_bergname.info1 AS hoehe,
       NULL AS art,
       NULL AS name_alternative,
       tim_bergname.info2 AS region,
       NULL AS text_hoehe,
       NULL AS name_lang,
       tim_bergname.geoloc,
       tim_bergname.symbolnummer
FROM tim_bergname