π₯ Syncing¶
Want to read more?
I wrote my master's thesis on comparing syncing strategies.
Meerschaum syncs time-series data in three ETL steps: fetch, filter, and upsert. When writing plugins, you only need to return data between begin and end bounds β Meerschaum handles the rest.
Syncing Stages¶
- Fetch (Extract & Transform): Requests rows newer than the previous sync time, minus a backtracking window.
Example fetch query
- Filter (Remove duplicates): Compares fetched rows against existing target rows and keeps only new or changed rows.
Skip filtering
Skip the filter stage with --skip-check-existing or pipe.sync(check_existing=False).
Example filter query
- Upsert (Load): Writes the delta rows into the target table via the pipe's instance connector. With
upsertenabled, inserts and updates run in a single statement.
Example upsert queries
Backtracking¶
When data arrives late or a pipe tracks multiple streams (an id column), fetching strictly after the newest timestamp misses lagging series.
The backtrack interval (default 1440 minutes) overlaps each sync with the previous window:

Configure it under fetch:backtrack_minutes:
Immutable synchronization strategies¶
Set sync.immutable: true when rows are permanent after insertion (new rows may still arrive late). Configure a non-null datetime column, a stable unique row key, and optionally an id column for multiple series.
Only backtrack (default) and naive work on mutable pipes (sync.immutable: false); all other strategies require sync.immutable: true. Normal deduplication still applies, and none of these strategies delete rows.
flowchart LR
Old["Old history"] --> Late["Newly visible late row"]
Late --> Mark["Target watermark"]
Mark --> New["Newer rows"]
Backtrack["Backtrack starts before watermark"] -.-> Mark
Repair["Verification revisits historical day"] -.-> Late
Incremental strategies¶
| Strategy | Rows requested | Notes |
|---|---|---|
backtrack |
Newest target timestamp minus fetch.backtrack_minutes. |
Default; supports mutable pipes. |
simple |
From the newest target timestamp onward (inclusive). | Late rows older than the watermark need verification. |
naive |
Entire source query, merged by row key. | Picks up edits anywhere; works on mutable pipes. |
slow-id |
From the oldest per-ID target watermark. | A stalled series widens reads for all known IDs. |
append |
UNION ALL of per-ID queries at each ID's watermark. |
Direct Postgres-compatible connectors; β€ 256 IDs. |
join |
Left-joins source to a VALUES table of per-ID watermarks. |
Direct Postgres-compatible connectors; β€ 256 IDs. |
The per-ID strategies (slow-id, append, join) pull all rows for brand-new IDs and require consistent text or integer ID types on both direct PostgreSQL-compatible connectors.
Historical verification¶
| Strategy | How it finds gaps | Requirements |
|---|---|---|
iterative-simple |
Compares exact source vs. target row counts per UTC day and fetches mismatches. | Any SQL connector. |
daily-row-count |
Compares grouped UTC daily counts on both sides and fetches mismatches. | PostgreSQL, SQLite, or DuckDB on both sides. |
binary-search |
Counts a window, then bisects unequal ranges to isolate missing rows. | Any SQL connector. |
iterative-cpisync |
Uses characteristic polynomial interpolation to fetch sparse missing timestamps per series. | PostgreSQL-compatible connectors; unique microsecond timestamps per ID. |
By default, verification strategies inspect the last 30 UTC days (sync.lookback_days). Prefix any strategy with unbounded- (e.g. unbounded-simple, alias for unbounded-iterative-simple) to cover full source history while pruning equal-count ranges.
Count-based strategies assume the target is a subset of an immutable source (equal counts hide in-place edits). For iterative-cpisync, days exceeding 1,024 source rows or 16 missing rows fall back to a regular window fetch (thesis details).
Monthly correction¶
Monthly strategies run simple incrementally and perform a full-history verification on the first automatic run of each UTC calendar month, checkpointing on success:
simple-monthly-naivesimple-monthly-iterative-simplesimple-monthly-daily-row-countsimple-monthly-binary-searchsimple-monthly-iterative-cpisync
Monthly strategies require a registered pipe on a direct SQL instance and an unfiltered automatic sync. All strategies apply only to automatic Pipe.sync() / mrsm sync pipes fetches without explicit begin/end bounds (merge mode with existing-row checks enabled).
Replacing a window¶
Normal syncs merge rows and leave unlisted rows untouched. Pass replace=True (or --replace) when incoming data is authoritative for [begin, end): existing rows in that window are deleted and new rows inserted in one transaction.
Window selection rules:
- With data (
pipe.sync(df, replace=True)): Uses explicitbegin/end; omitted bounds are inferred fromdf's min/max datetimes. Out-of-window rows are skipped, andparamsnarrows the replaced slice. - Without data (fetch): Replaces the fetch's
[begin, end)window (or each chunk's window). Setfetch:replace: trueto make every fetch replacing. - Empty frame: Clears
[begin, end)when both bounds are passed; refused if both are omitted (HTTP API returns400). - No
datetimecolumn: Replaces the entire table (filtered byparams). - Pipelines & instances: Marks the window changed so downstream Pipeline children re-sync it. SQL and
api:instances replace atomically in one transaction; Valkey clears then writes.
Verification Syncs¶
Re-sync a pipe's historical interval in chunks to backfill gaps with verify pipes or sync pipes --verify:
Tune chunk size (verify:chunk_minutes, default 43200) and default lookback bound (verify:bound_days, default 366):
Deduplication Syncs¶
If an upstream source emits duplicate rows, run deduplicate pipes to detect, clear, and re-sync affected chunks.
Requires an instance connector implementing clear_pipe() or deduplicate_pipe().
Combine --verify and --deduplicate
Run mrsm sync pipes --verify --deduplicate to verify and deduplicate in a single pass.