Skip to content

Regression in Npgsql 9: reading an empty multidimensional array throws InvalidCastException #6675

Closed

Description

@roji

Reading an empty PostgreSQL array with GetFieldValue<double[,]>() succeeds in Npgsql 8 but throws in Npgsql 9 and 10:

System.InvalidCastException: Unable to cast object of type 'System.Double[]' to type 'System.Double[,]'.
   at Npgsql.NpgsqlDataReader.GetFieldValueCore[T](Int32 ordinal)
   at Npgsql.NpgsqlDataReader.GetFieldValue[T](Int32 ordinal)

Originally reported through EF in npgsql/efcore.pg#3514. The EF repro and version matrix are in this analysis, but neither EF nor a value converter is necessary to reproduce the failure.

Minimal repro

Reference the Npgsql package and set Test__Npgsql__DefaultConnection to a PostgreSQL connection string. This program makes no database modifications.

using System;
using Npgsql;

await using var connection = new NpgsqlConnection(
    Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection"));
await connection.OpenAsync();
await using var command = new NpgsqlCommand("SELECT '{}'::double precision[]", connection);
await using var reader = await command.ExecuteReaderAsync();
await reader.ReadAsync();
reader.GetFieldValue<double[,]>(0);

Expected: an empty double[,], as in Npgsql 8.

Actual: InvalidCastException because the constructed value is double[].

Versions tested

All results below use PostgreSQL 18.4 and the same .NET 10.0.8 runtime, isolating package-version differences.

Npgsql Empty array read
7.0.0, 7.0.10 Succeeds
8.0.0, 8.0.9 Succeeds
9.0.0, 9.0.5 Throws
10.0.3 Throws

Replacing the SQL with SELECT '{{1,2}}'::double precision[] succeeds in the tested 7.0.0, 7.0.10, 8.0.0, 8.0.9, 9.0.0 and 10.0.3 versions. EF round-trips likewise succeed with provider 8.0.11, fail with providers 9.0.0, 9.0.4 and 10.0.3 for empty arrays, and succeed with nonempty arrays in all four cases.

Analysis

PostgreSQL represents an empty array with zero dimensions. Npgsql 8 honors the requested CLR rank when constructing the result, using new int[_expectedDimensions ?? dimensions].

npgsql/npgsql#5736 replaced that logic with a slice based solely on the dimensions received from PostgreSQL. The logic remains in 10.0.3. For an empty PostgreSQL array, the slice has length zero and CreateCollection returns Array.Empty<TElement?>(), losing the requested multidimensional rank.

The fix should restore the requested CLR rank when reading zero-dimensional PostgreSQL arrays and cover empty multidimensional reads in regression tests. This regresses the behavior added in npgsql/npgsql#2916, following the historical report npgsql/npgsql#1271.

Activity

  1. added theissue type on Oct 18, 2026
  2. self-assigned this
    on Oct 18, 2026
  3. roji commented on Oct 18, 2026

    @roji
    MemberAuthor

    The fix is already present on main in npgsql/npgsql#6472, commit fc81e74 (March 11, 2026). That change preserves the expected CLR dimensions for zero-dimensional PostgreSQL arrays and includes Generics_read_empty_multidim_array regression coverage.

    Verified unchanged main (1083526): all 35 ArrayTests pass, and the exact ADO.NET repro returns an empty rank-2 array. Additional checks cover async reads, rank-3 arrays, nullable/reference elements, and empty/nonempty parameter round-trips.

    Also verified the EF scenario with providers 9.0.0 and 10.0.3 against the built main driver: empty and nonempty arrays round-trip correctly, both as direct double[,] properties and through the original List<Vertex> / VertexesConverter mapping. The same assertions fail with the released drivers. Database setup was performed separately for this cross-major check because Npgsql 11 removes the Multiplexing API used by older EF providers during database creation/deletion; no application mapping or query changes were needed.

    No new main-branch fix is needed. The tested stable 9.x and 10.x releases remain affected; resolving this for those versions requires a backport.

  4. added this to the 10.0.4 milestone on Oct 18, 2026
  5. roji commented on Oct 18, 2026

    @roji
    MemberAuthor

    Already fixed in main via #6472, will submit backport PRs.

  6. modified the milestones: 10.0.4, 11.0.0 on Oct 18, 2026
  7. linked a pull request that will close this issueCleanup array converter #6472on Oct 18, 2026
  8. linked a pull request that will close this issue[9.0] Fix reading empty multidimensional arrays #6676on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions