Enum Type Support #185
Replies: 2 comments
-
Implementation PlanClassificationUser-facing feature (added). Requires release notes, changelog label ( Relevant Principles
Steps1. Add
|
Beta Was this translation helpful? Give feedback.
-
|
Implemented in #186. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Enum type support for user-defined PostgreSQL enums. Item #24 from the feature roadmap.
Problem
Users with PostgreSQL enum columns get
RawBytesinstead ofStringwhen usingPreparedQuery(binary format). The internal text-passthrough codec handles enums correctly, but it's package-private so users can't reuse it. Today they'd have to write a duplicate codec for each enum type.With
SimpleQuery(text format), enums already decode asStringvia the unknown-OID fallback -- no registration needed.Wire Protocol Facts
enum_send()delegates tostringTypeSend())Design
Add
with_enum_type(oid: U32): CodecRegistry ?toCodecRegistry. Internally registers text-passthrough codecs for both text and binary formats in one call. Returns a newCodecRegistry. Errors if the OID is already registered or collides with a known array OID -- same validation semantics aswith_codec.What changes
CodecRegistry.with_enum_type(oid)CodecRegistry._with_enum_type(base, oid)What doesn't change
FieldDataTypes(no new union members)_ParamEncoder(no new match arms)_FrontendMessage.bind()(no new encoding paths)CodecinterfaceFieldDatainterfaceSessionconstructorWhy this shape
with_array_typeset the precedent: when the driver knows the codec strategy for a class of PostgreSQL types, it provides a semantic method rather than exposing internal codecs. Enums are exactly this case -- the driver knows that enums are always text-passthrough. The method:with_enum_type) rather than the mechanism (with_codec(oid, TextPassthroughBinaryCodec))with_codeccall only handles one format)with_array_typefor enum arraysParameters
No parameter-side changes. String parameters with OID 0 work when PostgreSQL can infer the type from context. SQL casts (
$1::mood) handle ambiguous contexts. This is the standard approach used by most PostgreSQL drivers.Alternatives considered
_TextPassthroughBinaryCodecpublic: Locks internal codec identity into public API. Requires twowith_codeccalls for both formats. Inconsistent withwith_array_typeprecedent.PgEnumtoFieldDataTypes: Disproportionate cost (expanding a closed union) for a narrow edge case with a SQL-level workaround.RawBytesand must write duplicate codecs. The fix is too small to skip.Footgun: dual-format registration
After
with_enum_type(X), callingwith_codec(X, ...)errors for either format because the OID is registered in both codec maps. This is stricter than two separatewith_codeccalls (which allow independent text/binary registration for the same OID). The asymmetry is intentional -- enum registration is atomic -- and will be documented in the method's docstring.Usage
Register enum OIDs and query
Reading enum results
Enum values arrive as
Stringin query results:Domain modeling with union types
The driver gives you
String. You model your domain however you want:For users who want the driver itself to decode directly into domain types, the existing
with_codecAPI supports that -- write a custom codec whosedecodereturns the domain primitives instead ofString.Passing enum parameters
Beta Was this translation helpful? Give feedback.
All reactions