{"record":{"id":"05dd6611bb25e8db","repo":"apache/iceberg","slug":"table-already-exists-s-s","errorCode":null,"errorMessage":"Table already exists: %s.%s","messagePattern":"Table already exists: (.+?)\\.(.+?)","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java","lineNumber":286,"sourceCode":"    boolean updateHiveTable = false;\n\n    HiveLock lock = lockObject(base != null ? base : tableMetadata);\n    try {\n      lock.lock();\n\n      Table tbl = loadHmsTable();\n\n      if (tbl != null) {\n        // If we try to create the table but the metadata location is already set, then we had a\n        // concurrent commit\n        if (newTable\n            && tbl.getParameters().get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP)\n                != null) {\n          if (TableType.VIRTUAL_VIEW.name().equalsIgnoreCase(tbl.getTableType())) {\n            throw new AlreadyExistsException(\n                \"View with same name already exists: %s.%s\", database, tableName);\n          }\n          throw new AlreadyExistsException(\"Table already exists: %s.%s\", database, tableName);\n        }\n\n        updateHiveTable = true;\n        LOG.debug(\"Committing existing table: {}\", fullName);\n      } else {\n        tbl =\n            newHmsTable(\n                tableMetadata.property(HiveCatalog.HMS_TABLE_OWNER, HiveHadoopUtil.currentUser()));\n        LOG.debug(\"Committing new table: {}\", fullName);\n      }\n\n      tbl.setSd(\n          HiveOperationsBase.storageDescriptor(\n              tableMetadata.schema(),\n              tableMetadata.location(),\n              hiveEngineEnabled)); // set to pickup any schema changes\n\n      String metadataLocation = tbl.getParameters().get(METADATA_LOCATION_PROP);","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java#L268-L304","documentation":"HiveTableOperations.doCommit throws AlreadyExistsException 'Table already exists: %s.%s' when a create (newTable) commit finds an existing HMS entity that already has the Iceberg METADATA_LOCATION_PROP and is not a VIRTUAL_VIEW — a concurrent commit created the same table first. The commit fails rather than overwriting the other writer's table.","triggerScenarios":"Two concurrent catalog.createTable calls for the same database.name: the loser reaches doCommit, HMS returns the existing table, and its metadata_location property is set (excluding a plain HMS leftover).","commonSituations":"Duplicate jobs creating the same table in parallel; orchestration retries racing; multi-engine setup where two frameworks initialize the same Iceberg table.","solutions":["Catch AlreadyExistsException and load the existing table instead of creating (create-if-absent semantics).","Guard creation with an external lock or a check-then-create inside one orchestrator to prevent races.","If the existing table is unwanted, drop it first and then create, ensuring no other writer is committing."],"exampleFix":"// before\nTable t = catalog.createTable(ident, schema); // races with another writer\n// after\nTable t = catalog.tableExists(ident) ? catalog.loadTable(ident) : catalog.createTable(ident, schema);","handlingStrategy":"try-catch","validationCode":"boolean exists = catalog.tableExists(ident); // create only when false, ideally behind a lock","typeGuard":null,"tryCatchPattern":"try { table = catalog.createTable(ident, schema); } catch (AlreadyExistsException e) { table = catalog.loadTable(ident); }","preventionTips":["Adopt create-if-absent (load on AlreadyExists) for table initialization.","Use external locking or a single writer for schema bootstrap.","Avoid racing multi-engine initializations of the same table."],"tags":["concurrency","already-exists","hive-metastore"],"backgroundTag":"file-already-exists","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}