这是已经有效的方法,但它使用的是循环:
(我正在nickname, slug
为本地表中的每一行更新远程表上的字段)
DECLARE
row_ record;
rdbname_ varchar;
....
/* select from local */
FOR row_ IN SELECT rdbname, objectvalue1 as keyhash, cvalue1 as slug, cvalue2 as nickname
FROM bme_tag
where rdbname = rdbname_
and tagtype = 'NAME'
and wkseq = 0
LOOP
/* update remote */
PERFORM dblink_exec('sysdb',
format(
'update bme_usergroup
set nickname = %L
,slug = %L
where rdbname = %L
and wkseq = 0
and keyhash = %L'
, row_.nickname, row_.slug, row_.rdbname, row_.keyhash)
);
END LOOP;
现在,我想做的是做大量UPDATE (remote) FROM (local)
PERFORM dblink_exec('sysdb',
'update (remote)bme_usergroup
set nickname = bme_tag.cvalue2, slug=bme_tag.cvalue1
from (local).bme_tag s
where bme_usergroup.rdbname = %L
and bme_usergroup.wkseq = 0
and bme_usergroup.keyhash = s.keyhash
and bme_usergroup.rdbname = s.rdbname
)
通过查看各种解决方案( postgresql: INSERT INTO ... (SELECT * ...) ),我已经走到了这一步SELECT
,DELETE
并且我知道如何在INSERT/SELECT
. 我也可以使用绑定变量进行直接更新。但是怎么样UPDATE FROM
?
如果不可能,我应该查看 PostgresFOREIGN TABLE
或类似的东西吗?
本地和远程数据库都在同一个 Postgres 服务器上。如果重要的话,还有一点信息是,任何一个数据库都可以单独删除和恢复,我更喜欢一个轻量级的解决方案,每次都不需要大量配置来重新建立通信。