IF (select count(*) from json_array_elements(_users)) > 0 THEN -- select * from dbo.cf_arm_cd_points(1, 5, 1, null, 4); raise notice '%', _users; -- обходчики выбраны и находим все маршруты за текущую дату RETURN QUERY select * from dbo.cf_arm_cd_points((select array_to_json(array_agg(row_to_json(t))) #>> '{id}' from (select r.id from core.cd_userinroutes as uir INNER JOIN core.cd_routes as r ON r.id = uir.f_route where uir.b_main = true and uir.f_user IN ( select (t.value #>> '{f_user}')::integer from json_array_elements(_users) as t ) and _d_date >= r.d_date_start and (core.cf_old_date(r.d_date_end) or core.cf_old_date(r.d_extended))) as t)::json, now()::date); END IF;