在阅读PostreSQL (13)文档时,我看到了这页面,它列出了不同日期时间类型的存储大小。
除其他外,它指出:
Name Storage Size Description Low Value High Value Resolution
---- ------------ ----------- --------- ---------- ----------
timestamp without time zone 8 bytes both date and time (no time zone) 4713 BC 294276 AD 1 microsecond
timestamp with time zone 8 bytes both date and time, with time zone 4713 BC 294276 AD 1 microsecond
time with time zone 12 bytes time of day (no date), with time zone 00:00:00+1559 24:00:00-1559 1 microsecond我能理解timestamp without time zone:在低值和高值之间大约有(4713 + 294276) * 365 * 24 * 60 * 60 * 1000000微秒。(不计入日历变化等)。这相当于大约2^63个值,这些值可以存储在8个字节中。
但是,timestamp with timezone具有相同的范围和分辨率,并且可以附加存储时区信息。如果我们已经对值使用了63位,那么只剩下一位,这不足以存储时区,所以内部存储必须以某种方式不同。
更奇怪的是,虽然时间戳只使用8个字节,但time with time zone需要12个字节,尽管它的分辨率相同,允许的值范围也要小得多。
因此,我的问题是: PostgreSQL中这些类型的内部存储是如何工作的?
发布于 2020-12-04 13:30:33
timestamp with time zone和timestamp without time zone都存储为一个8字节的整数,表示自2000年1月1日午夜以来的微秒。
不同之处在于,timestamp with time zone被解释为在UTC中,并根据显示上的timezone参数的当前设置进行转换。
定义在src/include/datatype/timestamp.h中
typedef int64 Timestamp;
typedef int64 TimestampTz;time without time zone是一个8字节的整数,表示午夜以来的微秒(4字节的整数是不够的)。请参阅src/include/utils/date.h
typedef int64 TimeADT;time with time zone有一个额外的4字节整数,表示时区偏移量(以秒为单位)。
请参阅src/include/utils/date.h
typedef struct
{
TimeADT time; /* all time units other than months and years */
int32 zone; /* numeric time zone, in seconds */
} TimeTzADT;遵循文献资料,避免time with time zone
time with time zone类型是由SQL标准定义的,但是定义显示的属性导致了可疑的用途。
https://stackoverflow.com/questions/65143816
复制相似问题