Skip to content

Instantly share code, notes, and snippets.

@up1
Last active October 2, 2025 13:10
Show Gist options
  • Select an option

  • Save up1/2189e81d802848f959ddaab3417d511b to your computer and use it in GitHub Desktop.

Select an option

Save up1/2189e81d802848f959ddaab3417d511b to your computer and use it in GitHub Desktop.

Revisions

  1. up1 revised this gist Oct 2, 2025. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions 3.sql
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    --- 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');$$);

  2. up1 revised this gist Oct 2, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 3.sql
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    --- Create cron job to call expire_rows() :: every a hour
    --- Create cron job to call expire_rows() :: every hourly
    SELECT cron.schedule('0 * * * *', $$CALL expire_rows('1 hour');$$);

    --- List all cron jobs
  3. up1 revised this gist Oct 2, 2025. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions 3.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    --- Create cron job to call expire_rows() :: every a hour
    SELECT cron.schedule('0 * * * *', $$CALL expire_rows('1 hour');$$);

    --- List all cron jobs
    SELECT * FROM cron.job;
  4. up1 revised this gist Oct 2, 2025. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions 2.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    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;
  5. up1 revised this gist Oct 2, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1.sql
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,6 @@ CREATE UNLOGGED TABLE cache_data (
    id serial PRIMARY KEY,
    key text UNIQUE NOT NULL,
    value jsonb,
    inserted_at timestamp);
    created_at timestamp);

    CREATE INDEX idx_cache_key ON cache_data (key);
  6. up1 created this gist Oct 2, 2025.
    7 changes: 7 additions & 0 deletions 1.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    CREATE UNLOGGED TABLE cache_data (
    id serial PRIMARY KEY,
    key text UNIQUE NOT NULL,
    value jsonb,
    inserted_at timestamp);

    CREATE INDEX idx_cache_key ON cache_data (key);