]> git.openstreetmap.org Git - nominatim.git/blob - lib-sql/tables.sql
remove special casing for postcodes in trigger code
[nominatim.git] / lib-sql / tables.sql
1 -- SPDX-License-Identifier: GPL-2.0-only
2 --
3 -- This file is part of Nominatim. (https://nominatim.org)
4 --
5 -- Copyright (C) 2025 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
7
8 drop table if exists import_status;
9 CREATE TABLE import_status (
10   lastimportdate timestamp with time zone NOT NULL,
11   sequence_id integer,
12   indexed boolean
13   );
14 GRANT SELECT ON import_status TO "{{config.DATABASE_WEBUSER}}" ;
15
16 drop table if exists import_osmosis_log;
17 CREATE TABLE import_osmosis_log (
18   batchend timestamp,
19   batchseq integer,
20   batchsize bigint,
21   starttime timestamp,
22   endtime timestamp,
23   event text
24   );
25
26 CREATE TABLE new_query_log (
27   type text,
28   starttime timestamp,
29   ipaddress text,
30   useragent text,
31   language text,
32   query text,
33   searchterm text,
34   endtime timestamp,
35   results integer,
36   format text,
37   secret text
38   );
39 CREATE INDEX idx_new_query_log_starttime ON new_query_log USING BTREE (starttime);
40 GRANT INSERT ON new_query_log TO "{{config.DATABASE_WEBUSER}}" ;
41 GRANT UPDATE ON new_query_log TO "{{config.DATABASE_WEBUSER}}" ;
42 GRANT SELECT ON new_query_log TO "{{config.DATABASE_WEBUSER}}" ;
43
44 GRANT SELECT ON TABLE country_name TO "{{config.DATABASE_WEBUSER}}";
45
46 DROP TABLE IF EXISTS nominatim_properties;
47 CREATE TABLE nominatim_properties (
48     property TEXT NOT NULL,
49     value TEXT
50 );
51 GRANT SELECT ON TABLE nominatim_properties TO "{{config.DATABASE_WEBUSER}}";
52
53 drop table IF EXISTS location_area CASCADE;
54 CREATE TABLE location_area (
55   place_id BIGINT,
56   keywords INTEGER[],
57   partition SMALLINT,
58   rank_search SMALLINT NOT NULL,
59   rank_address SMALLINT NOT NULL,
60   country_code VARCHAR(2),
61   isguess BOOL,
62   postcode TEXT,
63   centroid GEOMETRY(Point, 4326),
64   geometry GEOMETRY(Geometry, 4326)
65   );
66
67 CREATE TABLE location_area_large () INHERITS (location_area);
68
69 DROP TABLE IF EXISTS location_area_country;
70 CREATE TABLE location_area_country (
71   place_id BIGINT,
72   country_code varchar(2),
73   geometry GEOMETRY(Geometry, 4326)
74   ) {{db.tablespace.address_data}};
75 CREATE INDEX idx_location_area_country_geometry ON location_area_country USING GIST (geometry) {{db.tablespace.address_index}};
76
77
78 CREATE TABLE location_property_tiger (
79   place_id BIGINT,
80   parent_place_id BIGINT,
81   startnumber INTEGER,
82   endnumber INTEGER,
83   step SMALLINT,
84   partition SMALLINT,
85   linegeo GEOMETRY,
86   postcode TEXT);
87 GRANT SELECT ON location_property_tiger TO "{{config.DATABASE_WEBUSER}}";
88
89 drop table if exists location_property_osmline;
90 CREATE TABLE location_property_osmline (
91     place_id BIGINT NOT NULL,
92     osm_id BIGINT,
93     parent_place_id BIGINT,
94     geometry_sector INTEGER,
95     indexed_date TIMESTAMP,
96     startnumber INTEGER,
97     endnumber INTEGER,
98     step SMALLINT,
99     partition SMALLINT,
100     indexed_status SMALLINT,
101     linegeo GEOMETRY,
102     address HSTORE,
103     token_info JSONB, -- custom column for tokenizer use only
104     postcode TEXT,
105     country_code VARCHAR(2)
106   ){{db.tablespace.search_data}};
107 CREATE UNIQUE INDEX idx_osmline_place_id ON location_property_osmline USING BTREE (place_id) {{db.tablespace.search_index}};
108 CREATE INDEX idx_osmline_geometry_sector ON location_property_osmline USING BTREE (geometry_sector) {{db.tablespace.address_index}};
109 CREATE INDEX idx_osmline_linegeo ON location_property_osmline USING GIST (linegeo) {{db.tablespace.search_index}}
110   WHERE startnumber is not null;
111 GRANT SELECT ON location_property_osmline TO "{{config.DATABASE_WEBUSER}}";
112
113 drop table IF EXISTS search_name;
114 {% if not db.reverse_only %}
115 CREATE TABLE search_name (
116   place_id BIGINT,
117   importance FLOAT,
118   search_rank SMALLINT,
119   address_rank SMALLINT,
120   name_vector integer[],
121   nameaddress_vector integer[],
122   country_code varchar(2),
123   centroid GEOMETRY(Geometry, 4326)
124   ) {{db.tablespace.search_data}};
125 CREATE INDEX idx_search_name_place_id ON search_name USING BTREE (place_id) {{db.tablespace.search_index}};
126 GRANT SELECT ON search_name to "{{config.DATABASE_WEBUSER}}" ;
127 {% endif %}
128
129 drop table IF EXISTS place_addressline;
130 CREATE TABLE place_addressline (
131   place_id BIGINT,
132   address_place_id BIGINT,
133   distance FLOAT,
134   cached_rank_address SMALLINT,
135   fromarea boolean,
136   isaddress boolean
137   ) {{db.tablespace.search_data}};
138 CREATE INDEX idx_place_addressline_place_id on place_addressline USING BTREE (place_id) {{db.tablespace.search_index}};
139
140 ---------  PLACEX - storage for all indexed places -----------------
141
142 DROP TABLE IF EXISTS placex;
143 CREATE TABLE placex (
144   place_id BIGINT NOT NULL,
145   parent_place_id BIGINT,
146   linked_place_id BIGINT,
147   importance FLOAT,
148   indexed_date TIMESTAMP,
149   geometry_sector INTEGER,
150   rank_address SMALLINT,
151   rank_search SMALLINT,
152   partition SMALLINT,
153   indexed_status SMALLINT,
154   LIKE place INCLUDING CONSTRAINTS,
155   wikipedia TEXT, -- calculated wikipedia article name (language:title)
156   token_info JSONB, -- custom column for tokenizer use only
157   country_code varchar(2),
158   housenumber TEXT,
159   postcode TEXT,
160   centroid GEOMETRY(Geometry, 4326)
161   ) {{db.tablespace.search_data}};
162
163 CREATE UNIQUE INDEX idx_place_id ON placex USING BTREE (place_id) {{db.tablespace.search_index}};
164 {% for osm_type in ('N', 'W', 'R') %}
165 CREATE INDEX idx_placex_osmid_{{osm_type | lower}} ON placex
166   USING BTREE (osm_id) {{db.tablespace.search_index}}
167   WHERE osm_type = '{{osm_type}}';
168 {% endfor %}
169
170 -- Usage: - removing linkage status on update
171 --        - lookup linked places for /details
172 CREATE INDEX idx_placex_linked_place_id ON placex
173   USING BTREE (linked_place_id) {{db.tablespace.address_index}}
174   WHERE linked_place_id IS NOT NULL;
175
176 -- Usage: - check that admin boundaries do not overtake each other rank-wise
177 --        - check that place node in a admin boundary with the same address level
178 --        - boundary is not completely contained in a place area
179 --        - parenting of large-area or unparentable features
180 CREATE INDEX idx_placex_geometry_address_area_candidates ON placex
181   USING gist (geometry) {{db.tablespace.address_index}}
182   WHERE rank_address between 1 and 25
183         and ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon');
184
185 -- Usage: - POI is within building with housenumber
186 CREATE INDEX idx_placex_geometry_buildings ON placex
187   USING SPGIST (geometry) {{db.tablespace.address_index}}
188   WHERE address is not null and rank_search = 30
189         and ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon');
190
191 -- Usage: - linking of similar named places to boundaries
192 --        - linking of place nodes with same type to boundaries
193 CREATE INDEX idx_placex_geometry_placenode ON placex
194   USING SPGIST (geometry) {{db.tablespace.address_index}}
195   WHERE osm_type = 'N' and rank_search < 26 and class = 'place';
196
197 -- Usage: - is node part of a way?
198 --        - find parent of interpolation spatially
199 CREATE INDEX idx_placex_geometry_lower_rank_ways ON placex
200   USING SPGIST (geometry) {{db.tablespace.address_index}}
201   WHERE osm_type = 'W' and rank_search >= 26;
202
203 -- Usage: - linking place nodes by wikidata tag to boundaries
204 CREATE INDEX idx_placex_wikidata on placex
205   USING BTREE ((extratags -> 'wikidata')) {{db.tablespace.address_index}}
206   WHERE extratags ? 'wikidata' and class = 'place'
207         and osm_type = 'N' and rank_search < 26;
208
209 -- The following two indexes function as a todo list for indexing.
210
211 CREATE INDEX idx_placex_rank_address_sector ON placex
212   USING BTREE (rank_address, geometry_sector) {{db.tablespace.address_index}}
213   WHERE indexed_status > 0;
214
215 CREATE INDEX idx_placex_rank_boundaries_sector ON placex
216   USING BTREE (rank_search, geometry_sector) {{db.tablespace.address_index}}
217   WHERE class = 'boundary' and type = 'administrative'
218         and indexed_status > 0;
219
220
221 DROP SEQUENCE IF EXISTS seq_place;
222 CREATE SEQUENCE seq_place start 1;
223 GRANT SELECT on placex to "{{config.DATABASE_WEBUSER}}" ;
224 GRANT SELECT on place_addressline to "{{config.DATABASE_WEBUSER}}" ;
225 GRANT SELECT ON planet_osm_ways to "{{config.DATABASE_WEBUSER}}" ;
226 GRANT SELECT ON planet_osm_rels to "{{config.DATABASE_WEBUSER}}" ;
227 GRANT SELECT on location_area to "{{config.DATABASE_WEBUSER}}" ;
228
229 -- Table for synthetic postcodes.
230 DROP TABLE IF EXISTS location_postcode;
231 CREATE TABLE location_postcode (
232   place_id BIGINT,
233   parent_place_id BIGINT,
234   rank_search SMALLINT,
235   rank_address SMALLINT,
236   indexed_status SMALLINT,
237   indexed_date TIMESTAMP,
238   country_code varchar(2),
239   postcode TEXT,
240   geometry GEOMETRY(Geometry, 4326)
241   );
242 CREATE UNIQUE INDEX idx_postcode_id ON location_postcode USING BTREE (place_id) {{db.tablespace.search_index}};
243 CREATE INDEX idx_postcode_geometry ON location_postcode USING GIST (geometry) {{db.tablespace.address_index}};
244 GRANT SELECT ON location_postcode TO "{{config.DATABASE_WEBUSER}}" ;
245
246 -- Table to store location of entrance nodes
247 DROP TABLE IF EXISTS placex_entrance;
248 CREATE TABLE placex_entrance (
249   place_id BIGINT NOT NULL,
250   osm_id BIGINT NOT NULL,
251   type TEXT NOT NULL,
252   location GEOMETRY(Point, 4326) NOT NULL,
253   extratags HSTORE
254   );
255 CREATE UNIQUE INDEX idx_placex_entrance_place_id_osm_id ON placex_entrance
256   USING BTREE (place_id, osm_id) {{db.tablespace.search_index}};
257 GRANT SELECT ON placex_entrance TO "{{config.DATABASE_WEBUSER}}" ;
258
259 -- Create an index on the place table for lookups to populate the entrance
260 -- table
261 CREATE INDEX IF NOT EXISTS idx_placex_entrance_lookup ON place
262   USING BTREE (osm_id)
263   WHERE class IN ('routing:entrance', 'entrance');
264
265 DROP TABLE IF EXISTS import_polygon_error;
266 CREATE TABLE import_polygon_error (
267   osm_id BIGINT,
268   osm_type CHAR(1),
269   class TEXT NOT NULL,
270   type TEXT NOT NULL,
271   name HSTORE,
272   country_code varchar(2),
273   updated timestamp,
274   errormessage text,
275   prevgeometry GEOMETRY(Geometry, 4326),
276   newgeometry GEOMETRY(Geometry, 4326)
277   );
278 CREATE INDEX idx_import_polygon_error_osmid ON import_polygon_error USING BTREE (osm_type, osm_id);
279 GRANT SELECT ON import_polygon_error TO "{{config.DATABASE_WEBUSER}}";
280
281 DROP TABLE IF EXISTS import_polygon_delete;
282 CREATE TABLE import_polygon_delete (
283   osm_id BIGINT,
284   osm_type CHAR(1),
285   class TEXT NOT NULL,
286   type TEXT NOT NULL
287   );
288 CREATE INDEX idx_import_polygon_delete_osmid ON import_polygon_delete USING BTREE (osm_type, osm_id);
289 GRANT SELECT ON import_polygon_delete TO "{{config.DATABASE_WEBUSER}}";
290
291 DROP SEQUENCE IF EXISTS file;
292 CREATE SEQUENCE file start 1;
293
294 {% if 'wikimedia_importance' not in db.tables and 'wikipedia_article' not in db.tables %}
295 -- create dummy tables here, if nothing was imported
296 CREATE TABLE wikimedia_importance (
297   language TEXT NOT NULL,
298   title TEXT NOT NULL,
299   importance double precision NOT NULL,
300   wikidata TEXT
301 )  {{db.tablespace.address_data}};
302 {% endif %}
303
304 -- osm2pgsql does not create indexes on the middle tables for Nominatim
305 -- Add one for lookup of associated street relations.
306 {% if db.middle_db_format == '1' %}
307 CREATE INDEX planet_osm_rels_parts_associated_idx ON planet_osm_rels USING gin(parts)
308   {{db.tablespace.address_index}}
309   WHERE tags @> ARRAY['associatedStreet'];
310 {% else %}
311 CREATE INDEX planet_osm_rels_relation_members_idx ON planet_osm_rels USING gin(planet_osm_member_ids(members, 'R'::character(1)))
312   WITH (fastupdate=off)
313   {{db.tablespace.address_index}};
314 {% endif %}
315
316 -- Needed for lookups if a node is part of an interpolation.
317 CREATE INDEX IF NOT EXISTS idx_place_interpolations
318     ON place USING gist(geometry) {{db.tablespace.address_index}}
319     WHERE osm_type = 'W' and address ? 'interpolation';
320
321 GRANT SELECT ON table country_osm_grid to "{{config.DATABASE_WEBUSER}}";