Preserve a statement cache size of 0 - #22767
Merged
desertaxle merged 2 commits intoAug 10, 2026
Merged
Conversation
AsyncPostgresConfiguration resolves both cache-size arguments with `or`:
self.statement_cache_size = statement_cache_size or <setting>
0 is falsy, so it is replaced by the setting, which defaults to None. The
key is then never added to connect_args and asyncpg keeps its cache.
0 is not an absent value for either of these, and the settings that back
them say so. statement_cache_size: "Setting this to 0 is required when
using PgBouncer in transaction mode." prepared_statement_cache_size:
"When set to 0, statement caching is disabled." So `or` discards exactly
the value the documentation gives as the reason to set them.
Resolve with `is None` instead. Passing 0 through the settings already
worked, because the argument is None there and the setting is consulted;
only callers constructing the configuration directly were affected.
Non-zero values were already preserved and still are; the tests cover
both so the fix cannot be read as special-casing zero.
ckarnell
requested review from
chrisguidry,
desertaxle and
zzstoatzz
as code owners
August 10, 2026 04:02
Merging this PR will not alter performance
Comparing Footnotes
|
desertaxle
requested changes
Aug 10, 2026
desertaxle
left a comment
Member
There was a problem hiding this comment.
Thanks @ckarnell! Looks like there's one small formatting issue, but once that's fixed, this should be good to merge!
The pre-commit ruff-format hook (pinned v0.15.19) strips a trailing blank line at the end of the test file this branch adds. Taking the formatter's output; ruff check was already clean and the four tests still pass.
Contributor
Author
|
Fixed, thanks. It was a trailing blank line at the end of the test file, which ruff-format strips at the hook pin. Worth flagging since it caught me out: |
desertaxle
approved these changes
Aug 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AsyncPostgresConfiguration.__init__resolves both cache-size arguments withor:0is falsy, so it gets replaced by the setting, which defaults toNone. Theif ... is not Nonefurther down then skips the key entirely and asyncpg keeps its cache at the default.Your own settings say
0is the value that matters.statement_cache_sizeis documented as "Setting this to 0 is required when using PgBouncer in transaction mode", andprepared_statement_cache_sizeas disabling the cache "when set to 0". Theordiscards exactly the value both descriptions name as the reason to reach for the setting.Switched to
is not Noneat both sites. Two lines.The scope is narrow, and saying so up front seems better than letting it read bigger than it is: passing
0through settings already worked, because the constructor argument isNoneon that path, and prefect itself only ever builds this class from a connection URL. So the person affected is someone wiring upAsyncPostgresConfigurationthemselves to sit behind PgBouncer, which is who the docstring is talking to.Verified with no database and no network, just the constructor. On main,
statement_cache_size=0comes back asNonewhilestatement_cache_size=100comes back as100, and the same split happens for the prepared cache. The same call keeps the non-zero value and drops the zero.Reverting the two lines with the new tests in place fails them both, and restoring passes all four.
tests/server/databaseis 97 passed and 7 skipped with the change, against 93 before it, no regressions either way.No PgBouncer here. The claim that
0reaches asyncpg is read offconnect_args, not observed on a live connection.