From e4de6b905ecff20c0b7347539fede378a44bdcfe Mon Sep 17 00:00:00 2001 From: TerrifiedBug Date: Sun, 26 Apr 2026 19:20:54 +0100 Subject: [PATCH] fix(migration): include id in compress_orderby for hypertables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hypertables migration changed each hypertable's primary key to (id, timestamp). TimescaleDB rejects ALTER TABLE ... SET (compress, ...) unless every column of the hypertable's unique indexes is present in either compress_segmentby or compress_orderby — so the next migration errored with 'column id must be used for segmenting or ordering' on every TimescaleDB-enabled deployment. Adding id to compress_orderby satisfies the constraint without harming compression: id is a high-cardinality cuid, so segmenting by it would produce one segment per row and destroy the ratio. --- .../migration.sql | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/prisma/migrations/20260329000001_timescaledb_compression/migration.sql b/prisma/migrations/20260329000001_timescaledb_compression/migration.sql index a7df87c5..618fbde6 100644 --- a/prisma/migrations/20260329000001_timescaledb_compression/migration.sql +++ b/prisma/migrations/20260329000001_timescaledb_compression/migration.sql @@ -3,6 +3,12 @@ -- Enable native compression on hypertable chunks older than 24 hours. -- Achieves 10-20x size reduction for time-series metric data. -- Safe no-op on plain PostgreSQL. +-- +-- Note on compress_orderby: every column of the hypertable's primary key +-- (changed to ("id", "timestamp") in the previous migration) must appear in +-- either compress_segmentby or compress_orderby. We put "id" in orderby — +-- segmenting by a high-cardinality cuid would create one segment per row and +-- destroy the compression ratio. DO $$ BEGIN @@ -12,7 +18,7 @@ BEGIN ALTER TABLE "PipelineMetric" SET ( timescaledb.compress, timescaledb.compress_segmentby = 'pipelineId', - timescaledb.compress_orderby = 'timestamp DESC' + timescaledb.compress_orderby = 'timestamp DESC, id' ); SELECT add_compression_policy( @@ -25,7 +31,7 @@ BEGIN ALTER TABLE "NodeMetric" SET ( timescaledb.compress, timescaledb.compress_segmentby = 'nodeId', - timescaledb.compress_orderby = 'timestamp DESC' + timescaledb.compress_orderby = 'timestamp DESC, id' ); SELECT add_compression_policy( @@ -38,7 +44,7 @@ BEGIN ALTER TABLE "PipelineLog" SET ( timescaledb.compress, timescaledb.compress_segmentby = 'pipelineId', - timescaledb.compress_orderby = 'timestamp DESC' + timescaledb.compress_orderby = 'timestamp DESC, id' ); SELECT add_compression_policy( @@ -51,7 +57,7 @@ BEGIN ALTER TABLE "NodeStatusEvent" SET ( timescaledb.compress, timescaledb.compress_segmentby = 'nodeId', - timescaledb.compress_orderby = 'timestamp DESC' + timescaledb.compress_orderby = 'timestamp DESC, id' ); SELECT add_compression_policy(