{"record":{"id":"4d26965a619c4ab7","repo":"apache/iceberg","slug":"view-already-exists-s-4d2696","errorCode":null,"errorMessage":"View already exists: %s","messagePattern":"View already exists: (.+?)","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/apache/iceberg/jdbc/JdbcViewOperations.java","lineNumber":125,"sourceCode":"        updateView(newMetadataLocation, oldMetadataLocation);\n      } else {\n        // view does not exist, create it\n        LOG.debug(\"Committing new view: {}\", viewName());\n        createView(newMetadataLocation);\n      }\n\n    } catch (SQLTimeoutException e) {\n      throw new UncheckedSQLException(e, \"Database Connection timeout\");\n    } catch (SQLTransientConnectionException | SQLNonTransientConnectionException e) {\n      throw new UncheckedSQLException(e, \"Database Connection failed\");\n    } catch (DataTruncation e) {\n      throw new UncheckedSQLException(e, \"Database data truncation error\");\n    } catch (SQLWarning e) {\n      throw new UncheckedSQLException(e, \"Database warning\");\n    } catch (SQLException e) {\n      if (JdbcUtil.isConstraintViolation(e)) {\n        if (currentMetadataLocation() == null) {\n          throw new AlreadyExistsException(e, \"View already exists: %s\", viewIdentifier);\n        } else {\n          throw new UncheckedSQLException(e, \"View already exists: %s\", viewIdentifier);\n        }\n      }\n\n      throw new UncheckedSQLException(e, \"Unknown failure\");\n    } catch (InterruptedException e) {\n      Thread.currentThread().interrupt();\n      throw new UncheckedInterruptedException(e, \"Interrupted during commit\");\n    }\n  }\n\n  @Override\n  protected String viewName() {\n    return viewIdentifier.toString();\n  }\n\n  @Override","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/core/src/main/java/org/apache/iceberg/jdbc/JdbcViewOperations.java#L107-L143","documentation":"AlreadyExistsException raised by JdbcViewOperations.doCommit when creating a NEW view (no current metadata location) hits a constraint violation in the catalog table — meaning a row for this view identifier already exists in iceberg_views. This is the create-vs-concurrent-create race detection: the INSERT's unique-key guard fired.","triggerScenarios":"CREATE VIEW on a name that already exists, or two sessions concurrently creating the same view — one insert succeeds, the other trips the primary-key/unique constraint and gets this exception.","commonSituations":"Race between two jobs both issuing CREATE VIEW IF NOT EXISTS-style logic, or a stale catalog client that didn't see the existing row during the pre-insert existence check.","solutions":["Check catalog.viewExists before creating, or handle AlreadyExistsException and reuse the existing view","Use CREATE OR REPLACE semantics where appropriate for your workflow","If the row is stale/corrupt (view was dropped but row remains), clean up the catalog row","Serialize view creation for the same identifier in your orchestration"],"exampleFix":"// before\ncatalog.createView(ident, schema, spec, sql); // AlreadyExistsException on race\n// after\nif (!catalog.viewExists(ident)) {\n  catalog.createView(ident, schema, spec, sql);\n} else {\n  View existing = catalog.loadView(ident); // reuse\n}","handlingStrategy":"try-catch","validationCode":"boolean exists = catalog.viewExists(ident);\nif (exists) {\n  throw new IllegalStateException(\"View already exists: \" + ident);\n}","typeGuard":null,"tryCatchPattern":"try {\n  catalog.createView(ident, schema, spec, sql);\n} catch (AlreadyExistsException e) {\n  // concurrent create won; load the existing view instead\n  View existing = catalog.loadView(ident);\n}","preventionTips":["Check viewExists before create and handle AlreadyExistsException gracefully","Serialize creation of same-named views in orchestration","Don't manually insert rows into iceberg_views"],"tags":["jdbc","view","already-exists","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"}