通过plpython 扩展解决pg 调用c library 的问题

属于一个简单的业务问题,需要调用一个c 库,希望用sql 去处理,使用上就比较简单,不想自己遍历数据然后再调用c 库,因为系统使用的pg ,所以想着用pg 包装c 库为一个函数

解决方法

  • 将c 库包装为一个独立的扩展,扩展集成了c 中的方法,然后就可以使用sql 处理了
  • 通过plpython 扩展,我们直接使用python 内部的ctypes 调用c 库

备注: 基于plpython 相对简单,成本低,必须要太多pg 扩展开发的技术,只需要编写ctypes 的python 代码就可以了

参考使用

就是一个演示

  • 函数包装
CREATE FUNCTION match_checkv5(ref integer[], target integer[])
  RETURNS integer
AS $$
   
import ctypes
import time
import ast
lib = ctypes.CDLL('/opt/mylibs/libfpmatch.so')
lib.Match2Fp.restype = ctypes.c_int  # 返回值类型为 int
lib.Match2Fp.argtypes = [ctypes.POINTER(ctypes.c_uint8),ctypes.POINTER(ctypes.c_uint8)]

arr = ctypes.c_uint8 * 256
a_1 = arr(*list(ref))
b_1 = arr(*list(target))

result_match = lib.Match2Fp(a_1, b_1)

return result_match
   
$$ LANGUAGE plpython3u;
  • 调用
SELECT match_checkv5(array[3,1,92,17,119,59,255,254,255,254,255,254,192,6,192,6,192,6,192,6,192,6,192,6,192,6,192,6,192,6,192,6,224,14,255,254,255,254,255,254,0,0,0,0,119,4,24,112,92,15,4,4,0,17,28,2,71,150,149,30,68,158,213,254,74,164,212,190,79,45,148,158,35,174,153,126,62,49,87,222,74,178,233,30,58,20,26,31,46,152,91,95,42,172,65,159,103,173,209,159,95,162,81,125,62,45,107,125,89,31,144,219,86,162,102,91,64,168,42,88,69,41,214,121,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],array[3,1,92,17,119,59,255,254,255,254,255,254,192,6,192,6,192,6,192,6,192,6,192,6,192,6,192,6,192,6,192,6,224,14,255,254,255,254,255,254,0,0,0,0,119,4,24,112,92,15,4,4,0,17,28,2,71,150,149,30,68,158,213,254,74,164,212,190,79,45,148,158,35,174,153,126,62,49,87,222,74,178,233,30,58,20,26,31,46,152,91,95,42,172,65,159,103,173,209,159,95,162,81,125,62,45,107,125,89,31,144,219,86,162,102,91,64,168,42,88,69,41,214,121,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
  • 效果

说明

当然基于perl 的扩展应该也是可以解决类似问题的,但是似乎没有plpython的简单,但是也要注意参数的控制,不然容易造成pg 服务不稳定

参考资料

https://docs.python.org/3/library/ctypes.html

https://www.postgresql.org/docs/current/plpython.html

https://www.postgresql.org/docs/current/plperl.html

posted on   荣锋亮  阅读(2)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2024-02-21 dremio FileDatasetHandle 简单说明
2024-02-21 pkl cli 简单试用
2022-02-21 codefever git  协议处理简单说明
2022-02-21 openresty net::ERR_HTTP2_SERVER_REFUSED_STREAM 问题参考解决方法-续
2022-02-21 codefever蒲公英团队开源的git server
2022-02-21 dremio 连接gitbase
2022-02-21 openresty net::ERR_HTTP2_SERVER_REFUSED_STREAM 问题参考解决方法

导航

< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示