Skip to content

Compare column types by identity when decoding untyped values - #57

Merged
quinnj-bot[bot] merged 2 commits into
mainfrom
claude/untyped-type-dispatch
Nov 19, 2026
Merged

quinnj-bot[bot] merged 2 commits into
mainfrom
claude/untyped-type-dispatch

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

Untyped reads (DBInterface.execute(conn, sql), then Tables.columntable or row iteration) spent most of their decode time choosing a decoder, not decoding. parse_value runs once per value and compared the column's Julia type with T == Bool, T == Int16, ... and T <: Number. On a Type that is not known at compile time each == is a runtime jl_types_equal call and each <: a runtime subtype check. A text value reached its branch after 18 of them. A profile of a million short text values puts 1,064 of its 2,047 samples outside garbage collection in those comparisons.

What changed

  • parse_value compares types with ===, which compiles to a pointer comparison (== compiles to a call to jl_types_equal). Text columns return first. The identity checks for bytea, JSON and the array types now come before the two subtype checks, which only see types no identity check matched. The branch each registered type takes is unchanged.
  • type_info looks up the default registry only when the connection's registry lacks the OID; it used to build the default entry for every value.

Typed reads into the field types the manual lists (integers, floats, strings, dates, UUIDs, ...) parse the column text without parse_value and are unchanged.

Measurements

Seven rounds of fresh processes, each side (main, this branch, two other perf branches, psycopg) in alternating order, PostgreSQL 17.12 in Docker on the same Mac, Julia 1.12.6 with one thread, load average 8 to 59 (median 16). Each process times two reads of 1M rows per case after a full GC and keeps the faster; the table gives the median over rounds, range in brackets. "Mcycles" is the reading thread's CPU cycles.

1M rows main wall ms branch wall ms main Mcycles branch Mcycles
12-column table, untyped 2743 [2695-3222] 1854 [1746-2069] 9494 6330 (0.67x)
12-column table, Tables.columntable 2382 [2303-2944] 1463 [1417-1672] 8131 4877 (0.60x)
12-column table, Postgres.cursor 2742 [2640-4026] 1892 [1832-2063] 7374 4264 (0.58x)
short text, untyped 316 [308-443] 191 [178-194] 1093 642 (0.59x)
date, untyped 278 [274-301] 205 [188-234] 966 692 (0.72x)
uuid, untyped 454 [442-499] 402 [369-417] 1557 1379 (0.89x)
int4[5], untyped 1269 [1216-1372] 1052 [980-1111] 4531 3620 (0.80x)
int8, untyped 206 [195-245] 180 [176-197] 680 600 (0.88x)

Untyped and cursor reads of the other types are faster or unchanged in cycles, except timestamptz untyped and columntable (below). numeric and timestamptz decode through registered parsers, which return before these checks, and bool is the first check, so those barely change. Typed reads run the same code on both sides; their medians moved by -9% to +5%. The one large wall-time outlier, timestamptz untyped (median 299 ms on main, 399 ms here, range 290-448), is a full garbage collection that landed inside the timed read in more rounds on this branch: its non-GC time (wall minus GC time) is 243 ms against 248 ms on main (columntable: 257 vs 258), its instructions and allocations are equal, and its cursor read, which keeps no result, is unchanged (627 vs 630 ms, cycles 0.99x).

Small queries are unchanged (6 interleaved rounds of 6,000 calls each): SELECT 1 243 vs 267 µs on main and prepared one-row reads 216-224 vs 214 µs, all bound by the round trip; a 100-row untyped read of the 12-column table goes from 590 to 500 µs.

Verification

  • The server-oracle decode harness (every scalar, array, range, enum and composite type untyped and typed, checked against the server's binary *send output, seed 12, 40 values per type, PostgreSQL 17.12) gives identical results on main and this branch: 2,339 untyped decodes, 9,471 typed reads, and the same known mismatches.
  • New tests cover the branches for types registered without a parser (text for other types, string elements for other vector types, an error for numbers), and the int2[], float4[] and bool[] decoders. Once text returned first, no existing test reached them; they pass on main too.
  • Self-review: every type compared by identity is concrete, and concrete types are unique objects, so === and == agree unless someone registers a different type object equal to one of them (such as Vector{T} where Int32 <: T <: Int32). NumericValue, IntervalType and the timestamp and array types with parsers return before these checks, as before. Trim: the JuliaC --trim=unsafe-warn build of test/postgres_trim_queries.jl (Julia 1.12.6) reports 0 package verifier errors and the 30 known stdlib ones, as on main.
  • Overlap: draft Parse timestamps with Durations.ZonedTimestamp #43 edits the DateTime branch of parse_value; whichever merges second needs a one-line conflict resolution.

🤖 Generated with Claude Code

quinnj and others added 2 commits November 19, 2026 11:03
parse_value runs once per value and picked the decoder with `==` and
`<:` on the column's Julia type, each a runtime call; a text value went
through about twenty of them. The identity checks now use `===`, a
pointer comparison, text columns return first, and the subtype checks
only see types no identity check matched. type_info also stops looking
up the default registry when the connection's registry has the OID.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Text columns now return before the subtype checks, so no test reached
the branches for other registered types; cover them, and the int2[],
float4[] and bool[] decoders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quinnj-bot
quinnj-bot Bot marked this pull request as ready for review November 19, 2026 20:45
@quinnj-bot
quinnj-bot Bot merged commit b109a75 into main Nov 19, 2026
16 checks passed
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