古いペアを引き出して、新しいペア(名前が変更されたキーを使用)を元に戻す必要があるのは正しいと思います。
あなたはワンライナーでそれを行うことができます:
(h - from_key) || hstore(to_key, h -> from_key)
ここで、h hstore、from_key 変更するキーであり、to_key 変更したいものです。これにより、必要な変更を加えた新しいhstoreが返されますが、from_keyであると想定されます。 hにあります; from_keyの場合 hにありません 次に、to_key -> NULLになります。 あなたのhstoreで。すべての正気の人のように、漂遊NULLを望まない場合は、存在チェックを簡単に追加できるように、ロジックを単純な関数でラップします。このようなもの:
create or replace function
change_hstore_key(h hstore, from_key text, to_key text) returns hstore as $$
begin
if h ? from_key then
return (h - from_key) || hstore(to_key, h -> from_key);
end if;
return h;
end
$$ language plpgsql;
次に、これらの両方を言って、期待される結果を得ることができます:
=> select change_hstore_key('a=>1,b=>2,c=>3'::hstore, 'b', 'pancakes');
change_hstore_key
------------------------------
"pancakes"=>"2", "a"=>"1", "c"=>"3"
=> select change_hstore_key('a=>1,b=>2,c=>3'::hstore, 'pancakes', 'X');
change_hstore_key
------------------------------
"a"=>"1", "b"=>"2", "c"=>"3"