Berkeley DB迁移到PostgreSQL遇到的问题

由于bdb不支持多进程访问、热备(这是很致命的),所以考虑迁移到pgsql。
遇到的问题是:
bdb支持insert or replace into 表名 values(...)这种用法,pgsql不支持

查看资料找到了自定义函数的办法:
CREATE FUNCTION update_online(uid INT, online TEXT,vosId TEXT,vosCheckout TEXT)
RETURNS SETOF INT AS $$
  UPDATE tb_online SET online = $online WHERE vosId = $vosId RETURNING $uid
$$ LANGUAGE sql;

CREATE FUNCTION upsert_online(k INT, v TEXT)
RETURNS VOID AS $$
  INSERT INTO foo
  SELECT $1, $2
  WHERE NOT EXISTS (SELECT update_foo($1, $2))

$$ LANGUAGE sql;


postGreSQL邮件列表原文地址:

http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE

stackoverflow讨论地址:

http://stackoverflow.com/questions/1109061/insert-on-duplicate-update-postgresql


你可能感兴趣的:(function,PostgreSQL,insert,returning)