CREATE OR REPLACE FUNCTION get_track_messages(workflow bigint, start_time bigint, end_time bigint) RETURNS SETOF text LANGUAGE sql AS $func$ SELECT DISTINCT tm.proto_location FROM track_message tm WHERE tm.workflow_analytic_instance_id = $1 AND EXISTS ( SELECT FROM track_message_to_track_mapping tm2tm JOIN track t ON t.id = tm2tm.track_id JOIN track_item ti ON t.id = ti.track_id JOIN track_point tp ON ti.id = tp.track_item_id WHERE tm.id = tm2tm.track_message_id AND ti.item_time BETWEEN $2 AND $3 AND ST_Intersects (tp.track_position , ST_GeomFromText('POLYGON((-144 59, -41 46, -75 15, -127 25, -144 59))',4326)) ); $func$;
Most importantly, it would work as sql function.
For a PL/pgSQL function you'd need to change more:
CREATE OR REPLACE FUNCTION get_track_messages(workflow bigint , start_time bigint , end_time bigint) RETURNS SETOF text LANGUAGE plpqsql AS $func$ BEGIN RETURN QUERY SELECT DISTINCT ... ; END $func$;
If the query returns more than 1 row, you also need SETOF
(as you figured out yourself). Call the function with:
SELECT * FROM get_track_messages( ... );
More about returning from a function in the manual.
Data type
proto_location
is a character type. If you actually want to return an array of text (like your title states) you can aggregate like this:
SELECT array_agg(DISTINCT tm.proto_location) ...
And adapt the return type of the function to SETOF text[]
.
For a sorted array:
SELECT array_agg(DISTINCT tm.proto_location ORDER BY proto_location) ...
Or, to get a sorted list (text
) instead of an array (text[]
):
SELECT string_agg(DISTINCT tm.proto_location ORDER BY proto_location, ', ') ...
See: