Skip to content
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

Disable JIT while doing type introspection #1082

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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