Skip to content

Use this LLM Friendly Docs as an MCP server for Marten.

The search box in the website knows all the secrets—try it!

For any queries, join our Discord Channel to reach us faster.

JasperFx Logo

JasperFx provides formal support for Marten and other JasperFx libraries. Please check our Support Plans for more details.

Rewriting Stored Events 10.0 ​

Events are immutable, and most of the time that is exactly right. But how an event is stored can fall behind how the application would store it today:

  • an upcaster reads an old shape on every read, forever, because the old rows never change;
  • an event type moved to binary serialization, but everything appended before that is still JSON;
  • a serializer that encrypts personal data was introduced, but the history is still in plain text;
  • a payload carries members its record dropped long ago.

RewriteStoredEventsAsync reads the selected events the way Marten reads them (upcasters, binary or JSON, the configured serializer) and writes each one back the way an append would store it today: under its current type name, binary or JSON as now configured, through the configured serializer.

cs
var result = await store.Advanced.RewriteStoredEventsAsync(x =>
{
    // Every event read as PersonRegistered, including those an upcaster reads from an older name
    x.IncludeType<PersonRegistered>();
});

snippet source | anchor

Once a type has been rewritten in every database, an upcaster that only existed for its old rows can be removed.

IncludeAllTypes() selects every event in the store instead. Archived events are history too, so they are rewritten like any other. Events of a type the application no longer knows are left as they are and counted as UnknownTypes.

To change a few individual events inside a session instead, see Changing single events in a session.

Correcting events ​

WARNING

A transform rewrites history. The old version of each event is gone once the rewrite has written it, and nothing that was built from the old version changes with it: projections that already processed the event, natural keys and tags keep what they had until you rebuild them. Take a backup first, and try it with DryRun on a copy.

A transform changes the events of one type before they are written back:

cs
await store.Advanced.RewriteStoredEventsAsync(x =>
    x.Transform<PersonMoved>(e => e with { City = e.City.ToUpperInvariant() }));

snippet source | anchor

Use this for corrections the event's readers can live with in either form: until the rewrite completes, old and new rows exist side by side. A change of shape belongs in an upcaster, which the rewrite then makes permanent.

Only events still stored as JSON ​

Some serializers never write the same bytes twice for the same event, for example one that encrypts with a random IV. Every row then looks changed, and every run writes everything again. When the point of the rewrite is to move events from JSON to such a serializer, read only the rows that are still JSON:

cs
await store.Advanced.RewriteStoredEventsAsync(x =>
{
    x.IncludeType<PersonRegistered>();
    x.OnlyEventsStoredAsJson();
});

snippet source | anchor

What it does to the database ​

A rewrite updates rows of the largest table in the store, so it is never started for you; you start it, and decide how hard it may push:

cs
await store.Advanced.RewriteStoredEventsAsync(x =>
{
    x.IncludeType<ClaimImported>();

    // Events read per page and written back in one statement
    x.BatchSize = 500;

    // Databases rewritten at the same time, for a multi-database store
    x.MaxParallelDatabases = 1;

    // Room for the application between pages
    x.PauseBetweenBatches = 100.Milliseconds();
});

snippet source | anchor

  • Only events that change are written. The comparison happens in the database, so a row that would come out the same leaves no dead tuple behind, and running a rewrite again costs a read of the selected events.
  • Every page is its own short statement. Nothing holds locks across pages.
  • A row changed by someone else in the meantime is skipped, not overwritten. The result counts these as Skipped; run the rewrite again to include them.
  • Updated rows leave dead tuples and write-ahead log. Expect WAL of a few times the rewritten payload, and a table that only shrinks after VACUUM FULL or pg_repack. Use PauseBetweenBatches to keep replication lag and I/O in check.

A dry run reads and compares everything but writes nothing, and reports how many events would change and their payload size before and after:

cs
var result = await store.Advanced.RewriteStoredEventsAsync(x =>
{
    x.IncludeType<ClaimImported>();
    x.DryRun = true;
});

foreach (var database in result.Databases)
{
    Console.WriteLine($"{database.Cursor.Database}: {database.Rewritten} of {database.Visited} events, " +
                      $"{database.BytesBefore} -> {database.BytesAfter} bytes");
}

snippet source | anchor

One database or one tenant ​

By default a rewrite covers every database of the store. To do one at a time, name the database by its identifier, or name a tenant. A tenant means its database, and with conjoined event tenancy only that tenant's rows:

cs
await store.Advanced.RewriteStoredEventsAsync(x =>
{
    x.IncludeType<ClaimImported>();
    x.ForTenant("tenant-b");
});

await store.Advanced.RewriteStoredEventsAsync(x =>
{
    x.IncludeType<ClaimImported>();
    x.ForDatabase(database.Identifier);
});

