Skip to content

Commit ca2eea3

Browse files
committed
Add is_create parameter to RefreshMatviewByOid().
RefreshMatviewByOid is used for both REFRESH and CREATE MATERIALIZED VIEW. This flag is currently just used for handling internal error messages, but also aimed to improve code-readability. Author: Yugo Nagata Discussion: https://postgr.es/m/20240726122630.70e889f63a4d7e26f8549de8@sraoss.co.jp
1 parent f683d3a commit ca2eea3

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/backend/commands/createas.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,8 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
346346
*/
347347
if (do_refresh)
348348
{
349-
RefreshMatViewByOid(address.objectId, false, false,
349+
RefreshMatViewByOid(address.objectId, true, false, false,
350350
pstate->p_sourcetext, qc);
351-
352-
if (qc)
353-
qc->commandTag = CMDTAG_SELECT;
354351
}
355352

356353
return address;

src/backend/commands/matview.c

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self);
6060
static void transientrel_shutdown(DestReceiver *self);
6161
static void transientrel_destroy(DestReceiver *self);
6262
static uint64 refresh_matview_datafill(DestReceiver *dest, Query *query,
63-
const char *queryString);
63+
const char *queryString, bool is_create);
6464
static char *make_temptable_name_n(char *tempname, int n);
6565
static void refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
6666
int save_sec_context);
@@ -135,8 +135,8 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
135135
RangeVarCallbackMaintainsTable,
136136
NULL);
137137

138-
return RefreshMatViewByOid(matviewOid, stmt->skipData, stmt->concurrent,
139-
queryString, qc);
138+
return RefreshMatViewByOid(matviewOid, false, stmt->skipData,
139+
stmt->concurrent, queryString, qc);
140140
}
141141

142142
/*
@@ -157,10 +157,14 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
157157
*
158158
* The matview's "populated" state is changed based on whether the contents
159159
* reflect the result set of the materialized view's query.
160+
*
161+
* This is also used to populate the materialized view created by CREATE
162+
* MATERIALIZED VIEW command.
160163
*/
161164
ObjectAddress
162-
RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
163-
const char *queryString, QueryCompletion *qc)
165+
RefreshMatViewByOid(Oid matviewOid, bool is_create, bool skipData,
166+
bool concurrent, const char *queryString,
167+
QueryCompletion *qc)
164168
{
165169
Relation matviewRel;
166170
RewriteRule *rule;
@@ -169,7 +173,6 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
169173
Oid tableSpace;
170174
Oid relowner;
171175
Oid OIDNewHeap;
172-
DestReceiver *dest;
173176
uint64 processed = 0;
174177
char relpersistence;
175178
Oid save_userid;
@@ -248,6 +251,8 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
248251
ListCell *indexoidscan;
249252
bool hasUniqueIndex = false;
250253

254+
Assert(!is_create);
255+
251256
foreach(indexoidscan, indexoidlist)
252257
{
253258
Oid indexoid = lfirst_oid(indexoidscan);
@@ -284,7 +289,9 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
284289
* NB: We count on this to protect us against problems with refreshing the
285290
* data using TABLE_INSERT_FROZEN.
286291
*/
287-
CheckTableNotInUse(matviewRel, "REFRESH MATERIALIZED VIEW");
292+
CheckTableNotInUse(matviewRel,
293+
is_create ? "CREATE MATERIALIZED VIEW" :
294+
"REFRESH MATERIALIZED VIEW");
288295

289296
/*
290297
* Tentatively mark the matview as populated or not (this will roll back
@@ -313,11 +320,16 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
313320
matviewRel->rd_rel->relam,
314321
relpersistence, ExclusiveLock);
315322
LockRelationOid(OIDNewHeap, AccessExclusiveLock);
316-
dest = CreateTransientRelDestReceiver(OIDNewHeap);
317323

318324
/* Generate the data, if wanted. */
319325
if (!skipData)
320-
processed = refresh_matview_datafill(dest, dataQuery, queryString);
326+
{
327+
DestReceiver *dest;
328+
329+
dest = CreateTransientRelDestReceiver(OIDNewHeap);
330+
processed = refresh_matview_datafill(dest, dataQuery, queryString,
331+
is_create);
332+
}
321333

322334
/* Make the matview match the newly generated data. */
323335
if (concurrent)
@@ -369,9 +381,14 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
369381
* i.e., the display_rowcount flag of CMDTAG_REFRESH_MATERIALIZED_VIEW
370382
* command tag is left false in cmdtaglist.h. Otherwise, the change of
371383
* completion tag output might break applications using it.
384+
*
385+
* When called from CREATE MATERIALIZED VIEW comand, the rowcount is
386+
* displayed with the command tag CMDTAG_SELECT.
372387
*/
373388
if (qc)
374-
SetQueryCompletion(qc, CMDTAG_REFRESH_MATERIALIZED_VIEW, processed);
389+
SetQueryCompletion(qc,
390+
is_create ? CMDTAG_SELECT : CMDTAG_REFRESH_MATERIALIZED_VIEW,
391+
processed);
375392

376393
return address;
377394
}
@@ -386,7 +403,7 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
386403
*/
387404
static uint64
388405
refresh_matview_datafill(DestReceiver *dest, Query *query,
389-
const char *queryString)
406+
const char *queryString, bool is_create)
390407
{
391408
List *rewritten;
392409
PlannedStmt *plan;
@@ -401,7 +418,8 @@ refresh_matview_datafill(DestReceiver *dest, Query *query,
401418

402419
/* SELECT should never rewrite to more or less than one SELECT query */
403420
if (list_length(rewritten) != 1)
404-
elog(ERROR, "unexpected rewrite result for REFRESH MATERIALIZED VIEW");
421+
elog(ERROR, "unexpected rewrite result for %s",
422+
is_create ? "CREATE MATERIALIZED VIEW " : "REFRESH MATERIALIZED VIEW");
405423
query = (Query *) linitial(rewritten);
406424

407425
/* Check for user-requested abort. */

src/include/commands/matview.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ extern void SetMatViewPopulatedState(Relation relation, bool newstate);
2525

2626
extern ObjectAddress ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
2727
QueryCompletion *qc);
28-
extern ObjectAddress RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
29-
const char *queryString, QueryCompletion *qc);
28+
extern ObjectAddress RefreshMatViewByOid(Oid matviewOid, bool is_create, bool skipData,
29+
bool concurrent, const char *queryString,
30+
QueryCompletion *qc);
3031

3132
extern DestReceiver *CreateTransientRelDestReceiver(Oid transientoid);
3233

0 commit comments

Comments
 (0)