{"record":{"id":"4b6a3078962327a4","repo":"apache/iceberg","slug":"namespacealreadyexistsexception-namespace","errorCode":null,"errorMessage":"NamespaceAlreadyExistsException(namespace)","messagePattern":"NamespaceAlreadyExistsException\\(namespace\\)","errorType":"exception","errorClass":"NamespaceAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java","lineNumber":519,"sourceCode":"\n    throw new NoSuchNamespaceException(namespace);\n  }\n\n  @Override\n  public void createNamespace(String[] namespace, Map<String, String> metadata)\n      throws NamespaceAlreadyExistsException {\n    if (asNamespaceCatalog != null) {\n      try {\n        if (asNamespaceCatalog instanceof HadoopCatalog\n            && DEFAULT_NS_KEYS.equals(metadata.keySet())) {\n          // Hadoop catalog will reject metadata properties, but Spark automatically adds \"owner\".\n          // If only the automatic properties are present, replace metadata with an empty map.\n          asNamespaceCatalog.createNamespace(Namespace.of(namespace), ImmutableMap.of());\n        } else {\n          asNamespaceCatalog.createNamespace(Namespace.of(namespace), metadata);\n        }\n      } catch (AlreadyExistsException e) {\n        throw new NamespaceAlreadyExistsException(namespace);\n      }\n    } else {\n      throw new UnsupportedOperationException(\n          \"Namespaces are not supported by catalog: \" + catalogName);\n    }\n  }\n\n  @Override\n  public void alterNamespace(String[] namespace, NamespaceChange... changes)\n      throws NoSuchNamespaceException {\n    if (asNamespaceCatalog != null) {\n      Map<String, String> updates = Maps.newHashMap();\n      Set<String> removals = Sets.newHashSet();\n      for (NamespaceChange change : changes) {\n        if (change instanceof NamespaceChange.SetProperty) {\n          NamespaceChange.SetProperty set = (NamespaceChange.SetProperty) change;\n          updates.put(set.property(), set.value());\n        } else if (change instanceof NamespaceChange.RemoveProperty) {","sourceCodeStart":501,"sourceCodeEnd":537,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java#L501-L537","documentation":"createNamespace was called on a catalog where the namespace already exists; the Iceberg AlreadyExistsException was caught and rethrown as Spark's NamespaceAlreadyExistsException. Iceberg catalogs enforce namespace uniqueness and do not create-if-absent silently.","triggerScenarios":"CREATE NAMESPACE catalog.ns where ns already exists; idempotent setup scripts that rerun createNamespace without an existence check; two concurrent jobs racing to create the same namespace; earlier partial pipeline run already created it.","commonSituations":"IF NOT EXISTS forgotten in DDL; Terraform/setup scripts executed twice; multi-engine setups where Spark and Flink both bootstrap the same namespaces.","solutions":["Use CREATE NAMESPACE IF NOT EXISTS in SQL, or check namespaceExists before calling createNamespace.","Catch NamespaceAlreadyExistsException and treat it as success in idempotent setup code.","Serialize bootstrap jobs (locking/orchestration) to avoid concurrent-create races.","Remove the pre-existing namespace first if recreation is truly intended."],"exampleFix":"// before\ncatalog.createNamespace(new String[] {\"analytics\"}); // throws if exists\n\n// after\nString[] ns = {\"analytics\"};\nif (!catalog.namespaceExists(ns)) {\n  catalog.createNamespace(ns);\n}","handlingStrategy":"validation","validationCode":"String[] ns = {\"analytics\"};\nif (!catalog.namespaceExists(ns)) {\n  catalog.createNamespace(ns, metadata);\n}","typeGuard":null,"tryCatchPattern":"try {\n  catalog.createNamespace(ns, metadata);\n} catch (NamespaceAlreadyExistsException e) {\n  // idempotent bootstrap: treat as success\n}","preventionTips":["Use CREATE NAMESPACE IF NOT EXISTS in SQL","Make bootstrap scripts idempotent via existence checks or catch-and-continue","Serialize concurrent namespace-creation jobs","Verify preconditions after failed pipeline reruns"],"tags":["spark","catalog","namespace","already-exists"],"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"}