Hello everyone
I am trying to integrate two server side test functions with Postgres the first one, nimAddOne (int): int works fine integrate it like this:
# AddOne adds one to the given arg and retuns it
proc nimAddOne (i: cint): cint {.exportc.} =
result = i + 1
I made the call from C this way:
#include "postgres.h"
#include "fmgr.h"
#undef HAVE_STDINT_H
#include "converter.h"
PG_FUNCTION_INFO_V1 (c_addone);
Datum
c_addone (PG_FUNCTION_ARGS)
{
int32 arg = PG_GETARG_INT32 (0);
int32 result = nimAddOne (arg);
PG_RETURN_INT32 (result);
}
But when I pass strings to another function like this:
# ConcatName concat name and last name
proc nimConcatName (name, last: cstring): cstring {.exportc.} =
var r: cstring
r = $ name & "" & $ last
result = r
And calling it from C like this
#include "postgres.h"
#include "fmgr.h"
#include "utilities.h"
#undef HAVE_STDINT_H
#include "converter.h"
PG_FUNCTION_INFO_V1 (c_fullname);
Datum
c_fullname (PG_FUNCTION_ARGS)
{
text * ptrName = PG_GETARG_TEXT_PP (0);
text * ptrLast = PG_GETARG_TEXT_PP (1);
const char * name = GetStringFromText (ptrName);
const char * last = GetStringFromText (ptrLast);
char * result = nimConcatName ((char *) name, (char *) last);
elog (INFO, "% s", result);
text * ptrResult = (text *) GetTextFromString (result);
PG_RETURN_TEXT_P (ptrResult);
}
Postgres server goes down hopelessly
Could you tell me what I do wrong in the second function?
Thank you!
NOTES: "converter.h" contains the base NIM types after compiling the "converter.nim" file that contains both functions
Do you compile to shared library? Things to check: You do call NimMain or setupForeignThreadGC.
Another thing I recommend trying is to compile with --gc:arc -d:useMalloc this gives you garbage collection free environment that will ease integration with postgres.
Hello, thanks for answering
I compile and link in this way
$ (MY_LIB_NIM): * .nim Makefile
nim c -d: release --passC: -fPIC --noMain --opt: speed --app: staticlib --outdir: $ (NIM_OUTDIR) --nimcache: $ (NIM_CACHEDIR) --out: $ (basename $ < ) .a --header: $ (basename $ <). h $ <
% .o:% .c Makefile
gcc -std = c11 -fPIC -O2 -D_GNU_SOURCE -c $ <-I $ (PG_SERVER_INC) -I $ (NIM_INCLUDE_BASE) -I $ (NIM_INCLUDE_LOCAL)
$ (MY_LIB_GCC): $ (MY_OBJS)
gcc -shared -fPIC -O2 $ ^ $ (MY_LIB_NIM) -o $ @
I will test with the parameters you indicateI feel like I'm faster, is true
Good news:
It worked perfectly !!
Thanks to both!