From: HawkOnPK (@HawkOnPK) <noreply+HawkOnPK@github.com>
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
Subject: Re: [psycopg/psycopg] issue #1157: proposal: retrieve range value dumper from inner_oid setting
Date: Tue, 10 Nov 2026 16:12:19 +0000
Message-ID: <gh-psycopg-psycopg-1157-c6772532746@github.com> (raw)
In-Reply-To: <gh-psycopg-psycopg-1157@github.com>
References: <gh-psycopg-psycopg-1157@github.com>
Another, simpler way to hit this, without COPY or `set_types`: a plain query parameter of the typed `Int4Range` class, in AUTO or binary format.
```python
import psycopg
from psycopg.types.range import Int4Range, Range
with psycopg.connect(DSN) as conn:
conn.execute("select %t::int4range", [Int4Range(1, 5)]) # ok (text)
conn.execute("select %s::int4range", [Range(1, 5)]) # ok (generic Range, oid upgraded)
conn.execute("select %s::int4range", [Int4Range(1, 5)]) # ProtocolViolation
conn.execute("select %b::int4range", [Int4Range(1, 5)]) # ProtocolViolation
```
```
psycopg.errors.ProtocolViolation: insufficient data left in message
CONTEXT: unnamed portal parameter $1
```
`Int4RangeBinaryDumper` dumps its bounds with the dumper of the Python value, so `1` and `5` go out as int2 while the oid says int4range:
```python
>>> bytes(Int4RangeBinaryDumper(Int4Range, conn).dump(Int4Range(1, 5)))
b'\x02\x00\x00\x00\x02\x00\x01\x00\x00\x00\x02\x00\x05'
```
Since `Int4RangeBinaryDumper` is the last dumper registered for `Int4Range`, it is also the one chosen for `%s`, so the typed class fails by default, while the generic `Range` works. Taking the bound dumper from the range's element oid, as proposed here, would fix it. Until then, registering `Int4RangeDumper` (text) last for `Int4Range` works around it.
psycopg 3.3.8 (binary implementation), PostgreSQL 17, Python 3.14, Windows.