GithubHelp home page GithubHelp logo

jim-mlodgenski / plrust Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tcdi/plrust

0.0 0.0 0.0 90 KB

A Rust procedural language handler for PostgreSQL

License: PostgreSQL License

Rust 69.42% Nix 30.58%

plrust's Introduction

plrust Extension for PostgreSQL

CI status

Support for plrust in PostgreSQL functions.

CREATE EXTENSION IF NOT EXISTS plrust;
CREATE OR REPLACE FUNCTION sum_array(a BIGINT[]) RETURNS BIGINT
    IMMUTABLE STRICT
    LANGUAGE PLRUSTU AS
$$
[dependencies]
    # Add Cargo.toml dependencies here.
[code]
    Some(a.into_iter().map(|v| v.unwrap_or_default()).sum())
$$;
SELECT sum_array(ARRAY[1,2,3]);
/*
sum_array
----------------
              6
(1 row)
*/

Installation

Ensure cargo pgx installed & initialized:

cargo install cargo-pgx
cargo pgx init

Then, add some configuration to the postgresql.conf and ensure there is a writable work_dir:

cat <<-EOF >> ~/.pgx/data-12/postgresql.conf
  plrust.pg_config = '/home/${USER}/.pgx/12.6/pgx-install/bin/pg_config'
  plrust.work_dir = '/home/${USER}/git/zombodb/plrust/scratch'
EOF
mkdir -p scratch
chmod -R 777 scratch

It's possible to test debug builds inside a working PostgreSQL with:

cargo pgx run pg12

The output should resemble:

$ cargo pgx run pg12
    Stopping Postgres v12
building extension with features `pg12`
"cargo" "build" "--features" "pg12" "--no-default-features"
    Finished dev [unoptimized + debuginfo] target(s) in 0.56s

installing extension
     Copying control file to `/home/nixos/.pgx/12.6/pgx-install/share/postgresql/extension/plrust.control`
     Copying shared library to `/home/nixos/.pgx/12.6/pgx-install/lib/postgresql/plrust.so`
     Writing extension schema to `/home/nixos/.pgx/12.6/pgx-install/share/postgresql/extension/plrust--1.0.sql`
    Finished installing plrust
    Starting Postgres v12 on port 28812
    Re-using existing database plrust
psql (12.6)
Type "help" for help.

plrust=# \dx
                 List of installed extensions
  Name   | Version |   Schema   |         Description
---------+---------+------------+------------------------------
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(1 row)

plrust=# CREATE EXTENSION IF NOT EXISTS plrust;
CREATE EXTENSION
plrust=# \dx
                 List of installed extensions
  Name   | Version |   Schema   |         Description
---------+---------+------------+------------------------------
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
 plrust  | 1.0     | plrust     | plrust:  Created by pgx
(2 rows)

To install it to the locally running PostgreSQL server:

cargo pgx install

Creating a distributable

cargo pgx package

The output should resemble the following:

$ cargo pgx package
building extension with features `pg12`
"cargo" "build" "--release" "--features" "pg12" "--no-default-features"
   Compiling pgx-pg-sys v0.1.19 (/git/zombodb/pgx/pgx-pg-sys)
   Compiling pgx v0.1.19 (/git/zombodb/pgx/pgx)
   Compiling plrust v0.0.0 (/git/zombodb/plrust)
    Finished release [optimized] target(s) in 52.76s

installing extension
     Copying control file to `target/release/plrust-pg12/usr/share/postgresql/12/extension/plrust.control`
     Copying shared library to `target/release/plrust-pg12/usr/lib/postgresql/12/lib/plrust.so`
     Writing extension schema to `target/release/plrust-pg12/usr/share/postgresql/12/extension/plrust--1.0.sql`
    Finished installing plrust

The directory tree inside target/release/plrust-pg12/ contains a tree corresponding to the local where the extension should be placed.

From here, you can use an archive or a tool like fpm to distribute the artifact to the target.

Once installed in the right directory, connect via psql:

psql=# CREATE EXTENSION IF NOT EXISTS plrust;
CREATE EXTENSION
psql=# \dx
                 List of installed extensions
  Name   | Version |   Schema   |         Description          
---------+---------+------------+------------------------------
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
 plrust  | 1.0     | plrust     | plrust:  Created by pgx
(2 rows)

Options

There are two postgresql.conf settings that must be configured:

Option Description
plrust.pg_config The location of the postgresql.conf.
plrust.work_dir The directory where pl/rust will build functions with cargo.

plrust's People

Contributors

hoverbear avatar eeeebbbbrrrr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.