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):
71 return "UPDATE placex SET indexed_status = 0 WHERE place_id = %s"
74 class InterpolationRunner(object):
75 """ Returns SQL commands for indexing the address interpolation table
76 location_property_osmline.
80 return "interpolation lines (location_property_osmline)"
82 def sql_count_objects(self):
83 return """SELECT count(*) FROM location_property_osmline
84 WHERE indexed_status > 0"""
86 def sql_get_objects(self):
87 return """SELECT place_id FROM location_property_osmline
88 WHERE indexed_status > 0
89 ORDER BY geometry_sector"""
91 def sql_index_place(self):
92 return """UPDATE location_property_osmline
93 SET indexed_status = 0 WHERE place_id = %s"""
96 class DBConnection(object):
97 """ A single non-blocking database connection.
100 def __init__(self, options):
101 self.current_query = None
102 self.current_params = None
108 if self.conn is not None:
112 self.conn = make_connection(options, asynchronous=True)
115 self.cursor = self.conn.cursor()
116 # Disable JIT and parallel workers as they are known to cause problems.
117 # Update pg_settings instead of using SET because it does not yield
118 # errors on older versions of Postgres where the settings are not
121 """ UPDATE pg_settings SET setting = -1 WHERE name = 'jit_above_cost';
122 UPDATE pg_settings SET setting = 0
123 WHERE name = 'max_parallel_workers_per_gather';""")
127 """ Block until any pending operation is done.
131 wait_select(self.conn)
132 self.current_query = None
134 except psycopg2.extensions.TransactionRollbackError as e:
135 if e.pgcode == '40P01':
136 log.info("Deadlock detected (params = {}), retry."
137 .format(self.current_params))
138 self.cursor.execute(self.current_query, self.current_params)
141 except psycopg2.errors.DeadlockDetected:
142 self.cursor.execute(self.current_query, self.current_params)
144 def perform(self, sql, args=None):
145 """ Send SQL query to the server. Returns immediately without
148 self.current_query = sql
149 self.current_params = args
150 self.cursor.execute(sql, args)
153 """ File descriptor to wait for. (Makes this class select()able.)
155 return self.conn.fileno()
158 """ Check if the connection is available for a new query.
160 Also checks if the previous query has run into a deadlock.
161 If so, then the previous query is repeated.
163 if self.current_query is None:
167 if self.conn.poll() == psycopg2.extensions.POLL_OK:
168 self.current_query = None
170 except psycopg2.extensions.TransactionRollbackError as e:
171 if e.pgcode == '40P01':
172 log.info("Deadlock detected (params = {}), retry.".format(self.current_params))
173 self.cursor.execute(self.current_query, self.current_params)
176 except psycopg2.errors.DeadlockDetected:
177 self.cursor.execute(self.current_query, self.current_params)
182 class Indexer(object):
183 """ Main indexing routine.
186 def __init__(self, options):
187 self.minrank = max(0, options.minrank)
188 self.maxrank = min(30, options.maxrank)
189 self.conn = make_connection(options)
190 self.threads = [DBConnection(options) for i in range(options.threads)]
193 """ Run indexing over the entire database.
195 log.warning("Starting indexing rank ({} to {}) using {} threads".format(
196 self.minrank, self.maxrank, len(self.threads)))
198 for rank in range(self.minrank, self.maxrank):
199 self.index(RankRunner(rank))
201 if self.maxrank == 30:
202 self.index(InterpolationRunner())
204 self.index(RankRunner(self.maxrank))
206 def index(self, obj):
207 """ Index a single rank or table. `obj` describes the SQL to use
210 log.warning("Starting {}".format(obj.name()))
212 cur = self.conn.cursor()
213 cur.execute(obj.sql_count_objects())
215 total_tuples = cur.fetchone()[0]
216 log.debug("Total number of rows: {}".format(total_tuples))
220 next_thread = self.find_free_thread()
221 progress = ProgressLogger(obj.name(), total_tuples)
223 cur = self.conn.cursor(name='places')
224 cur.execute(obj.sql_get_objects())
228 log.debug("Processing place {}".format(place_id))
229 thread = next(next_thread)
231 thread.perform(obj.sql_index_place(), (place_id,))
236 for t in self.threads:
241 def find_free_thread(self):
242 """ Generator that returns the next connection that is free for
254 # refresh the connections occasionaly to avoid potential
255 # memory leaks in Postgresql.
256 if command_stat > 100000:
257 for t in self.threads:
258 while not t.is_done():
264 ready, _, _ = select.select(self.threads, [], [])
266 assert False, "Unreachable code"
269 def nominatim_arg_parser():
270 """ Setup the command-line parser for the tool.
273 return re.sub("\s\s+" , " ", s)
275 p = ArgumentParser(description="Indexing tool for Nominatim.",
276 formatter_class=RawDescriptionHelpFormatter)
278 p.add_argument('-d', '--database',
279 dest='dbname', action='store', default='nominatim',
280 help='Name of the PostgreSQL database to connect to.')
281 p.add_argument('-U', '--username',
282 dest='user', action='store',
283 help='PostgreSQL user name.')
284 p.add_argument('-W', '--password',
285 dest='password_prompt', action='store_true',
286 help='Force password prompt.')
287 p.add_argument('-H', '--host',
288 dest='host', action='store',
289 help='PostgreSQL server hostname or socket location.')
290 p.add_argument('-P', '--port',
291 dest='port', action='store',
292 help='PostgreSQL server port')
293 p.add_argument('-r', '--minrank',
294 dest='minrank', type=int, metavar='RANK', default=0,
295 help='Minimum/starting rank.')
296 p.add_argument('-R', '--maxrank',
297 dest='maxrank', type=int, metavar='RANK', default=30,
298 help='Maximum/finishing rank.')
299 p.add_argument('-t', '--threads',
300 dest='threads', type=int, metavar='NUM', default=1,
301 help='Number of threads to create for indexing.')
302 p.add_argument('-v', '--verbose',
303 dest='loglevel', action='count', default=0,
304 help='Increase verbosity')
308 if __name__ == '__main__':
309 logging.basicConfig(stream=sys.stderr, format='%(levelname)s: %(message)s')
311 options = nominatim_arg_parser().parse_args(sys.argv[1:])
313 log.setLevel(max(3 - options.loglevel, 0) * 10)
315 options.password = None
316 if options.password_prompt:
317 password = getpass.getpass("Database password: ")
318 options.password = password
320 Indexer(options).run()