Skip to content

Composite mapping leaks across connections #1612

Closed

Description

@tedypranolo

It would be great if we can create our own TypeHandler and optionally specify a custom Dictionary<Type,TypeHandler> for a NpgsqlCommand that will override any global/connection wide mapping.

It will solve a couple of problems I'm currently having:

  1. Mapping smallint composite field to C# byte property.
  2. MapComposite (non global) seems to retain mapping across connection instance when connection pooling is enabled.

Steps to reproduce

Sql definition

create type person as ( id int, num SMALLINT);
create function getperson(out result person) LANGUAGE  plpgsql
  as $$
  BEGIN
    result = ROW(1,2)::person;
  END
  $$;
select getperson();

Code

public class Person
    {
        public int id { get; set; }
        public int num { get; set; }
    }
    public class Employee : Person
    {
        public string job { get; set; }
    }
    public static Person GetComposite(Npgsql.NpgsqlConnection cnn)
    {

        using (var cmd = new Npgsql.NpgsqlCommand("select getperson()", cnn))
        {
            var par = new NpgsqlParameter("result",
                NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Composite)
            { Direction = ParameterDirection.Output };
            cmd.Parameters.Add(par);
            cmd.ExecuteNonQuery();
            return (Person)par.Value;
        }
    }
    private static NpgsqlConnection CreateOpenConnection()
    {
        var connectionString = "someconnection";
        var cnn = new Npgsql.NpgsqlConnection(connectionString);
        cnn.Open();
        return cnn;
    }
    [TestMethod]
    public void MapCompositeTests()
    {
        Npgsql.NpgsqlConnection.MapCompositeGlobally<Person>("person");
        using(var cnn = CreateOpenConnection())
        {
            var person = GetComposite(cnn);
            Assert.IsFalse(person is Employee);
            cnn.MapComposite<Employee>("person");
            var employee = (Employee)GetComposite(cnn);
        }
        using(var cnn2 = CreateOpenConnection())
        {
            var shouldBePerson= GetComposite(cnn2);
            Assert.IsFalse(shouldBePerson is Employee);
        }
    }

The issue

If connection pooling is enabled, shouldBePerson is materialized as type Employee instead of Person.
The current workaround is to call MapComposite before each NpgsqlCommand

Further technical details

Npgsql version: 3.2.2
PostgreSQL version: 9.6
Operating system: Windows 10

Activity

  1. changed the title [-]Custom TypeHandler and pluggable Composite mapping[/-] [+]Composite mapping leaks across connections[/+] on Jun 20, 2017
  2. self-assigned this
    on Jun 20, 2017
  3. roji commented on Jun 20, 2017

    @roji
    Member

    The ability to create your own type handlers is tracked by #1475, which is planned for version 3.3 - please go vote on that and contribute any thoughts.

    I'm leaving this open for your second issue, about mappings leaking from one connection to another.

  4. added this to the 3.2.5 milestone on Jun 20, 2017
  5. modified the milestones: , 3.2.5 on Jun 29, 2017
  6. roji commented on Jun 29, 2017

    @roji
    Member

    Yeah, unfortunately connection mappings aren't removed when the connection is closed (returned to the pool). I'm just now in the middle of redoing the whole Npgsql type mapping system to support #1475, and mappings are properly removed. As this is non-trivial to fix in 3.2 and as there's a workaround I'm going to leave this for 3.3.

  7. roji commented on Jul 3, 2017

    @roji
    Member

    Done in dev for 3.3, see test LocalMappingDontLeak

  8. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions