Skip to content

Data Model Analytics

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

Data Model Analytics

This page describes the data of the three analytics extensions of phase 12: kingres_columnar 0.2 (storage), kingres_timepart 0.3 (time partitioning) and kingres_sketch 1.0 (approximate aggregates), the default versions of their install scripts. Their review state is on Analytics Extensions; the control plane and console parts of phase 12 (conversion of a table between row and columnar storage, time-partitioning policies and runs) use these tables through the REST API (Control Plane and REST API).

kingres_columnar: the storage format

kingres_columnar is a table access method (USING columnar) for insert-only analytic tables. The format is described in the section "On-disk format" of its README.

  • One main fork, no other. Block 0 is the metapage: magic, format version, the table's options (codec, level, stripe and chunk sizes), the next row number and stripe id, the identity of the stored content, the creating transaction and the transaction that created the table. It is logged as a whole page. Format version 2 is what 0.1 writes; version 3 (0.2) allows writers to reserve row numbers so that a row has its final row number when it is inserted. A file never changes version; a rewrite writes a new file.
  • Stripes. Every other block belongs to a stripe: a run of consecutive blocks written once and never changed, each with a 24-byte special area. The payload is 8144 bytes per page. A stripe is a 64-byte header, chunk groups and a footer.
  • Chunks. A chunk group holds, per column, one chunk: an optional null bitmap, then the non-null values densely, compressed as a unit (a chunk that does not shrink is stored raw). A group has at most 10000 rows by default and closes early near 32 MB raw. The footer lists per chunk group and column the offset, compressed and raw sizes, codec, null count and minimum and maximum; its CRC-32C is in the header. Large values are stored inline; there is no TOAST.
  • Codecs. none, pglz, lz4, zstd, or auto (zstd, else lz4, else pglz); zstd level 1 to 19.
  • The stripe catalog kingres.columnar_stripe is an ordinary heap table, so a stripe is visible exactly when its catalog row is. This makes rollback, crash recovery, standbys, backup and VACUUM FULL follow PostgreSQL's rules.
