sqlpostgresqlmaterialized-views

Calling a materialized view from another materialized view in Postgres


Is it possible to create a view, called first_view and in another view called second_view the first one is called? This is the original question.

This is the first view:

CREATE MATERIALIZED VIEW first_view
AS SELECT atable.variable_one, btable.another_variable, ctable.variable_x
FROM a atable, b btable, c ctable

So that f(a,b,c) view can be called in f(ALL) which is f(a,b,c) including f(m) with aggregate functions.


Solution

  • The answer is so simple that I assume that I do not understand your question properly:

    Just use the first MVIEW the same way you use any other table or view in the second MVIEW:

    create materialized view first_view
    as
    select a.column_one, b.column_two, c.column_three
    from table_a a 
       join table_b b on a.id = b.aid
       join table_c c on b.id = c.bid;
    
    create materialized view second_view
    as
    select x.some_column, f.*
    from other_table x
       join first_view f on x.id = f.column_one;