Skip to content

Backfill Plugin (beta)

@chkit/plugin-backfill builds a saved plan from ClickHouse’s physical partitions and sorting keys, then executes its chunks with local checkpoints or a managed job. This page describes the current TypeScript planner.

RequirementApproach
Populate an existing MV’s empty destination from historical source rowsmv_replay, selected automatically from your schema’s materialized views
Replay rows into the same tableTable copy, only after reviewing the engine and retry/duplicate semantics
Copy the rows of a chkit generate --cutover into its shadow table--cutover <database.table> on plan or submit (cutover copy)
Change sorting/partition keys or transform source rows into a shadow tableA zero-downtime cutover, or a reviewed tracked SQL migration with copy, validation and swap

MV replay runs each materialized view’s SELECT over the source chunks and inserts the result into its destination. A destination fed by several views replays their queries together with UNION ALL. Source-based chunking requires every view to resolve to the same simple FROM table; verify that source in the saved plan. Unresolved or different sources fall back to destination metadata and are unsuitable for this empty-target bootstrap. With a resolved source, chunk sizing uses the source table, so the destination may start empty. Define the source, destination and MVs in the configured schema before planning.

Use an empty historical aggregate target and coordinate writers so live materialization does not overlap the historical replay. Compare the final grouped totals with the source; a completed checkpoint alone does not establish aggregate correctness.

Terminal window
bun add -d @chkit/plugin-backfill
# npm alternative:
npm install --save-dev @chkit/plugin-backfill

Register it alongside your existing schema/connection configuration:

import { defineConfig } from '@chkit/core'
import { backfill } from '@chkit/plugin-backfill'
export default defineConfig({
schema: './src/db/schema/**/*.ts',
plugins: [backfill({
stateDir: './chkit/backfill',
maxChunkBytes: 1024 ** 3,
requireIdempotencyToken: true,
failCheckOnRequiredPendingBackfill: true,
})],
// Keep your explicitly configured clickhouse connection here.
})

Planning and local execution require the real clickhouse connection. Obtain URL, username, password and database; do not substitute localhost/default credentials when these are unknown. Use matching chkit/core/plugin versions. Python is in beta; consult its separate documentation rather than assuming identical planning flags.

The planner reads active physical partitions from system.parts, sizes them using uncompressed input-byte metadata (falling back to on-disk bytes when necessary), and divides large partitions along sorting-key ranges. Plans contain partition IDs and the range predicates used to select rows.

  • --max-chunk-bytes controls approximate uncompressed input bytes per chunk. It is not a hard upper bound: unsplittable keys or tables without usable sorting keys can exceed it. It is not a time interval.
  • Optional --from and --to filter partitions using system.parts.min_time / max_time, or min_date / max_date when the partition key uses a Date column. Partitions whose metadata overlaps the interval are eligible. Empty parts (such as those a DELETE mutation leaves) report a zero minimum and do not move a partition’s start to 1970.
  • These flags do not add an exact timestamp-column WHERE filter to every row. An overlapping partition can include rows outside the supplied interval.
  • Unpartitioned tables and tables with a non-temporal partition key (or one on a Date32 column) report epoch timestamps in this metadata. A recent date filter may therefore select no partitions even though a timestamp column contains recent rows. For whole-table replay, omit both filters and review all selected partitions. For an exact row-level interval, use explicit reviewed SQL with the required predicate or design a suitable source; do not reinterpret the partition filter as that predicate.

--time-column, --chunk-hours, --force-large-window and --force are not current planning flags. Older time-windowed examples do not describe this planner. The accepted legacy config fields timeColumn/chunkHours do not switch it back to timestamp-column chunking.

First migrate the target table and materialized view through the normal schema workflow. Verify that the historical target is empty and that the source contains the expected rows. Pause writers for a simple bootstrap; concurrent ingestion requires a separately designed boundary.

Terminal window
# Example: replay the complete source history into an empty MV destination.
chkit plugin backfill plan --target analytics.daily_event_counts --max-chunk-bytes 1G --json > backfill-plan.json

Read the returned planId and planPath, then inspect that saved JSON. For this aggregate bootstrap, require the returned strategy to be mv_replay and the saved chunkPlan.table to identify the intended source. Schema-loading failures can fall back to table-copy mode; do not execute that plan against an aggregate destination. Confirm the target, source queries, selected partitions, sort-key ranges and environment before running. plan reads live metadata and saves local state but does not execute replay INSERTs.

Terminal window
chkit plugin backfill run --plan-id <planId> --concurrency 1 --json
chkit plugin backfill status --plan-id <planId> --json
chkit status --json
chkit check --strict --json

Check the data separately. For a destination with day, event_type, and total, compare its grouped totals with the source query used by the MV:

Terminal window
chkit query "SELECT toDate(occurred_at) AS day, event_type, count() AS total FROM analytics.events GROUP BY day, event_type ORDER BY day, event_type" --json
chkit query "SELECT day, event_type, sum(total) AS total FROM analytics.daily_event_counts GROUP BY day, event_type ORDER BY day, event_type" --json

The result rows must match exactly; also verify that the source rows are unchanged. Store review evidence without committing sensitive production data.

A zero-downtime cutover generated with --cutover-copy backfill leaves the copy of the existing rows to this plugin. Once chkit migrate --apply has stopped at the cutover’s copy check, plan the copy by naming the table instead of a target:

Terminal window
chkit plugin backfill plan --cutover analytics.events --json
chkit plugin backfill run --plan-id <planId>
# or, on ObsessionDB:
chkit plugin backfill submit --cutover analytics.events
chkit migrate --apply

plan finds the cutover’s snapshot table (events__cutover_<stamp>_snapshot) and reads the shadow table, the unique key and the copied columns from its comment. The plan’s strategy is cutover_copy: chunks are sized over the snapshot and insert into the shadow, and each chunk skips the rows whose key the shadow already holds, so the mirrored rows are never copied and a chunk that runs again never duplicates rows. The skip reads the shadow’s keys within the chunk’s sort-key range when that range is on a copied column, otherwise every key of the shadow. Cutover chunks carry no deduplication token (a retry writes fewer rows than the first attempt, so a token would drop them) and read with select_sequential_consistency = 1. --target and --cutover cannot be combined. The plan is refused when no cutover of the table is pending, or when several are.

Terminal window
chkit plugin backfill resume --plan-id <samePlanId> --json
# To retry failed chunks deliberately:
chkit plugin backfill resume --plan-id <samePlanId> --replay-failed --json
chkit plugin backfill doctor --plan-id <samePlanId> --json

Resume uses saved progress and skips completed chunks. A completed run returns a successful no-op; it does not create a new plan. run refuses an existing run and directs you to resume. A chunk’s first attempt runs under the query id backfill-<planId>-<chunkId>; each replay runs under a new id, that prefix followed by a random suffix. The id of every attempt is saved with the chunk before it is submitted, so its status is read from that attempt alone, however soon after a failure the replay starts.

Before submitting anything, resume reads system.processes and system.query_log to find chunks that are still running or already finished, so it polls them instead of submitting them again. A chunk runs on the replica that received it, so on a multi-replica target these reads cover every replica of clickhouse.cluster, or of default (a multi-replica ObsessionDB service) when no cluster is set. If a replica cannot be read, resume stops with an error and submits nothing; re-run it.

