{"record":{"id":"35f55b0431f2a654","repo":"apache/iceberg","slug":"namespace-already-exists-s-35f55b","errorCode":null,"errorMessage":"Namespace already exists: %s","messagePattern":"Namespace already exists: (.+?)","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java","lineNumber":551,"sourceCode":"        isValidateNamespace(namespace),\n        \"Cannot support multi part namespace in Hive Metastore: %s\",\n        namespace);\n    Preconditions.checkArgument(\n        meta.get(HMS_DB_OWNER_TYPE) == null || meta.get(HMS_DB_OWNER) != null,\n        \"Create namespace setting %s without setting %s is not allowed\",\n        HMS_DB_OWNER_TYPE,\n        HMS_DB_OWNER);\n    try {\n      clients.run(\n          client -> {\n            client.createDatabase(convertToDatabase(namespace, meta));\n            return null;\n          });\n\n      LOG.info(\"Created namespace: {}\", namespace);\n\n    } catch (org.apache.hadoop.hive.metastore.api.AlreadyExistsException e) {\n      throw new AlreadyExistsException(e, \"Namespace already exists: %s\", namespace);\n\n    } catch (TException e) {\n      throw new RuntimeException(\n          \"Failed to create namespace \" + namespace + \" in Hive Metastore\", e);\n\n    } catch (InterruptedException e) {\n      Thread.currentThread().interrupt();\n      throw new RuntimeException(\n          \"Interrupted in call to createDatabase(name) \" + namespace + \" in Hive Metastore\", e);\n    }\n  }\n\n  @Override\n  public List<Namespace> listNamespaces(Namespace namespace) {\n    if (!namespace.isEmpty() && (!isValidateNamespace(namespace) || !namespaceExists(namespace))) {\n      throw new NoSuchNamespaceException(\"Namespace does not exist: %s\", namespace);\n    }\n    if (!namespace.isEmpty()) {","sourceCodeStart":533,"sourceCodeEnd":569,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveCatalog.java#L533-L569","documentation":"HiveCatalog.createNamespace maps Hive Metastore's AlreadyExistsException to Iceberg's org.apache.iceberg.exceptions.AlreadyExistsException when a database (namespace) with the same name already exists in the Hive Metastore. The catalog aborts creation rather than overwriting the existing database.","triggerScenarios":"Calling catalog.createNamespace(Namespace.of(\"db\")) when a Hive database named 'db' already exists in the metastore.","commonSituations":"Re-running an initialization script without a create-if-not-exists check; two jobs racing to create the same namespace; migrating catalogs where the namespace was created previously; case-sensitivity mismatches making the developer think the namespace is new.","solutions":["Check existence first with catalog.namespaceExists(namespace) before calling createNamespace, or catch AlreadyExistsException and treat it as success","List existing namespaces via catalog.listNamespaces() to confirm the conflicting name","Use a different namespace name if the existing database is not yours to reuse","If a race is possible, wrap the call in try/catch for AlreadyExistsException instead of pre-checking"],"exampleFix":"// before\ncatalog.createNamespace(Namespace.of(\"analytics\"));\n// after\nNamespace ns = Namespace.of(\"analytics\");\nif (!catalog.namespaceExists(ns)) {\n  catalog.createNamespace(ns);\n}","handlingStrategy":"validation","validationCode":"Namespace ns = Namespace.of(\"analytics\");\nif (catalog.namespaceExists(ns)) {\n  throw new IllegalStateException(\"Namespace already exists: \" + ns);\n}","typeGuard":null,"tryCatchPattern":"try {\n  catalog.createNamespace(ns);\n} catch (AlreadyExistsException e) {\n  // treat as success or fail fast depending on workflow\n}","preventionTips":["Check namespaceExists before creating","Idempotent init scripts: catch AlreadyExistsException","Serialize namespace creation through one job/owner","Avoid mixed-case names that cause apparent duplicates"],"tags":["catalog","namespace","hive-metastore","idempotency"],"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"}