Repository navigation
Sharding Table Lifecycle
Phase 9 of kingres_shard (introduced in version 0.7, current version 0.9) changes tables that already exist: distribute a table that holds rows, turn a distributed table back into an ordinary one, change its distribution, and manage the partitions of a user-partitioned distributed table. See Sharding for the concepts.
Phase 9 is built, including the control-plane tasks and the console dialogs:
- The report,
create_distributed_tableof a table with rows, and the online forms. -
undistribute_tableandalter_distributed_table, blocking and online. - Tables partitioned by the user (RANGE or LIST) can be distributed, and their partitions can be created, attached, detached and dropped.
- Dependent views (
kingres_shard.dependent_views = 'recreate') and storage parameters are carried. - Distribute with related tables (through the control plane) and the operation indicator of a table (
Table.operation).
The limits that remain are listed below and on Limitations and Known Issues.
SELECT * FROM kingres.table_change_report('distribute', 'public.orders'::regclass,
'{"column": "id"}');It returns severity (blocker, warning or note), code, object and detail, and changes nothing. It never stops at the first problem. kind is distribute, distribute_reference, undistribute, alter, create_partition, attach_partition, detach_partition or drop_partition. Superuser, on any node that has the catalog. The codes (for example row_level_security, trigger, foreign_key_plain_table, group_busy, dependent_object) are listed in the README of the extension.
SELECT kingres.create_distributed_table('orders', 'id');
SELECT kingres.create_reference_table('countries');Map group primary only; superuser; READ COMMITTED only (REPEATABLE READ and SERIALIZABLE are refused). This is the blocking form: readers go on and writers wait from the lock until the commit, and readers wait too between the drop of the old table and the commit. The new table is built under a shadow name on every node, the rows are copied through the row router to the groups that own their buckets (the row counts must match or the call fails), and the swap and the commit are one two-phase transaction. A failure at any point before the decision leaves the old table as it was. Memory does not depend on the number of rows.
Carried: columns, defaults, identity and generated columns, constraints, indexes, owned sequences, owner, privileges, comments, storage parameters (fillfactor, autovacuum_*, toast.*; they are kept on the bucket partitions and recorded in kingres.dist_table.reloptions), and foreign keys to reference tables or to distributed tables of the same group on the distribution column. Refused: triggers, rules, row-level security, exclusion constraints, inheritance, temporary and unlogged tables, tablespaces, ledger tables (KS016), and code of a non-superuser role that the copy would reach. A view, materialized view or other object that depends on the table is refused unless kingres_shard.dependent_views = 'recreate' (below). A table partitioned by the user is taken as described under "User-partitioned tables". Not carried (a warning): statistics objects, per-column storage options, CLUSTER ON and the replica identity. Every unique constraint must include the distribution column. Sequences stay per node.
SELECT kingres.create_distributed_table_concurrently('orders', 'id'); -- returns the operation id
SELECT kingres.create_reference_table_concurrently('countries');
SELECT * FROM kingres.table_operations;Writers go on while the rows are copied and wait only for a short switch. These functions cannot run inside a transaction block, need a primary key or a unique index over NOT NULL columns (code no_key_for_online), and run on the map group primary, as a superuser, in READ COMMITTED. A capture trigger and a log record the key of every changed row; a replay reads the current row for every logged key; the switch is one transaction with bounded waits. A table that always has a transaction open for longer than the bounds cannot be rebuilt online with the call. The old copies stay for kingres_shard.table_cleanup_delay (10 minutes by default); kingres.cleanup_table_operations() drops them. DDL on a table under an operation is refused.
SELECT kingres.undistribute_table('orders'); -- onto the map group
SELECT kingres.undistribute_table('orders', cascade_via_foreign_keys => true, target_group => 2);
SELECT kingres.alter_distributed_table('orders', distribution_column => 'customer_id');
SELECT kingres.alter_distributed_table('orders', bucket_count => 256);
SELECT kingres.alter_distributed_table('orders', colocate_with => 'none');-
undistribute_tableturns a distributed or reference table into an ordinary table on the primary oftarget_group(default: the map group; the data must fit there). -
alter_distributed_tablechanges the distribution column, the bucket count or the colocation group ('default','none'or the name of a distributed table), and withcascade_to_colocatedthe tables of its group together. A change of colocation only is a catalog change; anything else is a rebuild. - The blocking forms run in one transaction and writers wait. All rows of a rebuild pass through the session of the map group primary, so the cost is the size of the table.
- The online forms
undistribute_table_concurrentlyandalter_distributed_table_concurrentlylet writers go on, need a primary key or a unique index overNOT NULLcolumns, and refuse foreign keys to or from another hash-distributed table; cascades are the work of the blocking forms.
create_distributed_table takes a table that the user partitioned by RANGE or LIST (one level, ordinary partitions with nothing of their own, at most kingres_shard.max_partition_leaves leaves, the bucket count times the number of partitions). The blocking form only: the online variants refuse it. The layout is hash on top and the user's partitioning below: every bucket partition <table>_bNNNN is itself partitioned, with a leaf <partition>_bNNNN per user partition. A user partition is a row of kingres.dist_partition, not a table, so SELECT * FROM <partition> does not work; query the table with the key condition (the planner prunes both levels). Four functions manage the partitions, and the statements CREATE TABLE ... PARTITION OF, ALTER TABLE ... ATTACH PARTITION, ... DETACH PARTITION and DROP TABLE <partition> are mapped to them on the map group primary:
-
create_partition(parent, partition_name, bound, if_not_exists),drop_partition(parent, partition_name, missing_ok),detach_partition(parent, partition_name)(the partition becomes a distributed table of the same group, no row moves) andattach_partition(parent, partition, bound). -
partition_leaves(parent, partition_name)lists the leaves, andcheck_cluster()reportsdist_partition_mismatch. - A bucket move moves the whole tree of a bucket.
undistribute_tableandalter_distributed_tableof a user-partitioned table are not available (not_available_yet). -
kingres_timepart0.3 runs time-partition policies on these tables (Analytics Extensions).
A table operation replaces the table by a new one, and a view is bound to a table by its identity, so by default a view that uses the table stops the operation (dependent_object). With kingres_shard.dependent_views = 'recreate' the plain views that use the table, and the views that use those, are dropped and made again in the same transaction as the swap, with the same definition, owner, options, privileges and comments. Refused before anything is made: a materialized view, a view with a trigger or a rule of its own or with privileges on a column, and a view that a function or publication uses. Storage parameters are carried by every operation, including a bucket move.
On a database with sharding on, the operations are tasks of the control plane: table.distribute, table.undistribute, table.alter and table.partition, with a preview (POST .../distribution/preview) that shows what would prevent a change. A change runs in mode auto (the default: online when the table has a usable key and more than 100000 estimated rows), online or blocking. These calls refuse REPEATABLE READ and SERIALIZABLE sessions. Distribute with related tables (contract 0.32.0) takes the ordinary tables that foreign keys link to the table and distributes them with it, colocated, in one task: the keys between the tables are taken out, each table is distributed in a step of its own (blocking copy only), and the keys are added back NOT VALID and validated. Table.operation in listTables and getTable says which long operation runs on a table, also one started from SQL, and the console shows it as a badge and turns the conflicting buttons off. See Control Plane and REST API and Web Console.
From kingres_shard--0.9.sql (the lifecycle functions are those of 0.7). All run on the map group primary as a superuser, except table_change_report, which runs on any node that has the catalog.
| Function | Arguments | Purpose |
|---|---|---|
table_change_report |
kind text, relation regclass, options jsonb DEFAULT '{}' |
What prevents a change (blocker), what it would not carry (warning), what is good to know (note). |
create_distributed_table |
relation regclass, dist_column name, colocate_with regclass DEFAULT NULL, bucket_count int DEFAULT NULL |
Blocking form. |
create_reference_table |
relation regclass |
Blocking form. |
create_distributed_table_concurrently |
same as create_distributed_table
|
Online form; returns the operation id. |
create_reference_table_concurrently |
relation regclass |
Online form; returns the operation id. |
undistribute_table |
relation regclass, cascade_via_foreign_keys bool DEFAULT false, target_group int DEFAULT NULL |
Back to an ordinary table. |
undistribute_table_concurrently |
same | Online form. |
alter_distributed_table |
relation regclass, distribution_column name DEFAULT NULL, bucket_count int DEFAULT NULL, colocate_with text DEFAULT NULL, cascade_to_colocated bool DEFAULT false |
Changes the distribution column, bucket count or colocation. |
alter_distributed_table_concurrently |
same | Online form for one table; returns the operation id (NULL when only the catalog changed). |
begin_table_operation, table_operation_advance
|
kind text, relation regclass, options jsonb and op_id bigint, to_phase text, timeout_ms int DEFAULT NULL
|
The steps of an online operation: check, claim and record it; run it to the end of a phase. The _concurrently functions call them. |
cancel_table_operation |
op_id bigint |
Undoes an online operation that has not switched. |
resume_table_operations |
none | Undoes operations whose session is gone and that did not switch, finishes those that switched; returns how many. |
cleanup_table_operations |
force bool DEFAULT false |
Drops the old copies of replaced tables whose time has come; returns how many. |
The partition functions are listed above. Views: kingres.table_operations (progress of the operations; readable by pg_monitor) and kingres.partitions. kingres.check_cluster() also reports leftovers of table operations.
Load the new library on every node, then run ALTER EXTENSION kingres_shard UPDATE on every node. Until a node is updated, create_distributed_table and create_reference_table refuse to run, and the version handshake names the node that is behind.
kingres_shard.rebuild_batch_rows (10000), rebuild_drain_threshold (1000), rebuild_lock_timeout (200ms), rebuild_switch_timeout (10s), rebuild_switch_max_attempts (30), table_cleanup_delay (10min), rebuild_verify (on), dependent_views (refuse) and max_partition_leaves (8192). Failures for tests are injected with kingres_shard.fault_injection. See also Configuration.
- 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/guide/operator-guide.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/reviews/2026-11-19-kingres-shard-phase9-increments-1-2.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/reviews/2026-11-20-kingres-shard-phase9-rebuild.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/README.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