postgresqlhstoreplpython

How to enable hstore_plpython3u


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)

Solution

  • 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