Repository navigation
Description
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
roji commentedon Oct 18, 2026
The fix is already present on
mainin npgsql/npgsql#6472, commit fc81e74 (March 11, 2026). That change preserves the expected CLR dimensions for zero-dimensional PostgreSQL arrays and includesGenerics_read_empty_multidim_arrayregression coverage.Verified unchanged main (1083526): all 35
ArrayTestspass, 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 originalList<Vertex>/VertexesConvertermapping. The same assertions fail with the released drivers. Database setup was performed separately for this cross-major check because Npgsql 11 removes theMultiplexingAPI 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.
roji commentedon Oct 18, 2026
Already fixed in main via #6472, will submit backport PRs.