]> git.openstreetmap.org Git - nominatim.git/blob - lib-sql/functions/utils.sql
do not attempt to delete old data for newly created placex entries
[nominatim.git] / lib-sql / functions / utils.sql
1 -- SPDX-License-Identifier: GPL-2.0-only
2 --
3 -- This file is part of Nominatim. (https://nominatim.org)
4 --
5 -- Copyright (C) 2026 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
7
8 -- Assorted helper functions for the triggers.
9
10 CREATE OR REPLACE FUNCTION get_center_point(place GEOMETRY)
11   RETURNS GEOMETRY
12   AS $$
13 DECLARE
14   geom_type TEXT;
15 BEGIN
16   geom_type := ST_GeometryType(place);
17   IF geom_type = 'ST_Point' THEN
18     RETURN place;
19   END IF;
20   IF geom_type = 'ST_LineString' THEN
21     RETURN ST_ReducePrecision(ST_LineInterpolatePoint(place, 0.5), 0.0000001);
22   END IF;
23
24   RETURN ST_ReducePrecision(ST_PointOnSurface(place), 0.0000001);
25 END;
26 $$
27 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
28
29
30 CREATE OR REPLACE FUNCTION geometry_sector(partition INTEGER, place GEOMETRY)
31   RETURNS INTEGER
32   AS $$
33 BEGIN
34   RETURN (partition*1000000) + (500-ST_X(place)::INTEGER)*1000 + (500-ST_Y(place)::INTEGER);
35 END;
36 $$
37 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
38
39
40
41 CREATE OR REPLACE FUNCTION array_merge(a INTEGER[], b INTEGER[])
42   RETURNS INTEGER[]
43   AS $$
44 DECLARE
45   i INTEGER;
46   r INTEGER[];
47 BEGIN
48   IF array_upper(a, 1) IS NULL THEN
49     RETURN COALESCE(b, '{}'::INTEGER[]);
50   END IF;
51   IF array_upper(b, 1) IS NULL THEN
52     RETURN COALESCE(a, '{}'::INTEGER[]);
53   END IF;
54   r := a;
55   FOR i IN 1..array_upper(b, 1) LOOP
56     IF NOT (ARRAY[b[i]] <@ r) THEN
57       r := r || b[i];
58     END IF;
59   END LOOP;
60   RETURN r;
61 END;
62 $$
63 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
64
65 -- Return the node members with a given label from a relation member list
66 -- as a set.
67 --
68 -- \param members      Member list in osm2pgsql middle format.
69 -- \param memberLabels Array of labels to accept.
70 --
71 -- \returns Set of OSM ids of nodes that are found.
72 --
73 CREATE OR REPLACE FUNCTION get_rel_node_members(members TEXT[],
74                                                 memberLabels TEXT[])
75   RETURNS SETOF BIGINT
76   AS $$
77 DECLARE
78   i INTEGER;
79 BEGIN
80   FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP
81     IF members[i+1] = ANY(memberLabels)
82        AND upper(substring(members[i], 1, 1))::char(1) = 'N'
83     THEN
84       RETURN NEXT substring(members[i], 2)::bigint;
85     END IF;
86   END LOOP;
87
88   RETURN;
89 END;
90 $$
91 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
92
93
94 CREATE OR REPLACE FUNCTION get_rel_node_members(members JSONB, memberLabels TEXT[])
95   RETURNS SETOF BIGINT
96   AS $$
97 DECLARE
98   member JSONB;
99 BEGIN
100   FOR member IN SELECT * FROM jsonb_array_elements(members)
101   LOOP
102     IF member->>'type' = 'N' and member->>'role' = ANY(memberLabels) THEN
103         RETURN NEXT (member->>'ref')::bigint;
104     END IF;
105   END LOOP;
106
107   RETURN;
108 END;
109 $$
110 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
111
112
113 -- Copy 'name' to or from the default language.
114 --
115 -- \param country_code     Country code of the object being named.
116 -- \param[inout] name      List of names of the object.
117 --
118 -- If the country named by country_code has a single default language,
119 -- then a `name` tag is copied to `name:<country_code>` if this tag does
120 -- not yet exist and vice versa.
121 CREATE OR REPLACE FUNCTION add_default_place_name(country_code VARCHAR(2),
122                                                   INOUT name HSTORE)
123   AS $$
124 DECLARE
125   default_language VARCHAR(10);
126 BEGIN
127   IF name is not null AND array_upper(akeys(name),1) > 1 THEN
128     default_language := get_country_language_code(country_code);
129     IF default_language IS NOT NULL THEN
130       IF name ? 'name' AND NOT name ? ('name:'||default_language) THEN
131         name := name || hstore(('name:'||default_language), (name -> 'name'));
132       ELSEIF name ? ('name:'||default_language) AND NOT name ? 'name' THEN
133         name := name || hstore('name', (name -> ('name:'||default_language)));
134       END IF;
135     END IF;
136   END IF;
137 END;
138 $$
139 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
140
141
142 -- Find the best-matching postcode for the given geometry
143 CREATE OR REPLACE FUNCTION get_nearest_postcode(country VARCHAR(2), geom GEOMETRY)
144   RETURNS TEXT
145   AS $$
146 DECLARE
147   outcode TEXT;
148   cnt INTEGER;
149   location RECORD;
150 BEGIN
151     -- If the geometry is an area then only one postcode must be within
152     -- that area, otherwise consider the area as not having a postcode.
153     IF ST_GeometryType(geom) in ('ST_Polygon','ST_MultiPolygon') THEN
154       SELECT min(postcode), count(*) FROM
155         (SELECT postcode FROM location_postcodes
156            WHERE geom && location_postcodes.geometry -- want to use the index
157                  AND ST_Contains(geom, location_postcodes.centroid)
158                  AND country_code = country
159            LIMIT 2) sub
160         INTO outcode, cnt;
161
162         IF cnt = 1 THEN
163             RETURN outcode;
164         END IF;
165
166         RETURN null;
167     END IF;
168
169     -- Otherwise: be fully within the coverage area of a postcode
170     FOR location IN
171       SELECT postcode
172         FROM location_postcodes p
173        WHERE ST_Covers(p.geometry, geom)
174              AND p.country_code = country
175        ORDER BY osm_id is null, ST_Distance(p.centroid, geom)
176        LIMIT 1
177     LOOP
178         RETURN location.postcode;
179     END LOOP;
180
181     RETURN NULL;
182 END;
183 $$
184 LANGUAGE plpgsql STABLE PARALLEL SAFE;
185
186
187 CREATE OR REPLACE FUNCTION get_country_code(place geometry)
188   RETURNS TEXT
189   AS $$
190 DECLARE
191   nearcountry RECORD;
192   countries TEXT[];
193 BEGIN
194 -- RAISE WARNING 'get_country_code, start: %', ST_AsText(place);
195
196   -- Try for a OSM polygon
197   SELECT array_agg(country_code) FROM location_area_country
198     WHERE country_code is not null and st_covers(geometry, place)
199     INTO countries;
200
201   IF array_length(countries, 1) = 1 THEN
202     RETURN countries[1];
203   END IF;
204
205   IF array_length(countries, 1) > 1 THEN
206     -- more than one country found, confirm against the fallback data what to choose
207     FOR nearcountry IN
208         SELECT country_code FROM country_osm_grid
209           WHERE ST_Covers(geometry, place) AND country_code = ANY(countries)
210           ORDER BY area ASC
211     LOOP
212         RETURN nearcountry.country_code;
213     END LOOP;
214     -- Still nothing? Choose the country code with the smallest partition number.
215     -- And failing that, just go by the alphabet.
216     FOR nearcountry IN
217         SELECT cc,
218                (SELECT partition FROM country_name WHERE country_code = cc) as partition
219         FROM unnest(countries) cc
220         ORDER BY partition, cc
221     LOOP
222         RETURN nearcountry.cc;
223     END LOOP;
224
225     -- Should never be reached.
226     RETURN countries[1];
227   END IF;
228
229 -- RAISE WARNING 'osm fallback: %', ST_AsText(place);
230
231   -- Try for OSM fallback data
232   -- The order is to deal with places like HongKong that are 'states' within another polygon
233   FOR nearcountry IN
234     SELECT country_code from country_osm_grid
235     WHERE st_covers(geometry, place) order by area asc limit 1
236   LOOP
237     RETURN nearcountry.country_code;
238   END LOOP;
239
240 -- RAISE WARNING 'near osm fallback: %', ST_AsText(place);
241
242   RETURN NULL;
243 END;
244 $$
245 LANGUAGE plpgsql STABLE PARALLEL SAFE;
246
247
248 CREATE OR REPLACE FUNCTION get_country_language_code(search_country_code VARCHAR(2))
249   RETURNS TEXT
250   AS $$
251 DECLARE
252   nearcountry RECORD;
253 BEGIN
254   FOR nearcountry IN
255     SELECT distinct country_default_language_code from country_name
256     WHERE country_code = search_country_code limit 1
257   LOOP
258     RETURN lower(nearcountry.country_default_language_code);
259   END LOOP;
260   RETURN NULL;
261 END;
262 $$
263 LANGUAGE plpgsql STABLE PARALLEL SAFE;
264
265
266 CREATE OR REPLACE FUNCTION get_partition(in_country_code VARCHAR(10))
267   RETURNS INTEGER
268   AS $$
269 DECLARE
270   nearcountry RECORD;
271 BEGIN
272   FOR nearcountry IN
273     SELECT partition from country_name where country_code = in_country_code
274   LOOP
275     RETURN nearcountry.partition;
276   END LOOP;
277   RETURN 0;
278 END;
279 $$
280 LANGUAGE plpgsql STABLE PARALLEL SAFE;
281
282
283 -- Find the parent of an address with addr:street/addr:place tag.
284 --
285 -- \param token_info Naming info with the address information.
286 -- \param partition  Partition where to search the parent.
287 -- \param centroid   Location of the address.
288 --
289 -- \return Place ID of the parent if one was found, NULL otherwise.
290 CREATE OR REPLACE FUNCTION find_parent_for_address(token_info JSONB,
291                                                    partition SMALLINT,
292                                                    centroid GEOMETRY)
293   RETURNS BIGINT
294   AS $$
295 DECLARE
296   parent_place_id BIGINT;
297 BEGIN
298   -- Check for addr:street attributes
299   parent_place_id := getNearestNamedRoadPlaceId(partition, centroid, token_info);
300   IF parent_place_id is not null THEN
301     {% if debug %}RAISE WARNING 'Get parent from addr:street: %', parent_place_id;{% endif %}
302     RETURN parent_place_id;
303   END IF;
304
305   -- Check for addr:place attributes.
306   parent_place_id := getNearestNamedPlacePlaceId(partition, centroid, token_info);
307   {% if debug %}RAISE WARNING 'Get parent from addr:place: %', parent_place_id;{% endif %}
308   RETURN parent_place_id;
309 END;
310 $$
311 LANGUAGE plpgsql STABLE PARALLEL SAFE;
312
313
314 CREATE OR REPLACE FUNCTION delete_location(OLD_place_id BIGINT)
315   RETURNS BOOLEAN
316   AS $$
317 DECLARE
318 BEGIN
319   DELETE FROM location_area where place_id = OLD_place_id;
320 -- TODO:location_area
321   RETURN true;
322 END;
323 $$
324 LANGUAGE plpgsql;
325
326
327 -- Return the bounding box of the geometry buffered by the given number
328 -- of meters.
329 CREATE OR REPLACE FUNCTION expand_by_meters(geom GEOMETRY, meters FLOAT)
330   RETURNS GEOMETRY
331   AS $$
332     SELECT ST_Envelope(ST_Buffer(geom::geography, meters, 1)::geometry)
333 $$
334 LANGUAGE sql IMMUTABLE PARALLEL SAFE;
335
336
337 -- Create a bounding box with an extent computed from the radius (in meters)
338 -- which in turn is derived from the given search rank.
339 CREATE OR REPLACE FUNCTION place_node_fuzzy_area(geom GEOMETRY, rank_search INTEGER)
340   RETURNS GEOMETRY
341   AS $$
342 DECLARE
343   radius FLOAT := 500;
344 BEGIN
345   IF rank_search <= 16 THEN -- city
346     radius := 15000;
347   ELSIF rank_search <= 18 THEN -- town
348     radius := 4000;
349   ELSIF rank_search <= 19 THEN -- village
350     radius := 2000;
351   ELSIF rank_search  <= 20 THEN -- hamlet
352     radius := 1000;
353   END IF;
354
355   RETURN expand_by_meters(geom, radius);
356 END;
357 $$
358 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
359
360
361 CREATE OR REPLACE FUNCTION add_location(place_id BIGINT, country_code varchar(2),
362                                         partition INTEGER, keywords INTEGER[],
363                                         rank_search INTEGER, rank_address INTEGER,
364                                         in_postcode TEXT, geometry GEOMETRY,
365                                         centroid GEOMETRY)
366   RETURNS BOOLEAN
367   AS $$
368 DECLARE
369   postcode TEXT;
370 BEGIN
371   -- add postcode only if it contains a single entry, i.e. ignore postcode lists
372   postcode := NULL;
373   IF in_postcode is not null AND in_postcode not similar to '%(,|;)%' THEN
374       postcode := upper(trim (in_postcode));
375   END IF;
376
377   IF ST_Dimension(geometry) = 2 THEN
378     RETURN insertLocationAreaLarge(partition, place_id, country_code, keywords,
379                                    rank_search, rank_address, false, postcode,
380                                    centroid, geometry);
381   END IF;
382
383   IF ST_Dimension(geometry) = 0 THEN
384     RETURN insertLocationAreaLarge(partition, place_id, country_code, keywords,
385                                    rank_search, rank_address, true, postcode,
386                                    centroid, place_node_fuzzy_area(geometry, rank_search));
387   END IF;
388
389   RETURN false;
390 END;
391 $$
392 LANGUAGE plpgsql;
393
394
395 CREATE OR REPLACE FUNCTION quad_split_geometry(geometry GEOMETRY, maxarea FLOAT,
396                                                maxdepth INTEGER)
397   RETURNS SETOF GEOMETRY
398   AS $$
399 DECLARE
400   xmin FLOAT;
401   ymin FLOAT;
402   xmax FLOAT;
403   ymax FLOAT;
404   xmid FLOAT;
405   ymid FLOAT;
406   secgeo GEOMETRY;
407   secbox GEOMETRY;
408   seg INTEGER;
409   geo RECORD;
410   area FLOAT;
411   remainingdepth INTEGER;
412 BEGIN
413 --  RAISE WARNING 'quad_split_geometry: maxarea=%, depth=%',maxarea,maxdepth;
414
415   IF not ST_IsValid(geometry) THEN
416     RETURN;
417   END IF;
418
419   IF ST_Dimension(geometry) != 2 OR maxdepth <= 1 THEN
420     RETURN NEXT geometry;
421     RETURN;
422   END IF;
423
424   remainingdepth := maxdepth - 1;
425   area := ST_AREA(geometry);
426   IF area < maxarea THEN
427     RETURN NEXT geometry;
428     RETURN;
429   END IF;
430
431   xmin := st_xmin(geometry);
432   xmax := st_xmax(geometry);
433   ymin := st_ymin(geometry);
434   ymax := st_ymax(geometry);
435   secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(ymin,xmin),ST_Point(ymax,xmax)),4326);
436
437   -- if the geometry completely covers the box don't bother to slice any more
438   IF ST_AREA(secbox) = area THEN
439     RETURN NEXT geometry;
440     RETURN;
441   END IF;
442
443   xmid := (xmin+xmax)/2;
444   ymid := (ymin+ymax)/2;
445
446   FOR seg IN 1..4 LOOP
447
448     IF seg = 1 THEN
449       secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmin,ymin),ST_Point(xmid,ymid)),4326);
450     END IF;
451     IF seg = 2 THEN
452       secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmin,ymid),ST_Point(xmid,ymax)),4326);
453     END IF;
454     IF seg = 3 THEN
455       secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmid,ymin),ST_Point(xmax,ymid)),4326);
456     END IF;
457     IF seg = 4 THEN
458       secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmid,ymid),ST_Point(xmax,ymax)),4326);
459     END IF;
460
461     secgeo := st_intersection(geometry, secbox);
462     IF NOT ST_IsEmpty(secgeo) AND ST_Dimension(secgeo) = 2 THEN
463       FOR geo IN SELECT quad_split_geometry(secgeo, maxarea, remainingdepth) as geom LOOP
464         IF NOT ST_IsEmpty(geo.geom) AND ST_Dimension(geo.geom) = 2 THEN
465           RETURN NEXT geo.geom;
466         END IF;
467       END LOOP;
468     END IF;
469   END LOOP;
470
471   RETURN;
472 END;
473 $$
474 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
475
476
477 CREATE OR REPLACE FUNCTION split_geometry(geometry GEOMETRY)
478   RETURNS SETOF GEOMETRY
479   AS $$
480 DECLARE
481   geo RECORD;
482 BEGIN
483   IF ST_GeometryType(geometry) = 'ST_MultiPolygon'
484      and ST_Area(geometry) * 10 > ST_Area(Box2D(geometry))
485   THEN
486     FOR geo IN
487         SELECT quad_split_geometry(g, 0.25, 20) as geom
488         FROM (SELECT (ST_Dump(geometry)).geom::geometry(Polygon, 4326) AS g) xx
489     LOOP
490       RETURN NEXT geo.geom;
491     END LOOP;
492   ELSE
493     FOR geo IN
494         SELECT quad_split_geometry(geometry, 0.25, 20) as geom
495     LOOP
496       RETURN NEXT geo.geom;
497     END LOOP;
498   END IF;
499   RETURN;
500 END;
501 $$
502 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
503
504 CREATE OR REPLACE FUNCTION simplify_large_polygons(geometry GEOMETRY)
505   RETURNS GEOMETRY
506   AS $$
507 BEGIN
508   IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
509      and ST_MemSize(geometry) > 3000000
510   THEN
511     geometry := ST_SimplifyPreserveTopology(geometry, 0.0001);
512   END IF;
513   RETURN geometry;
514 END;
515 $$
516 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
517
518
519 CREATE OR REPLACE FUNCTION place_force_delete(placeid BIGINT)
520   RETURNS BOOLEAN
521   AS $$
522 DECLARE
523     osmid BIGINT;
524     osmtype character(1);
525     pclass text;
526     ptype text;
527 BEGIN
528   SELECT osm_type, osm_id, class, type FROM placex WHERE place_id = placeid INTO osmtype, osmid, pclass, ptype;
529   DELETE FROM import_polygon_delete where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
530   DELETE FROM import_polygon_error where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
531   -- force delete by directly entering it into the to-be-deleted table
532   INSERT INTO place_to_be_deleted (osm_type, osm_id, class, type, deferred)
533          VALUES(osmtype, osmid, pclass, ptype, false);
534   PERFORM flush_deleted_places();
535
536   RETURN TRUE;
537 END;
538 $$
539 LANGUAGE plpgsql;
540
541
542 CREATE OR REPLACE FUNCTION place_force_update(placeid BIGINT)
543   RETURNS BOOLEAN
544   AS $$
545 DECLARE
546   placegeom GEOMETRY;
547   geom GEOMETRY;
548   diameter FLOAT;
549   rank SMALLINT;
550 BEGIN
551   UPDATE placex SET indexed_status = 2 WHERE place_id = placeid;
552
553   SELECT geometry, rank_address INTO placegeom, rank
554     FROM placex WHERE place_id = placeid;
555
556   IF placegeom IS NOT NULL AND ST_IsValid(placegeom) THEN
557     IF ST_GeometryType(placegeom) in ('ST_Polygon','ST_MultiPolygon')
558        AND rank > 0
559     THEN
560       FOR geom IN SELECT split_geometry(placegeom) LOOP
561         UPDATE placex SET indexed_status = 2
562          WHERE ST_Intersects(geom, placex.geometry)
563                and indexed_status = 0
564                and ((rank_address = 0 and rank_search > rank) or rank_address > rank)
565                and (rank_search < 28 or name is not null or (rank >= 16 and address ? 'place'));
566       END LOOP;
567     ELSE
568         diameter := update_place_diameter(rank);
569         IF diameter > 0 THEN
570           IF rank >= 26 THEN
571             -- roads may cause reparenting for >27 rank places
572             update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter);
573           ELSEIF rank >= 16 THEN
574             -- up to rank 16, street-less addresses may need reparenting
575             update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter) and (rank_search < 28 or name is not null or address ? 'place');
576           ELSE
577             -- for all other places the search terms may change as well
578             update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter) and (rank_search < 28 or name is not null);
579           END IF;
580         END IF;
581     END IF;
582     RETURN TRUE;
583   END IF;
584
585   RETURN FALSE;
586 END;
587 $$
588 LANGUAGE plpgsql;
589
590 CREATE OR REPLACE FUNCTION flush_deleted_places()
591   RETURNS INTEGER
592   AS $$
593 BEGIN
594   -- deleting large polygons can have a massive effect on the system - require manual intervention to let them through
595   INSERT INTO import_polygon_delete (osm_type, osm_id, class, type)
596     SELECT osm_type, osm_id, class, type FROM place_to_be_deleted WHERE deferred;
597
598   -- delete from place table
599   ALTER TABLE place DISABLE TRIGGER place_before_delete;
600   DELETE FROM place USING place_to_be_deleted
601     WHERE place.osm_type = place_to_be_deleted.osm_type
602           and place.osm_id = place_to_be_deleted.osm_id
603           and place.class = place_to_be_deleted.class
604           and place.type = place_to_be_deleted.type
605           and not deferred;
606   ALTER TABLE place ENABLE TRIGGER place_before_delete;
607
608   -- Mark for delete in the placex table
609   UPDATE placex SET indexed_status = 100 FROM place_to_be_deleted
610     WHERE placex.osm_type = 'N' and place_to_be_deleted.osm_type = 'N'
611           and placex.osm_id = place_to_be_deleted.osm_id
612           and placex.class = place_to_be_deleted.class
613           and placex.type = place_to_be_deleted.type
614           and not deferred;
615   UPDATE placex SET indexed_status = 100 FROM place_to_be_deleted
616     WHERE placex.osm_type = 'W' and place_to_be_deleted.osm_type = 'W'
617           and placex.osm_id = place_to_be_deleted.osm_id
618           and placex.class = place_to_be_deleted.class
619           and placex.type = place_to_be_deleted.type
620           and not deferred;
621   UPDATE placex SET indexed_status = 100 FROM place_to_be_deleted
622     WHERE placex.osm_type = 'R' and place_to_be_deleted.osm_type = 'R'
623           and placex.osm_id = place_to_be_deleted.osm_id
624           and placex.class = place_to_be_deleted.class
625           and placex.type = place_to_be_deleted.type
626           and not deferred;
627
628    -- Mark for delete in interpolations
629    UPDATE location_property_osmline SET indexed_status = 100 FROM place_to_be_deleted
630     WHERE place_to_be_deleted.osm_type = 'W'
631           and place_to_be_deleted.class = 'place'
632           and place_to_be_deleted.type = 'houses'
633           and location_property_osmline.osm_id = place_to_be_deleted.osm_id
634           and not deferred;
635
636    -- Clear todo list.
637    TRUNCATE TABLE place_to_be_deleted;
638
639    RETURN NULL;
640 END;
641 $$ LANGUAGE plpgsql;
642
643
644 CREATE OR REPLACE FUNCTION place_update_entrances(placeid BIGINT, osmid BIGINT)
645   RETURNS INTEGER
646   AS $$
647 DECLARE
648   entrance RECORD;
649   osm_ids BIGINT[];
650 BEGIN
651   osm_ids := '{}';
652   FOR entrance in SELECT osm_id, type, geometry, extratags
653       FROM place_entrance
654       WHERE osm_id IN (SELECT unnest(nodes) FROM planet_osm_ways WHERE id=osmid)
655   LOOP
656     osm_ids := array_append(osm_ids, entrance.osm_id);
657     INSERT INTO placex_entrance (place_id, osm_id, type, location, extratags)
658       VALUES (placeid, entrance.osm_id, entrance.type, entrance.geometry, entrance.extratags)
659       ON CONFLICT (place_id, osm_id) DO UPDATE
660         SET type = excluded.type, location = excluded.location, extratags = excluded.extratags;
661   END LOOP;
662
663   IF array_length(osm_ids, 1) > 0 THEN
664     DELETE FROM placex_entrance WHERE place_id=placeid AND NOT osm_id=ANY(osm_ids);
665   ELSE
666     DELETE FROM placex_entrance WHERE place_id=placeid;
667   END IF;
668
669   RETURN NULL;
670 END;
671 $$
672 LANGUAGE plpgsql;