Skip to content

Commit 3e43fcf

Browse files
committed
Make prepared statement uid generator global
1 parent f4e17dd commit 3e43fcf

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

asyncpg/connection.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Connection(metaclass=ConnectionMeta):
3939
"""
4040

4141
__slots__ = ('_protocol', '_transport', '_loop',
42-
'_top_xact', '_uid', '_aborted',
42+
'_top_xact', '_aborted',
4343
'_pool_release_ctr', '_stmt_cache', '_stmts_to_close',
4444
'_listeners', '_server_version', '_server_caps',
4545
'_intro_query', '_reset_query', '_proxy',
@@ -54,7 +54,6 @@ def __init__(self, protocol, transport, loop,
5454
self._transport = transport
5555
self._loop = loop
5656
self._top_xact = None
57-
self._uid = 0
5857
self._aborted = False
5958
# Incremented very time the connection is released back to a pool.
6059
# Used to catch invalid references to connection-related resources
@@ -990,8 +989,9 @@ def _check_open(self):
990989
raise exceptions.InterfaceError('connection is closed')
991990

992991
def _get_unique_id(self, prefix):
993-
self._uid += 1
994-
return '__asyncpg_{}_{}__'.format(prefix, self._uid)
992+
global _uid
993+
_uid += 1
994+
return '__asyncpg_{}_{:x}__'.format(prefix, _uid)
995995

996996
def _mark_stmts_as_closed(self):
997997
for stmt in self._stmt_cache.iter_statements():
@@ -1598,3 +1598,6 @@ def _detect_server_capabilities(server_version, connection_settings):
15981598
sql_reset=sql_reset,
15991599
sql_close_all=sql_close_all
16001600
)
1601+
1602+
1603+
_uid = 0

tests/test_introspection.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
from asyncpg import _testbase as tb
9+
from asyncpg import connection as apg_con
910

1011

1112
MAX_RUNTIME = 0.1
@@ -47,6 +48,8 @@ async def test_introspection_on_large_db(self):
4748

4849
@tb.with_connection_options(statement_cache_size=0)
4950
async def test_introspection_no_stmt_cache_01(self):
51+
old_uid = apg_con._uid
52+
5053
self.assertEqual(self.con._stmt_cache.get_max_size(), 0)
5154
await self.con.fetchval('SELECT $1::int[]', [1, 2])
5255

@@ -62,12 +65,14 @@ async def test_introspection_no_stmt_cache_01(self):
6265
DROP EXTENSION hstore
6366
''')
6467

65-
self.assertEqual(self.con._uid, 0)
68+
self.assertEqual(apg_con._uid, old_uid)
6669

6770
@tb.with_connection_options(max_cacheable_statement_size=1)
6871
async def test_introspection_no_stmt_cache_02(self):
6972
# max_cacheable_statement_size will disable caching both for
7073
# the user query and for the introspection query.
74+
old_uid = apg_con._uid
75+
7176
await self.con.fetchval('SELECT $1::int[]', [1, 2])
7277

7378
await self.con.execute('''
@@ -82,13 +87,15 @@ async def test_introspection_no_stmt_cache_02(self):
8287
DROP EXTENSION hstore
8388
''')
8489

85-
self.assertEqual(self.con._uid, 0)
90+
self.assertEqual(apg_con._uid, old_uid)
8691

8792
@tb.with_connection_options(max_cacheable_statement_size=10000)
8893
async def test_introspection_no_stmt_cache_03(self):
8994
# max_cacheable_statement_size will disable caching for
9095
# the user query but not for the introspection query.
96+
old_uid = apg_con._uid
97+
9198
await self.con.fetchval(
9299
"SELECT $1::int[], '{foo}'".format(foo='a' * 10000), [1, 2])
93100

94-
self.assertEqual(self.con._uid, 1)
101+
self.assertEqual(apg_con._uid, old_uid + 1)

0 commit comments

Comments
 (0)