{"record":{"id":"80e353cc860705b1","repo":"apache/iceberg","slug":"cannot-create-namespace-s-already-exists","errorCode":null,"errorMessage":"Cannot create namespace %s: already exists","messagePattern":"Cannot create namespace (.+?): already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"aws/src/main/java/org/apache/iceberg/aws/dynamodb/DynamoDbCatalog.java","lineNumber":217,"sourceCode":"  }\n\n  @Override\n  public void createNamespace(Namespace namespace, Map<String, String> metadata) {\n    validateNamespace(namespace);\n    Map<String, AttributeValue> values = namespacePrimaryKey(namespace);\n    setNewCatalogEntryMetadata(values);\n    metadata.forEach(\n        (key, value) -> values.put(toPropertyCol(key), AttributeValue.builder().s(value).build()));\n\n    try {\n      dynamo.putItem(\n          PutItemRequest.builder()\n              .tableName(awsProperties.dynamoDbTableName())\n              .conditionExpression(\"attribute_not_exists(\" + DynamoDbCatalog.COL_VERSION + \")\")\n              .item(values)\n              .build());\n    } catch (ConditionalCheckFailedException e) {\n      throw new AlreadyExistsException(\"Cannot create namespace %s: already exists\", namespace);\n    }\n  }\n\n  @Override\n  public List<Namespace> listNamespaces(Namespace namespace) throws NoSuchNamespaceException {\n    validateNamespace(namespace);\n    List<Namespace> namespaces = Lists.newArrayList();\n    Map<String, AttributeValue> lastEvaluatedKey = null;\n    String condition = COL_IDENTIFIER + \" = :identifier\";\n    Map<String, AttributeValue> conditionValues = Maps.newHashMap();\n    conditionValues.put(\n        \":identifier\", AttributeValue.builder().s(COL_IDENTIFIER_NAMESPACE).build());\n    if (!namespace.isEmpty()) {\n      condition += \" AND \" + \"begins_with(\" + COL_NAMESPACE + \",:ns)\";\n      conditionValues.put(\":ns\", AttributeValue.builder().s(namespace.toString()).build());\n    }\n\n    do {","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/aws/src/main/java/org/apache/iceberg/aws/dynamodb/DynamoDbCatalog.java#L199-L235","documentation":"DynamoDbCatalog.createNamespace() writes a new namespace item with a condition expression `attribute_not_exists(...)`. If the item already exists, DynamoDB raises ConditionalCheckFailedException, which is translated into this AlreadyExistsException. This keeps namespace creation idempotent-safe and prevents silent overwrites.","triggerScenarios":"Calling catalog.createNamespace(namespace) when a row with the same namespace primary key already exists in the DynamoDB catalog table — including multi-level namespaces sharing the same partition key (e.g. creating `a.b` after `a.b.c` exists).","commonSituations":"Two concurrent jobs both creating the same namespace; rerunning an idempotent setup script without existence checks; nested-namespace collisions where a shorter namespace already occupies the key; stale catalog caches making callers believe the namespace is absent.","solutions":["Check existence first with catalog.namespaceExists(namespace) before calling createNamespace.","Catch org.apache.iceberg.exceptions.AlreadyExistsException and treat it as success in idempotent setup code.","Inspect the existing namespace item to confirm it is not a stale/conflicting entry before deleting it deliberately."],"exampleFix":"// before\ncatalog.createNamespace(Namespace.of(\"analytics\"));\n\n// after\nif (!catalog.namespaceExists(Namespace.of(\"analytics\"))) {\n  catalog.createNamespace(Namespace.of(\"analytics\"));\n}","handlingStrategy":"validation","validationCode":"if (catalog.namespaceExists(namespace)) {\n  return; // already created, skip\n}\ncatalog.createNamespace(namespace);","typeGuard":null,"tryCatchPattern":"try {\n  catalog.createNamespace(namespace);\n} catch (AlreadyExistsException e) {\n  // idempotent: treat as success\n}","preventionTips":["Make all createNamespace calls idempotent with an existence check or catch","Serialize namespace provisioning in one pipeline step to avoid concurrent creation races","Remember nested namespaces can collide with existing shorter-namespace rows"],"tags":["aws","dynamodb","catalog","already-exists","conflict"],"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"}