drop type gebliste cascade;
create type gebliste as (datum date, vorname varchar, nachname varchar, tage int, alter int);

create or replace function geb(int) returns setof gebliste as $$
declare
	ziel	date;
	rec	gebliste;
begin
	ziel := CURRENT_DATE + ($1 || ' days')::interval;


	
	for rec in select *, 0, extract(year from age(ziel, datum))  from geburtstage where 
		(date_part('year',  CURRENT_DATE) || '/' || date_part('month', datum) || '/' || date_part('day', datum))::date between CURRENT_DATE and ziel
		or
		(date_part('year',  ziel) || '/' || date_part('month', datum) || '/' || date_part('day', datum))::date between CURRENT_DATE and ziel
		loop
		return next rec;
	end loop;
	return;
	
end;
$$ language plpgsql;


-- Hint from casay, #postgresql:
-- where extract (year from age (birthdate)) < extract (year from age (current_date+10, birthdate))
--
