Repository navigation
Tolerate concurrent table creation when initializing SqlCatalog - #4621
Open
faceWang753 wants to merge 1 commit into
Open
faceWang753 wants to merge 1 commit into
faceWang753 wants to merge 1 commit into
Conversation
Several processes opening a new SqlCatalog at once can all find the catalog tables missing and try to create them. The ones that lose the race failed with "table iceberg_tables already exists" (SQLite) or DuplicateTable / a pg_type unique violation (Postgres). Create the tables one at a time and, when CREATE TABLE fails, accept the table if it exists by then, as Java's JdbcCatalog.atomicCreateTable does. Generated-by: Claude Opus 5.5
Fokko
reviewed
Dec 4, 2026
Fokko
left a comment
Contributor
There was a problem hiding this comment.
Nice one @faceWang753! Verified that this matches Java's JdbcCatalog.atomicCreateTable, which also tolerates the table appearing between the check and the CREATE TABLE, and that the new test indeed fails on main. One nit: the test only exercises the SQLite OperationalError path, so the ProgrammingError (Postgres DuplicateTable) and IntegrityError branches stay untested. Any way to cover those too? Otherwise LGTM 🚀
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
When several processes open the same new
SqlCatalogat once (for example two services starting together against one SQLite file, or several workers against one Postgres database), each of them can find the catalog tables missing and try to create them. The processes that lose the race fail:sqlite3.OperationalError: table iceberg_tables already existspsycopg2.errors.DuplicateTable: relation "iceberg_tables" already exists, orpsycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "pg_type_typname_nsp_index"Java's
JdbcCatalogalready handles this inatomicCreateTable: it creates each catalog table separately and, ifCREATE TABLEfails, checks whether the table was "created by another thread or process" before rethrowing. This PR does the same inSqlCatalog.create_tables(): it creates the tables one at a time (checkfirst=True), and when SQLAlchemy raisesOperationalError,ProgrammingErrororIntegrityError, it re-raises only if the table still does not exist. Other errors, such as missing privileges, are raised as before.Are these changes tested?
Yes.
test_creation_when_tables_are_created_concurrentlyuses abefore_createlistener to create the tables through a second engine after the catalog has checked for them and before it issuesCREATE TABLE, so the race happens every time. The test fails onmainwithsqlite3.OperationalError: table iceberg_tables already existsand passes with this change.make lintand the unit tests pass.I also ran a script that opens a new catalog from 4 processes at the same moment:
mainAre there any user-facing changes?
No API changes. Opening a new
SqlCatalogfrom several processes at once no longer fails.I used an AI assistant (Claude) to help write the change and the test; I reviewed and ran everything myself. The commit carries a
Generated-bytrailer, following the ASF guidance on generative tooling.