Skip to content

Instantly share code, notes, and snippets.

@up1
Last active October 2, 2025 13:10
Show Gist options
  • Save up1/2189e81d802848f959ddaab3417d511b to your computer and use it in GitHub Desktop.
Save up1/2189e81d802848f959ddaab3417d511b to your computer and use it in GitHub Desktop.
PostgreSQL with unlogged table
CREATE UNLOGGED TABLE cache_data (
id serial PRIMARY KEY,
key text UNIQUE NOT NULL,
value jsonb,
created_at timestamp);
CREATE INDEX idx_cache_key ON cache_data (key);
CREATE OR REPLACE PROCEDURE expire_rows (retention_period INTERVAL) AS
$$
BEGIN
DELETE FROM cache_data
WHERE created_at < NOW() - retention_period;
COMMIT;
END;
$$ LANGUAGE plpgsql;
--- Install pg_cron
CREATE EXTENSION pg_cron;
--- Create cron job to call expire_rows() :: every hourly
SELECT cron.schedule('0 * * * *', $$CALL expire_rows('1 hour');$$);
--- List all cron jobs
SELECT * FROM cron.job;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment