1 -- SPDX-License-Identifier: GPL-2.0-only
 
   3 -- This file is part of Nominatim. (https://nominatim.org)
 
   5 -- Copyright (C) 2022 by the Nominatim developer community.
 
   6 -- For a full list of authors see the git log.
 
   8 -- Trigger functions for location_postcode table.
 
  11 -- Trigger for updates of location_postcode
 
  13 -- Computes the parent object the postcode most likely refers to.
 
  14 -- This will be the place that determines the address displayed when
 
  15 -- searching for this postcode.
 
  16 CREATE OR REPLACE FUNCTION postcode_update()
 
  23     IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
 
  27     NEW.indexed_date = now();
 
  29     partition := get_partition(NEW.country_code);
 
  31     SELECT * FROM get_postcode_rank(NEW.country_code, NEW.postcode)
 
  32       INTO NEW.rank_search, NEW.rank_address;
 
  34     NEW.parent_place_id = 0;
 
  37         FROM getNearFeatures(partition, NEW.geometry, NEW.geometry, NEW.rank_search)
 
  38         WHERE NOT isguess ORDER BY rank_address DESC, distance asc LIMIT 1
 
  40         NEW.parent_place_id = location.place_id;