skip to content

With EF Core 10 and the Npgsql provider, how do you use PostgreSQL's xmin column as an automatic row version for a Ticket entity?

level: middleimportance: should knowfreq 35%

answer

  1. a uint property
  2. [Timestamp] or IsRowVersion()
  3. xid store type, column name xmin
  4. system column: no migration DDL
  5. new value read back after each save

basics

~20 s

Declare a uint property and configure it with [Timestamp] or IsRowVersion(). The Npgsql provider maps such a property to PostgreSQL's xmin system column of type xid, which changes on every row update, so no migration creates it.

solid answer

~40 s

PostgreSQL has no `rowversion` type, but every row has the system column `xmin`, which changes whenever the row is updated. In Npgsql.EntityFrameworkCore.PostgreSQL 10, you get it by declaring `public uint Version { get; set; }` and marking it `[Timestamp]` or `.IsRowVersion()`: the provider maps a `uint` row version to the store type `xid`, and a model-finalizing convention names the column `xmin`. Because `xmin` is a system column, migrations never create, alter or drop it. EF Core then behaves as with any row version: the original value goes into the `WHERE` of each `UPDATE` and `DELETE`, the property is never written, and the new value is read back after each save. A stale `xmin` means zero rows affected and a `DbUpdateConcurrencyException`. There is no separate xmin-specific configuration call in the 10.x provider.

code

sql · 3 lines
sql
UPDATE "Tickets" SET "Status" = @p0
WHERE "Id" = @p1 AND "xmin" = @p2
RETURNING "xmin";

go deeper

for a junior

Know that PostgreSQL has a hidden xmin column that EF Core can use as a row version through a uint property.

for a middle

Configure a uint with [Timestamp] or IsRowVersion, and explain why migrations create no column for it.

for a senior

Explain what EF Core sends and reads back, treat xmin as opaque, and carry it through client round trips and provider upgrades.

for a principal

Decide between xmin and an application-managed token when data is also written by other systems, replicated or exported.

## The problem on PostgreSQL SQL Server has a `rowversion` type that changes on every write, which is what EF Core's `[Timestamp]` was designed around. PostgreSQL has no such column type. Writing your own version column works - a `[ConcurrencyCheck]` `Guid` or counter you bump on every save - but it relies on every writer remembering to bump it. PostgreSQL does keep a value that changes on every update: the **`xmin` system column**, the ID of the transaction that last wrote the row. Every table has it and `SELECT *` does not show it. That makes it a free row version. ## Configuring it with Npgsql 10 In a helpdesk API using `Npgsql.EntityFrameworkCore.PostgreSQL` 10: ```csharp public class Ticket { public int Id { get; set; } public string Subject { get; set; } = ""; public string Status { get; set; } = "Open"; [Timestamp] public uint Version { get; set; } // maps to xmin } ``` Or with the fluent API: `modelBuilder.Entity<Ticket>().Property(t => t.Version).IsRowVersion();` What happens in the provider: 1. `[Timestamp]` / `IsRowVersion()` make the property **value-generated on add and update** and a **concurrency token**. 2. Npgsql's type mapping source maps a `uint` row-version property to the PostgreSQL type **`xid`**. 3. A model-finalizing convention finds properties that are `uint`, generated on add or update, concurrency tokens and of store type `xid`, and sets their **column name to `xmin`**. 4. The migrations SQL generator treats `xmin` (with `tableoid`, `cmin`, `xmax`, `cmax` and `ctid`) as a **system column** and never emits DDL for it - so adding the property produces no column in the migration's SQL. The property name in C# can be anything; `Version` or `RowVersion` is common. ## What EF Core sends - On load, `xmin` is selected like any column and its value becomes the property's original value. - On update: `UPDATE "Tickets" SET "Status" = @p0 WHERE "Id" = @p1 AND "xmin" = @p2 RETURNING "xmin";` - the token in the `WHERE`, and the new value returned so the tracked entity is current. - On delete, the token is in the `WHERE` as well. - The property is never in the `SET` list, because a value generated on update is ignored when saving. If another agent updated ticket 4711 first, its `xmin` changed, the statement matches no row, and `SaveChanges` throws `DbUpdateConcurrencyException`. ## Things to know - **Only `uint` maps to `xmin`.** The convention keys on the `xid` store type, which is what a `uint` row version gets. Use exactly that type. - **The value is a transaction ID, not a counter.** Do not compare `xmin` values for order or show them as a version number; treat the value as opaque and only test it for equality. - **Any update changes it** - including one from another application or a trigger - so every writer is covered automatically, unlike an application-managed token. - **Round trips need the value.** An API that lets a client edit a ticket must send `Version` to the client and restore it as the original value when the edit comes back. - **No special call.** In the 10.x provider the `uint` row version described here is the only configuration needed; there is no separate xmin-specific method on the model or options builders. - `ExecuteUpdateAsync` does not apply concurrency tokens; a bulk update bypasses this check unless you add the token to its `Where` yourself.

  • Why does adding a [Timestamp] uint Version to Ticket produce no column in the generated migration SQL?
    The property is mapped to `xmin`, and Npgsql's migrations SQL generator lists `xmin` among PostgreSQL's system columns (`tableoid`, `xmin`, `cmin`, `xmax`, `cmax`, `ctid`) for which it never generates operations. Every table already has `xmin`, so there is nothing to create.
  • Can you show the xmin value to support agents as a ticket revision number?
    Not meaningfully. `xmin` is the ID of the transaction that last wrote the row, so it is not a per-row counter and its numeric value carries no order you should rely on. Use it only for equality checks; keep a separate counter column if the business needs a visible revision number.

  • An int or long property marked [Timestamp] also maps to xmin with Npgsql.
  • You must add an xmin column to the table in a migration.
  • xmin increases by one on each update, so it doubles as a revision counter.
  • EF Core writes the new xmin value into the SET list of every UPDATE.
  • Only updates made through EF Core change xmin.

Assumes EF Core 10 with Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3, where a uint row version maps to xmin by convention and no xmin-specific configuration method exists.