Skip to content

Commit a8ff8ba

Browse files
encimajfroche
authored andcommitted
add hypertable to partman test
1 parent 215a4f2 commit a8ff8ba

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
8+
CREATE SCHEMA ts;
9+
10+
-- Create a table for time-series data
11+
CREATE TABLE ts.readings (
12+
time TIMESTAMPTZ NOT NULL,
13+
sensor_id INT NOT NULL,
14+
value DOUBLE PRECISION NOT NULL
15+
);
16+
17+
-- Convert the table into a hypertable
18+
SELECT create_hypertable('ts.readings', by_range('time', INTERVAL '1 day'));
19+
20+
-- Convert default 7 day chunk interval
21+
SELECT set_chunk_time_interval('ts.readings', INTERVAL '24 hours');
22+
23+
-- Insert sample data
24+
INSERT INTO ts.readings (time, sensor_id, value)
25+
SELECT
26+
time_series AS time,
27+
FLOOR(RANDOM() * 10 + 1)::INT AS sensor_id, -- Random sensor_id between 1 and 10
28+
RANDOM() * 100 AS value -- Random value between 0 and 100
29+
FROM
30+
generate_series(
31+
'2023-01-19 00:00:00+00'::TIMESTAMPTZ,
32+
'2025-03-26 03:00:00+00'::TIMESTAMPTZ,
33+
INTERVAL '1 second'
34+
) AS time_series
35+
LIMIT 600000;
36+
37+
-- List hypertables
38+
SELECT * FROM timescaledb_information.hypertables;
39+
40+
-- Rename hypertable
41+
ALTER TABLE ts.readings RENAME TO ht_readings;
42+
43+
-- Create copy of hypertable with original name
44+
CREATE TABLE ts.readings (LIKE ts.ht_readings) PARTITION BY RANGE(time);
45+
46+
-- Configure pg_partman for daily partitions on sensor_data
47+
SELECT partman.create_parent(
48+
p_parent_table := 'ts.readings',
49+
p_control := 'time',
50+
p_type := 'range',
51+
p_interval := '1 day',
52+
p_premake := 7, -- Create partitions for the next 7 days
53+
p_start_partition := '2023-01-19 00:00:00+00' -- Start date for partitioning
54+
);
55+
56+
INSERT INTO ts.readings SELECT * FROM ts.ht_readings;
57+
58+
DROP SCHEMA ts CASCADE;

0 commit comments

Comments
 (0)