I have a PostgreSQL 11x environment in Windows. I am trying to figure out syntax for a Function which returns results of a query. The first query in the code below works much better most of the time but if it fails to return something then I'd like to use the second query's return--which is much slower but at least has some data being returned. Here is my Function:
CREATE OR REPLACE FUNCTION public.myfunction()
RETURNS TABLE(start_vid bigint, end_vid bigint, agg_cost double precision)
LANGUAGE plpgsql
AS $function$
DECLARE
--declarations
BEGIN
RETURN QUERY
SELECT * FROM pgr_dijkstraCost(
'SELECT gid as id, * FROM ways as r,
(SELECT ST_Expand(ST_Extent(the_geom), ' || box_size || ') as box FROM ways as edge_table
WHERE edge_table.source = ' || VT1 || ' OR edge_table.target = ' || VT2 || ') as box
WHERE r.the_geom && box.box',
VT1, VT2, arg_directed_flag::boolean
RETURN QUERY
SELECT * FROM pgr_dijkstraCost( 'SELECT gid as id, * FROM ways', VT1, VT2,
arg_directed_flag::boolean);
END
$function$
Note: I want to execute the second SELECT only if the first Select doesn't return anything. Can't find any code for PostgreSQL to modify my function and fairly new to PostgreSQL environment.
Thank you!
SELECT *s ? And the second query is slow because it retrieves the complete ways table, which probably is quite large.