Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 31b6606

Browse files
committed
Make concurrent refresh check early that there is a unique index on matview.
In REFRESH MATERIALIZED VIEW command, CONCURRENTLY option is only allowed if there is at least one unique index with no WHERE clause on one or more columns of the matview. Previously, concurrent refresh checked the existence of a unique index on the matview after filling the data to new snapshot, i.e., after calling refresh_matview_datafill(). So, when there was no unique index, we could need to wait a long time before we detected that and got the error. It was a waste of time. To eliminate such wasting time, this commit changes concurrent refresh so that it checks the existence of a unique index at the beginning of the refresh operation, i.e., before starting any time-consuming jobs. If CONCURRENTLY option is not allowed due to lack of a unique index, concurrent refresh can immediately detect it and emit an error. Author: Masahiko Sawada Reviewed-by: Michael Paquier, Fujii Masao
1 parent 57c9324 commit 31b6606

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

src/backend/commands/matview.c

+53-6
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,51 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
216216
"the rule for materialized view \"%s\" is not a single action",
217217
RelationGetRelationName(matviewRel));
218218

219+
/*
220+
* Check that there is a unique index with no WHERE clause on
221+
* one or more columns of the materialized view if CONCURRENTLY
222+
* is specified.
223+
*/
224+
if (concurrent)
225+
{
226+
List *indexoidlist = RelationGetIndexList(matviewRel);
227+
ListCell *indexoidscan;
228+
bool hasUniqueIndex = false;
229+
230+
foreach(indexoidscan, indexoidlist)
231+
{
232+
Oid indexoid = lfirst_oid(indexoidscan);
233+
Relation indexRel;
234+
Form_pg_index indexStruct;
235+
236+
indexRel = index_open(indexoid, AccessShareLock);
237+
indexStruct = indexRel->rd_index;
238+
239+
if (indexStruct->indisunique &&
240+
IndexIsValid(indexStruct) &&
241+
RelationGetIndexExpressions(indexRel) == NIL &&
242+
RelationGetIndexPredicate(indexRel) == NIL &&
243+
indexStruct->indnatts > 0)
244+
{
245+
hasUniqueIndex = true;
246+
index_close(indexRel, AccessShareLock);
247+
break;
248+
}
249+
250+
index_close(indexRel, AccessShareLock);
251+
}
252+
253+
list_free(indexoidlist);
254+
255+
if (!hasUniqueIndex)
256+
ereport(ERROR,
257+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
258+
errmsg("cannot refresh materialized view \"%s\" concurrently",
259+
quote_qualified_identifier(get_namespace_name(RelationGetNamespace(matviewRel)),
260+
RelationGetRelationName(matviewRel))),
261+
errhint("Create a unique index with no WHERE clause on one or more columns of the materialized view.")));
262+
}
263+
219264
/*
220265
* The stored query was rewritten at the time of the MV definition, but
221266
* has not been scribbled on by the planner.
@@ -695,12 +740,14 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
695740

696741
list_free(indexoidlist);
697742

698-
if (!foundUniqueIndex)
699-
ereport(ERROR,
700-
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
701-
errmsg("cannot refresh materialized view \"%s\" concurrently",
702-
matviewname),
703-
errhint("Create a unique index with no WHERE clause on one or more columns of the materialized view.")));
743+
/*
744+
* There must be at least one unique index on the matview.
745+
*
746+
* ExecRefreshMatView() checks that after taking the exclusive lock on
747+
* the matview. So at least one unique index is guaranteed to exist here
748+
* because the lock is still being held.
749+
*/
750+
Assert(foundUniqueIndex);
704751

705752
appendStringInfoString(&querybuf,
706753
" AND newdata OPERATOR(pg_catalog.*=) mv) "

0 commit comments

Comments
 (0)