prestodb/presto · error · MaterializedViewAlreadyExistsException
Materialized view already exists
Error message
Materialized view already exists
What it means
Converted from TableAlreadyExistsException at the end of createMaterializedView: the metastore createTable call (with ignoreExisting honored) found that an object already exists under the materialized view's name, so MaterializedViewAlreadyExistsException is raised. The name is taken by some existing table or materialized view.
Source
Thrown at presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java:2677
.setViewExpandedText(Optional.of("/* Presto Materialized View */"))
.build();
MetastoreContext metastoreContext = getMetastoreContext(session);
validateMaterializedViewPartitionColumns(metastore, metastoreContext, viewTable, viewDefinition);
try {
PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(viewTable.getOwner());
metastore.createTable(
session,
viewTable,
principalPrivileges,
Optional.empty(),
ignoreExisting,
new PartitionStatistics(createEmptyStatistics(), ImmutableMap.of()),
emptyList());
}
catch (TableAlreadyExistsException e) {
throw new MaterializedViewAlreadyExistsException(e.getTableName());
}
}
@Override
public void dropMaterializedView(ConnectorSession session, SchemaTableName viewName)
{
Optional<MaterializedViewDefinition> view = getMaterializedView(session, viewName);
if (!view.isPresent()) {
throw new MaterializedViewNotFoundException(viewName);
}
try {
metastore.dropTable(
new HdfsContext(session, viewName.getSchemaName(), viewName.getTableName()),
viewName.getSchemaName(),
viewName.getTableName());
}
catch (TableNotFoundException e) {View on GitHub (pinned to 55bb57d202)
Solutions
- Use CREATE MATERIALIZED VIEW IF NOT EXISTS or pass ignoreExisting = true
- Drop the existing object first (DROP MATERIALIZED VIEW / DROP TABLE as appropriate)
- Coordinate deployments so only one pipeline creates the view
- Catch MaterializedViewAlreadyExistsException and treat as no-op when definitions match
Example fix
-- before CREATE MATERIALIZED VIEW mv_daily AS SELECT ...; -- after CREATE MATERIALIZED VIEW IF NOT EXISTS mv_daily AS SELECT ...;
Defensive patterns
Strategy: try-catch
Try / catch
try {
createMaterializedView(session, metadata, definition, ignoreExisting);
} catch (MaterializedViewAlreadyExistsException e) {
if (!ignoreExisting) {
run("DROP MATERIALIZED VIEW IF EXISTS " + e.getTableName());
createMaterializedView(session, metadata, definition, ignoreExisting);
}
} Prevention
- Use IF NOT EXISTS for idempotent deployments
- Pass ignoreExisting=true in re-runnable jobs
- Avoid two pipelines creating the same MV concurrently
- Clean up leftovers from failed runs before retrying
When it happens
Trigger: CREATE MATERIALIZED VIEW where the target name already exists in the metastore; a race where another session creates the same name after the existence check; re-running the same DDL without IF NOT EXISTS/ignoreExisting.
Common situations: CI/CD re-deploying the same materialized view definition; two pipelines materializing the same aggregate concurrently; leftover object from a previous failed drop.
Related errors
- View already exists
- Materialized view not found
- ALREADY_EXISTS
- MATERIALIZED_VIEW_ALREADY_EXISTS
- View not found
AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04).
Data as JSON: /api/errors/0fe03c752f319e08.
Report an issue: GitHub.