Skip to content

Instantly share code, notes, and snippets.

@edib
Last active July 12, 2016 10:50
Show Gist options
  • Save edib/5906c59f33fc0e5897d4dcbb3052a959 to your computer and use it in GitHub Desktop.
Save edib/5906c59f33fc0e5897d4dcbb3052a959 to your computer and use it in GitHub Desktop.
returns fibonacci sequence in pl/pgsql
CREATE OR REPLACE FUNCTION fibonacci(num integer)
RETURNS SETOF numeric AS $$
DECLARE
a numeric := 0;
b numeric := 1;
BEGIN
IF (num <= 0)
THEN RETURN;
END IF;
RETURN NEXT a;
LOOP
EXIT WHEN num <= 1;
RETURN NEXT b;
num = num - 1;
SELECT b, a + b INTO a, b;
END LOOP;
END;
$$ language plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment