Skip to content

Add support for PostgreSQL composite types #441

Closed

Description

@roji
No description provided.

Activity

  1. franciscojunior commented on Dec 27, 2014

    @franciscojunior
    Member

    User defined types would benefit a lot of the new type handling mechanism added by @roji . We could use this support to add an API for users to register new custom types inside Npgsql and be able to send and receive data using those types.

  2. roji commented on Jan 6, 2015

    @roji
    MemberAuthor

    See issue with parameter type inference in the enum PR.

  3. added this to the milestone on Jan 18, 2015
  4. Emill commented on Jan 25, 2015

    @Emill
    Contributor

    I was thinking of using the same mechanism as enums: the composite types are always loaded in at type handler registry initialization. But since tables can also composite types and we must load all attributes, that can be a quite large amount of data... Any idea how we can make this more optimized?

    We can of course ignore loading composite types that are simply tables, but I think we should support such as well...

  5. roji commented on Jan 25, 2015

    @roji
    MemberAuthor

    Hmm...

    One idea is to defer the loading of composite types (and maybe enums?) from the backend to the moment when they are registered... In other words, calling RegisterCompositeType sends a query to the database loading the necessary information from pg_type (and other necessary tables). If a composite type is globally registered, then at connection time the data is loaded along with other, regular fields. This way we never load info for special fields before they are registered by the user.

    Note that in my latest work on enums/TypeHandlerRegistry I made a change where we simply load all rows from pg_type, instead of constructing a comma-separated list of type names as before. The idea was that we'll soon be close to handling all types, but with this new composite type situation it might be a good idea to bring it back (and select only rows we want).

    What do you think?

  6. Emill commented on Jan 25, 2015

    @Emill
    Contributor

    What about this:
    Composite types registered globally are loaded at type handler registry construction, but connection-registered composite types are loaded (and requires a round-trip) on demand?

  7. roji commented on Jan 25, 2015

    @roji
    MemberAuthor

    What about this:
    Composite types registered globally are loaded at type handler registry construction, but connection-registered composite types are loaded (and requires a round-trip) on demand?

    Yep, that's what I meant... There's one small difficulty: the backend types as loaded from pg_type are now cached to prevent additional roundtrips on each connection. This means that a new global registration must invalidate all caches, otherwise new connections will reuse the cached types which don't contain the new composite type. It's no big deal, since typically global registration would happen before any connections are made.

    Regardless, it means that the construction-time query would look like:

    WHERE typtype IN ('b', 'r', 'e') OR (typtype = 'c' AND typname in (/* ALL GLOBALLY REGISTERED COMPOSITE TYPES */))
    
  8. modified the milestones: , on Mar 26, 2015
  9. roji commented on Aug 15, 2015

    @roji
    MemberAuthor

    For people who want to upgrade to Npgsql 3.0 but need composite type support, there's a workaround: you can treat composite types like other unknown to Npgsql: you can let Npgsql know that they're unknown, and it will have them transferred as text only - see this page.

  10. roji commented on Aug 15, 2015

    @roji
    MemberAuthor

    I'll be working on this in the coming days (just to prevent any double work @Emill :))

  11. assigned and unassigned on Aug 23, 2015
  12. roji commented on Aug 23, 2015

    @roji
    MemberAuthor

    Note: considered a fallback whereby if a composite type is fetched but it hasn't been mapped to a CLR type by the user, we return a dynamic type. Very nice and elegant, but it raises the question of when we load the column definitions for the unregistered composite type.

    We could in theory load the column definitions for all composite types at startup (right after we load all types). Then, the user can choose to map some of these types to static CLR types and leave the others unmapped; the last would be returned as dynamic. The problem with this idea is that in a default PostgreSQL database there seem to be around 200 composite types... It doesn't make sense to load that.

    It's hard to see another solution...

  13. lukas-shawford commented on Nov 2, 2015

    @lukas-shawford

    Great to see this feature is being added! Just tried it out and noticed a small issue.

    I have a composite type that contains an enum:

    CREATE TYPE DistanceUom AS ENUM
    (
        'Feet',
        'Meters'
    );
    
    
    CREATE TYPE Distance AS
    (
        Value numeric,
        Unit DistanceUom
    );
    

    In C#, I have an enum for DistanceUom and a CLR type for Distance :

    public enum DistanceUom { Feet, Meters }
    
    public class Distance
    {
        public decimal Value { get; set; }
    
        public DistanceUom Unit { get; set; }
    }
    

    I registered both the enum and the composite type:

    NpgsqlConnection.MapEnumGlobally<DistanceUom>();
    NpgsqlConnection.MapCompositeGlobally<Distance>();
    

    However, when I attempt to query a table that has a Distance column, I get the error:

    PostgreSQL composite type distance contains field unit which could not match any on CLR type Distance
    

    If I rename the Unit property in my Distance class to lowercase unit, then it works fine. However, that breaks C# capitalization conventions. I'd rather not have to use the [PgName] attribute for this.

    Looking through the code, I think if the comparison in CompositeHandler.cs#L224 was made case-insensitive, it ought to fix it. Is that something that could be done?

  14. roji commented on Nov 2, 2015

    @roji
    MemberAuthor

    @SergKr, both C# and PostgreSQL are completely case-sensitive, so I don't think it's a good idea to have Npgsql apply case-insensitive comparisons...

    I did toy around with allowing users to provide a name mapping convention, which would allow them to transform C# member names to and from PostgreSQL names, that would solve this issue. But I haven't gotten around to that yet...

  15. lukas-shawford commented on Nov 3, 2015

    @lukas-shawford

    I'm not very familiar with PostgreSQL or Npgsql yet, so maybe the solution I proposed isn't the right one. But still, it seems things are kind of inconsistent here... If I query for an enum directly, the casing doesn't seem to matter - it's able to map the PostgreSQL enum to the C# enum just fine, regardless of case:

    // works
    con.Query<DistanceUom>("SELECT (Distance).Unit FROM Distances;");
    
    // also works
    con.Query<DistanceUom>("SELECT (Distance).Unit AS \"uNIT\" FROM Distances;");
    

    But if that enum is contained inside a composite type, then it fails. It seems counter-intuitive that type mapping has different behavior depending on whether the type is contained inside another composite type...

  16. roji commented on Nov 3, 2015

    @roji
    MemberAuthor

    @SergKr, you're right that the behavior shouldn't be different if the type is contained or not. Can you please open a separate issue with the original description and I'll take a look?

  17. lukas-shawford commented on Nov 5, 2015

    @lukas-shawford

    Just submitted as #855.

  18. shift-evgeny commented on Mar 1, 2016

    @shift-evgeny

    This is an important feature for us - thanks very much for implementing it! When is 3.1 stable expected to be released?

  19. shift-evgeny commented on Mar 8, 2016

    @shift-evgeny

    Hmm, 33 issues still open for 3.1 (and 18 closed). I'm guessing it won't come out soon. Any chance of backporting this to 3.0?

  20. roji commented on Mar 22, 2016

    @roji
    MemberAuthor

    Sorry for the long inactivity, I've been working on a very intensive project and haven't had any time for Npgsql. The good news is that I'm back in 2 weeks and will prioritize the 3.1 release. There's still quite a bit of work to be done, and unfortunately the composite type support is a bit too complex to backport to 3.0...

    For simple composite types, don't forget you have the option of transferring it as text (see FAQ) and doing the parsing yourself - this is obviously not ideal but could be a temporary hack.

  21. shift-evgeny commented on Mar 22, 2016

    @shift-evgeny

    Thanks for getting back to me on this! I guess there's no ETA for 3.1 at this point then? We haven't started really using Postgres yet, so I'm trying to figure out if it's worth writing the parsing myself, as you say, or just waiting for 3.1.

  22. roji commented on Mar 22, 2016

    @roji
    MemberAuthor

    Yeah, unfortunately there's no ETA yet. If you really want to use composite and your primitive values are relatively simple (strings, numbers), parsing could be a relatively painless solution; when 3.1 is released you would replace it with Npgsql's true binary support for composites.

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

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions