KNN Function
CREATE TABLE building_height AS
SELECT b.ogc_fid, geom, id, featcode,
(SELECT h.prop_value FROM terrain h
ORDER BY b.geom <-> h.geom LIMIT 1)
FROM building b;
Useful blog posts
http://geeohspatial.blogspot.co.uk/2013/05/k-nearest-neighbor-search-in-postgis.html
http://boundlessgeo.com/2011/09/indexed-nearest-neighbour-search-in-postgis/
Updated less than a minute ago