Skip to content

Commit

Permalink
Reorder tasks columns to optimize row byte alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewmeyer committed Nov 28, 2023
1 parent 4eb5c0f commit fcbbd4e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions migrations/0002_create_tasks.up.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
CREATE TABLE tasks(
id UUID NOT NULL PRIMARY KEY,
type TEXT NOT NULL,
priority INT NOT NULL DEFAULT 0,
payload JSONB NOT NULL,
created TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(type, payload)
id BIGINT PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY,
state SMALLINT NOT NULL DEFAULT 0,
priority SMALLINT NOT NULL DEFAULT 100 CHECK (priority >= 0),
attempt SMALLINT NOT NULL DEFAULT 0 CHECK (attempt >= 0),
max_attempts SMALLINT NOT NULL CHECK (max_attempts > 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
attempted_at TIMESTAMPTZ,
scheduled_at TIMESTAMPTZ,
queue TEXT NOT NULL DEFAULT 'main' CHECK (char_length(queue) > 0 AND char_length(queue) < 128),
name TEXT NOT NULL CHECK (char_length(name) > 0 AND char_length(name) < 128),
payload JSONB NOT NULL
);
CREATE INDEX tasks_priority_idx ON tasks(priority);

CREATE UNIQUE INDEX tasks_row_unique_idx ON tasks USING btree(name, payload);
CREATE INDEX tasks_fetch_idx ON tasks USING btree(state, queue, priority, scheduled_at, id);

0 comments on commit fcbbd4e

Please sign in to comment.