I've written a SQL query that I want to wrap in a function. It collects data between two timestamps and returns it to a user. Using the provided timestamps slows down the function incredibly while using the same timestamps, hardcoded into the query, acts fine.
Any ideas what could possibly be happening?
First function:
CREATE OR REPLACE FUNCTION my_variable_func(
id text,
start_time timestamp with time zone DEFAULT '2022-05-24 07:10:00',
end_time timestamp with time zone DEFAULT '2022-05-24 07:20:00')
RETURNS TABLE(... some stuff...)
LANGUAGE 'sql'
COST 100
VOLATILE PARALLEL SAFE
ROWS 1000
AS $BODY$
SELECT
... some complex CTE + window query + aggregate ...
WHERE event_time > $2::timestamp with time zone
AND event_time < $3::timestamp with time zone
Fast hardcoded query:
CREATE OR REPLACE FUNCTION my_hardcoded_func(
id text,
start_time timestamp with time zone DEFAULT '2022-05-24 07:10:00',
end_time timestamp with time zone DEFAULT '2022-05-24 07:20:00')
RETURNS TABLE(... some stuff...)
LANGUAGE 'sql'
COST 100
VOLATILE PARALLEL SAFE
ROWS 1000
AS $BODY$
SELECT
... some complex CTE + window query + aggregate ...
WHERE event_time > '2022-05-24 07:10:00'::timestamp with time zone
AND event_time < '2022-05-24 07:20:00'::timestamp with time zone
Running
SELECT * FROM my_variable_func(123, '2022-05-24 07:10:00','2022-05-24 07:20:00')
completes in 30 sec while
SELECT * FROM my_hardcoded_func(123, '2022-05-24 07:10:00','2022-05-24 07:20:00')
takes under 2 secs. Any ideas? I'm stumped...
EDIT
After some more work: Taking a look at the locks on the database; it looks like the "hardcoded" function fetches data from the correct partitions and the "variable" function searches through all partitions to get the data that it requires, which is massively slow. I'm not sure what has changed since it's taking MUCH longer than 30 seconds to run the variable function. I've tried to force index use with "set enable_seqscan = off;" without success.
some complex CTE