]> git.openstreetmap.org Git - nominatim.git/blob - lib-sql/tables.sql
add consistent SPDX copyright headers
[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) 2022 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,
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   partition SMALLINT,
84   linegeo GEOMETRY,
85   interpolationtype TEXT,
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     partition SMALLINT,
99     indexed_status SMALLINT,
100     linegeo GEOMETRY,
101     interpolationtype TEXT,
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 GRANT SELECT ON location_property_osmline TO "{{config.DATABASE_WEBUSER}}";
111
112 drop table IF EXISTS search_name;
113 {% if not db.reverse_only %}
114 CREATE TABLE search_name (
115   place_id BIGINT,
116   importance FLOAT,
117   search_rank SMALLINT,
118   address_rank SMALLINT,
119   name_vector integer[],
120   nameaddress_vector integer[],
121   country_code varchar(2),
122   centroid GEOMETRY(Geometry, 4326)
123   ) {{db.tablespace.search_data}};
124 CREATE INDEX idx_search_name_place_id ON search_name USING BTREE (place_id) {{db.tablespace.search_index}};
125 GRANT SELECT ON search_name to "{{config.DATABASE_WEBUSER}}" ;
126 {% endif %}
127
128 drop table IF EXISTS place_addressline;
129 CREATE TABLE place_addressline (
130   place_id BIGINT,
131   address_place_id BIGINT,
132   distance FLOAT,
133   cached_rank_address SMALLINT,
134   fromarea boolean,
135   isaddress boolean
136   ) {{db.tablespace.search_data}};
137 CREATE INDEX idx_place_addressline_place_id on place_addressline USING BTREE (place_id) {{db.tablespace.search_index}};
138
139 drop table if exists placex;
140 CREATE TABLE placex (
141   place_id BIGINT NOT NULL,
142   parent_place_id BIGINT,
143   linked_place_id BIGINT,
144   importance FLOAT,
145   indexed_date TIMESTAMP,
146   geometry_sector INTEGER,
147   rank_address SMALLINT,
148   rank_search SMALLINT,
149   partition SMALLINT,
150   indexed_status SMALLINT,
151   LIKE place INCLUDING CONSTRAINTS,
152   wikipedia TEXT, -- calculated wikipedia article name (language:title)
153   token_info JSONB, -- custom column for tokenizer use only
154   country_code varchar(2),
155   housenumber TEXT,
156   postcode TEXT,
157   centroid GEOMETRY(Geometry, 4326)
158   ) {{db.tablespace.search_data}};
159 CREATE UNIQUE INDEX idx_place_id ON placex USING BTREE (place_id) {{db.tablespace.search_index}};
160 CREATE INDEX idx_placex_osmid ON placex USING BTREE (osm_type, osm_id) {{db.tablespace.search_index}};
161 CREATE INDEX idx_placex_linked_place_id ON placex USING BTREE (linked_place_id) {{db.tablespace.address_index}} WHERE linked_place_id IS NOT NULL;
162 CREATE INDEX idx_placex_rank_search ON placex USING BTREE (rank_search, geometry_sector) {{db.tablespace.address_index}};
163 CREATE INDEX idx_placex_geometry ON placex USING GIST (geometry) {{db.tablespace.search_index}};
164 CREATE INDEX idx_placex_geometry_buildings ON placex
165   USING {{postgres.spgist_geom}} (geometry) {{db.tablespace.search_index}}
166   WHERE address is not null and rank_search = 30
167         and ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon');
168 CREATE INDEX idx_placex_geometry_placenode ON placex
169   USING {{postgres.spgist_geom}} (geometry) {{db.tablespace.search_index}}
170   WHERE osm_type = 'N' and rank_search < 26
171         and class = 'place' and type != 'postcode' and linked_place_id is null;
172 CREATE INDEX idx_placex_wikidata on placex USING BTREE ((extratags -> 'wikidata')) {{db.tablespace.address_index}} WHERE extratags ? 'wikidata' and class = 'place' and osm_type = 'N' and rank_search < 26;
173
174 DROP SEQUENCE IF EXISTS seq_place;
175 CREATE SEQUENCE seq_place start 1;
176 GRANT SELECT on placex to "{{config.DATABASE_WEBUSER}}" ;
177 GRANT SELECT on place_addressline to "{{config.DATABASE_WEBUSER}}" ;
178 GRANT SELECT ON planet_osm_ways to "{{config.DATABASE_WEBUSER}}" ;
179 GRANT SELECT ON planet_osm_rels to "{{config.DATABASE_WEBUSER}}" ;
180 GRANT SELECT on location_area to "{{config.DATABASE_WEBUSER}}" ;
181
182 -- Table for synthetic postcodes.
183 DROP TABLE IF EXISTS location_postcode;
184 CREATE TABLE location_postcode (
185   place_id BIGINT,
186   parent_place_id BIGINT,
187   rank_search SMALLINT,
188   rank_address SMALLINT,
189   indexed_status SMALLINT,
190   indexed_date TIMESTAMP,
191   country_code varchar(2),
192   postcode TEXT,
193   geometry GEOMETRY(Geometry, 4326)
194   );
195 CREATE UNIQUE INDEX idx_postcode_id ON location_postcode USING BTREE (place_id) {{db.tablespace.search_index}};
196 CREATE INDEX idx_postcode_geometry ON location_postcode USING GIST (geometry) {{db.tablespace.address_index}};
197 GRANT SELECT ON location_postcode TO "{{config.DATABASE_WEBUSER}}" ;
198
199 DROP TABLE IF EXISTS import_polygon_error;
200 CREATE TABLE import_polygon_error (
201   osm_id BIGINT,
202   osm_type CHAR(1),
203   class TEXT NOT NULL,
204   type TEXT NOT NULL,
205   name HSTORE,
206   country_code varchar(2),
207   updated timestamp,
208   errormessage text,
209   prevgeometry GEOMETRY(Geometry, 4326),
210   newgeometry GEOMETRY(Geometry, 4326)
211   );
212 CREATE INDEX idx_import_polygon_error_osmid ON import_polygon_error USING BTREE (osm_type, osm_id);
213 GRANT SELECT ON import_polygon_error TO "{{config.DATABASE_WEBUSER}}";
214
215 DROP TABLE IF EXISTS import_polygon_delete;
216 CREATE TABLE import_polygon_delete (
217   osm_id BIGINT,
218   osm_type CHAR(1),
219   class TEXT NOT NULL,
220   type TEXT NOT NULL
221   );
222 CREATE INDEX idx_import_polygon_delete_osmid ON import_polygon_delete USING BTREE (osm_type, osm_id);
223 GRANT SELECT ON import_polygon_delete TO "{{config.DATABASE_WEBUSER}}";
224
225 DROP SEQUENCE IF EXISTS file;
226 CREATE SEQUENCE file start 1;
227
228 -- null table so it won't error
229 -- deliberately no drop - importing the table is expensive and static, if it is already there better to avoid removing it
230 CREATE TABLE wikipedia_article (
231     language text NOT NULL,
232     title text NOT NULL,
233     langcount integer,
234     othercount integer,
235     totalcount integer,
236     lat double precision,
237     lon double precision,
238     importance double precision,
239     osm_type character(1),
240     osm_id bigint,
241     wd_page_title text,
242     instance_of text
243 );
244 ALTER TABLE ONLY wikipedia_article ADD CONSTRAINT wikipedia_article_pkey PRIMARY KEY (language, title);
245 CREATE INDEX idx_wikipedia_article_osm_id ON wikipedia_article USING btree (osm_type, osm_id);
246
247 CREATE TABLE wikipedia_redirect (
248     language text,
249     from_title text,
250     to_title text
251 );
252 ALTER TABLE ONLY wikipedia_redirect ADD CONSTRAINT wikipedia_redirect_pkey PRIMARY KEY (language, from_title);
253
254 -- osm2pgsql does not create indexes on the middle tables for Nominatim
255 -- Add one for lookup of associated street relations.
256 CREATE INDEX planet_osm_rels_parts_associated_idx ON planet_osm_rels USING gin(parts) WHERE tags @> ARRAY['associatedStreet'];
257
258 GRANT SELECT ON table country_osm_grid to "{{config.DATABASE_WEBUSER}}";