Skip to content

Data Quality and Integrity

Mohammad Rasoul Kazemi edited this page Nov 26, 2026 · 2 revisions

Data Quality and Integrity

KingresDB has several checks, each for a different layer. None of them replaces the others. This page lists what each check proves, what it does not, and how to run it. For what to do with a finding, see Runbooks and Troubleshooting.

Layer Check Proves Does not prove
Cluster structure kingres.check_cluster() catalogs agree, every bucket partition exists where the map says, nothing is left behind by moves, no old prepared transactions that the rows of a partition are right (it is structural)
Standbys kingres.standby_status() each standby streams and is in recovery that data is identical
Ledger inside the database kingres.ledger_verify() the chain, blocks, trees and entries are consistent with each other and with the tables integrity against someone who rewrites the whole chain consistently
Ledger against an outside copy ledger_verify_digest, kingres-verify the chain matches a digest or checkpoint kept outside the database transactions not yet covered by a digest
Columnar storage columnar_check(), columnar_check_clean(), columnar_check_indexes() catalog rows and files agree; stripes and chunks readable; indexes have entries for visible rows nothing about logical content beyond CRCs
Time partitioning kingres.timepart_check() policies are suitable, partitions are ahead, nothing is misaligned or blocked n/a
B-tree indexes PostgreSQL amcheck index structure see the columnar limit below
Table rebuilds built-in verification (kingres_shard.rebuild_verify) the old and new copy hold the same rows after the swap writes after the swap

check_cluster

SELECT * FROM kingres.check_cluster();                     -- older_than defaults to 5 minutes
SELECT * FROM kingres.check_cluster(interval '1 hour');    -- age of a prepared transaction that counts as old

Run it on the map group primary as a superuser. It returns one row (check_name, node_id, object, detail) per inconsistency; no rows means consistent, in the structural sense only: it never compares the rows, indexes, constraints or replica identity of a partition with its parent, and a copy that diverged in content does not show up. After a move, compare counts or checksums of the bucket's tables yourself if you need that assurance. The checks are:

  • Catalog: catalog_differs, extension_version_differs, secret_missing, server_major_differs, node_unreachable.
  • Buckets: partition_missing, partition_misplaced, stray_bucket_table, bucket_map_incomplete, group_without_primary.
  • Moves and splits: orphan_table, orphan_blocked (an old copy that objects of its owner keep from being dropped), stalled_move, split_stalled, replication_slot, publication, subscription, replication_origin, bucket_moving_without_move, move_without_moving_bucket.
  • Transactions: prepared_transaction (older than the argument), foreign_prepared_transaction, prepared_rolled_back_after_failover, participant_lost_committed_transaction, decision_unconfirmed.
  • Partitions and layout: bucket_layout, dist_partition_mismatch, leaf_conversion_stale (a claim of kingres_timepart on the leaves of a partition that nobody renewed).
  • Distributed schemas: schema_missing, schema_table_missing, schema_structure_differs, stray_schema_rows, schema_catalog_differs. For ledger tables: ledger_distributed_table (a ledger table that the catalog also lists as a distributed or reference table) and ledger_on_non_owner (a ledger table on a node that does not own its distributed schema).
  • Reference tables: reference_table_differs (copies compared by their rows).
  • Standbys: standby_unreachable and related problems.
  • Online table operations: table_operation_stalled, shadow_table, log_table, capture_trigger, old_table, table_verification_failed.

kingresd calls check_cluster() itself inside a failover (after resolving prepared transactions, with incidents going into the report of the run) and expects it clean after a move; a general scheduled run with alerts is not described in the sources. See High Availability and Monitoring and Alerts.

Rebuild verification. In the cleaning phase of an online table operation, kingres.internal_table_digest() compares row counts and a digest of the old and new copy; a difference makes the call fail and shows as table_verification_failed. A table written after the swap can give a false failed, which only keeps the old copy.

Ledger: ledger_verify

