Skip to content

Dynamically updated MultiHostDataSource host list #5064

Description

@careless6666

Now we create data source like this

var dataSourceBuilder = new NpgsqlDataSourceBuilder(dbSettings.Value.ConnectionString);
dataSourceBuilder.BuildMultiHost();

It seems that this implementation implies a static connection string, but in our scenario we have our own ConfigurationProvider where we periodically update the list of hosts, for example, the database stopped coping with the load and additional replicas were added to it, it mean that we need to restart service. We need something like UsePeriodicPasswordProvider, ability update connection strings periodically

Is there any way to do what we want now?

Activity

  1. roji commented on May 17, 2023

    @roji
    Member

    By design, a data source can only have one connection string; since the connection string can significantly affect connection characteristics and connections are typically pooled, allowing the connection string to be changed would mean that connections with different characteristics are returned randomly... It would effectively make the data source abstraction meaningless.

    However, it may make sense to specifically allow hosts to be added/removed on the data source, a bit like how we allow the password to be set on it (not sure a callback like UsePeriodicPasswordProvider makes sense here). I'll keep this open to see what the other team members think.

    Until then, it shouldn't be too hard to simply create a new data source when your host list changes, transition to it and dispose the old one.

  2. careless6666 commented on May 17, 2023

    @careless6666
    Author

    Thanks for the quick response, will try the suggested solution

  3. added this to the Backlog milestone on May 17, 2023
  4. roji commented on May 22, 2023

    @roji
    Member

    Opening to consider supporting this better in Npgsql, in the backlog.

  5. reopened this on May 22, 2023
  6. benlongo commented on Jun 12, 2023

    @benlongo
  7. changed the title [-]MultiHostDataSource with dynamic connection string[/-] [+]Dynamically updated MultiHostDataSource host list[/+] on Mar 12, 2024
  8. jernejg commented on Aug 13, 2024

    @jernejg

    Until then, it shouldn't be too hard to simply create a new data source when your host list changes, transition to it and dispose the old one.

    It's not hard it's just that the code becomes icki as you need to start thinking about thread safety. I assume most of the apps initialise the data source with the hosts in the constructor of a component registered in the DI as a singleton.

    It would be great if this could be done under the hood by npgsql 🙇

  9. added theissue type on Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions