X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/bc7f6209d83dcc161f093ce9907991facabb3491..7d911f9ffbdf63b2b2a45c3a3ee7063d006a5779:/nominatim/db/sql_preprocessor.py?ds=sidebyside diff --git a/nominatim/db/sql_preprocessor.py b/nominatim/db/sql_preprocessor.py index b450422d..2e11f571 100644 --- a/nominatim/db/sql_preprocessor.py +++ b/nominatim/db/sql_preprocessor.py @@ -11,6 +11,7 @@ from typing import Set, Dict, Any import jinja2 from nominatim.db.connection import Connection +from nominatim.db.async_connection import WorkerPool from nominatim.config import Configuration def _get_partitions(conn: Connection) -> Set[int]: @@ -56,9 +57,11 @@ def _setup_postgresql_features(conn: Connection) -> Dict[str, Any]: """ pg_version = conn.server_version_tuple() postgis_version = conn.postgis_version_tuple() + pg11plus = pg_version >= (11, 0, 0) + ps3 = postgis_version >= (3, 0) return { - 'has_index_non_key_column': pg_version >= (11, 0, 0), - 'spgist_geom' : 'SPGIST' if postgis_version >= (3, 0) else 'GIST' + 'has_index_non_key_column': pg11plus, + 'spgist_geom' : 'SPGIST' if pg11plus and ps3 else 'GIST' } class SQLPreprocessor: @@ -96,3 +99,21 @@ class SQLPreprocessor: with conn.cursor() as cur: cur.execute(sql) conn.commit() + + + def run_parallel_sql_file(self, dsn: str, name: str, num_threads: int = 1, + **kwargs: Any) -> None: + """ Execure the given SQL files using parallel asynchronous connections. + The keyword arguments may supply additional parameters for + preprocessing. + + After preprocessing the SQL code is cut at lines containing only + '---'. Each chunk is sent to one of the `num_threads` workers. + """ + sql = self.env.get_template(name).render(**kwargs) + + parts = sql.split('\n---\n') + + with WorkerPool(dsn, num_threads) as pool: + for part in parts: + pool.next_free_worker().perform(part)