Skip to content

New Npgsql configuration story #5936

Open

Description

@roji

In ADO.NET, the classical way to specify database/connection configuration is via connection strings. Connection strings generally have the following drawbacks:

  1. Connection strings are an obsolete story in today's world of JSON configuration, Microsoft.Extensions.Options, etc. Specifically, it's quite odd to have a connection string within a JSON configuration - that's a structured string nested within a structured document. It makes more sense to simply represent the different options (host, port) directly in JSON, as is standard with everything else (e.g. ASP.NET).
  2. Connection strings can only be used to represent simple scalar data (string, int, bool...), and can't e.g. be used to provide lambdas (e.g. SSL callbacks).
  3. While connection strings make sense for environmental configuration (host, port) which needs be changed without recompiling the application, with the lack of other configuration mechanisms, various options slipped in that affect Npgsql's behavior (e.g. Enlist, Load Table Composites, etc. It's not possible to change such settings without also changing program code, and so these don't make sense as externally provided, configurable options.

Since the introduction of NpgsqlDataSource in Npgsql 7.0, it's now possible to have clean, programmatic configuration APIs on NpgsqlDataSourceBuilder; this solves problem 2 and 3, but users still need to specify environmental configuration via connection strings.

We want to explore some steps forward in the configuration story of Npgsql for 10. Directions include:

  • Have a single, publicly-exposed NpgsqlOptions - encapsulating everything that can be configured on a data source - and allow reading it e.g. from JSON via Microsoft.Extensions.Options (#5067).
    • NpgsqlDataSourceBuilder would become a way to configure and produce an NpgsqlOptions. It would be able to accept an NpgsqlOptions as input (e.g. an externally-built one from M.E.Options), and to produce one as output (although the usual output would still be NpgsqlDataSource directly).
    • We can also allow NpgsqlDataSource to be constructed directly from an NpgsqlOptions, without going through NpgsqlDataSourceBuilder.
    • Note that M.E.Options brings in M.E.DI.Abstractions
  • Consider allowing options to be modified at runtime (IOptionsMonitor). For example, this would allow switching over from one database to another without taking down the application for a configuration change.
    • Internally, this could mean that we separate the connection pool from the data source, and have the data source create a new pool when a configuration change is applied (disposing the old pool). We need to carefully consider what happens here: during the transition period the database could get overloaded as we go past Max Pool Size across the old and new pools.
    • We could get fancy here, and pre-seed the new pool with minimum connection (Min Pool Size?) before switching over, to prevent warmup delays.
    • Some options don't even require a new pool - stuff like Max Pool Size can be changed in an existing pool directly, as it doesn't affect physical connections.
    • This moves away from the idea that a data source is fully immutable; we should consider the consequences of that.
  • Possibly expose an immutable/snapshotted NpgsqlOptions on NpgsqlDataSource to allow code to query how the data source is configured.
  • Integrate well with all the other configuration sources: traditional connection strings, PostgreSQL URLs (#2090), environment variables (e.g. PGPORT) and hardcoded defaults (e.g. 5432). This could also included configuration-free operation, where nothing is specified either in a connection string or programmatically, relying only on PG-standard environment variables and defaults (see POC for allowing empty connection strings #4770, /cc @Brar).

/cc @edwardneal we discussed various aspects around this in dotnet/SqlClient#2870, where I was arguing against SqlClientOptions. I think it does make sense if one wants to go in the direction of M.E.Options.
/cc @ajcvickers

Activity

  1. added this to the 10.0.0 milestone on Nov 17, 2024
  2. edwardneal commented on Nov 17, 2024

    @edwardneal

    Thanks for the ping @roji.

    Being able to generate and configure NpgsqlDataSources (and perhaps NpgsqlConnections) from a strongly-typed object is a good idea IMO. We effectively decouple the data source from a hardcoded serialization format. I can see a situation where we wrap the connection string, a PostgreSQL URL or the various environment variables using IConfigurationSource implementations and bind an NpgsqlOptions instance to it.

    With NpgsqlDataSource being creatable from an NpgsqlOptions instance, an immutable way for clients to read those options makes sense. I see it in a similar vein to having a ConnectionString property on an NpgsqlConnection. There might also be a case for adding a similar property to NpgsqlConnection too - while there's no point if we know that an NpgsqlDataSource won't change post-instantiation, if IOptionsMonitor is implemented then I think it makes sense to let clients understand what options a specific NpgsqlConnection was instantiated with.

    I think you've summed up most of my impression of implementing IOptionsMonitor:

    This moves away from the idea that a data source is fully immutable; we should consider the consequences of that.

    The lifetime of an NpgsqlOptions instance becomes key. I'm not familiar with the internals of Npgsql, but I'd expect to encounter some difficult situations if an NpgsqlOptions setting which controls the behaviour of the client library changes midway through an async command. I tried to prevent that possibility with the SqlClientOptions design by having the SqlConnection clone the input options in the constructor.

    Stepping back from a single NpgsqlConnection to NpgsqlDataSource: if an NpgsqlDataSource's options can change between issuing two NpgsqlConnection instance, it seems sensible to consider the impact of transactions (and any other way that commands run on those two NpgsqlConnection instances could interact with one another.) What should the expected behaviour be if the Enlist property changes between one NpgsqlConnection and another being issued? If we want to handle this kind of situation, we might need to wait for any local transaction scopes to complete before applying the updated NpgsqlOptions to NpgsqlDataSource.

    I have some broader thoughts on connection pooling, but they're not strictly related to Npgsql configuration.


    Min & Max Pool Size also exist in Microsoft.Data.SqlClient, they exist in the older System.Data.SqlClient, and when OracleClient was part of .NET Framework they existed there too. The SqlClientX work for M.D.S is also working on an implementation which uses these options. Outside of the more ubiqitous libraries, in the past I've needed to connect to older iSeries systems via ODBC, and have had to throttle these connections because the source system could only accept a fixed number of connections. These options seem like they'd be fairly useful for most data access libraries which use connection pooling.

    It'd be useful to have a more generic DbConnectionPool<TPhysicalConnection> type in System.Data.Common which handles the creation of a pool of physical connections, with a common design for a minimum and maximum size, possibly with control over the growth rate. That growth rate will always be a tradeoff between the warmup time for a client connection, and what could effectively be a stampede on the database server. SqlClient's Min Pool Size logic is expected to mitigate those stampedes, but it does so by opening one connection at a time.

  3. added theissue type on Jun 14, 2025
  4. modified the milestones: 10.0.0, 11.0.0 on Oct 1, 2025
  5. modified the milestones: 11.0.0, 12.0.0 on Nov 20, 2026
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