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