duckdb / duckdb_mysql

https://duckdb.org/docs/extensions/mysql
MIT License
55 stars 13 forks source link

ATTACH mysql database does not support concurrency #89

Open robotslacker opened 2 months ago

robotslacker commented 2 months ago

As we know, mysql extension and PG extension are both working on session layer, which means that if there are multiple sessions and each session needs to access external data, then each session must perform it's own attach operation. If we perform attach concurrently, we may encounter errors:

Invalid Input Error: Initialization function "mysql_scanner_init" from file "extensions/v1.1.0/linux_amd64/gcc4/mysql_scanner.duckdb_extension" threw an exception: “Table Function with name "mysql_clear_cache" already exists!"

Mytherin commented 2 months ago

Thanks for the report!

As we know, mysql extension and PG extension are both working on session layer, which means that if there are multiple sessions and each session needs to access external data, then each session must perform it's own attach operation.

This is not accurate, attached databases are shared across connections to the same DuckDB instance.

If we perform attach concurrently, we may encounter errors:

That seems like the extension is being loaded twice. Are you attaching concurrently from separate threads? Which API are you using? Could you provide a complete reproducible example?

robotslacker commented 2 months ago

"Are you attaching concurrently from separate threads?" Yes. I tried to attach the schema with "ATTACH IF NOT EXIST '' AS db ..." from separate thread. Each thread has a separate connection, duplicate() from master connection. I executed this sql with jdbc statement.execute().

I will try upload complete reproducible example later, it is not very easy. Because this issue is intermittent.

Mytherin commented 2 months ago

It sounds like this is caused by loading the extension concurrently in separate threads. You can solve this by calling LOAD mysql once right after connecting to the system for the first time.