@@ -60,7 +60,7 @@ static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self);
60
60
static void transientrel_shutdown (DestReceiver * self );
61
61
static void transientrel_destroy (DestReceiver * self );
62
62
static uint64 refresh_matview_datafill (DestReceiver * dest , Query * query ,
63
- const char * queryString );
63
+ const char * queryString , bool is_create );
64
64
static char * make_temptable_name_n (char * tempname , int n );
65
65
static void refresh_by_match_merge (Oid matviewOid , Oid tempOid , Oid relowner ,
66
66
int save_sec_context );
@@ -135,8 +135,8 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
135
135
RangeVarCallbackMaintainsTable ,
136
136
NULL );
137
137
138
- return RefreshMatViewByOid (matviewOid , stmt -> skipData , stmt -> concurrent ,
139
- queryString , qc );
138
+ return RefreshMatViewByOid (matviewOid , false , stmt -> skipData ,
139
+ stmt -> concurrent , queryString , qc );
140
140
}
141
141
142
142
/*
@@ -157,10 +157,14 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
157
157
*
158
158
* The matview's "populated" state is changed based on whether the contents
159
159
* 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.
160
163
*/
161
164
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 )
164
168
{
165
169
Relation matviewRel ;
166
170
RewriteRule * rule ;
@@ -169,7 +173,6 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
169
173
Oid tableSpace ;
170
174
Oid relowner ;
171
175
Oid OIDNewHeap ;
172
- DestReceiver * dest ;
173
176
uint64 processed = 0 ;
174
177
char relpersistence ;
175
178
Oid save_userid ;
@@ -248,6 +251,8 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
248
251
ListCell * indexoidscan ;
249
252
bool hasUniqueIndex = false;
250
253
254
+ Assert (!is_create );
255
+
251
256
foreach (indexoidscan , indexoidlist )
252
257
{
253
258
Oid indexoid = lfirst_oid (indexoidscan );
@@ -284,7 +289,9 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
284
289
* NB: We count on this to protect us against problems with refreshing the
285
290
* data using TABLE_INSERT_FROZEN.
286
291
*/
287
- CheckTableNotInUse (matviewRel , "REFRESH MATERIALIZED VIEW" );
292
+ CheckTableNotInUse (matviewRel ,
293
+ is_create ? "CREATE MATERIALIZED VIEW" :
294
+ "REFRESH MATERIALIZED VIEW" );
288
295
289
296
/*
290
297
* Tentatively mark the matview as populated or not (this will roll back
@@ -313,11 +320,16 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
313
320
matviewRel -> rd_rel -> relam ,
314
321
relpersistence , ExclusiveLock );
315
322
LockRelationOid (OIDNewHeap , AccessExclusiveLock );
316
- dest = CreateTransientRelDestReceiver (OIDNewHeap );
317
323
318
324
/* Generate the data, if wanted. */
319
325
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
+ }
321
333
322
334
/* Make the matview match the newly generated data. */
323
335
if (concurrent )
@@ -369,9 +381,14 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
369
381
* i.e., the display_rowcount flag of CMDTAG_REFRESH_MATERIALIZED_VIEW
370
382
* command tag is left false in cmdtaglist.h. Otherwise, the change of
371
383
* 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.
372
387
*/
373
388
if (qc )
374
- SetQueryCompletion (qc , CMDTAG_REFRESH_MATERIALIZED_VIEW , processed );
389
+ SetQueryCompletion (qc ,
390
+ is_create ? CMDTAG_SELECT : CMDTAG_REFRESH_MATERIALIZED_VIEW ,
391
+ processed );
375
392
376
393
return address ;
377
394
}
@@ -386,7 +403,7 @@ RefreshMatViewByOid(Oid matviewOid, bool skipData, bool concurrent,
386
403
*/
387
404
static uint64
388
405
refresh_matview_datafill (DestReceiver * dest , Query * query ,
389
- const char * queryString )
406
+ const char * queryString , bool is_create )
390
407
{
391
408
List * rewritten ;
392
409
PlannedStmt * plan ;
@@ -401,7 +418,8 @@ refresh_matview_datafill(DestReceiver *dest, Query *query,
401
418
402
419
/* SELECT should never rewrite to more or less than one SELECT query */
403
420
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" );
405
423
query = (Query * ) linitial (rewritten );
406
424
407
425
/* Check for user-requested abort. */
0 commit comments