{"record":{"id":"0fe03c752f319e08","repo":"prestodb/presto","slug":"materialized-view-already-exists","errorCode":null,"errorMessage":"Materialized view already exists","messagePattern":"Materialized view already exists","errorType":"exception","errorClass":"MaterializedViewAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java","lineNumber":2677,"sourceCode":"                .setViewExpandedText(Optional.of(\"/* Presto Materialized View */\"))\n                .build();\n\n        MetastoreContext metastoreContext = getMetastoreContext(session);\n        validateMaterializedViewPartitionColumns(metastore, metastoreContext, viewTable, viewDefinition);\n\n        try {\n            PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(viewTable.getOwner());\n            metastore.createTable(\n                    session,\n                    viewTable,\n                    principalPrivileges,\n                    Optional.empty(),\n                    ignoreExisting,\n                    new PartitionStatistics(createEmptyStatistics(), ImmutableMap.of()),\n                    emptyList());\n        }\n        catch (TableAlreadyExistsException e) {\n            throw new MaterializedViewAlreadyExistsException(e.getTableName());\n        }\n    }\n\n    @Override\n    public void dropMaterializedView(ConnectorSession session, SchemaTableName viewName)\n    {\n        Optional<MaterializedViewDefinition> view = getMaterializedView(session, viewName);\n        if (!view.isPresent()) {\n            throw new MaterializedViewNotFoundException(viewName);\n        }\n\n        try {\n            metastore.dropTable(\n                    new HdfsContext(session, viewName.getSchemaName(), viewName.getTableName()),\n                    viewName.getSchemaName(),\n                    viewName.getTableName());\n        }\n        catch (TableNotFoundException e) {","sourceCodeStart":2659,"sourceCodeEnd":2695,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java#L2659-L2695","documentation":"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.","triggerScenarios":"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.","commonSituations":"CI/CD re-deploying the same materialized view definition; two pipelines materializing the same aggregate concurrently; leftover object from a previous failed drop.","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"],"exampleFix":"-- before\nCREATE MATERIALIZED VIEW mv_daily AS SELECT ...;\n-- after\nCREATE MATERIALIZED VIEW IF NOT EXISTS mv_daily AS SELECT ...;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    createMaterializedView(session, metadata, definition, ignoreExisting);\n} catch (MaterializedViewAlreadyExistsException e) {\n    if (!ignoreExisting) {\n        run(\"DROP MATERIALIZED VIEW IF EXISTS \" + e.getTableName());\n        createMaterializedView(session, metadata, definition, ignoreExisting);\n    }\n}","preventionTips":["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"],"tags":["hive","materialized-view","already-exists","ddl"],"backgroundTag":"object-already-exists","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}