I am using the following Nearest Neighbor Query in PostGIS :
SELECT g1.gid g2.gid FROM points as g1, polygons g2 WHERE g1.gid <> g2.gid ORDER BY g1.
You can do it with KNN index and lateral join.
SELECT v.gid, v2.gid,st_distance(v.the_geom, v2.the_geom) FROM geonames v, lateral(select * from geonames v2 where v2.id<>v.id ORDER BY v.the_geom <-> v2.the_geom LIMIT 10) v2 where v.gid in (...) - or other filtering condition