Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Postgres extensions written in Rust

NotificationsYou must be signed in to change notification settings

thehydroimpulse/postgres-extension.rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

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.

Getting Started

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"

Hello World!

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_MAGICC 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)

License

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp