Postgres ltree 模块,使用所有子相关路径更新节点的 id

Postgres ltree module, update id of a node with all the children related paths

我有以下简单的table:

create table nodes(
    id text primary key,
    path ltree
);

假设我将一些数据放入 table:

insert into nodes (id, path) values ('A', 'A');
insert into nodes (id, path) values ('B', 'A.B');
insert into nodes (id, path) values ('C', 'A.C');

所以树看起来像:

       A

  /          \

B             C

现在我想将 A 的 id 重命名为 X,这样树

       X

  /          \

B             C

而 table 看起来像

insert into nodes (id, path) values ('X', 'X');
insert into nodes (id, path) values ('B', 'X.B');
insert into nodes (id, path) values ('C', 'X.C');

有人可以给个提示吗 - 可以用一个查询来完成吗?

非常感谢任何帮助,谢谢

使用 http://patshaughnessy.net/2017/12/14/manipulating-trees-using-sql-and-the-postgres-ltree-extension 中的方法,特别是 "Moving branch" 部分:

UPDATE nodes SET id = 'X' WHERE id = 'A';
UPDATE nodes SET path = 'X' || subpath(path, 1) WHERE path <@ 'A';