{"record":{"id":"a3790e53bfb49c1c","repo":"apache/iceberg","slug":"database-s-already-exists-in-the-iceberg-catalog","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/v1.20/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/v1.20/flink/src/main/java/org/apache/iceberg/flink/FlinkDynamicTableFactory.java#L195-L231","documentation":"When FlinkDynamicTableFactory.createTableLoader initializes an Iceberg table, it auto-creates the database in the external catalog if missing. If FlinkCatalog.createDatabase still reports DatabaseAlreadyExistException (a race or a misreported existence check), it is rethrown as Flink's AlreadyExistsException with this message. It signals a concurrent creation or a stale existence check.","triggerScenarios":"Two jobs/statements creating the same iceberg database concurrently so databaseExists returns false but createDatabase collides; flinkCatalog.databaseExists returning false inconsistently.","commonSituations":"Parallel Flink SQL client sessions issuing CREATE CATALOG/DDL against the same iceberg catalog at once; retry storms after a failed submission.","solutions":["Rerun the job — after the first creation succeeds, the existence check will pass and the error disappears.","Serialize database creation: create the database explicitly once (e.g. via catalog DDL or the Iceberg catalog API) before launching concurrent jobs.","Add retry-on-already-exists handling in your launcher so a race is treated as success."],"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  // ignore — database created concurrently\n}","handlingStrategy":"try-catch","validationCode":"if (!flinkCatalog.databaseExists(databaseName)) {\n  LOG.info(\"Creating iceberg database {}\", databaseName);\n}","typeGuard":"null","tryCatchPattern":"try {\n  createDatabasesAndTables();\n} catch (AlreadyExistsException e) {\n  LOG.warn(\"Database created concurrently; continuing\", e);\n}","preventionTips":["Create databases once in a serialized setup step before parallel job launches.","Make launchers idempotent: treat already-exists as success.","Avoid retry loops that hammer createDatabase concurrently."],"tags":["flink","catalog","concurrency"],"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"}