Referencia · §8.16

Partitioning

partition, §M14.20

Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.

CREATE TABLE t (…) PARTITION BY RANGE | LIST | HASH (columns) makes a partitioned table: it holds no rows; its partitions do — CREATE TABLE p PARTITION OF t FOR VALUES FROM (…) TO (…) (the lower bound included, the upper excluded; MINVALUE / MAXVALUE open; several key columns compare in order), FOR VALUES IN (…) (list: one column), FOR VALUES WITH (MODULUS m, REMAINDER r) (hash: partition_hash(key, m) = r, a hash of the key’s encoding, the same in every build), or DEFAULT (range or list: the keys no other partition takes). A partition gets the table’s definition — columns, keys, checks, indexes, foreign keys — and its own storage (WITH (storage = …), §8.15); overlapping bounds are refused, and so is a new bound a default partition already holds rows of. A partitioned table’s primary key and unique keys must include its partition key (so each lives in one partition); CREATE INDEX on it makes the index on every partition (name_partition); ADD COLUMN adds the column to every partition; dropping it drops its partitions. Other schema changes of a partitioned table or a partition, and foreign keys referencing a partitioned table, are refused for now (0A000).

ALTER TABLE t ATTACH PARTITION p FOR VALUES … makes an existing table a partition: it must have the table’s columns (names, types, nullability) and primary key (22023 otherwise); its rows must fall in the bound (23514) and pass the table’s checks; its own indexes and foreign keys give way to the table’s, built over its rows. ALTER TABLE t DETACH PARTITION p makes a partition a table of its own again (42809 when it is not one of t’s); ALTER TABLE t DROP PARTITION p detaches and drops it.

A table partitioned by range of a date or timestamp may have a storage policy: ALTER TABLE t SET STORAGE POLICY (column AFTER interval '30 days') (any storage SET STORAGE names, without columns; NONE takes it away; not in a transaction block, 25001). A partition whose range ends at least the interval before now is converted online to that storage (§8.15) — when the policy is set, and every minute by the server’s maintenance thread (nucleus_server::maintain; nucleus_sql::partition::apply_policies for embedders) — so recent data stays in rows, for writes, and old data goes to columns, for scans.

A query of the partitioned table is an Append of its partitions’ scans (under the table’s policies), each partition read when reached. The optimizer pushes the predicates into each partition and, from those comparing the key’s first column (range) or the key (list, hash =) with a value of no row, derives what a partition must satisfy to hold a match: evaluated now when it is a constant (a partition ruled out is not in the plan), else kept as a guard evaluated when the plan runs — parameters, enclosing rows (EXPLAIN shows Append (pruned when run: …)). A row inserted into it goes to the partition its key falls in (23514 when none does); one written to a partition directly must fall in its bound (23514). UPDATE and DELETE of it change its partitions’ rows — all chosen before any is written — and an update that changes a row’s key to another partition’s moves the row (deleted there, inserted in the other). crates/sql/tests/partition.rs runs random inserts, key-moving updates and deletes on a partitioned table (of row, column and row,column partitions) and a plain one, compares queries and parameter-pruned ones, and runs the integrity check, and applies a storage policy at chosen times; 42_partitions.slt covers the statements.