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
See discussion in #347. As NodaTime is widely used it may be useful to support mapping date/time types to it directly. This could be achieved with an extra project/nuget that somehow replaces the date/time type handlers, etc.
If you are interested in this feature, please vote for it here.
Reacted by Dwayne Bent, Matthew Evans, Nikita Moshenskiy, JasonTheProgrammer, Karol Etrych, Peter Grman, dario-hd, Andrew Smith, Robert R., Mani Gandham and 2 more
@roji have you ever thought about making the type mapping system pluggable so that one could create custom mapping libraries to map any (standard or custom) PostgreSQL type to any (standard or custom) .Net type?
That way one could create a Npgsql.NodaTime (or NodaTime.Npgsql) Nuget package that one could configure to replace the default date/time type handlers as well as some other mapping library that maps some other types.
Factoring non standard mappings (like postgis mappings) out of core Npgsql could also reduce the codebase and the complexity of the Npgsql type mapping system (which seems overly complex to me anyways).
Yeah, this has been raised before: see conversation in #696.
Here are some points, you're welcome to respond!
I did have pluggability in mind when I originally designed the type handler API for 3.0.
However, opening up the type handler API would mean we wouldn't be able to make changes to it as easily. As you yourself said (and I agree), the API is indeed overly complex (there are some good historical reasons for this). 3.2 is (partially) moving towards a simplification of async/sync I/O in general (single codepath with async flag instead of the async rewriter + manual state machines), which will bring a big simplication to how type handlers work as well (when completed, probably for 3.3).
The main point here is that the type handler API isn't really stable yet, so I don't think it's a good moment to open it up. Once we're happy with how the API looks and don't plan to change it anymore, it may make sense to do so.
There simply aren't that many PostgreSQL (extension) types out there, and the list doesn't seem to be evolving very quickly. In other words, there's hardly been any user demand for unimplemented type handlers up to now.
There's nothing really "nonstandard" about PostGIS - it's just a bunch of types that have a standard (chunking) type handler. I don't really see what we'd gain by moving them out of core Npgsql...
If someone wants to write support for some missing type, they can definitely do it today as a pull request to the core, this way everyone benefits from it. The only disadvantage is that you have to wait for a release (or use your private fork until the release)
Am currently working on this feature and it's going well - I have an experimental plugin that makes Npgsql read/write Nodatime 2.0 types.
I'd like to make sure I get the type mapping right between the PostgreSQL date/time types and the Nodatime types. I'd really appreciate feedback from anybody with experience in this domain and in Nodatime in particular (especially from you @jskeet!). The goal is to end up with a simple, sensible mapping table that doesn't resemble the current BCL mapping and forces users to do the right thing.
PostgreSQL type
Nodatime type
Comments
timestamp
LocalDateTime
Simple timestamp in PostgreSQL, no timezone/offset. There's a concept of infinity/negative infinity which could be represented by wrapping LocalDateTime in a LocalDateTimeWithFiniteness or something.
timestamptz
ZonedDateTime
Despite the name, this fields holds only a timestamp, no timezone. But PostgreSQL treats it as UTC and performs timezone conversions based on the session-configured timezone when converting to text, etc. In binary transfer (which is what Npgsql uses) the timestamp is simply transferred as-is, and it seems correct to treat it as UTC. So this seems to make sense as a ZonedDateTime; when reading a timestamptz Npgsql would return either a ZonedDateTime with timezone UTC, or a ZonedDateTime with the session (or computer's) timezone.
date
LocalDate
A simple date, no timezone/offset. There's a concept of infinity/negative infinity which could be represented by wrapping LocalDate in a LocalDateWithFiniteness or something.
time
LocalTime
A simple time-of-day, no timezone/offset.
timetz
OffsetDateTime
This is a PostgreSQL type that actually stores a time and an offset (odd and generally not recommended for use by PostgreSQL). The only option seems to be to map to OffsetDateTime and keep the date component zero'd out.
interval
Period
The wire representation of this type is months, days and microseconds in the day, so it clearly corresponds to Period rather than Duration.
Without having looked at Postgres specifically, I'd expect both timestamp and timestamptz to be represented by Instant. Logically, a timestamp isn't in a particular calendar system but does represent an absolute instant in time. Given that a timestamp is usually "Event X happened at timestamp Y", I'd always suggest using an Instant for that - it's unambiguous about what information it does and doesn't include. Is there anything about Postgres that would make that hard to achieve?
@jskeet thanks for the super-quick review. I'm new to Nodatime, but I was under the impression an Instant is only an absolute, globally-agreed upon point in time (represented in number of ticks since epoch in UTC). Since there's no timezone information at all, is it possible/correct to actually treat timestamp as an Instant? It seems to make sense for timestamptz (in general it seems that Instant and ZonedDateTime are convertible with no additional information: everything is there in both), but isn't it problematic to simply assume that timestamp is UTC?
Note that it's possible to allow mapping both LocalDateTime and Instant, but one of them would still have to be the default.
The problem here is that I'm not an application - just a component translating types from one system to another. I have no idea as to the meaning or source of timestamps in a particular PostgreSQL database (i.e. the "what is the original data source" question in your docs). It seemed to make sense to me to provide LocalDateTime, which pretends to claim/guarantee the least, and then allow the user to say "it's OK, this is actually a UTC timestamp" via the Nodatime conversion methods etc.
Put it this way: if you're recording something as a timestamp in a database but you can't tell when it actually happened, that seems pretty broken to me. Using a local date/time as a timestamp definitely sounds like a bad idea as:
It can be ambiguous due to DST changes
It could change meaning if you change the time zone of the database
I definitely see the predicament, and it's effectively a deficiency in SQL/Postgres... but I'm not sure of the best way to handle it in terms of library support. I think I'd rather reward users who are behaving sensibly (i.e. storing UTC) with a logical type, at the cost of users who are genuinely storing a local time, rather than the other way round.
On the other hand, the Postgres docs do explicitly state that it's behaving badly (IMO):
When a timestamp with time zone value is output, it is always converted from UTC to the current timezone zone, and displayed as local time in that zone. To see the time in another time zone, either change timezone or use the AT TIME ZONE construct (see Section 9.9.3).
Conversions between timestamp without time zone and timestamp with time zone normally assume that the timestamp without time zone value should be taken or given as timezone local time. A different time zone can be specified for the conversion using AT TIME ZONE.
Urgh.
How feasible would it be to make this configurable?
Don't forget there's timestamp and timestamptz, with precisely this difference - PostgreSQL assumes that timestamptz values - which again are just timestamps, no timezone/offset - are in UTC. So again, it seems OK to do the same on my side and map timestamptz to Instant or ZonedDateTime (or allow both). The problem is with timestamp, for which it doesn't seem to make sense to make assumptions (that's what timestamptz is about) - at the very least the two should be mapped differently.
How feasible would it be to make this configurable?
It's definitely possible, although it would make the mapping even more complex/unpredictable when moving around applications. I guess we could have a knob or two when setting up the Nodatime plugin. But in that case what would you set the default behavior to?
Not sure about the default behaviour, to be honest. I tend to err on the side of aggressively pursuing logical goodness, which might ignore practicality...
I'd definitely make timestamptz map to Instant, and encourage applications to use that. It's not a ZonedDateTime because it doesn't really store a time zone, and shouldn't be viewed that way. It's logically just an instant in time, IMO.
One possible reason to put a local date/time in a timestamp column, is that you're storing its timezone/offset in another column. The lack of a single datatype containing both a timestamp and timezone pretty much forces you to do it. In that scenario you're pretty much forced to go for a two-column solution, and then mapping timestamp to LocalDateTime does make sense. That's at least a case where a local date/time in timestamp isn't the wrong thing to do.
But I admit that seems to be the rare case, and that 90% of the use cases are storing UTC in timestamp, so LocalDateTime would just make things more difficult to work with? So maybe defaulting to Instant, but allowing LocalDateTime is the right way to go.
This does mean that Npgsql treats timestamp and timestamptz in the exact same way, which may be a bit odd (maybe the LocalDateTime option shouldn't be available for timestamptz, where it makes no real sense).
Do we have any idea how common timestamp is vs timestamptz?
Maybe use LocalDateTime as the default for timestamp but have it configurable to Instant, but always use Instant for timestamptz, which I think is what you were suggesting... and then document a recommendation to use timestamptz.
Database date/time handling feels even more broken than language/library handling :(
Oh yeah, database date/time is really awful, and mapping that to language/library types is yet another level of awful - especially when those types happen to be .NET BCL.
To be completely honest, I (unscientifically) think a lot more people use timestamp than timestamptz. If you just want to store UTC timestamps, and also to work on them in UTC in your app - the UTC everywhere pattern - you're better off just using timestamp and thereby avoiding the conversions PostgreSQL does on timestamptz. That's my general way of working, and I'd guess that's what most people do.
So Instant for both timestamp and timestamptz seems to make sense, with the option to get LocalDateTime from timestamp (note that this isn't a config option - it's an ADO.NET API option).
So here's a final proposal:
PostgreSQL type
Nodatime type
Comments
timestamp
Instant, also allow LocalDateTime
Simple timestamp in PostgreSQL, no timezone/offset. Since the vast (and correct) usage is to store UTC timestamps in this type, it makes sense to map to Instant. For the few cases where the value actually represents a local date time (e.g. another column holds the timezone/offset), allow reading/writing LocalDateTime as well. In addition, there's a concept of infinity/negative infinity which could be represented by wrapping LocalDateTime in a LocalDateTimeWithFiniteness or something.
timestamptz
Instant, also allow ZonedDateTime
Despite the name, this fields holds only a timestamp, no timezone. But PostgreSQL treats it as UTC and performs timezone conversions based on the session-configured timezone when converting to text, etc. In binary transfer (which is what Npgsql uses) the timestamp is simply transferred as-is, and it seems correct to treat it as UTC. So this seems to make sense as a ZonedDateTime; when reading a timestamptz Npgsql would return either a ZonedDateTime with timezone UTC, or a ZonedDateTime with the session (or computer's) timezone.
date
LocalDate
A simple date, no timezone/offset. There's a concept of infinity/negative infinity which could be represented by wrapping LocalDate in a LocalDateWithFiniteness or something.
time
LocalTime
A simple time-of-day, no timezone/offset.
timetz
OffsetDateTime
This is a PostgreSQL type that actually stores a time and an offset (odd and generally not recommended for use by PostgreSQL). The only option seems to be to map to OffsetDateTime and keep the date component zero'd out.
interval
Period
The wire representation of this type is months, days and microseconds in the day, so it clearly corresponds to Period rather than Duration.
Note that there would be one difference between timestamp and timestamptz handling - if a ZonedDateTime is written as a timestamptz, Npgsql would perform conversion to UTC before sending it out.
An additional thing: when reading a ZonedDateTime, the type handler could return a non-UTC value based on the PostgreSQL session's time zone, mimicking timestamptz behavior. The problem here is for the type handler to be aware of the current session's timezone, things don't work that way at the moment.
Activity
Brar commentedon Jan 14, 2017
@roji have you ever thought about making the type mapping system pluggable so that one could create custom mapping libraries to map any (standard or custom) PostgreSQL type to any (standard or custom) .Net type?
That way one could create a Npgsql.NodaTime (or NodaTime.Npgsql) Nuget package that one could configure to replace the default date/time type handlers as well as some other mapping library that maps some other types.
Factoring non standard mappings (like postgis mappings) out of core Npgsql could also reduce the codebase and the complexity of the Npgsql type mapping system (which seems overly complex to me anyways).
What do you think?
roji commentedon Jan 17, 2017
Hi @Brar!
Yeah, this has been raised before: see conversation in #696.
Here are some points, you're welcome to respond!
roji commentedon May 23, 2017
After #1475 is implemented, provide an Npgsql.Nodatime package which overrides the date/time handlers.
roji commentedon Jun 29, 2017
Am currently working on this feature and it's going well - I have an experimental plugin that makes Npgsql read/write Nodatime 2.0 types.
I'd like to make sure I get the type mapping right between the PostgreSQL date/time types and the Nodatime types. I'd really appreciate feedback from anybody with experience in this domain and in Nodatime in particular (especially from you @jskeet!). The goal is to end up with a simple, sensible mapping table that doesn't resemble the current BCL mapping and forces users to do the right thing.
jskeet commentedon Jun 29, 2017
Without having looked at Postgres specifically, I'd expect both
timestampandtimestamptzto be represented byInstant. Logically, a timestamp isn't in a particular calendar system but does represent an absolute instant in time. Given that a timestamp is usually "Event X happened at timestamp Y", I'd always suggest using anInstantfor that - it's unambiguous about what information it does and doesn't include. Is there anything about Postgres that would make that hard to achieve?The rest look reasonable.
roji commentedon Jun 29, 2017
@jskeet thanks for the super-quick review. I'm new to Nodatime, but I was under the impression an
Instantis only an absolute, globally-agreed upon point in time (represented in number of ticks since epoch in UTC). Since there's no timezone information at all, is it possible/correct to actually treattimestampas an Instant? It seems to make sense fortimestamptz(in general it seems thatInstantandZonedDateTimeare convertible with no additional information: everything is there in both), but isn't it problematic to simply assume thattimestampis UTC?Note that it's possible to allow mapping both
LocalDateTimeandInstant, but one of them would still have to be the default.roji commentedon Jun 29, 2017
The problem here is that I'm not an application - just a component translating types from one system to another. I have no idea as to the meaning or source of timestamps in a particular PostgreSQL database (i.e. the "what is the original data source" question in your docs). It seemed to make sense to me to provide LocalDateTime, which pretends to claim/guarantee the least, and then allow the user to say "it's OK, this is actually a UTC timestamp" via the Nodatime conversion methods etc.
jskeet commentedon Jun 29, 2017
Put it this way: if you're recording something as a timestamp in a database but you can't tell when it actually happened, that seems pretty broken to me. Using a local date/time as a timestamp definitely sounds like a bad idea as:
I definitely see the predicament, and it's effectively a deficiency in SQL/Postgres... but I'm not sure of the best way to handle it in terms of library support. I think I'd rather reward users who are behaving sensibly (i.e. storing UTC) with a logical type, at the cost of users who are genuinely storing a local time, rather than the other way round.
On the other hand, the Postgres docs do explicitly state that it's behaving badly (IMO):
Urgh.
How feasible would it be to make this configurable?
roji commentedon Jun 29, 2017
Don't forget there's
timestampandtimestamptz, with precisely this difference - PostgreSQL assumes thattimestamptzvalues - which again are just timestamps, no timezone/offset - are in UTC. So again, it seems OK to do the same on my side and maptimestamptztoInstantorZonedDateTime(or allow both). The problem is withtimestamp, for which it doesn't seem to make sense to make assumptions (that's whattimestamptzis about) - at the very least the two should be mapped differently.It's definitely possible, although it would make the mapping even more complex/unpredictable when moving around applications. I guess we could have a knob or two when setting up the Nodatime plugin. But in that case what would you set the default behavior to?
jskeet commentedon Jun 29, 2017
Not sure about the default behaviour, to be honest. I tend to err on the side of aggressively pursuing logical goodness, which might ignore practicality...
I'd definitely make timestamptz map to Instant, and encourage applications to use that. It's not a ZonedDateTime because it doesn't really store a time zone, and shouldn't be viewed that way. It's logically just an instant in time, IMO.
roji commentedon Jun 29, 2017
Hmm, OK.
One possible reason to put a local date/time in a
timestampcolumn, is that you're storing its timezone/offset in another column. The lack of a single datatype containing both a timestamp and timezone pretty much forces you to do it. In that scenario you're pretty much forced to go for a two-column solution, and then mappingtimestampto LocalDateTime does make sense. That's at least a case where a local date/time intimestampisn't the wrong thing to do.But I admit that seems to be the rare case, and that 90% of the use cases are storing UTC in
timestamp, soLocalDateTimewould just make things more difficult to work with? So maybe defaulting toInstant, but allowingLocalDateTimeis the right way to go.This does mean that Npgsql treats
timestampandtimestamptzin the exact same way, which may be a bit odd (maybe theLocalDateTimeoption shouldn't be available fortimestamptz, where it makes no real sense).jskeet commentedon Jun 29, 2017
Do we have any idea how common timestamp is vs timestamptz?
Maybe use LocalDateTime as the default for timestamp but have it configurable to Instant, but always use Instant for timestamptz, which I think is what you were suggesting... and then document a recommendation to use timestamptz.
Database date/time handling feels even more broken than language/library handling :(
roji commentedon Jun 29, 2017
Oh yeah, database date/time is really awful, and mapping that to language/library types is yet another level of awful - especially when those types happen to be .NET BCL.
To be completely honest, I (unscientifically) think a lot more people use
timestampthantimestamptz. If you just want to store UTC timestamps, and also to work on them in UTC in your app - the UTC everywhere pattern - you're better off just usingtimestampand thereby avoiding the conversions PostgreSQL does ontimestamptz. That's my general way of working, and I'd guess that's what most people do.So
Instantfor bothtimestampandtimestamptzseems to make sense, with the option to get LocalDateTime fromtimestamp(note that this isn't a config option - it's an ADO.NET API option).So here's a final proposal:
roji commentedon Jul 2, 2017
Note that there would be one difference between
timestampandtimestamptzhandling - if a ZonedDateTime is written as atimestamptz, Npgsql would perform conversion to UTC before sending it out.An additional thing: when reading a ZonedDateTime, the type handler could return a non-UTC value based on the PostgreSQL session's time zone, mimicking
timestamptzbehavior. The problem here is for the type handler to be aware of the current session's timezone, things don't work that way at the moment.pawepaw commentedon Nov 27, 2018
@roji How to change default map to read timestamp as LocalDateTime? I got exception that it's unable to cast instant to localDateTime.
Message: System.InvalidCastException : Unable to cast object of type 'NodaTime.Instant' to type 'NodaTime.LocalDateTime'.roji commentedon Nov 27, 2018
@pawepaw can you please open a new issue and post some code to show exactly what you're trying to do?