All Projects â†’ wasmerio â†’ Wasmer Postgres

wasmerio / Wasmer Postgres

Licence: mit
💽🕸 Postgres library to run WebAssembly binaries.

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Wasmer Postgres

Plv8
V8 Engine Javascript Procedural Language add-on for PostgreSQL
Stars: ✭ 1,195 (+387.76%)
Mutual labels:  postgresql, postgres, postgresql-extension
Pg auto failover
Postgres extension and service for automated failover and high-availability
Stars: ✭ 564 (+130.2%)
Mutual labels:  postgresql, postgres, postgresql-extension
Bgworker
Background Worker Processes for PostgreSQL written in Go
Stars: ✭ 77 (-68.57%)
Mutual labels:  postgresql, postgres, postgresql-extension
Pgx
Build Postgres Extensions with Rust!
Stars: ✭ 903 (+268.57%)
Mutual labels:  postgresql, postgres, postgresql-extension
Pg hashids
Short unique id generator for PostgreSQL, using hashids
Stars: ✭ 164 (-33.06%)
Mutual labels:  postgresql, postgres, postgresql-extension
Vue
The progressive framework for WebAssembly applications.
Stars: ✭ 211 (-13.88%)
Mutual labels:  webassembly, wasm
Wasm Worker
Move a WebAssembly module into its own thread
Stars: ✭ 215 (-12.24%)
Mutual labels:  webassembly, wasm
Wapm Cli
📦 WebAssembly Package Manager (CLI)
Stars: ✭ 236 (-3.67%)
Mutual labels:  webassembly, wasm
Pguri
uri type for PostgreSQL
Stars: ✭ 235 (-4.08%)
Mutual labels:  postgresql, postgresql-extension
Sandspiel
Creative cellular automata browser game
Stars: ✭ 2,476 (+910.61%)
Mutual labels:  webassembly, wasm
Asm Dom
A minimal WebAssembly virtual DOM to build C++ SPA (Single page applications)
Stars: ✭ 2,604 (+962.86%)
Mutual labels:  webassembly, wasm
Rpostgres
A DBI-compliant interface to PostgreSQL
Stars: ✭ 245 (+0%)
Mutual labels:  postgresql, postgres
Tds fdw
A PostgreSQL foreign data wrapper to connect to TDS databases (Sybase and Microsoft SQL Server)
Stars: ✭ 238 (-2.86%)
Mutual labels:  postgresql, postgresql-extension
Spring Boot Postgresql Jpa Hibernate Rest Api Demo
Building RESTful APIs with Spring Boot, PostgreSQL, JPA and Hibernate
Stars: ✭ 209 (-14.69%)
Mutual labels:  postgresql, postgres
Pont
An online board game in Rust and WebAssembly
Stars: ✭ 218 (-11.02%)
Mutual labels:  webassembly, wasm
Artichoke
💎 Artichoke is a Ruby made with Rust
Stars: ✭ 2,557 (+943.67%)
Mutual labels:  webassembly, wasm
Wasm By Example
Wasm By Example is a website with a set of hands-on introduction examples and tutorials for WebAssembly (Wasm)
Stars: ✭ 226 (-7.76%)
Mutual labels:  webassembly, wasm
Massive Js
A data mapper for Node.js and PostgreSQL.
Stars: ✭ 2,521 (+928.98%)
Mutual labels:  postgresql, postgres
Sqliterally
Lightweight SQL query builder
Stars: ✭ 231 (-5.71%)
Mutual labels:  postgresql, postgres
Sql Lint
An SQL linter
Stars: ✭ 243 (-0.82%)
Mutual labels:  postgresql, postgres

Wasmer logo Wasmer Postgres Wasmer Slack Channel

A complete and mature WebAssembly runtime for Postgres based on Wasmer. It's an original way to extend your favorite database capabilities.

Features:

  • Easy to use: The wasmer API mimics the standard WebAssembly API,
  • Fast: wasmer executes the WebAssembly modules as fast as possible, close to native speed,
  • Safe: All calls to WebAssembly will be fast, but more importantly, completely safe and sandboxed.

Note: The project is still in heavy development. This is a 0.1.0 version. Some API are missing and are under implementation. But it's fun to play with it.

Installation

The project comes in two parts:

  1. A shared library, and
  2. A PL/pgSQL extension.

