Just addressing the length question here. The pgRouting docs give an example of pgr_kdijkstraPath
where the result includes the path geometry. Then you just need to use ST_Length
on the output geometry.
Example from the docs to get the geometry:
SELECT id1 as path, st_astext(st_linemerge(st_union(b.the_geom))) as the_geom
FROM pgr_kdijkstraPath(
'SELECT id, source, target, cost FROM edge_table',
10, array[4,12], false, false
) a,
edge_table b
WHERE a.id3=b.id
GROUP by id1
ORDER by id1;
path | the_geom
------+---------------------------------
4 | LINESTRING(2 3,3 3,4 3,4 2,4 1)
12 | LINESTRING(2 3,3 3,4 3)
(2 rows)
Therefore if you want length, just use st_length(st_linemerge(st_union(b.the_geom)))