{"record":{"id":"d03f9f37d43b493d","repo":"prestodb/presto","slug":"already-exists-d03f9f","errorCode":"ALREADY_EXISTS","errorMessage":"Branch %s already exists in table %s","messagePattern":"Branch (.+?) already exists in table (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-iceberg/src/main/java/com/facebook/presto/iceberg/IcebergAbstractMetadata.java","lineNumber":1189,"sourceCode":"            return;\n        }\n        Optional<Long> targetSnapshotId = tableVersion\n                .map(version -> getSnapshotIdForTableVersion(icebergTable, version))\n                .or(() -> Optional.ofNullable(icebergTable.currentSnapshot()).map(Snapshot::snapshotId));\n        ManageSnapshots manageSnapshots = icebergTable.manageSnapshots();\n        if (replace && branchExists) {\n            targetSnapshotId.ifPresent(snapshotId -> manageSnapshots.replaceBranch(branchName, snapshotId));\n        }\n        else if (!branchExists) {\n            if (targetSnapshotId.isPresent()) {\n                manageSnapshots.createBranch(branchName, targetSnapshotId.get());\n            }\n            else {\n                manageSnapshots.createBranch(branchName);\n            }\n        }\n        else {\n            throw new PrestoException(ALREADY_EXISTS, format(\"Branch %s already exists in table %s\", branchName, icebergTableHandle.getSchemaTableName().getTableName()));\n        }\n        // Apply retention policies if specified\n        retainDays.ifPresent(retainDs -> manageSnapshots.setMaxRefAgeMs(branchName, ofDays(retainDs).toMillis()));\n        minSnapshotsToKeep.ifPresent(minSnapshots -> manageSnapshots.setMinSnapshotsToKeep(branchName, minSnapshots));\n        maxSnapshotAgeDays.ifPresent(maxAgeDays -> manageSnapshots.setMaxSnapshotAgeMs(branchName, ofDays(maxAgeDays).toMillis()));\n        manageSnapshots.commit();\n    }\n\n    @Override\n    public void createTag(\n            ConnectorSession session,\n            ConnectorTableHandle tableHandle,\n            String tagName,\n            boolean replace,\n            boolean ifNotExists,\n            Optional<ConnectorTableVersion> tableVersion,\n            Optional<Long> retainDays)\n    {","sourceCodeStart":1171,"sourceCodeEnd":1207,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-iceberg/src/main/java/com/facebook/presto/iceberg/IcebergAbstractMetadata.java#L1171-L1207","documentation":"Thrown when creating a branch that already exists as a reference on the Iceberg table and replace semantics were not requested. Iceberg refuses to create a duplicate ref, so the connector fails with ALREADY_EXISTS.","triggerScenarios":"ALTER TABLE ... CREATE BRANCH <name> where a branch with that name is already present in icebergTable.refs() (without REPLACE).","commonSituations":"Re-running an idempotency-scripted CREATE BRANCH without IF NOT EXISTS; naming collision with an existing tag or branch created by CI pipelines; concurrent creation by two sessions.","solutions":["Use CREATE BRANCH IF NOT EXISTS (or the replace option) so an existing branch doesn't fail the statement.","Pick a new unique branch name.","If the intent is to re-point the branch, use the replace/REPLACE form instead of plain create."],"exampleFix":"// before\nALTER TABLE t CREATE BRANCH etl; -- already exists\n// after\nALTER TABLE t CREATE BRANCH IF NOT EXISTS etl;","handlingStrategy":"validation","validationCode":"-- check branch existence before creating\nSELECT * FROM \"t$refs\" WHERE name = 'etl' AND type = 'BRANCH';","typeGuard":null,"tryCatchPattern":"try { createBranch(...); }\ncatch (PrestoException e) { if (ALREADY_EXISTS.equals(e.getErrorCode())) { /* reuse, replace, or rename */ } }","preventionTips":["Use CREATE BRANCH IF NOT EXISTS for idempotent pipelines","Generate unique branch names per run (date/uuid suffix)","Centralize branch lifecycle in one job to avoid races"],"tags":["iceberg","branch","already-exists","ddl"],"backgroundTag":"iceberg-branch-already-exists","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}