How to add a field in the RETURNS TABLE of a plpgpsql function, the value I want to add is not present in the SELECT performed inside the function :
I have this function that samples the sum of cpu and ram used by jobs every 'step' seconds between start_date and end_date (epoch timestamp):
CREATE OR REPLACE FUNCTION resources(start_date INTEGER, end_date INTEGER, step INTEGER)
RETURNS TABLE (
cpu bigint
, ram bigint) AS $$
DECLARE
counter INTEGER := 0 ;
BEGIN
counter := start_date;
LOOP
EXIT WHEN counter > end_date ;
RETURN QUERY EXECUTE
'SELECT sum(j_ram) as ram, sum(j_cpu) as cpu from jobs where j_start_time <= ' || counter || ' and j_end_time >= ' || counter;
counter := counter + step ;
END LOOP ;
END;
$$ LANGUAGE plpgsql;
The outputed results is something like :
cpu | ram
-------+------
0 |
6 | 12000
6 | 11000
1 | 1000
How to add the value of the variable 'counter' in the table to have something like :
cpu | ram | counter
-------+-------+--------
0 | | 100
6 | 12000 | 110
6 | 11000 | 120
1 | 1000 | 130