@@ -234,22 +234,33 @@ def _create_refresh_function() -> str:
234234def _schedule_refresh_job () -> str :
235235 return f"""
236236 DO $do$
237- DECLARE
238- existing_job_id bigint;
239237 BEGIN
240- SELECT jobid INTO existing_job_id
241- FROM cron.job
242- WHERE jobname = '{ REFRESH_JOB_NAME } ';
243-
244- IF existing_job_id IS NOT NULL THEN
245- PERFORM cron.unschedule(existing_job_id);
246- END IF;
238+ BEGIN
239+ -- Avoid direct SELECT on cron.job because managed Postgres
240+ -- environments may deny access to the cron schema table.
241+ PERFORM cron.unschedule('{ REFRESH_JOB_NAME } ');
242+ EXCEPTION
243+ WHEN undefined_function THEN
244+ NULL;
245+ WHEN invalid_parameter_value THEN
246+ NULL;
247+ WHEN insufficient_privilege THEN
248+ RAISE NOTICE
249+ 'Skipping pg_cron unschedule for % due to insufficient privileges.',
250+ '{ REFRESH_JOB_NAME } ';
251+ RETURN;
252+ END;
247253
248254 PERFORM cron.schedule(
249255 '{ REFRESH_JOB_NAME } ',
250256 '{ REFRESH_SCHEDULE } ',
251257 $cmd$SELECT public.{ REFRESH_FUNCTION_NAME } ();$cmd$
252258 );
259+ EXCEPTION
260+ WHEN insufficient_privilege THEN
261+ RAISE NOTICE
262+ 'Skipping pg_cron schedule for % due to insufficient privileges.',
263+ '{ REFRESH_JOB_NAME } ';
253264 END
254265 $do$;
255266 """
@@ -258,20 +269,19 @@ def _schedule_refresh_job() -> str:
258269def _unschedule_refresh_job () -> str :
259270 return f"""
260271 DO $do$
261- DECLARE
262- existing_job_id bigint;
263272 BEGIN
264- IF to_regclass('cron.job') IS NULL THEN
265- RETURN;
266- END IF;
267-
268- SELECT jobid INTO existing_job_id
269- FROM cron.job
270- WHERE jobname = '{ REFRESH_JOB_NAME } ';
271-
272- IF existing_job_id IS NOT NULL THEN
273- PERFORM cron.unschedule(existing_job_id);
274- END IF;
273+ BEGIN
274+ PERFORM cron.unschedule('{ REFRESH_JOB_NAME } ');
275+ EXCEPTION
276+ WHEN undefined_function THEN
277+ NULL;
278+ WHEN invalid_parameter_value THEN
279+ NULL;
280+ WHEN insufficient_privilege THEN
281+ RAISE NOTICE
282+ 'Skipping pg_cron unschedule for % due to insufficient privileges.',
283+ '{ REFRESH_JOB_NAME } ';
284+ END;
275285 END
276286 $do$;
277287 """
0 commit comments