openGauss/MogDB調用C FUNCTION
摘要
之前寫過一篇關于postgresql自定義函數實現,通過contrib模塊進行擴展的帖子,今天和恩墨工程師進行了一些交流,在MogDB中也可以實現同樣的功能,原以為需要完整的openGauss的源碼才能完成,但在恩墨工程師的指點下,了解到,通過既有官網版本的安裝包就可以進行插件開發。而且,使用postgres的C FUNCTION要比開發插件更加容易些。也感謝恩墨專家提供的線索和思路?? ??
環境準備
- 安裝MogDB
參考官方文檔,寫的已經很詳細了。 - 服務器環境
本地虛擬機 centos 7.9
注意:盡量進入omm用戶下進行編譯,可以避免一些不必要的環境問題
代碼
- C代碼
基本與postgres插件開發一樣,關鍵是4,5,6三行。
#include "postgres.h"
#include "fmgr.h"
PG_MODULE_MAGIC;
extern "C" Datum add_ab(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(add_ab);
Datum
add_ab(PG_FUNCTION_ARGS)
{
int32 arg_a = PG_GETARG_INT32(0);
int32 arg_b = PG_GETARG_INT32(1);
PG_RETURN_INT32(arg_a + arg_b);
}
- CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (gs_plug)
set(CMAKE_CXX_FLAGS "-Wall -std=c++11 -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-g3")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
set(CMAKE_BUILD_TYPE Debug)
set(MOG_INCLUDE /opt/mogdb/app/include/postgresql/server)
set(MOG_LIBPATH /opt/mogdb/app/lib/postgresql/proc_srclib)
include_directories(${MOG_INCLUDE})
aux_source_directory(. DIR_SRCS)
add_library (${PROJECT_NAME} SHARED ${DIR_SRCS})
install(TARGETS ${PROJECT_NAME} DESTINATION ${MOG_LIBPATH})
要點1:獲取包含頭文件的目錄
[omm@vmpc funcs]$ pg_config --includedir
/opt/mogdb/app/include
所需頭文件路徑:`pg_config --includedir`/postgresql/server
要點1:c函數安裝路徑
[omm@vmpc funcs]$ pg_config --pkglibdir
/opt/mogdb/app/lib/postgresql
安裝路徑:`pg_config --pkglibdir`/proc_srclib/
編譯 & 安裝
[omm@vmpc funcs]$ mkdir build
[omm@vmpc funcs]$ cd build/
[omm@vmpc build]$ cmake ../
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /opt/mogdb/funcs/build
[omm@vmpc build]$ make
[ 50%] Building CXX object CMakeFiles/gs_plug.dir/testfunc.cpp.o
[100%] Linking CXX shared library libgs_plug.so
[100%] Built target gs_plug
[omm@vmpc build]$ make install
Consolidate compiler generated dependencies of target gs_plug
[100%] Built target gs_plug
Install the project...
-- Install configuration: "Debug"
-- Installing: /opt/mogdb/app/lib/proc_srclib/libgs_plug.so
依次執行如下命令
mkdir build
cd build
cmake ../
make
make install
確認安裝
[omm@vmpc build]$ ll /opt/mogdb/app/lib/proc_srclib/libgs_plug.so
-rwxr-xr-x. 1 omm dbgrp 215696 Apr 2 00:17 /opt/mogdb/app/lib/proc_srclib/libgs_plug.so
驗證
- 鏈接mogdb
[omm@vmpc ~]$ pgcli -p 26000 -d postgres
Server: PostgreSQL 9.2.4
Version: 3.4.1
Home: http://pgcli.com
postgres>
- 創建C FUNCTION
postgres> CREATE FUNCTION add_ab(a int ,b int ) RETURNS integer
AS 'testfunc.so', 'add_ab'
LANGUAGE C STRICT;
CREATE FUNCTION
Time: 0.039s
- 查看函數

- 調用函數
postgres> select add_ab(a := 4, b := 2);
+--------+
| add_ab |
|--------|
| 6 |
+--------+
SELECT 1
Time: 0.033s
postgres>
最后修改時間:2022-08-04 16:28:58
「喜歡這篇文章,您的關注和贊賞是給作者最好的鼓勵」
關注作者
【版權聲明】本文為墨天輪用戶原創內容,轉載時必須標注文章的來源(墨天輪),文章鏈接,文章作者等基本信息,否則作者和墨天輪有權追究責任。如果您發現墨天輪中有涉嫌抄襲或者侵權的內容,歡迎發送郵件至:contact@modb.pro進行舉報,并提供相關證據,一經查實,墨天輪將立刻刪除相關內容。




