Python bindings guide
tsink ships Python bindings via UniFFI. Thetsink package gives you the full storage engine — writes, queries,
aggregation, rollups, snapshots — from Python with no server process required.
After installation, import it as tsink.
UniFFI record types such as Label, Row, and MetricSeries use keyword-only
constructors in Python.
Installation
Building from source
If a pre-built wheel is not available for your platform:Quick start
TsinkStorageBuilder
Create a builder, call configuration methods, then callbuild() once to get a
TsinkDB handle. The builder is consumed by build() and cannot be reused.
Configuration methods
All methods returnNone and mutate the builder in place.
Data & persistence
| Method | Default | Description |
|---|---|---|
with_data_path(path) | none | Directory for WAL, segments, and metadata. |
with_object_store_path(path) | none | Path (or object-store prefix) for warm/cold tier segments. |
Retention
| Method | Default | Description |
|---|---|---|
with_retention(duration) | 14 days | Global retention window. |
with_retention_enforced(bool) | False | Reject writes outside the retention window. |
with_tiered_retention_policy(hot, warm) | none | Separate retention for hot and warm tiers. |
Timestamp precision
| Method | Default | Description |
|---|---|---|
with_timestamp_precision(precision) | Nanoseconds | One of Nanoseconds, Microseconds, Milliseconds, Seconds. |
Chunks & partitions
| Method | Default | Description |
|---|---|---|
with_chunk_points(n) | 2048 | Points per in-memory chunk before sealing. |
with_partition_duration(duration) | 1 hour | Time range per partition. |
with_max_active_partition_heads_per_series(n) | 8 | Active partition heads per series (out-of-order fanout). |
Memory & cardinality
| Method | Default | Description |
|---|---|---|
with_memory_limit(bytes) | unlimited | Memory budget; exceeding triggers backpressure. |
with_cardinality_limit(series) | unlimited | Maximum unique series count. |
Concurrency
| Method | Default | Description |
|---|---|---|
with_max_writers(n) | CPU count | Parallel writer threads. |
with_write_timeout(duration) | 30 s | Maximum wait for a writer slot. |
WAL
| Method | Default | Description |
|---|---|---|
with_wal_enabled(bool) | True | Enable/disable the write-ahead log. |
with_wal_size_limit(bytes) | unlimited | Maximum WAL size on disk. |
with_wal_buffer_size(size) | default | In-memory WAL buffer size. |
with_wal_sync_mode(mode) | PerAppend | WalSyncMode.PER_APPEND (crash-safe) or WalSyncMode.PERIODIC(interval). |
with_wal_replay_mode(mode) | Strict | WalReplayMode.STRICT or WalReplayMode.SALVAGE. |
Remote segments
| Method | Default | Description |
|---|---|---|
with_remote_segment_cache_policy(policy) | MetadataOnly | Caching strategy for remote tier segments. |
with_remote_segment_refresh_interval(duration) | default | Refresh interval for the remote segment catalog. |
with_mirror_hot_segments_to_object_store(bool) | False | Mirror hot segments to the object store. |
Runtime
| Method | Default | Description |
|---|---|---|
with_runtime_mode(mode) | ReadWrite | StorageRuntimeMode.READ_WRITE or COMPUTE_ONLY. |
with_background_fail_fast(bool) | True | Halt on unrecoverable background errors. |
with_metadata_shard_count(n) | auto | Number of metadata shards. |
TsinkDB
TsinkDB is the main database handle returned by builder.build(). It is
thread-safe and can be shared across Python threads.
Writing data
Value types
Values are represented by theValue enum:
Insert rows
Write acknowledgement
insert_rows_with_result returns a WriteResult so you can inspect the
durability guarantee:
Querying data
Simple select
Select all label combinations
LabeledDataPoints, each carrying labels and data_points.
Select multiple series at once
Advanced queries with QueryOptions
Aggregation variant | Description |
|---|---|
NONE | No aggregation (default) |
SUM | Sum of values |
MIN / MAX | Minimum / maximum |
AVG | Arithmetic mean |
FIRST / LAST | First / last point in window |
COUNT | Number of points |
MEDIAN | Median value |
RANGE | Max − Min |
VARIANCE / STD_DEV | Statistical variance / standard deviation |
Paginated row scanning
For large result sets, scan in pages:scan_series_rows works the same way but accepts a list of MetricSeries
instead of a metric name.
Series discovery
List all metrics
list_metrics_with_wal() includes series that exist only in the WAL (not yet
flushed).
Filter with matchers
SeriesMatcherOp | Equivalent | Example |
|---|---|---|
EQUAL | = | method="GET" |
NOT_EQUAL | != | status!="500" |
REGEX_MATCH | =~ | host=~"web-.*" |
REGEX_NO_MATCH | !~ | env!~"staging|dev" |
Deleting series
Rollup policies
Define materialized downsampled views:Snapshots
Observability
Inspect engine internals at runtime:Memory inspection
Closing the database
close() raises TsinkUniFFIError.
Native histograms
Store Prometheus-style native histograms:Shard window operations
These methods support distributed deployments where data is partitioned across nodes. They are used internally by the cluster layer but are available for advanced use cases.Error handling
All methods raiseTsinkUniFFIError on failure. The exception carries a string
message and is one of these variants:
| Variant | When it occurs |
|---|---|
NoDataPoints | No data found for the given metric/time range. |
InvalidTimeRange | start > end or otherwise invalid range. |
StorageClosed | Operation attempted after close(). |
InvalidInput | Bad metric name, label, or unsupported operation. |
IoError | File-system or disk I/O failure. |
DataCorruption | Checksum mismatch or parse error. |
ResourceExhausted | Memory budget, cardinality limit, or WAL size limit exceeded. |
Other | Lock poisoning, channel errors, WAL issues, etc. |
Type reference
Records (dataclasses)
| Python type | Fields | Notes |
|---|---|---|
Label | name: str, value: str | Metric label key-value pair. |
DataPoint | timestamp: int, value: Value | Single data point. |
Row | metric: str, labels: list[Label], data_point: DataPoint | Complete write record. |
MetricSeries | name: str, labels: list[Label] | Series identity. |
SeriesPoints | series: MetricSeries, points: list[DataPoint] | Series with its data. |
LabeledDataPoints | labels: list[Label], data_points: list[DataPoint] | Labels with points (from select_all). |
QueryOptions | labels, start, end, aggregation, downsample, limit, offset | Advanced query configuration. |
DownsampleOptions | interval: int | Downsample bucket width. |
SeriesSelection | metric: Optional[str], matchers, start, end | Series filter. |
SeriesMatcher | name: str, op: SeriesMatcherOp, value: str | Single label matcher. |
RollupPolicy | id, metric, match_labels, interval, aggregation, bucket_origin | Rollup definition. |
WriteResult | acknowledgement: WriteAcknowledgement | Write durability level. |
DeleteSeriesResult | matched_series: int, tombstones_applied: int | Deletion outcome. |
Enums
| Python type | Variants |
|---|---|
Value | F64, I64, U64, BOOL, BYTES, STR, HISTOGRAM |
Aggregation | NONE, SUM, MIN, MAX, AVG, FIRST, LAST, COUNT, MEDIAN, RANGE, VARIANCE, STD_DEV |
TimestampPrecision | NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS |
StorageRuntimeMode | READ_WRITE, COMPUTE_ONLY |
RemoteSegmentCachePolicy | METADATA_ONLY |
WalSyncMode | PER_APPEND, PERIODIC(interval) |
WalReplayMode | STRICT, SALVAGE |
WriteAcknowledgement | VOLATILE, APPENDED, DURABLE |
SeriesMatcherOp | EQUAL, NOT_EQUAL, REGEX_MATCH, REGEX_NO_MATCH |
HistogramCount | INT(v), FLOAT(v) |
HistogramResetHint | UNKNOWN, YES, NO, GAUGE |