Skip to content

NpgsqlRange<T>.ToString() formats bounds with the current culture, so EF Core's ToQueryString() and parameter logs show range text PostgreSQL misreads #6730

Description

@CaffeinatedCoder

NpgsqlRange<T>.ToString() appends each bound with StringBuilder.Append(T), which uses the current culture. EF Core prints a parameter in ToQueryString() and in its command log (with EnableSensitiveDataLogging) through Convert.ToString(value, CultureInfo.InvariantCulture). That asks for invariant text, but NpgsqlRange<T> doesn't implement IFormattable, so the request has no effect and the current culture wins.

Under de-DE a range parameter prints like this:

  • [1,5,2,25) for [1.5, 2.25). PostgreSQL rejects it: malformed range literal … Too many commas.
  • [01.07.2026,04.07.2026] for 1 to 4 July. PostgreSQL accepts it and, with the default DateStyle (ISO, MDY), reads it as [2026-01-07,2026-04-08), 7 January to 7 April.

So the query text people copy from ToQueryString() or a log into psql fails or, worse, silently queries other dates. The values sent to the server are fine, because parameters go out in binary. Only the text is wrong.

Under the invariant culture, dates print as [07/01/2026,07/04/2026], which works only because PostgreSQL's default date order is month first. A DateTime bound also loses its fraction digits, so 10:30:00.5 prints as 10:30:00.

The bounds are appended in NpgsqlRange.cs L329–L347. EF Core asks for invariant text in DbParameterCollectionExtensions.cs L212.

Formatting the bounds with CultureInfo.InvariantCulture would fix the decimal case and make the output the same on every machine. Writing date and time bounds in ISO 8601 (yyyy-MM-dd, and the round-trip time) would make the text mean the same thing to PostgreSQL whatever its DateStyle, and keep the fraction digits. #3599 was about the reverse direction (culture in Parse) and was closed in favour of removing Parse. This one is only about the text ToString() writes.

Environment
PostgreSQL 18.6 (postgres:18), DateStyle ISO, MDY
Packages Npgsql 10.0.3, Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3, Microsoft.EntityFrameworkCore 10.0.4
Runtime .NET 10.0.14, macOS arm64

main at b35800b has the same ToString().

Repro
CREATE TABLE "Rows" ("Id" int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, "Days" daterange NOT NULL, "Prices" numrange NOT NULL);
using System.Globalization;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using NpgsqlTypes;

var days = new NpgsqlRange<DateOnly>(new DateOnly(2026, 7, 1), true, new DateOnly(2026, 7, 4), true);
var prices = new NpgsqlRange<decimal>(1.5m, true, 2.25m, false);

foreach (var culture in new[] { "de-DE", "" })
{
    CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
    Console.WriteLine($"--- CurrentCulture = \"{culture}\"");
    Console.WriteLine($"days.ToString():   {days}");
    Console.WriteLine($"prices.ToString(): {prices}");

    await using var context = new TestContext();
    var query = context.Rows.Where(r => r.Days.Overlaps(days) && r.Prices.Contains(prices));
    Console.WriteLine(query.ToQueryString());
    await query.ToListAsync();
}

public class TestContext : DbContext
{
    public DbSet<Row> Rows => Set<Row>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql("Host=localhost;Port=55432;Username=postgres;Password=postgres")
            .LogTo(Console.WriteLine, [RelationalEventId.CommandExecuted])
            .EnableSensitiveDataLogging();
}

public class Row
{
    public int Id { get; set; }
    public NpgsqlRange<DateOnly> Days { get; set; }
    public NpgsqlRange<decimal> Prices { get; set; }
}

Output:

--- CurrentCulture = "de-DE"
days.ToString():   [01.07.2026,04.07.2026]
prices.ToString(): [1,5,2,25)
-- @days='[01.07.2026,04.07.2026]' (DbType = Object)
-- @prices='[1,5,2,25)' (DbType = Object)
SELECT r."Id", r."Days", r."Prices"
FROM "Rows" AS r
WHERE r."Days" && @days AND r."Prices" @> @prices
info: 30.10.2026 00:51:16.788 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (28ms) [Parameters=[@days='[01.07.2026,04.07.2026]' (DbType = Object), @prices='[1,5,2,25)' (DbType = Object)], CommandType='Text', CommandTimeout='30']
      ...
--- CurrentCulture = ""
days.ToString():   [07/01/2026,07/04/2026]
prices.ToString(): [1.5,2.25)
-- @days='[07/01/2026,07/04/2026]' (DbType = Object)
-- @prices='[1.5,2.25)' (DbType = Object)
...

And what PostgreSQL makes of the de-DE text:

postgres=# SELECT '[01.07.2026,04.07.2026]'::daterange;
[2026-01-07,2026-04-08)
postgres=# SELECT '[1,5,2,25)'::numrange;
ERROR:  malformed range literal: "[1,5,2,25)"
DETAIL:  Too many commas.

Activity

  1. changed the title [-]NpgsqlRange<T>.ToString() formats bounds with the current culture, so EF Core's ToQueryString() and parameter logs show range text PostgreSQL misreads[/-] [+]`NpgsqlRange<T>.ToString()` formats bounds with the current culture, so EF Core's `ToQueryString()` and parameter logs show range text PostgreSQL misreads[/+] on Oct 29, 2026
  2. EnesKartal commented on Oct 30, 2026

    @EnesKartal

    I'd like to take this. Plan: format the bounds with CultureInfo.InvariantCulture in NpgsqlRange<T>.ToString() and write date/time bounds in ISO 8601, with tests under a non-invariant culture. Let me know if you'd rather keep the date format out of scope, or if someone is already working on it.

  3. added theissue type on Nov 17, 2026
  4. added this to the 10.0.4 milestone on Nov 17, 2026
  5. vonzshik commented on Nov 17, 2026

    @vonzshik
    Contributor

    This indeed looks like a bug, though I'm not entirely sure whether we should just implement IFormattable rather than pass InvariantCulture everywhere.
    Since this seems to be (somewhat) EF Core related, we should wait for @roji to chime in.

  6. modified the milestones: 10.0.4, 10.0.5 on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions