Saludos a todos en la lista. Tengo una función en C programada que calcula un factorial (al menos eso creo) con la técnica de recursividad, es algo puramente de prueba para aprender sobre como programar en C para postgreSQL. Lo hice mirando el manual de PostgreSQL y tiene el código siguiente.
el archivo se llama otrasfunciones.c
#include "postgres.h"
#include "fmgr.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

PG_FUNCTION_INFO_V1(facto);
Datum
facto(PG_FUNCTION_ARGS)
{
int32 n = PG_GETARG_INT32(0);
int32 result;

  if (n==0)              // Caso base
     result = 1;
                       //Caso general
else                // Caso general
     result = n*facto(n-1);




 PG_RETURN_INT32(result);
}

lo compile y me dice esto:

/otrasfunciones.c: In function âfactoâ:
otrasfunciones.c:18: warning: passing argument 1 of âfactoâ makes pointer from integer without a cast otrasfunciones.c:9: note: expected âFunctionCallInfoâ but argument is of type âintâ/

Luego la creo dentro de PostgreSQL

/postgres=# create FUNCTION facto (integer) RETURNS integer as '/usr/local/pgsql/lib/otrasfunciones.so' ,'facto' LANGUAGE c STRICT;
CREATE FUNCTION/
luego la ejecuto para ver si funciona y sale todo ok
postgres=# select facto(0);
 facto
--------
      1
(1 row)


el problema viene cuando pongo otro valor que no sea 0
por ejemplo
/postgres=# select facto(2);/

me sale  lo siguiente  :-(

/server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed./

Será que no puede ejecutar para postgres algoritmos recursivos desde funciones implementadas en C y agregadas luego o debe hacerse usando una técnica especial y lo estoy haciendo mal?

Saludos

Responder a