Table of Contents

openGauss学习笔记-307 openGauss AI特性-DB4AI数据库驱动AI-全流程AI-PLPython Fenced模式

传统的AI任务往往具有多个流程,如数据的收集过程包括数据的采集、数据清洗、数据存储等,在算法的训练过程中又包括数据的预处理、训练、模型的保存与管理等。其中,对于模型的训练过程,又包括超参数的调优过程。诸如此类机器学习模型生命周期的全过程,可大量集成于数据库内部。在距离数据存储侧最近处进行模型的训练、管理、优化等流程,在数据库端提供SQL语句式的开箱即用的AI全声明周期管理的功能,称之为全流程AI.

openGauss实现了部分全流程AI的功能,将在本章节中详细展开。

307.1 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下创建文件夹,并将文件夹权限修改为可读可写可执行状态。

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

配置完成,启动数据库。

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

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

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

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

307.1.1 使用指导

  • 创建Extension

    • 当编译的plpython为python2时:

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

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

    下面示例是以python2为例。

  • 创建plpython-fenced UDF

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

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

    • 创建一个数据表:

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

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

👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!

openGauss学习笔记-307 openGauss AI特性-DB4AI数据库驱动AI-全流程AI-PLPython Fenced模式_openGauss