I created a custom function in pl/pgsql and it's only input is an array. I would like to use the result of a query as the input for this custom function, but can't seem to get it right. I'll use a simpler function as an example and also a simpler query than the one I'm trying to use as input.
Function definition:
CREATE OR REPLACE FUNCTION bigger_than_ones(input_array int[])
returns SETOF int
AS
$$
DECLARE
array_item int;
BEGIN
FOREACH array_item in ARRAY input_array
LOOP
IF array_item > 1 THEN
RETURN NEXT array_item;
END IF;
END LOOP;
END
$$ LANGUAGE plpgsql;
I have a table that has a column the same datatype of the array datatype. In this case an integer column:
SELECT * FROM my_table;
id | int_attribute
---+--------------
1 | 2
2 | 3
3 | 1
4 | 4
5 | 1
6 | 6
7 | 1
8 | 1
9 | 8
I would like to use a query that returns a column of that datatype as the input for my function. I've tried a few variations of this, without any luck:
SELECT * FROM bigger_than_ones(SELECT int_attribute FROM my_table);
To return:
result
-------------
2
3
4
6
8
The reason I'm using a similarly structured function is because I need to calculate resulting merged time ranges (custom type) from multiple tables, in multiple places in my code. My best thought was to create a function that would do that, using as input all the time ranges, regardless of where I took it from, so I wouldn't have to make a more complex query each time.
Any thoughts on what I'm missing?