Skip to content

feat: enhance refresh job scheduling with improved privilege handling…#551

Merged
jirhiker merged 2 commits intostagingfrom
jir-ogc
Feb 27, 2026
Merged

feat: enhance refresh job scheduling with improved privilege handling…#551
jirhiker merged 2 commits intostagingfrom
jir-ogc

Conversation

@jirhiker
Copy link
Copy Markdown
Member

… and error management

Why

This PR addresses the following problem / context:

  • Use bullet points here

How

Implementation summary - the following was changed / added / removed:

  • Use bullet points here

Notes

Any special considerations, workarounds, or follow-up work to note?

  • Use bullet points here

Copilot AI review requested due to automatic review settings February 27, 2026 15:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Alembic migration that creates/schedules the nightly pygeoapi materialized-view refresh job, aiming to work better in managed Postgres environments with restricted pg_cron privileges.

Changes:

  • Replaced direct SELECT access to cron.job with cron.unschedule(jobname) calls.
  • Added PL/pgSQL exception handling and NOTICE logging around pg_cron unschedule/schedule operations.

'{REFRESH_SCHEDULE}',
$cmd$SELECT public.{REFRESH_FUNCTION_NAME}();$cmd$
);
EXCEPTION
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cron.schedule(...) is only wrapped with an EXCEPTION WHEN insufficient_privilege handler. If scheduling fails for other pg_cron-related reasons that this migration is already trying to be resilient to (e.g., undefined_function when pg_cron functions aren’t available, or other server-state errors), the migration will still abort here. Consider extending the exception handling (or adding a pre-check) so the migration can safely continue when pg_cron scheduling isn’t possible.

Suggested change
EXCEPTION
EXCEPTION
WHEN undefined_function THEN
RAISE NOTICE
'Skipping pg_cron schedule for % because cron functions are not available.',
'{REFRESH_JOB_NAME}';
WHEN invalid_parameter_value THEN
RAISE NOTICE
'Skipping pg_cron schedule for % due to invalid parameter value.',
'{REFRESH_JOB_NAME}';
WHEN internal_error THEN
RAISE NOTICE
'Skipping pg_cron schedule for % due to internal pg_cron error.',
'{REFRESH_JOB_NAME}';

Copilot uses AI. Check for mistakes.
Comment on lines 234 to 236
def _schedule_refresh_job() -> str:
return f"""
DO $do$
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description is still the template (Why/How/Notes bullets aren’t filled in). Please update the description to summarize the motivation and approach so reviewers/operators can understand the intended behavior changes around pg_cron privilege/error handling.

Copilot uses AI. Check for mistakes.
Comment on lines +238 to 258
BEGIN
-- Avoid direct SELECT on cron.job because managed Postgres
-- environments may deny access to the cron schema table.
PERFORM cron.unschedule('{REFRESH_JOB_NAME}');
EXCEPTION
WHEN undefined_function THEN
NULL;
WHEN invalid_parameter_value THEN
NULL;
WHEN internal_error THEN
-- Some pg_cron builds raise internal_error when the named
-- job does not exist. Treat this as already-unscheduled.
NULL;
WHEN insufficient_privilege THEN
RAISE NOTICE
'Skipping pg_cron unschedule for % due to insufficient privileges.',
'{REFRESH_JOB_NAME}';
RETURN;
END;

PERFORM cron.schedule(
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In _schedule_refresh_job, the undefined_function branch for cron.unschedule('{REFRESH_JOB_NAME}') is swallowed and then the code proceeds to cron.schedule(...). In environments where cron.unschedule(text) is not available, this means any pre-existing job with the same name will not be removed by this migration, which can lead to inconsistent behavior when scheduling (e.g., an existing job remains unchanged). Consider either (a) falling back to the jobid-based unschedule when possible, or (b) skipping scheduling when unschedule cannot run, or (c) handling the “job already exists” case when calling cron.schedule.

Copilot uses AI. Check for mistakes.
@jirhiker jirhiker merged commit 2f97d25 into staging Feb 27, 2026
8 checks passed
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 43852fefb0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +280 to +283
WHEN insufficient_privilege THEN
RAISE NOTICE
'Skipping pg_cron unschedule for % due to insufficient privileges.',
'{REFRESH_JOB_NAME}';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Abort downgrade when pg_cron unschedule is unauthorized

Swallowing insufficient_privilege in the downgrade unschedule block allows the migration to continue and later drop public.refresh_pygeoapi_materialized_views(), even when the cron job could not be removed. In environments where the job is owned by a different role, this leaves the job active and it will keep firing a command that now points to a dropped function, causing recurring pg_cron execution errors after downgrade; previously this path failed fast instead of committing a broken partial teardown.

Useful? React with 👍 / 👎.

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