Skip to content

Commit 5c9986c

Browse files
authored
Avoid performing type introspection on known types (#1243)
Type codec setup functions will no longer attempt to introspect the type if it's one of the known builtin types. Fixes: #1206 Fixes: #1138 Fixes: #1242
1 parent 5a1ee01 commit 5c9986c

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

asyncpg/connection.py

+12-20
Original file line numberDiff line numberDiff line change
@@ -534,26 +534,18 @@ async def _introspect_types(self, typeoids, timeout):
534534
return result
535535

536536
async def _introspect_type(self, typename, schema):
537-
if (
538-
schema == 'pg_catalog'
539-
and typename.lower() in protocol.BUILTIN_TYPE_NAME_MAP
540-
):
541-
typeoid = protocol.BUILTIN_TYPE_NAME_MAP[typename.lower()]
542-
rows = await self._execute(
543-
introspection.TYPE_BY_OID,
544-
[typeoid],
545-
limit=0,
546-
timeout=None,
547-
ignore_custom_codec=True,
548-
)
549-
else:
550-
rows = await self._execute(
551-
introspection.TYPE_BY_NAME,
552-
[typename, schema],
553-
limit=1,
554-
timeout=None,
555-
ignore_custom_codec=True,
556-
)
537+
if schema == 'pg_catalog' and not typename.endswith("[]"):
538+
typeoid = protocol.BUILTIN_TYPE_NAME_MAP.get(typename.lower())
539+
if typeoid is not None:
540+
return introspection.TypeRecord((typeoid, None, b"b"))
541+
542+
rows = await self._execute(
543+
introspection.TYPE_BY_NAME,
544+
[typename, schema],
545+
limit=1,
546+
timeout=None,
547+
ignore_custom_codec=True,
548+
)
557549

558550
if not rows:
559551
raise ValueError(

asyncpg/introspection.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
from __future__ import annotations
88

99
import typing
10+
from .protocol.protocol import _create_record # type: ignore
1011

1112
if typing.TYPE_CHECKING:
1213
from . import protocol
1314

15+
1416
_TYPEINFO_13: typing.Final = '''\
1517
(
1618
SELECT
@@ -267,16 +269,12 @@
267269
'''
268270

269271

270-
TYPE_BY_OID = '''\
271-
SELECT
272-
t.oid,
273-
t.typelem AS elemtype,
274-
t.typtype AS kind
275-
FROM
276-
pg_catalog.pg_type AS t
277-
WHERE
278-
t.oid = $1
279-
'''
272+
def TypeRecord(
273+
rec: typing.Tuple[int, typing.Optional[int], bytes],
274+
) -> protocol.Record:
275+
assert len(rec) == 3
276+
return _create_record( # type: ignore
277+
{"oid": 0, "elemtype": 1, "kind": 2}, rec)
280278

281279

282280
# 'b' for a base type, 'd' for a domain, 'e' for enum.

0 commit comments

Comments
 (0)