Data Lake and Data Warehouse 5 min read

The Silent Data Loss Bug in Incremental ETL (and How to Fix It)

Many incremental pipelines have a bug that never raises an error. It loses a handful of rows on a busy day, the warehouse drifts slightly out of step with the source, and months later someone finds that the revenue report doesn’t match the ledger.

The cause is how the pipeline records its bookmark, usually called a watermark. This article explains the problem and shows a fix for SQL Server sources that takes only a few lines of SQL.

Why rowversion beats LastModifiedDate

Most teams start with a LastModifiedDate column and a query like WHERE LastModifiedDate > @LastRun. It has three weaknesses:

  • Application code must remember to set it, and bulk scripts or direct fixes often don’t.
  • Clock skew between application servers makes it an unreliable cut-off.
  • Several changes can share the same timestamp.

SQL Server’s rowversion type avoids all three. Every insert or update to a row stamps it with a new 8-byte value from a counter that increases across the whole database. SQL Server sets it automatically, so no code can skip it, and every value is unique.

ALTER TABLE dbo.Inventory ADD RowVer ROWVERSION;

The trap: using the highest version you’ve seen

The obvious approach is to remember the highest rowversion loaded and fetch everything above it next time:

SELECT ... FROM dbo.Inventory WHERE RowVer > @LastMaxRowVer;
-- then save MAX(RowVer) (or @@DBTS) as the new watermark

The problem is that rowversion values are assigned when a row is written, not when the transaction commits. Consider two transactions:

  1. Transaction A updates a row and receives version 1000, but takes five seconds to commit.
  2. Transaction B updates another row, receives version 1001, and commits at once.
  3. The pipeline runs in between. It can’t see A’s uncommitted row, loads B’s row, and saves 1001 as the watermark.
  4. Transaction A commits. Its row has version 1000, which is below the watermark.

The next run asks for rows above 1001, so Transaction A’s change is never loaded. Nothing fails and nothing is logged.

The fix: stop at the oldest open transaction

SQL Server provides MIN_ACTIVE_ROWVERSION(), which returns the lowest rowversion still held by an open transaction (or the next value to be issued, if none are open). Everything below it is guaranteed to be committed.

Using it as the upper bound for each run closes the gap. In the example above, the function returns 1000 while Transaction A is open, so the pipeline loads only rows below 1000, saves 1000 as the watermark, and picks up both A and B on the next run.

-- One-time setup: a table to hold the watermark for each source table
CREATE TABLE dbo.EtlWatermark (
    TableName   VARCHAR(128) PRIMARY KEY,
    LastVersion BINARY(8)    NOT NULL
);

INSERT INTO dbo.EtlWatermark (TableName, LastVersion)
VALUES ('Inventory', 0x0000000000000000);
-- Each run
DECLARE @LastProcessedVer BINARY(8);
DECLARE @CurrentSafeVer   BINARY(8);

SELECT @LastProcessedVer = LastVersion
FROM dbo.EtlWatermark
WHERE TableName = 'Inventory';

-- Everything below this value is committed
SET @CurrentSafeVer = MIN_ACTIVE_ROWVERSION();

SELECT ItemID, ItemName, Quantity, RowVer
FROM dbo.Inventory
WHERE RowVer >  @LastProcessedVer
  AND RowVer <  @CurrentSafeVer;

-- Only after the extracted data is safely stored:
UPDATE dbo.EtlWatermark
SET LastVersion = @CurrentSafeVer
WHERE TableName = 'Inventory';

Two details matter here. The upper bound uses <, not <=, because the value returned by MIN_ACTIVE_ROWVERSION() may belong to a transaction that is still open. And the watermark is updated only after the data has been written to its destination, so a failed run restarts from the last good point.

What rowversion does not capture: deletes

A deleted row takes its rowversion with it, so a watermark query can never see it. If your warehouse needs to reflect deletes, choose one of these approaches:

  • Soft deletes. Mark rows as deleted with a flag instead of removing them. The flag change updates rowversion and flows through the pipeline like any other update.
  • SQL Server Change Tracking. A lightweight built-in feature that records which keys were deleted, which you can combine with the rowversion extraction.
  • Periodic key reconciliation. Compare the set of primary keys in the source and target on a schedule and remove keys that no longer exist.

The right choice depends on table size, how often rows are deleted, and whether you can change the source schema. It is worth deciding this before the warehouse goes live rather than discovering missing deletes afterwards.

A useful side effect: protecting application writes

The same column can also protect your application. Entity Framework Core, for example, can include rowversion in every update so that a write fails if someone else changed the row first, rather than silently overwriting their change.

public class Inventory
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public int Quantity { get; set; }

    [Timestamp] // maps to a SQL Server rowversion column
    public byte[] RowVer { get; set; }
}

When a conflict happens, EF Core raises DbUpdateConcurrencyException, and the application can reload the current values and ask the user what to do. One column now serves both data integrity in the application and reliable change detection for the warehouse.

Checklist

CheckWhy it matters
Change detection uses rowversion, not a manually maintained dateEvery change is stamped automatically
Upper bound is MIN_ACTIVE_ROWVERSION(), not MAX(RowVer) or @@DBTSRows from slow transactions aren’t skipped
Upper bound comparison is <The boundary value may still be uncommitted
Watermark is saved only after data lands safelyFailed runs restart without gaps
There is a deliberate strategy for deletesDeleted rows are otherwise invisible

Next steps

If you aren’t sure how your current pipeline sets its watermark, it is worth checking before the next audit or board report relies on it. YenDigital can review your incremental loads and show where rows could be going missing.

Book a pipeline review

Share this article

Our Partners

Built with leading
AI technologies

We build and integrate AI solutions using technologies from OpenAI, Google Gemini, Anthropic, DeepSeek and Perplexity, selected to suit each client’s needs.

Enterprise-Ready AI Solutions Built for Scale & Security

Secure, scalable AI solutions designed to integrate with your enterprise systems and accelerate business outcomes.

Get a Quote