]> git.openstreetmap.org Git - nominatim.git/blob - lib-sql/functions/placex_triggers.sql
introduce index for finding surrounding buildings
[nominatim.git] / lib-sql / functions / placex_triggers.sql
1 -- Trigger functions for the placex table.
2
3 -- Retrieve the data needed by the indexer for updating the place.
4 --
5 -- Return parameters:
6 --  name            list of names
7 --  address         list of address tags, either from the object or a surrounding
8 --                  building
9 --  country_feature If the place is a country feature, this contains the
10 --                  country code, otherwise it is null.
11 CREATE OR REPLACE FUNCTION placex_prepare_update(p placex,
12                                                  OUT name HSTORE,
13                                                  OUT address HSTORE,
14                                                  OUT country_feature VARCHAR)
15   AS $$
16 BEGIN
17   -- For POI nodes, check if the address should be derived from a surrounding
18   -- building.
19   IF p.rank_search < 30 OR p.osm_type != 'N' OR p.address is not null THEN
20     RAISE WARNING 'self address for % %', p.osm_type, p.osm_id;
21     address := p.address;
22   ELSE
23     -- The additional && condition works around the misguided query
24     -- planner of postgis 3.0.
25     SELECT placex.address || hstore('_inherited', '') INTO address
26       FROM placex
27      WHERE ST_Covers(geometry, p.centroid)
28            and geometry && p.centroid
29            and placex.address is not null
30            and (placex.address ? 'housenumber' or placex.address ? 'street' or placex.address ? 'place')
31            and rank_search = 30 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
32      LIMIT 1;
33     RAISE WARNING 'other address for % %: % (%)', p.osm_type, p.osm_id, address, p.centroid;
34   END IF;
35
36   address := address - '_unlisted_place'::TEXT;
37   name := p.name;
38
39   country_feature := CASE WHEN p.admin_level = 2
40                                and p.class = 'boundary' and p.type = 'administrative'
41                                and p.osm_type = 'R'
42                           THEN p.country_code
43                           ELSE null
44                      END;
45 END;
46 $$
47 LANGUAGE plpgsql STABLE;
48
49
50 -- Find the parent road of a POI.
51 --
52 -- \returns Place ID of parent object or NULL if none
53 --
54 -- Copy data from linked items (POIs on ways, addr:street links, relations).
55 --
56 CREATE OR REPLACE FUNCTION find_parent_for_poi(poi_osm_type CHAR(1),
57                                                poi_osm_id BIGINT,
58                                                poi_partition SMALLINT,
59                                                bbox GEOMETRY,
60                                                addr_street TEXT,
61                                                addr_place TEXT,
62                                                fallback BOOL = true)
63   RETURNS BIGINT
64   AS $$
65 DECLARE
66   parent_place_id BIGINT DEFAULT NULL;
67   location RECORD;
68   parent RECORD;
69 BEGIN
70     {% if debug %}RAISE WARNING 'finding street for % %', poi_osm_type, poi_osm_id;{% endif %}
71
72     -- Is this object part of an associatedStreet relation?
73     FOR location IN
74       SELECT members FROM planet_osm_rels
75       WHERE parts @> ARRAY[poi_osm_id]
76         and members @> ARRAY[lower(poi_osm_type) || poi_osm_id]
77         and tags @> ARRAY['associatedStreet']
78     LOOP
79       FOR i IN 1..array_upper(location.members, 1) BY 2 LOOP
80         IF location.members[i+1] = 'street' THEN
81           FOR parent IN
82             SELECT place_id from placex
83              WHERE osm_type = 'W' and osm_id = substring(location.members[i],2)::bigint
84                and name is not null
85                and rank_search between 26 and 27
86           LOOP
87             RETURN parent.place_id;
88           END LOOP;
89         END IF;
90       END LOOP;
91     END LOOP;
92
93     parent_place_id := find_parent_for_address(addr_street, addr_place,
94                                                poi_partition, bbox);
95     IF parent_place_id is not null THEN
96       RETURN parent_place_id;
97     END IF;
98
99     IF poi_osm_type = 'N' THEN
100       -- Is this node part of an interpolation?
101       FOR parent IN
102         SELECT q.parent_place_id
103           FROM location_property_osmline q, planet_osm_ways x
104          WHERE q.linegeo && bbox and x.id = q.osm_id
105                and poi_osm_id = any(x.nodes)
106          LIMIT 1
107       LOOP
108         {% if debug %}RAISE WARNING 'Get parent from interpolation: %', parent.parent_place_id;{% endif %}
109         RETURN parent.parent_place_id;
110       END LOOP;
111
112       -- Is this node part of any other way?
113       FOR location IN
114         SELECT p.place_id, p.osm_id, p.rank_search, p.address,
115                coalesce(p.centroid, ST_Centroid(p.geometry)) as centroid
116           FROM placex p, planet_osm_ways w
117          WHERE p.osm_type = 'W' and p.rank_search >= 26
118                and p.geometry && bbox
119                and w.id = p.osm_id and poi_osm_id = any(w.nodes)
120       LOOP
121         {% if debug %}RAISE WARNING 'Node is part of way % ', location.osm_id;{% endif %}
122
123         -- Way IS a road then we are on it - that must be our road
124         IF location.rank_search < 28 THEN
125           {% if debug %}RAISE WARNING 'node in way that is a street %',location;{% endif %}
126           return location.place_id;
127         END IF;
128
129         SELECT find_parent_for_poi('W', location.osm_id, poi_partition,
130                                    location.centroid,
131                                    location.address->'street',
132                                    location.address->'place',
133                                    false)
134           INTO parent_place_id;
135         IF parent_place_id is not null THEN
136           RETURN parent_place_id;
137         END IF;
138       END LOOP;
139     END IF;
140
141     IF fallback THEN
142       IF addr_street is null and addr_place is not null THEN
143         -- The address is attached to a place we don't know.
144         -- Instead simply use the containing area with the largest rank.
145         FOR location IN
146           SELECT place_id FROM placex
147             WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
148                   AND rank_address between 5 and 25
149             ORDER BY rank_address desc
150         LOOP
151             RETURN location.place_id;
152         END LOOP;
153       ELSEIF ST_Area(bbox) < 0.005 THEN
154         -- for smaller features get the nearest road
155         SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id;
156         {% if debug %}RAISE WARNING 'Checked for nearest way (%)', parent_place_id;{% endif %}
157       ELSE
158         -- for larger features simply find the area with the largest rank that
159         -- contains the bbox, only use addressable features
160         FOR location IN
161           SELECT place_id FROM placex
162             WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
163                   AND rank_address between 5 and 25
164             ORDER BY rank_address desc
165         LOOP
166             RETURN location.place_id;
167         END LOOP;
168       END IF;
169     END IF;
170
171     RETURN parent_place_id;
172 END;
173 $$
174 LANGUAGE plpgsql STABLE;
175
176 -- Try to find a linked place for the given object.
177 CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
178   RETURNS placex
179   AS $$
180 DECLARE
181   relation_members TEXT[];
182   rel_member RECORD;
183   linked_placex placex%ROWTYPE;
184   bnd_name TEXT;
185 BEGIN
186   IF bnd.rank_search >= 26 or bnd.rank_address = 0
187      or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
188      or bnd.type IN ('postcode', 'postal_code')
189   THEN
190     RETURN NULL;
191   END IF;
192
193   IF bnd.osm_type = 'R' THEN
194     -- see if we have any special relation members
195     SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
196     {% if debug %}RAISE WARNING 'Got relation members';{% endif %}
197
198     -- Search for relation members with role 'lable'.
199     IF relation_members IS NOT NULL THEN
200       FOR rel_member IN
201         SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
202       LOOP
203         {% if debug %}RAISE WARNING 'Found label member %', rel_member.member;{% endif %}
204
205         FOR linked_placex IN
206           SELECT * from placex
207           WHERE osm_type = 'N' and osm_id = rel_member.member
208             and class = 'place'
209         LOOP
210           {% if debug %}RAISE WARNING 'Linked label member';{% endif %}
211           RETURN linked_placex;
212         END LOOP;
213
214       END LOOP;
215     END IF;
216   END IF;
217
218   IF bnd.name ? 'name' THEN
219     bnd_name := lower(bnd.name->'name');
220     IF bnd_name = '' THEN
221       bnd_name := NULL;
222     END IF;
223   END IF;
224
225   -- If extratags has a place tag, look for linked nodes by their place type.
226   -- Area and node still have to have the same name.
227   IF bnd.extratags ? 'place' and bnd_name is not null THEN
228     FOR linked_placex IN
229       SELECT * FROM placex
230       WHERE (position(lower(name->'name') in bnd_name) > 0
231              OR position(bnd_name in lower(name->'name')) > 0)
232         AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
233         AND placex.osm_type = 'N'
234         AND placex.linked_place_id is null
235         AND placex.rank_search < 26 -- needed to select the right index
236         AND placex.type != 'postcode'
237         AND ST_Covers(bnd.geometry, placex.geometry)
238     LOOP
239       {% if debug %}RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;{% endif %}
240       RETURN linked_placex;
241     END LOOP;
242   END IF;
243
244   IF bnd.extratags ? 'wikidata' THEN
245     FOR linked_placex IN
246       SELECT * FROM placex
247       WHERE placex.class = 'place' AND placex.osm_type = 'N'
248         AND placex.extratags ? 'wikidata' -- needed to select right index
249         AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
250         AND placex.linked_place_id is null
251         AND placex.rank_search < 26
252         AND _st_covers(bnd.geometry, placex.geometry)
253       ORDER BY lower(name->'name') = bnd_name desc
254     LOOP
255       {% if debug %}RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;{% endif %}
256       RETURN linked_placex;
257     END LOOP;
258   END IF;
259
260   -- Name searches can be done for ways as well as relations
261   IF bnd_name is not null THEN
262     {% if debug %}RAISE WARNING 'Looking for nodes with matching names';{% endif %}
263     FOR linked_placex IN
264       SELECT placex.* from placex
265       WHERE lower(name->'name') = bnd_name
266         AND ((bnd.rank_address > 0
267               and bnd.rank_address = (compute_place_rank(placex.country_code,
268                                                          'N', placex.class,
269                                                          placex.type, 15::SMALLINT,
270                                                          false, placex.postcode)).address_rank)
271              OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
272         AND placex.osm_type = 'N'
273         AND placex.class = 'place'
274         AND placex.linked_place_id is null
275         AND placex.rank_search < 26 -- needed to select the right index
276         AND placex.type != 'postcode'
277         AND ST_Covers(bnd.geometry, placex.geometry)
278     LOOP
279       {% if debug %}RAISE WARNING 'Found matching place node %', linked_placex.osm_id;{% endif %}
280       RETURN linked_placex;
281     END LOOP;
282   END IF;
283
284   RETURN NULL;
285 END;
286 $$
287 LANGUAGE plpgsql STABLE;
288
289
290 -- Insert address of a place into the place_addressline table.
291 --
292 -- \param obj_place_id  Place_id of the place to compute the address for.
293 -- \param partition     Partition number where the place is in.
294 -- \param maxrank       Rank of the place. All address features must have
295 --                      a search rank lower than the given rank.
296 -- \param address       Address terms for the place.
297 -- \param geometry      Geometry to which the address objects should be close.
298 --
299 -- \retval parent_place_id  Place_id of the address object that is the direct
300 --                          ancestor.
301 -- \retval postcode         Postcode computed from the address. This is the
302 --                          addr:postcode of one of the address objects. If
303 --                          more than one of has a postcode, the highest ranking
304 --                          one is used. May be NULL.
305 -- \retval nameaddress_vector  Search terms for the address. This is the sum
306 --                             of name terms of all address objects.
307 CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
308                                                partition SMALLINT,
309                                                maxrank SMALLINT,
310                                                address HSTORE,
311                                                geometry GEOMETRY,
312                                                country TEXT,
313                                                OUT parent_place_id BIGINT,
314                                                OUT postcode TEXT,
315                                                OUT nameaddress_vector INT[])
316   AS $$
317 DECLARE
318   address_havelevel BOOLEAN[];
319
320   location_isaddress BOOLEAN;
321   current_boundary GEOMETRY := NULL;
322   current_node_area GEOMETRY := NULL;
323
324   parent_place_rank INT := 0;
325   addr_place_ids BIGINT[];
326
327   location RECORD;
328 BEGIN
329   parent_place_id := 0;
330   nameaddress_vector := '{}'::int[];
331
332   address_havelevel := array_fill(false, ARRAY[maxrank]);
333
334   FOR location IN
335     SELECT * FROM get_places_for_addr_tags(partition, geometry,
336                                                    address, country)
337     ORDER BY rank_address, distance, isguess desc
338   LOOP
339     {% if not db.reverse_only %}
340       nameaddress_vector := array_merge(nameaddress_vector,
341                                         location.keywords::int[]);
342     {% endif %}
343
344     IF location.place_id is not null THEN
345       location_isaddress := not address_havelevel[location.rank_address];
346       IF not address_havelevel[location.rank_address] THEN
347         address_havelevel[location.rank_address] := true;
348         IF parent_place_rank < location.rank_address THEN
349           parent_place_id := location.place_id;
350           parent_place_rank := location.rank_address;
351         END IF;
352       END IF;
353
354       INSERT INTO place_addressline (place_id, address_place_id, fromarea,
355                                      isaddress, distance, cached_rank_address)
356         VALUES (obj_place_id, location.place_id, not location.isguess,
357                 true, location.distance, location.rank_address);
358
359       addr_place_ids := array_append(addr_place_ids, location.place_id);
360     END IF;
361   END LOOP;
362
363   FOR location IN
364     SELECT * FROM getNearFeatures(partition, geometry, maxrank)
365     WHERE addr_place_ids is null or not addr_place_ids @> ARRAY[place_id]
366     ORDER BY rank_address, isguess asc,
367              distance *
368                CASE WHEN rank_address = 16 AND rank_search = 15 THEN 0.2
369                     WHEN rank_address = 16 AND rank_search = 16 THEN 0.25
370                     WHEN rank_address = 16 AND rank_search = 18 THEN 0.5
371                     ELSE 1 END ASC
372   LOOP
373     -- Ignore all place nodes that do not fit in a lower level boundary.
374     CONTINUE WHEN location.isguess
375                   and current_boundary is not NULL
376                   and not ST_Contains(current_boundary, location.centroid);
377
378     -- If this is the first item in the rank, then assume it is the address.
379     location_isaddress := not address_havelevel[location.rank_address];
380
381     -- Further sanity checks to ensure that the address forms a sane hierarchy.
382     IF location_isaddress THEN
383       IF location.isguess and current_node_area is not NULL THEN
384         location_isaddress := ST_Contains(current_node_area, location.centroid);
385       END IF;
386       IF not location.isguess and current_boundary is not NULL
387          and location.rank_address != 11 AND location.rank_address != 5 THEN
388         location_isaddress := ST_Contains(current_boundary, location.centroid);
389       END IF;
390     END IF;
391
392     IF location_isaddress THEN
393       address_havelevel[location.rank_address] := true;
394       parent_place_id := location.place_id;
395
396       -- Set postcode if we have one.
397       -- (Returned will be the highest ranking one.)
398       IF location.postcode is not NULL THEN
399         postcode = location.postcode;
400       END IF;
401
402       -- Recompute the areas we need for hierarchy sanity checks.
403       IF location.rank_address != 11 AND location.rank_address != 5 THEN
404         IF location.isguess THEN
405           current_node_area := place_node_fuzzy_area(location.centroid,
406                                                      location.rank_search);
407         ELSE
408           current_node_area := NULL;
409           SELECT p.geometry FROM placex p
410               WHERE p.place_id = location.place_id INTO current_boundary;
411         END IF;
412       END IF;
413     END IF;
414
415     -- Add it to the list of search terms
416     {% if not db.reverse_only %}
417       nameaddress_vector := array_merge(nameaddress_vector,
418                                         location.keywords::integer[]);
419     {% endif %}
420
421     INSERT INTO place_addressline (place_id, address_place_id, fromarea,
422                                      isaddress, distance, cached_rank_address)
423         VALUES (obj_place_id, location.place_id, not location.isguess,
424                 location_isaddress, location.distance, location.rank_address);
425   END LOOP;
426 END;
427 $$
428 LANGUAGE plpgsql;
429
430
431 CREATE OR REPLACE FUNCTION placex_insert()
432   RETURNS TRIGGER
433   AS $$
434 DECLARE
435   postcode TEXT;
436   result BOOLEAN;
437   is_area BOOLEAN;
438   country_code VARCHAR(2);
439   diameter FLOAT;
440   classtable TEXT;
441 BEGIN
442   {% if debug %}RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
443
444   NEW.place_id := nextval('seq_place');
445   NEW.indexed_status := 1; --STATUS_NEW
446
447   NEW.centroid := ST_PointOnSurface(NEW.geometry);
448   NEW.country_code := lower(get_country_code(NEW.centroid));
449
450   NEW.partition := get_partition(NEW.country_code);
451   NEW.geometry_sector := geometry_sector(NEW.partition, NEW.centroid);
452
453   IF NEW.osm_type = 'X' THEN
454     -- E'X'ternal records should already be in the right format so do nothing
455   ELSE
456     is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
457
458     IF NEW.class in ('place','boundary')
459        AND NEW.type in ('postcode','postal_code')
460     THEN
461       IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
462           -- most likely just a part of a multipolygon postcode boundary, throw it away
463           RETURN NULL;
464       END IF;
465
466       NEW.name := hstore('ref', NEW.address->'postcode');
467
468     ELSEIF NEW.class = 'highway' AND is_area AND NEW.name is null
469            AND NEW.extratags ? 'area' AND NEW.extratags->'area' = 'yes'
470     THEN
471         RETURN NULL;
472     ELSEIF NEW.class = 'boundary' AND NOT is_area
473     THEN
474         RETURN NULL;
475     ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
476            AND NEW.admin_level <= 4 AND NEW.osm_type = 'W'
477     THEN
478         RETURN NULL;
479     END IF;
480
481     SELECT * INTO NEW.rank_search, NEW.rank_address
482       FROM compute_place_rank(NEW.country_code,
483                               CASE WHEN is_area THEN 'A' ELSE NEW.osm_type END,
484                               NEW.class, NEW.type, NEW.admin_level,
485                               (NEW.extratags->'capital') = 'yes',
486                               NEW.address->'postcode');
487
488     -- a country code make no sense below rank 4 (country)
489     IF NEW.rank_search < 4 THEN
490       NEW.country_code := NULL;
491     END IF;
492
493   END IF;
494
495   {% if debug %}RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
496
497 {% if not disable_diff_updates %}
498   -- The following is not needed until doing diff updates, and slows the main index process down
499
500   IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
501       -- might be part of an interpolation
502       result := osmline_reinsert(NEW.osm_id, NEW.geometry);
503   ELSEIF NEW.rank_address > 0 THEN
504     IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
505       -- Performance: We just can't handle re-indexing for country level changes
506       IF st_area(NEW.geometry) < 1 THEN
507         -- mark items within the geometry for re-indexing
508   --    RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
509
510         UPDATE placex SET indexed_status = 2
511          WHERE ST_Intersects(NEW.geometry, placex.geometry)
512                and indexed_status = 0
513                and ((rank_address = 0 and rank_search > NEW.rank_address)
514                     or rank_address > NEW.rank_address
515                     or (class = 'place' and osm_type = 'N')
516                    )
517                and (rank_search < 28
518                     or name is not null
519                     or (NEW.rank_address >= 16 and address ? 'place'));
520       END IF;
521     ELSE
522       -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
523       diameter := update_place_diameter(NEW.rank_search);
524       IF diameter > 0 THEN
525   --      RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
526         IF NEW.rank_search >= 26 THEN
527           -- roads may cause reparenting for >27 rank places
528           update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
529           -- reparenting also for OSM Interpolation Lines (and for Tiger?)
530           update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
531         ELSEIF NEW.rank_search >= 16 THEN
532           -- up to rank 16, street-less addresses may need reparenting
533           update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter) and (rank_search < 28 or name is not null or address ? 'place');
534         ELSE
535           -- for all other places the search terms may change as well
536           update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter) and (rank_search < 28 or name is not null);
537         END IF;
538       END IF;
539     END IF;
540   END IF;
541
542
543    -- add to tables for special search
544    -- Note: won't work on initial import because the classtype tables
545    -- do not yet exist. It won't hurt either.
546   classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
547   SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
548   IF result THEN
549     EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)' 
550     USING NEW.place_id, ST_Centroid(NEW.geometry);
551   END IF;
552
553 {% endif %} -- not disable_diff_updates
554
555   RETURN NEW;
556
557 END;
558 $$
559 LANGUAGE plpgsql;
560
561 CREATE OR REPLACE FUNCTION placex_update()
562   RETURNS TRIGGER
563   AS $$
564 DECLARE
565   i INTEGER;
566   location RECORD;
567   relation_members TEXT[];
568
569   geom GEOMETRY;
570   parent_address_level SMALLINT;
571   place_address_level SMALLINT;
572
573   addr_street TEXT;
574   addr_place TEXT;
575
576   max_rank SMALLINT;
577
578   name_vector INTEGER[];
579   nameaddress_vector INTEGER[];
580   addr_nameaddress_vector INTEGER[];
581
582   linked_node_id BIGINT;
583   linked_importance FLOAT;
584   linked_wikipedia TEXT;
585
586   result BOOLEAN;
587 BEGIN
588   -- deferred delete
589   IF OLD.indexed_status = 100 THEN
590     {% if debug %}RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;{% endif %}
591     delete from placex where place_id = OLD.place_id;
592     RETURN NULL;
593   END IF;
594
595   IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
596     RETURN NEW;
597   END IF;
598
599   {% if debug %}RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;{% endif %}
600
601   NEW.indexed_date = now();
602
603   {% if 'search_name' in db.tables %}
604     DELETE from search_name WHERE place_id = NEW.place_id;
605   {% endif %}
606   result := deleteSearchName(NEW.partition, NEW.place_id);
607   DELETE FROM place_addressline WHERE place_id = NEW.place_id;
608   result := deleteRoad(NEW.partition, NEW.place_id);
609   result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
610   UPDATE placex set linked_place_id = null, indexed_status = 2
611          where linked_place_id = NEW.place_id;
612   -- update not necessary for osmline, cause linked_place_id does not exist
613
614   NEW.extratags := NEW.extratags - 'linked_place'::TEXT;
615
616   IF NEW.linked_place_id is not null THEN
617     {% if debug %}RAISE WARNING 'place already linked to %', NEW.linked_place_id;{% endif %}
618     RETURN NEW;
619   END IF;
620
621   -- Postcodes are just here to compute the centroids. They are not searchable
622   -- unless they are a boundary=postal_code.
623   -- There was an error in the style so that boundary=postal_code used to be
624   -- imported as place=postcode. That's why relations are allowed to pass here.
625   -- This can go away in a couple of versions.
626   IF NEW.class = 'place'  and NEW.type = 'postcode' and NEW.osm_type != 'R' THEN
627     RETURN NEW;
628   END IF;
629
630   -- Speed up searches - just use the centroid of the feature
631   -- cheaper but less acurate
632   NEW.centroid := ST_PointOnSurface(NEW.geometry);
633   {% if debug %}RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(NEW.centroid);{% endif %}
634
635   -- recompute the ranks, they might change when linking changes
636   SELECT * INTO NEW.rank_search, NEW.rank_address
637     FROM compute_place_rank(NEW.country_code,
638                             CASE WHEN ST_GeometryType(NEW.geometry)
639                                         IN ('ST_Polygon','ST_MultiPolygon')
640                             THEN 'A' ELSE NEW.osm_type END,
641                             NEW.class, NEW.type, NEW.admin_level,
642                             (NEW.extratags->'capital') = 'yes',
643                             NEW.address->'postcode');
644   -- We must always increase the address level relative to the admin boundary.
645   IF NEW.class = 'boundary' and NEW.type = 'administrative'
646      and NEW.osm_type = 'R' and NEW.rank_address > 0
647   THEN
648     -- First, check that admin boundaries do not overtake each other rank-wise.
649     parent_address_level := 3;
650     FOR location IN
651       SELECT rank_address,
652              (CASE WHEN extratags ? 'wikidata' and NEW.extratags ? 'wikidata'
653                         and extratags->'wikidata' = NEW.extratags->'wikidata'
654                    THEN ST_Equals(geometry, NEW.geometry)
655                    ELSE false END) as is_same
656       FROM placex
657       WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
658             and admin_level < NEW.admin_level and admin_level > 3
659             and rank_address > 0
660             and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
661       ORDER BY admin_level desc LIMIT 1
662     LOOP
663       IF location.is_same THEN
664         -- Looks like the same boundary is replicated on multiple admin_levels.
665         -- Usual tagging in Poland. Remove our boundary from addresses.
666         NEW.rank_address := 0;
667       ELSE
668         parent_address_level := location.rank_address;
669         IF location.rank_address >= NEW.rank_address THEN
670           IF location.rank_address >= 24 THEN
671             NEW.rank_address := 25;
672           ELSE
673             NEW.rank_address := location.rank_address + 2;
674           END IF;
675         END IF;
676       END IF;
677     END LOOP;
678
679     IF NEW.rank_address > 9 THEN
680         -- Second check that the boundary is not completely contained in a
681         -- place area with a higher address rank
682         FOR location IN
683           SELECT rank_address FROM placex
684           WHERE class = 'place' and rank_address < 24
685                 and rank_address > NEW.rank_address
686                 and geometry && NEW.geometry
687                 and geometry ~ NEW.geometry -- needed because ST_Relate does not do bbox cover test
688                 and ST_Relate(geometry, NEW.geometry, 'T*T***FF*') -- contains but not equal
689           ORDER BY rank_address desc LIMIT 1
690         LOOP
691           NEW.rank_address := location.rank_address + 2;
692         END LOOP;
693     END IF;
694   ELSEIF NEW.class = 'place' and NEW.osm_type = 'N'
695      and NEW.rank_address between 16 and 23
696   THEN
697     -- If a place node is contained in a admin boundary with the same address level
698     -- and has not been linked, then make the node a subpart by increasing the
699     -- address rank (city level and above).
700     FOR location IN
701         SELECT rank_address FROM placex
702         WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
703               and rank_address = NEW.rank_address
704               and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
705         LIMIT 1
706     LOOP
707       NEW.rank_address = NEW.rank_address + 2;
708     END LOOP;
709   ELSE
710     parent_address_level := 3;
711   END IF;
712
713   {% if debug %}RAISE WARNING 'Copy over address tags';{% endif %}
714   -- housenumber is a computed field, so start with an empty value
715   NEW.housenumber := NULL;
716   IF NEW.address is not NULL THEN
717       IF NEW.address ? 'conscriptionnumber' THEN
718         IF NEW.address ? 'streetnumber' THEN
719             NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
720         ELSE
721             NEW.housenumber := NEW.address->'conscriptionnumber';
722         END IF;
723       ELSEIF NEW.address ? 'streetnumber' THEN
724         NEW.housenumber := NEW.address->'streetnumber';
725       ELSEIF NEW.address ? 'housenumber' THEN
726         NEW.housenumber := NEW.address->'housenumber';
727       END IF;
728       NEW.housenumber := create_housenumber_id(NEW.housenumber);
729
730       addr_street := NEW.address->'street';
731       addr_place := NEW.address->'place';
732
733       IF NEW.address ? 'postcode' and NEW.address->'postcode' not similar to '%(:|,|;)%' THEN
734         i := getorcreate_postcode_id(NEW.address->'postcode');
735       END IF;
736   END IF;
737
738   NEW.postcode := null;
739
740   -- recalculate country and partition
741   IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
742     -- for countries, believe the mapped country code,
743     -- so that we remain in the right partition if the boundaries
744     -- suddenly expand.
745     NEW.country_code := lower(NEW.address->'country');
746     NEW.partition := get_partition(lower(NEW.country_code));
747     IF NEW.partition = 0 THEN
748       NEW.country_code := lower(get_country_code(NEW.centroid));
749       NEW.partition := get_partition(NEW.country_code);
750     END IF;
751   ELSE
752     IF NEW.rank_search >= 4 THEN
753       NEW.country_code := lower(get_country_code(NEW.centroid));
754     ELSE
755       NEW.country_code := NULL;
756     END IF;
757     NEW.partition := get_partition(NEW.country_code);
758   END IF;
759   {% if debug %}RAISE WARNING 'Country updated: "%"', NEW.country_code;{% endif %}
760
761   -- waterway ways are linked when they are part of a relation and have the same class/type
762   IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
763       FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
764       LOOP
765           FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
766               IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
767                 {% if debug %}RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];{% endif %}
768                 FOR linked_node_id IN SELECT place_id FROM placex
769                   WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
770                   and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
771                   and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
772                 LOOP
773                   UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
774                   {% if 'search_name' in db.tables %}
775                     DELETE FROM search_name WHERE place_id = linked_node_id;
776                   {% endif %}
777                 END LOOP;
778               END IF;
779           END LOOP;
780       END LOOP;
781       {% if debug %}RAISE WARNING 'Waterway processed';{% endif %}
782   END IF;
783
784   NEW.importance := null;
785   SELECT wikipedia, importance
786     FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
787     INTO NEW.wikipedia,NEW.importance;
788
789 {% if debug %}RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;{% endif %}
790
791   -- ---------------------------------------------------------------------------
792   -- For low level elements we inherit from our parent road
793   IF NEW.rank_search > 27 THEN
794
795     {% if debug %}RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;{% endif %}
796     NEW.parent_place_id := null;
797
798     -- We have to find our parent road.
799     NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
800                                                NEW.partition,
801                                                ST_Envelope(NEW.geometry),
802                                                addr_street, addr_place);
803
804     -- If we found the road take a shortcut here.
805     -- Otherwise fall back to the full address getting method below.
806     IF NEW.parent_place_id is not null THEN
807
808       -- Get the details of the parent road
809       SELECT p.country_code, p.postcode, p.name FROM placex p
810        WHERE p.place_id = NEW.parent_place_id INTO location;
811
812       IF addr_street is null and addr_place is not null THEN
813         -- Check if the addr:place tag is part of the parent name
814         SELECT count(*) INTO i
815           FROM svals(location.name) AS pname WHERE pname = addr_place;
816         IF i = 0 THEN
817           NEW.address = NEW.address || hstore('_unlisted_place', addr_place);
818         END IF;
819       END IF;
820
821       NEW.country_code := location.country_code;
822       {% if debug %}RAISE WARNING 'Got parent details from search name';{% endif %}
823
824       -- determine postcode
825       IF NEW.address is not null AND NEW.address ? 'postcode' THEN
826           NEW.postcode = upper(trim(NEW.address->'postcode'));
827       ELSE
828          NEW.postcode := location.postcode;
829       END IF;
830       IF NEW.postcode is null THEN
831         NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
832       END IF;
833
834       IF NEW.name is not NULL THEN
835           NEW.name := add_default_place_name(NEW.country_code, NEW.name);
836           name_vector := make_keywords(NEW.name);
837
838           IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
839             result := add_location(NEW.place_id, NEW.country_code, NEW.partition,
840                                    name_vector, NEW.rank_search, NEW.rank_address,
841                                    upper(trim(NEW.address->'postcode')), NEW.geometry,
842                                    NEW.centroid);
843             {% if debug %}RAISE WARNING 'Place added to location table';{% endif %}
844           END IF;
845
846       END IF;
847
848       {% if not db.reverse_only %}
849       IF array_length(name_vector, 1) is not NULL
850          OR NEW.address is not NULL
851       THEN
852         SELECT * INTO name_vector, nameaddress_vector
853           FROM create_poi_search_terms(NEW.place_id,
854                                        NEW.partition, NEW.parent_place_id,
855                                        NEW.address,
856                                        NEW.country_code, NEW.housenumber,
857                                        name_vector, NEW.centroid);
858
859         IF array_length(name_vector, 1) is not NULL THEN
860           INSERT INTO search_name (place_id, search_rank, address_rank,
861                                    importance, country_code, name_vector,
862                                    nameaddress_vector, centroid)
863                  VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
864                          NEW.importance, NEW.country_code, name_vector,
865                          nameaddress_vector, NEW.centroid);
866           {% if debug %}RAISE WARNING 'Place added to search table';{% endif %}
867         END IF;
868       END IF;
869       {% endif %}
870
871       -- If the address was inherited from a surrounding building,
872       -- do not add it permanently to the table.
873       IF NEW.address ? '_inherited' THEN
874         IF NEW.address ? '_unlisted_place' THEN
875           NEW.address := hstore('_unlisted_place', NEW.address->'_unlisted_place');
876         ELSE
877           NEW.address := null;
878         END IF;
879       END IF;
880
881       RETURN NEW;
882     END IF;
883
884   END IF;
885
886   -- ---------------------------------------------------------------------------
887   -- Full indexing
888   {% if debug %}RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;{% endif %}
889   SELECT * INTO location FROM find_linked_place(NEW);
890   IF location.place_id is not null THEN
891     {% if debug %}RAISE WARNING 'Linked %', location;{% endif %}
892
893     -- Use the linked point as the centre point of the geometry,
894     -- but only if it is within the area of the boundary.
895     geom := coalesce(location.centroid, ST_Centroid(location.geometry));
896     IF geom is not NULL AND ST_Within(geom, NEW.geometry) THEN
897         NEW.centroid := geom;
898     END IF;
899
900     {% if debug %}RAISE WARNING 'parent address: % rank address: %', parent_address_level, location.rank_address;{% endif %}
901     IF location.rank_address > parent_address_level
902        and location.rank_address < 26
903     THEN
904       NEW.rank_address := location.rank_address;
905     END IF;
906
907     -- merge in the label name
908     IF NOT location.name IS NULL THEN
909       NEW.name := location.name || NEW.name;
910     END IF;
911
912     -- merge in extra tags
913     NEW.extratags := hstore('linked_' || location.class, location.type)
914                      || coalesce(location.extratags, ''::hstore)
915                      || coalesce(NEW.extratags, ''::hstore);
916
917     -- mark the linked place (excludes from search results)
918     UPDATE placex set linked_place_id = NEW.place_id
919       WHERE place_id = location.place_id;
920     -- ensure that those places are not found anymore
921     {% if 'search_name' in db.tables %}
922       DELETE FROM search_name WHERE place_id = location.place_id;
923     {% endif %}
924     PERFORM deleteLocationArea(NEW.partition, location.place_id, NEW.rank_search);
925
926     SELECT wikipedia, importance
927       FROM compute_importance(location.extratags, NEW.country_code,
928                               'N', location.osm_id)
929       INTO linked_wikipedia,linked_importance;
930
931     -- Use the maximum importance if one could be computed from the linked object.
932     IF linked_importance is not null AND
933        (NEW.importance is null or NEW.importance < linked_importance)
934     THEN
935       NEW.importance = linked_importance;
936     END IF;
937   ELSE
938     -- No linked place? As a last resort check if the boundary is tagged with
939     -- a place type and adapt the rank address.
940     IF NEW.rank_address > 0 and NEW.extratags ? 'place' THEN
941       SELECT address_rank INTO place_address_level
942         FROM compute_place_rank(NEW.country_code, 'A', 'place',
943                                 NEW.extratags->'place', 0::SMALLINT, False, null);
944       IF place_address_level > parent_address_level and
945          place_address_level < 26 THEN
946         NEW.rank_address := place_address_level;
947       END IF;
948     END IF;
949   END IF;
950
951   -- Initialise the name vector using our name
952   NEW.name := add_default_place_name(NEW.country_code, NEW.name);
953   name_vector := make_keywords(NEW.name);
954
955   -- make sure all names are in the word table
956   IF NEW.admin_level = 2
957      AND NEW.class = 'boundary' AND NEW.type = 'administrative'
958      AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
959   THEN
960     PERFORM create_country(NEW.name, lower(NEW.country_code));
961     {% if debug %}RAISE WARNING 'Country names updated';{% endif %}
962
963     -- Also update the list of country names. Adding an additional sanity
964     -- check here: make sure the country does overlap with the area where
965     -- we expect it to be as per static country grid.
966     FOR location IN
967       SELECT country_code FROM country_osm_grid
968        WHERE ST_Covers(geometry, NEW.centroid) and country_code = NEW.country_code
969        LIMIT 1
970     LOOP
971       {% if debug %}RAISE WARNING 'Updating names for country '%' with: %', NEW.country_code, NEW.name;{% endif %}
972       UPDATE country_name SET name = name || NEW.name WHERE country_code = NEW.country_code;
973     END LOOP;
974   END IF;
975
976   -- For linear features we need the full geometry for determining the address
977   -- because they may go through several administrative entities. Otherwise use
978   -- the centroid for performance reasons.
979   IF ST_GeometryType(NEW.geometry) in ('ST_LineString', 'ST_MultiLineString') THEN
980     geom := NEW.geometry;
981   ELSE
982     geom := NEW.centroid;
983   END IF;
984
985   IF NEW.rank_address = 0 THEN
986     max_rank := geometry_to_rank(NEW.rank_search, NEW.geometry, NEW.country_code);
987     -- Rank 0 features may also span multiple administrative areas (e.g. lakes)
988     -- so use the geometry here too. Just make sure the areas don't become too
989     -- large.
990     IF NEW.class = 'natural' or max_rank > 10 THEN
991       geom := NEW.geometry;
992     END IF;
993   ELSEIF NEW.rank_address > 25 THEN
994     max_rank := 25;
995   ELSE
996     max_rank = NEW.rank_address;
997   END IF;
998
999   SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition, max_rank,
1000                                     NEW.address, geom, NEW.country_code)
1001     INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
1002
1003   {% if debug %}RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;{% endif %}
1004
1005   IF NEW.address is not null AND NEW.address ? 'postcode' 
1006      AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
1007     NEW.postcode := upper(trim(NEW.address->'postcode'));
1008   END IF;
1009
1010   IF NEW.postcode is null AND NEW.rank_search > 8 THEN
1011     NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
1012   END IF;
1013
1014   -- if we have a name add this to the name search table
1015   IF NEW.name IS NOT NULL THEN
1016
1017     IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
1018       result := add_location(NEW.place_id, NEW.country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, upper(trim(NEW.address->'postcode')), NEW.geometry, NEW.centroid);
1019       {% if debug %}RAISE WARNING 'added to location (full)';{% endif %}
1020     END IF;
1021
1022     IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
1023       result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
1024       {% if debug %}RAISE WARNING 'insert into road location table (full)';{% endif %}
1025     END IF;
1026
1027     result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
1028                                NEW.rank_search, NEW.rank_address, NEW.geometry);
1029     {% if debug %}RAISE WARNING 'added to search name (full)';{% endif %}
1030
1031     {% if not db.reverse_only %}
1032         INSERT INTO search_name (place_id, search_rank, address_rank,
1033                                  importance, country_code, name_vector,
1034                                  nameaddress_vector, centroid)
1035                VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
1036                        NEW.importance, NEW.country_code, name_vector,
1037                        nameaddress_vector, NEW.centroid);
1038     {% endif %}
1039
1040   END IF;
1041
1042   {% if debug %}RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;{% endif %}
1043
1044   RETURN NEW;
1045 END;
1046 $$
1047 LANGUAGE plpgsql;
1048
1049
1050 CREATE OR REPLACE FUNCTION placex_delete()
1051   RETURNS TRIGGER
1052   AS $$
1053 DECLARE
1054   b BOOLEAN;
1055   classtable TEXT;
1056 BEGIN
1057   -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
1058
1059   IF OLD.linked_place_id is null THEN
1060     update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
1061     {% if debug %}RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1062     update placex set linked_place_id = null where linked_place_id = OLD.place_id;
1063     {% if debug %}RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1064   ELSE
1065     update placex set indexed_status = 2 where place_id = OLD.linked_place_id and indexed_status = 0;
1066   END IF;
1067
1068   IF OLD.rank_address < 30 THEN
1069
1070     -- mark everything linked to this place for re-indexing
1071     {% if debug %}RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1072     UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id 
1073       and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
1074
1075     {% if debug %}RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1076     DELETE FROM place_addressline where address_place_id = OLD.place_id;
1077
1078     {% if debug %}RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1079     b := deleteRoad(OLD.partition, OLD.place_id);
1080
1081     {% if debug %}RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1082     update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
1083     {% if debug %}RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1084     -- reparenting also for OSM Interpolation Lines (and for Tiger?)
1085     update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
1086
1087   END IF;
1088
1089   {% if debug %}RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1090
1091   IF OLD.rank_address < 26 THEN
1092     b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
1093   END IF;
1094
1095   {% if debug %}RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1096
1097   IF OLD.name is not null THEN
1098     {% if 'search_name' in db.tables %}
1099       DELETE from search_name WHERE place_id = OLD.place_id;
1100     {% endif %}
1101     b := deleteSearchName(OLD.partition, OLD.place_id);
1102   END IF;
1103
1104   {% if debug %}RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1105
1106   DELETE FROM place_addressline where place_id = OLD.place_id;
1107
1108   {% if debug %}RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1109
1110   -- remove from tables for special search
1111   classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1112   SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1113   IF b THEN
1114     EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1115   END IF;
1116
1117   {% if debug %}RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1118
1119   RETURN OLD;
1120
1121 END;
1122 $$
1123 LANGUAGE plpgsql;