sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Postgres: "unable to resolve type OIDs" when resolving nested composite types — `fill_cache` drops pending rows on `continue 'outer`

Open NuclEnergy opened this issue 1 month ago • 1 comments

I have found these related issues/pull requests

I searched for "unable to resolve type OIDs" and found no existing reports of this problem. Related, but different:

  • #4361 (fixes #4305): touched the same type-resolution path in resolve.rs (unnamed statement destroyed by the catalog lookup), but does not change the fill_cache dependency loop.
  • #1672: problems with arrays of custom types inside records, on the decoding side; this report is about OID resolution before decoding.
  • #2133: custom type name → OID resolution ignoring search_path; a different resolution bug.

Description

Bug Description

TypeResolver::fill_cache in sqlx-postgres/src/connection/resolve.rs can lose types it still has to resolve. The query then fails with:

Protocol("unable to resolve type OIDs: [Oid(…)]")

It happens when one catalog row resolves a type that several pending rows depend on, and one of those dependents still needs another type that isn't cached yet.

In that case the loop runs continue 'outer and throws away the rest of resolved_dependencies. Those rows were already taken out of missing_dependencies and are never processed again. Any type that was waiting on one of them stays in missing_dependencies forever, and the function returns the error above.

The type cache is per connection, so in practice this shows up as intermittent errors on fresh connections only. A connection that already cached the leaf types through an earlier query does not hit it. This makes it hard to track down in production.

Reproduction steps

Minimal Reproduction

Schema. Creation order matters because rows come back ORDER BY lookup_oid:

CREATE TYPE leaf_a AS (x int4);
CREATE TYPE leaf_b AS (y int4);
CREATE TYPE pair   AS (a leaf_a, b leaf_b);
CREATE TYPE holder AS (items leaf_a[]);
use sqlx::{Connection, PgConnection};

#[tokio::main]
async fn main() {
    let url = std::env::var("DATABASE_URL").unwrap();
    let mut conn = PgConnection::connect(&url).await.unwrap();
    let res = sqlx::query("SELECT NULL::pair[] AS pairs, NULL::holder AS holder")
        .execute(&mut conn)
        .await;
    println!("{res:?}");
}

Output (the missing OID is leaf_a[]):

Err(Protocol("unable to resolve type OIDs: [Oid(120067)]"))

Walkthrough

Pass Rows (by OID) What happens
1 pair[], holder pair[] waits on pair; holder waits on leaf_a[]
2 leaf_a[], pair leaf_a[] waits on leaf_a; pair waits on leaf_a → missing[leaf_a] = [leaf_a[], pair]
3 leaf_a leaf_a is cached and resolved_dependencies = [leaf_a[], pair]. pop_back() gives pair, which now waits on leaf_b → continue 'outer, and leaf_a[] is dropped
4 leaf_b leaf_b → pair → pair[] resolve fine
end missing[leaf_a[]] = [holder] is still there → error

Proposed Fix

When a dependent row hits a new missing dependency, register it as missing as before, then keep draining resolved_dependencies instead of leaving the row loop:

--- a/sqlx-postgres/src/connection/resolve.rs
+++ b/sqlx-postgres/src/connection/resolve.rs
@@ -444,7 +444,15 @@
 
                         self.push_type("NULL", missing_oid.0);
 
-                        continue 'outer;
+                        // Keep draining rows whose dependency was just resolved;
+                        // abandoning them here would leave them unresolved forever.
+                        match resolved_dependencies.pop_back() {
+                            Some(next_row) => {
+                                type_row = next_row;
+                                continue;
+                            }
+                            None => continue 'outer,
+                        }
                     }
 
                     resolved_dependencies.extend(

I tested this locally with [patch.crates-io] against 0.9.0. The reproduction above then returns Ok(PgQueryResult { rows_affected: 1 }), and the real-world queries that originally failed also work.

The same code is unchanged on main, so the patch should apply there as is. The reproduction schema could also be used as a regression test.

Workaround

Until this is fixed, warm the per-connection type cache in PgPoolOptions::after_connect by running SELECT NULL::<type> once per custom type, in dependency order (leaf types first).

SQLx version

0.9.0 (also present on main as of 2026-11-02)

Enabled SQLx features

postgres, runtime-tokio

Database server and version

PostgreSQL 18.4

Operating system

macOS (server in Docker, Debian)

Rust version

rustc 1.98.1 (48a229cea 2026-09-01)

NuclEnergy avatar Nov 02 '26 00:11 NuclEnergy

I reproduced this on main (PostgreSQL 18.3) with the schema above; it fails with the same Protocol("unable to resolve type OIDs: [Oid(16390)]").

The analysis is correct: resolved_dependencies is filled by missing_dependencies.remove(&type_row.oid), so those rows have already left the only durable store. When a dependent row then hits another missing OID, continue 'outer drops the remaining deque entries.

I verified the proposed fix locally:

  • test_issue_4497 fails before the patch and passes after it.
  • Existing custom-type tests (it_resolves_custom_type_in_array, it_resolves_custom_types_in_anonymous_records, custom_type_resolution_respects_search_path, test_issue_4305) still pass.
  • Full tests/postgres: 63 passed, 2 ignored.

I'm planning to open a PR with the fix plus the regression test unless you'd rather take it yourself.

ducnhan123-co avatar Nov 02 '26 07:11 ducnhan123-co