1 #! /usr/bin/env python3
2 #-----------------------------------------------------------------------------
3 # nominatim - [description]
4 #-----------------------------------------------------------------------------
6 # Indexing tool for the Nominatim database.
8 # Based on C version by Brian Quinion
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #-----------------------------------------------------------------------------
25 from argparse import ArgumentParser, RawDescriptionHelpFormatter, ArgumentTypeError
30 from datetime import datetime
32 from psycopg2.extras import wait_select
35 from indexer.progress import ProgressLogger
37 log = logging.getLogger()
39 def make_connection(options, asynchronous=False):
40 params = {'dbname' : options.dbname,
41 'user' : options.user,
42 'password' : options.password,
43 'host' : options.host,
44 'port' : options.port,
45 'async' : asynchronous}
47 return psycopg2.connect(**params)
50 class RankRunner(object):
51 """ Returns SQL commands for indexing one rank within the placex table.
54 def __init__(self, rank):
58 return "rank {}".format(self.rank)
60 def sql_count_objects(self):
61 return """SELECT count(*) FROM placex
62 WHERE rank_search = {} and indexed_status > 0
65 def sql_get_objects(self):
66 return """SELECT place_id FROM placex
67 WHERE indexed_status > 0 and rank_search = {}
68 ORDER BY geometry_sector""".format(self.rank)
70 def sql_index_place(self, ids):
71 return "UPDATE placex SET indexed_status = 0 WHERE place_id IN ({})"\
72 .format(','.join((str(i) for i in ids)))
75 class InterpolationRunner(object):
76 """ Returns SQL commands for indexing the address interpolation table
77 location_property_osmline.
81 return "interpolation lines (location_property_osmline)"
83 def sql_count_objects(self):
84 return """SELECT count(*) FROM location_property_osmline
85 WHERE indexed_status > 0"""
87 def sql_get_objects(self):
88 return """SELECT place_id FROM location_property_osmline
89 WHERE indexed_status > 0
90 ORDER BY geometry_sector"""
92 def sql_index_place(self, ids):
93 return """UPDATE location_property_osmline
94 SET indexed_status = 0 WHERE place_id IN ({})"""\
95 .format(','.join((str(i) for i in ids)))
98 class DBConnection(object):
99 """ A single non-blocking database connection.
102 def __init__(self, options):
103 self.current_query = None
104 self.current_params = None
110 if self.conn is not None:
114 self.conn = make_connection(options, asynchronous=True)
117 self.cursor = self.conn.cursor()
118 # Disable JIT and parallel workers as they are known to cause problems.
119 # Update pg_settings instead of using SET because it does not yield
120 # errors on older versions of Postgres where the settings are not
123 """ UPDATE pg_settings SET setting = -1 WHERE name = 'jit_above_cost';
124 UPDATE pg_settings SET setting = 0
125 WHERE name = 'max_parallel_workers_per_gather';""")
129 """ Block until any pending operation is done.
133 wait_select(self.conn)
134 self.current_query = None
136 except psycopg2.extensions.TransactionRollbackError as e:
137 if e.pgcode == '40P01':
138 log.info("Deadlock detected (params = {}), retry."
139 .format(self.current_params))
140 self.cursor.execute(self.current_query, self.current_params)
143 except psycopg2.errors.DeadlockDetected:
144 self.cursor.execute(self.current_query, self.current_params)
146 def perform(self, sql, args=None):
147 """ Send SQL query to the server. Returns immediately without
150 self.current_query = sql
151 self.current_params = args
152 self.cursor.execute(sql, args)
155 """ File descriptor to wait for. (Makes this class select()able.)
157 return self.conn.fileno()
160 """ Check if the connection is available for a new query.
162 Also checks if the previous query has run into a deadlock.
163 If so, then the previous query is repeated.
165 if self.current_query is None:
169 if self.conn.poll() == psycopg2.extensions.POLL_OK:
170 self.current_query = None
172 except psycopg2.extensions.TransactionRollbackError as e:
173 if e.pgcode == '40P01':
174 log.info("Deadlock detected (params = {}), retry.".format(self.current_params))
175 self.cursor.execute(self.current_query, self.current_params)
178 except psycopg2.errors.DeadlockDetected:
179 self.cursor.execute(self.current_query, self.current_params)
184 class Indexer(object):
185 """ Main indexing routine.
188 def __init__(self, options):
189 self.minrank = max(0, options.minrank)
190 self.maxrank = min(30, options.maxrank)
191 self.conn = make_connection(options)
192 self.threads = [DBConnection(options) for i in range(options.threads)]
195 """ Run indexing over the entire database.
197 log.warning("Starting indexing rank ({} to {}) using {} threads".format(
198 self.minrank, self.maxrank, len(self.threads)))
200 for rank in range(self.minrank, self.maxrank):
201 self.index(RankRunner(rank))
203 if self.maxrank == 30:
204 self.index(InterpolationRunner(), 20)
206 self.index(RankRunner(self.maxrank), 20)
208 def index(self, obj, batch=1):
209 """ Index a single rank or table. `obj` describes the SQL to use
210 for indexing. `batch` describes the number of objects that
211 should be processed with a single SQL statement
213 log.warning("Starting {}".format(obj.name()))
215 cur = self.conn.cursor()
216 cur.execute(obj.sql_count_objects())
218 total_tuples = cur.fetchone()[0]
219 log.debug("Total number of rows: {}".format(total_tuples))
223 next_thread = self.find_free_thread()
224 progress = ProgressLogger(obj.name(), total_tuples)
226 cur = self.conn.cursor(name='places')
227 cur.execute(obj.sql_get_objects())
230 places = [p[0] for p in cur.fetchmany(batch)]
234 log.debug("Processing places: {}".format(places))
235 thread = next(next_thread)
237 thread.perform(obj.sql_index_place(places))
238 progress.add(len(places))
242 for t in self.threads:
247 def find_free_thread(self):
248 """ Generator that returns the next connection that is free for
260 # refresh the connections occasionaly to avoid potential
261 # memory leaks in Postgresql.
262 if command_stat > 100000:
263 for t in self.threads:
264 while not t.is_done():
270 ready, _, _ = select.select(self.threads, [], [])
272 assert False, "Unreachable code"
275 def nominatim_arg_parser():
276 """ Setup the command-line parser for the tool.
279 return re.sub("\s\s+" , " ", s)
281 p = ArgumentParser(description="Indexing tool for Nominatim.",
282 formatter_class=RawDescriptionHelpFormatter)
284 p.add_argument('-d', '--database',
285 dest='dbname', action='store', default='nominatim',
286 help='Name of the PostgreSQL database to connect to.')
287 p.add_argument('-U', '--username',
288 dest='user', action='store',
289 help='PostgreSQL user name.')
290 p.add_argument('-W', '--password',
291 dest='password_prompt', action='store_true',
292 help='Force password prompt.')
293 p.add_argument('-H', '--host',
294 dest='host', action='store',
295 help='PostgreSQL server hostname or socket location.')
296 p.add_argument('-P', '--port',
297 dest='port', action='store',
298 help='PostgreSQL server port')
299 p.add_argument('-r', '--minrank',
300 dest='minrank', type=int, metavar='RANK', default=0,
301 help='Minimum/starting rank.')
302 p.add_argument('-R', '--maxrank',
303 dest='maxrank', type=int, metavar='RANK', default=30,
304 help='Maximum/finishing rank.')
305 p.add_argument('-t', '--threads',
306 dest='threads', type=int, metavar='NUM', default=1,
307 help='Number of threads to create for indexing.')
308 p.add_argument('-v', '--verbose',
309 dest='loglevel', action='count', default=0,
310 help='Increase verbosity')
314 if __name__ == '__main__':
315 logging.basicConfig(stream=sys.stderr, format='%(levelname)s: %(message)s')
317 options = nominatim_arg_parser().parse_args(sys.argv[1:])
319 log.setLevel(max(3 - options.loglevel, 0) * 10)
321 options.password = None
322 if options.password_prompt:
323 password = getpass.getpass("Database password: ")
324 options.password = password
326 Indexer(options).run()