| From: | shihao zhong <zhong950419(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Cc: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
| Subject: | [PG19] jsonb_object_agg as a window function returns stale values |
| Date: | 2026-10-13 03:27:55 |
| Message-ID: | CAGRkXqTCfSu=aEUCvKDTGroEWOcyz3MCv4Xi8x1PZ1T1rEmG=Q@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
This is High 2 from my list in [1].
Since b61aa76e458, jsonb_object_agg as a window function can return
an old value for a key.
select g, jsonb_object_agg(k, v) over (order by g)
from (values (1,'a',1),(1,'a',2),(1,'a',3),(1,'b',5),(2,'b',6),
(3,'a',7)) t(g,k,v);
For g=2 master gives {"a": 3, "b": 5}. 18 gives {"a": 3, "b": 6}.
The second query in 0002 runs over 120 rows with 8 keys, and 97 rows
come back wrong.
In a window the final function runs once per row. Each run ends the
object on the shared state, which sorts the pairs array and removes
duplicate keys in place. A pair added after that can lose to an older
pair with the same key. The comment there assumes this step can run
many times on the same state, and that does not hold.
0001 makes the final function work on a copy of the pairs array. The
cost is one memcpy per final call. 0002 adds a test. jsonb_agg is not
affected. The patches apply to master and REL_19_STABLE.
The commit came from [2].
A smaller fix is to renumber the pairs after the dedup. That fixes
the queries above. But the same step drops null pairs from the state,
so this still differs from 18:
select g, jsonb_object_agg_unique_strict(k, v) over (order by g)
from (values (1, 'a', null::int), (2, 'a', 1)) t(g, k, v);
18 and 0001 report a duplicate key. master and the smaller fix return
{"a": 1} for g=2.
[1]
https://postgr.es/m/CAGRkXqSFdjqZP_kwRz7dv6LHX-CSrkDnynNXgntRsCEnQTKxmg@mail.gmail.com
[2] https://postgr.es/m/1060917.1753202222@sss.pgh.pa.us
Thanks,
Shihao
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0002-Add-test-for-jsonb_object_agg-as-a-window-functio.patch | application/octet-stream | 2.4 KB |
| v1-0001-Fix-jsonb_object_agg-used-as-a-window-function.patch | application/octet-stream | 2.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tianyi Gao | 2026-10-13 03:31:30 | Use a hash table for the regexp cache |
| Previous Message | Junwang Zhao | 2026-10-13 03:24:39 | Re: [PATCH] numeric: canonicalize wide digit accumulation |