duckdb_mysql icon indicating copy to clipboard operation
duckdb_mysql copied to clipboard

ATTACH mysql database does not support concurrency

Open robotslacker opened this issue 1 year ago • 4 comments

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!"

robotslacker avatar Sep 18 '24 13:09 robotslacker

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?

Mytherin avatar Sep 19 '24 06:09 Mytherin

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

robotslacker avatar Sep 19 '24 13:09 robotslacker

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.

Mytherin avatar Sep 19 '24 20:09 Mytherin

I believe this is the same problem (concurrent auto-load) as duckdb/duckdb#17324 and it was fixed some time ago.

The whole scenario with MySQL still has problems with concurrent ATTACH IF NOT EXISTS. I've filed duckdb/ducklake#561 for that (reproducer there does not require the external server).

staticlibs avatar Nov 14 '25 02:11 staticlibs