部分更新json在postgres中提交

bxs*_*shi 4 postgresql json postgresql-9.2

storehouse.storehouse我有这样的数据库:

CREATE TABLE storehouse
(
  user_id bigint NOT NULL,
  capacity integer NOT NULL,
  storehouse json NOT NULL,
  last_modified timestamp without time zone NOT NULL,
  CONSTRAINT storehouse_pkey PRIMARY KEY (user_id)
)
Run Code Online (Sandbox Code Playgroud)

storehouse.storehouse.slots[2]存储这样的数据:

{
    "slots":[
        {
            "slot" : 1,
            "id" : 938
        },
        {
            "slot" : 2,
            "id" : 127
        },
    ]
}
Run Code Online (Sandbox Code Playgroud)

问题是,我想更新storehouse.storehouse,但我不知道如何做到这一点,我知道如何改变整个json文件,但我想知道自json支持text类型,它应该支持部分修改,否则那将是没有json类型和text类型之间的区别.(我知道storehouse.storehouse类型也有不同的类型验证storehouse.storehouse.slots[2])

Cra*_*ger 7

目前不支持JSON索引和部分更新.PostgreSQL 9.2中的JSON支持是基本的,仅限于验证JSON以及将行和数组转换为JSON.在内部,json确实非常公正text.

目前正在进行部分更新,索引等增强功能.无论如何,当JSON值的一部分发生变化时,PostgreSQL将无法避免重写整行,因为这是MVCC并发模型所固有的.实现这一目标的唯一方法是将JSON值拆分为多边形关系中的多个元组,如TOAST表 - 这是可能的,但可能性能很差,而且目前还远未被考虑.

正如Chris Travers指出的那样,您可以使用其他语言的PL/V8函数或函数,使用像Perl或Python这样的json支持来提取值,然后在这些函数上创建表达式索引.