|
| 1 | +/* |
| 2 | + |
| 3 | +The purpose of this test is to validate the method of moving from Timescale Hypertables to Postgres native partitioned tables |
| 4 | +managed by pg_partman |
| 5 | + |
| 6 | +*/ |
| 7 | +CREATE SCHEMA ts; |
| 8 | +-- Create a table for time-series data |
| 9 | +CREATE TABLE ts.readings ( |
| 10 | + time TIMESTAMPTZ NOT NULL, |
| 11 | + sensor_id INT NOT NULL, |
| 12 | + value DOUBLE PRECISION NOT NULL |
| 13 | +); |
| 14 | +-- Convert the table into a hypertable |
| 15 | +SELECT create_hypertable('ts.readings', by_range('time', INTERVAL '1 day')); |
| 16 | + create_hypertable |
| 17 | +------------------- |
| 18 | + (2,t) |
| 19 | +(1 row) |
| 20 | + |
| 21 | +-- Convert default 7 day chunk interval |
| 22 | +SELECT set_chunk_time_interval('ts.readings', INTERVAL '24 hours'); |
| 23 | + set_chunk_time_interval |
| 24 | +------------------------- |
| 25 | + |
| 26 | +(1 row) |
| 27 | + |
| 28 | +-- Insert sample data |
| 29 | +INSERT INTO ts.readings (time, sensor_id, value) |
| 30 | +SELECT |
| 31 | + time_series AS time, |
| 32 | + FLOOR(RANDOM() * 10 + 1)::INT AS sensor_id, -- Random sensor_id between 1 and 10 |
| 33 | + RANDOM() * 100 AS value -- Random value between 0 and 100 |
| 34 | +FROM |
| 35 | + generate_series( |
| 36 | + '2023-01-19 00:00:00+00'::TIMESTAMPTZ, |
| 37 | + '2025-03-26 03:00:00+00'::TIMESTAMPTZ, |
| 38 | + INTERVAL '1 second' |
| 39 | + ) AS time_series |
| 40 | +LIMIT 600000; |
| 41 | +-- List hypertables |
| 42 | +SELECT * FROM timescaledb_information.hypertables; |
| 43 | + hypertable_schema | hypertable_name | owner | num_dimensions | num_chunks | compression_enabled | tablespaces |
| 44 | +-------------------+-----------------+----------------+----------------+------------+---------------------+------------- |
| 45 | + ts | readings | supabase_admin | 1 | 7 | f | |
| 46 | +(1 row) |
| 47 | + |
| 48 | +-- Rename hypertable |
| 49 | +ALTER TABLE ts.readings RENAME TO ht_readings; |
| 50 | +-- Create copy of hypertable with original name |
| 51 | +CREATE TABLE ts.readings (LIKE ts.ht_readings) PARTITION BY RANGE(time); |
| 52 | +-- Configure pg_partman for daily partitions on sensor_data |
| 53 | +SELECT partman.create_parent( |
| 54 | + p_parent_table := 'ts.readings', |
| 55 | + p_control := 'time', |
| 56 | + p_type := 'range', |
| 57 | + p_interval := '1 day', |
| 58 | + p_premake := 7, -- Create partitions for the next 7 days |
| 59 | + p_start_partition := '2023-01-19 00:00:00+00' -- Start date for partitioning |
| 60 | +); |
| 61 | + create_parent |
| 62 | +--------------- |
| 63 | + t |
| 64 | +(1 row) |
| 65 | + |
| 66 | +INSERT INTO ts.readings SELECT * FROM ts.ht_readings; |
| 67 | +DROP SCHEMA ts CASCADE; |
| 68 | +NOTICE: drop cascades to 9 other objects |
| 69 | +DETAIL: drop cascades to table ts.ht_readings |
| 70 | +drop cascades to table _timescaledb_internal._hyper_2_3_chunk |
| 71 | +drop cascades to table _timescaledb_internal._hyper_2_4_chunk |
| 72 | +drop cascades to table _timescaledb_internal._hyper_2_5_chunk |
| 73 | +drop cascades to table _timescaledb_internal._hyper_2_6_chunk |
| 74 | +drop cascades to table _timescaledb_internal._hyper_2_7_chunk |
| 75 | +drop cascades to table _timescaledb_internal._hyper_2_8_chunk |
| 76 | +drop cascades to table _timescaledb_internal._hyper_2_9_chunk |
| 77 | +drop cascades to table ts.readings |
0 commit comments