我在 Postgres 中使用 ltree 扩展来管理树的路径。我想确保名称字段对于树上的任何给定路径都是唯一的。我可以使用约束来完成此操作,还是必须将其构建到查询中?
demo table
------------------------------
| name (text) | path (ltree) |
------------------------------
| a | 1 |
------------------------------
| b | 1.2 |
------------------------------
| b | 1.3 |
------------------------------
| b | 1.2.4 | <-- this should fail on insert
------------------------------
如果我必须将它构建到查询中,如果我使用 READ COMMITTED,这是否可能存在竞争条件?
CREATE TABLE demo (
name text NOT null,
path ltree NOT null
);
CREATE INDEX path_gist_idx ON demo USING GIST (path);
INSERT INTO demo (name, path)
SELECT 'a', '1'
WHERE NOT EXISTS (
SELECT * FROM demo WHERE name = 'a' AND (path <@ '1' OR path @> '1')
);
INSERT INTO demo (name, path)
SELECT 'b', '1.2'
WHERE NOT EXISTS (
SELECT * FROM demo WHERE name = 'b' AND (path <@ '1.2' OR path @> '1.2')
);
INSERT INTO demo (name, path)
SELECT 'b', '1.3'
WHERE NOT EXISTS (
SELECT * FROM demo WHERE name = 'b' AND (path <@ '1.3' OR path @> '1.3')
);
INSERT INTO demo (name, path)
SELECT 'b', '1.2.4'
WHERE NOT EXISTS (
SELECT * FROM demo WHERE name = 'b' AND (path <@ '1.2.4' OR path @> '1.2.4')
);
SELECT * FROM demo;