How to Create an PostgreSQL Extension
转自:https://severalnines.com/blog/creating-new-modules-using-postgresql-create-extension
Extensibility is one of the most powerful feature in PostgreSQL. You can add new functionality for a particular use case by using contrib module and install it using CREATE EXTENSION.
In this section, we are going to learn how to create a simple contrib module and how to use its functionality in PostgreSQL.
Extension Files
To be able to run the CREATE EXTENSION command in your database, your extension must need at least two files:
- Control file
The file format must be extension_name.control, which tells the basics about extension to PostgreSQL, and must be placed in the installation’s SHAREDIR/extension directory. - SQL script file
The file in the format extension--version.sql contains the functions that you would like to add.
The file format of the control file in the extension is same as postgresql.conf file, namely a list of parameter_name = value assignments, one per line.
Example
Please check the below complete example of an SQL-only extension, create Oracle compatible NVL function in PostgreSQL. There are many cases but here we can consider only one case for example.
The SQL script file nvlfunc--1.0.sql looks like this...
Nvlfunc--1.0.sql file:
|
1
2
3
4
5
6
7
|
--complain if script is sourced in psql, rather than via CREATE EXTENSION\echo Use "CREATE EXTENSION nvlfunc" to load this file. \quitCREATE OR REPLACE FUNCTION public.NVL(SMALLINT,SMALLINT)RETURNS SMALLINT AS $$SELECT COALESCE($1,$2);$$ LANGUAGE SQL IMMUTABLE; |
The control file nvlfunc looks like this...
Nvlfunc.conntrol file:
|
1
2
3
4
5
|
# nvlfunc extensioncomment = 'Oracle compatible nvl function'default_version = '1.0'module_pathname = '$libdir/nvlfunc'relocatable = false |
While you hardly need a makefile to install these files into the correct directory, you could use a Makefile containing this:
Makefile:
|
1
2
3
4
5
6
|
EXTENSION = nvlfuncDATA = nvlfunc--1.0.sqlPG_CONFIG = pg_configPGXS := $(shell $(PG_CONFIG) --pgxs)include $(PGXS) |
If you have implemented the function using ‘C’ language then you need to add the file in the makefile.
Installation
The command make install will install the control file and sql script file into the correct directory as reported by pg_config.
Once the files are installed, use the CREATE EXTENSION command to load the objects into any particular database in PostgreSQL.
Please check the following steps to install the nvlfunc extension and you can also add this file in your extension directory:
INSTALL.nvlfunc file:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
This module is a PostgreSQL extension which provides the Oracle compatible nvl function feature.Use below command in source directory of nvlfunc to install the module.make installThen use the below command to create extension nvlfunc in database.CREATE EXTENSION nvlfunc;Use the below command to remove the nvlfunc extension from database.DROP EXTENSION nvlfunc;Use below command in source directory of nvlfunc to uninstall the module.make uninstallNote:This extension module requires PostgreSQL 9.1 or later because CREATE EXTENSIONfeature is available in PostgreSQL 9.1 and later version. |
PostgreSQL extensions must be installed in your database before you can use their functionality. To install a particular extension, run the CREATE EXTENSION command from psql to load the packaged objects into the database.
Testing
Once you create the extension, it is recommended to create a test case for that extension so that when you are installing this module in a different PostgreSQL version, you can then check whether all the test cases are working as expected or not.
This is an optional step but you can create it. The test cases look like this:
sql/nvlfunc.sql file:
|
1
|
SELECT NVL(NULL::SMALLINT, 11::SMALLINT); |
Then you can add expected output in another file. The expected output file looks like this:
expected/nvlfunc.out file:
|
1
2
3
4
5
|
SELECT NVL(NULL::SMALLINT, 11::SMALLINT); nvl ----- 11(1 row) |
This is just one example, that’s why only one test case is added. But when you are creating a new extension, you can add more test cases.
Nvlfunc extension directory structure:
|
1
2
3
4
|
# mkdir nvlfunc# cd nvlfunc# lsMakefile nvlfunc.control nvlfunc--1.0.sql sql expected INSTALL.nvlfunc README |
Pros
- Easy to extend the PostgreSQL functionality
- Very easy to create and install the extension
- Easy to test for regressions on different versions of PostgreSQL
Cons
- There is no special cons but test the feature you are adding in the extension before you use it.
How to Create an PostgreSQL Extension的更多相关文章
- use Swig to create C/C++ extension for Python
SWIG is a software development tool that simplifies the task of interfacing different languages to C ...
- 开发一个简单的postgresql extension
主要是学习如何编写一个简单的pg extension,参考https://severalnines.com/blog/creating-new-modules-using-postgresql-c ...
- 使用fpm 方便快速生成postgresql extension分发包
fpm 是一个不错,而且强大的rpm.deb,系统启动服务工具包,我们可以用来快速的生成专业的软件分发包 演示一个pg 扩展包分发包的生成(rpm 以及deb) 安装fpm sudo gem inst ...
- [Tools] Create a Chrome Extension
Creating a Chrome extension requires a manifest.json file which defines how your extension will beha ...
- [官网]CREATE EXTENSION PostGreSQL 创建函数的方法
CREATE EXTENSION https://www.postgresql.org/docs/current/sql-createextension.html CREATE EXTENSION — ...
- How to debug PostgreSQL function with pgAdminIII
How to debug plpgsql with pgAdminIII [root@localhost soft_bak]# git clone git://git.postgresql.org/g ...
- ubuntu安装postgresql与postgis
版本信息 ubuntu 14.04.1LTS postgresql 9.3.5 postgis 2.1.2 今天尝试着安装了postgis 2.1.2,(较简便的包安装,不是源码 ...
- 使用rpm 打包开发的postgres extension
环境准备 安装依赖包 rpmdevtools rpm-build yum install -y rpm-build rpmdevtools 初始化rpm pacakge 项目 主要是rpm 打包的 ...
- PostgreSQL通过mysql_fdw访问MySQL数据库
Mysql与PostgreSQL的安装过程省略. 为简便起见,把MySQL和PostgreSQL都安装在一个机器上,然后在此机器上(准确地说是在PostgreSQL运行的机器上)安装mysql_fdw ...
随机推荐
- LINQ to Entities 不识别方法“System.String get_Item(Int32)”,因此该方法无法转换为存储表达式。
1.LINQ to Entities 不识别方法“System.String get_Item(Int32)”,因此该方法无法转换为存储表达式.项目中发现linq to entities 不识别? , ...
- Java问题解决:The project cannot be built until build path errors are resolved
参考:http://blog.csdn.net/marty_zhu/article/details/2566299 1,看看project -- Build Automatically有没有勾上?如果 ...
- 力扣(LeetCode)463. 岛屿的周长
给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域. 网格中的格子水平和垂直方向相连(对角线方向不相连).整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地 ...
- dom常用操作
创建节点:document.createElement(元素名), document.createTextNode(文本内容) 添加节点:parent.appendChild(newChild) 移除 ...
- 第 8 章 容器网络 - 066 - Weave 如何与外网通信?
Weave 与外网通信 weave 是一个私有的 VxLAN 网络,默认与外部网络隔离. 外部网络如果要访问到 weave 中的容器:1.首先将主机加入到 weave 网络.2.然后把主机当作访问 w ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 创建页脚摘要
jQuery EasyUI 数据网格 - 创建页脚摘要 在本教程中,我们将向您展示如何在数据网格(datagrid)页脚显示摘要信息行. 为了显示页脚行,您应该设置 showFooter 属性为 tr ...
- laravel框架中的邮箱发送
1.设置邮件服务器登录 163邮箱设置 POP3/SMTP/IMAP勾选:POP3/SMTP服务 IMAP/SMTP服务 保存 设置客户端授权密码 2.在laravel中配置服务器 打开.env文件 ...
- 机器学习之决策树_CART算法
目录 3.CART算法(classification and regression tree tree) 3.1 CART生成算法(回归树生成和分类树生成) 3.2 CART剪枝 决策树基本知识参考, ...
- 『TensorFlow』第三弹_可视化框架介绍_悄悄问圣僧
添加记录节点 -> 汇总记录节点 -> run汇总节点 -> [书写器生成]书写入文件 [-> 刷新缓冲区] 可视化关键点: 注意, 1.with tf.name_scope( ...
- 使用python来访问Hadoop HDFS存储实现文件的操作
原文:http://rfyiamcool.blog.51cto.com/1030776/1258292 在调试环境下,咱们用hadoop提供的shell接口测试增加删除查看,但是不利于复杂的逻辑编程 ...