diff --git a/docs/v3/develop/settings-ref.mdx b/docs/v3/develop/settings-ref.mdx index 9c9816e9b8d3..839f92d27dc5 100644 --- a/docs/v3/develop/settings-ref.mdx +++ b/docs/v3/develop/settings-ref.mdx @@ -1183,7 +1183,7 @@ A connection timeout, in seconds, applied to database connections. Defaults to ` `PREFECT_SERVER_DATABASE_CONNECTION_TIMEOUT`, `PREFECT_API_DATABASE_CONNECTION_TIMEOUT` ### `sqlalchemy_pool_size` -Controls connection pool size when using a PostgreSQL database with the Prefect API. If not set, the default SQLAlchemy pool size will be used. +Controls connection pool size of database connection pools from the Prefect API. If not set, the default SQLAlchemy pool size will be used. **Type**: `integer | None` @@ -1194,6 +1194,18 @@ Controls connection pool size when using a PostgreSQL database with the Prefect **Supported environment variables**: `PREFECT_SERVER_DATABASE_SQLALCHEMY_POOL_SIZE`, `PREFECT_SQLALCHEMY_POOL_SIZE` +### `connection_app_name` +Controls the application_name field for connections opened from the connection pool when using a PostgreSQL database with the Prefect API. + +**Type**: `string | None` + +**Default**: `None` + +**TOML dotted key path**: `server.database.connection_app_name` + +**Supported environment variables**: +`PREFECT_SERVER_DATABASE_CONNECTION_APP_NAME` + ### `sqlalchemy_max_overflow` Controls maximum overflow of the connection pool when using a PostgreSQL database with the Prefect API. If not set, the default SQLAlchemy maximum overflow value will be used. diff --git a/schemas/settings.schema.json b/schemas/settings.schema.json index 6d4903602356..ecfcc749e43c 100644 --- a/schemas/settings.schema.json +++ b/schemas/settings.schema.json @@ -1046,13 +1046,29 @@ } ], "default": null, - "description": "Controls connection pool size when using a PostgreSQL database with the Prefect API. If not set, the default SQLAlchemy pool size will be used.", + "description": "Controls connection pool size of database connection pools from the Prefect API. If not set, the default SQLAlchemy pool size will be used.", "supported_environment_variables": [ "PREFECT_SERVER_DATABASE_SQLALCHEMY_POOL_SIZE", "PREFECT_SQLALCHEMY_POOL_SIZE" ], "title": "Sqlalchemy Pool Size" }, + "connection_app_name": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Controls the application_name field for connections opened from the connection pool when using a PostgreSQL database with the Prefect API.", + "supported_environment_variables": [ + "PREFECT_SERVER_DATABASE_CONNECTION_APP_NAME" + ], + "title": "Connection App Name" + }, "sqlalchemy_max_overflow": { "anyOf": [ { diff --git a/src/prefect/server/database/configurations.py b/src/prefect/server/database/configurations.py index 31c93b5eddf8..c09caec7fc28 100644 --- a/src/prefect/server/database/configurations.py +++ b/src/prefect/server/database/configurations.py @@ -26,6 +26,7 @@ PREFECT_API_DATABASE_CONNECTION_TIMEOUT, PREFECT_API_DATABASE_ECHO, PREFECT_API_DATABASE_TIMEOUT, + PREFECT_SERVER_DATABASE_CONNECTION_APP_NAME, PREFECT_SQLALCHEMY_MAX_OVERFLOW, PREFECT_SQLALCHEMY_POOL_SIZE, PREFECT_TESTING_UNIT_TEST_MODE, @@ -121,6 +122,7 @@ def __init__( connection_timeout: Optional[float] = None, sqlalchemy_pool_size: Optional[int] = None, sqlalchemy_max_overflow: Optional[int] = None, + connection_app_name: Optional[str] = None, ) -> None: self.connection_url = connection_url self.echo: bool = echo or PREFECT_API_DATABASE_ECHO.value() @@ -134,6 +136,9 @@ def __init__( self.sqlalchemy_max_overflow: Optional[int] = ( sqlalchemy_max_overflow or PREFECT_SQLALCHEMY_MAX_OVERFLOW.value() ) + self.connection_app_name: Optional[str] = ( + connection_app_name or PREFECT_SERVER_DATABASE_CONNECTION_APP_NAME.value() + ) def unique_key(self) -> tuple[Hashable, ...]: """ @@ -210,6 +215,11 @@ async def engine(self) -> AsyncEngine: if self.connection_timeout is not None: connect_args["timeout"] = self.connection_timeout + if self.connection_app_name is not None: + connect_args["server_settings"] = dict( + application_name=self.connection_app_name + ) + if connect_args: kwargs["connect_args"] = connect_args @@ -337,6 +347,12 @@ async def engine(self) -> AsyncEngine: if self.timeout is not None: kwargs["connect_args"] = dict(timeout=self.timeout) + if self.sqlalchemy_pool_size is not None: + kwargs["pool_size"] = self.sqlalchemy_pool_size + + if self.sqlalchemy_max_overflow is not None: + kwargs["max_overflow"] = self.sqlalchemy_max_overflow + # use `named` paramstyle for sqlite instead of `qmark` in very rare # circumstances, we've seen aiosqlite pass parameters in the wrong # order; by using named parameters we avoid this issue diff --git a/src/prefect/settings/models/server/database.py b/src/prefect/settings/models/server/database.py index f4820d218cfc..4ad352bbcad6 100644 --- a/src/prefect/settings/models/server/database.py +++ b/src/prefect/settings/models/server/database.py @@ -148,7 +148,7 @@ class ServerDatabaseSettings(PrefectBaseSettings): sqlalchemy_pool_size: Optional[int] = Field( default=None, - description="Controls connection pool size when using a PostgreSQL database with the Prefect API. If not set, the default SQLAlchemy pool size will be used.", + description="Controls connection pool size of database connection pools from the Prefect API. If not set, the default SQLAlchemy pool size will be used.", validation_alias=AliasChoices( AliasPath("sqlalchemy_pool_size"), "prefect_server_database_sqlalchemy_pool_size", @@ -156,6 +156,11 @@ class ServerDatabaseSettings(PrefectBaseSettings): ), ) + connection_app_name: Optional[str] = Field( + default=None, + description="Controls the application_name field for connections opened from the connection pool when using a PostgreSQL database with the Prefect API.", + ) + sqlalchemy_max_overflow: Optional[int] = Field( default=None, description="Controls maximum overflow of the connection pool when using a PostgreSQL database with the Prefect API. If not set, the default SQLAlchemy maximum overflow value will be used.", diff --git a/tests/test_settings.py b/tests/test_settings.py index 96f71927546a..ba925186bc2d 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -297,6 +297,7 @@ "test_value": timedelta(seconds=10), "legacy": True, }, + "PREFECT_SERVER_DATABASE_CONNECTION_APP_NAME": {"test_value": "testingconn"}, "PREFECT_SERVER_DATABASE_CONNECTION_TIMEOUT": {"test_value": 10.0}, "PREFECT_SERVER_DATABASE_CONNECTION_URL": {"test_value": "sqlite:///"}, "PREFECT_SERVER_DATABASE_DRIVER": {"test_value": "sqlite+aiosqlite"},