X Tutup
Skip to content

Allow specifying TargetSessionAttributes in connection string with NpgsqlDataSourceBuilder#6046

Merged
vonzshik merged 1 commit intomainfrom
support-target-session-attributes-in-connection-string
Oct 5, 2025
Merged

Allow specifying TargetSessionAttributes in connection string with NpgsqlDataSourceBuilder#6046
vonzshik merged 1 commit intomainfrom
support-target-session-attributes-in-connection-string

Conversation

@vonzshik
Copy link
Copy Markdown
Contributor

No description provided.

@NinoFloris
Copy link
Copy Markdown
Member

A downside of this would be that we can't trim out the multi-host portion anymore?

@vonzshik
Copy link
Copy Markdown
Contributor Author

A downside of this would be that we can't trim out the multi-host portion anymore?

I'm pretty sure nothing changed in that regard since you already can specify multiple hosts (and as a result, get a multi-host data source), it's just now you'll be able to control the way that data source will work.

@vonzshik vonzshik force-pushed the support-target-session-attributes-in-connection-string branch from c4e0ebd to 76651d0 Compare August 20, 2025 09:44
@vonzshik
Copy link
Copy Markdown
Contributor Author

Yeah, there is 0 difference in binary size with the main, so we already drag multiple hosts.

@vonzshik vonzshik force-pushed the support-target-session-attributes-in-connection-string branch from 76651d0 to 93d67e6 Compare September 10, 2025 11:42
@roji
Copy link
Copy Markdown
Member

roji commented Oct 5, 2025

@vonzshik am trying to remember the exact context behind us doing this. I looked at #3495, and the EF story around multihost could indeed be improved, but I'm not sure exactly how allowing TargetSessionAttribute in the connection string helps... As I wrote in #3495, while it would make it possible to use NpgsqlDataSourceSource.Build() while passing TargetSessionAttribute, you'd end up with a single data source that has a single TargetSessionAttribute, and have no way to use the same physical connections with a different TargetSessionAttribute (e.g. a classical read-only and read-write setup).

EF users can still create their own multihost data source and get read-only/read-write wrappers (just like non-EF users), and then pass those to EF - it's not the prettiest, but it works. Otherwise someone who doesn't care about TargetSessionAttributes at all can also use multihost for failover/load balancing - they don't need to specify TargetSessionAttributes at all.

So what would be the user scenario where this is needed?

A downside of this would be that we can't trim out the multi-host portion anymore?

I'm pretty sure nothing changed in that regard since you already can specify multiple hosts (and as a result, get a multi-host data source), it's just now you'll be able to control the way that data source will work.

I think @NinoFloris was saying that if we do this, then we wouldn't ever be able to trim out multihost; I understand that we don't currently trim it out either (as you posted later), but in theory we could. On the other hand, IIRC we already accept multiple hosts in the connection string anyway, and that already triggers multihost, so also adding TargetSessionAttributes doesn't seem to make the current situation any worse. For example, we could say that the slim data source builder simply doesn't support multi-host at all except if you call BuildMultihost() (if you just call Build() we throw if multiple hosts and/or TargetSessionAttributes are specified).

@vonzshik
Copy link
Copy Markdown
Contributor Author

vonzshik commented Oct 5, 2025

EF users can still create their own multihost data source and get read-only/read-write wrappers (just like non-EF users), and then pass those to EF - it's not the prettiest, but it works. Otherwise someone who doesn't care about TargetSessionAttributes at all can also use multihost for failover/load balancing - they don't need to specify TargetSessionAttributes at all.

IMO for most EF consumers they shouldn't even make wrappers in the first place. Say, you have the standard primary-standby failover in place. So, to make it work with Npgsql ideally all you have to do is specify both hosts and then set TargetSessionAttributes to primary (because the default value is any). But currently you can't do that, you have to make a wrapper, and the worst thing, you can't change the behavior of the app from outside. Like, what if I have a read-only app, so by default I want to only read from a replica. But in some installations I might want to allow to read from primary, but then that would require changes in the code.

@roji
Copy link
Copy Markdown
Member

roji commented Oct 5, 2025

Thanks, makes sense (it has been a while since I've looked at all this).

@vonzshik vonzshik enabled auto-merge (squash) October 5, 2025 17:55
@vonzshik vonzshik merged commit 6e308c3 into main Oct 5, 2025
16 checks passed
@vonzshik vonzshik deleted the support-target-session-attributes-in-connection-string branch October 5, 2025 17:57
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.

3 participants

X Tutup