Call .NET function in external DLL from postgres

2019-08-22 01:07发布

问题:

It's possible to call C functions through CREATE FUNCTION but how can .NET functions in dll's be called from postgres?

回答1:

You can define an external function using CREATE FUNCTION. This reference implies that the DLL must be a native DLL using the C calling convention and hence cannot handle managed code.

You could try to use Mono AOT or MS Native Image Generator to compile the managed code to native code, but I cannot tell if this will result in a native DLL using C calling convention. You have to try.

If this does not work, you could create an unmanged wrapper for your DLL.

UPDATE

You have to write a wrapper - C-Language Functions states that the DLL must include a magic block that will not be availiable if you compile managed code to native code.



回答2:

It looks like there is some overhead to calling C functions from PosgtreSQL, External Functions in Postgres @ Linux Gazette covers writing such a method in C. I would surmise from that that you could, if you REALLY need to, build a hosting module that loads a CLR instance and provides an entry point into your C# method that is mapped with the appropriate Postgres linking logic. This seems extremely costly.

Another approach that might be better would be to encapsulate the C# method (which I am assuming is already written) in a web service, and use a wrapper (not necessarily C, but one of the lighter-weight extension development bindings for Postgres) that will call out to the web service.