{"record":{"id":"ae18ae7dc00a97c0","repo":"apache/iceberg","slug":"table-s-already-exists-in-the-database-s-and-cat-ae18ae","errorCode":null,"errorMessage":"Table %s already exists in the database %s and catalog %s","messagePattern":"Table (.+?) already exists in the database (.+?) and catalog (.+?)","errorType":"exception","errorClass":"org.apache.flink.table.api.AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkDynamicTableFactory.java","lineNumber":226,"sourceCode":"    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);\n      }\n    }\n\n    return TableLoader.fromCatalog(\n        flinkCatalog.getCatalogLoader(), TableIdentifier.of(catalogDatabase, catalogTable));\n  }\n\n  /**\n   * Merges source catalog properties (catalog name, database, table) with connector properties.\n   * Source catalog name, database, table are serialized as json in FlinkCatalog#getTable to be able\n   * to isolate them from iceberg table props, Here, we flatten and merge them back.\n   *\n   * @param tableProps the existing table properties","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/FlinkDynamicTableFactory.java#L208-L244","documentation":"FlinkDynamicTableFactory attempts to create the Iceberg table in the external catalog if tableExists says it is absent; if createIcebergTable then throws TableAlreadyExistException (the table appeared concurrently or the check was stale), it rethrows as AlreadyExistsException. It reports that the requested table already exists in the given database and catalog.","triggerScenarios":"createTableLoader (via createDynamicTableSource/createDynamicTableSink) when tableExists(objectPath) is false but flinkCatalog.createIcebergTable(objectPath, resolvedCatalogTable, true) throws TableAlreadyExistException.","commonSituations":"Two Flink jobs submitting the same new table concurrently; schema-evolution statements resubmitted in a retry loop; a sink and a source job both creating the table on startup.","solutions":["Verify the existing table's schema matches what you intended; if so, just use it — retry the query.","Drop or rename the existing table if it was created with the wrong schema (DROP TABLE ... then resubmit).","Gate table creation in your deployment pipeline (create once, then start jobs) to avoid the race."],"exampleFix":"// before\nCREATE TABLE catalog.db.events (...); // two jobs racing\n// after\n// job A: create table once; job B just reads/writes\nCREATE TABLE IF NOT EXISTS catalog.db.events (...);","handlingStrategy":"try-catch","validationCode":"if (flinkCatalog.tableExists(objectPath)) { /* skip creation; verify schema instead */ }","typeGuard":null,"tryCatchPattern":"try {\n  flinkCatalog.createIcebergTable(objectPath, table, true);\n} catch (TableAlreadyExistException e) {\n  // table exists: compare schemas and proceed if compatible\n}","preventionTips":["Create tables once in a controlled pipeline step, not inside every job's startup.","Use CREATE TABLE IF NOT EXISTS semantics or ignoreIfExists-style flags.","On retry loops, check tableExists before re-submitting creation."],"tags":["flink","catalog","table","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"}