{"record":{"id":"836a94605efc1275","repo":"apache/iceberg","slug":"cannot-rename-table-s-to-s-s-already-exists","errorCode":null,"errorMessage":"Cannot rename table %s to %s: %s already exists","messagePattern":"Cannot rename table (.+?) to (.+?): (.+?) already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":409,"severity":"error","filePath":"aws/src/main/java/org/apache/iceberg/aws/dynamodb/DynamoDbCatalog.java","lineNumber":454,"sourceCode":"                .consistentRead(true)\n                .key(fromKey)\n                .build());\n\n    if (!fromResponse.hasItem()) {\n      throw new NoSuchTableException(\n          \"Cannot rename table %s to %s: %s does not exist\", from, to, from);\n    }\n\n    GetItemResponse toResponse =\n        dynamo.getItem(\n            GetItemRequest.builder()\n                .tableName(awsProperties.dynamoDbTableName())\n                .consistentRead(true)\n                .key(toKey)\n                .build());\n\n    if (toResponse.hasItem()) {\n      throw new AlreadyExistsException(\n          \"Cannot rename table %s to %s: %s already exists\", from, to, to);\n    }\n\n    fromResponse.item().entrySet().stream()\n        .filter(e -> isProperty(e.getKey()))\n        .forEach(e -> toKey.put(e.getKey(), e.getValue()));\n\n    setNewCatalogEntryMetadata(toKey);\n\n    dynamo.transactWriteItems(\n        TransactWriteItemsRequest.builder()\n            .transactItems(\n                TransactWriteItem.builder()\n                    .delete(\n                        Delete.builder()\n                            .tableName(awsProperties.dynamoDbTableName())\n                            .key(fromKey)\n                            .conditionExpression(COL_VERSION + \" = :v\")","sourceCodeStart":436,"sourceCodeEnd":472,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/aws/src/main/java/org/apache/iceberg/aws/dynamodb/DynamoDbCatalog.java#L436-L472","documentation":"DynamoDbCatalog.renameTable() checks that the destination table does not exist before writing the new item; if the GetItem for the `to` key finds an item it throws AlreadyExistsException with this message. This prevents renaming a table onto an existing one and silently overwriting its metadata.","triggerScenarios":"Calling catalog.renameTable(from, to) when an item for the `to` identifier already exists in the DynamoDB catalog table.","commonSituations":"Retrying a failed rename that actually completed (destination written before source delete); two concurrent renames to the same target name; re-running a migration script that already renamed the table.","solutions":["Check catalog.tableExists(to) first; if it exists, decide whether to drop it, pick a different name, or treat the rename as done.","Make retry logic idempotent: on AlreadyExistsException verify the destination's metadata location matches the source's and skip if the rename already completed.","Remove or rename the existing destination table if it is confirmed stale."],"exampleFix":"// before\ncatalog.renameTable(from, to);\n\n// after\nif (!catalog.tableExists(to)) {\n  catalog.renameTable(from, to);\n}","handlingStrategy":"validation","validationCode":"if (catalog.tableExists(to)) {\n  throw new IllegalStateException(\"Destination \" + to + \" already exists; choose another name or drop it\");\n}\ncatalog.renameTable(from, to);","typeGuard":null,"tryCatchPattern":"try {\n  catalog.renameTable(from, to);\n} catch (AlreadyExistsException e) {\n  // possibly a completed retry; verify destination metadata before rethrowing\n  if (!catalog.tableExists(from)) return; // rename already finished\n  throw e;\n}","preventionTips":["Pre-check the destination name before renaming","Design rename retry logic to detect completed renames (source gone + destination present)","Reserve/generate unique destination names in automated pipelines"],"tags":["aws","dynamodb","catalog","rename","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"}