Search code examples
postgisnpgsql

Timeout when selecting PostgisMultipolygon with Npgsql


In this example I use Npgsql in development version 3.1.0-alpha6.

I want to specify a PostgisGeometry object as a parameter in a query (NpgsqlDbType.Geometry) and select the object again.

Queries with types like Point, MultiPoint, LineString, MultiLineString, Polygon and GeometryCollection will be returned correctly. A PostgisMultiPolygon object with only one polygon will be returned correctly too.

However, it does not work with a PostgisMultiPolygon with more than one polygon.

PostgisMultiPolygon geom1 = new PostgisMultiPolygon(new[]
{
    new PostgisPolygon(new[]
    {
        new[]
        {
            new Coordinate2D(40, 40),
            new Coordinate2D(20, 45),
            new Coordinate2D(45, 30),
            new Coordinate2D(40, 40)
        }
    })
}) {SRID = 4326};
PostgisMultiPolygon geom2 = new PostgisMultiPolygon(new[]
{
    new PostgisPolygon(new[]
    {
        new[]
        {
            new Coordinate2D(40, 40),
            new Coordinate2D(20, 45),
            new Coordinate2D(45, 30),
            new Coordinate2D(40, 40)
        }
    }),
    new PostgisPolygon(new[]
    {
        new[]
        {
            new Coordinate2D(20, 35),
            new Coordinate2D(10, 30),
            new Coordinate2D(10, 10),
            new Coordinate2D(30, 5),
            new Coordinate2D(45, 20),
            new Coordinate2D(20, 35)
        }
    })
}) {SRID = 4326};

using (NpgsqlConnection connection = CreateConnection())
{
    NpgsqlCommand command = connection.CreateCommand();
    command.Parameters.AddWithValue("p1", NpgsqlDbType.Geometry, geom1);
    command.CommandText = "Select :p1";
    command.ExecuteScalar();
}

using (NpgsqlConnection connection = CreateConnection())
{
    NpgsqlCommand command = connection.CreateCommand();
    command.Parameters.AddWithValue("p1", NpgsqlDbType.Geometry, geom2);
    command.CommandText = "Select :p1";
    command.ExecuteScalar(); //timeout occurs...
}

If increasing the CommandTimeout, the timeout occurs anyway. Thanks in advance!


Solution

  • The fix for this bug has just been merged: https://github.com/npgsql/npgsql/pull/1025