Field notes · CDC reconciliation

Why your CDC row counts don't match, and how to find the missing rows

Eight causes of row count drift between MySQL or Postgres and ClickHouse or Snowflake, and the order I check them in.

By Danish Rajpoot · 16 November 2026 · 7 min read

Sooner or later every team running change data capture runs the same query on both sides. SELECT COUNT(*) FROM orders on MySQL, the same thing on ClickHouse or Snowflake, and the numbers are different. Sometimes by twelve rows, sometimes by forty thousand. Then someone in the meeting asks whether the dashboard numbers can be trusted, and nobody has a clean answer.

I build and run CDC pipelines (MySQL and Postgres through Debezium and Kafka into ClickHouse, Snowflake and lakehouse tables), and I have spent more hours on this exact question than I would like to admit. The good news: the gap almost always comes from a short list of causes, and most of them are not data loss at all. The bad news: one or two of them are, and you only find those if you stop comparing a single total.

This is the order I go through when the counts drift.

First, stop comparing one number

A single COUNT(*) on each side tells you that something is off. It tells you nothing about where. Before chasing causes, split the table into ranges of the primary key and compare per range: row count, min id, max id and the sum of ids. If the source and the warehouse agree on 498 of 500 buckets, you have just turned a vague problem into two small ranges you can look at by hand.

I made a free reconciliation SQL generator that writes these bucketed queries for MySQL, Postgres, ClickHouse, Snowflake and BigQuery so the output lines up row for row. Run it on both sides, diff the results, and keep reading with the bucket list in hand.

Cause 1: you are comparing a moving target

The source keeps writing while you count. Even a healthy pipeline is a few seconds to a few minutes behind, so a count taken "now" on both sides will never match on a busy table. This is the most common false alarm I see.

The fix is a cutoff. Compare only rows created before a fixed timestamp that is safely older than your worst lag, for example created_at < now() - interval 30 minute, using the same literal timestamp on both sides. If your tables have no reliable created or updated column, compare closed primary key ranges instead (ids below a value you read from the warehouse first). If the gap disappears with a cutoff, you never had a data problem, you had a timing problem.

Cause 2: duplicates that have not been merged yet

On ClickHouse, CDC tables usually land in a ReplacingMergeTree keyed on the primary key with a version column. Every update arrives as a new row, and the old versions are only removed when background merges get around to it. Until then, COUNT(*) counts every version, so the warehouse looks bigger than the source.

Count with FINAL, or count distinct primary keys, before deciding anything. If the warehouse is larger than the source, duplicates are the first suspect. On Snowflake the same thing happens if the load is append-only and the merge step into the final table failed or was skipped for a batch: the raw table grows, the final table does not.

Cause 3: deletes that never landed, or landed as flags

Debezium emits a delete event and, by default, a tombstone after it. What happens next depends on your sink. Some setups drop deletes completely. Others keep the row with a __deleted = 'true' flag or an is_deleted column. Either way the warehouse will hold rows the source no longer has.

Check how your sink handles deletes before you count. If you soft-delete, filter the flag out on the warehouse side. If deletes are being dropped, that is a real bug, and the bucket comparison will show it as the warehouse having higher counts and higher id sums in the ranges where your application deletes data.

Cause 4: the snapshot and the stream overlapped badly

When a connector starts, it takes an initial snapshot and then switches to reading the binlog or WAL. If the connector was restarted mid-snapshot, re-snapshotted after an offset reset, or a table was added later with an incremental snapshot, you can end up with gaps or extra copies in specific id ranges. These usually show up as a handful of neighbouring buckets that are all wrong while the rest are perfect.

The tell is in the shape: a contiguous block of bad buckets, often at the old end of the table, with the newest data fine. For those ranges a targeted re-snapshot (Debezium's signalling table supports incremental snapshots with a filter) is safer than reloading the whole table.

Cause 5: binlog or WAL retention ran out

This is the one that is real data loss. If the connector is down longer than your MySQL binlog retention, or a Postgres replication slot was dropped and recreated, the changes in that window are gone from the stream. Debezium will either fail with an error about missing positions or, depending on snapshot.mode, quietly start from a newer point.

The signature is a time window rather than an id range: rows created or updated during the outage are missing or stale, across many buckets. Compare bucket results with a cutoff before and after the outage and it stands out. The long-term fix is retention that covers your worst realistic outage plus alerting on connector lag. My Kafka and CDC sizing calculator helps you work out how much retention and broker storage that actually needs.

Cause 6: messages that went to the dead letter queue (or nowhere)

Sink connectors and custom consumers often have an error policy. With errors.tolerance=all, a record that fails conversion (a bad date, a decimal that overflows, a schema change the sink did not expect) is skipped or sent to a dead letter topic. The pipeline stays green and the row is simply not there.

If you have a DLQ topic, count what is in it. If you do not, that is worth fixing on its own. I prefer pipelines that fail closed: stop and alert on a record they cannot write, instead of skipping it and carrying on. A stopped pipeline gets fixed in an hour. A silently skipping one gets found three months later by finance.

Cause 7: schema changes and type conversions

An ALTER TABLE on the source can change how events are serialized. New columns are usually fine. Renamed columns, changed types, or a new not-null column without a default can make a sink reject rows from that moment on. Type mapping also causes quieter problems: DATETIME versus timezone-aware timestamps, MySQL zero dates, and decimals with more precision than the target column. Those rows exist on both sides, so counts match, but the values do not.

This is why counts alone are not enough. Add a checksum of a few important columns per bucket (the generator does this with an MD5-based hash that is computed the same way on every engine). If counts match and checksums do not, look for type and timezone issues in that range.

Cause 8: filters you forgot you had

Connector configs pick up filters over time: table.include.list, column exclusions, a transform that drops test accounts, a WHERE clause in a dbt model that someone added for a report. Then the source query and the warehouse query are not counting the same population. Before you chase anything else, read the connector config and the model SQL and write down every filter between the source table and the table you are counting.

A practical order of work

  1. Pick a cutoff timestamp older than your worst lag and use the same literal on both sides.
  2. On the warehouse side, count with FINAL or distinct keys, and exclude soft-deleted rows.
  3. Run bucketed counts, id sums and min/max on both sides and diff them.
  4. Look at the shape of the bad buckets: a contiguous id block points at snapshots, a time window points at retention or outages, scattered single rows point at the DLQ or conversion errors.
  5. Inside a bad bucket, list the missing ids by comparing the id list in smaller ranges.
  6. Add a checksum on key columns once counts agree, to catch value drift.
  7. Fix the cause, backfill only the affected ranges, and re-run the same queries to prove it.

Make it a job, not an investigation

Doing this by hand once is useful. Doing it every day is what actually builds trust. On the platforms I run, reconciliation is a scheduled job: the bucketed queries run on both sides after a cutoff, results are stored, and anything above a small tolerance raises an alert with the bucket ranges attached. That turns "can we trust the numbers?" into a question with a dated answer.

It also changes the conversation with the business. Instead of "we think it is fine", you can say which tables were checked, against what cutoff, and how many rows differed, which is usually zero.

When to get help

If you have run through this list and still cannot explain the gap, or you can explain it but do not have time to fix the pipeline properly, that is the work I do. A pipeline health and cost audit covers exactly this: where rows go missing, why, and a written fix list. If the pipeline itself needs rebuilding to be fail-closed, my real-time CDC pipeline package covers MySQL or Postgres into ClickHouse or Snowflake with reconciliation built in from day one. And if you would rather do it yourself, my Zero-Loss CDC playbook walks through the full MySQL, Debezium, Kafka and ClickHouse setup end to end.

Either way, start with the buckets. A number you can narrow down is a problem you can fix.

Generate the bucket queries

Free, runs in your browser, nothing is sent anywhere.

Open the generator
Who does the work

One senior engineer, start to finish

I'm Danish Rajpoot (Muhammad Danish Ishfaq), a data architect and principal data engineer with nearly 9 years on data platforms and a master's in computer science from the University of Eastern Finland. I do the work myself and write the handover so your team can run it without me.

Counts still not matching?

Send me the table, the two numbers and how the pipeline is built. I will tell you where I would look first.

A short note is enough: what you have, what is going wrong, and when you need it.

CDC reconciliationfree SQL generatorOpen