]> git.openstreetmap.org Git - nominatim.git/commitdiff
Merge pull request #3063 from lonvia/variable-parameters
authorSarah Hoffmann <lonvia@denofr.de>
Thu, 18 May 2023 20:27:18 +0000 (22:27 +0200)
committerGitHub <noreply@github.com>
Thu, 18 May 2023 20:27:18 +0000 (22:27 +0200)
Rework how search parameters are handed to the Python API

.github/actions/build-nominatim/action.yml
nominatim/api/core.py
nominatim/db/async_core_library.py [new file with mode: 0644]

index 6508f03ec57d4b7b52fa5aaee2f23d9d9b82e490..5d06267c0453808ec1a66ae3ac66c5af27150276 100644 (file)
@@ -30,7 +30,7 @@ runs:
                 pip3 install MarkupSafe==2.0.1 python-dotenv psycopg2==2.7.7 jinja2==2.8 psutil==5.4.2 pyicu==2.9 osmium PyYAML==5.1 sqlalchemy==1.4 GeoAlchemy2==0.10.0 datrie asyncpg
             else
                 sudo apt-get install -y -qq python3-icu python3-datrie python3-pyosmium python3-jinja2 python3-psutil python3-psycopg2 python3-dotenv python3-yaml
-                pip3 install sqlalchemy GeoAlchemy2 asyncpg
+                pip3 install sqlalchemy GeoAlchemy2 psycopg
             fi
           shell: bash
           env:
index b69946c5c048212e7b546ba61832d5a890a0a86e..f1a656da483eda6fecf3b5f2038b3c1806b38142 100644 (file)
@@ -14,9 +14,10 @@ from pathlib import Path
 
 import sqlalchemy as sa
 import sqlalchemy.ext.asyncio as sa_asyncio
-import asyncpg
+
 
 from nominatim.db.sqlalchemy_schema import SearchTables
+from nominatim.db.async_core_library import PGCORE_LIB, PGCORE_ERROR
 from nominatim.config import Configuration
 from nominatim.api.connection import SearchConnection
 from nominatim.api.status import get_status, StatusResult
@@ -55,26 +56,22 @@ class NominatimAPIAsync:
 
             query = {k: v for k, v in dsn.items()
                       if k not in ('user', 'password', 'dbname', 'host', 'port')}
-            query['prepared_statement_cache_size'] = '0'
+            if PGCORE_LIB == 'asyncpg':
+                query['prepared_statement_cache_size'] = '0'
 
             dburl = sa.engine.URL.create(
-                       'postgresql+asyncpg',
+                       f'postgresql+{PGCORE_LIB}',
                        database=dsn.get('dbname'),
                        username=dsn.get('user'), password=dsn.get('password'),
                        host=dsn.get('host'), port=int(dsn['port']) if 'port' in dsn else None,
                        query=query)
-            engine = sa_asyncio.create_async_engine(
-                             dburl, future=True,
-                             connect_args={'server_settings': {
-                                'DateStyle': 'sql,european',
-                                'max_parallel_workers_per_gather': '0'
-                             }})
+            engine = sa_asyncio.create_async_engine(dburl, future=True)
 
             try:
                 async with engine.begin() as conn:
                     result = await conn.scalar(sa.text('SHOW server_version_num'))
                     server_version = int(result)
-            except asyncpg.PostgresError:
+            except (PGCORE_ERROR, sa.exc.OperationalError):
                 server_version = 0
 
             if server_version >= 110000:
@@ -82,6 +79,7 @@ class NominatimAPIAsync:
                 def _on_connect(dbapi_con: Any, _: Any) -> None:
                     cursor = dbapi_con.cursor()
                     cursor.execute("SET jit_above_cost TO '-1'")
+                    cursor.execute("SET max_parallel_workers_per_gather TO '0'")
                 # Make sure that all connections get the new settings
                 await self.close()
 
@@ -124,7 +122,7 @@ class NominatimAPIAsync:
         try:
             async with self.begin() as conn:
                 status = await get_status(conn)
-        except asyncpg.PostgresError:
+        except (PGCORE_ERROR, sa.exc.OperationalError):
             return StatusResult(700, 'Database connection failed')
 
         return status
diff --git a/nominatim/db/async_core_library.py b/nominatim/db/async_core_library.py
new file mode 100644 (file)
index 0000000..d86ce5e
--- /dev/null
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+# This file is part of Nominatim. (https://nominatim.org)
+#
+# Copyright (C) 2023 by the Nominatim developer community.
+# For a full list of authors see the git log.
+"""
+Import the base libary to use with asynchronous SQLAlchemy.
+"""
+# pylint: disable=invalid-name
+
+from typing import Any
+
+try:
+    import psycopg
+    PGCORE_LIB = 'psycopg'
+    PGCORE_ERROR: Any = psycopg.Error
+except ModuleNotFoundError:
+    import asyncpg
+    PGCORE_LIB = 'asyncpg'
+    PGCORE_ERROR = asyncpg.PostgresError