SELECT * FROM kingres.ledger_verify();             -- from block 0 to the head; no rows when consistent
SELECT * FROM kingres.ledger_verify(1000, 2000);   -- a range of heights
SELECT kingres.ledger_verify_recorded();           -- the same, recorded in kingres.ledger_verification; returns the id

It reads every ledger table and entry and returns (check_name, object, detail) per problem. It detects changed fields, wrong links or hashes, gaps, rows that point above the head, an unregistered capture trigger, an operator_removal record, and (for client signatures) duplicate attestations, unknown or malformed client keys, orphan attestations and unattested entries under policy required. It does not verify signatures and does not detect blocks removed from the end together with the rows that refer to them. A digest or signed checkpoint kept elsewhere does.

Run it on a schedule. The console's Ledger page and the REST API run it as the task ledger.verify (through ledger_verify_recorded), listing results with the kinds blockHashMismatch, prevHashMismatch, merkleRootMismatch, rowHashMismatch, missingBlock and other. Only the first 100 problems are stored. The verification of records of other shard groups and verification against a given digest are not done by this operation yet.

Against an outside copy:

SELECT kingres.ledger_generate_digest();                   -- store the result OUTSIDE the database
SELECT kingres.ledger_verify_digest('{...}');              -- a digest exported earlier
SELECT kingres.ledger_inclusion_proof('accounts', kingres.ledger_row_hash(a.*)) FROM accounts a WHERE id = 1;
SELECT kingres.ledger_verify_proof(proof, digest);         -- uses only its two arguments

The standalone verifier kingres-verify re-checks digests, proofs, blocks, signed checkpoints, tile logs, consistency between checkpoints, aggregates, bundles, client signatures and anchors without trusting the database; exit status 0 means everything asked for was verified, 3 means a part could not be checked and must be treated as an alert. See The Verifier, Ledger.

If a finding shows a stray row above the head, do not remove it before comparing block B with what was published or kept outside the database: if block B was published or digested, the finding is damage, not a stray row, and the way back is a restore from a backup or a point in time that has block B, or accepting the divergence in the publication (Backup Restore and PITR).

Columnar checks

SELECT * FROM kingres.columnar_check();           -- superuser; returns (check_name, relation, detail)
SELECT kingres.columnar_check_clean();            -- true only when every columnar table was checked and nothing was found
SELECT * FROM kingres.columnar_check_indexes('t'::regclass);   -- index_entry_missing
SELECT kingres.columnar_reconcile();              -- deletes catalog rows of files no relation uses
  • columnar_check reports catalog rows of files nobody uses, stripes beyond the end of the file, stripes whose first page is not theirs, stripes over the row limit and unreadable relations. It reads the catalog as of now, never waits for a lock, and reports not_checked for a table it could not lock. columnar_check(true) deletes the first two kinds.
  • columnar_check_clean() is the strict form for a monitor: false for any row columnar_check() would return, a not_checked table included. A monitor tests that the result is empty, or calls columnar_check_clean().
  • Every stored chunk has a CRC-32C checked before decoding, so damage gives SQLSTATE XX001.
  • amcheck. bt_index_check(idx), bt_index_check(idx, false, true) (with heapallindexed) and bt_index_parent_check(idx) work on a columnar B-tree index; heapallindexed reports a row that has no entry. columnar_check_indexes() finds the same without amcheck (superuser, up to 50 million entries). Indexes built with CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY are refused on a columnar table.

Time partitioning

SELECT * FROM kingres.timepart_check();      -- returns (schema_name, table_name, problem, detail)

Reports problems such as misaligned, retire_blocked (a partition a foreign key still references), policy_unverified and an orphan policy after a schema rename. The run history (timepart_run, timepart_run_action) records every action and its outcome.

Which checks change data

All of these checks are read-only except columnar_check(true), columnar_reconcile() and ledger_remove_stray (and the rebuild verification, which only decides whether the old copy is kept). The alerts of the control plane, including ledgerChainDiverged for a diverged chain, are listed on Monitoring and Alerts.

Sources

Clone this wiki locally