uuidv7()

一个用于生成随机 v7 UUID 的函数

uuidv7() 是一个系统函数,用于生成一个随机的第 7 版 UUID,可以选择性地按指定间隔进行偏移。

uuidv7() 添加于 PostgreSQL 18

用法

uuidv7 ([ shift interval ]) → uuid

可以使用可选的 shift 参数来按给定间隔偏移内部时间戳。

变更历史

示例

uuidv7() 的基本用法示例

postgres=# SELECT uuidv7();
                uuidv7                
--------------------------------------
 0193c2e9-6df4-706e-aad0-6692a305f590
(1 row)

提供 shift 值以按给定间隔偏移内部时间戳

postgres=# SELECT uuidv7(), uuidv7(INTERVAL '1 day');
                uuidv7                |                uuidv7                
--------------------------------------+--------------------------------------
 0193c2ed-6bb8-70b7-8950-e3c4647047bd | 0193c813-c7b8-70e5-ae87-1d6ced208063
(1 row)

可以使用 uuid_extract_timestamp() 检索 UUID 的时间戳

postgres=# SELECT uuid_extract_timestamp( uuidv7() );
   uuid_extract_timestamp   
----------------------------
 2024-12-14 11:13:21.088+09
(1 row)

postgres=# SELECT uuid_extract_timestamp( uuidv7(INTERVAL '1 year') );
   uuid_extract_timestamp   
----------------------------
 2025-12-14 11:13:30.911+09
(1 row)

分类

系统函数, UUID

另请参阅

uuidv4(), uuid-ossp

反馈

在此处 提交关于“uuidv7()”的任何评论、建议或更正