Skip to content

Commit

Permalink
Disable JIT while doing type introspection
Browse files Browse the repository at this point in the history
The misapplication of JIT to asyncpg introspection queries has been a
constant source of user complaints.

Closes: #530
Closes: #1078
Previously: #875, #794, #782, #741, #727 (and probably more).
  • Loading branch information
elprans committed Sep 23, 2023
1 parent 7cb4e70 commit a6c560b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
44 changes: 41 additions & 3 deletions asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,47 @@ async def _get_statement(
return statement

async def _introspect_types(self, typeoids, timeout):
return await self.__execute(
if self._server_caps.jit:
try:
cfgrow, _ = await self.__execute(
"""
SELECT
current_setting('jit') AS cur,
set_config('jit', 'off', false) AS new
""",
(),
0,
timeout,
ignore_custom_codec=True,
)
jit_state = cfgrow[0]['cur']
except exceptions.UndefinedObjectError:
jit_state = 'off'
else:
jit_state = 'off'

result = await self.__execute(
self._intro_query,
(list(typeoids),),
0,
timeout,
ignore_custom_codec=True,
)

if jit_state != 'off':
await self.__execute(
"""
SELECT
set_config('jit', $1, false)
""",
(jit_state,),
0,
timeout,
ignore_custom_codec=True,
)

return result

async def _introspect_type(self, typename, schema):
if (
schema == 'pg_catalog'
Expand Down Expand Up @@ -2370,7 +2403,7 @@ class _ConnectionProxy:
ServerCapabilities = collections.namedtuple(
'ServerCapabilities',
['advisory_locks', 'notifications', 'plpgsql', 'sql_reset',
'sql_close_all'])
'sql_close_all', 'jit'])
ServerCapabilities.__doc__ = 'PostgreSQL server capabilities.'


Expand All @@ -2382,34 +2415,39 @@ def _detect_server_capabilities(server_version, connection_settings):
plpgsql = False
sql_reset = True
sql_close_all = False
jit = False
elif hasattr(connection_settings, 'crdb_version'):
# CockroachDB detected.
advisory_locks = False
notifications = False
plpgsql = False
sql_reset = False
sql_close_all = False
jit = False
elif hasattr(connection_settings, 'crate_version'):
# CrateDB detected.
advisory_locks = False
notifications = False
plpgsql = False
sql_reset = False
sql_close_all = False
jit = False
else:
# Standard PostgreSQL server assumed.
advisory_locks = True
notifications = True
plpgsql = True
sql_reset = True
sql_close_all = True
jit = server_version >= (11, 0)

return ServerCapabilities(
advisory_locks=advisory_locks,
notifications=notifications,
plpgsql=plpgsql,
sql_reset=sql_reset,
sql_close_all=sql_close_all
sql_close_all=sql_close_all,
jit=jit,
)


Expand Down
8 changes: 7 additions & 1 deletion tests/test_introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def tearDownClass(cls):

super().tearDownClass()

@classmethod
def get_server_settings(cls):
settings = super().get_server_settings()
settings.pop('jit', None)
return settings

def setUp(self):
super().setUp()
self.loop.run_until_complete(self._add_custom_codec(self.con))
Expand Down Expand Up @@ -124,7 +130,7 @@ async def test_introspection_no_stmt_cache_03(self):
await self.con.fetchval(
"SELECT $1::int[], '{foo}'".format(foo='a' * 10000), [1, 2])

self.assertEqual(apg_con._uid, old_uid + 1)
self.assertGreater(apg_con._uid, old_uid)

async def test_introspection_sticks_for_ps(self):
# Test that the introspected codec pipeline for a prepared
Expand Down

0 comments on commit a6c560b

Please sign in to comment.