在创建PostgreSQL的扩展测试 [英] Creating an extension test in postgresql
问题描述
我想创建一个的在Postgres扩展测试的(使用PostGIS的),所以我想执行以下步骤:
1:编辑文件 btree_interval.c
来自的这样btree_gist :
gbt_intvkey_cmp(常量无效*一,常量无效* B)
{
intvKEY * IA =(intvKEY *)(((常量NSRT *)一) - GT;吨);
intvKEY * IB =(intvKEY *)(((常量NSRT *)B) - >吨);
中期业绩;
......
...... 的printf(试验POSTGIS \\ n); 返回水库;
}
只添加一个的printf
,因为我只想做一个小测试
2:运行以下命令:
的gcc -o -shared -fPIC btree_gist_test.so btree_gist.c
我的疑惑是:
1:我不知道在哪里可以找到文件
btree_gist.c
一度的PostgreSQL安装,然后运行上面的命令。
块引用>如果你问我:你为什么不只是你做的下载源$ C $ C'
嗯,因为当我做,我得到这个错误信息:的#include了postgres.h
^
编译终止所以,我认为这是更好地做到这一点在PostgreSQL是已经安装了相同的文件夹中。
2:一旦我得到了我btree_gist_test.so知道我必须复制到路径
/ usr / lib目录/ PostgreSQL的/ lib目录/
,但我不知道如果我要创建一个符号链接到该文件的其他地方一个
块引用>解决方案这是一个可行的小例子,如果你有
的PostgreSQL服务器
开发包安装了Ubuntuextension.c 结果
的简单扩展的/ * Postgres的头文件* /
#包括LT&;&了postgres.h GT;
#包括LT&; utils的/ rel.h>#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;#IFDEF PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#万一静态的char *
extract_string(文字*字)
{
字符*头;
字符*尾; 如果(字== NULL)
返回NULL; 头= VARDATA(字);
尾=头+ VARSIZE(字) - VARHDRSZ;
尾[0] ='\\ 0'; 返回头;
}PG_FUNCTION_INFO_V1(compare_strings);
基准
compare_strings(PG_FUNCTION_ARGS)
{
字符* LHS;
字符* RHS; LHS = extract_string(PG_GETARG_TEXT_P(0));
RHS = extract_string(PG_GETARG_TEXT_P(1)); PG_RETURN_BOOL(STRCMP(左,右)== 0);
}的Makefile 结果
的一个简单的makefile以说明如何可以建立扩展。的CC = GCC
OBJECT = extension.o
NAME = my-extension.so
CFLAGS = -Wall -Werror -g3 -O0 -I $(壳pg_config --includedir服务器)所有:$(物体)
$(CC)-shared -o $(NAME)$(OBJECT)%的.o:%.C
$(CC)-c -fPIC $(CFLAGS)$<安装:所有
@Install -DV -m755 $(NAME)$(壳pg_config --pkglibdir)/ $(NAME)
@psql -U Postgres的-f创建,function.sql清洁:
@Rm -fv * *的.o。所以CREATE-function.sql 结果
的一个简单的脚本来创建功能的CREATE OR REPLACE FUNCTION
compare_strings(VARCHAR,VARCHAR)RETURNS整数为我的扩展
c语言严格;当它从你的问题看来,你就能明白这样做,以及如何使您的使用情况下工作。
I want to create an extension test in postgres (Using PostGis), so I want to do the following steps:
1.- Edit the file
btree_interval.c
from btree_gist in this way:gbt_intvkey_cmp(const void *a, const void *b) { intvKEY *ia = (intvKEY *) (((const Nsrt *) a)->t); intvKEY *ib = (intvKEY *) (((const Nsrt *) b)->t); int res; ...... ...... printf("Test for PostGis\n"); return res; }
Only add a
printf
, because I just want to do a little test2.- Run the following command:
gcc -shared -o btree_gist_test.so -fPIC btree_gist.c
My doubts are:
1.- I don't know where I can find the file
btree_gist.c
once postgresql is installed and then run the command above.If you ask me: 'Why don't just you do that downloading the source code?' Well, because When I did, I got this error message:
#include "postgres.h" ^ compilation terminated
So, I thought that it's better do it in the same folder where postgresql is already installed.
2.- Once I get the btree_gist_test.so I know that I have to copy to the path
/usr/lib/postgresql/lib/
, but I'm not sure if I have to create a symbolic link to a somewhere else for this file.
解决方案This is a minimal example that works if you have the
postgresql-server
development package for ubuntu installedextension.c
A simple extension/* Postgres headers */ #include <postgres.h> #include <utils/rel.h> #include <stdio.h> #include <string.h> #ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif static char * extract_string(text *word) { char *head; char *tail; if (word == NULL) return NULL; head = VARDATA(word); tail = head + VARSIZE(word) - VARHDRSZ; tail[0] = '\0'; return head; } PG_FUNCTION_INFO_V1(compare_strings); Datum compare_strings(PG_FUNCTION_ARGS) { char *lhs; char *rhs; lhs = extract_string(PG_GETARG_TEXT_P(0)); rhs = extract_string(PG_GETARG_TEXT_P(1)); PG_RETURN_BOOL(strcmp(lhs, rhs) == 0); }
Makefile
A simple Makefile to illustrate how you could build the extension.CC = gcc OBJECT = extension.o NAME = my-extension.so CFLAGS = -Wall -Werror -g3 -O0 -I$(shell pg_config --includedir-server) all: $(OBJECT) $(CC) -shared -o $(NAME) $(OBJECT) %.o: %.c $(CC) -c -fPIC $(CFLAGS) $< install: all @install -Dv -m755 $(NAME) $(shell pg_config --pkglibdir)/$(NAME) @psql -U postgres -f create-function.sql clean: @rm -fv *.o *.so
create-function.sql
A simple script to create the functionCREATE OR REPLACE FUNCTION compare_strings(VARCHAR,VARCHAR) RETURNS integer AS 'my-extension' LANGUAGE C STRICT;
As it seems from your question, you will be able to understand what this does and also how to make it work for your use case.
这篇关于在创建PostgreSQL的扩展测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!