Skip to content

Commit

Permalink
fix(voyager): better db structure
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-aitlahcen committed May 28, 2024
1 parent 38b5c93 commit e0c827a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/pg-queue/migrations/20240316012044_queue.sql
Original file line number Diff line number Diff line change
@@ -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');

0 comments on commit e0c827a

Please sign in to comment.