-- E-mail delivery by the worker: retries with backoff, permanent failure, -- and a notification so the worker sends new e-mails right away. ALTER TABLE email_outbox ADD COLUMN next_attempt_at timestamptz NOT NULL DEFAULT now(), ADD COLUMN failed_at timestamptz; DROP INDEX email_outbox_unsent_idx; CREATE INDEX email_outbox_due_idx ON email_outbox (next_attempt_at) WHERE sent_at IS NULL AND failed_at IS NULL; CREATE FUNCTION notify_email_outbox() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN PERFORM pg_notify('email_outbox', ''); RETURN NULL; END; $$; CREATE TRIGGER email_outbox_notify AFTER INSERT ON email_outbox FOR EACH STATEMENT EXECUTE FUNCTION notify_email_outbox();