Skip to content

Reading a large interval as TimeSpan returns a wrong value instead of throwing #6726

Description

@johnkaram02

Steps to reproduce

await using var dataSource = NpgsqlDataSource.Create("Host=localhost;Username=test;Password=test;Database=test");
await using var cmd = dataSource.CreateCommand("SELECT interval '300000000 hours'");
var value = (TimeSpan)(await cmd.ExecuteScalarAsync())!;
Console.WriteLine(value);

The issue

The interval is larger than TimeSpan.MaxValue, but reading it as TimeSpan doesn't throw. The tick computation overflows and the read returns a wrapped value with the wrong sign, -8850398.05:36:10.9551616. interval '-300000000 hours' comes back positive, and interval '20000000 days' wraps the same way. PostgreSQL itself returns 300000000:00:00.

Reading an interval that has months as TimeSpan already throws InvalidCastException. An interval outside the range of TimeSpan should throw too, instead of returning a wrong value.

Further technical details

Npgsql version: main at b35800b
PostgreSQL version: 17.11
Operating system: Linux x86_64

Other details about my project setup: .NET SDK 10.0.403

Activity

  1. vonzshik commented on Nov 17, 2026

    @vonzshik
    Contributor

    @Mr444-dev could you post anything here so I can assign this issue to you?

  2. added theissue type on Nov 17, 2026
  3. added this to the 10.0.4 milestone on Nov 17, 2026
  4. 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