]> git.openstreetmap.org Git - nominatim.git/blob - sql/tables.sql
can't have a unique index on placex - only on place
[nominatim.git] / sql / tables.sql
1 drop table import_status;
2 CREATE TABLE import_status (
3   lastimportdate timestamp NOT NULL
4   );
5 GRANT SELECT ON import_status TO "www-data" ;
6
7 drop table import_osmosis_log;
8 CREATE TABLE import_osmosis_log (
9   batchend timestamp,
10   batchsize integer,
11   starttime timestamp,
12   endtime timestamp,
13   event text
14   );
15
16 drop table import_npi_log;
17 CREATE TABLE import_npi_log (
18   npiid integer,
19   batchend timestamp,
20   batchsize integer,
21   starttime timestamp,
22   endtime timestamp,
23   event text
24   );
25
26 --drop table IF EXISTS query_log;
27 CREATE TABLE query_log (
28   starttime timestamp,
29   query text,
30   ipaddress text,
31   endtime timestamp,
32   results integer
33   );
34 CREATE INDEX idx_query_log ON query_log USING BTREE (starttime);
35 GRANT INSERT ON query_log TO "www-data" ;
36 GRANT UPDATE ON query_log TO "www-data" ;
37
38 CREATE TABLE new_query_log (
39   type text,
40   starttime timestamp,
41   ipaddress text,
42   useragent text,
43   language text,
44   query text,
45   endtime timestamp,
46   results integer,
47   format text,
48   secret text
49   );
50 CREATE INDEX idx_new_query_log_starttime ON new_query_log USING BTREE (starttime);
51 GRANT INSERT ON new_query_log TO "www-data" ;
52 GRANT UPDATE ON new_query_log TO "www-data" ;
53 GRANT SELECT ON new_query_log TO "www-data" ;
54
55 create view vw_search_query_log as SELECT substr(query, 1, 50) AS query, starttime, endtime - starttime AS duration, substr(useragent, 1, 20) as 
56 useragent, language, results, ipaddress FROM new_query_log WHERE type = 'search' ORDER BY starttime DESC;
57
58 --drop table IF EXISTS report_log;
59 CREATE TABLE report_log (
60   starttime timestamp,
61   ipaddress text,
62   query text,
63   description text,
64   email text
65   );
66 GRANT INSERT ON report_log TO "www-data" ;
67
68 drop table IF EXISTS word;
69 CREATE TABLE word (
70   word_id INTEGER,
71   word_token text,
72   word_trigram text,
73   word text,
74   class text,
75   type text,
76   country_code varchar(2),
77   search_name_count INTEGER,
78   operator TEXT
79   );
80 SELECT AddGeometryColumn('word', 'location', 4326, 'GEOMETRY', 2);
81 CREATE INDEX idx_word_word_token on word USING BTREE (word_token);
82 --CREATE INDEX idx_word_trigram ON word USING gin(word_trigram gin_trgm_ops) WITH (fastupdate = off);
83 GRANT SELECT ON word TO "www-data" ;
84 DROP SEQUENCE seq_word;
85 CREATE SEQUENCE seq_word start 1;
86
87 drop table IF EXISTS location_area CASCADE;
88 CREATE TABLE location_area (
89   partition integer,
90   place_id BIGINT,
91   country_code VARCHAR(2), 
92   keywords INTEGER[],
93   rank_search INTEGER NOT NULL,
94   rank_address INTEGER NOT NULL,
95   isguess BOOL
96   );
97 SELECT AddGeometryColumn('location_area', 'centroid', 4326, 'POINT', 2);
98 SELECT AddGeometryColumn('location_area', 'geometry', 4326, 'GEOMETRY', 2);
99
100 CREATE TABLE location_area_large () INHERITS (location_area);
101 CREATE TABLE location_area_roadnear () INHERITS (location_area);
102 CREATE TABLE location_area_roadfar () INHERITS (location_area);
103
104 drop table IF EXISTS location_property CASCADE;
105 CREATE TABLE location_property (
106   place_id BIGINT,
107   partition integer,
108   parent_place_id BIGINT,
109   housenumber TEXT,
110   postcode TEXT
111   );
112 SELECT AddGeometryColumn('location_property', 'centroid', 4326, 'POINT', 2);
113
114 CREATE TABLE location_property_aux () INHERITS (location_property);
115 CREATE INDEX idx_location_property_aux_place_id ON location_property_aux USING BTREE (place_id);
116 CREATE INDEX idx_location_property_aux_parent_place_id ON location_property_aux USING BTREE (parent_place_id);
117 CREATE INDEX idx_location_property_aux_housenumber_parent_place_id ON location_property_aux USING BTREE (parent_place_id, housenumber);
118 GRANT SELECT ON location_property_aux TO "www-data";
119
120 CREATE TABLE location_property_tiger () INHERITS (location_property);
121 CREATE INDEX idx_location_property_tiger_place_id ON location_property_tiger USING BTREE (place_id);
122 CREATE INDEX idx_location_property_tiger_parent_place_id ON location_property_tiger USING BTREE (parent_place_id);
123 CREATE INDEX idx_location_property_tiger_housenumber_parent_place_id ON location_property_tiger USING BTREE (parent_place_id, housenumber);
124 GRANT SELECT ON location_property_tiger TO "www-data";
125
126 drop table IF EXISTS search_name_blank CASCADE;
127 CREATE TABLE search_name_blank (
128   place_id BIGINT,
129   search_rank integer,
130   address_rank integer,
131   importance FLOAT,
132   country_code varchar(2),
133   name_vector integer[],
134   nameaddress_vector integer[]
135   );
136 SELECT AddGeometryColumn('search_name_blank', 'centroid', 4326, 'GEOMETRY', 2);
137
138 drop table IF EXISTS search_name;
139 CREATE TABLE search_name () INHERITS (search_name_blank);
140 CREATE INDEX idx_search_name_place_id ON search_name USING BTREE (place_id);
141
142 drop table IF EXISTS place_addressline;
143 CREATE TABLE place_addressline (
144   place_id BIGINT,
145   address_place_id BIGINT,
146   fromarea boolean,
147   isaddress boolean,
148   distance float,
149   cached_rank_address integer
150   );
151 CREATE INDEX idx_place_addressline_place_id on place_addressline USING BTREE (place_id);
152
153 drop table IF EXISTS place_boundingbox CASCADE;
154 CREATE TABLE place_boundingbox (
155   place_id BIGINT,
156   minlat float,
157   maxlat float,
158   minlon float,
159   maxlon float,
160   numfeatures integer,
161   area float
162   );
163 SELECT AddGeometryColumn('place_boundingbox', 'outline', 4326, 'GEOMETRY', 2);
164 GRANT SELECT on place_boundingbox to "www-data" ;
165 GRANT INSERT on place_boundingbox to "www-data" ;
166
167 drop table IF EXISTS reverse_cache;
168 CREATE TABLE reverse_cache (
169   latlonzoomid integer,
170   country_code varchar(2),
171   place_id BIGINT
172   );
173 GRANT SELECT on reverse_cache to "www-data" ;
174 GRANT INSERT on reverse_cache to "www-data" ;
175 CREATE INDEX idx_reverse_cache_latlonzoomid ON reverse_cache USING BTREE (latlonzoomid);
176
177 drop table country;
178 CREATE TABLE country (
179   country_code varchar(2),
180   country_name hstore,
181   country_default_language_code varchar(2)
182   );
183 SELECT AddGeometryColumn('country', 'geometry', 4326, 'POLYGON', 2);
184 insert into country select iso3166::varchar(2), 'name:en'->cntry_name, null, 
185   ST_Transform(geometryn(the_geom, generate_series(1, numgeometries(the_geom))), 4326) from worldboundaries;
186 CREATE INDEX idx_country_country_code ON country USING BTREE (country_code);
187 CREATE INDEX idx_country_geometry ON country USING GIST (geometry);
188
189 drop table placex;
190 CREATE TABLE placex (
191   place_id BIGINT NOT NULL,
192   partition integer,
193   LIKE place INCLUDING CONSTRAINTS,
194   parent_place_id BIGINT,
195   linked_place_id BIGINT,
196   rank_address INTEGER,
197   rank_search INTEGER,
198   importance FLOAT,
199   indexed_status INTEGER,
200   indexed_date TIMESTAMP,
201   wikipedia TEXT, -- calculated wikipedia article name (language:title)
202   geometry_sector INTEGER,
203   calculated_country_code varchar(2)
204   );
205 SELECT AddGeometryColumn('placex', 'centroid', 4326, 'GEOMETRY', 2);
206 CREATE UNIQUE INDEX idx_place_id ON placex USING BTREE (place_id);
207 CREATE INDEX idx_placex_osmid ON placex USING BTREE (osm_type, osm_id);
208 CREATE INDEX idx_placex_linked_place_id ON placex USING BTREE (linked_place_id);
209 CREATE INDEX idx_placex_rank_search ON placex USING BTREE (rank_search, geometry_sector);
210 CREATE INDEX idx_placex_geometry ON placex USING GIST (geometry);
211 CREATE INDEX idx_placex_adminname on placex USING BTREE (make_standard_name(name->'name'),rank_search) WHERE osm_type='N' and rank_search < 26;
212
213 --CREATE INDEX idx_placex_indexed ON placex USING BTREE (indexed);
214
215 --CREATE INDEX idx_placex_pendingbylatlon ON placex USING BTREE (geometry_index(geometry_sector,indexed,name),rank_search)  where geometry_index(geometry_sector,indexed,name) IS NOT NULL;
216
217 DROP SEQUENCE seq_place;
218 CREATE SEQUENCE seq_place start 1;
219 GRANT SELECT on placex to "www-data" ;
220 GRANT UPDATE ON placex to "www-data" ;
221 GRANT SELECT ON search_name to "www-data" ;
222 GRANT DELETE on search_name to "www-data" ;
223 GRANT INSERT on search_name to "www-data" ;
224 GRANT SELECT on place_addressline to "www-data" ;
225 GRANT INSERT ON place_addressline to "www-data" ;
226 GRANT DELETE on place_addressline to "www-data" ;
227 GRANT SELECT ON seq_word to "www-data" ;
228 GRANT UPDATE ON seq_word to "www-data" ;
229 GRANT INSERT ON word to "www-data" ;
230 GRANT SELECT ON planet_osm_ways to "www-data" ;
231 GRANT SELECT ON planet_osm_rels to "www-data" ;
232 GRANT SELECT on location_area to "www-data" ;
233 GRANT SELECT on country to "www-data" ;
234
235 -- insert creates the location tagbles, creates location indexes if indexed == true
236 CREATE TRIGGER placex_before_insert BEFORE INSERT ON placex
237     FOR EACH ROW EXECUTE PROCEDURE placex_insert();
238
239 -- update insert creates the location tables
240 CREATE TRIGGER placex_before_update BEFORE UPDATE ON placex
241     FOR EACH ROW EXECUTE PROCEDURE placex_update();
242
243 -- diff update triggers
244 CREATE TRIGGER placex_before_delete AFTER DELETE ON placex
245     FOR EACH ROW EXECUTE PROCEDURE placex_delete();
246 CREATE TRIGGER place_before_delete BEFORE DELETE ON place
247     FOR EACH ROW EXECUTE PROCEDURE place_delete();
248 CREATE TRIGGER place_before_insert BEFORE INSERT ON place
249     FOR EACH ROW EXECUTE PROCEDURE place_insert();
250
251 alter table placex add column geometry_sector INTEGER;
252 alter table placex add column indexed_status INTEGER;
253 alter table placex add column indexed_date TIMESTAMP;
254
255 update placex set geometry_sector = geometry_sector(geometry);
256 drop index idx_placex_pendingbylatlon;
257 drop index idx_placex_interpolation;
258 drop index idx_placex_sector;
259 CREATE INDEX idx_placex_pendingbylatlon ON placex USING BTREE (geometry_index(geometry_sector,indexed,name),rank_search) 
260   where geometry_index(geometry_sector,indexed,name) IS NOT NULL;
261 CREATE INDEX idx_placex_interpolation ON placex USING BTREE (geometry_sector) where indexed = false and class='place' and type='houses';
262 CREATE INDEX idx_placex_sector ON placex USING BTREE (geometry_sector,rank_address,osm_type,osm_id);
263
264 DROP SEQUENCE seq_postcodes;
265 CREATE SEQUENCE seq_postcodes start 1;
266
267 drop table import_polygon_error;
268 CREATE TABLE import_polygon_error (
269   osm_type char(1),
270   osm_id INTEGER,
271   class TEXT NOT NULL,
272   type TEXT NOT NULL,
273   name HSTORE,
274   country_code varchar(2),
275   updated timestamp,
276   errormessage text
277   );
278 SELECT AddGeometryColumn('import_polygon_error', 'prevgeometry', 4326, 'GEOMETRY', 2);
279 SELECT AddGeometryColumn('import_polygon_error', 'newgeometry', 4326, 'GEOMETRY', 2);
280 CREATE INDEX idx_import_polygon_error_osmid ON import_polygon_error USING BTREE (osm_type, osm_id);
281
282 drop table import_polygon_delete;
283 CREATE TABLE import_polygon_delete (
284   osm_type char(1),
285   osm_id INTEGER,
286   class TEXT NOT NULL,
287   type TEXT NOT NULL
288   );
289 CREATE INDEX idx_import_polygon_delete_osmid ON import_polygon_delete USING BTREE (osm_type, osm_id);
290
291 drop sequence file;
292 CREATE SEQUENCE file start 1;
293
294 -- null table so it won't error
295 -- deliberately no drop - importing the table is expensive and static, if it is already there better to avoid removing it
296 CREATE TABLE wikipedia_article (
297     language text NOT NULL,
298     title text NOT NULL,
299     langcount integer,
300     othercount integer,
301     totalcount integer,
302     lat double precision,
303     lon double precision,
304     importance double precision,
305     osm_type character(1),
306     osm_id bigint
307 );
308 ALTER TABLE ONLY wikipedia_article ADD CONSTRAINT wikipedia_article_pkey PRIMARY KEY (language, title);
309 CREATE INDEX idx_wikipedia_article_osm_id ON wikipedia_article USING btree (osm_type, osm_id);
310
311 CREATE TABLE wikipedia_redirect (
312     language text,
313     from_title text,
314     to_title text
315 );
316 ALTER TABLE ONLY wikipedia_redirect ADD CONSTRAINT wikipedia_redirect_pkey PRIMARY KEY (language, from_title);
317