Friday, February 4, 2011

SQL/PSM runtime

I enhanced a PLPSM executor engine to show a more informations when exception is raised. Now PLPSM has same and maybe better error processing than PL/pgSQL. - Almost all code coming from PL/pgSQL. Still, there are lot of work - mainly about exception trapping.
pavel=# \sf fib
CREATE OR REPLACE FUNCTION public.fib(n integer)
 RETURNS double precision
 LANGUAGE psm0
AS $function$xxx:begin
  declare f1 double precision default 0.0;
  declare f2 double precision default 1.0;
  declare cnt int default 1;
  while cnt <= n do
    set (f2, f1, cnt) = (f1 + f2, f2, cnt + 1);
  end while;
  return f2;
end;
$function$
pavel=# select fib(1000);
          fib          
───────────────────────
 7.03303677114228e+208
(1 row)

Time: 16.680 ms
pavel=# select fib(2000);
ERROR:  value out of range: overflow
CONTEXT:  SQL statement "SELECT ($2 + $3)::double precision,($3)::double precision,($4 + 1)::integer"
PLPSM function "fib"  Oid 604602 line 6

   4   declare cnt int default 1;
   5   while cnt <= n do
   6     set (f2, f1, cnt) = (f1 + f2, f2, cnt + 1);
   7   end while;
   8   return f2;

Local variables:

   0 fib.$1  integer              = 2000
   0 fib.n  integer              = 2000
   1 xxx.f1  double precision     = 8.07763763215622e+307
   2 xxx.f2  double precision     = 1.3069892237634e+308
   3 xxx.cnt  integer              = 1476