Skip to content

Instantly share code, notes, and snippets.

@woodRock
Last active June 3, 2023 09:19
Show Gist options
  • Save woodRock/fce672cd13d1dcb0ff3e954f2d2bc25c to your computer and use it in GitHub Desktop.
Save woodRock/fce672cd13d1dcb0ff3e954f2d2bc25c to your computer and use it in GitHub Desktop.
Postgres function to give Fibonacci Sequence in SQL.
create or replace function fib(n int)
returns setof int as $$
declare
a int := 0;
b int := 1;
begin
return next 0;
return next 1;
while a < n loop
a := a + b;
return next a;
b := b + a;
return next b;
end loop;
end;
$$ language plpgsql;
select * from fib(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment