转自: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:

  1. 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.
  2. 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. \quit
 
CREATE 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 extension
comment = '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 = nvlfunc
DATA = nvlfunc--1.0.sql
 
PG_CONFIG = pg_config
PGXS := $(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 install
Then 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 uninstall
Note:
This extension module requires PostgreSQL 9.1 or later because CREATE EXTENSION
feature 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
# ls
Makefile         nvlfunc.control    nvlfunc--1.0.sql    sql     expected    INSTALL.nvlfunc    README
Related resources

Pros

  1. Easy to extend the PostgreSQL functionality
  2. Very easy to create and install the extension
  3. Easy to test for regressions on different versions of PostgreSQL

Cons

  1. 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的更多相关文章

  1. 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 ...

  2. 开发一个简单的postgresql extension

      主要是学习如何编写一个简单的pg extension,参考https://severalnines.com/blog/creating-new-modules-using-postgresql-c ...

  3. 使用fpm 方便快速生成postgresql extension分发包

    fpm 是一个不错,而且强大的rpm.deb,系统启动服务工具包,我们可以用来快速的生成专业的软件分发包 演示一个pg 扩展包分发包的生成(rpm 以及deb) 安装fpm sudo gem inst ...

  4. [Tools] Create a Chrome Extension

    Creating a Chrome extension requires a manifest.json file which defines how your extension will beha ...

  5. [官网]CREATE EXTENSION PostGreSQL 创建函数的方法

    CREATE EXTENSION https://www.postgresql.org/docs/current/sql-createextension.html CREATE EXTENSION — ...

  6. How to debug PostgreSQL function with pgAdminIII

    How to debug plpgsql with pgAdminIII [root@localhost soft_bak]# git clone git://git.postgresql.org/g ...

  7. ubuntu安装postgresql与postgis

    版本信息 ubuntu    14.04.1LTS postgresql   9.3.5 postgis       2.1.2 今天尝试着安装了postgis 2.1.2,(较简便的包安装,不是源码 ...

  8. 使用rpm 打包开发的postgres extension

      环境准备 安装依赖包 rpmdevtools rpm-build yum install -y rpm-build rpmdevtools 初始化rpm pacakge 项目 主要是rpm 打包的 ...

  9. PostgreSQL通过mysql_fdw访问MySQL数据库

    Mysql与PostgreSQL的安装过程省略. 为简便起见,把MySQL和PostgreSQL都安装在一个机器上,然后在此机器上(准确地说是在PostgreSQL运行的机器上)安装mysql_fdw ...

随机推荐

  1. kafka生产者

    1.kafka生产者是线程安全的,她允许多个线程共享一个kafka实例 2.kafka管理一个简单的后台线程,所有的IO操作以及与每个broker的tcp连接通信,如果没有正确的关闭生产者可能会造成资 ...

  2. MATLAB绘图功能(1) 二维高层绘图操作

    文末源代码 部分源代码 %% 基本绘图操作 x=:*pi; y=sin(x); plot(x,y); % 第二个参数为矩阵 y1=sin(x); y2=cos(x); y3=0.002*exp(x); ...

  3. obs studio 使用

    专业,开源,无广告,免费,录屏/推流神器--obs studio 稍微简单的也有captura, 原理:调用本地API获取音频流,图像流(全屏幕,单个windows窗口的图像输出)->开源音视频 ...

  4. Confluence 6 安装指南

    在你开始之前 在你开始安装 Confluence 之前,请确定你的安装环境满足 最小系统安装要求和支持的平台. 如果你计划将你的 Confluence 运行到虚拟环境下,请参考 Running Con ...

  5. mongodb笔记(二)

    1.使用mongodb的shell连接Mongodb服务: 标准URL连接语法: mongodb://[username:password@]host1[:port1][,host2[:port2], ...

  6. 移动端解决悬浮层(悬浮header、footer)会遮挡住内容的方法

    固定Footer Bootstrap框架提供了两种固定导航条的方式: ☑  .navbar-fixed-top:导航条固定在浏览器窗口顶部 ☑  .navbar-fixed-bottom:导航条固定在 ...

  7. babel-plugin-import配置babel按需引入antd模块,编译后报错.bezierEasingMixin()

    用create-react-app做项目的时候,同时引入了antd,为了实现按需加载antd模块,用到他们提供的  babel-plugin-import  ( 一个用于按需加载组件代码和样式的 ba ...

  8. windows服务项目的 安装 卸载 查看

    安装服务:installutil.exe C:\a.exe卸载服务Installutil.exe /u C:\a.exe 查看服务状态 services.msc

  9. laravel 实现增 与查

    //调用模型层 <?phpnamespace App;use Illuminate\Support\Facades\DB;use Illuminate\Database\Eloquent\Mod ...

  10. eclipse快捷键大全(转载自CSDN)

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...