Repository navigation
Data Model Sharding
The catalog of kingres_shard (version 0.9, the default version) lives in the schema kingres of every database that has the extension. The tables below are taken from kingres_shard--0.9.sql. The comments in the script state which tables are synchronised across nodes and which are local to one node.
- A node is one PostgreSQL instance in the cluster. Nodes belong to a shard group (
group_id); a group has one primary and optional standbys. Group 0 is the map group; its primary holds the authoritative catalog. - A colocation group fixes a bucket count (1 to 4096) and the placement of every bucket. Its kind is
hash(the buckets of hash-distributed tables, with the type and collation of the distribution column) orschema(one bucket that holds the tables of one distributed schema). Tables in one colocation group have the same bucket placement, so joins on the distribution column stay local. - A distributed table (method
hash) belongs to one colocation group and has a distribution column. A reference table (methodreference) has a full copy on every node. A table of a distributed schema (methodschema) has no distribution column; it belongs to the schema's colocation group. - A bucket is a hash partition of a distributed table. The bucket map says which group owns each bucket of a colocation group and in which state (
active,moving,splitting,orphan). A bucket can be split in 2, 4, 8 or 16 parts; themodulusof a bucket tells how finely the hash space is cut where it lies. - A move records the transfer of one bucket (or of a distributed schema) between groups. A split records the splitting of a bucket. A table operation records an online rebuild of a table.
erDiagram
COLOCATION ||--o{ BUCKET_MAP : "places"
COLOCATION ||--o{ DIST_TABLE : "groups"
COLOCATION ||--o| DIST_SCHEMA : "holds one schema"
DIST_TABLE ||--o{ DIST_PARTITION : "has user partitions"
COLOCATION {
int colocation_id PK
text kind
int bucket_count
int max_modulus
bigint placement_epoch
regtype dist_column_type
oid dist_collation
}
DIST_TABLE {
regclass relid PK
text method
name dist_column
int colocation_id FK
text partition_method
text partition_key
text_array reloptions
}
BUCKET_MAP {
int colocation_id PK
int bucket PK
int group_id
text state
int modulus
}
NODE {
int node_id PK
int group_id
text host
int port
text role
bool is_active
bool should_have_shards
text server_group
float8 weight
}
DIST_PARTITION {
regclass parent PK
name partition_name PK
text bound
timestamptz created_at
}
DIST_SCHEMA {
regnamespace nsp PK
int colocation_id FK
timestamptz created_at
bool ship_functions
}
A shard group is not a table: it is identified by group_id in node and in bucket_map (there is no foreign key between them).
| Table | Purpose and constraints |
|---|---|
kingres.node |
One row per instance. Key node_id; (host, port) unique; role is primary or standby; port 1 to 65535. A unique partial index allows at most one primary per group_id. should_have_shards and server_group steer placement; weight (greater than 0, at most 1000, default 1) is the capacity of the group whose primary this node is, used by rebalance plans. |
kingres.colocation |
Key colocation_id; bucket_count between 1 and 4096; kind (hash or schema); max_modulus (the greatest modulus of a bucket of the group, 1 to 65536) and placement_epoch (raised by every move or split switch, so a scan that overlaps one retries); type and collation of the distribution column (NULL for kind schema, which has exactly one bucket). Ids come from the sequence kingres.colocation_id_seq and are never reused. |
kingres.dist_table |
Key relid. A check keeps the shape: hash needs dist_column and colocation_id; reference has neither; schema has colocation_id and no dist_column. Foreign key to colocation. partition_method (range or list) and partition_key describe user partitioning below the hash level; both are NULL or both set, and only for method hash. reloptions keeps the storage parameters of the table, which are applied to the bucket partitions and carried to every table that replaces it. |
kingres.bucket_map |
Key (colocation_id, bucket); group_id; state is active, moving, splitting or orphan; modulus (at least 1, greater than bucket). Foreign key to colocation. |
kingres.dist_partition |
The user partitions of a distributed table: key (parent, partition_name), the bound text, created_at, foreign key to dist_table with cascade. One row stands for one leaf per bucket (the leaf of bucket b is named <partition_name>_b<bbbb> inside <parent>_b<bbbb>). |
kingres.dist_schema |
One row per distributed schema: nsp (a node-local OID; the catalog snapshot carries the name), the colocation_id of the schema's one bucket (unique), created_at and ship_functions. |
kingres.sharding_settings |
One row: the sharding mode of the database (row, schema or both) and distribute_new_schemas (allowed only in modes schema and both). Set with set_sharding_mode(). |
kingres.map_version |
One row. version is raised by every statement that changes the catalog (a statement trigger does it). term is raised by every promote_node(); a node refuses a catalog push of an older term (SQLSTATE KS002). superseded_by_term is node-local. |
Views: kingres.nodes (adds is_local), kingres.buckets (per bucket of each distributed table: owner group and node, state, partition size where the partition exists on this node, modulus), kingres.partitions, kingres.schemas (per distributed schema: owner, group and node, state, table count, size on the owner node, target of a running move), kingres.catalog_status (term, whether this node has been superseded, map version; superusers), kingres.bucket_move_status and kingres.table_operations.
The physical layout of a distributed table on a node: the parent is a partitioned table (PARTITION BY HASH) and each bucket the node owns is a partition with the name pattern <table>_b<nnnn> (the README shows d_b0001). A node holds only the partitions of its own buckets. For a user-partitioned table each bucket has one leaf per user partition. A table of a distributed schema is an ordinary table on the group that owns the schema (its view row shows bucket 0 and no partition); on every other node it is an empty shell.
| Table | Purpose |
|---|---|
kingres.local_node |
One row: which node this server is, and the WAL timeline recorded with it (this is how a promotion done in PostgreSQL behind the catalog's back is noticed). |
kingres.cluster_secret |
One row: the secret behind the tokens of node-to-node sessions (at least 32 bytes), the previous secret and when it was replaced. Row level security is forced and no role has a privilege on it; the C code reads it with the heap API. Not dumped by pg_dump. |
kingres.dtx_log |
Commit decisions of two-phase transactions that originated here (key gid, target node, durable, exempt). The row is written by the committing transaction itself, so its presence means "commit". |
kingres.dtx_applied |
Distributed transactions this node prepared a part of, written inside that part just before PREPARE; the origin compares it after a failover of this group. Removed after decision_retention plus a minute. |
kingres.dtx_incident |
Prepared transactions of an earlier timeline that recovery rolled back without a commit decision; check_cluster() lists them for 30 days. |
kingres.dtx_unconfirmed |
Participants whose commit decisions are kept without a time limit until their synchronous standbys confirm. |
kingres.dtx_epoch |
One row: the time before which untagged transaction identifiers are still resolved. Identifiers carry a tag, the first 16 hex digits of an HMAC of the name with the cluster secret. |
kingres.pending_node |
Addresses add_node() or remove_node() is working on, so recovery can find a prepared transaction on a node the aborted call never registered. |
kingres.add_node_progress |
On a node that add_node() is filling: the distributed schemas whose batch is complete (schema name, digest of the statements). Removed when the node is registered. |
kingres.move_marker |
Replication markers of bucket moves. |
erDiagram
TABLE_OPERATION ||--o{ TABLE_OPERATION_ITEM : "has tables"
TABLE_OPERATION ||--o| TABLE_OPERATION_LOCK : "claims"
BUCKET_MOVE {
bigint move_id PK
int colocation_id
int bucket
int source_group
int target_group
text phase
bool cancel_requested
timestamptz cleanup_after
}
BUCKET_SPLIT {
bigint split_id PK
int colocation_id
int bucket
int group_id
int modulus
int parts
text verify
text phase
bigint rows_done
bigint rows_total
}
TABLE_OPERATION {
bigint op_id PK
text kind
text mode
text phase
jsonb spec
int attempts
text verified
}
TABLE_OPERATION_ITEM {
bigint op_id PK
int ordinal PK
regclass relid
name shadow_name
bigint rows_copied
}
TABLE_OPERATION_LOCK {
int colocation_id PK
bigint op_id FK
}
TABLE_OLD_COPY {
regclass relid PK
timestamptz dropped_after
}
-
kingres.bucket_move(map group primary only, not synchronised): keymove_id; phasespreparing,copying,catching_up,switching,cleaning,done,failed,cancelled; source and target groups differ. A unique partial index allows one unfinished move per bucket. It also records the slot name, the subscription oid,cleanup_afterand whether the replication was dropped. A move of a distributed schema is a move of its one bucket. -
kingres.bucket_split(map group primary only, not synchronised): keysplit_id; the colocation group, the bucket, its group, the new modulus,parts(2, 4, 8 or 16),verify(count,fullornone), the same phases as a move,cancel_requested,cleanup_after,rows_doneandrows_total. A unique partial index allows one unfinished split per colocation group. -
kingres.table_operation,table_operation_item,table_operation_lock,table_old_copy(node-local, superusers only): the record of an online rebuild.kindisdistribute,distribute_reference,undistribute,alterorconvert_leaf(the claim thatkingres_timeparttakes while it converts the leaves of one partition to another access method); the mode isonline; the phases arepreparing,shadow,copying,catching_up,switching,cleaning,done,failed,cancelled;verifiedisok,failedorskipped. The lock table makes a bucket move and a table operation exclude each other per colocation group.table_old_copykeeps replaced tables until theirdropped_aftertime. The blocking variant ofcreate_distributed_tablekeeps no record.
The control plane keeps its own record of rebalance runs and moves; see Data Model System Database.
The script kingres_shard--0.9.sql is the full installation; upgrade scripts exist for every step from 0.1 to 0.9 (kingres_shard--A--B.sql). check_cluster() reports extension_version_differs for a node on another version and server_major_differs for a node on another PostgreSQL major. See Data Quality and Integrity and Upgrades.
User-partitioned tables, dist_partition and the view partitions are built; see Sharding Table Lifecycle.
- https://github.com/KingresDB/Kingres/blob/kingres-18/contrib/kingres_shard/kingres_shard--0.9.sql
- https://github.com/KingresDB/Kingres/blob/kingres-18/contrib/kingres_shard/kingres_shard.control
- https://github.com/KingresDB/Kingres/blob/kingres-18/contrib/kingres_shard/README.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/design/shard-phase9.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/adr/0007-fixed-hash-buckets.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/adr/0008-map-group-source-of-truth.md
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