1

The extension hstore_plpython3u is supposed to transform hstore to/from python dict as described in the docs, but doesn't seem to work. The hstore becomes a string (no transform seems to happen). Why, and how to fix it?

The extensions for PL/Python are called hstore_plpythonu, hstore_plpython2u, and hstore_plpython3u (see Section 45.1 for the PL/Python naming convention). If you use them, hstore values are mapped to Python dictionaries. https://www.postgresql.org/docs/current/hstore.html

Create extensions

create extension plpython3u;
create extension hstore_plpython3u;

Create a function

create function type(names hstore)
returns text
language plpython3u as $$
   return type(names);
$$;

Call function

select type(hstore('name','martin'));

returns

     type      
---------------
 <class 'str'>
(1 row)
Martin
  • 97
  • 1
  • 6
  • I'm guessing it has to do with this; 'If you install these transforms and specify them when creating a function, ...' from the docs. I have no idea how you specify them when creating a function though? – Adrian Klaver Jun 16 '21 at 17:54

1 Answers1

1

Figured out I think. From the expected/hstore_plpython.out file in /contrib/hstore_plpython:

CREATE FUNCTION test1arr(val hstore[]) RETURNS int
LANGUAGE plpythonu
TRANSFORM FOR TYPE hstore
AS $$
assert(val == [{'aa': 'bb', 'cc': None}, {'dd': 'ee'}])
return len(val)
$$;
SELECT test1arr(array['aa=>bb, cc=>NULL'::hstore, 'dd=>ee']);
 test1arr
----------
        2



Adrian Klaver
  • 15,886
  • 2
  • 17
  • 28