{"record":{"id":"ea528d28c3d06b2b","repo":"prestodb/presto","slug":"materialized-view-already-exists-ea528d","errorCode":"MATERIALIZED_VIEW_ALREADY_EXISTS","errorMessage":"Destination materialized view '%s' already exists","messagePattern":"Destination materialized view '(.+?)' already exists","errorType":"error_code","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java","lineNumber":895,"sourceCode":"            accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), viewName);\n\n            validateColumns(node, queryScope.getRelationType());\n\n            return createAndAssignScope(node, scope);\n        }\n\n        @Override\n        protected Scope visitCreateMaterializedView(CreateMaterializedView node, Optional<Scope> scope)\n        {\n            QualifiedObjectName viewName = createQualifiedObjectName(session, node, node.getName(), metadata);\n            analysis.setUpdateInfo(node.getUpdateInfo());\n            analysis.setCreateTableDestination(viewName);\n\n            if (metadataResolver.tableExists(viewName)) {\n                if (node.isNotExists()) {\n                    return createAndAssignScope(node, scope);\n                }\n                throw new SemanticException(MATERIALIZED_VIEW_ALREADY_EXISTS, node, \"Destination materialized view '%s' already exists\", viewName);\n            }\n            validateProperties(node.getProperties(), scope);\n\n            analysis.setCreateTableProperties(mapFromProperties(node.getProperties()));\n            analysis.setCreateTableComment(node.getComment());\n\n            accessControl.checkCanCreateTable(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), viewName);\n            accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), viewName);\n\n            // analyze the query that creates the table\n            Scope queryScope = process(node.getQuery(), scope);\n\n            validateColumns(node, queryScope.getRelationType());\n\n            validateBaseTables(analysis.getTableNodes(), node);\n\n            validateDeterministicFunctionsInMV(node);\n","sourceCodeStart":877,"sourceCodeEnd":913,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java#L877-L913","documentation":"CREATE MATERIALIZED VIEW fails when the destination materialized view (or table) name already exists, unless IF NOT EXISTS is specified. visitCreateMaterializedView checks metadataResolver.tableExists(viewName) and throws MATERIALIZED_VIEW_ALREADY_EXISTS when isNotExists() is false.","triggerScenarios":"CREATE MATERIALIZED VIEW on an existing name without IF NOT EXISTS; note the guard uses tableExists so an ordinary table with that name also triggers it.","commonSituations":"Re-deploying materialized view definitions from version control without drop-first; a name collision with an existing table; re-running setup scripts.","solutions":["Add IF NOT EXISTS: CREATE MATERIALIZED VIEW IF NOT EXISTS ...","DROP MATERIALIZED VIEW (or TABLE) with the conflicting name first, then recreate","Use a new destination name for the materialized view"],"exampleFix":"-- before\nCREATE MATERIALIZED VIEW reports.summary AS SELECT ...;\n-- after\nCREATE MATERIALIZED VIEW IF NOT EXISTS reports.summary AS SELECT ...;","handlingStrategy":"try-catch","validationCode":"// before CREATE MATERIALIZED VIEW:\n// boolean exists = metadataResolver.tableExists(viewName);","typeGuard":null,"tryCatchPattern":"catch (SemanticException e) { if (e.getCode() == MATERIALIZED_VIEW_ALREADY_EXISTS) { /* use IF NOT EXISTS or drop first */ } }","preventionTips":["Use IF NOT EXISTS when re-deploying MV definitions from VCS","Add drop-first steps to MV deployment scripts","Check for table name collisions before choosing the MV name"],"tags":["presto","materialized-view","already-exists","ddl"],"backgroundTag":"table-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"}