postgresqltimescaledb

Registered job in TimescalDB calls incorrect user function


I have user function:

CREATE FUNCTION public.some_func(arg1 timestamp without time zone DEFAULT NULL, arg2 
BOOLEAN DEFAULT FALSE)
 RETURNS text
 ...

In timescaledb(2.15.3) I registered job as:

SELECT add_job('public.some_func'::REGPROC,'1 DAY'::INTERVAL, initial_start => '2024-09-22 08:00:00+00'::timestamptz, timezone => 'posix/Universal');

Job does not work and some_func is not calling daily, and I see error logs in timescaledb_information.job_errors DB, here err_message shows: function public.some_func(integer, jsonb) does not exist

I don't get why timescale tries to call public.some_func function with integer, jsonb parameters? from where comes this parameters and why is not called public.some_func(timestamp, bool) instead?


Solution

  • This part in the doc Use Timescale > User-defined actions > About user-defined actions:

    Use this code to create the function:

    CREATE FUNCTION totalRecords (job_id INT DEFAULT NULL, config JSONB DEFAULT NULL)
    RETURNS integer AS $total$
    declare
    total integer;
    BEGIN
      SELECT count(*) into total FROM fill_measurements;
      RETURN total;
    END;
    $total$ LANGUAGE plpgsql;
    

    Isn't just an example, it's a mandatory template you need to follow if you plan to register the routine as scheduled job. As long as they have defaults, you might get away with listing additional parameters beyond these two but I'm not sure how you'd go about passing them in.

    You can either write a wrapper or change your function directly, so that it expects those arguments to be passed inside the config jsonb parameter. Working off of one of their examples:

    CREATE FUNCTION public.some_func(
        job_id int default null
      , config jsonb default '{"arg1":null,"arg2":false}'::jsonb)
    RETURNS text LANGUAGE plpgsql AS $f$
    DECLARE
      arg1 timestamp without timezone;
      arg2 boolean;
    BEGIN
      SELECT jsonb_object_field_text(config, 'arg1')::timestamp INTO STRICT arg1;
      SELECT jsonb_object_field_text(config, 'arg2')::boolean INTO STRICT arg2;
      RAISE NOTICE 'arg1:%; arg2:%', arg1::text,arg2::text;
      RETURN format('arg1:%; arg2:%', arg1::text,arg2::text);
    END $f$;
    
    SELECT add_job(  'public.some_func'::REGPROC
                   , '1 DAY'::INTERVAL
                   , initial_start => '2024-09-22 08:00:00+00'::timestamptz
                   , timezone => 'posix/Universal'
                   , config => '{ "arg1":"2011-11-11 11:11:11-11"
                                 ,"arg2":true}'::jsonb);