From c59fc13d3cb8406dc5a700007f199c3800f5e591 Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Sat, 15 Mar 2025 22:17:01 -0700 Subject: [PATCH] Make `prepare()` not use named statements by default when cache is disabled We allow disabling the statement cache to circumvent a proxy's inability to cope with them, however there are still some holes where the use of a named statement is attempted: `prepare()` with the default arguments, and `copy_in()`. Fix both so that the use of a named statement is dependent upon whether the prepared statement cache is enabled. Fixes: #1219 Closes: #1218 --- asyncpg/connection.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/asyncpg/connection.py b/asyncpg/connection.py index ba8be2ef..9215eeba 100644 --- a/asyncpg/connection.py +++ b/asyncpg/connection.py @@ -394,7 +394,7 @@ async def _get_statement( query, timeout, *, - named=False, + named: typing.Union[str, bool, None] = False, use_cache=True, ignore_custom_codec=False, record_class=None @@ -628,7 +628,6 @@ async def prepare( query, name=name, timeout=timeout, - use_cache=False, record_class=record_class, ) @@ -636,16 +635,18 @@ async def _prepare( self, query, *, - name=None, + name: typing.Union[str, bool, None] = None, timeout=None, use_cache: bool=False, record_class=None ): self._check_open() + if name is None: + name = self._stmt_cache_enabled stmt = await self._get_statement( query, timeout, - named=True if name is None else name, + named=name, use_cache=use_cache, record_class=record_class, ) @@ -1099,7 +1100,7 @@ async def copy_records_to_table(self, table_name, *, records, intro_query = 'SELECT {cols} FROM {tab} LIMIT 1'.format( tab=tabname, cols=col_list) - intro_ps = await self._prepare(intro_query, use_cache=True) + intro_ps = await self.prepare(intro_query) cond = self._format_copy_where(where) opts = '(FORMAT binary)'