简体   繁体   English

postgres中的部分更新json字段

[英]partial update json field in postgres

In Postgres I have a table like this: 在Postgres中,我有一个像这样的表:

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)
)

And storehouse.storehouse is storing data like this: storehouse.storehouse正在存储这样的数据:

{
    "slots":[
        {
            "slot" : 1,
            "id" : 938
        },
        {
            "slot" : 2,
            "id" : 127
        },
    ]
}

The thing is, I want to update storehouse.storehouse.slots[2] , but I do not have an idea on how to do it. 问题是,我想更新storehouse.storehouse.slots[2] ,但是我不知道该怎么做。

I know how to alter the entire storehouse.storehouse field, but I am wondering since Postgres supports json type, it should support partial modify, otherwise that would be no difference between json type and text type. 我知道如何更改整个storehouse.storehouse字段,但是我想知道既然Postgres支持json类型,它应该支持部分修改,否则json类型和text类型之间没有区别。 (I know json type also has type validation which is differ to text ) (我知道json类型也具有与text不同的类型验证)

JSON indexing and partial updates are not currently supported. 当前不支持JSON索引编制和部分更新。 The JSON support in PostgreSQL 9.2 is rudimentary, limited to validating JSON and to converting rows and arrays to JSON. PostgreSQL 9.2中的JSON支持是基本的,仅限于验证JSON以及将行和数组转换为JSON。 Internally, json is indeed pretty much just text . 在内部, json实际上几乎只是text

There's ongoing work for enhancements like partial updates,indexing, etc. No matter what, though, PostgreSQL won't be able to avoid rewriting the whole row when part of a JSON value changes, because that's inherent to the MVCC model of concurrency. 目前正在进行诸如部分更新,索引编制等增强功能的工作。但是,无论如何,当JSON值的一部分发生更改时,PostgreSQL将无法避免重写整个行,因为这是MVCC并发模型所固有的。 The only way to make that possible would be to split JSON values out into multiple tuples in a side relation, like TOAST tables - something that's possible, but likely to perform poorly and that's very far from being considered at this point. 使其成为可能的唯一方法是将JSON值在侧关系中拆分为多个元组,例如TOAST表-这是可能的,但可能会表现不佳,目前还远远没有考虑。

As Chris Travers points out, you can use PL/V8 functions or functions in other languages with json support like Perl or Python to extract values, then create expression indexes on those functions. 就像Chris Travers指出的那样,您可以使用PL / V8函数或其他支持json的语言(如Perl或Python)来提取值,然后在这些函数上创建表达式索引。

Since PostgreSQL 9.5, there a function called jsonb_set which takes as input parameters: 从PostgreSQL 9.5开始,有一个名为jsonb_set的函数将其作为输入参数:

  • a JSON object JSON对象
  • an array indicating the path (keys and subkeys) 指示路径的数组(键和子键)
  • the new value to be stored (also a JSON object) 要存储的新值(也是JSON对象)

Example: 例:

 # SELECT jsonb_set('{"name": "James", "contact": {"phone": "01234 567890", "fax": "01987 543210"}}'::jsonb,
            '{contact,phone}',
            '"07900 112233"'::jsonb);
                                  jsonb_replace                                  
 --------------------------------------------------------------------------------
  {"name": "James", "contact": {"fax": "01987 543210", "phone": "07900 112233"}}
 (1 row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM