Repository navigation
Closed as not planned
Closed as not planned
Copy link
Description
Steps to reproduce
The code below creates a table and populates it with data. It doesn't read inserted row intentionally, just look at the table content after the code is done in pgAdmin and you will see the problem with your eyes. I can add a Select query on demand.
using System;
using System.Globalization;
class Program
{
private static string MinValueString;
private static string MinPlusValueString;
static void Main(string[] args)
{
//this is doesn't really matter, just to have consistent constants format
var dateTimeFormat = (DateTimeFormatInfo) CultureInfo.InvariantCulture.DateTimeFormat.Clone();
var dateTimeFormatString = @"\'yyyyMMdd HHmmss.ffffff\'";
var min = DateTime.MinValue; // 0001-01-01 00:00:00.000000
var minPlus = min.AddMilliseconds(1); // 0001-01-01 00:00:00.001000
//values as string constants, to use exact same constants within CREATE and INSERT statements.
MinValueString = min.ToString(dateTimeFormatString, dateTimeFormat);
MinPlusValueString = minPlus.ToString(dateTimeFormatString, dateTimeFormat);
//all the tested versions, edit them to yours and choose one below
var pg90cs = "HOST=DB;PORT=5490;DATABASE=dotest;USER ID=dotest;PASSWORD=dotest";
var pg91cs = "HOST=DB;PORT=5491;DATABASE=dotest;USER ID=dotest;PASSWORD=dotest";
var pg92cs = "HOST=DB;PORT=5492;DATABASE=dotest;USER ID=dotest;PASSWORD=dotest";
var pg96cs = "HOST=DB2;PORT=5496;DATABASE=dotest;USER ID=dotest;PASSWORD=dotest";
var pg10_0cs = "HOST=DB2;PORT=54100;DATABASE=dotest;USER ID=dotest;PASSWORD=dotest";
var pg11_0cs = "HOST=DB2;PORT=54110;DATABASE=dotest;USER ID=dotest;PASSWORD=dotest";
var currentCs = pg11_0cs;
using (var connection = new Npgsql.NpgsqlConnection(currentCs)) {
connection.Open();
using (var dropTableCommand = connection.CreateCommand()) {
dropTableCommand.CommandText = GetDropTableScript();
dropTableCommand.ExecuteNonQuery();
}
using (var createTableCommand = connection.CreateCommand()) {
createTableCommand.CommandText = GetCreateTableScript();
createTableCommand.ExecuteNonQuery();
}
using(var insertRowCommand = connection.CreateCommand()) {
insertRowCommand.CommandText = GetInsertScript();
var insertMinValueParam = insertRowCommand.CreateParameter();
insertMinValueParam.ParameterName = "p_min";
insertMinValueParam.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Timestamp;
insertMinValueParam.NpgsqlValue = min;
insertRowCommand.Parameters.Add(insertMinValueParam);
var insertMinValuePlusMsParam = insertRowCommand.CreateParameter();
insertMinValuePlusMsParam.ParameterName = "p_minplus";
insertMinValuePlusMsParam.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Timestamp;
insertMinValuePlusMsParam.NpgsqlValue = minPlus;
insertRowCommand.Parameters.Add(insertMinValuePlusMsParam);
_ = insertRowCommand.ExecuteScalar();
}
}
}
private static string GetCreateTableScript() =>
$"CREATE TABLE \"public\".\"TestTable\" (" +
"\"Id\" bigint NOT NULL," +
"\"ValueDefault\" timestamp DEFAULT " + MinValueString + "::timestamp Not NULL," +
"\"ValueMinConst\" timestamp," +
"\"ValueMinPlusConst\" timestamp," +
"\"ValueMinParam\" timestamp," +
"\"ValueMinPlusParam\" timestamp," +
"CONSTRAINT \"PK_TestTable\" PRIMARY KEY(\"Id\")) WITHOUT OIDS;";
private static string GetInsertScript() =>
"INSERT INTO \"public\".\"TestTable\"(\"Id\", \"ValueDefault\", \"ValueMinConst\", \"ValueMinPlusConst\", \"ValueMinParam\", \"ValueMinPlusParam\")" +
"VALUES(1, DEFAULT, " + MinValueString + "::timestamp, " + MinPlusValueString + "::timestamp, (@p_min)::timestamp, (@p_minplus)::timestamp)";
private static string GetDropTableScript() => "DROP TABLE IF EXISTS \"public\".\"TestTable\"";
}The issue
For some reason DateTime.MinValue is replaced by -Infinity when using parameter although the value fits perfectly to native SQL type (timestamp). The exact same value but in form of constant works just fine. The problem didn't appear with Nuget packages v4.1.11 and v5.0.13
Further technical details
Npgsql package version: 6.0.4 (also checked 6.0.0, same behavior)
PostgreSQL version: 9.0, 9.1, 9.2, 9.6, 10.0, 11.0
Operating system: Windows 10 Enterprise (Version 21H2) OS build 19044.1645
Reacted by Eske Rahn
Metadata
Metadata
Assignees
Labels
No labels
Activity
vonzshik commentedon May 19, 2022
Hello.
Npgsql 6.0 converts
DateTime.Min/Maxtoinfinityby default. You can return the previous behavior by setting app context switch at the start of the app:alex-kulakov commentedon May 19, 2022
What about filtering stuff in where clause? I'm actually cannot find rows with default value set for the column with parameter. So they actually broke working feature and now they give me a hook to workaround it? What about not breaking things and make new things optional?
alex-kulakov commentedon May 19, 2022
I see it this way. It is up to user what to do with data, the client library should give fastest way to do the job. If I, user, want to use infinity I'm aware where and when I use infinity as a sign of min value, and if I'm not, i don't want some library to forcefully decide what I want from it. People don't write apps from scratch, they update existing ones and you guys waste time of developing teams of the projects that rely on your library.
roji commentedon May 19, 2022
@alex-kulakov the problem is that there's no way for you to convey infinity/-infinity via .NET DateTime - it's simply not possible.
Now, from our experience, DateTime.MaxValue and DateTime.MinValue are almost always used as "a timestamp bigger/smaller than all the others", and not actual, specific timestamps; almost nobody wants to record the timestamp of an actual event that will happen at e.g. the last second of the year 9999. As such, the corresponding values in PG - preserving that meaning - are infinity and -infinity.
I get that this is a breaking change, and those are never nice; that's why we documented this in the release notes, and provided the appcontext switch above for you to switch to the older behavior. But I'd personally recommend replacing the values in the database to infinity/-infinity.
alex-kulakov commentedon May 20, 2022
Yes, I agree that it is impossible. But this statement makes substitutions even more ridiculous. If there is not way to read it back from query results why use it for the values that perfectly fit to .NET
DateTime? We're talking about .NET anyway, so within this framework it takes some effort to write Infinity value to database because everything works in terms ofDateTimes,DateTimeOffsets andTimeSpans, so the chances that people will actually want to write nativeInfinityvalue are very small, and chances of using DateTime.MinValue are definitely bigger.Here's the example, there is a table with ton of data and there is need to add a column of
timestampto it, where are two ways of doing that (maybe more but these two are what comes to mind at first) - make it nullable or set DEFAULT value to it. In case of DEFAULT value it makes sense to use a value that is readable and stands out, like min value, which will never be used in real life. But after this you are not able to filter them out.Speaking of "impossible to read DateTime", ORMs most likely write values using parameters and fetch entities form database after that, and most likely they will have some DateTime columns, the chances that they will use Postgre-specific Infinity are extremely low, they are not interested in such because they are interested in cross-RDBMS capabilities. From their standpoint all DateTime values are valid values so they will allow their users (and apparently your users too if they use Postgre) to write all of them if RDBMS supports whole range (Postgre does!) and filter by any of these values. Some users might want to use min/max values for some reason, it is not my business why, and not ORM developers', and even not your business why some specific user wanted to do so. But you replaced damn data of the user without any conscious action of this user! And you can't even transform this damn Infinity to DateTime value (as you said earlier) because it is a data loss compression - any value can be substituted with Infinity but there is no way to figure it out back. See my point why this is does not make sense? By the way, I haven't seen projects that use pure native SQL queries for a long time, and using Infinity will probably require some. Even in .Net Framework 1.1 and .Net Framework 2.0 people didn't like working with native SQL.
As documentation says
-infinityis everything "earlier than all other time stamps" andInfinityis everything "later than all other time stamps". Their meaning is more likeNULL'cause they have no special values of timestamp, likeNULL.Guys, please, stop being like Postgre and think "Oh! who needs this?". Think twice, apparently someone will need it but you will figure this out after public release when this "someone" will come here and say that your "fancy new feature" broke his app and cost him money. Postgre did it with zone part of
timestamptzby not giving extra bytes to store actual time zone (MS SQL Server or Oracle database allow to store it). No, they decided "No one needs this! Lets use data loss compression". No one thought that if user choose thetzversion then the user needs to store the damn time zone of date. I actually hate them for this.alex-kulakov commentedon May 20, 2022
Also this tendency to use AppContext for features keys is kind of junk at least for me as I develop a mid-layer software between your library and end-user apps/libs. To guarantee proper work of my library I need to be confident with settings I get and set, but I cannot be confident in this feature because it can be changed in any time by anyone and I have to check and reset this feature to what I need before every query otherwise I cannot guarantee this. It would be way better to have some keys in connection string that you could check and remove from it before actual connection to PostgreSQL, It would be more reliable.
roji commentedon May 22, 2022
First, while I understand you're frustrated by this change, I'd highly advise avoiding words such as "ridiculous", "damn" and "junk" when communicating. They definitely don't help the project maintainer get motivated for engaging with you or fixing your problems. This is an community OSS project which you're using for free, please keep that in mind show some minimum respect when posting.
I don't understand what you're saying here. Yes, it's common to use DateTime.MinValue to indicate "lack of value" on the .NET side (in the same way as -infinity is sometimes used on the PG side), allowing one to use comparisons (greater than/less than). You can easily filter these columns out by - once again - sending a parameter with DateTime.MinValue, which Npgsql will send as -infinity. It's not clear what problem you're describing is.
What is this assumption based, have you gone over various major databases and checked this? In any case, we're not discussing an ORM here, but rather Npgsql, a lower-level database driver. The driver's job is to map .NET values to PG in the most correct and natural way possible, and if some other ORM in some other language happens to not support some PG feature, well, that shouldn't mean we should avoid it. Even if you're right about ORMs generally not supporting infinity values, cross-ORM compatibility isn't necessarily something that affects everyone, and there's the opt-out for those who are affected.
Not really - I still don't understand what you mean. There's no data loss - there's simply a mapping between .NET DateTime.Min/MaxValue to PG -infinity/infinity. Before Npgsql 6.0, infinity/-infinity couldn't be read or written, while in Npgsql 6.0 (by default), midnight at 0001 and the last second of 9999 cannot. There's no data loss here, just a different mapping of values.
I see projects that use raw SQL all the time - both with EF Core and without. But again, I don't see how raw SQL is relevant to this discussion, and why you'd need to use raw SQL specifically with infinity values (as Npgsql does all the reading/writing for you).
I really don't see any connection between this and the current discussion. To my understand, the PG timestamptz type is how the SQL standard defines it, regardless of whether you think it's good or not. I'll also add that SQL Server's datetimeoffset does not store a timezone, but rather an offset - a completely different thing which notably isn't aware of daylight savings. This makes it IMHO quite useless for many (if not most) usages. But again, this is all unrelated.
This is not how AppContext switches work in general: it is standard practice for them to be cached by libraries (and the .NET runtime), meaning that changing an AppContext switch back and forth in the middle of a running program generally isn't a supported scenario. Npgsql definitely does this caching, so another library can do so as well.
I'll summarize by repeating what I wrote above. DateTime.MinValue really isn't used as a value, since it's extremely rare to want to record events that occured at midnight on January 1st, 0001. The value is used to have a value smaller than all other values, which makes its PG equivalent -infinity. The ability to express -infinity is important, since the PG database may conceivably contain actual timestamps outside the range of .NET DateTime. So I feel that the actual behavior here makes a lot of sense - though there's still the problem of the breaking change itself, which is always a painful thing. At the same time, the changeis both clearly documented in the 6.0 release notes, and a compat switch is available for reverting to the previous behavior. That really should address concerns about upgrading and dealing with the breaking change.
alex-kulakov commentedon May 23, 2022
Pardon me for the strong words, but when something worked for years (at least 9+ years) and then it became broken and somebody tells me that this "broken" is new "working" I became kind of frustrated, indeed.
For the rest of your answer, clearly we have opposite points of view on this issue and I just can shut my mouth for the rest of this project's life because it will do whatever frustrated things it wants. "Live long and keep breaking" 🖖
roji commentedon May 23, 2022
Putting aside the specifics of the change, you main gripe here indeed seems to be that a breaking change was done - any breaking change. Breaking changes do need to happen from time to time - that's part of modern software development. You not only have the clear documentation in the release notes, but also an opt-out; we don't take breaking changes lightly, but we really have done what we could to make it easier for users.
At some point, if you want things to always just keep on working and never break, consider not upgradnig.
EskeRahn commentedon Aug 9, 2024
We have been unaware of this purposely introduced bug that are here called a feature, and now have to run through a shit-load of tables to correct for this bug....
Yes I know that the hack is meant to circumvent a .net limitation.
if you wanted to reserve a value for -infinity, you could have introduced a npgsql-value
DateTime?e.g.NpgsqlHackValueForDateTimeMinusInfinitydefault null, and (as @alex-kulakov pointed out) the app could then set this hack-value to whatever were suitable for the app (e.g. '0102-03-04'), without breaking anything.Add: You could have made a similar
NpgsqlHackValueForDateTimeMapBCdatesthat when reading from PG mapped anything below the DateTime supported range to this value. e.g. DateTime.MinValueYunusAkbalik commentedon Jan 2, 2025
THXXXX