]> git.openstreetmap.org Git - nominatim.git/blob - docs/admin/Migration.md
docs: rewrite functions when migrating
[nominatim.git] / docs / admin / Migration.md
1 # Database Migrations
2
3 This page describes database migrations necessary to update existing databases
4 to newer versions of Nominatim.
5
6 SQL statements should be executed from the postgres commandline. Execute
7 `psql nominiatim` to enter command line mode.
8
9 ## 3.1.0 -> 3.2.0
10
11 ### New reverse algorithm
12
13 The reverse algorithm has changed and requires new indexes. Run the following
14 SQL statements to create the indexes:
15
16 ```
17 CREATE INDEX idx_placex_geometry_reverse_lookupPoint
18   ON placex USING gist (geometry)
19   WHERE (name is not null or housenumber is not null or rank_address between 26 and 27)
20     AND class not in ('railway','tunnel','bridge','man_made')
21     AND rank_address >= 26 AND indexed_status = 0 AND linked_place_id is null;
22 CREATE INDEX idx_placex_geometry_reverse_lookupPolygon
23   ON placex USING gist (geometry)
24   WHERE St_GeometryType(geometry) in ('ST_Polygon', 'ST_MultiPolygon')
25     AND rank_address between 4 and 25 AND type != 'postcode'
26     AND name is not null AND indexed_status = 0 AND linked_place_id is null;
27 CREATE INDEX idx_placex_geometry_reverse_placeNode
28   ON placex USING gist (geometry)
29   WHERE osm_type = 'N' AND rank_search between 5 and 25
30     AND class = 'place' AND type != 'postcode'
31     AND name is not null AND indexed_status = 0 AND linked_place_id is null;
32 ```
33
34 You also need to grant the website user access to the `country_osm_grid` table:
35
36 ```
37 GRANT SELECT ON table country_osm_grid to "www-user";
38 ```
39
40 Replace the `www-user` with the user name of your website server if necessary.
41
42 You can now drop the unused indexes:
43
44 ```
45 DROP INDEX idx_placex_reverse_geometry;
46 ```
47
48 Finally, update all SQL functions:
49
50 ```sh
51 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
52 ```
53
54 ## 3.0.0 -> 3.1.0
55
56 ### Postcode Table
57
58 A new separate table for artificially computed postcode centroids was introduced.
59 Migration to the new format is possible but **not recommended**.
60
61 Create postcode table and indexes, running the following SQL statements:
62
63 ```sql
64 CREATE TABLE location_postcode
65   (place_id BIGINT, parent_place_id BIGINT, rank_search SMALLINT,
66    rank_address SMALLINT, indexed_status SMALLINT, indexed_date TIMESTAMP,
67    country_code varchar(2), postcode TEXT,
68    geometry GEOMETRY(Geometry, 4326));
69 CREATE INDEX idx_postcode_geometry ON location_postcode USING GIST (geometry);
70 CREATE UNIQUE INDEX idx_postcode_id ON location_postcode USING BTREE (place_id);
71 CREATE INDEX idx_postcode_postcode ON location_postcode USING BTREE (postcode);
72 GRANT SELECT ON location_postcode TO "www-data";
73 drop type if exists nearfeaturecentr cascade;
74 create type nearfeaturecentr as (
75   place_id BIGINT,
76   keywords int[],
77   rank_address smallint,
78   rank_search smallint,
79   distance float,
80   isguess boolean,
81   postcode TEXT,
82   centroid GEOMETRY
83 );
84 ```
85
86 Add postcode column to `location_area` tables with SQL statement:
87
88 ```sql
89 ALTER TABLE location_area ADD COLUMN postcode TEXT;
90 ```
91
92 Then reimport the functions:
93
94 ```sh
95 ./utils/setup.php --create-functions --enable-diff-updates --create-partition-functions
96 ```
97
98 Create appropriate triggers with SQL:
99
100 ```sql
101 CREATE TRIGGER location_postcode_before_update BEFORE UPDATE ON location_postcode
102     FOR EACH ROW EXECUTE PROCEDURE postcode_update();
103 ```
104
105 Finally populate the postcode table (will take a while):
106
107 ```sh
108 ./utils/setup.php --calculate-postcodes --index --index-noanalyse
109 ```
110
111 This will create a working database. You may also delete the old artificial
112 postcodes now. Note that this may be expensive and is not absolutely necessary.
113 The following SQL statement will remove them:
114
115 ```sql
116 DELETE FROM place_addressline a USING placex p
117  WHERE a.address_place_id = p.place_id and p.osm_type = 'P';
118 ALTER TABLE placex DISABLE TRIGGER USER;
119 DELETE FROM placex WHERE osm_type = 'P';
120 ALTER TABLE placex ENABLE TRIGGER USER;
121 ```