1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-27 22:56:53 +03:00

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
This commit is contained in:
Fujii Masao 2016-02-16 02:15:44 +09:00
parent 57c9324755
commit 31b6606c48

View File

@ -216,6 +216,51 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
"the rule for materialized view \"%s\" is not a single action", "the rule for materialized view \"%s\" is not a single action",
RelationGetRelationName(matviewRel)); RelationGetRelationName(matviewRel));
/*
* Check that there is a unique index with no WHERE clause on
* one or more columns of the materialized view if CONCURRENTLY
* is specified.
*/
if (concurrent)
{
List *indexoidlist = RelationGetIndexList(matviewRel);
ListCell *indexoidscan;
bool hasUniqueIndex = false;
foreach(indexoidscan, indexoidlist)
{
Oid indexoid = lfirst_oid(indexoidscan);
Relation indexRel;
Form_pg_index indexStruct;
indexRel = index_open(indexoid, AccessShareLock);
indexStruct = indexRel->rd_index;
if (indexStruct->indisunique &&
IndexIsValid(indexStruct) &&
RelationGetIndexExpressions(indexRel) == NIL &&
RelationGetIndexPredicate(indexRel) == NIL &&
indexStruct->indnatts > 0)
{
hasUniqueIndex = true;
index_close(indexRel, AccessShareLock);
break;
}
index_close(indexRel, AccessShareLock);
}
list_free(indexoidlist);
if (!hasUniqueIndex)
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("cannot refresh materialized view \"%s\" concurrently",
quote_qualified_identifier(get_namespace_name(RelationGetNamespace(matviewRel)),
RelationGetRelationName(matviewRel))),
errhint("Create a unique index with no WHERE clause on one or more columns of the materialized view.")));
}
/* /*
* The stored query was rewritten at the time of the MV definition, but * The stored query was rewritten at the time of the MV definition, but
* has not been scribbled on by the planner. * has not been scribbled on by the planner.
@ -695,12 +740,14 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
list_free(indexoidlist); list_free(indexoidlist);
if (!foundUniqueIndex) /*
ereport(ERROR, * There must be at least one unique index on the matview.
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), *
errmsg("cannot refresh materialized view \"%s\" concurrently", * ExecRefreshMatView() checks that after taking the exclusive lock on
matviewname), * the matview. So at least one unique index is guaranteed to exist here
errhint("Create a unique index with no WHERE clause on one or more columns of the materialized view."))); * because the lock is still being held.
*/
Assert(foundUniqueIndex);
appendStringInfoString(&querybuf, appendStringInfoString(&querybuf,
" AND newdata OPERATOR(pg_catalog.*=) mv) " " AND newdata OPERATOR(pg_catalog.*=) mv) "