1

Is there any similar thing like SQL Server's UDF (where a function can be exported in a DLL and imported as a function in SQL Server) in Postgres?

For example the function I want to import has the following signature:

public static extern double* svd(double[] a, int d);

the Dll import looks like this:

[DllImport("D:\\my.dll", EntryPoint = "mydll")]
7
  • possible duplicate of Call .NET function in external DLL from postgres Commented Feb 4, 2012 at 0:08
  • Are you trying to write a UDF in C# and use it in T-SQL code? If so, look at CLR Integration: msdn.microsoft.com/en-us/library/ms254498(v=vs.80).aspx Commented Feb 4, 2012 at 0:18
  • 1
    That's not a dupe. Please read the question. Commented Feb 4, 2012 at 0:22
  • This is not at all what the questioner wants. He wants to import a DLL into Postgres. Commented Feb 4, 2012 at 0:22
  • Yes what happens here is that i have a c code that is in a dll and in c# I am calling the extern functions, and finally I use the c# udf as an stored procedure, but I do not know what would be the approach in Postgres... Commented Feb 4, 2012 at 0:24

1 Answer 1

0

You can use the Create Function in Postgres:

CREATE FUNCTION svd(double*, int) RETURNS double*
AS 'c:/path/mydll.dll'
LANGUAGE 'C';

More info: http://www.postgresql.org/docs/current/static/xfunc-c.html

You'll likely need to use composite types since it appears you're manipulating matricies with variable numbers of rows and columns.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I am manipulating matrices, about the composite types can you give a little more detail?
@cMinor You can represent an n-dimensional matrix using an n-dimensional PostgreSQL array, so you won't have to implement your own data types. Just write a C function that accepts a double[][] array and returns another double[][] then map it into SQL using "CREATE FUNCTION". Read the PostgreSQL sources for examples - look up functions that take and/or return n-dimensional arrays and look at their C implementations. The documentation covers the basics of C functions well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.