]> git.openstreetmap.org Git - nominatim.git/blob - sql/tables.sql
07b0ada51b461576ec71c2f13322fa59dd2a8766
[nominatim.git] / sql / tables.sql
1 drop table if exists import_status;
2 CREATE TABLE import_status (
3   lastimportdate timestamp NOT NULL,
4   sequence_id integer,
5   indexed boolean
6   );
7 GRANT SELECT ON import_status TO "{www-user}" ;
8
9 drop table if exists import_osmosis_log;
10 CREATE TABLE import_osmosis_log (
11   batchend timestamp,
12   batchseq integer,
13   batchsize integer,
14   starttime timestamp,
15   endtime timestamp,
16   event text
17   );
18
19 CREATE TABLE new_query_log (
20   type text,
21   starttime timestamp,
22   ipaddress text,
23   useragent text,
24   language text,
25   query text,
26   searchterm text,
27   endtime timestamp,
28   results integer,
29   format text,
30   secret text
31   );
32 CREATE INDEX idx_new_query_log_starttime ON new_query_log USING BTREE (starttime);
33 GRANT INSERT ON new_query_log TO "{www-user}" ;
34 GRANT UPDATE ON new_query_log TO "{www-user}" ;
35 GRANT SELECT ON new_query_log TO "{www-user}" ;
36
37 GRANT SELECT ON TABLE country_name TO "{www-user}";
38 GRANT SELECT ON TABLE gb_postcode TO "{www-user}";
39
40 drop table IF EXISTS word;
41 CREATE TABLE word (
42   word_id INTEGER,
43   word_token text,
44   word text,
45   class text,
46   type text,
47   country_code varchar(2),
48   search_name_count INTEGER,
49   operator TEXT
50   ) {ts:search-data};
51 CREATE INDEX idx_word_word_token on word USING BTREE (word_token) {ts:search-index};
52 GRANT SELECT ON word TO "{www-user}" ;
53 DROP SEQUENCE IF EXISTS seq_word;
54 CREATE SEQUENCE seq_word start 1;
55
56 drop table IF EXISTS location_area CASCADE;
57 CREATE TABLE location_area (
58   place_id BIGINT,
59   keywords INTEGER[],
60   partition SMALLINT,
61   rank_search SMALLINT NOT NULL,
62   rank_address SMALLINT NOT NULL,
63   country_code VARCHAR(2),
64   isguess BOOL
65   );
66 SELECT AddGeometryColumn('location_area', 'centroid', 4326, 'POINT', 2);
67 SELECT AddGeometryColumn('location_area', 'geometry', 4326, 'GEOMETRY', 2);
68
69 CREATE TABLE location_area_large () INHERITS (location_area);
70
71 drop table IF EXISTS location_property CASCADE;
72 CREATE TABLE location_property (
73   place_id BIGINT,
74   parent_place_id BIGINT,
75   partition SMALLINT,
76   housenumber TEXT,
77   postcode TEXT
78   );
79 SELECT AddGeometryColumn('location_property', 'centroid', 4326, 'POINT', 2);
80
81 CREATE TABLE location_property_aux () INHERITS (location_property);
82 CREATE INDEX idx_location_property_aux_place_id ON location_property_aux USING BTREE (place_id);
83 CREATE INDEX idx_location_property_aux_parent_place_id ON location_property_aux USING BTREE (parent_place_id);
84 CREATE INDEX idx_location_property_aux_housenumber_parent_place_id ON location_property_aux USING BTREE (parent_place_id, housenumber);
85 GRANT SELECT ON location_property_aux TO "{www-user}";
86
87 CREATE TABLE location_property_tiger (
88   place_id BIGINT,
89   parent_place_id BIGINT,
90   startnumber INTEGER,
91   endnumber INTEGER,
92   partition SMALLINT,
93   linegeo GEOMETRY,
94   interpolationtype TEXT,
95   postcode TEXT);
96 GRANT SELECT ON location_property_tiger TO "{www-user}";
97
98 drop table if exists location_property_osmline;
99 CREATE TABLE location_property_osmline (
100     place_id BIGINT NOT NULL,
101     osm_id BIGINT,
102     parent_place_id BIGINT,
103     geometry_sector INTEGER,
104     indexed_date TIMESTAMP,
105     startnumber INTEGER,
106     endnumber INTEGER,
107     partition SMALLINT,
108     indexed_status SMALLINT,
109     linegeo GEOMETRY,
110     interpolationtype TEXT,
111     address HSTORE,
112     postcode TEXT,
113     country_code VARCHAR(2)
114   ){ts:search-data};
115 CREATE UNIQUE INDEX idx_osmline_place_id ON location_property_osmline USING BTREE (place_id) {ts:search-index};
116 CREATE INDEX idx_osmline_geometry_sector ON location_property_osmline USING BTREE (geometry_sector) {ts:address-index};
117 CREATE INDEX idx_osmline_linegeo ON location_property_osmline USING GIST (linegeo) {ts:search-index};
118 GRANT SELECT ON location_property_osmline TO "{www-user}";
119
120 drop table IF EXISTS search_name;
121 CREATE TABLE search_name (
122   place_id BIGINT,
123   importance FLOAT,
124   search_rank SMALLINT,
125   address_rank SMALLINT,
126   name_vector integer[],
127   nameaddress_vector integer[],
128   country_code varchar(2)
129   ) {ts:search-data};
130 SELECT AddGeometryColumn('search_name', 'centroid', 4326, 'GEOMETRY', 2);
131 CREATE INDEX idx_search_name_place_id ON search_name USING BTREE (place_id) {ts:search-index};
132
133 drop table IF EXISTS place_addressline;
134 CREATE TABLE place_addressline (
135   place_id BIGINT,
136   address_place_id BIGINT,
137   distance FLOAT,
138   cached_rank_address SMALLINT,
139   fromarea boolean,
140   isaddress boolean
141   ) {ts:search-data};
142 CREATE INDEX idx_place_addressline_place_id on place_addressline USING BTREE (place_id) {ts:search-index};
143
144 drop table if exists placex;
145 CREATE TABLE placex (
146   place_id BIGINT NOT NULL,
147   parent_place_id BIGINT,
148   linked_place_id BIGINT,
149   importance FLOAT,
150   indexed_date TIMESTAMP,
151   geometry_sector INTEGER,
152   rank_address SMALLINT,
153   rank_search SMALLINT,
154   partition SMALLINT,
155   indexed_status SMALLINT,
156   LIKE place INCLUDING CONSTRAINTS,
157   wikipedia TEXT, -- calculated wikipedia article name (language:title)
158   country_code varchar(2),
159   housenumber TEXT,
160   postcode TEXT
161   ) {ts:search-data};
162 SELECT AddGeometryColumn('placex', 'centroid', 4326, 'GEOMETRY', 2);
163 CREATE UNIQUE INDEX idx_place_id ON placex USING BTREE (place_id) {ts:search-index};
164 CREATE INDEX idx_placex_osmid ON placex USING BTREE (osm_type, osm_id) {ts:search-index};
165 CREATE INDEX idx_placex_linked_place_id ON placex USING BTREE (linked_place_id) {ts:address-index} WHERE linked_place_id IS NOT NULL;
166 CREATE INDEX idx_placex_rank_search ON placex USING BTREE (rank_search, geometry_sector) {ts:address-index};
167 CREATE INDEX idx_placex_geometry ON placex USING GIST (geometry) {ts:search-index};
168 CREATE INDEX idx_placex_adminname on placex USING BTREE (make_standard_name(name->'name'),rank_search) {ts:address-index} WHERE osm_type='N' and rank_search < 26;
169
170 DROP SEQUENCE IF EXISTS seq_place;
171 CREATE SEQUENCE seq_place start 1;
172 GRANT SELECT on placex to "{www-user}" ;
173 GRANT SELECT ON search_name to "{www-user}" ;
174 GRANT SELECT on place_addressline to "{www-user}" ;
175 GRANT SELECT ON seq_word to "{www-user}" ;
176 GRANT SELECT ON planet_osm_ways to "{www-user}" ;
177 GRANT SELECT ON planet_osm_rels to "{www-user}" ;
178 GRANT SELECT on location_area to "{www-user}" ;
179
180 -- insert creates the location tables, creates location indexes if indexed == true
181 CREATE TRIGGER placex_before_insert BEFORE INSERT ON placex
182     FOR EACH ROW EXECUTE PROCEDURE placex_insert();
183 CREATE TRIGGER osmline_before_insert BEFORE INSERT ON location_property_osmline
184     FOR EACH ROW EXECUTE PROCEDURE osmline_insert();
185
186 -- update insert creates the location tables
187 CREATE TRIGGER placex_before_update BEFORE UPDATE ON placex
188     FOR EACH ROW EXECUTE PROCEDURE placex_update();
189 CREATE TRIGGER osmline_before_update BEFORE UPDATE ON location_property_osmline
190     FOR EACH ROW EXECUTE PROCEDURE osmline_update();
191
192 -- diff update triggers
193 CREATE TRIGGER placex_before_delete AFTER DELETE ON placex
194     FOR EACH ROW EXECUTE PROCEDURE placex_delete();
195 CREATE TRIGGER place_before_delete BEFORE DELETE ON place
196     FOR EACH ROW EXECUTE PROCEDURE place_delete();
197 CREATE TRIGGER place_before_insert BEFORE INSERT ON place
198     FOR EACH ROW EXECUTE PROCEDURE place_insert();
199
200 DROP TABLE IF EXISTS location_postcode;
201 CREATE TABLE location_postcode (
202   place_id BIGINT,
203   parent_place_id BIGINT,
204   rank_address SMALLINT,
205   indexed_status SMALLINT,
206   indexed_date TIMESTAMP,
207   country_code varchar(2),
208   postcode TEXT,
209   geometry GEOMETRY(Geometry, 4326)
210   );
211
212 DROP TABLE IF EXISTS import_polygon_error;
213 CREATE TABLE import_polygon_error (
214   osm_id BIGINT,
215   osm_type CHAR(1),
216   class TEXT NOT NULL,
217   type TEXT NOT NULL,
218   name HSTORE,
219   country_code varchar(2),
220   updated timestamp,
221   errormessage text
222   );
223 SELECT AddGeometryColumn('import_polygon_error', 'prevgeometry', 4326, 'GEOMETRY', 2);
224 SELECT AddGeometryColumn('import_polygon_error', 'newgeometry', 4326, 'GEOMETRY', 2);
225 CREATE INDEX idx_import_polygon_error_osmid ON import_polygon_error USING BTREE (osm_type, osm_id);
226 GRANT SELECT ON import_polygon_error TO "{www-user}";
227
228 DROP TABLE IF EXISTS import_polygon_delete;
229 CREATE TABLE import_polygon_delete (
230   osm_id BIGINT,
231   osm_type CHAR(1),
232   class TEXT NOT NULL,
233   type TEXT NOT NULL
234   );
235 CREATE INDEX idx_import_polygon_delete_osmid ON import_polygon_delete USING BTREE (osm_type, osm_id);
236 GRANT SELECT ON import_polygon_delete TO "{www-user}";
237
238 DROP SEQUENCE IF EXISTS file;
239 CREATE SEQUENCE file start 1;
240
241 -- null table so it won't error
242 -- deliberately no drop - importing the table is expensive and static, if it is already there better to avoid removing it
243 CREATE TABLE wikipedia_article (
244     language text NOT NULL,
245     title text NOT NULL,
246     langcount integer,
247     othercount integer,
248     totalcount integer,
249     lat double precision,
250     lon double precision,
251     importance double precision,
252     osm_type character(1),
253     osm_id bigint
254 );
255 ALTER TABLE ONLY wikipedia_article ADD CONSTRAINT wikipedia_article_pkey PRIMARY KEY (language, title);
256 CREATE INDEX idx_wikipedia_article_osm_id ON wikipedia_article USING btree (osm_type, osm_id);
257
258 CREATE TABLE wikipedia_redirect (
259     language text,
260     from_title text,
261     to_title text
262 );
263 ALTER TABLE ONLY wikipedia_redirect ADD CONSTRAINT wikipedia_redirect_pkey PRIMARY KEY (language, from_title);
264