I'm using Visual Studio 2015 to create a function in a dll, and when I try to use it in Postgres, I get the following message:
server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.
Follow the code:
C:
.c file:
#include<iostream>
#include<math.h>
extern "C" {
#include "postgres.h"
#include "fmgr.h"
#include "sysmoroundtoabnt.h"
#include "utils/builtins.h"
#include "catalog/pg_type.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
}
PG_FUNCTION_INFO_V1(sysmoroundtoabnt);
Datum
sysmoroundtoabnt(PG_FUNCTION_ARGS)
{
PG_RETURN_INT32(10);
//PG_RETURN_FLOAT8(10);
//...
}
.h file
#ifndef SYSMOROUNDTOABNT_H
#define SYSMOROUNDTOABNT_H
#include "fmgr.h"
extern "C" __declspec(dllimport) Datum sysmoroundtoabnt(PG_FUNCTION_ARGS);
#endif
Postgresql:
create or replace function teste_victor( double precision, integer )
returns double precision
as '$libdir/sysmoroundtoabnt', 'sysmoroundtoabnt' language C;
SQL Command
select teste_victor(0.015, -2)
PS: If I change the code in C, to return PG_RETURN_FLOAT8 (10), the error becomes this:
invalid memory alloc request size 4294967290
Another important information is that the error only happens in Windows. For Linux it works perfectly, I just need to remove the extern
from the files .c
and .h
What's wrong with my code?