postgresql插件应用及开发实验 任务345

任务三、五:

任务三用c语言即可,任务五也会一并完成!!!

任务三:

1.进入容器终端:

docker exec -it pgv bash

2.进入插件目录:

cd /var/lib/postgresql/data/myplugin/

创建名为char_count的目录:

如果已经存在,请先删除:

rm -r char_count

创建:

mkdir char_count

3.进入char_count目录:

cd /var/lib/postgresql/data/myplugin/char_count

4.创建4个文件,如下所示:

char_count.c

char_count–1.0.sql

(注意这里char_count–1.0.sql有两个“-”,显示有点问题)

char_count.control

Makefile

创建文件使用vim,例如:

vim char_count.c

如果提示没有 vim,就下一个vim:

apt-get update
apt-get install vim

Makefile内容如下:

# PostgreSQL extension build infrastructure
MODULE_big = char_count
OBJS = char_count.o

EXTENSION = char_count
DATA = char_count--1.0.sql
PGFILEDESC = "char_count - count occurrences of a character in a text"

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

char_count.control内容如下:

comment = 'Character count plugin for PostgreSQL'
default_version = '1.0'
module_pathname = '$libdir/char_count'
relocatable = true

char_count–1.0.sql内容如下:

-- Install the char_count function
CREATE OR REPLACE FUNCTION char_count(text, char)
RETURNS integer
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;

char_count.c内容如下:

#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/elog.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(char_count);

Datum char_count(PG_FUNCTION_ARGS) {
    text *input_text = PG_GETARG_TEXT_PP(0);  // 直接使用变量
    text *search_char = PG_GETARG_TEXT_PP(1);  // 直接使用变量
    char *str = text_to_cstring(input_text);
    char target_char = *text_to_cstring(search_char);  // 直接获取字符
    int count = 0;

    // 检查搜索字符长度是否为1
    if (strlen(text_to_cstring(search_char)) != 1) {
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("第二个参数必须是单个字符")));  // 修正括号和分号
    }

    // 统计字符出现次数
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == target_char) {
            count++;
        }
    }

    pfree(str);
    pfree(text_to_cstring(search_char));  // 释放内存

    PG_RETURN_INT32(count);  // 确保返回值
}

文件创建完成以后,确保在char_count目录下,执行:

make
make install

5.进入数据库,创建扩展:

确保在容器里执行:

psql -U pgvector

进入test数据库:

\c test

创建char_count扩展:

CREATE EXTENSION char_count;

查看是否成功:

\df

检查是否有名为char_count的扩展,如果有,检查一下小智有没有打勾;如果没有,在容器里删掉/var/lib/postgresql/data/myplugin/下char_count目录,重新来一次。

任务四:

1.进入容器终端:

docker exec -it pgv bash

进入插件目录:

cd /var/lib/postgresql/data/myplugin

把原来用c语言写的hello备份,准备使用plpgsql语言写hello:

mv hello hello_c

创建新的hello目录:

mkdir hello

进入hello目录:

cd /var/lib/postgresql/data/myplugin/hello

2.创建3个文件:

hello–1.0.sql
注意这里hello–1.0.sql有两个“-”,显示有点问题

hello.control

Makefile

同样使用vim,比如

vim hello--1.0.sql

hello–1.0.sql内容如下:

-- 创建 hello 函数
CREATE OR REPLACE FUNCTION hello()
RETURNS text AS $$
BEGIN
    RETURN 'Hello from the plpgsql plugin!';
END;
$$ LANGUAGE plpgsql;

-- 安装插件时执行的 SQL 语句
COMMENT ON FUNCTION hello() IS 'Returns a hello message';

hello.control内容如下:

comment = 'Hello plugin for PostgreSQL'
default_version = '1.0'
module_pathname = '$libdir/hello'
relocatable = true

Makefile内容如下:

# PostgreSQL extension build infrastructure
EXTENSION = hello
DATA = hello--1.0.sql
PGFILEDESC = "hello - A simple hello plugin"

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

文件创建完成以后,确保在hello目录下,执行:

make
make install

3.进入数据库,创建扩展:

确保在容器里执行:

psql -U pgvector

进入test数据库:

\c test

由于我们在任务2里已经创建过hello的扩展,所以我们先删掉:

DROP EXTENSION hello;

然后再创建扩展:

CREATE EXTENSION hello;

查看是否成功:

\df

检查是否有名为hello的扩展。

调用函数查看结果:

SELECT * FROM hello();

然后检查一下小智有没有打勾;

如果没有,在容器里删掉/var/lib/postgresql/data/myplugin/下hello目录,重新来一次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值