To compile the former, run just build (Postgres server headers are required, see pg_config --includedir-server). To install the latter, run just install. After that, run CREATE EXTENSION wasm in a Postgres shell. A new function will appear: wasm_init; it must be called with the absolute path to the shared library. It looks like this:

$ # Build the shared library.
$ just build

$ # Install the extension in the Postgres tree.
$ just install

$ # Activate and initialize the extension.
$ just host=$host database=$database activate

And you are ready to go!

Note: On macOS, the shared library extension is .dylib, on Windows, it is .dll, and on other distributions, it is .so.

Note 2: Yes, you need just.

Supported Postgres versions

So far, the extension works on Postgres 10 only. It doesn't work with Postgres 11 yet (follow this issue if you want to learn more). Any help is welcomed!

Usage & documentation

Consider the examples/simple.rs program:

#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
    x + y
}

Once compiled to WebAssembly, one obtains a similar WebAssembly binary to examples/simple.wasm (download it). To use the sum exported function, first, create a new instance of the WebAssembly module, and second, call the sum function.

To instantiate a WebAssembly module, the wasm_new_instance function must be used. It has two arguments:

  1. The absolute path to the WebAssembly module, and
  2. A namespace used to prefix exported functions in SQL.

For instance, calling wasm_new_instance('/path/to/simple.wasm', 'ns') will create the ns_sum function that is a direct call to the sum exported function of the WebAssembly instance. Thus:

-- New instance of the `simple.wasm` WebAssembly module.
SELECT wasm_new_instance('/absolute/path/to/simple.wasm', 'ns');

-- Call a WebAssembly exported function!
SELECT ns_sum(1, 2);

--  ns_sum
-- --------
--       3
-- (1 row)

Isn't it awesome? Calling Rust from Postgres through WebAssembly!

Let's inspect a little bit further the ns_sum function:

\x
\df+ ns_sum
Schema              | public
Name                | ns_sum
Result data type    | integer
Argument data types | integer, integer
Type                | normal
Volatility          | volatile
Parallel            | unsafe
Owner               | …
Security            | invoker
Access privileges   |
Language            | plpgsql
Source code         | …
Description         |

The Postgres ns_sum signature is (integer, integer) -> integer, which maps the Rust sum signature (i32, i32) -> i32.

So far, only the WebAssembly types i32, i64 and v128 are supported; they respectively map to integer, bigint and decimal in Postgres. Floats are partly implemented for the moment.

Inspect a WebAssembly instance

The extension provides two foreign data wrappers, gathered together in the wasm foreign schema:

  • wasm.instances is a table with the id and wasm_file columns, respectively for the instance ID, and the path of the WebAssembly module,
  • wasm.exported_functions is a table with the instance_id, name, inputs and output columns, respectively for the instance ID of the exported function, its name, its input types (already formatted for Postgres), and its output types (already formatted for Postgres).

Let's see:

-- Select all WebAssembly instances.
SELECT * FROM wasm.instances;

--                   id                  |          wasm_file
-- --------------------------------------+-------------------------------
--  426e17af-c32f-5027-ad73-239e5450dd91 | /absolute/path/to/simple.wasm
-- (1 row)

-- Select all exported functions for a specific instance.
SELECT
    name,
    inputs,
    outputs
FROM
    wasm.exported_functions
WHERE
    instance_id = '426e17af-c32f-5027-ad73-239e5450dd91';

--   name  |     inputs      | outputs
-- --------+-----------------+---------
--  ns_sum | integer,integer | integer
-- (1 row)

Benchmarks

Benchmarks are useless most of the time, but it shows that WebAssembly can be a credible alternative to procedural languages such as PL/pgSQL. Please, don't take those numbers for granted, it can change at any time, but it shows promising results:

Benchmark Runtime Time (ms) Ratio
Fibonacci (n = 50) postgres-ext-wasm 0.206 1×
PL/pgSQL 0.431 2×
Fibonacci (n = 500) postgres-ext-wasm 0.217 1×
PL/pgSQL 2.189 10×
Fibonacci (n = 5000) postgres-ext-wasm 0.257 1×
PL/pgSQL 18.643 73×

Test

Once the library is built, run the following commands:

$ just pg-start
$ just test

License

The entire project is under the MIT License. Please read the LICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].