Skip to content

Data Model System Database

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

Data Model System Database

kingresd keeps the state of the cluster in a system database. This page lists its entities from the 31 migrations (numbers 0001 to 0045, with gaps) in kingres/control-plane/internal/sysdb/migrations.

Name of the database

The database is named kingres_system and the objects are in the schema kingres_cp. ADR 0019 says "the database kingres_system on the map group, in the schema kingres_cp"; the Go code connects to dbname=kingres_system and queries kingres_cp.*; the REST contract says the internal kingres_system database is not listed with the user databases. The name kingres_cp alone (used in the comments of the migrations, for example "kingres_cp 1") is the schema, not a database.

The schema is created by versioned migrations embedded in the kingresd binary and applied by the manager leader at start-up, each in one transaction and recorded in kingres_cp.schema_migration. A binary older than the schema refuses to start. The numbers 0019 to 0029 and 0037 to 0039 are not used (the comments say the numbers 15 to 29, or 16 to 29, belong to the failover work); the runner applies every migration it has not recorded, so the gaps are allowed.

The database lives on the map group, so it moves with the map group primary (see Architecture Decisions, ADR 0018 and 0019).

Entities by area

Tasks, servers, settings, alerts (0001 to 0007)

erDiagram
  SERVER_GROUP ||--o{ SERVER : "contains"
  SERVER ||--o{ CERTIFICATE : "was issued"
  SERVER ||--o| CONFIG_SERVER_STATE : "reports"
  TASK ||--o{ CONFIG_CHANGE : "records"
  CONFIG_CHANGE ||--o| CONFIG_CHANGE : "reverts"
  TASK {
    bigint id PK
    text kind
    text state
    text subject_type
    text subject_id
    jsonb input
    jsonb progress
    bool cancel_requested
    text lease_holder
    bigint lease_epoch
  }
  SERVER {
    text server_id PK
    text server_group FK
    text status
    text internal_address
    int pg_port
    text certificate_serial
    int ordinal
  }
  SERVER_GROUP {
    text name PK
    text description
  }
  CERTIFICATE {
    text serial PK
    text server_id
    timestamptz issued_at
    timestamptz not_after
  }
  CONFIG_CHANGE {
    bigint change_id PK
    bigint revision
    bigint task_id
    text level
    text name
    text old_value
    text new_value
  }
  CONFIG_SERVER_STATE {
    text server_id PK
    bigint applied_revision
    text state
  }
Loading
Table Purpose
task The task engine: kind, state (pending, running, paused, failed, done), subject, JSON input, progress and error, a lease (lease_holder, lease_expires_at) and a lease_epoch that fences a holder that lost its lease (0005). A unique partial index allows one unfinished task per subject.
database_setting Per database: sharding on or off, bucket count (1 to 4096), durability (async, sync, syncApply), ledger mode (off, optIn) and digest interval (60 to 86400 s); and, since 0035, the approximate-aggregates setting (approx_enabled, approx_count_distinct as exact or approximate, approx_error_rate from 0.0005 to 0.1).
cluster_setting Key and JSON value.
server_group and server Server groups and members. server holds the status (pending, active, removing), the internal address, the PostgreSQL port, the certificate serial and PEM, the public key (DER), the pinned fingerprint, and an ordinal number that makes alert ids distinct across servers. Private keys are never stored.
certificate Every certificate the CA issued (serial, server, issue time, expiry), so that revoking a server reaches every valid one (0004).
alert Alerts with severity (critical, warning, info), kind, subject, message and the times raised, cleared and acknowledged. A unique partial index allows one open alert per kind and subject.
network_rule, network_revision The network rules by revision and position (JSON), and a single row with the current revision, tls_required and who changed it.
config_value, config_change, config_server_state, sequence config_revision Configuration from the console (0007): the desired value of a parameter at a level (cluster, serverGroup, server, database), the history of changes with old and new value and the change it reverts, and what each server reported at its last apply (applied, pendingRestart, failed, waiting).
ledger_verification, ledger_digest_actor Control-plane identifiers for the extension's verification records, and who requested a digest (0003, 0005).

Task exclusion is enforced in the database: a trigger on task (function task_exclusion, rewritten by 0009, 0011, 0013, 0014, 0016) raises SQLSTATE KX001 to KX005. A rebalance and a change of servers (add, remove, leave, failover, restore of preferred primaries) exclude each other; at most one failover-class task is unfinished at a time; an instance stop, restart or config restart is refused while a failover group it touches is unfinished; and (0016) a failover is refused while a database is created or dropped or a table is distributed, and the other way round. Two further triggers on task, each with a function of its own, add KX006 and KX007:

  • Distributed-schema tasks (0041, task_exclusion_schema). A task of kind schema.distribute, schema.move or schema.undistribute and a failover (or the restore of preferred primaries) exclude each other; schema.move and schema.undistribute also exclude a running rebalance and exclude server.add, server.remove and server.leave.
  • Database import (0045, task_exclusion_import). A database.import and a rebalance (a drain is a rebalance), and a database.import and server.add, server.remove or server.leave, exclude each other. The private database of an import is left out of every walk that changes a catalog, so a drain or a server change that ran meanwhile would leave it wrong. A change of a server's weight or drain flag is not a task; UpdateServer asks for an unfinished import under the same lock (kingres_cp.lock_operations).

Standbys and replication (0006, 0011, 0015, 0018)

erDiagram
  FAILOVER_RUN ||--o{ FAILOVER_GROUP : "covers"
  FAILOVER_RUN ||--o{ FAILOVER_EVENT : "logs"
  REBALANCE_RUN ||--o{ REBALANCE_MOVE : "performs"
  STANDBY {
    bigint standby_id PK
    int group_id
    text server_id
    text primary_server
    text slot_name
    text state
    bool needs_rebuild
    bool diverged
    timestamptz waiting_since
  }
  GROUP_REPLICATION_STATE {
    int group_id PK
    text primary_server
    text flush_lsn
    bool sync_streaming
    int planned_standbys
  }
  FAILOVER_RUN {
    bigint task_id PK
    text kind
    text trigger
    text outcome
    bigint expected_term
  }
  FAILOVER_GROUP {
    bigint task_id PK
    int group_id PK
    text state
    text old_server
    text new_server
    bigint term_before
    bigint term_after
    jsonb data_loss
  }
  FAILOVER_EVENT {
    bigint task_id PK
    int seq PK
    text kind
    jsonb detail
  }
  REBALANCE_RUN {
    bigint task_id PK
    text trigger
    text strategy
    text outcome
    int round
  }
  REBALANCE_MOVE {
    bigint task_id PK
    int seq PK
    text database_name
    int colocation_id
    int bucket
    int from_group
    int to_group
    text state
    bigint move_id
  }
Loading
  • standby: one row per standby instance managed by kingresd (state creating, ready, rebuilding, removing, failed); a server runs at most one standby per group. The sequence standby_plan_version versions the list of standbys that the leader pushes to every server.
  • group_replication_state: what the leader last knew of each group's primary (flush LSN, whether a synchronous standby streams, how many standbys the primary's server knew of).
  • failover_run (kind failover or restorePreferredPrimaries, trigger manual or automatic, outcome completed, completedWithWarnings, partial, failed or cancelled), failover_group (per group state pending, frozen, fenced, pinned, promoted, done, failed, skipped, the old and new server and node, the terms, the decision and data-loss JSON, the report of the rebuild of the old primary) and failover_event (an ordered log).
  • rebalance_run and rebalance_move (0008, 0010, 0012, 0040, 0044): one run per task of kind rebalance, outliving the task as the report, with the placement before and after and one row per move (state running, done, failed, cancelled, skipped; the extension's move_id; a round for each restart by a person; ext_mark; since 0040 reason (balance or drain) and cost (1, or the bucket's bytes); since 0044 schema_name when the move carries a distributed schema).

Ledger publication and anchoring (0030 to 0032)

Table Purpose
ledger_cluster One row (id is always true): the 32-byte cluster id, the manager leader's term (the fencing token of every write), publication on/off, origin prefix and public URL, intervals (publication 10 to 3600 s, anchor 60 to 86400 s), anchor_minimum (1 to 8), anchor_required, first_checkpoint_at (after which the origin prefix cannot change), keys_revision (compare and swap), the published trust file, the servers that hold key files, backup_advised and discontinuity.
ledger_signing_key Public information only: fingerprint (SHA-256 of the public key), 32-byte public key, state next, active, retired or revoked, mode file or command, the validity times, compromised_since, max_tree_size for a retired key. A private key is never stored here.
ledger_event The signed key events, numbered from 0 without gaps.
ledger_log, ledger_checkpoint, ledger_tile The transparency logs, their signed checkpoints and their tiles; see Data Model Ledger. ledger_tile rejects updates.
ledger_digest_view A digest of every group of a database at one moment (what generateLedgerDigest returns): per-chain documents, complete, uncovered groups, aggregate size and hash.
ledger_anchor_target Anchor targets: kind worm, tsa or ots, a unique name, enabled, non-secret JSON configuration, credentials_set (only the fact; the credentials are in a file), the last test and a revision.
ledger_anchor One record per (target, aggregate log, size): the signed note, payload hash, state (pending, submitted, claimed, anchored, failed, superseded, expired), attempt counters, the evidence as text, times, the alert it raised (ledgerAnchorFailed or ledgerAnchorConflict) and a version for compare-and-swap writes.

The migrations 0033 and 0034 add two more ledger tables:

Table Purpose
ledger_client_key_actor Who registered and who revoked a client key of the ledger, by database and key id (32 hexadecimal characters). The extension records the service role, so the control plane remembers the caller, as ledger_digest_actor does for a digest.
ledger_bundle The bundles of the publication for offline verification: database, the server that built the file, who asked, state (building, ready, failed), size and expires_at (24 hours after creation, when the file goes).

Analytics, data transfer and import (0035, 0036, 0042, 0043)

Table Purpose
database_setting (0035) The approximate-aggregates columns described above.
partition_run_task (0036) Which task and which role made a run of a time-partitioning policy, by database and run id. The run itself is a row of kingres_timepart in the database of the table; the extension only knows the service role.
data_transfer (0042, 0043) One row per staged file of a table export, a database export or an upload: kind (tableExport, databaseExport, upload), state (receiving, building, ready, failed, deleted, expired, lost), the server whose disk holds the file, the creating role by name and OID (only that role may read or use the file), database, schema and table, sizes and the reserved quota, the SHA-256, the format (csv, csv.gz, archive/1), a JSON report that never holds data values, the id of the master key and the data key wrapped under it (never the key), times and the task that builds or uses the file. Since 0043: purpose (tableImport or databaseArchive) and archive_state (validating, valid, invalid) for the validation of an uploaded database archive. The name of the file comes from transfer_id only; nothing a client sends is part of a path.

Compatibility and limits stated in the sources

  • The task list is not pruned: the control plane README says the task engine has no retention (open).
  • Credentials of anchor targets and private signing keys are not stored in this database (migration comments: only the fact that credentials were set, and public key information).
  • The sharding extension has its own catalog in each user database (Data Model Sharding); the system database does not duplicate it, but the rebalance tables refer to its colocation_id, bucket and move_id.

Sources

Clone this wiki locally