- Notifications
You must be signed in to change notification settings - Fork22
thehydroimpulse/postgres-extension.rs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Library to write Postgres extensions in Rust! Overall, the Postgres codebase has some pretty narly C macroseverywhere, so we have to work around them a little bit.
This library doesn't make any huge assumptions on the version of Postgres that is needed. Each extension specifies a version of postgres that it's compatible with, such as90500
. Postgres does some compatibility checks on the shared library before using it.
This library is fully compatible with Cargo! Just include the two crates that are needed (one for the actual library and one for macros) and you'll be good to go!
# Cargo.toml[package]name ="is_zero"version ="0.0.1"authors = ["Daniel Fagnan <dnfagnan@gmail.com>"][lib]name ="is_zero"crate-type = ["dylib"][dependencies.postgres_extension]path ="https://github.com/thehydroimpulse/postgres-extension.rs"[dependencies.postgres_extension_macros]path ="https://github.com/thehydroimpulse/postgres-extension.rs"
Let's create a simple, "hello world"-like extension.
The first task is to link in the appropriate crates that we need:
// lib.rs#![feature(phase)]externcrate postgres_extension;#[phase(plugin)]externcrate postgres_extension_macros;
Thephase
feature allows us to specify when to link the specified crate (compile-time? run-time?).
The reason we need two crates is because syntax extensions need to link againstrustc
andlibsyntax
, the Rust compiler and parser (among other things), respectively. These are both fairly big crates and we only have acompile-time requirement on them. Meaning, when we run our program (or whatever final output we have) we never ever need access to those compiler crates.
As a result, we'll use thephase
feature to selectively choose to only link the macro crate during compilation andnot during runtime.
Compatibility Checks:
As I mentioned above, Postgres does compatibility check on the loaded shared library. If these do not match, it won't load. Postgers has it's ownPG_MODULE_MAGIC
C macro that handles this automatically. Luckily, we have our ownpg_module!
macro in Rust.
So, continuing from the previous code we wrote:
// lib.rs// ...pg_module!(version:90500)
We're just specifying that this extension is compatible with Postgres 9.5, that's it!
Exporting Functions:
Next up is being able to export a function to Postgres. This is done through thepg_export
attribute that can be placed on any function.
// lib.rs// ...#[pg_export]pubfnis_zero(a:i32) ->i32{if a ==0{return1;}else{return0;}}
Importing Into Postgres:
Simply runpsql
(with whatever options you need/want).
CREATEFUNCTIONis_zero(int4) RETURNSBooleanAS'/path/to/target/libis_zero-*.dylib' LANGUAGE c;
Replacing the path with the real location to thedylib
, of course.
Now we can use the extension we just wrote in Rust within a SQL statement. Again inpsql
, we can do:
select is_zero(1);
And you should get something like:
postgres=# select is_zero(1); is_zero--------- f(1 row)
The MIT License (MIT)
Copyright (c) 2014 Daniel Fagnandnfagnan@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.
About
Postgres extensions written in Rust
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.