|
| 1 | +-- This file and its contents are licensed under the Timescale License. |
| 2 | +-- Please see the included NOTICE for copyright information and |
| 3 | +-- LICENSE-TIMESCALE for a copy of the license. |
| 4 | +-- _timescaledb_functions.decompress_batch(record) RETURNS SETOF record |
| 5 | +-- expands a single row of a compressed chunk into the user-visible rows it |
| 6 | +-- was compressed from. |
| 7 | +SET datestyle TO ISO; |
| 8 | +CREATE TABLE metrics(time timestamptz NOT NULL, device_id int, value float) |
| 9 | + WITH (tsdb.hypertable, tsdb.orderby = 'time', tsdb.segmentby = 'device_id'); |
| 10 | +NOTICE: using column "time" as partitioning column |
| 11 | +-- Populate with test data |
| 12 | +INSERT INTO metrics |
| 13 | +SELECT '2025-01-01'::timestamptz + (g || ' minute')::interval, g % 3, g::float |
| 14 | +FROM generate_series(1, 30) g; |
| 15 | +INSERT INTO metrics VALUES ('2025-01-01 02:00', NULL, NULL); |
| 16 | +SELECT compress_chunk(ch) FROM show_chunks('metrics') ch; |
| 17 | + compress_chunk |
| 18 | +---------------------------------------- |
| 19 | + _timescaledb_internal._hyper_1_1_chunk |
| 20 | + |
| 21 | +-- Capture the compressed chunk relation name |
| 22 | +SELECT format('%I.%I', cc.schema_name, cc.table_name) AS compressed_chunk |
| 23 | +FROM _timescaledb_catalog.chunk c |
| 24 | +JOIN _timescaledb_catalog.chunk cc ON c.compressed_chunk_id = cc.id |
| 25 | +JOIN _timescaledb_catalog.hypertable h ON c.hypertable_id = h.id |
| 26 | +WHERE h.table_name = 'metrics' \gset |
| 27 | +-- Verify set equality: no source row missing, no extra row introduced |
| 28 | +SELECT count(*) AS missing FROM ( |
| 29 | + TABLE metrics |
| 30 | + EXCEPT ALL |
| 31 | + SELECT decomp.time, decomp.device_id, decomp.value |
| 32 | + FROM :compressed_chunk t, |
| 33 | + LATERAL _timescaledb_functions.decompress_batch(t) |
| 34 | + AS decomp(time timestamptz, device_id int, value float) |
| 35 | +) m; |
| 36 | + missing |
| 37 | +--------- |
| 38 | + 0 |
| 39 | + |
| 40 | +SELECT count(*) AS extras FROM ( |
| 41 | + SELECT decomp.time, decomp.device_id, decomp.value |
| 42 | + FROM :compressed_chunk t, |
| 43 | + LATERAL _timescaledb_functions.decompress_batch(t) |
| 44 | + AS decomp(time timestamptz, device_id int, value float) |
| 45 | + EXCEPT ALL |
| 46 | + TABLE metrics |
| 47 | +) e; |
| 48 | + extras |
| 49 | +-------- |
| 50 | + 0 |
| 51 | + |
| 52 | +-- Decompressing a single batch yields exactly that batch. |
| 53 | +SELECT decomp.time, decomp.device_id, decomp.value |
| 54 | +FROM (SELECT t FROM :compressed_chunk t WHERE t.device_id = 1) comp, |
| 55 | + LATERAL _timescaledb_functions.decompress_batch(comp.t) |
| 56 | + AS decomp(time timestamptz, device_id int, value float) |
| 57 | +ORDER BY decomp.time; |
| 58 | + time | device_id | value |
| 59 | +------------------------+-----------+------- |
| 60 | + 2025-01-01 00:01:00-08 | 1 | 1 |
| 61 | + 2025-01-01 00:04:00-08 | 1 | 4 |
| 62 | + 2025-01-01 00:07:00-08 | 1 | 7 |
| 63 | + 2025-01-01 00:10:00-08 | 1 | 10 |
| 64 | + 2025-01-01 00:13:00-08 | 1 | 13 |
| 65 | + 2025-01-01 00:16:00-08 | 1 | 16 |
| 66 | + 2025-01-01 00:19:00-08 | 1 | 19 |
| 67 | + 2025-01-01 00:22:00-08 | 1 | 22 |
| 68 | + 2025-01-01 00:25:00-08 | 1 | 25 |
| 69 | + 2025-01-01 00:28:00-08 | 1 | 28 |
| 70 | + |
| 71 | +DROP TABLE metrics CASCADE; |
0 commit comments