Skip to main content
net10.0net8.0net9.0

PostgreSQL event store backend for ZeroAlloc.EventSourcing.

Published
Oct 16, 2026
3d ago
Total Downloads
730
730

Install

dotnet add package ZeroAlloc.EventSourcing.PostgreSql --version 1.6.4
ProductVersions
.NET
net10.0net8.0net9.0

README

ZeroAlloc.EventSourcing

NuGet Build License: MIT AOT GitHub Sponsors

Event sourcing for .NET with source-generated aggregates: appends allocate nothing of their own, there is no reflection, and it runs under NativeAOT.

Why ZeroAlloc.EventSourcing

  • No allocation on the write path. EventStore.AppendAsync and AggregateRepository.SaveAsync allocate 0 B of their own, asserted by exact-budget tests. A full stream read costs 96 B per call and nothing per event.
  • Works under NativeAOT and trimming. Aggregate event dispatch and the event type registry are generated at compile time. CI publishes and runs an AOT app for the core and for every storage adapter.
  • Compile-time diagnostics. The generator tells you when an aggregate or projection cannot be generated, instead of it silently doing nothing at runtime.
  • Tested against real databases. SQL Server and PostgreSQL run through Testcontainers, SQLite in-process, with optimistic concurrency checked inside the transaction.

Install

dotnet add package ZeroAlloc.EventSourcing.Aggregates
dotnet add package ZeroAlloc.EventSourcing.Generators
dotnet add package ZeroAlloc.EventSourcing.InMemory

The generator package emits the aggregate dispatch. The AOT-safe event serialiser comes from ZeroAlloc.Serialisation, which the core package brings in.

Quick start

using System.Text.Json.Serialization;
using Microsoft.Extensions.DependencyInjection;
using ZeroAlloc.EventSourcing;
using ZeroAlloc.EventSourcing.Aggregates;
using ZeroAlloc.EventSourcing.InMemory;
using ZeroAlloc.Serialisation;

var services = new ServiceCollection()
    .AddOrderPlacedSerializer().AddItemAddedSerializer().AddSerializerDispatcher()
    .AddSingleton<IEventTypeRegistry, OrderEventTypeRegistry>(); // emitted by the generator
services.AddEventSourcing()
    .UseInMemoryEventStore() // or UsePostgreSqlEventStore, UseSqlServerEventStore, UseSqliteEventStore
    .UseAggregateRepository<Order, OrderId>(() => new Order(), id => new StreamId($"order-{id.Value}"));
await using var provider = services.BuildServiceProvider();
var orders = provider.GetRequiredService<IAggregateRepository<Order, OrderId>>();

var id = new OrderId(Guid.NewGuid());
using (var order = new Order())
{
    order.Place("alice");
    order.AddItem(9.95m);
    await orders.SaveAsync(order, id); // appends two events, checks the expected version
}

using var loaded = (await orders.LoadAsync(id)).Value; // replays the stream
Console.WriteLine($"{loaded.State.Items} item, total {loaded.State.Total}"); // 1 item, total 9.95

[ZeroAllocSerializable(SerializationFormat.SystemTextJson)] public sealed record OrderPlaced(string Customer);
[ZeroAllocSerializable(SerializationFormat.SystemTextJson)] public sealed record ItemAdded(decimal Price);
[JsonSerializable(typeof(OrderPlaced))]
[JsonSerializable(typeof(ItemAdded))]
internal sealed partial class OrderJson : JsonSerializerContext;

public readonly record struct OrderId(Guid Value);

public partial struct OrderState : IAggregateState<OrderState>
{
    public static OrderState Initial => default;
    public int Items { get; private set; }
    public decimal Total { get; private set; }

    internal OrderState Apply(OrderPlaced e) => this;
    internal OrderState Apply(ItemAdded e) => this with { Items = Items + 1, Total = Total + e.Price };
}

// ApplyEvent dispatch and OrderEventTypeRegistry are source-generated: no reflection.
public sealed partial class Order : Aggregate<OrderId, OrderState>
{
    public void Place(string customer) => Raise(new OrderPlaced(customer));
    public void AddItem(decimal price) => Raise(new ItemAdded(price));
}

The feature tour shows consumers, projections, snapshots, Kafka and telemetry with one example each.

What you get

  • Append-only event store with optimistic concurrency through a typed StreamPosition, and a global stream across all aggregates.
  • Aggregates with struct state, generated event dispatch and a repository that loads and saves them.
  • Projections with filtering, batching and replayable rebuilds.
  • Snapshots with configurable policies, so loads replay only the newer events.
  • Stream consumers with checkpoints, batching, retry with backoff and FailFast, Skip or DeadLetter error handling.
  • Event upcasting, dead-letter stores and health checks.
  • Every package targets net8.0, net9.0 and net10.0.

Optional packages:

Performance

Against a hand-rolled SQLite event store on the same connection, both transactional, both checking the stream version inside the transaction. .NET 8.0.26, i9-12900HK, BenchmarkDotNet v0.15.8.

Operation Hand-rolled ZA.EventSourcing Overhead
Append 1 event (transactional, OCC check) 80.7 µs / 3.80 KB 106.3 µs / 4.79 KB +33% time, +26% alloc
Read 100-event stream (ordered) 66.0 µs / 11.95 KB 140.9 µs / 25.23 KB +114% time, +111% alloc

The overhead is the price of typed events, pluggable serialisation, optimistic concurrency and the aggregate, projection and snapshot layers; at real-database latency the SQL round trip dominates. Full methodology and the per-call allocation budgets: docs/performance.md.

Learn more

Changes from Previous Version

No structural changes between v1.6.3 and v1.6.4.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 3 target frameworks; confirm the exact target used by your application.The published metadata declares 33 direct dependency entries across framework groups. Inspect the resolved dependency graph for your target framework.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.