|
| 1 | +:sectnums: |
| 2 | +:sectnumlevels: 5 |
| 3 | + |
| 4 | += timescaledb_toolkit |
| 5 | + |
| 6 | +== Overview |
| 7 | + |
| 8 | +TimescaleDB Toolkit provides PostgreSQL aggregate functions for statistical analysis, approximate percentiles, distinct counting, counters, and time-weighted calculations. Toolkit can run on plain PostgreSQL-compatible databases; TimescaleDB is optional for the examples in this guide. |
| 9 | + |
| 10 | +This guide was validated on x86_64 Linux with IvorySQL 5.4 (PostgreSQL 18.4), TimescaleDB Toolkit 1.26.0, Rust 1.98.1, and `cargo-pgrx` 0.18.1. The Toolkit Rust workspace tests passed, the release extension built without source changes, and representative hyperfunctions passed in PostgreSQL and Oracle-compatible sessions. |
| 11 | + |
| 12 | +== Prerequisites |
| 13 | + |
| 14 | +Install the C build toolchain, Rust, and the development files for the same IvorySQL installation that will load the extension. Toolkit 1.26.0 requires Rust 1.96 or later. |
| 15 | + |
| 16 | +[source,bash] |
| 17 | +---- |
| 18 | +cargo install cargo-pgrx --version 0.18.1 --locked |
| 19 | +cargo pgrx init --pg18 /path-to/ivorysql/bin/pg_config |
| 20 | +---- |
| 21 | + |
| 22 | +== Build and install |
| 23 | + |
| 24 | +[source,bash] |
| 25 | +---- |
| 26 | +git clone --branch 1.26.0 --depth 1 \ |
| 27 | + https://github.com/timescale/timescaledb-toolkit.git |
| 28 | +cd timescaledb-toolkit |
| 29 | +
|
| 30 | +./tools/build \ |
| 31 | + -pg18 \ |
| 32 | + -pgconfig /path-to/ivorysql/bin/pg_config \ |
| 33 | + -profile release \ |
| 34 | + install |
| 35 | +---- |
| 36 | + |
| 37 | +Run the upstream tests before deployment: |
| 38 | + |
| 39 | +[source,bash] |
| 40 | +---- |
| 41 | +cargo test --workspace --exclude timescaledb_toolkit |
| 42 | +---- |
| 43 | + |
| 44 | +== Create the extension |
| 45 | + |
| 46 | +Connect through the IvorySQL PostgreSQL-compatible port and create the extension in each target database: |
| 47 | + |
| 48 | +[source,sql] |
| 49 | +---- |
| 50 | +CREATE EXTENSION timescaledb_toolkit; |
| 51 | +SELECT extversion |
| 52 | +FROM pg_extension |
| 53 | +WHERE extname = 'timescaledb_toolkit'; |
| 54 | +---- |
| 55 | + |
| 56 | +== Functional validation |
| 57 | + |
| 58 | +[source,sql] |
| 59 | +---- |
| 60 | +CREATE TABLE metrics ( |
| 61 | + ts timestamptz NOT NULL, |
| 62 | + device text NOT NULL, |
| 63 | + value double precision NOT NULL |
| 64 | +); |
| 65 | +
|
| 66 | +INSERT INTO metrics VALUES |
| 67 | + ('2026-01-01 00:00:00+00', 'sensor-a', 10), |
| 68 | + ('2026-01-01 00:01:00+00', 'sensor-a', 20), |
| 69 | + ('2026-01-01 00:02:00+00', 'sensor-b', 30), |
| 70 | + ('2026-01-01 00:03:00+00', 'sensor-c', 40); |
| 71 | +
|
| 72 | +SELECT round(average(stats_agg(value))::numeric, 2) AS average_value, |
| 73 | + round(approx_percentile(0.5, percentile_agg(value))::numeric, 2) |
| 74 | + AS approximate_median |
| 75 | +FROM metrics; |
| 76 | +
|
| 77 | +SELECT distinct_count(hyperloglog(32, device)) AS approximate_devices |
| 78 | +FROM metrics; |
| 79 | +
|
| 80 | +SELECT round(delta(counter_agg(ts, value))::numeric, 2) AS counter_delta, |
| 81 | + round(average(time_weight('Linear', ts, value))::numeric, 2) |
| 82 | + AS time_weighted_average |
| 83 | +FROM metrics; |
| 84 | +---- |
| 85 | + |
| 86 | +The validated results are an average of `25.00`, an approximate median of `29.99`, three distinct devices, a counter delta of `30.00`, and a time-weighted average of `25.00`. |
| 87 | + |
| 88 | +== Oracle-compatible mode |
| 89 | + |
| 90 | +After creating the extension through the PostgreSQL-compatible port, the tested functions can be used in an Oracle-compatible session: |
| 91 | + |
| 92 | +[source,sql] |
| 93 | +---- |
| 94 | +SET ivorysql.compatible_mode = oracle; |
| 95 | +SELECT 1 FROM dual; |
| 96 | +
|
| 97 | +SELECT round(average(stats_agg(value))::numeric, 2) AS oracle_average, |
| 98 | + distinct_count(hyperloglog(32, device)) AS oracle_devices |
| 99 | +FROM metrics; |
| 100 | +---- |
| 101 | + |
| 102 | +[IMPORTANT] |
| 103 | +==== |
| 104 | +With IvorySQL 5.4, running `CREATE EXTENSION timescaledb_toolkit` directly through an Oracle-compatible connection fails because the upstream installation script uses PostgreSQL syntax. Create the extension through the PostgreSQL-compatible port first. This limitation does not prevent the validated Toolkit functions from being called after switching a session to Oracle-compatible mode. |
| 105 | +==== |
| 106 | + |
| 107 | +Toolkit does not provide hypertables or background jobs. Install TimescaleDB separately if those features are required. See https://github.com/timescale/timescaledb-toolkit[the upstream project] for the complete function reference. |
0 commit comments