Pay*_*ian 3 postgresql uuid join aggregate-functions
我正在尝试从表中选择一行:
但是当我尝试强制执行第一个约束时遇到了问题。
以下是对整数按预期工作的所有内容:首先,创建如下所示的表:
t1
+----+---------+
| id | content |
+----+---------+
| 1 | a |
| 2 | b |
| 3 | c |
+----+---------+
Run Code Online (Sandbox Code Playgroud)
和
t2
+----+---------+
| id | t1_id |
+----+---------+
| 1 | 1 |
+----+---------+
Run Code Online (Sandbox Code Playgroud)
postgres=# create table t1(id int, content varchar(10), primary key (id));
CREATE TABLE
postgres=# create table t2(id int, t1_id int, foreign key (t1_id) references t1(id));
CREATE TABLE
postgres=# insert into t1 values (1, 'a');
INSERT 0 1
postgres=# insert into t1 values (2, 'b');
INSERT 0 1
postgres=# insert into t1 values (3, 'c');
INSERT 0 1
postgres=# insert into t2 values (1, 1);
INSERT 0 1
Run Code Online (Sandbox Code Playgroud)
现在,我想在选择行t1与最低id不显示为外键t2。我想选择在该行t1拥有id = 2和它按预期工作:
postgres=# select min(t1.id) from t1 left outer join t2 on t1.id = t2.t1_id where t2.id is null;
min
-----
2
(1 row)
Run Code Online (Sandbox Code Playgroud)
但是,当我对 UUID 进行相同尝试时,最终查询根本无法返回任何内容。请注意,我已经使用这篇文章中的答案来定义一种查找最小 UUID 的方法:
CREATE OR REPLACE FUNCTION min(uuid, uuid)
RETURNS uuid AS $$
BEGIN
IF $2 IS NULL OR $1 > $2 THEN
RETURN $2;
END IF;
RETURN $1;
END;
$$ LANGUAGE plpgsql;
create aggregate min(uuid) (
sfunc = min,
stype = uuid,
combinefunc = min,
parallel = safe,
sortop = operator (<)
);
Run Code Online (Sandbox Code Playgroud)
现在,像以前一样构建表,用于gen_random_uuid自动生成 UUID:
postgres=# drop table t2;
postgres=# drop table t1;
postgres=# create table t1(id uuid default gen_random_uuid(), content varchar(10), primary key (id));
postgres=# create table t2(id int, t1_id uuid, foreign key (t1_id) references t1(id));
postgres=# insert into t1(content) ('a');
postgres=# insert into t1(content) values ('a');
postgres=# insert into t1(content) values ('b');
postgres=# insert into t1(content) values ('c');
Run Code Online (Sandbox Code Playgroud)
我们已经成功地在t1. 添加条目到t2:
postgres=# select * from t1;
id | content
--------------------------------------+---------
b6148ae3-db56-4a4a-8d46-d5b4f04277ac | a
03abd324-8626-4fb1-9cb0-593373abf9ca | b
9f12b297-3f60-48a7-8282-e27c3aff1152 | c
(3 rows)
postgres=# insert into t2 values(1, '9f12b297-3f60-48a7-8282-e27c3aff1152');
Run Code Online (Sandbox Code Playgroud)
尝试从中选择t1没有出现在 中的最小 ID的行t2,请注意这会失败。
postgres=# select min(t1.id) from t1 left outer join t2 on t1.id = t2.t1_id where t2.id is null;
min
-----
(1 row)
Run Code Online (Sandbox Code Playgroud)
这里我们展示了我们可以选择两个未引用的条目,t1并且我们可以独立选择一个最小的 UUID:
postgres=# select t1.id from t1 left outer join t2 on t1.id = t2.t1_id where t2.id is null;
id
--------------------------------------
03abd324-8626-4fb1-9cb0-593373abf9ca
b6148ae3-db56-4a4a-8d46-d5b4f04277ac
(2 rows)
postgres=# select min(id) from t1;
min
--------------------------------------
03abd324-8626-4fb1-9cb0-593373abf9ca
(1 row)
Run Code Online (Sandbox Code Playgroud)
因此,当我尝试选择最小 UUID 并尝试执行左外连接时,会发生一些有趣的事情。
编辑:使用时存在同样的问题not exists:
postgres=# select min(id) from t1 where not exists (select t1_id from t2 where t2.t1_id = t1.id);
min
-----
(1 row)
Run Code Online (Sandbox Code Playgroud)
但是使用时没有出现问题not in:
postgres=# select min(id) from t1 where id not in (select t1_id from t2);
min
--------------------------------------
03abd324-8626-4fb1-9cb0-593373abf9ca
(1 row)
Run Code Online (Sandbox Code Playgroud)
找到了一个解决方案,结果发现这篇文章中比较 UUID 的函数不正确。这是我编写的函数,它的性能可能较低,它通过了之前失败的所有情况:
CREATE FUNCTION min_uuid(uuid, uuid)
RETURNS uuid AS $$
BEGIN
-- if they're both null, return null
IF $2 IS NULL AND $1 IS NULL THEN
RETURN NULL ;
END IF;
-- if just 1 is null, return the other
IF $2 IS NULL THEN
RETURN $1;
END IF ;
IF $1 IS NULL THEN
RETURN $2;
END IF;
-- neither are null, return the smaller one
IF $1 > $2 THEN
RETURN $2;
END IF;
RETURN $1;
END;
$$ LANGUAGE plpgsql;
create aggregate min(uuid) (
sfunc = min_uuid,
stype = uuid,
combinefunc = min_uuid,
parallel = safe,
sortop = operator (<)
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1169 次 |
| 最近记录: |