2 Collection of functions that check if the database is complete and functional.
5 from textwrap import dedent
7 from nominatim.db.connection import connect
8 from nominatim.errors import UsageError
9 from nominatim.tokenizer import factory as tokenizer_factory
13 class CheckState(Enum):
14 """ Possible states of a check. FATAL stops check execution entirely.
21 def _check(hint=None):
22 """ Decorator for checks. It adds the function to the list of
23 checks to execute and adds the code for printing progress messages.
26 title = func.__doc__.split('\n', 1)[0].strip()
27 def run_check(conn, config):
28 print(title, end=' ... ')
29 ret = func(conn, config)
30 if isinstance(ret, tuple):
34 if ret == CheckState.OK:
35 print('\033[92mOK\033[0m')
36 elif ret == CheckState.NOT_APPLICABLE:
37 print('not applicable')
39 print('\x1B[31mFailed\033[0m')
41 print(dedent(hint.format(**params)))
44 CHECKLIST.append(run_check)
51 def __init__(self, msg):
55 """ Dummy function to provide the implementation.
58 def check_database(config):
59 """ Run a number of checks on the database and return the status.
62 conn = connect(config.get_libpq_dsn()).connection
63 except UsageError as err:
64 conn = _BadConnection(str(err))
67 for check in CHECKLIST:
68 ret = check(conn, config)
69 if ret == CheckState.FATAL:
72 if ret in (CheckState.FATAL, CheckState.FAIL):
79 def _get_indexes(conn):
80 indexes = ['idx_place_addressline_address_place_id',
81 'idx_placex_rank_search',
82 'idx_placex_rank_address',
83 'idx_placex_parent_place_id',
84 'idx_placex_geometry_reverse_lookuppolygon',
85 'idx_placex_geometry_placenode',
86 'idx_osmline_parent_place_id',
87 'idx_osmline_parent_osm_id',
89 'idx_postcode_postcode'
91 if conn.table_exists('search_name'):
92 indexes.extend(('idx_search_name_nameaddress_vector',
93 'idx_search_name_name_vector',
94 'idx_search_name_centroid'))
95 if conn.server_version_tuple() >= (11, 0, 0):
96 indexes.extend(('idx_placex_housenumber',
97 'idx_osmline_parent_osm_id_with_hnr'))
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, _):
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
159 @_check(hint="""{msg}""")
160 def check_tokenizer(_, config):
161 """ Checking that tokenizer works
164 tokenizer = tokenizer_factory.get_tokenizer_for_db(config)
166 return CheckState.FAIL, dict(msg="""\
167 Cannot load tokenizer. Did the import finish sucessfully?""")
169 result = tokenizer.check_database()
174 return CheckState.FAIL, dict(msg=result)
178 The indexing didn't finish. {count} entries are not yet indexed.
180 To index the remaining entries, run: {index_cmd}
182 def check_indexing(conn, _):
183 """ Checking indexing status
185 with conn.cursor() as cur:
186 cnt = cur.scalar('SELECT count(*) FROM placex WHERE indexed_status > 0')
191 if conn.index_exists('idx_placex_rank_search'):
192 # Likely just an interrupted update.
193 index_cmd = 'nominatim index'
195 # Looks like the import process got interrupted.
196 index_cmd = 'nominatim import --continue indexing'
198 return CheckState.FAIL, dict(count=cnt, index_cmd=index_cmd)
202 The following indexes are missing:
205 Rerun the index creation with: nominatim import --continue db-postprocess
207 def check_database_indexes(conn, _):
208 """ Checking that database indexes are complete
211 for index in _get_indexes(conn):
212 if not conn.index_exists(index):
213 missing.append(index)
216 return CheckState.FAIL, dict(indexes='\n '.join(missing))
222 At least one index is invalid. That can happen, e.g. when index creation was
223 disrupted and later restarted. You should delete the affected indices
229 def check_database_index_valid(conn, _):
230 """ Checking that all database indexes are valid
232 with conn.cursor() as cur:
233 cur.execute(""" SELECT relname FROM pg_class, pg_index
234 WHERE pg_index.indisvalid = false
235 AND pg_index.indexrelid = pg_class.oid""")
240 return CheckState.FAIL, dict(indexes='\n '.join(broken))
247 Run TIGER import again: nominatim add-data --tiger-data <DIR>
249 def check_tiger_table(conn, config):
250 """ Checking TIGER external data table.
252 if not config.get_bool('USE_US_TIGER_DATA'):
253 return CheckState.NOT_APPLICABLE
255 if not conn.table_exists('location_property_tiger'):
256 return CheckState.FAIL, dict(error='TIGER data table not found.')
258 with conn.cursor() as cur:
259 if cur.scalar('SELECT count(*) FROM location_property_tiger') == 0:
260 return CheckState.FAIL, dict(error='TIGER data table is empty.')