Przykładowy sql:
select *, length(p_path::path) as distance from (select *, p.p_name, '((54.175540523964, 15.527111972064), ('||p_lat::text||','||p_lng::text||'))' as p_path
from
points p
where
p.p_status (p.m_id=5
or
(51.175540 < p_lat and p_lat < 57.17554 and 12.52711 < p_lng and p_lng < 18.52711))
order by
p.m_id=5 DESC nulls last) t
order by distance asc limit 6