A chunk is submitted again only on the server’s proof that its last attempt failed before it wrote anything: an ExceptionBeforeStart or ExceptionWhileProcessing row for that attempt’s query id, where no row of that id wrote a row, with no QueryFinish and nothing running. The attempt must also have run no secondary query on another replica or shard (a row whose initial_query_id is the attempt’s id): the initiator’s written_rows doesn’t count what those write, and their system.query_log rows can flush late, or not at all. And the failure is recorded only on the read after the 10-second recheck window, so a row a replica flushes late can still make it unknown. That proof needs two things. Every replica must answer the read: the replica probe runs with skip_unavailable_shards = 0, so one replica that doesn’t answer stops the resume. And the attempt must be one this release recorded and sent exactly once under its id through its own client (the checkpoint marks it singleSend). An SDK executor that may resend a request never gets that mark. A checkpoint in another format, including one an older release rewrote after a rollback, keeps no such mark. Nor is a failure ever proved when the target is a Distributed table, or its engine can’t be read: an INSERT SELECT into it can run on the shards (parallel_distributed_insert_select, 2 by default in ClickHouse 26.3), and the initial query’s written_rows then doesn’t count the rows they wrote. Once a failure is proved, the checkpoint records it, and a later resume keeps it as proved even if its system.query_log rows have aged out or sit on a replica it can’t read; only a newer outcome of the same query id replaces it, and a secondary query of the attempt, or a row it wrote, seen by any later read revokes it. The checkpoint keeps what each read saw of an attempt that rules a proof out (a secondary query, written rows), so it still counts once those rows are gone, also after a restart. The default resume skips those chunks and runs the pending ones; --replay-failed replays them. A failure recorded by an older release still needs the proof. When a saved attempt has no such proof, resume reads its outcome again for up to 10 seconds while system.query_log catches up. If the outcome is still unknown, resume stops with an error that names each chunk and its query id, and submits nothing, so a chunk that may have written its rows never runs twice. The outcome is unknown, whatever the flags, when:

  • the attempt runs on no replica and system.query_log has no outcome for it: the log flushes later than usual, query logging is off, or the submit never reached the server;
  • the attempt failed after writing rows (a late block failed, or it was cancelled midway): those rows stay in the table, and a replay writes them again unless the table’s deduplication drops them;
  • the attempt failed and its target is a Distributed table, or a table whose engine can’t be read (see above);
  • the attempt ran a secondary query on another replica or shard, whatever that query’s written rows say;
  • not every replica can be read through clusterAllReplicas: the user may not query the cluster, the cluster (clickhouse.cluster, or default) doesn’t exist, or the probe returns no replica count. Then only a run or a QueryFinish found on the replica it can read counts, and a failure there does not. So on a server with no cluster named default and no clickhouse.cluster set, no failure is ever proved, and replaying a failed chunk always needs --accept-unknown. Set clickhouse.cluster to the cluster your server defines (a one-node cluster works) so resume can read it with clusterAllReplicas and prove failures;
  • polling lost contact with the server (also during run: the run then stops starting chunks, waits for the ones in flight, and ends with the same error; the chunk keeps its state);
  • the checkpoint records a failure the server doesn’t show, such as the “Lost contact” failure older releases recorded for a polling outage;
  • an older chkit release replayed the chunk under its shared query id, so a failure row may be the earlier attempt’s;
  • the checkpoint was written by an older release, which saved a chunk’s query id only after its submit returned, and lists the chunk as pending, or not at all, with nothing in system.query_log;
  • the attempt was recorded by an older release, and system.query_log shows only its failure: an older resume could send the chunk’s id a second time, and an older chkit-py could resend a request after a connection reset, so that failure may not belong to the last send.

So that a chunk never runs as secondary queries and is always logged, every chunk INSERT runs with allow_experimental_parallel_reading_from_replicas = 0 (enable_parallel_replicas from 24.10 on), parallel_distributed_insert_select = 0, log_queries = 1, log_queries_min_type = 'QUERY_START' and log_queries_min_query_duration_ms = 0 in its own SETTINGS, which win over profile defaults. A chunk INSERT that a profile ran with parallel replicas, or a Distributed INSERT SELECT that ran on the shards, now runs on the node that receives it, which can be slower; make chunks smaller or raise --concurrency if it matters. Before it sends anything, run and resume check that the server accepts those settings and stop with an error if it doesn’t (readonly = 1, or a settings constraint). In chkit-py, insertSettings that set one of them, or an alias such as enable_parallel_replicas, to another value are refused; one with the same value is dropped, so enable_parallel_replicas: 0 keeps working.

