jsonb

Postgresql: query on jsonb column - index doesn't make it quicker

左心房为你撑大大i 提交于 2021-02-09 08:27:55
问题 There is a table in Postgresql 9.6 , query on jsonb column is slow compared to a relational table, and adding a GIN index on it doesn't make it quicker. Table: -- create table create table dummy_jsonb ( id serial8, data jsonb, primary key (id) ); -- create index CREATE INDEX dummy_jsonb_data_index ON dummy_jsonb USING gin (data); -- CREATE INDEX dummy_jsonb_data_index ON dummy_jsonb USING gin (data jsonb_path_ops); Generate data: -- generate data, CREATE OR REPLACE FUNCTION dummy_jsonb_gen

postgresql jsonb case insensitive query

倖福魔咒の 提交于 2021-02-08 13:45:24
问题 I have a table like: CREATE TABLE cityData ( item character varying, data jsonb ); it contains values like ITEM DATA test1 [{"rank":"1", "city":"New York"},{"rank":"3", "city":"Sidney"}] test2 [{"rank":"2", "city":"NEW YORK"},{"rank":"4", "city":"New Delhi"}] I need to get number of distinct json objects where city is 'New York' I am using the following query SELECT * FROM cityData t WHERE ( data @> '[{"city":"New York"}]') and t.item ilike '%test%'; But this query outputs test1 row. I need

Postgres JSONb to XML with tag_name and tag_value

和自甴很熟 提交于 2021-02-08 07:23:49
问题 I am new to Postgres XML functions. I have a table as below: id (VARCHAR) | field1 (text) | attributes (jsonb) --------------+---------------+---------------------------------- 123 | a | {"age": "1", "place": "TX"} 456 | b | {"age": "2", "name": "abcdef"} 789 | | 098 | c | {"name": "gefd"} Would like to convert it to : <Company id="123" field="a"> <CompanyTag tagName="age" tagValue="1"/> <CompanyTag tagName="place" tagValue="TX"/> </Company> <Company id="456" field="b"> <CompanyTag tagName=

Indexing Postgresql JSONB arrays for element existence and unicity

与世无争的帅哥 提交于 2021-01-29 18:37:12
问题 I have a Postgresql 11.8 table named posts where I would like to define a column slugs of type JSONB, which would contain arrays of strings such as ["my-first-post", "another-slug-for-my-first-post"] . I can find a post having a specific slug using the ? existence operator: SELECT * FROM posts WHERE slugs ? 'some-slug' . Each post is expected to only have a handful of slugs but the amount of posts is expected to grow. Considering the above query where some-slug could be any string: How can I

Postgresql - Merge rows into jsonb object

别说谁变了你拦得住时间么 提交于 2021-01-29 15:34:43
问题 (This is a follow up question after Postgresql - Count freuency of array or jsonb object) In postgresql 12+, given following input rows: The expected output is: uid tag_freq 1 {'a':2, 'b':1, 'c':1} 2 {'a':1, 'b':2, 'c':2, 'e':1} ... Output column tag_freq is jsonb object, and it's merged result for a user. Is there any way to write such a query? 回答1: You can use jsonb_object_agg() for this: select uid, jsonb_object_agg(tag, count) as tag_freq from the_table group by uid; 来源: https:/

Aggregating distinct values from JSONB arrays combined with SQL group by

余生颓废 提交于 2021-01-29 15:13:11
问题 I am trying to aggregate distinct values from JSONB arrays in a SQL GROUP BY statement: One dataset has many cfiles and a cfile only ever has one dataset SELECT * FROM cfiles; id | dataset_id | property_values (jsonb) ----+------------+----------------------------------------------- 1 | 1 | {"Sample Names": ["SampA", "SampB", "SampC"]} 2 | 1 | {"Sample Names": ["SampA", "SampB", "SampD"]} 3 | 1 | {"Sample Names": ["SampE"]} 4 | 2 | {"Sample Names": ["SampA", "SampF"]} 5 | 2 | {"Sample Names":

UPDATE with jsonb_set() only affects one object in nested array

≯℡__Kan透↙ 提交于 2021-01-28 04:56:31
问题 Trying to update all elements of a nested array in a jsonb column, but only one element is updated. My query: update table_ set value_ = jsonb_set(value_,cte.json_path,cte.namevalue,false) FROM ( select vals2->'ao'->'sc'->'name' as namevalue, ('{iProps,'||index1-1||',value,rules,'||index2-1||',ao,sc}')::text[] as json_path from table_, jsonb_array_elements(value_->'iProps') with ordinality arr1(vals1,index1), jsonb_array_elements(vals1->'value'->'rules') with ordinality arr2(vals2,index2) )

How to modify field with jsonPath in PostgreSQL?

若如初见. 提交于 2021-01-28 02:54:46
问题 How to modify a field using jsonPath in PostgreSQL like SQL Server JSON_MODIFY (https://docs.microsoft.com/en-us/sql/t-sql/functions/json-modify-transact-sql?view=sql-server-ver15)? Thanks! 回答1: There are currently no ways to update JSON properties using JsonPath. The only way is with jsonb query jsonb_set 来源: https://stackoverflow.com/questions/62006261/how-to-modify-field-with-jsonpath-in-postgresql

PostgreSQL: Delete key/value pair from array with json objects

孤街醉人 提交于 2021-01-28 02:07:04
问题 I have a table: CREATE TABLE movies( id text, data jsonb ); INSERT INTO movies(id, data) VALUES ( '1', { "actors": [ { "name": "actor1", "email": "actor1@somemail.com" }, { "name": "actor2", "email": "actor2@somemail.com" } ] } ); What I want is to delete the email field (key + value) from each json object of the actors array. I've tried the following solution and although it does execute, it doesn't have any effect on the array at all: update movies set data = jsonb_set(data, '{actors}',

Conditional update with jsonb_set()

南笙酒味 提交于 2021-01-27 13:05:58
问题 I have a table in a Postgres 11.3 database with a jsonb column. Trying to update all objects inside a nested array name "iProps" . If the path {iProps -> value -> rules -> ao -> sc} is an object, then the path should be updated from an object to a string with the value {iProps -> value -> rules -> ao -> sc -> name} If the path {iProps -> value -> rules -> ao -> sc} is not present, then the object should be left unchanged. Test setup with query: Fiddle link Desired result: { "iProps": [ {