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

  1. Use CREATE MATERIALIZED VIEW IF NOT EXISTS or pass ignoreExisting = true
  2. Drop the existing object first (DROP MATERIALIZED VIEW / DROP TABLE as appropriate)
  3. Coordinate deployments so only one pipeline creates the view
  4. 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

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


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/0fe03c752f319e08. Report an issue: GitHub.