snippet source | anchor

Progress and resuming ​

Events are visited in order of sequence, then tenant, because a sequence alone is not unique with per-tenant event partitions. After every page OnProgress reports the cursor; pass it to ResumeFrom to continue a database where an earlier run stopped:

cs
await store.Advanced.RewriteStoredEventsAsync(x =>
{
    x.IncludeType<ClaimImported>();
    x.ResumeFrom(lastCursor);
    x.OnProgress = (progress, ct) => saveCursorAsync(progress.Cursor, ct);
}, cancellationToken);

snippet source | anchor

Cancelling the token stops the rewrite. Each page is written in one statement, so a page is written whole or not at all, and the last cursor OnProgress reported is where to pick it up again. The rewrite keeps no state of its own and needs no running host or async daemon: where the cursor is kept, and when the rewrite runs, is up to you.

Because unchanged events are never written, starting over from the beginning is always safe, only slower.

StopAfter(sequence) leaves the events after that sequence alone. Take the sequence when you decide to run the rewrite, so a transform never touches events appended after that:

cs
var ceiling = await store.Storage.Database.FetchHighestEventSequenceNumber();

await store.Advanced.RewriteStoredEventsAsync(x =>
{
    x.Transform<PersonMoved>(e => e with { City = e.City.ToUpperInvariant() });
    x.StopAfter(ceiling);
});

snippet source | anchor

Running a rewrite as a job ​

Marten also implements IEventStore.RewriteStoredEventsAsync from JasperFx, which a Wolverine job uses to run a rewrite one database at a time. A transform used that way is registered by name on opts.Events:

cs
opts.Events.AddRewriteTransform<PersonMoved>("upper-case-city",
    e => e with { City = e.City.ToUpperInvariant() });

snippet source | anchor

The job's ceiling and cursor are kept in the mt_event_range_marks table. Each page is written in one transaction with its cursor, so a job that crashes and resumes never sends an event through a transform twice. Under per-tenant event partitions, a job for the whole database goes through the tenants one after another, each with its own ceiling and cursor.

Changing single events in a session ​

RewriteStoredEventsAsync works through whole event types, page by page. To change a handful of events you have already found, IDocumentSession.Events has two targeted operations instead. Both only queue an update of the event's row in mt_events: nothing is written until SaveChangesAsync(), and the update is part of that transaction.

OverwriteEvent(IEvent) writes back an event you loaded, after you changed its body or its headers:

cs
var events = await session.Events.FetchStreamAsync(ticketId);

foreach (var opened in events.OfType<Event<ApiDocsTicketOpened>>())
{
    // Change the body of the event you loaded...
    opened.Data = opened.Data with { ReporterEmail = "redacted" };

    // ...and its headers, which are only written when headers are enabled
    opened.SetHeader("redacted-by", "support");

    session.Events.OverwriteEvent(opened);
}

// Nothing is written until SaveChangesAsync()
await session.SaveChangesAsync();

snippet source | anchor

It finds the row by the event's Sequence and rewrites its data, its type name and .NET type (those of the event's current type, so an event an upcaster read from an older name is stored under the new one) and, when headers are enabled, its headers. The id, version, stream, timestamp, tenant, and the causation, correlation and user name columns stay as they were. This is the operation ApplyEventDataMasking() uses for every event a masking rule matched.

CompletelyReplaceEvent<T>(sequence, body) puts a new body, which may be of another event type, in place of the event at that sequence:

cs
var events = await session.Events.FetchStreamAsync(ticketId);
var last = events[^1];

// The replacement can be of a different event type than the original. Marten
// returns the new id it assigns to the event
var newEventId = session.Events.CompletelyReplaceEvent(last.Sequence,
    new ApiDocsTicketClosed("Closed by an administrator"));

await session.SaveChangesAsync();

snippet source | anchor

The stream and version stay the same. The event gets a new id, which the method returns, and a new timestamp, and its headers are emptied and its causation id, correlation id and user name cleared, for whichever of those are enabled. This is the operation stream compacting uses to put the Compacted<T> snapshot in place of the stream's last event.

Both store the event the way an append stores it today. An event type that uses binary serialization is written to the binary column, with an empty JSON object in the JSON column, and a JSON event clears the binary column, so a row is never read back through an old binary payload. Before Marten 10 (#5642) both operations always wrote the body as JSON and left the binary column alone, and OverwriteEvent() left the type name as it was.

WARNING

Neither operation re-runs anything that was built from the old event. Inline and asynchronous projections, natural keys and tags keep what they had until you rebuild them, and an asynchronous projection that has already passed the event's sequence never sees the change. With conjoined event tenancy, open the session for the event's tenant: only that tenant's row with the sequence is updated.

Released under the MIT License.