PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度進(jìn)位問(wèn)題解析
PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度進(jìn)位問(wèn)題
1. PostgreSQL簡(jiǎn)介
PostgreSQL 起源于 1986 年加州大學(xué)伯克利分校開(kāi)發(fā)的 POSTGRES 項(xiàng)目。它經(jīng)過(guò)多年的發(fā)展和演進(jìn),成為了一個(gè)功能強(qiáng)大、開(kāi)源的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)。
PostgreSQL 本身是開(kāi)源的,通常不存在嚴(yán)格意義上的商業(yè)版和社區(qū)版之分。
官網(wǎng)地址:https://www.postgresql.org/
在官網(wǎng)上,您可以獲取更詳細(xì)和準(zhǔn)確的信息,包括最新的特性、下載安裝指南、文檔教程等。
2. PostgreSQL特點(diǎn)
1.強(qiáng)大的功能:支持豐富的特性,如復(fù)雜查詢、事務(wù)處理、存儲(chǔ)過(guò)程、視圖等。
2.開(kāi)源和社區(qū)驅(qū)動(dòng):擁有活躍的開(kāi)源社區(qū),不斷進(jìn)行改進(jìn)和更新。
3.可擴(kuò)展性:易于擴(kuò)展功能和數(shù)據(jù)類型。
4.標(biāo)準(zhǔn)符合性:高度符合 SQL 標(biāo)準(zhǔn)。
5.安全性:提供多種安全機(jī)制,包括用戶認(rèn)證、權(quán)限管理等。
3. PostgreSQL優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
1.穩(wěn)定性高:經(jīng)過(guò)長(zhǎng)期的實(shí)踐和優(yōu)化,具有出色的穩(wěn)定性和可靠性。
2.數(shù)據(jù)完整性:確保數(shù)據(jù)的一致性和完整性。
3.跨平臺(tái)支持:可在多種操作系統(tǒng)上運(yùn)行。
4.豐富的文檔和支持:有詳細(xì)的官方文檔和活躍的社區(qū)支持。
缺點(diǎn):
1.配置相對(duì)復(fù)雜:對(duì)于新手來(lái)說(shuō),初始配置可能具有一定的難度。
2.資源消耗:在處理大型數(shù)據(jù)集時(shí),可能需要較多的系統(tǒng)資源。
4. PostgreSQL數(shù)據(jù)類型
包括數(shù)值類型(如整數(shù)、浮點(diǎn)數(shù))、字符串類型(固定長(zhǎng)度和可變長(zhǎng)度)、日期和時(shí)間類型、布爾類型、數(shù)組類型、枚舉類型、幾何類型等。
數(shù)據(jù)類型 | 說(shuō)明 | 精度 | 特點(diǎn) | 案例 |
---|---|---|---|---|
smallint | 小范圍整數(shù) | 2 字節(jié) | 占用存儲(chǔ)空間小,適用于數(shù)據(jù)范圍較小的整數(shù) | smallint: 100 |
integer (或 int ) | 常規(guī)整數(shù) | 4 字節(jié) | 常見(jiàn)的整數(shù)類型,能滿足大多數(shù)常規(guī)整數(shù)需求 | integer: 2000 |
bigint | 大范圍整數(shù) | 8 字節(jié) | 用于存儲(chǔ)非常大的整數(shù)數(shù)值 | bigint: 9000000000 |
decimal (或 numeric ) | 用戶指定精度和小數(shù)位數(shù)的精確數(shù)值 | 取決于指定的精度和小數(shù)位數(shù) | 精度和小數(shù)位數(shù)可自定義,適用于對(duì)精度要求高的數(shù)值計(jì)算 | decimal(5, 2): 123.45 |
real | 單精度浮點(diǎn)數(shù) | 4 字節(jié) | 大約 6 位十進(jìn)制精度 | 存儲(chǔ)精度要求不高的浮點(diǎn)數(shù) |
double precision | 雙精度浮點(diǎn)數(shù) | 8 字節(jié) | 大約 15 位十進(jìn)制精度 | 適用于高精度的浮點(diǎn)數(shù)計(jì)算 |
char(n) | 固定長(zhǎng)度的字符字符串,不足長(zhǎng)度用空格填充 | n 個(gè)字符 | 長(zhǎng)度固定,存儲(chǔ)和讀取效率較高 | char(10): 'hello ' |
varchar(n) | 可變長(zhǎng)度的字符字符串,最大長(zhǎng)度為 n | 最大 n 個(gè)字符 | 節(jié)省存儲(chǔ)空間,適用于長(zhǎng)度不固定的字符串 | varchar(20): 'hello world' |
text | 可變長(zhǎng)度的字符字符串,無(wú)長(zhǎng)度限制 | 無(wú)限制 | 可存儲(chǔ)大量文本數(shù)據(jù) | text: 'This is a long text.' |
date | 日期 | 格式為 YYYY-MM-DD | 只存儲(chǔ)日期信息 | date: '2024-08-08' |
time | 時(shí)間,不包含日期 | 格式為 HH:MM:SS | 只存儲(chǔ)時(shí)間信息 | time: '12:30:00' |
timestamp | 日期和時(shí)間 | 格式為 YYYY-MM-DD HH:MM:SS ,支持到微秒級(jí)別(6 位小數(shù)) | 同時(shí)包含日期和時(shí)間信息 | timestamp: '2024-08-08 12:30:00.123456' |
boolean | 布爾值 | 真或假 | 用于表示邏輯值 | boolean: true |
array | 數(shù)組 | 取決于元素類型和數(shù)組長(zhǎng)度 | 可以存儲(chǔ)相同數(shù)據(jù)類型的元素集合 | integer[]: '{1, 2, 3}' |
json | JSON 數(shù)據(jù) | 無(wú)固定精度 | 方便存儲(chǔ)和處理 JSON 格式的數(shù)據(jù) | json: '{"key": "value"}' |
uuid | 通用唯一標(biāo)識(shí)符 | 128 位 | 用于生成唯一標(biāo)識(shí)符 | uuid: 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' |
5. PostgreSQL注意事項(xiàng)
PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度存在進(jìn)位問(wèn)題,先說(shuō)結(jié)論,在測(cè)試驗(yàn)證。
PostgreSQL只能存儲(chǔ)最多6位的小數(shù)部分。因此,當(dāng)嘗試存儲(chǔ)超過(guò)6位的分?jǐn)?shù)秒時(shí),會(huì)發(fā)生以下情況之一:
如果額外的數(shù)字導(dǎo)致整個(gè)分?jǐn)?shù)秒部分超過(guò) 999999 微秒,則整個(gè)時(shí)間值會(huì)進(jìn)位到下一秒,剩余的部分會(huì)被截?cái)嗷蛘咚纳嵛迦氲?6 位小數(shù)。
在這種情況下,2024-08-07 16:19:23.999999999 會(huì)被處理為 2024-08-07 16:19:24.000000。
如果你使用的是某種客戶端或者編程語(yǔ)言接口來(lái)處理這個(gè)時(shí)間值,在插入之前它可能會(huì)自動(dòng)進(jìn)行四舍五入或截?cái)唷?/p>
比如,
截取前三位數(shù)據(jù):小于 2024-08-07 16:19:23.999999500
進(jìn)位數(shù)據(jù):[2024-08-07 16:19:23.999999500 - 2024-08-07 16:19:23.999999999]
通過(guò)指定精度數(shù)據(jù)庫(kù)中仍然會(huì)保存6位,但是指定精度3時(shí)后三位始終為0,精度6則保存6位。
SELECT * FROM crm.t_wx_user_1 WHERE 1=1; SELECT user_id, to_char(last_login_time, 'YYYY-MM-DD HH24:MI:SS.US'), to_char(create_time, 'YYYY-MM-DD HH24:MI:SS.US') FROM crm.t_wx_user_1 WHERE 1=1; -- last_login_time timestamp,-- 》timestamp(3) ALTER TABLE crm.t_wx_user_1 ALTER COLUMN last_login_time TYPE TIMESTAMP(3) WITHOUT TIME ZONE; ALTER TABLE crm.t_wx_user_1 ALTER COLUMN create_time TYPE TIMESTAMP(6) WITHOUT TIME ZONE;
注意問(wèn)題:
PostgreSQL 的 timestamp 類型默認(rèn)只支持到微秒級(jí)別(6位小數(shù)),為什么select出來(lái)的數(shù)據(jù)中展示三位2024-08-07 16:19:23.999?
PostgreSQL 的 timestamp 類型確實(shí)支持到微秒級(jí)別(6位小數(shù))。然而,默認(rèn)情況下,當(dāng)您從數(shù)據(jù)庫(kù)中查詢 timestamp 值時(shí),PostgreSQL 會(huì)將結(jié)果顯示為毫秒級(jí)別(3位小數(shù))以提高可讀性。這是默認(rèn)的行為,但是您可以更改輸出的精度。案例如下。
--PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度進(jìn)位問(wèn)題 --# 1.PostgreSQL數(shù)據(jù)庫(kù)數(shù)據(jù)類型 -- 數(shù)據(jù)庫(kù)版本,PostgreSQL 15.3 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit SELECT version(); -- 創(chuàng)建數(shù)據(jù)庫(kù) -- crm.t_wx_user_1 definition -- Drop table -- DROP TABLE crm.t_wx_user_1; CREATE TABLE crm.t_wx_user_1 ( user_id int8 NOT NULL, union_id varchar(50) NULL, open_id varchar(50) NULL, last_login_time timestamp NULL, privacy_agreement_version int8 NOT NULL DEFAULT 0, create_time timestamp NOT NULL, update_time timestamp NOT NULL DEFAULT '1970-01-01 00:00:00'::timestamp without time zone, delete_flag int2 NOT NULL DEFAULT 0, delete_time timestamp NOT NULL DEFAULT '1970-01-01 00:00:00'::timestamp without time zone, first_login_time timestamp NULL, created_by int8 NOT NULL DEFAULT '-1'::integer, updated_by int8 NOT NULL DEFAULT '-1'::integer, inst_id int8 NULL, CONSTRAINT t_wx_user_pkey_1 PRIMARY KEY (user_id) ); CREATE INDEX idx_t_wx_user_open_id_1 ON crm.t_wx_user_1 USING btree (open_id); CREATE INDEX idx_t_wx_user_union_id_1 ON crm.t_wx_user_1 USING btree (union_id); CREATE UNIQUE INDEX uniq_wx_userid_unionid_openid_1 ON crm.t_wx_user_1 USING btree (user_id, union_id, open_id); -- 測(cè)試驗(yàn)證數(shù)據(jù)庫(kù) select * from crm.t_wx_user_1 order by user_id ; -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,支持類型 2024-08-07 16:19:23.142,正常插入數(shù)據(jù)庫(kù) INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230000, '1', '1', '2024-08-07 16:19:23.142', 0, '2024-07-12 16:52:39.674', '2024-08-07 16:19:23.142', 0, '1970-01-01 00:00:00.000', '2024-07-12 16:52:39.674', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,6位數(shù)據(jù)類型 2024-08-07 16:19:23.123456,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù) INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230001, '1', '1', '2024-08-07 16:19:23.123456', 0, '2024-07-12 16:52:39.123456', '2024-08-07 16:19:23.123456', 0, '1970-01-01 00:00:00.123456', '2024-07-12 16:52:39.123456', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,3個(gè)9驗(yàn)證 2024-08-07 16:19:23.999,正常插入數(shù)據(jù)庫(kù) INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230002, '1', '1', '2024-08-07 16:19:23.999', 0, '2024-07-12 16:52:39.999', '2024-08-07 16:19:23.999', 0, '1970-01-01 00:00:00.999', '2024-07-12 16:52:39.999', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,4個(gè)9驗(yàn)證 2024-08-07 16:19:23.9999,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù) INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230003, '1', '1', '2024-08-07 16:19:23.9999', 0, '2024-07-12 16:52:39.9999', '2024-08-07 16:19:23.9999', 0, '1970-01-01 00:00:00.9999', '2024-07-12 16:52:39.9999', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,6個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù) INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230004, '1', '1', '2024-08-07 16:19:23.999999', 0, '2024-07-12 16:52:39.999999', '2024-08-07 16:19:23.999999', 0, '1970-01-01 00:00:00.999999', '2024-07-12 16:52:39.999999', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999999,會(huì)進(jìn)位,存到數(shù)據(jù)庫(kù)為:2024-08-07 16:19:24.000 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230005, '1', '1', '2024-08-07 16:19:23.999999999', 0, '2024-07-12 16:52:39.999999999', '2024-08-07 16:19:23.999999999', 0, '1970-01-01 00:00:00.999999999', '2024-07-12 16:52:39.999999999', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,查詢操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999999,會(huì)進(jìn)位,把2024-08-07 16:19:24.000的也查詢出來(lái)。 select * from crm.t_wx_user_1 where last_login_time <= '2024-08-07 16:19:23.999999999'; -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)值驗(yàn)證 2024-08-07 16:19:23.123456789,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù) INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230006, '1', '1', '2024-08-07 16:19:23.123456789', 0, '2024-07-12 16:52:39.123456789', '2024-08-07 16:19:23.123456789', 0, '1970-01-01 00:00:00.123456789', '2024-07-12 16:52:39.123456789', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,查詢操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999999,會(huì)進(jìn)位,把2024-08-07 16:19:24.000的也查詢出來(lái)。 select * from crm.t_wx_user_1 where last_login_time <= '2024-08-07 16:19:23.999999999'; -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999998,會(huì)進(jìn)位,存到數(shù)據(jù)庫(kù)為:2024-08-07 16:19:24.000 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230007, '1', '1', '2024-08-07 16:19:23.999999998', 0, '2024-07-12 16:52:39.999999998', '2024-08-07 16:19:23.999999998', 0, '1970-01-01 00:00:00.999999998', '2024-07-12 16:52:39.999999998', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999001,截?cái)嗲叭徊迦霐?shù)據(jù)庫(kù) INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230008, '1', '1', '2024-08-07 16:19:23.999999001', 0, '2024-07-12 16:52:39.999999001', '2024-08-07 16:19:23.999999001', 0, '1970-01-01 00:00:00.999999001', '2024-07-12 16:52:39.999999001', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999991,進(jìn)位 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230009, '1', '1', '2024-08-07 16:19:23.999999991', 0, '2024-07-12 16:52:39.999999991', '2024-08-07 16:19:23.999999991', 0, '1970-01-01 00:00:00.999999991', '2024-07-12 16:52:39.999999991', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999990,進(jìn)位 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230010, '1', '1', '2024-08-07 16:19:23.999999990', 0, '2024-07-12 16:52:39.999999990', '2024-08-07 16:19:23.999999990', 0, '1970-01-01 00:00:00.999999990', '2024-07-12 16:52:39.999999990', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999900,進(jìn)位 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230011, '1', '1', '2024-08-07 16:19:23.999999900', 0, '2024-07-12 16:52:39.999999900', '2024-08-07 16:19:23.999999900', 0, '1970-01-01 00:00:00.999999900', '2024-07-12 16:52:39.999999900', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999800,進(jìn)位 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230012, '1', '1', '2024-08-07 16:19:23.999999800', 0, '2024-07-12 16:52:39.999999800', '2024-08-07 16:19:23.999999800', 0, '1970-01-01 00:00:00.999999800', '2024-07-12 16:52:39.999999800', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999000,截取前三位 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230013, '1', '1', '2024-08-07 16:19:23.999999000', 0, '2024-07-12 16:52:39.999999000', '2024-08-07 16:19:23.999999000', 0, '1970-01-01 00:00:00.999999000', '2024-07-12 16:52:39.999999000', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999500,進(jìn)位 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230014, '1', '1', '2024-08-07 16:19:23.999999500', 0, '2024-07-12 16:52:39.999999500', '2024-08-07 16:19:23.999999500', 0, '1970-01-01 00:00:00.999999500', '2024-07-12 16:52:39.999999000', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,新增操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999100,進(jìn)位 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230015, '1', '1', '2024-08-07 16:19:23.999999100', 0, '2024-07-12 16:52:39.999999100', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1); INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230016, '1', '1', '2024-08-07 16:19:23.999999200', 0, '2024-07-12 16:52:39.999999200', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1); INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230017, '1', '1', '2024-08-07 16:19:23.999999300', 0, '2024-07-12 16:52:39.999999300', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1); INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230018, '1', '1', '2024-08-07 16:19:23.999999400', 0, '2024-07-12 16:52:39.999999400', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1); -- 再次驗(yàn)證 500 進(jìn)位 INSERT INTO crm.t_wx_user_1 (user_id, union_id, open_id, last_login_time, privacy_agreement_version, create_time, update_time, delete_flag, delete_time, first_login_time, created_by, updated_by, inst_id) VALUES(1230019, '1', '1', '2024-08-07 16:19:23.999999500', 0, '2024-07-12 16:52:39.999999500', '2024-08-07 16:19:23.999999100', 0, '1970-01-01 00:00:00.999999100', '2024-07-12 16:52:39.999999100', -1, -1, 1); -- postgres數(shù)據(jù)庫(kù) timestamp類型,查詢操作,9個(gè)9驗(yàn)證 2024-08-07 16:19:23.999999999,會(huì)進(jìn)位,把2024-08-07 16:19:24.000的也查詢出來(lái)。 select * from crm.t_wx_user_1 ; where last_login_time <= '2024-08-07 16:19:23.999999999'; --截取前三位數(shù)據(jù):小于 2024-08-07 16:19:23.999999500 --進(jìn)位數(shù)據(jù):[2024-08-07 16:19:23.999999500 - 2024-08-07 16:19:23.999999999] 結(jié)論 PostgreSQL只能存儲(chǔ)最多6位的小數(shù)部分。因此,當(dāng)嘗試存儲(chǔ)超過(guò)6位的分?jǐn)?shù)秒時(shí),會(huì)發(fā)生以下情況之一: 如果額外的數(shù)字導(dǎo)致整個(gè)分?jǐn)?shù)秒部分超過(guò) 999999 微秒,則整個(gè)時(shí)間值會(huì)進(jìn)位到下一秒,剩余的部分會(huì)被截?cái)嗷蛘咚纳嵛迦氲?6 位小數(shù)。 在這種情況下,2024-08-07 16:19:23.999999999 會(huì)被處理為 2024-08-07 16:19:24.000000。 如果你使用的是某種客戶端或者編程語(yǔ)言接口來(lái)處理這個(gè)時(shí)間值,在插入之前它可能會(huì)自動(dòng)進(jìn)行四舍五入或截?cái)唷? 比如: 截取前三位數(shù)據(jù):小于 2024-08-07 16:19:23.999999500 進(jìn)位數(shù)據(jù):[2024-08-07 16:19:23.999999500 - 2024-08-07 16:19:23.999999999] -- 創(chuàng)建表 CREATE TABLE crm.wx_users ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL ); -- 插入數(shù)據(jù) INSERT INTO crm.wx_users (name, created_at) VALUES ('John Doe', '2024-08-07 16:19:23.999999'); INSERT INTO crm.wx_users (name, created_at) VALUES ('Jerry', '2024-08-07 16:19:23.999996'); -- 查詢數(shù)據(jù) SELECT * FROM crm.wx_users; PostgreSQL 的 timestamp 類型默認(rèn)只支持到微秒級(jí)別(6位小數(shù)),為什么select出來(lái)的數(shù)據(jù)中展示三位2024-08-07 16:19:23.999 PostgreSQL 的 timestamp 類型確實(shí)支持到微秒級(jí)別(6位小數(shù))。然而,默認(rèn)情況下,當(dāng)您從數(shù)據(jù)庫(kù)中查詢 timestamp 值時(shí),PostgreSQL 會(huì)將結(jié)果顯示為毫秒級(jí)別(3位小數(shù))以提高可讀性。這是默認(rèn)的行為,但是您可以更改輸出的精度。 -- 使用 to_char 函數(shù)來(lái)顯示完整的微秒 SELECT id, name, to_char(created_at, 'YYYY-MM-DD HH24:MI:SS.US') AS created_at FROM crm.wx_users;
到此這篇關(guān)于PostgreSQL數(shù)據(jù)庫(kù)timestamp數(shù)據(jù)類型精度進(jìn)位問(wèn)題的文章就介紹到這了,更多相關(guān)PostgreSQL timestamp數(shù)據(jù)類型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
postgresql無(wú)序uuid性能測(cè)試及對(duì)數(shù)據(jù)庫(kù)的影響
小編最近在做一個(gè)超大表的性能測(cè)試,在過(guò)程中發(fā)現(xiàn)無(wú)序uuid做主鍵對(duì)表插入性能有些影響,糾結(jié)該怎么處理這一問(wèn)題呢?接下來(lái)小編給大家分享postgresql無(wú)序uuid性能測(cè)試的相關(guān)知識(shí)幫助大家學(xué)習(xí),需要的彭參考下吧2021-06-06PostgreSQL數(shù)據(jù)庫(kù)中匿名塊的寫(xiě)法實(shí)例
這篇文章主要介紹了PostgreSQL數(shù)據(jù)庫(kù)中匿名塊的寫(xiě)法實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01PostgreSQL數(shù)據(jù)庫(kù)視圖及子查詢使用操作
這篇文章主要為大家介紹了PostgreSQL數(shù)據(jù)庫(kù)視圖及子查詢的使用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04postgresql查詢自動(dòng)將大寫(xiě)的名稱轉(zhuǎn)換為小寫(xiě)的案例
這篇文章主要介紹了postgresql查詢自動(dòng)將大寫(xiě)的名稱轉(zhuǎn)換為小寫(xiě)的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01