![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
My suspicion is that Postgres calculates the local offset from UTC only once per session, during session initialization. |
#3
| |||
| |||
|
|
Randall Nortman <postgreslists (AT) wonderclown (DOT) com> writes: My suspicion is that Postgres calculates the local offset from UTC only once per session, during session initialization. This is demonstrably not so. We might be able to figure out what actually went wrong, if you would show us the exact commands your application issued. |
#4
| |||
| |||
|
|
I can't reproduce the error without messing up my clock, but from my logs, here's the text of the SQL sent to the server: insert into sensor_readings_numeric (sensor_id, reading_ts, reading, min, max) values (3, '2004-10-31 01:00:00', 0.540602, 0.519071837254, 0.551811824539) And this came back: ERROR: duplicate key violates unique constraint "sensor_readings_numeric_pkey" |
#5
| |||
| |||
|
|
Randall Nortman <postgreslists (AT) wonderclown (DOT) com> writes: I can't reproduce the error without messing up my clock, but from my logs, here's the text of the SQL sent to the server: insert into sensor_readings_numeric (sensor_id, reading_ts, reading, min, max) values (3, '2004-10-31 01:00:00', 0.540602, 0.519071837254, 0.551811824539) And this came back: ERROR: duplicate key violates unique constraint "sensor_readings_numeric_pkey" Hmm ... and you were generating that timestamp string how exactly? I suspect that you actually sent the same timestamp string twice, one hour apart, in which case I'd have to call this an application bug. You really need to include the timezone specification in order to have an unambiguous timestamp string. It doesn't have to be UTC as you previously suggested, but it does have to be labeled with the intended zone. |
#6
| |||
| |||
|
|
Ah, I see now. PostgreSQL is behaving a bit differently than I expected. The timestamp string above is ambiguous in the timezone US/Eastern -- it could be EST or EDT. I was expecting PostgreSQL to resolve this ambiguity based on the current time when the SQL statement is processed |
|
But it appears that PostgreSQL always assumes EDT in this case, regardless of the current time? |
|
In my code, in fact, timestamps are recorded as seconds since the epoch, in UTC, so it makes little sense to convert to local time anyway. Right now, psycopg (the python module I'm using for postgres access) is generating the timestamp string for me (via psycopg.TimestampFromTicks()). I just need to figure out how to get it to generate the string with an explicit time zone, which I'm sure is possible. And if not, I'll just generate the string myself. |
#7
| ||||
| ||||
|
|
Randall Nortman <postgreslists (AT) wonderclown (DOT) com> writes: Ah, I see now. PostgreSQL is behaving a bit differently than I expected. The timestamp string above is ambiguous in the timezone US/Eastern -- it could be EST or EDT. I was expecting PostgreSQL to resolve this ambiguity based on the current time when the SQL statement is processed I think this would be a very bad thing for it to do. It might seem to make sense for a timestamp representing "now", but as soon as you consider a timestamp that isn't "now" it becomes a sure way to shoot yourself in the foot. |
|
But it appears that PostgreSQL always assumes EDT in this case, regardless of the current time? Actually, the intended and documented behavior is that it should interpret an ambiguous time as local standard time (e.g., EST not EDT). That seems to be broken at the moment :-(, which is odd because I'm quite certain I tested it last time we touched the relevant subroutine. |
|
Before I go off and try to fix it, does anyone have any objection to the rule "interpret an ambiguous time as local standard time"? This would normally mean picking the later of the two possible interpretations, which might be the wrong choice for some applications. (I notice that HPUX's cron is documented to choose the earlier interpretation in comparable situations.) |

|
Actually, your best bet is to forgo the conversion altogether. The recommended way to get from a Unix epoch value to a timestamp is 'epoch'::timestamptz + NNNNN * '1 second'::interval |
#8
| |||
| |||
|
|
On Sun, Oct 31, 2004 at 02:44:51PM -0500, Tom Lane wrote: Actually, the intended and documented behavior is that it should interpret an ambiguous time as local standard time (e.g., EST not EDT). I'm finding it hard to see how either way is likely to generate good results in *any* application, much less in a majority of applications. So in a way, perhaps the most correct thing to do would be to spit out an error if the timestamp is ambiguous. Any application which deals with timestamps in anything other than UTC should really be handling the disambiguation itself, because the server can't possibly know what the application means to do. Not generating an error is likely to allow an application bug to go unnoticed, especially if the database does not have a unique constraint on timestamps (as mine does). |
#9
| |||
| |||
|
|
That line of argument leads directly to the conclusion that we shouldn't allow timezone-less input strings at all, since it's unlikely that anyone would code their app to append a timezone spec only during the two hours a year when it actually matters. And wouldn't you rather have had the problem pointed out immediately on testing the app, rather than waiting till 1AM on a fall Sunday morning to find out it's broken? |
|
For human users, there would be some value in acting this way, since it would serve to remind them of the issue only when it actually matters. Comments anyone? |
|
Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a tool for doing 5% of the work and then sitting around waiting for someone else to do the other 95% so you can sue them. |
#10
| |||
| |||
|
|
On Sun, Oct 31, 2004 at 04:14:52PM -0500, Tom Lane wrote: That line of argument leads directly to the conclusion that we shouldn't allow timezone-less input strings at all, since it's unlikely that anyone would code their app to append a timezone spec only during the two hours a year when it actually matters. For human users, there would be some value in acting this way, since it would serve to remind them of the issue only when it actually matters. Comments anyone? Except that means your program will work all the time except for one or two hours per year where it breaks. Chances are your testing is not going to trip it... |
![]() |
| Thread Tools | |
| Display Modes | |
| |