CoreEx.Database.Postgres 4.0.0-preview-1

This is a prerelease version of CoreEx.Database.Postgres.
dotnet add package CoreEx.Database.Postgres --version 4.0.0-preview-1
                    
NuGet\Install-Package CoreEx.Database.Postgres -Version 4.0.0-preview-1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CoreEx.Database.Postgres" Version="4.0.0-preview-1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CoreEx.Database.Postgres" Version="4.0.0-preview-1" />
                    
Directory.Packages.props
<PackageReference Include="CoreEx.Database.Postgres" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CoreEx.Database.Postgres --version 4.0.0-preview-1
                    
#r "nuget: CoreEx.Database.Postgres, 4.0.0-preview-1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CoreEx.Database.Postgres@4.0.0-preview-1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CoreEx.Database.Postgres&version=4.0.0-preview-1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=CoreEx.Database.Postgres&version=4.0.0-preview-1&prerelease
                    
Install as a Cake Tool

CoreEx.Database.Postgres

Provides the PostgreSQL (Npgsql) implementation of IDatabase: PostgresDatabase, PostgresUnitOfWork, outbox relay, Postgres-specific parameter extensions, and OpenTelemetry metrics for outbox operations.

Overview

CoreEx.Database.Postgres is the PostgreSQL provider package built on top of CoreEx.Database. It supplies concrete implementations of every abstract type in that package — PostgresDatabase, PostgresCommand, PostgresDatabaseArgs, PostgresDatabaseColumns — and adds PostgreSQL-specific capabilities: structured error-code mapping from PostgreSQL SqlState strings to CoreEx semantic exceptions, and PostgresUnitOfWork for transactional outbox support.

The error-code mapping convention mirrors the SQL Server convention: stored procedures and functions raise well-known SQLSTATE codes (56001–56010) to signal domain exceptions without any application-layer switch statements. Unique-violation errors (23505 by default) are automatically converted to DuplicateException.

The outbox sub-namespace provides ready-to-use PostgresOutboxPublisher, PostgresOutboxRelay, and PostgresOutboxRelayHostedService with .NET Meter-backed counters and histograms for enqueue throughput and relay lag.

Key capabilities

  • 🗃️ PostgresDatabase: Concrete IDatabase for NpgsqlConnection (constructed from NpgsqlDataSource); maps PostgreSQL SqlState strings to CoreEx semantic exceptions; configurable DuplicateErrorNumbers; RowVersion encoded via EncodedStringToUInt32Converter.
  • 🔢 Error-code convention: SQLSTATE values 56001–56007 and 56010 map to ValidationException, BusinessException, AuthorizationException, ConcurrencyException, NotFoundException, ConflictException, DuplicateException, and DataConsistencyException respectively — identical to the SQL Server convention.
  • 🔁 PostgresUnitOfWork: IDatabaseUnitOfWork implementation wrapping TransactionAsync with PostgresUnitOfWorkInvoker; optionally accepts an IEventPublisher outbox for transactional event enqueuing.
  • 📤 Outbox relay: PostgresOutboxPublisher (writes to outbox table), PostgresOutboxRelay (polls and publishes), and PostgresOutboxRelayHostedService (timer-driven hosted service) — all PostgreSQL-specific subclasses of the base CoreEx.Database.Outbox types.
  • 📊 Outbox metrics: PostgresMetrics exposes .NET Meter instruments: postgres.outbox.enqueue (counter), postgres.outbox.relay.batch.size (counter), postgres.outbox.batch.oldest_lag and postgres.outbox.batch.newest_lag (histograms in ms).
  • 📡 OpenTelemetry: CoreExPostgresExtensions.AddCoreExPostgresOpenTelemetry wires PostgresInvoker activity sources and the outbox meter into the OTEL tracer and meter providers.
  • ⚙️ DI registration: AddPostgresDatabase<TDatabase>(services, configure?) registers PostgresDatabase as a scoped service; AddPostgresUnitOfWork(services, configure?) registers PostgresUnitOfWork.

Key types

