From 73c134dd83b705ae254a191340302c20ca0f28a0 Mon Sep 17 00:00:00 2001 From: Raymond Bourges Date: Wed, 4 Mar 2026 19:46:33 +0100 Subject: [PATCH] FEAT : hypertable et select dessus --- docker-compose.yml | 7 +++++ notes.md | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 docker-compose.yml create mode 100644 notes.md diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..dea6070 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +services: + timescaledb: + image: timescale/timescaledb-ha:pg18 + ports: + - 5432:5432 + environment: + - POSTGRES_PASSWORD=postgres \ No newline at end of file diff --git a/notes.md b/notes.md new file mode 100644 index 0000000..d90e29a --- /dev/null +++ b/notes.md @@ -0,0 +1,68 @@ +# timescaledb + + + +## Test + +### Récup datas + +```bash +ssh grab-4 +docker exec -t client-demo-postgres-1 pg_dump -Fc -U postgres -d postgres -f /tmp/backup_postgres.dump +docker cp client-demo-postgres-1:/tmp/backup_postgres.dump /tmp/backup_postgres.dump +exit + +cd /tmp +scp grab-4:/tmp/backup_postgres.dump . +pg_restore -c -x -t mesure -d postgres -h localhost -U postgres -W backup_postgres.dump +pg_restore -c -x -I mesure_idx1 -d postgres -h localhost -U postgres -W backup_postgres.dump +pg_restore -c -x -I mesure_idx2 -d postgres -h localhost -U postgres -W backup_postgres.dump +``` + +### Création d'une hypertable + +```sql +CREATE TABLE mesure_ng ( + "date" TIMESTAMPTZ, + id_captation INT NOT NULL, + valeur float4 NOT NULL, +) WITH ( + tsdb.hypertable, + timescaledb.segmentby = 'id_captation', + timescaledb.orderby='time DESC' +) + +INSERT INTO mesure_ng ("date", id_captation, valeur) + SELECT "date", id_captation, valeur + FROM mesure +``` + +### Ex. de requête + +```sql +SELECT + time_bucket('1 hour', "date") AS heure, + round( + average( + time_weight('linear', "date", valeur) + ) + ) AS moyenne_conso, + COUNT(*) AS nombre_de_mesures +FROM + mesure_ng +WHERE + "date" BETWEEN '2026-03-01 00:00:00' AND '2026-03-31 23:59:59' + and id_captation = 59 -- puissance PAC +GROUP BY + heure +ORDER BY + heure; +``` + +### Continuous aggregates (WIP) + + + +* CREATE MATERIALIZED VIEW +* SELECT add_continuous_aggregate_policy +* supprimer les "vieilles" données de mesure_ng \ No newline at end of file