erDiagram
  COLUMNAR_TABLE ||--|| METAPAGE : "block 0"
  COLUMNAR_TABLE ||--o{ STRIPE : "blocks 1..n"
  STRIPE ||--|{ CHUNK_GROUP : "contains"
  CHUNK_GROUP ||--|{ CHUNK : "one per column"
  COLUMNAR_STRIPE_ROW }o--|| STRIPE : "catalog row of"
  COLUMNAR_STRIPE_ROW {
    bigint rel_uid PK
    bigint stripe_id PK
    bigint first_row
    int row_count
    int chunk_groups
    bigint first_block
    int block_count
    bytea cid_points
    bytea col_stats
    bool maintenance
    bool reserved
  }
Loading

Columns of kingres.columnar_stripe (key (rel_uid, stripe_id); no privilege for PUBLIC): rel_uid is the identity of the stored content (not the relation's OID, so a byte-for-byte copy keeps its stripes), first_row, row_count, chunk_groups, natts, first_block, block_count, blob_len, raw_bytes, footer_offset, cid_points (command-id change points of the writer), col_stats (stripe-level statistics), maintenance (written by columnar_compact) and, since 0.2, reserved (a claim on row numbers whose stripe is not yet written; readers skip it).

WAL: the resource manager KingresColumnar (id 255, registered on the PostgreSQL wiki list of custom WAL resource managers; the id is part of the WAL format and never changes) logs whole-page images only (stripe pages, metapage, freeze, and since 0.2 a record that returns the tail of a reservation). Options are kept in the metapage, so they are not in a dump.

B-tree indexes (0.2) rely on a row having its final row number when it is inserted; see the README sections "B-tree indexes" and "Reserved row numbers". The limits of the extension are in Limitations and Known Issues.

What is validated on read: page and stripe identity, the footer CRC, offsets, counts, codec ids, decompressed sizes, statistic lengths and a CRC-32C of every stored chunk, so accidental damage gives SQLSTATE XX001.

kingres_timepart: the policy catalog

The extension (0.3) manages range partitions of a table by time, for an ordinary partitioned table and for a user-partitioned distributed table (for the latter it reads the partitions from kingres.dist_partition and changes them with the partition functions of kingres_shard, as a superuser). Its catalog is in the schema kingres:

erDiagram
  TIMEPART_POLICY_DATA ||--o| TIMEPART_POLICY_SEAL : "sealed by"
  TIMEPART_POLICY_DATA ||--o{ TIMEPART_RUN_DATA : "runs"
  TIMEPART_RUN_DATA ||--o{ TIMEPART_RUN_ACTION_DATA : "actions"
  TIMEPART_POLICY_DATA {
    name schema_name PK
    name table_name PK
    interval partition_every
    text timezone
    interval create_ahead
    interval retention
    text retention_action
    interval columnar_after
    text_array columnar_sort
    jsonb columnar_options
    interval run_every
    bool enabled
    timestamptz next_run_at
    uuid stamp
  }
  TIMEPART_RUN_DATA {
    bigint run_id PK
    name schema_name
    name table_name
    timestamptz as_of
    text trigger
    text state
    name started_by
    text error
  }
  TIMEPART_RUN_ACTION_DATA {
    bigint run_id PK
    int seq PK
    text kind
    name partition_schema
    name partition_name
    timestamptz range_from
    timestamptz range_to
    text state
    bigint bytes_before
    bigint bytes_after
  }
  TIMEPART_POLICY_SEAL {
    name schema_name PK
    name table_name PK
    text fingerprint
  }
Loading
  • timepart_policy_data: one policy per table. partition_every must be on the grid (hours dividing 24, days, a week, months dividing 12, years); create_ahead defaults to 7 days; retention NULL keeps everything; retention_action is drop or detach; columnar_after must not exceed retention. Cells and names are explained in the README: a partition is named <table>_p<suffix>.
  • timepart_run_data: one row per run (trigger is schedule, manual or sql; state is running, done, partial or failed). timepart_run_action_data: one row per action (kind is create, convert, drop, detach or misaligned; state is running, done, failed or skipped; sizes before and after).
  • timepart_lock: one row per table, locked without waiting by each step of a run so that a role cannot stop the runs of a table.
  • Identity after a restore. Rows carry OIDs and a stamp, the random value of the installation that wrote them. A row with another stamp comes from a backup: its names are the identity until timepart_adopt() replaces the OIDs. A policy is trusted only with a matching row in timepart_policy_seal, which a dump does not carry.
  • Views timepart_policy, timepart_run, timepart_run_action (security barrier) and timepart_policies (security invoker) are the interface; the base tables are not readable by PUBLIC.
  • The three base tables and the run sequence are registered with pg_extension_config_dump, so a logical dump carries policies and history.

kingres_sketch: the types

hll, topn and tdigest are fixed-format byte strings that live in ordinary columns; they have no comparison operator and cannot be keys.

Type Content Byte layout after the 4-byte header (magic 0x4B, format version 1, kind, parameter)
hll (kind 1) HyperLogLog, precision 4 to 18 (byte 3) nothing when empty; dense 6-bit registers, or sparse entries of 4 bytes, whichever is smaller
topn (kind 2) Space-Saving summary, capacity 1 to 100000 k, total count, then counters sorted by count descending: item length, count, error, item bytes
tdigest (kind 3) t-digest of float8, compression 10 to 10000 compression, centroid count, total weight, min, max, then centroids sorted by mean
state of approx_percentile (kind 4) aggregate state as tdigest, plus the fraction

All integers are little-endian; the bytes are a function of the content, so equal sketches have equal bytes. Text form is \x plus hex digits. A reader that meets another format version raises unsupported sketch format N. Rollups keep one sketch per group of rows; hll union is idempotent, topn and tdigest unions add up, so a rollup must add each source row exactly once or rebuild a window whole.

Sources

Clone this wiki locally