Skip to content

Tolerate concurrent table creation when initializing SqlCatalog - #4621

Open
faceWang753 wants to merge 1 commit into
apache:mainfrom
faceWang753:fix/sql-catalog-concurrent-init
Open

faceWang753 wants to merge 1 commit into
apache:mainfrom
faceWang753:fix/sql-catalog-concurrent-init

Conversation

@faceWang753

Copy link
Copy Markdown

Rationale for this change

When several processes open the same new SqlCatalog at 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:

  • SQLite: sqlite3.OperationalError: table iceberg_tables already exists
  • Postgres: psycopg2.errors.DuplicateTable: relation "iceberg_tables" already exists, or psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "pg_type_typname_nsp_index"

Java's JdbcCatalog already handles this in atomicCreateTable: it creates each catalog table separately and, if CREATE TABLE fails, checks whether the table was "created by another thread or process" before rethrowing. This PR does the same in SqlCatalog.create_tables(): it creates the tables one at a time (checkfirst=True), and when SQLAlchemy raises OperationalError, ProgrammingError or IntegrityError, 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_concurrently uses a before_create listener to create the tables through a second engine after the catalog has checked for them and before it issues CREATE TABLE, so the race happens every time. The test fails on main with sqlite3.OperationalError: table iceberg_tables already exists and passes with this change. make lint and the unit tests pass.

I also ran a script that opens a new catalog from 4 processes at the same moment:

Backend main this PR
SQLite file 13 of 30 rounds failed 0 of 30
Postgres 16 30 of 30 rounds failed 0 of 30

Are there any user-facing changes?

No API changes. Opening a new SqlCatalog from 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-by trailer, following the ASF guidance on generative tooling.

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 Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants