proposal - idea - enhancing plpgsql FOREACH for JSON, jsonb and hstore

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: proposal - idea - enhancing plpgsql FOREACH for JSON, jsonb and hstore
Date: 2021-01-23 06:46:01
Message-ID: CAFj8pRCKngezkKEVbLS=oB8GWaq1DcS-XR7sTUs7K9UQ=vdSFg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi

jsonb with subscripting support can be used as a dictionary object in
plpgsql.

Can be nice to have support for iteration over a set of tuples (key,
value).

Some like

FOREACH fieldvar [ KEY keyvar] IN DICTIONARY sourceexpr [VALUE searchexpr]
LOOP
END LOOP;

and for JSON arrays

FOREACH var IN ARRAY jsonval
LOOP
END LOOP

Example:

dict jsonb DEFAULT '{"a", "a1", "b", "b1"}
v text; k text;
j jsonb;
BEGIN
FOREACH v KEY k IN DICTIONARY dict
LOOP
RAISE NOTICE '%=>%', k, v; -- a=>a1\nb=>b1
END LOOP;
--
FOREACH j IN DICTIONARY dict
LOOP
RAISE NOTICE '%', j; -- {"a":"a1"}\n{"b":"b1"}
END LOOP;

The goal is to support fast iteration over some non atomic objects
different from arrays.

Maybe some background of XMLTABLE and JSON_TABLE functions can be used
there.

Comments, notes?

Regards

Pavel

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Julien Rouhaud 2021-01-23 06:52:02 Re: proposal - idea - enhancing plpgsql FOREACH for JSON, jsonb and hstore
Previous Message Amit Kapila 2021-01-23 06:20:12 Re: Logical Replication - behavior of ALTER PUBLICATION .. DROP TABLE and ALTER SUBSCRIPTION .. REFRESH PUBLICATION