create or replace function get_all_prequils(filmid int)
returns table (id int, filmname varchar, prequelid int) as $$
with recursive rec as (
select f.FilmID, f.FilmName, f.PrequelID
from Film as f
where f.FilmID = filmid
union
select r.FilmID, r.FilmName, r.PrequelID
from rec as r
join Film f on f.PrequelID = r.FilmID
)
select *
from rec
$$ language sql;
select get_all_prequils(1);