Skip to content

Add builtin varchar array codec#1321

Open
anton-oai wants to merge 1 commit into
MagicStack:masterfrom
anton-oai:codex/add-varchar-array-codec
Open

Add builtin varchar array codec#1321
anton-oai wants to merge 1 commit into
MagicStack:masterfrom
anton-oai:codex/add-varchar-array-codec

Conversation

@anton-oai
Copy link
Copy Markdown

Summary

Add a builtin binary codec for PostgreSQL varchar[] / pg_catalog._varchar.

asyncpg already has builtin support for scalar varchar and for text[], but not for varchar[]. As a result, queries that return or bind varchar[] require asyncpg to run its recursive typeinfo introspection query on each new connection.

This change registers varchar[] as a builtin array codec. It reuses text element encode/decode behavior, while encoding arrays with VARCHAROID as the element OID so bound varchar[] parameters match PostgreSQL's expected array element type.

Motivation

Without an explicit builtin codec for varchar[], asyncpg runs its typeinfo introspection query for every new connection that sees this type.

In isolation, the cost of one introspection query is low in our measurements, around ~200us when a single query is executed. However, during reconnect storm events with thousands of concurrent reconnects, this introspection query becomes a major CPU hit and can materially degrade overall database performance.

For schemas with many varchar[] columns, especially on large tables where changing column types is not desirable, supporting this native PostgreSQL type out of the box is more practical than requiring applications to avoid varchar[] or add query-level casts.

Test Plan

Added test_varchar_array_does_not_introspect, which uses a custom connection class that counts _introspect_types() calls and verifies that both parameter and result varchar[] paths work without triggering type introspection.

Locally run:

PYTHONPATH=. .venv/bin/python -m pytest tests/test_introspection.py -k varchar_array_does_not_introspect -q

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant