7

Given for example a materialized view like this (Postgres 10.3):

create materialized view my_view as
 select * from my_table where sell_date < '2018-03-01';

The sell_date comparison value ('2018-03-01') can change sometime, but I want to avoid to drop and recreate the materialized view each time. The only idea I came up with it is using an external table with some metadata values like the date needed:

create materialized view my_view as
 select * from my_table where sell_date < 
 (select original_sell_date from some_metadata_table);

Is there any other way to work around this limitation of materialized views on Postgres?

One of the problems with my current solution is that you can have two or more materialized views that use the same value, but they may need at some point of different values. In this case a duplication of the metadata table is needed.

asked Jun 2, 2018 at 6:53
2
  • Why are you using a materialized view for such a basic query? Commented Jun 3, 2018 at 8:37
  • @Colin'tHart it is just an example Commented Jun 3, 2018 at 9:17

1 Answer 1

3

There are three ways,

  • Use foreign data in a table that holds the inputs.
  • Make sell_date a function of the current time. Such as AND sell_date > current_time - '5 days'.
  • If nothing depends on it, drop and reload a new ad-hoc MATERIALIZED VIEW by the same name in a transaction.
answered Jun 2, 2018 at 22:21
1
  • Can you elaborate on 1? Are you referring to FDWs? Commented Sep 2, 2020 at 18:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.