Skip to content

Commit

Permalink
Select procedure based on argument type of cron_unschedule_named
Browse files Browse the repository at this point in the history
The citusdata#299 PR took care of bacward compatibility of the function, but
there is still one problem left. If extension isn't updated type of the
jobname field in the cron.job table is still NAME, and as a result scan
was failing to find rows and unschedule a named job.

This fix is relying on the fact that SQL API of the unschedule()
function is updated consistently with the cron.job table definition.
  • Loading branch information
CyberDem0n committed Aug 9, 2024
1 parent e5eb77e commit bd0196a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
12 changes: 12 additions & 0 deletions expected/pg_cron-test.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ SELECT extversion FROM pg_extension WHERE extname='pg_cron';
ALTER EXTENSION pg_cron UPDATE TO '1.4';
SELECT cron.unschedule(job_name := 'no_such_job');
ERROR: could not find valid entry for job 'no_such_job'
SELECT cron.schedule('testjob', '* * * * *', 'SELECT 1');
schedule
----------
1
(1 row)

SELECT cron.unschedule('testjob');
unschedule
------------
t
(1 row)

-- Test cache invalidation
DROP EXTENSION pg_cron;
CREATE EXTENSION pg_cron VERSION '1.4';
Expand Down
2 changes: 2 additions & 0 deletions sql/pg_cron-test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ SELECT extversion FROM pg_extension WHERE extname='pg_cron';
-- Test binary compatibility with v1.4 function signature.
ALTER EXTENSION pg_cron UPDATE TO '1.4';
SELECT cron.unschedule(job_name := 'no_such_job');
SELECT cron.schedule('testjob', '* * * * *', 'SELECT 1');
SELECT cron.unschedule('testjob');

-- Test cache invalidation
DROP EXTENSION pg_cron;
Expand Down
11 changes: 6 additions & 5 deletions src/job_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,9 @@ cron_unschedule(PG_FUNCTION_ARGS)
Datum
cron_unschedule_named(PG_FUNCTION_ARGS)
{
Datum jobNameDatum = 0;
Datum jobNameDatum = PG_GETARG_DATUM(0);
char *jobName = NULL;
RegProcedure procedure;

Oid userId = GetUserId();
char *userName = GetUserNameFromId(userId, false);
Expand All @@ -699,19 +700,19 @@ cron_unschedule_named(PG_FUNCTION_ARGS)
*/
if (get_fn_expr_argtype(fcinfo->flinfo, 0) == NAMEOID)
{
jobName = NameStr(*PG_GETARG_NAME(0));
jobNameDatum = CStringGetTextDatum(jobName);
procedure = F_NAMEEQ;
jobName = NameStr(*DatumGetName(jobNameDatum));
}
else
{
jobNameDatum = PG_GETARG_DATUM(0);
procedure = F_TEXTEQ;
jobName = TextDatumGetCString(jobNameDatum);
}

cronJobsTable = table_open(CronJobRelationId(), RowExclusiveLock);

ScanKeyInit(&scanKey[0], Anum_cron_job_jobname,
BTEqualStrategyNumber, F_TEXTEQ, jobNameDatum);
BTEqualStrategyNumber, procedure, jobNameDatum);
ScanKeyInit(&scanKey[1], Anum_cron_job_username,
BTEqualStrategyNumber, F_TEXTEQ, userNameDatum);

Expand Down

0 comments on commit bd0196a

Please sign in to comment.