{"record":{"id":"d981307b718514ba","repo":"apache/iceberg","slug":"table-s-already-exists-in-the-database-s-and-cat","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.catalog.exceptions.AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"flink/v1.20/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/v1.20/flink/src/main/java/org/apache/iceberg/flink/FlinkDynamicTableFactory.java#L208-L244","documentation":"During CREATE TABLE handling, FlinkDynamicTableFactory.createTableLoader tries to create the Iceberg table if it does not exist. If the creation races with another writer (or the existence check is stale), Flink throws TableAlreadyExistException which is wrapped as AlreadyExistsException with this message. It means the requested iceberg table already exists in the database/catalog.","triggerScenarios":"Submitting a CREATE TABLE (or a source/sink DDL with implicit create) for a table that already exists in the Iceberg catalog; two concurrent jobs creating the same table; ignoreIfExists semantics colliding.","commonSituations":"Re-running the same Flink SQL statement twice; CI pipelines submitting DDL idempotently without IF NOT EXISTS; multiple executors racing during job startup.","solutions":["Check whether the table already exists (SHOW TABLES / tableExists) before creating, or make creation idempotent.","Catch AlreadyExistsException and treat it as success if your workflow expects the table to potentially exist.","Create the table once out-of-band (e.g. via Spark/iceberg DDL) and have Flink only read/write it."],"exampleFix":"// before\nflinkCatalog.createIcebergTable(objectPath, resolvedCatalogTable, true);\n// after\nif (!flinkCatalog.tableExists(objectPath)) {\n  try { flinkCatalog.createIcebergTable(objectPath, resolvedCatalogTable, true); }\n  catch (TableAlreadyExistException e) { /* treat as success */ }\n}","handlingStrategy":"try-catch","validationCode":"if (flinkCatalog.tableExists(new ObjectPath(databaseName, tableName))) {\n  LOG.info(\"Table {}.{} already exists; skipping create\", databaseName, tableName);\n}","typeGuard":"null","tryCatchPattern":"try {\n  submitCreateTable();\n} catch (AlreadyExistsException e) {\n  LOG.info(\"Table already exists; treating as success\", e);\n}","preventionTips":["Use idempotent DDL practices (check existence or catch already-exists).","Create Iceberg tables out-of-band for shared environments; let Flink only read/write.","Deduplicate DDL submissions in CI/CD pipelines."],"tags":["flink","catalog","table-creation"],"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-23T08:17:48.524Z"}