C library function in Postgresql

2

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?

    
asked by anonymous 22.09.2016 / 21:31

1 answer

0

To resolve the problem:

  • Remove% with% of files;
  • Place extern "C" in .h;
  • Change PGDLLEXPORT Datum sysmoroundtoabnt(PG_FUNCTION_ARGS) by Double and Float8 by Int in .c;
  • I believe the main problem was the way I was exposing my method. The crash in Postgres was due to overuse of memory, which was caused when using native types instead of those suggested by the postgres documentation.

        
    27.09.2016 / 20:20