hstore key rename
Wednesday, June 11th, 2008hstore is a “Lazy DBA” extension for postgres, which enables storing hash tables in a table column.
When using hstore, sometimes you want to rename hash keys.
#
# create function renamekey( _hstore hstore, _oldname text, _newname text ) returns hstore as
$$ select delete($1, $2) || ($3 => ($1 -> $2)) $$ language sql immutable strict;
#
# \set old_codename '''dizzyflag'''
# \set new_codename '''is_trade'''
#
# UPDATE table_with_hstore
SET flags = renamekey(flags, :old_codename, :new_codename)
WHERE (flags -> :old_codename) is not null;
#