Store fetched array types before running the first query - #1230
Open
Nitjsefnie wants to merge 1 commit into
Open
Nitjsefnie wants to merge 1 commit into
Nitjsefnie wants to merge 1 commit into
Conversation
fetchArrayTypes() awaited the types query and only then filled typeArrayMap. The types query's ReadyForQuery resolves it and, in the same synchronous call, goes on to execute(initial), so a new connection's first query was built while the map was still empty and an sql.array() parameter was bound as its element type, e.g. "malformed array literal" with an array cast. Fill the map inside the query's resolve, like fetchState() does, so it is populated before the initial query is built. Fixes porsager#789 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A new connection built its first query before the array types it had just fetched were stored, so an
sql.array()parameter in that query was bound as its element type. This fillstypeArrayMapinside the types query'sresolve, which is thefetchState()idiom, so the map is populated beforeReadyForQuerygoes on toexecute(initial).Related Issues and Pull Requests
Fixes #789
Relates to #1081
Changes
src/connection.jsfetchArrayTypes(): wraps the types query'sresolveso thataddArrayTyperuns synchronously whenReadyForQueryresolves it. Before, it ran in theawaitcontinuation, which is queued as a microtask afterexecute(initial)has already built the opening query against an empty map. Startup sequencing and the rejection path are unchanged: a failing types query still rejectsfetchArrayTypes()'s promise exactly as before. Fixes #789: Wait for DB type initialization before querying #1081 fixes the same bug by deferringexecute(initial)behind a promise on every connection start instead.tests/index.js: new test "Array in first query of new client". It creates a fresh client, whosetypeArrayMapis empty, and sendssql.array([1, 2])::int[]as its first query. The existing array tests use the shared, already-warmed client, which is why they pass on the unfixed code.cjs/,cf/,deno/: regenerated withnpm run build, keeping only the hunks for this change. A fresh build atmasteralso rewrites output that411429eand9afd16dnever regenerated: theparameters[i] = xline in eachconnection.js, and the retry-serialization tests incjs/testsanddeno/tests. That output is left out of this PR.Testing
src/with22P02 malformed array literal: "1,2"and passes with the fix.npm test(esm, cjs, deno) passes on Node 24 against PostgreSQL 17, configured like CI (pg_hba.conf, ssl,wal_level = logical,max_prepared_transactions). Only one cell of the CI matrix was run.max > 1fail the same way on unfixed code: with three concurrent cold queries onmax: 10, exactly one fails in every run, and which one varies. With the fix, all three succeed in five runs out of five. There is no separate pooled test, because every connection goes through the samefetchArrayTypes()that the new test covers.Footer
Generated by Claude Opus 5.5 (brief, implementation, review)