proposal - idea - enhancing plpgsql FOREACH for JSON, jsonb and hstore - Mailing list pgsql-hackers

From Pavel Stehule
Subject proposal - idea - enhancing plpgsql FOREACH for JSON, jsonb and hstore
Date
Msg-id CAFj8pRCKngezkKEVbLS=oB8GWaq1DcS-XR7sTUs7K9UQ=vdSFg@mail.gmail.com
Whole thread Raw
Responses Re: proposal - idea - enhancing plpgsql FOREACH for JSON, jsonb and hstore
Re: proposal - idea - enhancing plpgsql FOREACH for JSON, jsonb and hstore
List 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



pgsql-hackers by date:

Previous
From: Amit Kapila
Date:
Subject: Re: Logical Replication - behavior of ALTER PUBLICATION .. DROP TABLE and ALTER SUBSCRIPTION .. REFRESH PUBLICATION
Next
From: Julien Rouhaud
Date:
Subject: Re: proposal - idea - enhancing plpgsql FOREACH for JSON, jsonb and hstore