From 6a8f9f7c29bcdb9f981f18eb399525fdd96103db Mon Sep 17 00:00:00 2001 From: Kamil Monicz Date: Fri, 5 Apr 2024 13:39:46 +0000 Subject: [PATCH] Adjust SQLAlchemy pool size, Fix postgis warnings --- config/postgres.conf | 16 +++++++++++++--- db.py | 2 ++ models/geometry.py | 4 ++-- services/aed_service.py | 4 ++-- services/country_service.py | 4 ++-- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/config/postgres.conf b/config/postgres.conf index 5ea9123..b5cbba6 100644 --- a/config/postgres.conf +++ b/config/postgres.conf @@ -9,13 +9,21 @@ # reason: unused, improved compatibility unix_socket_directories = '' -# increase max connections -max_connections = 1000 - # adjust memory usage shared_buffers = 512MB effective_cache_size = 1GB +# increase max connections +max_connections = 1000 + +# detect disconnected clients +# reason: safeguard resource usage +client_connection_check_interval = 5s + +# disconnect idle clients with open transactions +# reason: safeguard resource usage +idle_in_transaction_session_timeout = 5min + # disable replication and reduce WAL usage # reason: unused, reduced resource usage wal_level = minimal @@ -46,6 +54,8 @@ random_page_cost = 1.1 # increase logging verbosity # reason: useful for development +log_connections = on +log_disconnections = on log_lock_waits = on log_temp_files = 0 # == log all temp files diff --git a/db.py b/db.py index 88cd470..f2cead2 100644 --- a/db.py +++ b/db.py @@ -12,6 +12,8 @@ echo_pool=POSTGRES_LOG, json_deserializer=JSON_DECODE, json_serializer=lambda x: JSON_ENCODE(x).decode(), + pool_size=8, + max_overflow=-1, query_cache_size=128, ) diff --git a/models/geometry.py b/models/geometry.py index 67083ec..8dbcf19 100644 --- a/models/geometry.py +++ b/models/geometry.py @@ -19,7 +19,7 @@ def process(value: Point | None): return None x, y = get_coordinates(value)[0] - return f'SRID=4326;POINT({x} {y})' # WKT + return f'POINT({x} {y})' # WKT return process @@ -50,7 +50,7 @@ def process(value: Polygon | MultiPolygon | None): if value is None: return None - return 'SRID=4326;' + value.wkt + return value.wkt return process diff --git a/services/aed_service.py b/services/aed_service.py index 1f26085..3a7d4d0 100644 --- a/services/aed_service.py +++ b/services/aed_service.py @@ -86,10 +86,10 @@ async def get_intersecting( cls, bbox_or_geom: BBox | BaseGeometry, group_eps: float | None ) -> Sequence[AED | AEDGroup]: geometry = bbox_or_geom.to_polygon() if isinstance(bbox_or_geom, BBox) else bbox_or_geom - geometry_wkt = 'SRID=4326;' + geometry.wkt + geometry_wkt = geometry.wkt async with db_read() as session: - stmt = select(AED).where(func.ST_Intersects(AED.position, func.ST_GeomFromEWKT(geometry_wkt))) + stmt = select(AED).where(func.ST_Intersects(AED.position, func.ST_GeomFromText(geometry_wkt, 4326))) aeds = (await session.scalars(stmt)).all() if len(aeds) <= 1 or group_eps is None: diff --git a/services/country_service.py b/services/country_service.py index 30e7fa3..1e23de4 100644 --- a/services/country_service.py +++ b/services/country_service.py @@ -46,10 +46,10 @@ async def get_all() -> Sequence[Country]: @trace async def get_intersecting(cls, bbox_or_geom: BBox | Point) -> Sequence[Country]: geometry = bbox_or_geom.to_polygon() if isinstance(bbox_or_geom, BBox) else bbox_or_geom - geometry_wkt = 'SRID=4326;' + geometry.wkt + geometry_wkt = geometry.wkt async with db_read() as session: - stmt = select(Country).where(func.ST_Intersects(Country.geometry, func.ST_GeomFromEWKT(geometry_wkt))) + stmt = select(Country).where(func.ST_Intersects(Country.geometry, func.ST_GeomFromText(geometry_wkt, 4326))) return (await session.scalars(stmt)).all()