2 Collection of functions that check if the database is complete and functional.
5 from textwrap import dedent
9 from nominatim.db.connection import connect
10 from nominatim.errors import UsageError
14 class CheckState(Enum):
15 """ Possible states of a check. FATAL stops check execution entirely.
22 def _check(hint=None):
23 """ Decorator for checks. It adds the function to the list of
24 checks to execute and adds the code for printing progress messages.
27 title = func.__doc__.split('\n', 1)[0].strip()
28 def run_check(conn, config):
29 print(title, end=' ... ')
30 ret = func(conn, config)
31 if isinstance(ret, tuple):
35 if ret == CheckState.OK:
36 print('\033[92mOK\033[0m')
37 elif ret == CheckState.NOT_APPLICABLE:
38 print('not applicable')
40 print('\x1B[31mFailed\033[0m')
42 print(dedent(hint.format(**params)))
45 CHECKLIST.append(run_check)
50 class _BadConnection: # pylint: disable=R0903
52 def __init__(self, msg):
56 """ Dummy function to provide the implementation.
59 def check_database(config):
60 """ Run a number of checks on the database and return the status.
63 conn = connect(config.get_libpq_dsn()).connection
64 except UsageError as err:
65 conn = _BadConnection(str(err))
68 for check in CHECKLIST:
69 ret = check(conn, config)
70 if ret == CheckState.FATAL:
73 if ret in (CheckState.FATAL, CheckState.FAIL):
80 def _get_indexes(conn):
81 indexes = ['idx_word_word_id',
82 'idx_place_addressline_address_place_id',
83 'idx_placex_rank_search',
84 'idx_placex_rank_address',
85 'idx_placex_parent_place_id',
86 'idx_placex_geometry_reverse_lookuppolygon',
87 'idx_placex_geometry_placenode',
88 'idx_placex_housenumber',
89 'idx_osmline_parent_place_id',
90 'idx_osmline_parent_osm_id',
92 'idx_postcode_postcode'
94 if conn.table_exists('search_name'):
95 indexes.extend(('idx_search_name_nameaddress_vector',
96 'idx_search_name_name_vector',
97 'idx_search_name_centroid'))
98 if conn.table_exists('place'):
99 indexes.extend(('idx_placex_pendingsector',
100 'idx_location_area_country_place_id',
101 'idx_place_osm_unique'
109 # Functions are exectured in the order they appear here.
115 * Is the database server started?
116 * Check the NOMINATIM_DATABASE_DSN variable in your local .env
117 * Try connecting to the database with the same settings
119 Project directory: {config.project_dir}
120 Current setting of NOMINATIM_DATABASE_DSN: {config.DATABASE_DSN}
122 def check_connection(conn, config):
123 """ Checking database connection
125 if isinstance(conn, _BadConnection):
126 return CheckState.FATAL, dict(error=conn.msg, config=config)
131 placex table not found
134 * Are you connecting to the right database?
135 * Did the import process finish without errors?
137 Project directory: {config.project_dir}
138 Current setting of NOMINATIM_DATABASE_DSN: {config.DATABASE_DSN}
140 def check_placex_table(conn, config):
141 """ Checking for placex table
143 if conn.table_exists('placex'):
146 return CheckState.FATAL, dict(config=config)
149 @_check(hint="""placex table has no data. Did the import finish sucessfully?""")
150 def check_placex_size(conn, config): # pylint: disable=W0613
151 """ Checking for placex content
153 with conn.cursor() as cur:
154 cnt = cur.scalar('SELECT count(*) FROM (SELECT * FROM placex LIMIT 100) x')
156 return CheckState.OK if cnt > 0 else CheckState.FATAL
160 The Postgresql extension nominatim.so was not correctly loaded.
165 * Check the output of the CMmake/make installation step
166 * Does nominatim.so exist?
167 * Does nominatim.so exist on the database server?
168 * Can nominatim.so be accessed by the database user?
170 def check_module(conn, config): # pylint: disable=W0613
171 """ Checking that nominatim.so module is installed
173 with conn.cursor() as cur:
175 out = cur.scalar("SELECT make_standard_name('a')")
176 except psycopg2.ProgrammingError as err:
177 return CheckState.FAIL, dict(error=str(err))
180 return CheckState.FAIL, dict(error='Unexpected result for make_standard_name()')
186 The indexing didn't finish. {count} entries are not yet indexed.
188 To index the remaining entries, run: {index_cmd}
190 def check_indexing(conn, config): # pylint: disable=W0613
191 """ Checking indexing status
193 with conn.cursor() as cur:
194 cnt = cur.scalar('SELECT count(*) FROM placex WHERE indexed_status > 0')
199 if conn.index_exists('idx_word_word_id'):
200 # Likely just an interrupted update.
201 index_cmd = 'nominatim index'
203 # Looks like the import process got interrupted.
204 index_cmd = 'nominatim import --continue indexing'
206 return CheckState.FAIL, dict(count=cnt, index_cmd=index_cmd)
210 The following indexes are missing:
213 Rerun the index creation with: nominatim import --continue db-postprocess
215 def check_database_indexes(conn, config): # pylint: disable=W0613
216 """ Checking that database indexes are complete
219 for index in _get_indexes(conn):
220 if not conn.index_exists(index):
221 missing.append(index)
224 return CheckState.FAIL, dict(indexes='\n '.join(missing))
230 At least one index is invalid. That can happen, e.g. when index creation was
231 disrupted and later restarted. You should delete the affected indices
237 def check_database_index_valid(conn, config): # pylint: disable=W0613
238 """ Checking that all database indexes are valid
240 with conn.cursor() as cur:
241 cur.execute(""" SELECT relname FROM pg_class, pg_index
242 WHERE pg_index.indisvalid = false
243 AND pg_index.indexrelid = pg_class.oid""")
248 return CheckState.FAIL, dict(indexes='\n '.join(broken))
255 Run TIGER import again: nominatim add-data --tiger-data <DIR>
257 def check_tiger_table(conn, config):
258 """ Checking TIGER external data table.
260 if not config.get_bool('USE_US_TIGER_DATA'):
261 return CheckState.NOT_APPLICABLE
263 if not conn.table_exists('location_property_tiger'):
264 return CheckState.FAIL, dict(error='TIGER data table not found.')
266 with conn.cursor() as cur:
267 if cur.scalar('SELECT count(*) FROM location_property_tiger') == 0:
268 return CheckState.FAIL, dict(error='TIGER data table is empty.')