手册说明
Add function gen_random_uuid() to generate version 4 UUIDs (Peter Eisentraut)
Previously UUID generation functions were only available via external modules uuid-ossp and pgcrypto.
PostgreSQL 13版本前不提供生成UUID
数据的内置函数,如果需要使用UUID
数据,可通过创建外部扩展 uuid-ossp或 pgcrypto生成 UUID
数据。
PostgreSQL 13 新增gen_random_uuid()
内置函数,可生成UUID
数据。
关于gen_random_uuid()函数
gen_random_uuid()
函数生成 version 4 UUID
(基于随机数生成,使用最广泛)。
一个示例,如下:
1 | postgres=# \df gen_random_uuid |
生成一张包含UUID数据的表
PostgreSQL 提供 UUID数据类型,在UUID
数据上可创建btree
索引,下面演示下。
创建测试表并生成UUID
数据,之后创建索引,如下:
1 | mydb=> CREATE TABLE uuid_01(id_int int4, id_uuid uuid,ctime timestamp(6) without time zone); |
查看UUID
数据和执行计划,如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14mydb=> SELECT *FROM uuid_01 LIMIT 1;
id_int | id_uuid | ctime
--------+--------------------------------------+----------------------------
1 | 3ba43653-e470-40d9-bfc0-55d33677ec22 | 2020-07-08 21:10:39.010639
(1 row)
mydb=> EXPLAIN ANALYZE SELECT *FROM uuid_01 WHERE id_uuid='3ba43653-e470-40d9-bfc0-55d33677ec22';
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------
Index Scan using idx_uuid_01_uuid on uuid_01 (cost=0.43..4.45 rows=1 width=28) (actual time=0.027..0.029 rows=1 loops=1)
Index Cond: (id_uuid = '3ba43653-e470-40d9-bfc0-55d33677ec22'::uuid)
Planning Time: 0.162 ms
Execution Time: 0.059 ms
(4 rows)