PLPython Fenced模式

在fenced模式中添加plpython非安全语言。在数据库编译时需要将python集成进数据库中,在configure阶段加入–with-python选项。同时也可指定安装plpython的python路径,添加选项–with-includes=’/python-dir=path’。

在启动数据库之前配置GUC参数unix_socket_directory ,指定unix_socket进程间通讯的文件地址。用户需要提前在user-set-dir-path下创建文件夹,并将文件夹权限修改为可读可写可执行状态。

1. unix_socket_directory = '/user-set-dir-path'

配置完成,启动数据库。

将plpython加入数据库编译,并设置好GUC参数unix_socket_directory后,在启动数据库的过程中,自动创建fenced-Master进程。在数据库不进行python编译的情况下,fenced模式需要手动拉起master进程,在GUC参数设置完成后,输入创建master进程命令。

启动fenced-Master进程,命令为:

1. gaussdb --fenced -k /user-set-dir-path -D /user-set-dir-path &

完成fence模式配置,针对plpython-fenced UDF数据库将在fenced-worker进程中执行UDF计算。

使用指导

  • 创建Extension

  • 当编译的plpython为python2时:

    1. openGauss=# create Extension plpythonu; 2. CREATE Extension - 当编译的plpython为python3时:

    1. openGauss=# create Extension plpython3u; 2. CREATE Extension

下面示例是以python2为例。 - 创建plpython-fenced UDF

1. openGauss=# create or replace function pymax(a int, b int) 2. openGauss-# returns INT 3. openGauss-# language plpythonu fenced 4. openGauss-# as $$ 5. openGauss$# import numpy 6. openGauss$# if a > b: 7. openGauss$# return a; 8. openGauss$# else: 9. openGauss$# return b; 10. openGauss$# $$; 11. CREATE FUNCTION - 查看UDF信息

1. openGauss=# select * from pg_proc where proname='pymax'; 2. -[ RECORD 1 ]----+-------------- 3. proname | pymax 4. pronamespace | 2200 5. proowner | 10 6. prolang | 16388 7. procost | 100 8. prorows | 0 9. provariadic | 0 10. protransform | - 11. proisagg | f 12. proiswindow | f 13. prosecdef | f 14. proleakproof | f 15. proisstrict | f 16. proretset | f 17. provolatile | v 18. pronargs | 2 19. pronargdefaults | 0 20. prorettype | 23 21. proargtypes | 23 23 22. proallargtypes | 23. proargmodes | 24. proargnames | {a,b} 25. proargdefaults | 26. prosrc | 27. | import numpy 28. | if a > b: 29. | return a; 30. | else: 31. | return b; 32. | 33. probin | 34. proconfig | 35. proacl | 36. prodefaultargpos | 37. fencedmode | t 38. proshippable | f 39. propackage | f 40. prokind | f 41. proargsrc | - 运行UDF

  • 创建一个数据表:

    1. openGauss=# create table temp (a int ,b int) ; 2. CREATE TABLE 3. openGauss=# insert into temp values (1,2),(2,3),(3,4),(4,5),(5,6); 4. INSERT 0 5 - 运行UDF:

    1. openGauss=# select pymax(a,b) from temp; 2. pymax 3. ------- 4. 2 5. 3 6. 4 7. 5 8. 6 9. (5 rows)