proposal: plpgsql - FOREACH t IN JSON ARRAY expr

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: proposal: plpgsql - FOREACH t IN JSON ARRAY expr
Date: 2026-01-24 07:10:36
Message-ID: CAFj8pRB858EmA2xqaWQMUs_bYmLNZY8eBS2zP+wrg0vyT97gDA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

last year I did a performance audit of some applications and I found a new
relatively common pattern - iteration over jsonb arrays. Because PL/pgSQL
doesn't support this iteration directly, they used some combinations of
jsonb_array_elements function and FOR IN SELECT.

The overhead of this can be relatively high, and I think we can support
FOREACH json(b) arrays without some high cost.

My idea is a support of syntax

FOREACH target IN JSON ARRAY expression LOOP .. END LOOP

target can be a scalar variable of any type - we can use cast rules used in
JSON_TABLE

What do you think about this proposal?

Regards

Pavel

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Álvaro Herrera 2026-01-24 09:30:54 Re: Converting README documentation to Markdown
Previous Message David Rowley 2026-01-24 06:36:08 Re: unnecessary executor overheads around seqscans