0

everyone. In postgresql 12, i have a materialized view like this:

CREATE MATERIALIZED VIEW mv
AS
  WITH cte AS (SELECT ...), cte2 AS (SELECT ...) SELECT ...
WITH DATA;

when i invoke REFRESH MATERIALIZED VIEW mv, or perform this command in linux crontab, it cost 4 hours.

but i perform WITH cte AS (SELECT ...), cte2 AS (SELECT ...) SELECT ... in pgadmin4 Query Tool, it only cost 7 seconds.

I don't know why it's too diffrence. I would like to cost 7 seconds in crontab, What shoud i do?

Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42
Mal Sund
  • 78
  • 7

1 Answers1

0

may be i find the reason, but i am not sure.

in WITH cte AS (SELECT ...), cte2 AS (SELECT ...) SELECT ..., there are two foreign data wrapper tables. I copy fdw table to local use CREATE TABLE test AS SELECT * FROM fdw.table_name, and replace fdw table use local table in cte. When i invoke CREATE MATERIALIZED VIEW mv AS ... WITH DATA it cost 12 seconds, REFRESH MATERIALIZED VIEW mv_name, it cost 2 seconds.

I think foreign data wrapper is the reason. why fdw cost so many times?

not network speed, -> SELECT * FROM fdw.table_name only cost seconds.

may be some transform statements in fdw, -> when REFRESH MATERIALIZED VIEW(use fdw table), cpu work 100% in whole 4 hours.

Mal Sund
  • 78
  • 7