Type Description
PostgresDatabase Concrete IDatabase for NpgsqlConnection; SqlState string to exception mapping; RowVersionEncodedStringToUInt32Converter; accepts NpgsqlDataSource for connection-pool management.
PostgresUnitOfWork IDatabaseUnitOfWork implementation; wraps TransactionAsync with PostgresUnitOfWorkInvoker; optional transactional outbox via IEventPublisher Outbox.
PostgresCommand DatabaseCommand subclass for NpgsqlCommand; adds Postgres-specific parameter handling.
PostgresDatabaseArgs DatabaseArgs subclass with PostgreSQL-specific defaults.
PostgresDatabaseColumns DatabaseColumns subclass with PostgreSQL-specific convention column name defaults (e.g. snake_case names).
PostgresMetrics Static .NET Meter with counters and histograms for outbox enqueue throughput and relay lag.
PostgresOutboxPublisher DatabaseOutboxPublisherBase for PostgreSQL; inserts event rows into the outbox table via a configurable function/procedure.
PostgresOutboxRelay DatabaseOutboxRelayBase<PostgresDatabase, PostgresOutboxRelay> with PostgreSQL-specific dequeue/complete SQL; records outbox metrics per batch.
PostgresOutboxRelayHostedService DatabaseOutboxRelayHostedServiceBase<PostgresOutboxRelay> timer-driven hosted service for the PostgreSQL outbox relay.
PostgresExtensions IDatabase / DatabaseCommand extensions specific to PostgreSQL: Param helpers for NpgsqlDbType-typed parameters.
PostgresInvoker DatabaseInvoker subclass for PostgreSQL; the Default singleton used by PostgresDatabase.
PostgresUnitOfWorkInvoker InvokerBase<PostgresUnitOfWork> wrapping TransactionAsync with OpenTelemetry spans and begin/commit/rollback log entries.
  • CoreEx.Database - Abstract base types (Database<TConn>, DatabaseCommand, DatabaseRecord, DatabaseOutboxRelayBase, IDatabaseMapper) that all PostgreSQL types extend.
  • CoreEx.Data - IUnitOfWork and IDatabaseUnitOfWork interfaces implemented by PostgresUnitOfWork.
  • CoreEx.Events - IEventPublisher is the relay's publication target; PostgresOutboxPublisher implements IEventPublisher.
  • CoreEx.Invokers - PostgresInvoker and PostgresUnitOfWorkInvoker extend InvokerBase for OpenTelemetry tracing.

Additional Resources

  • Npgsql - The PostgreSQL ADO.NET driver this package uses.
  • PostgreSQL Error Codes - Reference for SqlState values including the CoreEx convention codes.

AI Usage Guide

An AGENTS.md file is included with this package. AI coding assistants (GitHub Copilot, Claude, Cursor, etc.) that support workspace-injected package documentation will automatically surface concise usage guidance, code examples, and Do Not rules for this package without requiring a local CoreEx checkout.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CoreEx.Database.Postgres:

Package Downloads
CoreEx.UnitTesting

Core .NET extensions and abstractions for the testing of backend services.

Beef.Database.Postgres

Business Entity Execution Framework (Beef) PostgreSQL Database tool.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on CoreEx.Database.Postgres:

Repository Stars
Avanade/Beef
The Business Entity Execution Framework (Beef) framework, and the underlying code generation, has been primarily created to support the industrialization of API development.
Version Downloads Last Updated
4.0.0-preview-1 43 6/20/2026
3.31.0 963 2/1/2025
3.30.2 214 12/11/2024
3.30.1 174 12/9/2024
3.30.0 847 11/21/2024
3.29.0 344 11/19/2024
3.28.0 225 11/9/2024
3.27.3 235 10/23/2024
3.27.2 206 10/17/2024
3.27.1 259 10/15/2024
3.27.0 338 10/11/2024
3.26.0 218 10/3/2024
3.25.6 367 10/2/2024
3.25.5 258 9/25/2024
3.25.4 238 9/24/2024
3.25.3 226 9/18/2024
3.25.2 242 9/17/2024
3.25.1 285 9/16/2024
3.25.0 214 9/10/2024
3.24.1 296 8/7/2024
Loading failed