You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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...
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 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?
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 */))
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.
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.
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?
@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...
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...
@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?
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.
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.
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.
Activity
franciscojunior commentedon Dec 27, 2014
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.
roji commentedon Jan 6, 2015
See issue with parameter type inference in the enum PR.
Emill commentedon Jan 25, 2015
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...
roji commentedon Jan 25, 2015
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?
Emill commentedon Jan 25, 2015
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?
roji commentedon Jan 25, 2015
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:
roji commentedon Aug 15, 2015
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.
roji commentedon Aug 15, 2015
I'll be working on this in the coming days (just to prevent any double work @Emill :))
roji commentedon Aug 23, 2015
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...
lukas-shawford commentedon Nov 2, 2015
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:
In C#, I have an enum for
DistanceUomand a CLR type forDistance:I registered both the enum and the composite type:
However, when I attempt to query a table that has a
Distancecolumn, I get the error:If I rename the
Unitproperty in myDistanceclass to lowercaseunit, 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?
roji commentedon Nov 2, 2015
@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...
lukas-shawford commentedon Nov 3, 2015
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:
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...
roji commentedon Nov 3, 2015
@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?
lukas-shawford commentedon Nov 5, 2015
Just submitted as #855.
shift-evgeny commentedon Mar 1, 2016
This is an important feature for us - thanks very much for implementing it! When is 3.1 stable expected to be released?
shift-evgeny commentedon Mar 8, 2016
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?
roji commentedon Mar 22, 2016
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.
shift-evgeny commentedon Mar 22, 2016
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.
roji commentedon Mar 22, 2016
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.