Problem with postgres Function
Change gap datatype from interval to text and enclose it between single quotes in your dynamic query.
create function test (gap text)
returns text
as $ftest$
declare
    cmd text;
begin
    cmd := 'select ''' ||  gap || '''::interval';
    return cmd;
end
$ftest$ language plpgsql 

select test('5 minute');

test                        
:--------------------------
select '5 minute'::interval


select '5 minute'::interval

| interval |
| :------- |
| 00:05:00 |

db&lt;&gt;fiddle here