Last active
October 2, 2025 13:10
-
-
Save up1/2189e81d802848f959ddaab3417d511b to your computer and use it in GitHub Desktop.
PostgreSQL with unlogged table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- 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