Repository navigation
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.
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).
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
}
| 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 kindschema.distribute,schema.moveorschema.undistributeand a failover (or the restore of preferred primaries) exclude each other;schema.moveandschema.undistributealso exclude a running rebalance and excludeserver.add,server.removeandserver.leave. -
Database import (0045,
task_exclusion_import). Adatabase.importand a rebalance (a drain is a rebalance), and adatabase.importandserver.add,server.removeorserver.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;UpdateServerasks for an unfinished import under the same lock (kingres_cp.lock_operations).
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
}
-
standby: one row per standby instance managed bykingresd(statecreating,ready,rebuilding,removing,failed); a server runs at most one standby per group. The sequencestandby_plan_versionversions 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(kindfailoverorrestorePreferredPrimaries, triggermanualorautomatic, outcomecompleted,completedWithWarnings,partial,failedorcancelled),failover_group(per group statepending,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) andfailover_event(an ordered log). -
rebalance_runandrebalance_move(0008, 0010, 0012, 0040, 0044): one run per task of kindrebalance, outliving the task as the report, with the placement before and after and one row per move (staterunning,done,failed,cancelled,skipped; the extension'smove_id; aroundfor each restart by a person;ext_mark; since 0040reason(balance or drain) andcost(1, or the bucket's bytes); since 0044schema_namewhen the move carries a distributed schema).
| 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). |
| 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. |
- 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 andmove_id.
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0001_init.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0002_servers.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0003_ledger_verification.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0004_certificates.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0005_task_fence.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0006_standby.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0007_config.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0008_rebalance.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0009_task_exclusion.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0011_failover.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0016_failover_excludes_database_changes.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0030_ledger_phase7.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0031_ledger_publication.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0032_ledger_anchoring.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0034_ledger_bundle.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0041_schema_tasks_exclusion.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0042_data_transfer.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/migrations/0045_import_exclusion.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/control-plane/internal/sysdb/sysdb.go
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/adr/0019-cluster-state-in-a-system-database.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/api/openapi.yaml
KingresDB's own code is licensed under AGPL-3.0-only. PostgreSQL code keeps the PostgreSQL License. Copyright of the project's own code: RooyeKhat Media Co. KingresDB is derived from PostgreSQL and is not endorsed by the PostgreSQL project. Repository | Licence | COPYRIGHT
KingresDB
1. Product
- Product Overview and Features
- Status and Roadmap
- User Guide
- Administrator Guide
- REST API Reference
- Web Console
- Command Line Client
- Extensions
- Limitations and Known Issues
- Release Notes
Features in detail
- Sharding
- Sharding Query Pushdown
- Sharding Table Lifecycle
- High Availability
- Ledger
- Ledger Signing and Publication
- Ledger Anchoring
- The Verifier
- Analytics Extensions
Administration
- Cluster Setup
- Servers Databases and Roles
- Rebalancing
- Configuration
- Memory and Warm-up
- Control Plane and REST API
- Upgrades
2. Architecture (arc42)
- Architecture
- Architecture Introduction and Goals
- Architecture Constraints
- Architecture System Context
- Architecture Containers
- Architecture Components
- Architecture Runtime Views Sharding
- Architecture Runtime Views Availability and Ledger
- Architecture Deployment
- Architecture Crosscutting Concepts
- Security Model
- Architecture Decisions
- Architecture Quality Requirements
- Architecture Risks and Technical Debt
- Enterprise Architecture
3. Data architecture
- Data Architecture
- Data Model Sharding
- Data Model Ledger
- Data Model System Database
- Data Model Analytics
- Data Lifecycle
- Backup Restore and PITR
- Data Security
- Data Quality and Integrity
4. Development
- Building from Source
- Development and Testing
- Repository Layout
- Coding Standards
- Branching and Synchronisation
- Testing Strategy
- Review Process
- Release Process
- Contributing
5. Operations