0
create or replace function fibonacci(int) returns int 
as $$
declare
    a int;
    res int := 0;
begin
    a := $1;
    if(a = 1 or a = 2) then
        res :=1;
    else
        res := fibonacci(a - 2) + fibonacci(a - 1);
    end if;
    return res;
end;
language plpgsql;

The error message that shows and I do not see the error since I probe in several ways the same continues to show is a fibonacci sequence

ERROR:  una cadena separada por $ está inconclusa en o cerca de «$$
declare
    a int;
    res int := 0;
begin
    a := $1;
    if(a = 1 or a = 2) then
        res :=1;
    else
        res := fibonacci(a - 2) + fibonacci(a - 1);
    end if;
    return res;
end;
language plpgsql;»
LINE 3: as $$
           ^
SQL state: 42601
Character: 106
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228

1 Answers1

0

Sounds like the Italian version of:

ERROR: unterminated dollar-quoted string at or near "$$

That's because the closing dollar-quotes are missing:

create or replace function fibonacci(int) returns int 
as $$
declare
    a int;
    res int := 0;
begin
    a := $1;
    if(a = 1 or a = 2) then
        res :=1;
    else
        res := fibonacci(a - 2) + fibonacci(a - 1);
    end if;
    return res;
end;
$$                 -- !!!
language plpgsql;

See:

But you can largely simplify the PL/pgSQL function:

CREATE OR REPLACE FUNCTION fibonacci(int)
  RETURNS int
  LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE AS
$func$
BEGIN
   IF $1 IN (1,2) THEN
     RETURN 1;
   ELSE
     RETURN fibonacci($1 - 2) + fibonacci($1 - 1);
   END IF;
END
$func$;

Or use a simpler SQL function:

CREATE OR REPLACE FUNCTION fibonacci(int)
  RETURNS int
  LANGUAGE sql IMMUTABLE PARALLEL SAFE AS
$func$
SELECT CASE WHEN $1 IN (1,2)
            THEN 1
            ELSE fibonacci($1 - 2) + fibonacci($1 - 1) END;
$func$;
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228