Skip to content

table-repair

Performs repairs on tables of divergent nodes based on the diff report generated by table-diff.

Syntax

./ace table-repair --diff-file=<diff_file> [--source-of-truth=<node>] [options] <cluster_name> <schema.table_name>
  • --diff-file: Path to the diff result directory that table-diff wrote, or to the JSON report next to it. Repair reads the rows from the block files of the directory; the JSON report only names the directory, and a change made in it by hand has no effect.
  • --source-of-truth (-r): Authoritative node for repairs. Required unless you run in bidirectional insert-only mode or --fix-nulls.
    • cluster_name: Name of the cluster. Optional. Overrides default_cluster in ace.yaml if specified.
  • schema.table_name: Schema‑qualified table name to repair.

Options

Option Alias Description Default
--diff-file <path> -f Path to the diff result directory, or to the JSON report next to it (required)
--dbname <name> -d Database name override
--nodes <list> -n Nodes to include when resolving cluster metadata all
--source-of-truth <node> -r Authoritative node for repairs (required unless --bidirectional)
--dry-run -y Emit a dry-run plan instead of executing changes false
--generate-report -g Write a JSON repair report to reports/<YYYY-MM-DD>/repair_report_<HHMMSS.mmm>.json false
--insert-only -i Only insert missing rows; skip updates/deletes false
--upsert-only -P Insert or update rows; skip deletes false
--repair-plan <path> -p Path to an advanced repair plan (YAML/JSON). Overrides --source-of-truth and uses rule-based actions.
--fix-nulls -X Fill NULL columns on each node using non-NULL values from its peers (no source-of-truth needed) false
--bidirectional -Z Perform insert-only repairs in both directions false
--fire-triggers -t Execute triggers (otherwise runs with session_replication_role='replica') false
--recovery-mode Enable recovery-mode repair when the diff was generated with --against-origin; can auto-select a source of truth using Spock LSNs false
--preserve-origin Preserve replication origin node ID and LSN with per-row timestamp accuracy for repaired rows. When enabled, repaired rows will have commits with the original node's origin ID and exact commit timestamp (microsecond precision) instead of the local node ID. Requires LSN to be available from a survivor node. false
--quiet -q Suppress non-essential logging false
--debug -v Enable verbose logging false

Advanced repair plans

  • Use --repair-plan to drive repairs from a plan. Source-of-truth becomes optional; the plan sets per-row decisions.
  • Mutually exclusive with --bidirectional and --fix-nulls (those are separate modes).
  • Dry-run and reports include rule usage counts per node.
  • See Advanced repair for grammar and Examples for recipes.

Example

./ace table-repair \
  --diff-file=public_customers_large_diffs-20250718134542.json \
  --source-of-truth=n1 \
  --generate-report \
  acctg public.customers_large

Diff reports share the same prefix generated by table-diff (for example public_customers_large_diffs-20250718134542.json). When you request a dry run or report, ACE also writes JSON summaries under reports/<YYYY-MM-DD>/repair_report_<HHMMSS.mmm>.json (or dry_run_report_<...>.json).

Recovery-mode behavior

  • If the diff file indicates only_origin, table-repair refuses to run unless --recovery-mode is set.
  • In recovery-mode, if no --source-of-truth is provided, ACE probes surviving nodes for the failed node’s Spock origin LSN (preferred) and slot LSN (fallback) and picks the highest. Ties or missing LSNs require you to provide --source-of-truth.
  • The chosen source (auto or explicit) is recorded in the repair report along with the LSN probes.
  • Recovery-mode still accepts --repair-plan; the plan is applied after the source of truth is determined. If no repair plan is provided, ACE performs a standard repair with the recovery-mode source selection.

How repair applies the changes

Repair reads the entries of each node pair from the diff result. An entry is one differing row and says whether the row is on both nodes or on one node only; repair does not match the rows of the two nodes again. Repair turns each entry into one row operation on the target node:

Difference Default --upsert-only --insert-only --bidirectional
row only on the source of truth INSERT INSERT INSERT INSERT on the other node
row only on the target DELETE skipped skipped INSERT on the other node
row on both nodes with other values UPDATE of all columns outside the key UPDATE skipped skipped

