Skip to content

Commit 7926a9a

Browse files
committed
Small refactoring around ExecCreateTableAs().
Since commit 4b74ebf, the refresh logic is used to populate materialized views, so we can simplify the error message in ExecCreateTableAs(). Also, RefreshMatViewByOid() is moved to just after create_ctas_nodata() call to improve code readability. Author: Yugo Nagata Discussion: https://postgr.es/m/20240802161301.d975daca9ba7a706fa05ecd7@sraoss.co.jp
1 parent 3cffe79 commit 7926a9a

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/backend/commands/createas.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,6 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
228228
bool do_refresh = false;
229229
DestReceiver *dest;
230230
ObjectAddress address;
231-
List *rewritten;
232-
PlannedStmt *plan;
233-
QueryDesc *queryDesc;
234231

235232
/* Check if the relation exists or not */
236233
if (CreateTableAsRelExists(stmt))
@@ -279,9 +276,25 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
279276
* from running the planner before all dependencies are set up.
280277
*/
281278
address = create_ctas_nodata(query->targetList, into);
279+
280+
/*
281+
* For materialized views, reuse the REFRESH logic, which locks down
282+
* security-restricted operations and restricts the search_path. This
283+
* reduces the chance that a subsequent refresh will fail.
284+
*/
285+
if (do_refresh)
286+
RefreshMatViewByOid(address.objectId, true, false, false,
287+
pstate->p_sourcetext, qc);
288+
282289
}
283290
else
284291
{
292+
List *rewritten;
293+
PlannedStmt *plan;
294+
QueryDesc *queryDesc;
295+
296+
Assert(!is_matview);
297+
285298
/*
286299
* Parse analysis was done already, but we still have to run the rule
287300
* rewriter. We do not do AcquireRewriteLocks: we assume the query
@@ -292,9 +305,7 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
292305

293306
/* SELECT should never rewrite to more or less than one SELECT query */
294307
if (list_length(rewritten) != 1)
295-
elog(ERROR, "unexpected rewrite result for %s",
296-
is_matview ? "CREATE MATERIALIZED VIEW" :
297-
"CREATE TABLE AS SELECT");
308+
elog(ERROR, "unexpected rewrite result for CREATE TABLE AS SELECT");
298309
query = linitial_node(Query, rewritten);
299310
Assert(query->commandType == CMD_SELECT);
300311

@@ -339,17 +350,6 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
339350
PopActiveSnapshot();
340351
}
341352

342-
/*
343-
* For materialized views, reuse the REFRESH logic, which locks down
344-
* security-restricted operations and restricts the search_path. This
345-
* reduces the chance that a subsequent refresh will fail.
346-
*/
347-
if (do_refresh)
348-
{
349-
RefreshMatViewByOid(address.objectId, true, false, false,
350-
pstate->p_sourcetext, qc);
351-
}
352-
353353
return address;
354354
}
355355

0 commit comments

Comments
 (0)