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

Avoid performing type introspection on known types #1243

Merged
merged 1 commit into from
Mar 15, 2025
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
32 changes: 12 additions & 20 deletions asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,26 +534,18 @@ async def _introspect_types(self, typeoids, timeout):
return result

async def _introspect_type(self, typename, schema):
if (
schema == 'pg_catalog'
and typename.lower() in protocol.BUILTIN_TYPE_NAME_MAP
):
typeoid = protocol.BUILTIN_TYPE_NAME_MAP[typename.lower()]
rows = await self._execute(
introspection.TYPE_BY_OID,
[typeoid],
limit=0,
timeout=None,
ignore_custom_codec=True,
)
else:
rows = await self._execute(
introspection.TYPE_BY_NAME,
[typename, schema],
limit=1,
timeout=None,
ignore_custom_codec=True,
)
if schema == 'pg_catalog' and not typename.endswith("[]"):
typeoid = protocol.BUILTIN_TYPE_NAME_MAP.get(typename.lower())
if typeoid is not None:
return introspection.TypeRecord((typeoid, None, b"b"))

rows = await self._execute(
introspection.TYPE_BY_NAME,
[typename, schema],
limit=1,
timeout=None,
ignore_custom_codec=True,
)

if not rows:
raise ValueError(
Expand Down
18 changes: 8 additions & 10 deletions asyncpg/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from __future__ import annotations

import typing
from .protocol.protocol import _create_record # type: ignore

if typing.TYPE_CHECKING:
from . import protocol


_TYPEINFO_13: typing.Final = '''\
(
SELECT
Expand Down Expand Up @@ -267,16 +269,12 @@
'''


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


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