Hello I ran into this while working on an event trigger issue in the same code path [1], and only found this thread after writing a fix, so here is a patch for option 2.
It follows the previously discussed approach: with ANALYZE, EXPLAIN first creates the empty view with create_ctas_nodata(), in the caller's context,. Then it plans and runs the query as a security-restricted operation with a restricted search_path, so it also covers functions executed by the planner. A new IntoClause field tells the intorel receiver to fill the existing relation instead of creating it. Like RefreshMatViewByOid(), it keeps the view open while filling it, otherwise a function evaluated by the planner could, for example, create an index on it that intorel_receive() doesn't maintain. [1]: https://postgr.es/m/CAN4CZFOycbakF5swg0i_hH2dZTkMP2%2BoA%3DnoU0VFFFe3687ojw%40mail.gmail.com
v1-0001-Restrict-EXPLAIN-ANALYZE-CREATE-MATERIALIZED-VIEW.patch
Description: Binary data