Re-run the resume once the outcome is in system.query_log. If it never will be, --accept-unknown replays those chunks under a new query id. Use it only after you checked the target: if the attempt did write its rows, the replay writes them again, unless the table’s deduplication drops them. --accept-unknown doesn’t replay a proved failure, and --replay-failed doesn’t replay an unknown outcome; pass both to replay both. Resume reads system.processes, system.query_log and the replica probe with strict settings (skip_unavailable_shards = 0, no query cache, every overflow mode 'throw'), so an inherited setting can’t make a partial result look whole. It therefore needs a user allowed to set query settings: with readonly = 1 those reads fail, and resume stops without submitting anything (it fails closed). If resume cannot read the server at all (for example a NETWORK_ERROR, or a replica that doesn’t answer the probe or a read), it stops before it classifies anything and submits nothing, even with both flags; re-run it. A replay gets its new query id only when it is about to be sent, so a chunk the run never reaches keeps its recorded failure. Once a chunk’s outcome is unknown, or a chunk’s submit fails, no other chunk is sent, including one whose checkpoint write was already under way. A chunk the server shows failed doesn’t stop the others: it is recorded failed, and the run goes on. A chunk whose send itself fails (a connect timeout, a lost connection) stops the run with an error that names it: its attempt may have reached the server, so a resume settles it from system.query_log, and if that shows no outcome, only --accept-unknown replays it.

A run, resume or cancel holds a lock on its plan, runs/<planId>.lock in the backfill state directory, from before it reads the checkpoint until it ends. A second one of the same plan, from either CLI, fails at once and names the lock holder’s pid, host and start time. A lock is never taken over: if its run crashed, check that the process is gone, then delete the file. Ctrl-C (SIGINT) or SIGTERM stops a run, resume or cancel cleanly, also while it waits on a read that hangs. No new chunk is sent. Before the command stops, it waits until each chunk it had already admitted has either written its HTTP request to its connection or been refused and had its earlier checkpoint state saved back; it never waits for an INSERT to finish. A chunk’s request gets 10 seconds to resolve the host, connect (TCP and TLS) and be written; past that, the send fails, so a load balancer or TLS terminator that accepts the connection and never answers can’t hold the run. The stop waits at most 15 seconds for those chunks; it then abandons the ones still sending: their connection is closed first, so nothing more of their request is written, and their attempt stays recorded as one that may have run, which the next resume settles from system.query_log. The checkpoint write in progress gets the same 15 seconds to finish, and no other starts after the stop; a write that hangs past them leaves the lock in place, and the message says so. The lock is released, and the command exits with 130 or 143, saying whether it could remove the lock file and naming any chunk it abandoned. If a chunk could not be stopped (it never reached its connection), the lock stays in place and the message says so. A second Ctrl-C or SIGTERM exits at once without waiting and leaves the lock in place, saying so: once the process has exited, delete the lock and re-run. Chunks already submitted keep running on the server, and the next resume attaches to them. Each checkpoint write is synced to disk. Before the lock is taken, runs/ and the state directory are synced into their parents, also when another process created them.

Each chunk has a stable token within its saved plan. ClickHouse’s actual deduplication depends on the table engine, settings and deduplication window; a token is not a universal exactly-once guarantee. A crash after an INSERT but before checkpoint persistence needs engine-specific recovery testing. Verify totals before and after resuming.

Calling plan again generates a new random plan ID, even for the same target/options. It is a new replay, not an idempotent lookup of the previous job, and can duplicate aggregates. Preserve and reuse the original plan/checkpoint.

All commands support the usual --json and --config flags. Replace chkit with bunx chkit or npx chkit for a local installation.

FlagRequiredMeaning
--target <database.table>Unless --cutoverExisting destination table
--cutover <database.table>Unless --targetCopy the rows of the pending cutover of this table
--from <timestamp>NoLower partition-metadata selection bound
--to <timestamp>NoUpper partition-metadata selection bound
--max-chunk-bytes <size>NoChunk-size override, e.g. 1G, 500M
FlagRequiredMeaning
--plan-id <hex16>YesSaved plan ID
--concurrency <n>NoConcurrent async queries, default 3
--poll-interval <ms>NoPoll interval, default 5000
--force-environmentNoExplicitly bypass the saved environment check
--replay-failedResume onlyReplay the chunks the server shows failed before they wrote a row
--accept-unknownResume onlyReplay the chunks whose last attempt has an unknown outcome; this can write their rows twice

status, cancel, and doctor accept --plan-id. cancel marks the saved run as cancelled; it does not kill ClickHouse queries. It takes the plan’s lock, so it fails while a run or resume of the plan is executing: stop that runner with Ctrl-C first, then cancel, and inspect resulting data before planning further work.

