I have a C function extension libray for PostgreSQL (v8.4). The library contains a function foo() with the following signature:
double foo(const double* values, const size_t len);
I want to pass columnar data for a specific table in a postgresql db (v 8.4) to the C function, by using an "aggregate" function agg().
Assuming I am using the employees table in my db, with the following columns in the employees table: id, name, salary.
I want to pass an array of all of the salaries as a float8[] to my C function - i.e. the data I want to pass to the C function is that which is printed to the console when I type select salary from employees
I discovered the array_agg() function and thought that I could use it to return a float8[] to my C function (via an adapter), so that I could execute a statement like:
select foo(array_agg(salary)) from employees;
however when I tried this I realised I was getting incorrect results.
I then run SELECT array_agg(wages) from employees and was suprised to find that instead of a simple array of numbers, a lot of blank lines were printed to the console - along with what a whole bunch of numbers "squeezed" into one array toward the end of the printout. Needless to say, the data in the table (salary column) does not accept NULL values, so I don't understand what array_agg() is returning to the screen.
What I need to find, is a way to create a function (aggregate or otherwise) that returns data equivalent to "select [columnname] from tablename" as an array of floats.
I have spent most of the day searching online and the postgres documentation - and I have not as yet found a useful example of creating user defined aggregate functions or anything remotely useful for helping solve this problem.
I'd be grateful for any help in writing a function that returns the values in a column (of a table or query), so that I pass those values to a C function.
array_agg()does exactly what you are looking for. There must be a misunderstanding. Your examples are inconclusive.