Path: redb/redb.Postgres/Extensions/PostgresOptionsExtensions.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Postgres/Extensions/PostgresOptionsExtensions.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using redb.Core;
using redb.Core.Data;
using redb.Core.Extensions;
using redb.Core.Models.Configuration;
using redb.Core.Models.Contracts;
using redb.Core.Models.Security;
using redb.Core.Providers;
using redb.Core.Query;
using redb.Core.Serialization;
using redb.Core.Services;
using redb.Postgres.Data;
using redb.Postgres.Providers;
using redb.Postgres.Sql;
namespace redb.Postgres.Extensions;
///
/// PostgreSQL provider extensions for RedbOptionsBuilder.
///
public static class PostgresOptionsExtensions
{
///
/// Configures REDB to use PostgreSQL as the database provider.
/// For Pro version, use AddRedbPro() from redb.Postgres.Pro package.
///
/// The options builder.
/// PostgreSQL connection string. If null, must be set in Configure().
/// The options builder for chaining.
///
/// services.AddRedb(options => options
/// .UsePostgres("Host=localhost;Database=mydb;Username=user;Password=pass")
/// .Configure(c => c.EnablePropsCache = true));
///
public static RedbOptionsBuilder UsePostgres(
this RedbOptionsBuilder builder,
string? connectionString = null)
{
var infrastructure = (IRedbOptionsBuilderInfrastructure)builder;
if (!string.IsNullOrEmpty(connectionString))
infrastructure.Configuration.ConnectionString = connectionString;
infrastructure.SetProviderRegistration((services, config, isPro) =>
{
if (isPro)
throw new InvalidOperationException(
"Pro version requires redb.Postgres.Pro package. Use AddRedbPro() from redb.Postgres.Pro.Extensions.");
RegisterOpenSourceServices(services, config);
});
return builder;
}
///
/// Registers OpenSource PostgreSQL services.
/// Called by UsePostgres() and can be used by Pro version as fallback.
///
public static void RegisterOpenSourceServices(IServiceCollection services, RedbServiceConfiguration config)
{
// DataSource and Context
if (!string.IsNullOrEmpty(config.ConnectionString))
{
// Factory registration hands OWNERSHIP to the container: disposing the provider disposes
// the NpgsqlDataSource and closes its pool. The old instance-registration form left the
// pool open forever - every container restart (module hot-reload, tests) stacked another
// orphaned pool of idle sessions until the server-side pruner got them (2026-09-09).
services.AddSingleton(_ => Data.NpgsqlDataSourceFactory.Create(config.ConnectionString, config.StringCollation, config.EnableLazyReferences));
services.AddScoped(sp =>
new NpgsqlRedbContext(sp.GetRequiredService()) { BulkInsertTimeout = config.BulkInsertTimeout });
}
// Security
services.AddScoped(sp =>
AmbientSecurityContext.GetOrCreateDefault());
// SQL Dialect. Constructed from the configuration rather than registered by type, so the
// collation travels with the dialect instead of living in a process-wide static.
services.AddSingleton(_ => new PostgreSqlDialect(config.StringCollation));
// Core providers
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
// Query providers
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped(sp => sp.GetRequiredService().ListProvider); // one per scope: the service's own
services.AddScoped();
// Background deletion service (singleton + hosted service)
services.AddSingleton();
services.AddSingleton(sp => sp.GetRequiredService());
services.AddHostedService(sp => sp.GetRequiredService());
// Main service
services.AddScoped();
}
}