-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add cleanup function for cron job run details #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jumski
merged 1 commit into
main
from
12-05-add_cleanup_ensure_workers_logs_sql_function_for_cron_job_log_maintenance
Dec 8, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
pkgs/core/schemas/0060_function_cleanup_ensure_workers_logs.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| -- Cleanup Ensure Workers Logs | ||
| -- Cleans up old cron job run details to prevent the table from growing indefinitely. | ||
| -- Note: net._http_response is automatically cleaned by pg_net (6 hour TTL), so we only clean cron logs. | ||
|
|
||
| drop function if exists pgflow.cleanup_ensure_workers_logs(integer); | ||
|
|
||
| create or replace function pgflow.cleanup_ensure_workers_logs( | ||
| retention_hours integer default 24 | ||
| ) | ||
| returns table (cron_deleted bigint) | ||
| language sql | ||
| security definer | ||
| set search_path = pgflow, cron, pg_temp | ||
| as $$ | ||
| with deleted as ( | ||
| delete from cron.job_run_details | ||
| where job_run_details.end_time < now() - (cleanup_ensure_workers_logs.retention_hours || ' hours')::interval | ||
| returning 1 | ||
| ) | ||
| select count(*)::bigint as cron_deleted from deleted | ||
| $$; | ||
|
|
||
| comment on function pgflow.cleanup_ensure_workers_logs(integer) is | ||
| 'Cleans up old cron job run details to prevent table growth. | ||
| Default retention is 24 hours. HTTP response logs (net._http_response) are | ||
| automatically cleaned by pg_net with a 6-hour TTL, so they are not cleaned here. | ||
| This function follows the standard pg_cron maintenance pattern recommended by | ||
| AWS RDS, Neon, and Supabase documentation.'; | ||
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
15 changes: 15 additions & 0 deletions
15
pkgs/core/supabase/migrations/20251205140756_pgflow_temp_cleanup_ensure_workers_logs.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| -- Create "cleanup_ensure_workers_logs" function | ||
| CREATE FUNCTION "pgflow"."cleanup_ensure_workers_logs" ("retention_hours" integer DEFAULT 24) RETURNS TABLE ("cron_deleted" bigint) LANGUAGE sql SECURITY DEFINER SET "search_path" = pgflow, cron, pg_temp AS $$ | ||
| with deleted as ( | ||
| delete from cron.job_run_details | ||
| where job_run_details.end_time < now() - (cleanup_ensure_workers_logs.retention_hours || ' hours')::interval | ||
| returning 1 | ||
| ) | ||
| select count(*)::bigint as cron_deleted from deleted | ||
| $$; | ||
| -- Set comment to function: "cleanup_ensure_workers_logs" | ||
| COMMENT ON FUNCTION "pgflow"."cleanup_ensure_workers_logs" IS 'Cleans up old cron job run details to prevent table growth. | ||
| Default retention is 24 hours. HTTP response logs (net._http_response) are | ||
| automatically cleaned by pg_net with a 6-hour TTL, so they are not cleaned here. | ||
| This function follows the standard pg_cron maintenance pattern recommended by | ||
| AWS RDS, Neon, and Supabase documentation.'; |
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
42 changes: 42 additions & 0 deletions
42
pkgs/core/supabase/tests/cleanup_ensure_workers_logs/basic_cleanup.test.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| -- Test: cleanup_ensure_workers_logs() deletes old entries and returns correct count | ||
| begin; | ||
| select plan(3); | ||
| select pgflow_tests.reset_db(); | ||
|
|
||
| -- Clear existing cron job run details | ||
| delete from cron.job_run_details; | ||
|
|
||
| -- Setup: Create job run details at different ages (testing 4h custom retention) | ||
| insert into cron.job_run_details (runid, jobid, command, status, end_time) | ||
| values | ||
| (1, 1, 'select some_function()', 'succeeded', now() - interval '5 hours'), -- Should be deleted | ||
| (2, 1, 'select some_function()', 'succeeded', now() - interval '6 hours'), -- Should be deleted | ||
| (3, 1, 'select some_function()', 'succeeded', now() - interval '3 hours'), -- Should be kept | ||
| (4, 1, 'select some_function()', 'succeeded', now() - interval '1 hour'); -- Should be kept | ||
|
|
||
| -- Execute cleanup with 4 hour retention | ||
| with result as ( | ||
| select * from pgflow.cleanup_ensure_workers_logs(retention_hours => 4) | ||
| ) | ||
| select is( | ||
| (select cron_deleted from result), | ||
| 2::bigint, | ||
| 'Should return count of 2 deleted entries' | ||
| ); | ||
|
|
||
| -- Test: Old entries were deleted | ||
| select is( | ||
| (select count(*) from cron.job_run_details where runid in (1, 2)), | ||
| 0::bigint, | ||
| 'Entries older than retention should be deleted' | ||
| ); | ||
|
|
||
| -- Test: Recent entries were kept | ||
| select is( | ||
| (select count(*) from cron.job_run_details where runid in (3, 4)), | ||
| 2::bigint, | ||
| 'Entries newer than retention should be kept' | ||
| ); | ||
|
|
||
| select finish(); | ||
| rollback; |
26 changes: 26 additions & 0 deletions
26
...e/supabase/tests/cleanup_ensure_workers_logs/returns_zero_when_nothing_to_delete.test.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| -- Test: cleanup_ensure_workers_logs() returns 0 when no old entries exist | ||
| begin; | ||
| select plan(1); | ||
| select pgflow_tests.reset_db(); | ||
|
|
||
| -- Clear existing cron job run details | ||
| delete from cron.job_run_details; | ||
|
|
||
| -- Setup: Only recent entries exist | ||
| insert into cron.job_run_details (runid, jobid, command, status, end_time) | ||
| values | ||
| (1, 1, 'select some_function()', 'succeeded', now() - interval '1 hour'), | ||
| (2, 1, 'select some_function()', 'succeeded', now() - interval '2 hours'); | ||
|
|
||
| -- Execute cleanup - nothing should be deleted | ||
| with result as ( | ||
| select * from pgflow.cleanup_ensure_workers_logs() | ||
| ) | ||
| select is( | ||
| (select cron_deleted from result), | ||
| 0::bigint, | ||
| 'Should return 0 when no entries exceed retention period' | ||
| ); | ||
|
|
||
| select finish(); | ||
| rollback; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function lacks validation for the
retention_hoursparameter. Passing a negative value or zero could cause catastrophic data loss:retention_hours => -5creates the conditionend_time < now() + 5 hours, deleting records including those with recent or future end timesretention_hours => 0createsend_time < now(), deleting ALL completed job recordsFix by adding validation at the start:
Or raise an exception for invalid input before the DELETE.
Spotted by Graphite Agent

Is this helpful? React 👍 or 👎 to let us know.