faceWang753 opened a new pull request, #4621:
URL: https://github.com/apache/iceberg-python/pull/4621

   # 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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to