--fix-nulls gives one UPDATE for each row that has NULL columns, and it sets only the columns that are still NULL.

  • Each row operation must change exactly one row. An INSERT of a row that appeared on the target after the diff fails with a unique violation; an UPDATE or a DELETE of a row that is gone changes no row. Both are failures; repair never turns an insert into an update or the other way round.
  • Commands. Repair sends the row operations in commands of up to 1000 rows of one kind. Each command is one transaction. If one row of a command fails, the whole command is rolled back: the failed row is reported with its error, and the other rows of the command are reported as rolled back with it. Repair then goes on with the next command and the next node.
  • Setup errors. An error that does not come from a row stops repair at once: no connection to a node, missing column types, or a failure in the setup of a command (the client role, spock repair mode, session_replication_role, the replication origin), or a connection that breaks while a command runs. Repair returns this error, and the task ends with the status FAILED. The commands that were committed before stay committed; the rows that were not tried are not listed as failed rows.
  • Unknown result of a commit. If the connection breaks while repair waits for the answer to COMMIT, the server may or may not have committed the command. Repair stops, and the report lists the rows of this command as in_doubt_rows, not as failed rows. Run table-diff to see their state.
  • Order. On each node, repair runs the deletes first, then the updates, then the inserts.
  • One row operation for each key and node. With a repair plan and more than two nodes, two node pairs can give a row operation for the same key on the same node. The diffs of the pairs are taken at different times, so one pair can see the row on the node and the other pair not. If the two row operations are different (an insert and an update, a write and a delete, or two different rows), repair does not change this key on this node and reports it as a failed row with the operation conflict.
  • Result. If a row was not applied, the task ends with the status FAILED and the command returns an error. The log names the keys of the failed rows, without the other values. With --generate-report, the report lists inserted_rows, updated_rows, deleted_rows, failed_rows and in_doubt_rows for each node; each failed row has the node, the key, the operation, the reason and the number of its command.

Run table-diff again after a failed repair. The new diff shows only the rows that are still different, with their current state. A failed row is not in it if the nodes became equal in another way, for example if the row that appeared on the target after the first diff has the same values as the source. For the full list of failed rows, use failed_rows in the report of --generate-report. The log lists at most 20 keys for each failed command.

Sample Output

2025/07/22 12:05:24 INFO Starting table repair for public.customers_large on cluster acctg
2025/07/22 12:05:24 INFO Processing repairs for divergent node: n2
2025/07/22 12:05:24 INFO Applied on n2: 49 inserted, 50 updated, 0 deleted
2025/07/22 12:05:24 INFO Repair of public.customers_large complete in 0.003s. Nodes n2 repaired (49 inserted, 50 updated, 0 deleted).

Fixing null-only drifts (--fix-nulls)

Replication hiccups can leave some columns NULL on one node while populated on another. The --fix-nulls mode cross-fills those NULLs in both directions using values from the paired node(s); it does not require a source-of-truth. Use it when the diff shows only NULL/NOT NULL mismatches and you want to reconcile columns without preferring a single node. Each update sets a column only if it is still NULL on the node; if a value appeared there after the diff, the update changes no row and its command is reported as failed.

Preserving replication origin (--preserve-origin)

When --preserve-origin is enabled, repaired rows maintain the correct replication origin node ID and LSN from the original transaction, along with precise per-row timestamp preservation. This is particularly important in recovery scenarios where:

  • A node fails and rows are repaired from a survivor
  • The failed node may come back online
  • Without origin tracking, the repaired rows would have the local node's origin ID, which could cause conflicts when the original node resumes replication
  • Temporal ordering and conflict resolution depend on accurate commit timestamps

How it works

  1. Origin extraction: ACE extracts the node_origin and commit_ts from the diff file metadata for each row being repaired.

  2. LSN retrieval: For each origin node, ACE queries a survivor node to obtain the origin LSN. If the LSN is not available, the affected rows fall back to a regular repair without origin preservation (a warning will be logged).

  3. Replication origin session: Before executing repairs for each origin group, ACE: - Gets or creates a replication origin for the origin node - Sets up a replication origin session - Configures the session with the origin LSN and timestamp - Executes the repairs - Resets the session

  4. Transaction management: Repair always runs one transaction per command (see How repair applies the changes). With preserve-origin, the inserts and updates are grouped by (origin node, LSN, timestamp), and each command holds rows of one group only, so that its transaction gets the replication origin xact of these rows (pg_replication_origin_xact_setup is transaction-scoped). Deletes have no origin. The repair is not all-or-nothing: commands that committed stay committed, and a failed command is rolled back and reported.

  5. Timestamp precision: Timestamps are stored in RFC3339Nano format (e.g., 2026-01-15T14:23:45.123456Z) to preserve microsecond-level accuracy. This precision is essential when: - Multiple transactions occurred within the same second on the origin node - Conflict resolution depends on precise temporal ordering - Recovery scenarios require exact timestamp matching for conflict-free reintegration

Requirements and limitations

  • LSN availability: The origin LSN should be available from at least one survivor node. If not available, the affected rows fall back to a regular repair without origin preservation.
  • Survivor nodes: At least one survivor node must be accessible to fetch the origin LSN.
  • Privileges: Replication origin functions require superuser or replication privileges on the target database.
  • Missing metadata: If origin metadata is missing from the diff file for some rows, those rows will be repaired without origin tracking (a warning will be logged).

When to use

Enable --preserve-origin in recovery scenarios where: - You want to prevent replication conflicts when the origin node returns - You need to maintain the original transaction timestamps and origin metadata

Origin preservation is off by default. Consider leaving it disabled if: - You're certain the origin node will not come back online - You've permanently removed the origin node from the cluster - You want repaired rows to be treated as local writes