环境准备
- 安装依赖包
rpmdevtools rpm-build
yum install -y rpm-build rpmdevtools
- 初始化rpm pacakge 项目
主要是rpm 打包的目录结构
使用rpmdev-setuptree 工具
效果如下:
── rpmbuild
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
创建简单pg extension 扩展
具体实现的功能很简单,可以参考github 项目https://github.com/rongfengliang/nvl-pg-extension
- 代码说明
扩展开发主要是control 以及函数定义
nvlfunc.control
# nvlfunc extension
comment = ‘Oracle compatible nvl function‘
default_version = ‘1.0‘
module_pathname = ‘$libdir/nvlfunc‘
relocatable = false
sql 函数定义
nvlfunc--1.0.sql
/* nvlfunc--1.0.sql */
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
\echo Use "CREATE EXTENSION nvlfunc" to load this file. \quit
CREATE OR REPLACE FUNCTION public.NVL(SMALLINT,SMALLINT)
RETURNS SMALLINT AS $$
SELECT COALESCE($1,$2);
$$ LANGUAGE SQL IMMUTABLE;
Makefile 定义,方便安装(注意pg 版本,可能需要配置path 路径,同时安装pg devel 包)
EXTENSION = nvlfunc
DATA = nvlfunc--1.0.sql
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
rpm spec 编写
/~rpmbuild/SPECS 路径
- 代码
Name: nvl-pg-extension
Version: 1.0
Release: 1%{?dist}
Summary: this is a postgresql extension nvl function just like oracle
License: APACHE
URL: https://github.com/rongfengliang/postgres-extension-demo
// 使用源码打包rpm 包
Source: https://github.com/rongfengliang/nvl-pg-extension/archive/v1.0.zip
%prep
%setup -q
%description
this is a postgresql extension nvl function just like oracle
%install
%{make_install} PREFIX=%{_prefix}
%files
/usr/pgsql-10/share/extension/nvlfunc--1.0.sql
/usr/pgsql-10/share/extension/nvlfunc.control
打包rpm 包
- 下载source
spectool -g -R ~/rpmbuild/SPECS/nvl-pg-extension.spec
- 打包rpm 包
rpmbuild -bb ~/rpmbuild/SPECS/nvl-pg-extension.spec
- 效果
spectool -g -R ~/rpmbuild/SPECS/nvl-pg-extension.spec
Getting https://github.com/rongfengliang/nvl-pg-extension/archive/v1.0.zip to /root/rpmbuild/SOURCES/v1.0.zip
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 133 0 133 0 0 72 0 --:--:-- 0:00:01 --:--:-- 72
0 0 0 1119 0 0 174 0 --:--:-- 0:00:06 --:--:-- 306
rpmbuild -bb ~/rpmbuild/SPECS/nvl-pg-extension.spec
执行(%prep): /bin/sh -e /var/tmp/rpm-tmp.uCoCHD
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd /root/rpmbuild/BUILD
+ rm -rf nvl-pg-extension-1.0
+ /usr/bin/unzip -qq /root/rpmbuild/SOURCES/v1.0.zip
+ STATUS=0
+ ‘[‘ 0 -ne 0 ‘]‘
+ cd nvl-pg-extension-1.0
+ /usr/bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
执行(%install): /bin/sh -e /var/tmp/rpm-tmp.YBV39p
+ umask 022
+ cd /root/rpmbuild/BUILD
+ ‘[‘ /root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64 ‘!=‘ / ‘]‘
+ rm -rf /root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64
++ dirname /root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64
+ mkdir -p /root/rpmbuild/BUILDROOT
+ mkdir /root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64
+ cd nvl-pg-extension-1.0
+ /usr/bin/make install DESTDIR=/root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64 PREFIX=/usr
/usr/bin/mkdir -p ‘/root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64/usr/pgsql-10/share/extension‘
/usr/bin/mkdir -p ‘/root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64/usr/pgsql-10/share/extension‘
/usr/bin/install -c -m 644 .//nvlfunc.control ‘/root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64/usr/pgsql-10/share/extension/‘
/usr/bin/install -c -m 644 .//nvlfunc--1.0.sql ‘/root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64/usr/pgsql-10/share/extension/‘
+ /usr/lib/rpm/find-debuginfo.sh --strict-build-id -m --run-dwz --dwz-low-mem-die-limit 10000000 --dwz-max-die-limit 110000000 /root/rpmbuild/BUILD/nvl-pg-extension-1.0
/usr/lib/rpm/sepdebugcrcfix: Updated 0 CRC32s, 0 CRC32s did match.
+ ‘[‘ ‘%{buildarch}‘ = noarch ‘]‘
+ QA_CHECK_RPATHS=1
+ case "${QA_CHECK_RPATHS:-}" in
+ /usr/lib/rpm/check-rpaths
+ /usr/lib/rpm/check-buildroot
+ /usr/lib/rpm/redhat/brp-compress
+ /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip
+ /usr/lib/rpm/brp-python-bytecompile /usr/bin/python 1
+ /usr/lib/rpm/redhat/brp-python-hardlink
+ /usr/lib/rpm/redhat/brp-java-repack-jars
处理文件:nvl-pg-extension-1.0-1.el7.x86_64
Provides: nvl-pg-extension = 1.0-1.el7 nvl-pg-extension(x86-64) = 1.0-1.el7
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
处理文件:nvl-pg-extension-debuginfo-1.0-1.el7.x86_64
Provides: nvl-pg-extension-debuginfo = 1.0-1.el7 nvl-pg-extension-debuginfo(x86-64) = 1.0-1.el7
Requires(rpmlib): rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rpmlib(CompressedFileNames) <= 3.0.4-1
检查未打包文件:/usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64
写道:/root/rpmbuild/RPMS/x86_64/nvl-pg-extension-1.0-1.el7.x86_64.rpm
写道:/root/rpmbuild/RPMS/x86_64/nvl-pg-extension-debuginfo-1.0-1.el7.x86_64.rpm
执行(%clean): /bin/sh -e /var/tmp/rpm-tmp.Ya1lDM
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd nvl-pg-extension-1.0
+ /usr/bin/rm -rf /root/rpmbuild/BUILDROOT/nvl-pg-extension-1.0-1.el7.x86_64
+ exit 0
── BUILD
│ └── nvl-pg-extension-1.0
│ ├── debugfiles.list
│ ├── debuglinks.list
│ ├── debugsources.list
│ ├── elfbins.list
│ ├── Makefile
│ ├── nvlfunc--1.0.sql
│ └── nvlfunc.control
├── BUILDROOT
├── RPMS
│ └── x86_64
│ ├── nvl-pg-extension-1.0-1.el7.x86_64.rpm
│ └── nvl-pg-extension-debuginfo-1.0-1.el7.x86_64.rpm
├── SOURCES
│ └── v1.0.zip
├── SPECS
│ └── nvl-pg-extension.spec
└── SRPMS
安装&&使用rpm 包
- 安装rpm包
yum install -y nvl-pg-extension-1.0-1.el7.x86_64.rpm
效果
yum install -y nvl-pg-extension-1.0-1.el7.x86_64.rpm
已加载插件:fastestmirror
正在检查 nvl-pg-extension-1.0-1.el7.x86_64.rpm: nvl-pg-extension-1.0-1.el7.x86_64
nvl-pg-extension-1.0-1.el7.x86_64.rpm 将被安装
正在解决依赖关系
--> 正在检查事务
---> 软件包 nvl-pg-extension.x86_64.0.1.0-1.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
=======================================================================================================================
Package 架构 版本 源 大小
=======================================================================================================================
正在安装:
nvl-pg-extension x86_64 1.0-1.el7 /nvl-pg-extension-1.0-1.el7.x86_64 435
事务概要
=======================================================================================================================
安装 1 软件包
总计:435
安装大小:435
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : nvl-pg-extension-1.0-1.el7.x86_64 1/1
验证中 : nvl-pg-extension-1.0-1.el7.x86_64 1/1
已安装:
nvl-pg-extension.x86_64 0:1.0-1.el7
完毕!
pg 扩展目录
find /usr/pgsql-10/share/extension/ -name nvlfunc.control
/usr/pgsql-10/share/extension/nvlfunc.control
- 使用
CREATE EXTENSION nvlfunc;
SELECT NVL(NULL::SMALLINT, 121::SMALLINT);
nvl
-----
121
(1 row)
说明
就是一个简单的学习测试,实际上基于pg 的扩展模型我们可以开发很多强大的功能
参考资料
https://github.com/rongfengliang/nvl-pg-extension
https://github.com/rongfengliang/postgres-extension-demo
https://github.com/rongfengliang/pg-extension-rpm-package
原文地址:https://www.cnblogs.com/rongfengliang/p/10650888.html
时间: 2024-11-05 18:42:32