Nos estamos acercando.
Mire, le pongo un ejemplo de toda la query donde quiero meter el LOOP a ver si me explico mejor.
Esta es mi función en SETOF:
CREATE OR REPLACE FUNCTION wrk1_fromAtoB(
IN edges_subset regclass,
IN x numeric[],
OUT seq INTEGER,
OUT gid BIGINT,
OUT name TEXT,
OUT distancia_m NUMERIC,
OUT distancia_total NUMERIC,
OUT cost NUMERIC,
OUT agg_cost NUMERIC,
OUT azimuth FLOAT,
OUT geom geometry
)
RETURNS SETOF record AS
$BODY$
DECLARE
final_query TEXT;
BEGIN
final_query :=
FORMAT( $$
WITH
vertices AS (
SELECT * FROM ways_vertices_pgr
WHERE id IN (
SELECT source FROM %1$I
UNION
SELECT target FROM %1$I)
),
dijkstra AS (
SELECT *
FROM pgr_dijkstravia(
'%1$I',
ARRAY[(SELECT osm_id FROM vertices
ORDER BY the_geom <-> ST_SetSRID(ST_Point(%2$s, %3$s), 4258) LIMIT 1)]
)
SELECT
seq,
dijkstra.gid,
dijkstra.name,
TRUNC (ways.length_m::numeric) AS distancia_m,
TRUNC (sum(length_m::numeric) OVER (ORDER BY seq), 2) as distancia_total,
ROUND (dijkstra.cost::numeric, 2) AS cost,
ROUND (dijkstra.agg_cost::numeric, 2) as agg_cost,
azimuth,
route_geom AS geom
FROM dijkstra JOIN ways USING (gid);$$,
edges_subset, x);
RAISE notice '%', final_query;
RETURN QUERY EXECUTE final_query;
END;
$BODY$
LANGUAGE 'plpgsql';
Ahora lo que necesito es que en
(
SELECT *
FROM pgr_dijkstravia(
'%1$I',
ARRAY[(SELECT osm_id FROM vertices
ORDER BY the_geom <-> ST_SetSRID(ST_Point(%2$s, %3$s), 4258) LIMIT 1)]
)
El proceso del ARRAY:
ARRAY[(SELECT osm_id FROM vertices
ORDER BY the_geom <-> ST_SetSRID(ST_Point(%2$s, %3$s), 4258) LIMIT 1)]
Lo haga tantas veces como subconjuntos haya en el ARRAY de la consulta. Por Ejemplo:
SELECT * FROM pgr_dijkstravia('ways', ARRAY[[-7.563947439193725, 43.02141169991877], [-7.56184458732605, 43.01576387609036],[-7.5599563121795645, 43.006741968861974]])
Y la query haga esto:
SELECT *
FROM pgr_dijkstravia(
'%1$I',
ARRAY[
(SELECT id FROM vertices
ORDER BY the_geom <-> ST_SetSRID(ST_Point(-7.563947439193725, 43.02141169991877), 4258) LIMIT 1),
(SELECT id FROM vertices
ORDER BY the_geom <-> ST_SetSRID(ST_Point(-7.56184458732605, 43.01576387609036), 4258) LIMIT 1),
(SELECT id FROM vertices
ORDER BY the_geom <-> ST_SetSRID(ST_Point(-7.5599563121795645, 43.006741968861974), 4258) LIMIT 1)
]
)
Entonces para esto quiero el proceso FOREACH x SLICE 1 IN ARRAY $2
Pero no se muy bien como encajarlo en esta función.
Gracias de antemano.
Saludos.