Sub topic 14
Verifying the Pipeline with Reruns and Counts
This chapter assumes you can write an INSERT ... ON CONFLICT DO UPDATE statement. It also assumes your table has a unique constraint on the external ID, and that you normalize IDs and deduplicate batches before loading. Here you check that these pieces work together.
What to check
A pipeline that runs without errors can still be wrong. Three checks catch most problems:
- Row count. After a load, the table holds one row per distinct normalized external ID in the input.
- Rerun stability. Running the same batch again changes neither the count nor the stored data. This is idempotency, tested directly.
- Key stability. Rows keep the same internal
idafter a rerun. A changedidmeans the pipeline deleted and re-inserted rows instead of updating them.
A runnable check
This script loads a batch that contains a duplicate ID and a messy ID. It then reloads the same batch and compares snapshots.
import sqlite3
SQL = (
"INSERT INTO customers (external_id, name) VALUES (?, ?) "
"ON CONFLICT(external_id) DO UPDATE SET name = excluded.name"
)
def connect():
conn = sqlite3.connect(":memory:")
conn.execute(
"CREATE TABLE customers ("
"id INTEGER PRIMARY KEY, "
"external_id TEXT NOT NULL UNIQUE, "
"name TEXT NOT NULL)"
)
return conn
def load(conn, rows):
latest = {}
for external_id, name in rows:
latest[external_id.strip().upper()] = name # last row wins
with conn:
conn.executemany(SQL, list(latest.items()))
return len(latest)
def snapshot(conn):
return conn.execute(
"SELECT id, external_id, name FROM customers ORDER BY id"
).fetchall()
conn = connect()
batch = [
("C-1001", "Ada Lovelace"),
("c-1002 ", "Grace Hopper"),
("C-1001", "Ada King"),
]
distinct = load(conn, batch)
first = snapshot(conn)
assert len(first) == distinct == 2, first
load(conn, batch) # rerun the same batch
second = snapshot(conn)
assert second == first, (first, second)
print("rows:", len(second))
print("stable ids:", [row[0] for row in second])The first assertion compares the table count with the number of distinct normalized IDs, not with len(batch). The batch has three rows but only two customers. The second assertion compares full snapshots, which includes the internal id values, so it covers all three checks.
Queries to run against the real database
On PostgreSQL, run these after each load.
SELECT source_system, COUNT(*) AS row_count
FROM customers
GROUP BY source_system;
SELECT source_system, upper(btrim(external_id)) AS normalized_id, COUNT(*)
FROM customers
GROUP BY source_system, upper(btrim(external_id))
HAVING COUNT(*) > 1;The first query gives per-source counts to compare with the source system's own total. The second must return no rows. The unique constraint compares exact strings, so C-1001 and c-1001 are different keys to it. If the second query returns rows, normalization is missing or inconsistent somewhere upstream.
Common errors
| Symptom | Likely cause |
|---|---|
| Count is higher after the rerun | The match column has no unique constraint, or IDs are normalized differently between runs |
| Count is lower than the input rows | Duplicate IDs in the batch, which is correct if you deduplicated on purpose |
| Internal `id` values change | The pipeline deletes and re-inserts instead of upserting |
| Data differs after the rerun | The update overwrites fields from a non-deterministic source, or a stale guard is comparing the wrong timestamp |
Exercise
A load of 1,000 input rows leaves 980 rows in the table. A rerun leaves 980 rows again. Is this a bug? What would you check next?
Show answer
Not necessarily. A stable count after a rerun suggests idempotency. The missing 20 rows are probably duplicate external IDs inside the batch, which collapse into one row each. Count the distinct normalized external IDs in the input. If that number is 980, the load is correct. If it is higher, rows are being dropped, so check for failed inserts or a filter in the pipeline.