Skip to content

Conversation

@jumski
Copy link
Contributor

@jumski jumski commented Dec 5, 2025

Add cleanup function for cron job run details

This PR adds a new cleanup_ensure_workers_logs function to prevent the cron.job_run_details table from growing indefinitely. The function:

  • Deletes cron job run details older than a configurable retention period (default 24 hours)
  • Returns the count of deleted records
  • Is implemented as a security definer function with appropriate search path settings
  • Includes comprehensive documentation explaining its purpose and behavior

The PR includes:

  • SQL schema definition for the function
  • TypeScript type definitions for the function's arguments and return values
  • Migration file for Supabase deployment
  • Two test files to verify the function works correctly:
    • Basic cleanup functionality with custom retention period
    • Proper handling when no records need deletion

Note that HTTP response logs (net._http_response) are not cleaned by this function as they are automatically managed by pg_net with a 6-hour TTL.

@changeset-bot
Copy link

changeset-bot bot commented Dec 5, 2025

⚠️ No Changeset found

Latest commit: 19991aa

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor Author

jumski commented Dec 5, 2025

@nx-cloud
Copy link

nx-cloud bot commented Dec 5, 2025

View your CI Pipeline Execution ↗ for commit 19991aa

Command Status Duration Result
nx run client:e2e ✅ Succeeded 1m 9s View ↗
nx run edge-worker:test:integration ✅ Succeeded 3m 46s View ↗
nx run cli:e2e ✅ Succeeded 3s View ↗
nx run core:pgtap ✅ Succeeded 1m 40s View ↗
nx affected -t verify-exports --base=origin/mai... ✅ Succeeded 3s View ↗
nx affected -t build --configuration=production... ✅ Succeeded 30s View ↗
nx affected -t lint typecheck test --parallel -... ✅ Succeeded 1m 43s View ↗
nx run edge-worker:e2e ✅ Succeeded 46s View ↗

☁️ Nx Cloud last updated this comment at 2025-12-07 16:56:58 UTC

Comment on lines +7 to +21
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
$$;
Copy link
Contributor

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_hours parameter. Passing a negative value or zero could cause catastrophic data loss:

  • Negative value: retention_hours => -5 creates the condition end_time < now() + 5 hours, deleting records including those with recent or future end times
  • Zero: retention_hours => 0 creates end_time < now(), deleting ALL completed job records

Fix by adding validation at the start:

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
      and cleanup_ensure_workers_logs.retention_hours > 0  -- Add validation
    returning 1
  )
  select count(*)::bigint as cron_deleted from deleted
$$;

Or raise an exception for invalid input before the DELETE.

Suggested change
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
$$;
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
and cleanup_ensure_workers_logs.retention_hours > 0
returning 1
)
select count(*)::bigint as cron_deleted from deleted
$$;

Spotted by Graphite Agent

Fix in Graphite


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

@jumski jumski force-pushed the 12-04-add_ensure_workers_sql_function_for_worker_invocation_orchestration branch from 7bc511b to c298fab Compare December 5, 2025 22:53
@jumski jumski force-pushed the 12-05-add_cleanup_ensure_workers_logs_sql_function_for_cron_job_log_maintenance branch from 6232598 to ac3a640 Compare December 5, 2025 22:53
@jumski jumski force-pushed the 12-04-add_ensure_workers_sql_function_for_worker_invocation_orchestration branch from c298fab to 6d218a6 Compare December 7, 2025 00:32
@jumski jumski force-pushed the 12-05-add_cleanup_ensure_workers_logs_sql_function_for_cron_job_log_maintenance branch from ac3a640 to 28eab2e Compare December 7, 2025 00:32
@jumski jumski force-pushed the 12-04-add_ensure_workers_sql_function_for_worker_invocation_orchestration branch from 6d218a6 to 923cbfb Compare December 7, 2025 16:45
@jumski jumski force-pushed the 12-05-add_cleanup_ensure_workers_logs_sql_function_for_cron_job_log_maintenance branch from 28eab2e to 19991aa Compare December 7, 2025 16:45
@github-actions
Copy link
Contributor

github-actions bot commented Dec 7, 2025

🔍 Preview Deployment: Website

Deployment successful!

🔗 Preview URL: https://pr-511.pgflow.pages.dev

📝 Details:

  • Branch: 12-05-add_cleanup_ensure_workers_logs_sql_function_for_cron_job_log_maintenance
  • Commit: aa780dd8d7856514d47fbff00e560f971656aff7
  • View Logs

_Last updated: _

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants