首页 > 解决方案 > 如何启用 hstore_plpython3u

问题描述

如文档中所述,扩展 hstore_plpython3u 应该将 hstore 转换为 python dict 或从 python dict 转换,但似乎不起作用。hstore 变成一个字符串(似乎没有发生转换)。为什么,以及如何解决?

PL/Python 的扩展名为 hstore_plpythonu、hstore_plpython2u 和 hstore_plpython3u(请参阅第 45.1 节了解 PL/Python 命名约定)。如果使用它们,hstore 值将映射到 Python 字典。 https://www.postgresql.org/docs/current/hstore.html

创建扩展

create extension plpython3u;
create extension hstore_plpython3u;

创建一个函数

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

调用函数

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

返回

     type      
---------------
 <class 'str'>
(1 row)

标签: postgresqlhstoreplpython

解决方案


想通了,我想。从expected/hstore_plpython.out文件中/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




推荐阅读