Postgresql Function Define
In Postgresql, If you want to do something like "select Hello(a) from A;". You should define your own function Hello().
First, go to postgresql root directory and go into contrib file folder. Create your own directory, and add hello.h , hello.c, Makefile, hello.sql.in and uninstall_Hello.sql.in
Makefile is like the following:
1 | MODULE_big = Hello // this is module name |
Attention!!! In Makefile those command should be with no blank at end.
Here we can write hello.sql.in:
1 | SET search_path = public; --you should add this code first. |
Here is the uninstall_Hello.sql.in:
1 | SET search_path = public; |
In hello.h
1 | #ifndef HELLO_H |
hello.c
1 | #include “hello.h” |
1 | PG_FUNCTION_INFO_V1(hello_out); |
Then cd your folder contrib/Hello, make, and make install
Then start psql to execute :
create table A(a HelloS);
insert into A values(‘cc’);
insert into A values(‘ff’);
select Hello(a) from A;
result:
Hello cc
Hello ff
select * from A;
result:
cc
ff