0

I am trying to create a custom function in C in PostgreSQL.

The function should take a table in as an argument and also return a table as an output, after accessing the data from the table passed in.

The examples I have seen all involve passing in integers and other basic types. Is my request possible in PostgreSQL? How would I go about doing this?

4
  • you might look at postgresql.org/docs/8.2/static/xfunc-c.html May 30, 2014 at 17:15
  • I have already read this, but the examples only involve passing single primitive types parameter. Any suggestions about pass table parameters? May 30, 2014 at 18:39
  • 1
    It'd really be a good idea to describe what this function is supposed to actually do - describe the problem you're trying to solve, not just how to do what you think you need to solve it. Sometimes there's a completely different and better way to do it that you didn't know of and didn't think of, where you don't have to do the complicated thing you expect after all. Also, always include your PostgreSQL version in questions. May 31, 2014 at 2:29
  • Do not copy and paste questions between multiple stack overflow sites. Most people monitor tags across all of them anyway. It's a pain and a waste of time when you do this. Please delete the duplicate you created on dba.stackexchange.com (dba.stackexchange.com/q/66271/7788). May 31, 2014 at 2:30

1 Answer 1

1

Yes, it's possible.

The output side is simple, you just use RETURNS TABLE (...) or RETURNS SETOF record, depending on whether the result set has dynamic columns or not. Lots of examples exist in the source code, and IIRC some in the docs.

The input side is less simple. You need to either pass a refcursor to the function, pass a table schema and name, or pass a regclass oid that's the table's id in pg_class. Which to choose depends on how you need to use the function. If you want it to be able to consume the output of another function then only a refcursor will be usable. If you don't need that (or don't mind doing it via a temp table) using a regclass oid is otherwise much more convenient, as you don't have to create a cursor for a table and pass the cursor to the function, you just pass the table name or oid directly.

A few functions in PostgreSQL read tables as regclass and process them. The table_to_xml function in the sources is a good starting point for how to do this.

You're not likely to find a lot of examples of how to take a refcursor and process it. Your best starting point is the PostgreSQL source code. The cursor_to_xml and cursor_to_xmlschema functions consume refcursors.

Those functions are all defined in src/backend/utils/adt/xml.c. You can see there that the table_to_xml version just does a select * from the_table and passes the cursor to query_to_xml_internal to process. That shows how to read and consume rows.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.