CREATE OR REPLACE FUNCTION working_days(start_date date, end_date date) RETURNS integer AS
$BODY$
declare  
  days int := 0;
begin
  days = (select count(1)
	        from (select(start_date+s.a*'1 day'::interval) AS dia 
		           from generate_series(0, end_date - start_date, 1) AS s(a)) days 
           where extract(DOW from dia) between 1 and 5);
  return days;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION working_days(date, date) OWNER TO postgres;