{"record":{"id":"611487393c252d78","repo":"apache/iceberg","slug":"namespace-already-exists-namespace","errorCode":null,"errorMessage":"Namespace already exists: ${namespace}","messagePattern":"Namespace already exists: (.+?)","errorType":"exception","errorClass":"NamespaceAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java","lineNumber":523,"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":505,"sourceCodeEnd":541,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/SparkCatalog.java#L505-L541","documentation":"Thrown by SparkCatalog.createNamespace when the underlying Iceberg catalog raises AlreadyExistsException because a namespace with the same name already exists; Spark translates this into NamespaceAlreadyExistsException. Creation is refused so existing namespace metadata is not overwritten.","triggerScenarios":"Calling CREATE NAMESPACE <ns> or createNamespace(ns, metadata) for a name that already exists in the catalog, including re-running idempotent-looking setup scripts and races where two jobs create the same namespace concurrently.","commonSituations":"Idempotent bootstrap scripts without IF NOT EXISTS, re-provisioning after partial failure, multi-tenant setups where another team already created the namespace.","solutions":["Use 'CREATE NAMESPACE IF NOT EXISTS <ns>' in SQL","Check namespaceExists(ns) before creating","Catch NamespaceAlreadyExistsException and treat it as success in idempotent scripts","In Java, call dropNamespace first only when the existing namespace can be safely replaced"],"exampleFix":"// before\nspark.sql(\"CREATE NAMESPACE prod.analytics\")\n// after\nspark.sql(\"CREATE NAMESPACE IF NOT EXISTS prod.analytics\")","handlingStrategy":"try-catch","validationCode":"if (nsc.namespaceExists(namespace)) {\n  log.info(\"Namespace {} already exists; skipping creation\", String.join(\".\", namespace));\n  return;\n}","typeGuard":"boolean needsCreation(SupportsNamespaces catalog, String[] ns) {\n  return !catalog.namespaceExists(ns);\n}","tryCatchPattern":"try {\n  nsc.createNamespace(ns, metadata);\n} catch (NamespaceAlreadyExistsException e) {\n  log.info(\"Namespace {} already exists; treating as success\", String.join(\".\", ns));\n}","preventionTips":["Use CREATE NAMESPACE IF NOT EXISTS in SQL scripts","Make bootstrap scripts idempotent by catching NamespaceAlreadyExistsException","Check namespaceExists before create","Coordinate namespace creation across concurrent jobs (single provisioner)"],"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"}