Skip to content

Instantly share code, notes, and snippets.

@ranjanprj
Last active November 9, 2019 18:10
Show Gist options
  • Select an option

  • Save ranjanprj/c18f0927ace72cf70b028e02e9f8f6e5 to your computer and use it in GitHub Desktop.

Select an option

Save ranjanprj/c18f0927ace72cf70b028e02e9f8f6e5 to your computer and use it in GitHub Desktop.

Revisions

  1. ranjanprj revised this gist Nov 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PG_EXTN_C_WITH_RUST.md
    Original 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.
    # 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
  2. ranjanprj revised this gist Nov 7, 2019. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion PG_EXTN_C_WITH_RUST.md
    Original 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/zig/libmathtest.so.0.0.0
    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);
  3. ranjanprj revised this gist Nov 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PG_EXTN_C_WITH_RUST.md
    Original 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.
    # 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
  4. ranjanprj revised this gist Nov 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PG_EXTN_C_WITH_RUST.md
    Original 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.
    # 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
  5. ranjanprj revised this gist Nov 7, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions PG_EXTN_C_WITH_RUST.md
    Original 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
  6. ranjanprj revised this gist Nov 7, 2019. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions PG_EXTN_C_WITH_RUST.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@

    1. Create new project Cargo embed
    2. Rename the file embed/src/main.rs to lib.rs
    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
    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. Makefile
    4. Create a file called Makefile in same folder
    ```
    MODULES = funcs

  7. ranjanprj revised this gist Nov 7, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions PG_EXTN_C_WITH_RUST.md
    Original 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
  8. ranjanprj renamed this gist Nov 7, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. ranjanprj created this gist Nov 7, 2019.
    68 changes: 68 additions & 0 deletions gistfile1.txt
    Original 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);
    ```