1 -- Trigger functions for location_postcode table.
 
   4 -- Trigger for updates of location_postcode
 
   6 -- Computes the parent object the postcode most likely refers to.
 
   7 -- This will be the place that determines the address displayed when
 
   8 -- searching for this postcode.
 
   9 CREATE OR REPLACE FUNCTION postcode_update()
 
  16     IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
 
  20     NEW.indexed_date = now();
 
  22     partition := get_partition(NEW.country_code);
 
  24     SELECT * FROM get_postcode_rank(NEW.country_code, NEW.postcode)
 
  25       INTO NEW.rank_search, NEW.rank_address;
 
  27     NEW.parent_place_id = 0;
 
  30         FROM getNearFeatures(partition, NEW.geometry, NEW.rank_search)
 
  31         WHERE NOT isguess ORDER BY rank_address DESC, distance asc LIMIT 1
 
  33         NEW.parent_place_id = location.place_id;