Last active
November 9, 2019 18:10
-
-
Save ranjanprj/c18f0927ace72cf70b028e02e9f8f6e5 to your computer and use it in GitHub Desktop.
Revisions
-
ranjanprj revised this gist
Nov 7, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Just a small gist to show how you can easily create a Rust shared lib, wrap it with C and call it from PostgreSQL SQL Command. 0. Prereq : Tested on Ubuntu 18, GCC, postgresql-server-11, postgresql-dev-11, rustlang 1. Create new project Cargo embed and cd embed -
ranjanprj revised this gist
Nov 7, 2019 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,7 +38,9 @@ double_num(PG_FUNCTION_ARGS) ``` 4. Create a file called Makefile in same folder *Please change the file path to point to your home directory* ``` MODULES = funcs PG_CONFIG = pg_config @@ -48,13 +50,15 @@ include $(PGXS) funcs.so: funcs.o cc -shared -o funcs.so funcs.o /home/ranjanprj/embed/target/release/libembed.so funcs.o: funcs.c cc -o funcs.o -c funcs.c $(CFLAGS) -I$(INCLUDEDIR) ``` 5. `sudo make && sudo make install` 6. In postgresql create the extension *Please change the file path to point to your home directory* ```sql drop function if exists double_num(integer); -
ranjanprj revised this gist
Nov 7, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Just a small gist to show how you can easily you can create a Rust shared lib, wrap it with C and call it from PostgreSQL SQL Command. 0. Prereq : Tested on Ubuntu 18, GCC, postgresql-server-11, postgresql-dev-11, rustlang 1. Create new project Cargo embed and cd embed -
ranjanprj revised this gist
Nov 7, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Just a small gist to show how you can easily you can create a Rust shared lib, wrap it with C and call it from PostgreSQL SQL # # # Command. 0. Prereq : Tested on Ubuntu 18, GCC, postgresql-server-11, postgresql-dev-11, rustlang 1. Create new project Cargo embed and cd embed -
ranjanprj revised this gist
Nov 7, 2019 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ #Just a small gist to show how you can easily you can create a Rust shared lib, wrap it with C and call it from PostgreSQL SQL #Command. 0. Prereq : Tested on Ubuntu 18, GCC, postgresql-server-11, postgresql-dev-11, rustlang 1. Create new project Cargo embed and cd embed 2. Rename the file src/main.rs to lib.rs -
ranjanprj revised this gist
Nov 7, 2019 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ 0. Prereq : Tested on Ubuntu 18, GCC, postgresql-server-11, postgresql-dev-11, rustlang 1. Create new project Cargo embed and cd embed 2. Rename the file src/main.rs to lib.rs 3. Add following code ``` #[no_mangle] @@ -10,7 +10,7 @@ pub extern fn double_input(input: i32) -> i32 { ``` 3. Create a c file called funcs.c add following code in embed folder ```C #include "postgres.h" @@ -35,7 +35,7 @@ double_num(PG_FUNCTION_ARGS) } ``` 4. Create a file called Makefile in same folder ``` MODULES = funcs -
ranjanprj revised this gist
Nov 7, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,6 +32,7 @@ double_num(PG_FUNCTION_ARGS) int32 arg = PG_GETARG_INT32(0); PG_RETURN_INT32(double_input(arg)); } ``` 4. Makefile -
ranjanprj renamed this gist
Nov 7, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ranjanprj created this gist
Nov 7, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ 1. Create new project Cargo embed 2. Rename the file embed/src/main.rs to lib.rs 3. Add following code ``` #[no_mangle] pub extern fn double_input(input: i32) -> i32 { input * 2 } ``` 3. Create a c file called funcs.c add following code ```C #include "postgres.h" #include <string.h> #include "fmgr.h" #include "utils/geo_decls.h" #ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif /* by value */ extern int32_t double_input(int32_t input); PG_FUNCTION_INFO_V1(double_num); Datum double_num(PG_FUNCTION_ARGS) { int32 arg = PG_GETARG_INT32(0); PG_RETURN_INT32(double_input(arg)); ``` 4. Makefile ``` MODULES = funcs PG_CONFIG = pg_config PGXS = $(shell $(PG_CONFIG) --pgxs) INCLUDEDIR = $(shell $(PG_CONFIG) --includedir-server) include $(PGXS) funcs.so: funcs.o cc -shared -o funcs.so funcs.o /home/ranjanprj/zig/libmathtest.so.0.0.0 funcs.o: funcs.c cc -o funcs.o -c funcs.c $(CFLAGS) -I$(INCLUDEDIR) ``` 5. `sudo make && sudo make install` 6. In postgresql create the extension ```sql drop function if exists double_num(integer); CREATE or replace FUNCTION double_num(integer) RETURNS integer AS '/home/ranjanprj/embed/funcs.so', 'double_num' LANGUAGE C STRICT; ``` 7. Execute ```sql select double_num(4); ```