From e0c827abf8fc4c70adcc8e116e04836ccac52c7e Mon Sep 17 00:00:00 2001 From: Hussein Ait Lahcen Date: Mon, 27 May 2024 18:43:27 +0200 Subject: [PATCH] fix(voyager): better db structure --- .../migrations/20240316012044_queue.sql | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/pg-queue/migrations/20240316012044_queue.sql b/lib/pg-queue/migrations/20240316012044_queue.sql index c11f16ed7f..d8f43cdf77 100644 --- a/lib/pg-queue/migrations/20240316012044_queue.sql +++ b/lib/pg-queue/migrations/20240316012044_queue.sql @@ -1,16 +1,23 @@ CREATE TABLE queue ( - id BIGSERIAL PRIMARY KEY, + id BIGSERIAL, status status NOT NULL DEFAULT 'ready', item JSONB NOT NULL, -- Can't have foreign key relations to hypertables, so recreate the constraints as best as possible parent BIGINT DEFAULT NULL CHECK (parent IS NULL OR parent > 0), -- Error message in case of permanent failure. If set, status MUST be 'failed'. message TEXT CHECK (((message IS NULL) AND (status != 'failed'::status)) OR ((message IS NOT NULL) AND (status = 'failed'::status))), - created_at TIMESTAMPTZ NOT NULL DEFAULT now() + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + PRIMARY KEY (id, created_at) ); -CREATE INDEX index_queue_status ON queue (status); +CREATE INDEX index_queue_id ON queue (id); -CREATE INDEX idx_queue_id_created ON queue (created_at); +CREATE INDEX index_queue_status_id ON queue (status, id); -SELECT create_hypertable('queue', 'id'); +SELECT create_hypertable('queue', 'created_at'); + +SELECT add_retention_policy('queue', INTERVAL '60 days'); + +ALTER TABLE queue SET (timescaledb.compress, timescaledb.compress_orderby = 'created_at DESC', timescaledb.compress_segmentby = 'id'); + +SELECT add_compression_policy('queue', compress_after => INTERVAL '14 days');