Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit31b6606

Browse files
committed
Make concurrent refresh check early that there is a unique index on matview.
In REFRESH MATERIALIZED VIEW command, CONCURRENTLY option is onlyallowed if there is at least one unique index with no WHERE clause onone or more columns of the matview. Previously, concurrent refreshchecked the existence of a unique index on the matview after fillingthe 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 timebefore we detected that and got the error. It was a waste of time.To eliminate such wasting time, this commit changes concurrent refreshso that it checks the existence of a unique index at the beginning ofthe 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 SawadaReviewed-by: Michael Paquier, Fujii Masao
1 parent57c9324 commit31b6606

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

‎src/backend/commands/matview.c

Lines changed: 53 additions & 6 deletions
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+
boolhasUniqueIndex= false;
229+
230+
foreach(indexoidscan,indexoidlist)
231+
{
232+
Oidindexoid=lfirst_oid(indexoidscan);
233+
RelationindexRel;
234+
Form_pg_indexindexStruct;
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)

[8]ページ先頭

©2009-2025 Movatter.jp