submit builds the same partition/sort-key plan and hands it to a managed backend. It accepts --target (or --cutover), optional --from/--to, --max-chunk-bytes, --title, and --concurrency (1–48). First register the ObsessionDB plugin alongside backfill(); it supplies the account/service commands. Submission requires an authenticated ObsessionDB session and selected service:

Terminal window
chkit obsessiondb login
chkit obsessiondb service select
chkit plugin backfill submit --target analytics.daily_event_counts --max-chunk-bytes 1G

The result supplies a job ID and console link. chkit plugin backfill list lists the selected service’s jobs (--service-slug <service> picks another service); status and cancel take --job-id for one job. Without the ObsessionDB plugin, list fails with setup guidance: local runs have no job list, so inspect them with status --plan-id. submit saves the plan like plan does and records the job ID next to it; chkit check then passes for that plan only once the ObsessionDB plugin confirms the job completed. When that job failed or was cancelled, submit --plan-id <planId> sends the saved plan again as a new job (with optional --title and --concurrency) and records the new job ID in place of the old one. See managed backfill jobs. If ObsessionDB is the active target, local plan/run/resume require an explicit --local choice; they do not silently bypass the selected managed service.

Useful current configuration fields:

OptionDefaultPurpose
stateDir<metaDir>/backfillLocal plans/checkpoints
maxChunkBytes10737418240 (10 GiB)Numeric approximate input-byte target
maxParallelChunks1Stored planning concurrency option
requireIdempotencyTokentrueInclude stable per-chunk insert tokens
failCheckOnRequiredPendingBackfilltrueFail the plugin check for unfinished plans

The execution command’s --concurrency controls local async query submission. Inspect --help and the saved plan when changing options; legacy time-window/retry-policy fields in the schema are not evidence of timestamp-column chunking or universal automatic retry guarantees.

chkit/backfill/
plans/<planId>.json # saved target, environment, chunk boundaries and queries
runs/<planId>.json # mutable progress checkpoint
submissions/<planId>.json # job ID of a plan sent with submit, and its confirmed completion

The plan stores an environment fingerprint derived from URL origin and database. A changed URL origin or configured database is refused unless --force-environment is explicitly supplied. This fingerprint does not verify URL paths, credentials, schema or data identity. Plan creation requires a connection; there is no offline metadata plan to treat as verified.

Automatic replay omits MATERIALIZED and ALIAS target columns and cannot supply EPHEMERAL inputs. Like a materialized view inserting into its TO table, mv_replay writes each view output column to the target column of the same name, whatever the column order; target columns the views do not output get their DEFAULT. plan reads the live target and each view’s output columns (also for a target not declared in your schema) and saves that column list in the plan as execution.targetColumns. Planning fails when a view outputs a column the target cannot take (missing, MATERIALIZED or ALIAS), or when the views feeding one target output different columns. run and resume refuse a plan whose target no longer has one of its planned columns; create a new plan. Every chunk INSERT sets asterisk_include_materialized_columns=0 and asterisk_include_alias_columns=0, so a settings profile that enables them does not change which columns SELECT * reads. Planning/execution refuses unsafe column combinations, unreadable metadata and missing targets. For a blocked target, use an explicitly mapped, reviewed SQL migration instead.

chkit check --strict --json includes plugins.backfill. For each saved plan, a missing, unfinished or failed local run can fail the gate; a relaxed policy is surfaced. If the plans directory is missing or empty, the check passes with requiredCount: 0: it cannot discover omitted historical work. A plan sent with submit and never run locally is left to its job backend: the backfill check counts it in submittedCount, not requiredCount, and the ObsessionDB plugin’s check fails until the job is confirmed completed. Without the ObsessionDB plugin loaded, the backfill check reports an unconfirmed submitted plan as backfill_submitted_unverified, with the same severity as backfill_required_pending. Once a local run of a submitted plan exists, the backfill check judges the plan by that run. Keep the state directory available to the machine running that check—local checkpoints are not migration-journal rows. A passing plugin check does not replace source preservation and aggregate validation.