-
Notifications
You must be signed in to change notification settings - Fork 236
Have a better story for EFCore.PG and multihost #3495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This should already (somewhat) be working (ignoring exception for a sec) as you can call So all in all, just removing the check should be enough (I think). |
BTW, are you sure it's true? Looking at the code, it seems like currently we'll throw the exact same error (barring Edit: it's kinda even worse, we do not throw for Edit2: OK, I missed that we indeed make a wrapper, though removing that shouldn't be a problem. |
Submitted npgsql/npgsql#6046 |
I'm not sure - how would an EF user using ConfigureDataSource(), be able to use both prefer-standby (for read-only loads) and primary (for write loads), with the same underlying connection pools (i.e. same instance of NpgsqlMultiHostDataSource)? If you have two UseNpgsql() configurations in your application with ConfigureDataSource(), where the only difference between them is the value of the (this interaction of EF and Npgsql/data source is generally not simple, unfortunately) |
I imagine that less than 1% of developers actually need something like this, as it's mostly going to be "I want to only connect to primary". But even then, the main idea is for
Mm, you mean in case of legacy, yeah? That indeed makes sense, kinda forgot about that. |
Interesting, I'd think the majority just want Any, not Primary, which IIRC is also the default (in both libpq and Npgsql). That's already well-supported via EF too - just don't specify Though I guess you're right, and there's no really good reason to block
In legacy (i.e. no data source) we don't throw (create the wrapper internally) - maybe we're talking about different things? |
The problem with this is, most of the apps do write stuff into the database, and since vanilla postgres doesn't support multi-master... getting a standby will most likely just completely break the app. And speaking from experience, the app I'm working on doesn't really care whether there are multiple hosts or not, as long as we connect to primary. So allowing us just calling
We might mix things a bit. I was mostly talking about npgsql/npgsql#6046 where I did remove that wrapper (it's already back + I added a test just in case).
I'm not sure whether you're talking about legacy here or not. In case of |
Try to have
Target Session Attributes
in the connection string fails the moment EFCore.PG needs to create a data source internally (full repro below):This throws "When creating a multi-host data source, TargetSessionAttributes cannot be specified".
The Npgsql story for target sessions is to call BuildMultiHost() instead of Build(), and then to call an overload of OpenConnectionAsync() that passes TargetSessionAttributes, or to extract wrapper data sources via WithTargetSession() (docs).
EFCore.PG doesn't know whether the connection string contains multiple hosts and/or
Target Session Attribute
, and never calls BuildMultiHost(). It seems like the thing to do would be to add an EF-level context option for the target session, and when EF creates the NpgsqlConnection, to call OpenConnectionAsync(TargetSessionAttributes).Note that we also have legacy mode (without NpgsqlDataSource), where we do allow
Target Session Attribute
in the connection string; we internally create a wrapper for the given target session and use that. We could do the same when NpgsqlDataSourceBuilder.Build() is called with aTarget Session Attribute
; but that would mean that the underlying NpgsqlMultiHostDataSource (which owns the actual connection pools) isn't accessible, and it's impossible to have multipleTarget Session Attribute
values referencing the same physical connections. In legacy mode the NpgsqlMultiHostDataSource is global, so this isn't a problem.In the meantime, the workaround is simply to construct a multi-host data source outside EF, get a wrapper via WithTargetSession() and pass that to EFCore.PG's UseNpgsql().
@NinoFloris @vonzshik does this all make sense?
Originally raised by @fmendez89 in npgsql/doc#263 (comment)
Full repro
The text was updated successfully, but these errors were encountered: