ITPub博客

首页 > 数据库 > PostgreSQL > PostgreSQL DBA(110) - pgAdmin(Don't do this: Don't use timestamp without tz)

PostgreSQL DBA(110) - pgAdmin(Don't do this: Don't use timestamp without tz)

翻译 PostgreSQL 作者:husthxd 时间:2019-10-18 12:20:44 0 删除 编辑

no zuo no die系列,来自于pg的wiki。
这一节的内容是:不要使用timestamp (without time zone) 。
理由是:

timestamptz records a single moment in time. Despite what the name says it doesn’t store a timestamp, just a point in time described as the number of microseconds since January 1st, 2000 in UTC. You can insert values in any timezone and it’ll store the point in time that value describes. By default it will display times in your current timezone, but you can use at time zone to display it in other time zones.
Because it stores a point in time it will do the right thing with arithmetic involving timestamps entered in different timezones - including between timestamps from the same location on different sides of a daylight savings time change.
timestamp (also known as timestamp without time zone) doesn’t do any of that, it just stores a date and time you give it. You can think of it being a picture of a calendar and a clock rather than a point in time. Without additional information - the timezone - you don’t know what time it records. Because of that, arithmetic between timestamps from different locations or between timestamps from summer and winter may give the wrong answer.
So if what you want to store is a point in time, rather than a picture of a clock, use timestamptz.

原因是timestamp (without time zone)没有记录时区信息,只是当时的时间点(可以理解为当时的时钟快照)。

创建数据表并插入数据

[local]:5432 pg12@testdb=# drop table if exists t_timestamp;
NOTICE:  table "t_timestamp" does not exist, skipping
DROP TABLE
Time: 1.594 ms
[local]:5432 pg12@testdb=# create table t_timestamp(id int,time1 timestamptz,time2 timestamp without time zone);
CREATE TABLE
Time: 4.389 ms
[local]:5432 pg12@testdb=# truncate table t_timestamp;
);
insert into t_timestamp values(4,date_trunc('day',CURRENT_TIMESTAMP),date_trunc('day',CURRENT_TIMESTAMP));
select * from t_timestamp;
TRUNCATE TABLE
Time: 2.228 ms
[local]:5432 pg12@testdb=# insert into t_timestamp values(1,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);
INSERT 0 1
Time: 1.355 ms
[local]:5432 pg12@testdb=# insert into t_timestamp values(2,now(),now());
INSERT 0 1
Time: 1.012 ms
[local]:5432 pg12@testdb=# insert into t_timestamp values(3,date_trunc('second',CURRENT_TIMESTAMP),date_trunc('second',CURRENT_TIMESTAMP));
INSERT 0 1
Time: 1.809 ms
[local]:5432 pg12@testdb=# insert into t_timestamp values(4,date_trunc('day',CURRENT_TIMESTAMP),date_trunc('day',CURRENT_TIMESTAMP));
INSERT 0 1
Time: 1.052 ms
[local]:5432 pg12@testdb=#

查询数据

[local]:5432 pg12@testdb=# select * from t_timestamp;
 id |             time1             |           time2            
----+-------------------------------+----------------------------
  1 | 2019-10-18 12:16:25.333097+08 | 2019-10-18 12:16:25.333097
  2 | 2019-10-18 12:16:25.335162+08 | 2019-10-18 12:16:25.335162
  3 | 2019-10-18 12:16:25+08        | 2019-10-18 12:16:25
  4 | 2019-10-18 00:00:00+08        | 2019-10-18 00:00:00
(4 rows)
Time: 0.794 ms

可以看到,timestamp (without time zone)没有时区信息,只是当时的时间点快照。

参考资料
Don’t Do This

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2660497/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
ITPUB数据库版块资深版主,对Oracle、PostgreSQL有深入研究。

注册时间:2007-12-28

  • 博文量
    1416
  • 访问量
    3855849