By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
select version();
version |
---|
PostgreSQL 9.6.24 on x86_64-pc-linux-musl, compiled by gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219, 64-bit |
SELECT 1
create table challenge (id int, lines jsonb)
CREATE TABLE
insert into challenge values(1,'[
{
"line": 1,
"blocs": [
{
"size": 100,
"name": "abc"
},
{
"size": 100,
"name": "def"
},
{
"size": 100,
"name": "ghi"
}
]
},
{
"line": 2,
"blocs": [
{
"size": 100,
"name": "xyz"
}
]
}
]')
INSERT 0 1
with cte as (
select id,
jsonb_agg(jsonb_set(val1,'{blocs}',
(select jsonb_agg(arr2 || '{"type": "regular"}') from jsonb_array_elements(arr1.val1->'blocs') arr2
))) from challenge,
jsonb_array_elements(lines) arr1(val1)
group by 1
)
update challenge
set lines=(cte.jsonb_agg)
from cte
where challenge.id=cte.id
UPDATE 1
select jsonb_pretty(lines) from challenge
jsonb_pretty |
---|
[ { "line": 1, "blocs": [ { "name": "abc", "size": 100, "type": "regular" }, { "name": "def", "size": 100, "type": "regular" }, { "name": "ghi", "size": 100, "type": "regular" } ] }, { "line": 2, "blocs": [ { "name": "xyz", "size": 100, "type": "regular" } ] } ] |
SELECT 1