]> git.openstreetmap.org Git - nominatim.git/blob - sql/tables.sql
87e58d68056c97c6d5e6e75628ed8e4f77d01b8e
[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 IF EXISTS query_log;
17 CREATE TABLE query_log (
18   starttime timestamp,
19   query text,
20   ipaddress text,
21   endtime timestamp,
22   results integer
23   );
24 CREATE INDEX idx_query_log ON query_log USING BTREE (starttime);
25 GRANT INSERT ON query_log TO "www-data" ;
26
27 CREATE TABLE new_query_log (
28   type text,
29   starttime timestamp,
30   ipaddress text,
31   useragent text,
32   language text,
33   query 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 "www-data" ;
41 GRANT UPDATE ON new_query_log TO "www-data" ;
42 GRANT SELECT ON new_query_log TO "www-data" ;
43
44 create view vw_search_query_log as SELECT substr(query, 1, 50) AS query, starttime, endtime - starttime AS duration, substr(useragent, 1, 20) as 
45 useragent, language, results, ipaddress FROM new_query_log WHERE type = 'search' ORDER BY starttime DESC;
46
47 --drop table IF EXISTS report_log;
48 CREATE TABLE report_log (
49   starttime timestamp,
50   ipaddress text,
51   query text,
52   description text,
53   email text
54   );
55 GRANT INSERT ON report_log TO "www-data" ;
56
57 drop table IF EXISTS word;
58 CREATE TABLE word (
59   word_id INTEGER,
60   word_token text,
61   word_trigram text,
62   word text,
63   class text,
64   type text,
65   country_code varchar(2),
66   search_name_count INTEGER,
67   operator TEXT
68   );
69 SELECT AddGeometryColumn('word', 'location', 4326, 'GEOMETRY', 2);
70 CREATE INDEX idx_word_word_id on word USING BTREE (word_id);
71 CREATE INDEX idx_word_word_token on word USING BTREE (word_token);
72 CREATE INDEX idx_word_trigram ON word USING gin(word_trigram gin_trgm_ops);
73 GRANT SELECT ON word TO "www-data" ;
74 DROP SEQUENCE seq_word;
75 CREATE SEQUENCE seq_word start 1;
76
77 drop table IF EXISTS location_area CASCADE;
78 CREATE TABLE location_area (
79   partition integer,
80   place_id INTEGER,
81   country_code VARCHAR(2), 
82   keywords INTEGER[],
83   rank_search INTEGER NOT NULL,
84   rank_address INTEGER NOT NULL,
85   isguess BOOL
86   );
87 SELECT AddGeometryColumn('location_area', 'centroid', 4326, 'POINT', 2);
88 SELECT AddGeometryColumn('location_area', 'geometry', 4326, 'GEOMETRY', 2);
89
90 CREATE TABLE location_area_large () INHERITS (location_area);
91 CREATE TABLE location_area_roadnear () INHERITS (location_area);
92 CREATE TABLE location_area_roadfar () INHERITS (location_area);
93
94 drop table IF EXISTS search_name_blank CASCADE;
95 CREATE TABLE search_name_blank (
96   place_id INTEGER,
97   search_rank integer,
98   address_rank integer,
99   importance FLOAT,
100   country_code varchar(2),
101   name_vector integer[],
102   nameaddress_vector integer[]
103   );
104 SELECT AddGeometryColumn('search_name_blank', 'centroid', 4326, 'GEOMETRY', 2);
105
106 drop table IF EXISTS search_name;
107 CREATE TABLE search_name () INHERITS (search_name_blank);
108 CREATE INDEX search_name_name_vector_idx ON search_name USING GIN (name_vector gin__int_ops);
109 CREATE INDEX searchnameplacesearch_search_nameaddress_vector_idx ON search_name USING GIN (nameaddress_vector gin__int_ops);
110 CREATE INDEX idx_search_name_centroid ON search_name USING GIST (centroid);
111 CREATE INDEX idx_search_name_place_id ON search_name USING BTREE (place_id);
112
113 drop table IF EXISTS place_addressline;
114 CREATE TABLE place_addressline (
115   place_id INTEGER,
116   address_place_id INTEGER,
117   fromarea boolean,
118   isaddress boolean,
119   distance float,
120   cached_rank_address integer
121   );
122 CREATE INDEX idx_place_addressline_place_id on place_addressline USING BTREE (place_id);
123 CREATE INDEX idx_place_addressline_address_place_id on place_addressline USING BTREE (address_place_id);
124
125 drop table IF EXISTS place_boundingbox CASCADE;
126 CREATE TABLE place_boundingbox (
127   place_id INTEGER,
128   minlat float,
129   maxlat float,
130   minlon float,
131   maxlon float,
132   numfeatures integer,
133   area float
134   );
135 CREATE INDEX idx_place_boundingbox_place_id on place_boundingbox USING BTREE (place_id);
136 SELECT AddGeometryColumn('place_boundingbox', 'outline', 4326, 'GEOMETRY', 2);
137 CREATE INDEX idx_place_boundingbox_outline ON place_boundingbox USING GIST (outline);
138 GRANT SELECT on place_boundingbox to "www-data" ;
139 GRANT INSERT on place_boundingbox to "www-data" ;
140
141 drop table IF EXISTS reverse_cache;
142 CREATE TABLE reverse_cache (
143   latlonzoomid integer,
144   country_code varchar(2),
145   place_id INTEGER
146   );
147 GRANT SELECT on reverse_cache to "www-data" ;
148 GRANT INSERT on reverse_cache to "www-data" ;
149 CREATE INDEX idx_reverse_cache_latlonzoomid ON reverse_cache USING BTREE (latlonzoomid);
150
151 drop table country;
152 CREATE TABLE country (
153   country_code varchar(2),
154   country_name hstore,
155   country_default_language_code varchar(2)
156   );
157 SELECT AddGeometryColumn('country', 'geometry', 4326, 'POLYGON', 2);
158 insert into country select iso3166::varchar(2), 'name:en'->cntry_name, null, 
159   ST_Transform(geometryn(the_geom, generate_series(1, numgeometries(the_geom))), 4326) from worldboundaries;
160 CREATE INDEX idx_country_country_code ON country USING BTREE (country_code);
161 CREATE INDEX idx_country_geometry ON country USING GIST (geometry);
162
163 drop table placex;
164 CREATE TABLE placex (
165   place_id INTEGER NOT NULL,
166   partition integer,
167   osm_type char(1),
168   osm_id INTEGER,
169   class TEXT NOT NULL,
170   type TEXT NOT NULL,
171   name HSTORE,
172   admin_level INTEGER,
173   housenumber TEXT,
174   street TEXT,
175   isin TEXT,
176   postcode TEXT,
177   country_code varchar(2),
178   extratags HSTORE,
179   parent_place_id INTEGER,
180   linked_place_id INTEGER,
181   rank_address INTEGER,
182   rank_search INTEGER,
183   indexed_status INTEGER,
184   indexed_date TIMESTAMP,
185   geometry_sector INTEGER
186   );
187 SELECT AddGeometryColumn('placex', 'geometry', 4326, 'GEOMETRY', 2);
188 CREATE UNIQUE INDEX idx_place_id ON placex USING BTREE (place_id);
189 CREATE INDEX idx_placex_osmid ON placex USING BTREE (osm_type, osm_id);
190 CREATE INDEX idx_placex_rank_search ON placex USING BTREE (rank_search);
191 CREATE INDEX idx_placex_rank_address ON placex USING BTREE (rank_address);
192 CREATE INDEX idx_placex_geometry ON placex USING GIST (geometry);
193
194 --CREATE INDEX idx_placex_indexed ON placex USING BTREE (indexed);
195
196 CREATE INDEX idx_placex_pending ON placex USING BTREE (rank_search) where indexed_status > 0;
197 CREATE INDEX idx_placex_pendingsector ON placex USING BTREE (rank_search,geometry_sector) where indexed_status > 0;
198
199 --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;
200
201 CREATE INDEX idx_placex_parent_place_id ON placex USING BTREE (parent_place_id) where parent_place_id IS NOT NULL;
202 CREATE INDEX idx_placex_interpolation ON placex USING BTREE (geometry_sector) where indexed_status > 0 and class='place' and type='houses';
203
204 CREATE INDEX idx_placex_sector ON placex USING BTREE (geometry_sector,rank_address,osm_type,osm_id);
205 CLUSTER placex USING idx_placex_sector;
206
207 DROP SEQUENCE seq_place;
208 CREATE SEQUENCE seq_place start 1;
209 GRANT SELECT on placex to "www-data" ;
210 GRANT UPDATE ON placex to "www-data" ;
211 GRANT SELECT ON search_name to "www-data" ;
212 GRANT DELETE on search_name to "www-data" ;
213 GRANT INSERT on search_name to "www-data" ;
214 GRANT SELECT on place_addressline to "www-data" ;
215 GRANT INSERT ON place_addressline to "www-data" ;
216 GRANT DELETE on place_addressline to "www-data" ;
217 GRANT SELECT ON seq_word to "www-data" ;
218 GRANT UPDATE ON seq_word to "www-data" ;
219 GRANT INSERT ON word to "www-data" ;
220 GRANT SELECT ON planet_osm_ways to "www-data" ;
221 GRANT SELECT ON planet_osm_rels to "www-data" ;
222 GRANT SELECT on location_area to "www-data" ;
223 GRANT SELECT on country to "www-data" ;
224
225 -- insert creates the location tagbles, creates location indexes if indexed == true
226 CREATE TRIGGER placex_before_insert BEFORE INSERT ON placex
227     FOR EACH ROW EXECUTE PROCEDURE placex_insert();
228
229 -- update insert creates the location tables
230 CREATE TRIGGER placex_before_update BEFORE UPDATE ON placex
231     FOR EACH ROW EXECUTE PROCEDURE placex_update();
232
233 -- diff update triggers
234 CREATE TRIGGER placex_before_delete AFTER DELETE ON placex
235     FOR EACH ROW EXECUTE PROCEDURE placex_delete();
236 CREATE TRIGGER place_before_delete BEFORE DELETE ON place
237     FOR EACH ROW EXECUTE PROCEDURE place_delete();
238 CREATE TRIGGER place_before_insert BEFORE INSERT ON place
239     FOR EACH ROW EXECUTE PROCEDURE place_insert();
240
241 alter table placex add column geometry_sector INTEGER;
242 alter table placex add column indexed_status INTEGER;
243 alter table placex add column indexed_date TIMESTAMP;
244
245 update placex set geometry_sector = geometry_sector(geometry);
246 drop index idx_placex_pendingbylatlon;
247 drop index idx_placex_interpolation;
248 drop index idx_placex_sector;
249 CREATE INDEX idx_placex_pendingbylatlon ON placex USING BTREE (geometry_index(geometry_sector,indexed,name),rank_search) 
250   where geometry_index(geometry_sector,indexed,name) IS NOT NULL;
251 CREATE INDEX idx_placex_interpolation ON placex USING BTREE (geometry_sector) where indexed = false and class='place' and type='houses';
252 CREATE INDEX idx_placex_sector ON placex USING BTREE (geometry_sector,rank_address,osm_type,osm_id);