{"record":{"id":"60862fd14cc2b061","repo":"apache/iceberg","slug":"database-s-already-exists-in-the-iceberg-catalog-60862f","errorCode":null,"errorMessage":"Database %s already exists in the iceberg catalog %s.","messagePattern":"Database (.+?) already exists in the iceberg catalog (.+?)\\.","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"flink/v2.2/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.2/flink/src/main/java/org/apache/iceberg/flink/FlinkDynamicTableFactory.java#L195-L231","documentation":"When creating a dynamic table source/sink, FlinkDynamicTableFactory auto-creates the database in the Iceberg catalog with ignoreIfExists=true; if createDatabase still reports DatabaseAlreadyExistException, it is rethrown as AlreadyExistsException with this message — indicating a race or inconsistent existence check.","triggerScenarios":"createTableLoader runs, databaseExists returns false, then createDatabase races with another job/connection that creates the same database concurrently.","commonSituations":"Multiple Flink jobs starting simultaneously against the same Iceberg catalog, creating the same database namespace concurrently.","solutions":["Retry the operation; the database now exists and the second attempt will succeed","Serialize database/table bootstrap steps in deployment (one job creates schema first)","Treat AlreadyExistsException as benign and proceed with loading the table loader"],"exampleFix":"// before\nflinkCatalog.createDatabase(catalogDatabase, new CatalogDatabaseImpl(Maps.newHashMap(), null), true);\n// after\ntry {\n  flinkCatalog.createDatabase(catalogDatabase, new CatalogDatabaseImpl(Maps.newHashMap(), null), true);\n} catch (AlreadyExistsException e) {\n  // concurrent creation; database exists, continue\n}","handlingStrategy":"retry","validationCode":"if (catalog.databaseExists(dbName)) { /* skip creation */ }","typeGuard":null,"tryCatchPattern":"try { loader.open(); } catch (AlreadyExistsException e) { /* concurrent creation; retry or proceed */ }","preventionTips":["Create databases/schemas before launching parallel Flink jobs","Use ignoreIfExists semantics and treat duplicates as benign"],"tags":["flink","already-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"}