@@ -112,15 +112,44 @@ SetMatViewPopulatedState(Relation relation, bool newstate)
112
112
/*
113
113
* ExecRefreshMatView -- execute a REFRESH MATERIALIZED VIEW command
114
114
*
115
+ * If WITH NO DATA was specified, this is effectively like a TRUNCATE;
116
+ * otherwise it is like a TRUNCATE followed by an INSERT using the SELECT
117
+ * statement associated with the materialized view. The statement node's
118
+ * skipData field shows whether the clause was used.
119
+ */
120
+ ObjectAddress
121
+ ExecRefreshMatView (RefreshMatViewStmt * stmt , const char * queryString ,
122
+ ParamListInfo params , QueryCompletion * qc )
123
+ {
124
+ Oid matviewOid ;
125
+ LOCKMODE lockmode ;
126
+
127
+ /* Determine strength of lock needed. */
128
+ lockmode = stmt -> concurrent ? ExclusiveLock : AccessExclusiveLock ;
129
+
130
+ /*
131
+ * Get a lock until end of transaction.
132
+ */
133
+ matviewOid = RangeVarGetRelidExtended (stmt -> relation ,
134
+ lockmode , 0 ,
135
+ RangeVarCallbackMaintainsTable ,
136
+ NULL );
137
+
138
+ return RefreshMatViewByOid (matviewOid , stmt -> skipData , stmt -> concurrent ,
139
+ queryString , params , qc );
140
+ }
141
+
142
+ /*
143
+ * RefreshMatViewByOid -- refresh materialized view by OID
144
+ *
115
145
* This refreshes the materialized view by creating a new table and swapping
116
146
* the relfilenumbers of the new table and the old materialized view, so the OID
117
147
* of the original materialized view is preserved. Thus we do not lose GRANT
118
148
* nor references to this materialized view.
119
149
*
120
- * If WITH NO DATA was specified, this is effectively like a TRUNCATE;
121
- * otherwise it is like a TRUNCATE followed by an INSERT using the SELECT
122
- * statement associated with the materialized view. The statement node's
123
- * skipData field shows whether the clause was used.
150
+ * If skipData is true, this is effectively like a TRUNCATE; otherwise it is
151
+ * like a TRUNCATE followed by an INSERT using the SELECT statement associated
152
+ * with the materialized view.
124
153
*
125
154
* Indexes are rebuilt too, via REINDEX. Since we are effectively bulk-loading
126
155
* the new heap, it's better to create the indexes afterwards than to fill them
@@ -130,10 +159,10 @@ SetMatViewPopulatedState(Relation relation, bool newstate)
130
159
* reflect the result set of the materialized view's query.
131
160
*/
132
161
ObjectAddress
133
- ExecRefreshMatView (RefreshMatViewStmt * stmt , const char * queryString ,
134
- ParamListInfo params , QueryCompletion * qc )
162
+ RefreshMatViewByOid (Oid matviewOid , bool skipData , bool concurrent ,
163
+ const char * queryString , ParamListInfo params ,
164
+ QueryCompletion * qc )
135
165
{
136
- Oid matviewOid ;
137
166
Relation matviewRel ;
138
167
RewriteRule * rule ;
139
168
List * actions ;
@@ -143,25 +172,12 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
143
172
Oid OIDNewHeap ;
144
173
DestReceiver * dest ;
145
174
uint64 processed = 0 ;
146
- bool concurrent ;
147
- LOCKMODE lockmode ;
148
175
char relpersistence ;
149
176
Oid save_userid ;
150
177
int save_sec_context ;
151
178
int save_nestlevel ;
152
179
ObjectAddress address ;
153
180
154
- /* Determine strength of lock needed. */
155
- concurrent = stmt -> concurrent ;
156
- lockmode = concurrent ? ExclusiveLock : AccessExclusiveLock ;
157
-
158
- /*
159
- * Get a lock until end of transaction.
160
- */
161
- matviewOid = RangeVarGetRelidExtended (stmt -> relation ,
162
- lockmode , 0 ,
163
- RangeVarCallbackMaintainsTable ,
164
- NULL );
165
181
matviewRel = table_open (matviewOid , NoLock );
166
182
relowner = matviewRel -> rd_rel -> relowner ;
167
183
@@ -190,7 +206,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
190
206
errmsg ("CONCURRENTLY cannot be used when the materialized view is not populated" )));
191
207
192
208
/* Check that conflicting options have not been specified. */
193
- if (concurrent && stmt -> skipData )
209
+ if (concurrent && skipData )
194
210
ereport (ERROR ,
195
211
(errcode (ERRCODE_SYNTAX_ERROR ),
196
212
errmsg ("%s and %s options cannot be used together" ,
@@ -275,7 +291,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
275
291
* Tentatively mark the matview as populated or not (this will roll back
276
292
* if we fail later).
277
293
*/
278
- SetMatViewPopulatedState (matviewRel , !stmt -> skipData );
294
+ SetMatViewPopulatedState (matviewRel , !skipData );
279
295
280
296
/* Concurrent refresh builds new data in temp tablespace, and does diff. */
281
297
if (concurrent )
@@ -301,7 +317,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
301
317
dest = CreateTransientRelDestReceiver (OIDNewHeap );
302
318
303
319
/* Generate the data, if wanted. */
304
- if (!stmt -> skipData )
320
+ if (!skipData )
305
321
processed = refresh_matview_datafill (dest , dataQuery , queryString );
306
322
307
323
/* Make the matview match the newly generated data. */
@@ -333,7 +349,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
333
349
* inserts and deletes it issues get counted by lower-level code.)
334
350
*/
335
351
pgstat_count_truncate (matviewRel );
336
- if (!stmt -> skipData )
352
+ if (!skipData )
337
353
pgstat_count_heap_insert (matviewRel , processed );
338
354
}
339
355
0 commit comments