{"record":{"id":"281947fa6204770c","repo":"apache/iceberg","slug":"database-s-already-exists-in-the-iceberg-catalog-281947","errorCode":null,"errorMessage":"Database %s already exists in the iceberg catalog %s.","messagePattern":"Database (.+?) already exists in the iceberg catalog (.+?)\\.","errorType":"exception","errorClass":"org.apache.flink.table.catalog.exceptions.AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/FlinkDynamicTableFactory.java","lineNumber":213,"sourceCode":"\n    String catalogDatabase = flinkConf.get(FlinkCreateTableOptions.CATALOG_DATABASE, databaseName);\n    Preconditions.checkArgument(\n        catalogDatabase != null,\n        \"Invalid database name: null. Set %s create table option or specify fully qualified table name.\",\n        FlinkCreateTableOptions.CATALOG_DATABASE);\n\n    String catalogTable = flinkConf.get(FlinkCreateTableOptions.CATALOG_TABLE, tableName);\n\n    FlinkCatalog flinkCatalog = createCatalogLoader(mergedProps, catalogName);\n    ObjectPath objectPath = new ObjectPath(catalogDatabase, catalogTable);\n\n    // Create database if not exists in the external catalog.\n    if (!flinkCatalog.databaseExists(catalogDatabase)) {\n      try {\n        flinkCatalog.createDatabase(\n            catalogDatabase, new CatalogDatabaseImpl(Maps.newHashMap(), null), true);\n      } catch (DatabaseAlreadyExistException e) {\n        throw new AlreadyExistsException(\n            e,\n            \"Database %s already exists in the iceberg catalog %s.\",\n            catalogName,\n            catalogDatabase);\n      }\n    }\n\n    // Create table if not exists in the external catalog.\n    if (!flinkCatalog.tableExists(objectPath)) {\n      try {\n        flinkCatalog.createIcebergTable(objectPath, resolvedCatalogTable, true);\n      } catch (TableAlreadyExistException e) {\n        throw new AlreadyExistsException(\n            e,\n            \"Table %s already exists in the database %s and catalog %s\",\n            catalogTable,\n            catalogDatabase,\n            catalogName);","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/FlinkDynamicTableFactory.java#L195-L231","documentation":"During dynamic table creation, FlinkDynamicTableFactory.createTableLoader tries to create the database in the external catalog with ignoreIfNotExists semantics. If createDatabase nevertheless throws DatabaseAlreadyExistException (race with another job/client), it is converted to AlreadyExistsException with 'Database %s already exists in the iceberg catalog %s.'","triggerScenarios":"Two concurrent Flink jobs/queries both creating the same database via CREATE TABLE IF NOT EXISTS paths; a race where the database is created between the databaseExists check and createDatabase call.","commonSituations":"Parallel job deployments against a shared REST/Hive catalog; retrying a failed deployment while another client already created the database; sandbox namespaces shared across teams.","solutions":["Verify the database exists and reuse it instead of creating it","Serialize database creation (create once via a setup job or catalog CLI) before submitting jobs","Catch AlreadyExistsException and treat it as success if the database is the one you intended","Use distinct database names per environment/team to avoid collisions"],"exampleFix":"// before\nflinkCatalog.createDatabase(db, new CatalogDatabaseImpl(new HashMap<>(), null), true);\n\n// after\nif (!flinkCatalog.databaseExists(db)) {\n  try {\n    flinkCatalog.createDatabase(db, new CatalogDatabaseImpl(new HashMap<>(), null), true);\n  } catch (DatabaseAlreadyExistException e) {\n    // another client created it concurrently; safe to proceed\n  }\n}","handlingStrategy":"try-catch","validationCode":"if (!catalog.databaseExists(db)) { /* create */ }","typeGuard":null,"tryCatchPattern":"try { catalog.createDatabase(db, new CatalogDatabaseImpl(new HashMap<>(), null), true); } catch (DatabaseAlreadyExistException | AlreadyExistsException e) { /* idempotent success */ }","preventionTips":["Create databases upfront in a single setup step","Use ignoreIfExists semantics and treat AlreadyExists as success","Avoid multiple jobs creating the same database concurrently"],"tags":["flink","catalog","database-exists","race-condition"],"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"}