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