- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2k
Add PgBouncer support via NullPool configuration #18643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This adds support for using external connection poolers like PgBouncer by allowing pool_size to be set to None/null, which configures SQLAlchemy to use NullPool. Key changes: - Allow pool_size setting to accept None/null values - Use NullPool when pool_size is None, skipping pool-related parameters - Add documentation on PgBouncer setup and configuration - Add tests for NullPool configuration Usage: export PREFECT_SERVER_DATABASE_SQLALCHEMY_POOL_SIZE=null export PREFECT_SERVER_DATABASE_SQLALCHEMY_CONNECT_ARGS_STATEMENT_CACHE_SIZE=0 export PREFECT_SERVER_DATABASE_CONNECTION_URL='postgresql+asyncpg://user:pass@pgbouncer:6432/db' Addresses #18616
| CodSpeed Performance ReportMerging #18643 will not alter performanceComparing  Summary
 | 
- Add comprehensive PgBouncer setup guide - Include Docker compose example with correct environment variables - Document how to configure Prefect with NullPool for PgBouncer - Add troubleshooting section and verified working configuration - Fix environment variable names (DB_HOST instead of DATABASES_HOST) - Add note about setting pool_size to None programmatically 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
| self.sqlalchemy_pool_size: Optional[int] = ( | ||
| sqlalchemy_pool_size | ||
| or get_current_settings().server.database.sqlalchemy.pool_size | ||
| if sqlalchemy_pool_size is not None | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we ever use this as init kwargs within the codebase? I think it would be weird if passing None as an init kwarg is the one value that isn't respected, but I don't think we actually use these init kwargs anyway
| This pull request is stale because it has been open 14 days with no activity. To keep this pull request open remove stale label or comment. | 
| Comment to keep it open. | 
| This pull request is stale because it has been open 14 days with no activity. To keep this pull request open remove stale label or comment. | 
| Comment to reopen. @zzstoatzz are you still planning on finishing this? The instance I'm running still shows some slowness due to connecting to the Postgres db. This might fix it. | 
| This pull request is stale because it has been open 14 days with no activity. To keep this pull request open remove stale label or comment. | 
| Sesam open u! | 
| This pull request is stale because it has been open 14 days with no activity. To keep this pull request open remove stale label or comment. | 
| comment | 
| This pull request is stale because it has been open 14 days with no activity. To keep this pull request open remove stale label or comment. | 
Closes #18616
Summary
This PR implements PgBouncer support for Prefect by allowing
pool_sizeto be set toNone, which configures SQLAlchemy to useNullPool. This addresses the double pooling issue when using external connection poolers like PgBouncer.Changes
1. Database Configuration
pool_sizesetting insrc/prefect/settings/models/server/database.pyto acceptOptional[int]2. Engine Creation
AsyncPostgresConfigurationinsrc/prefect/server/database/configurations.py:sqlalchemy_pool_sizeis None, use SQLAlchemy'sNullPool3. Tests
TestNullPoolConfigurationclass totests/server/database/test_dependencies.py4. Documentation
docs/v3/advanced/database-maintenance.mdxTesting
I've tested this implementation by:
Example Usage
🤖 Generated with Claude Code