Skip to content

Comments

#13924: in Iceberg catalog, JdbcTableOperations should catch and handle PostgresSQL's exception for duplicated keys#15434

Open
sqd wants to merge 1 commit intoapache:mainfrom
sqd:oss_psql_exception
Open

#13924: in Iceberg catalog, JdbcTableOperations should catch and handle PostgresSQL's exception for duplicated keys#15434
sqd wants to merge 1 commit intoapache:mainfrom
sqd:oss_psql_exception

Conversation

@sqd
Copy link

@sqd sqd commented Feb 24, 2026

Fixes #13924

Previously reviewed at #13925 (fixed all the suggestions in there)

@github-actions github-actions bot added the core label Feb 24, 2026
// SQLite doesn't set SQLState or throw SQLIntegrityConstraintViolationException
if (e.getMessage() != null && e.getMessage().contains("constraint failed")) {
// Postgres doesn't throw SQLIntegrityConstraintViolationException but sets SQLState to
// "23505" (unique violation)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need a similar fix in ViewOperations?
JDBCCatalog has a similar check in renameTable and renameView

It may be best to pull this bit of logic out into a utility method and ditch the magic string.

  static boolean isConstraintViolation(SQLException e) {
    return e instanceof SQLIntegrityConstraintViolationException
        || POSTGRES_UNIQUE_VIOLATION_SQLSTATE.equals(e.getSQLState())
        || (e.getMessage() != null && e.getMessage().contains("constraint failed"));
  }

  static void handleCommitSqlException(
      SQLException e, String currentMetadataLocation, String entityType, Object identifier) {
    if (isConstraintViolation(e)) {
      if (currentMetadataLocation == null) {
        throw new AlreadyExistsException(e, "%s already exists: %s", entityType, identifier);
      } else {
        throw new UncheckedSQLException(e, "%s already exists: %s", entityType, identifier);
      }
    }

    throw new UncheckedSQLException(e, "Unknown failure");
  }

We also need a test of the new pathway, should be a pretty simple mock I think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I extrapolated that out and added a few tests.

@sqd sqd force-pushed the oss_psql_exception branch from 76e9dee to e7ab9c8 Compare February 24, 2026 18:14
@sqd sqd force-pushed the oss_psql_exception branch from e7ab9c8 to b6107de Compare February 24, 2026 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In Iceberg catalog, JdbcTableOperations should catch and handle PostgresSQL's exception for duplicated keys

2 participants