PGX: Write Postgres extensions in Rust instead of C

adamnemecek | 305 points

Guys, this is really really cool. Been looking to learn Rust and it just so happens I need to write a Postgres extension in the near future so two birds and one stone.

Btw - Also, I've seen ZomboDB and while I've not used it I must say it looks EXCELLENT. So congrats on that too.

jarym | 4 years ago

This is really cool! The only other similar thing I know of is WASMer's extension (which only supports Rust atm I believe):

https://github.com/wasmerio/postgres-ext-wasm

One question though: If you built and compiled an extension with this, how would you deploy it on top of a Postgres Docker image?

I see instructions for installing and testing locally, via the cargo command, but couldn't find anything on this.

Does it spit out a static asset that you put in the project folder, and copy over during the build pipeline? Something like:

  FROM postgres:12-alpine
  COPY ./my-rust-extension /some/container/directory
  RUN install-command-here
gavinray | 4 years ago

Interesting, it makes coding a postgres extension seem delightfully simple. I have been looking for something to try using rust on! Thanks for posting this.

faizshah | 4 years ago

Just out of curiosity, what sort of requirements compel a person to write a postgres extension? I know of things like PostGIS but I'm not well versed enough in this world to know the totality of the problem.

rukittenme | 4 years ago

This has been on my back burner for about a year now...glad someone has gotten to it. It already looks full featured. I've been thinking that with the right library subset (just like libcore or no_std), it would be a very safe possibility for a trusted language...accessible to users that are not superusers. That would be extremely valuable to RDS users that would love to use something fast, as opposed to the standard trusted languages like pl/python.

darksaints | 4 years ago

Recent similar thing in the Ada world: https://github.com/AdaCore/gnatcoll-db/tree/master/pgxs

touisteur | 4 years ago

Also of interest: rpgffi - https://github.com/posix4e/rpgffi

"R(Rust) PG(Postgresql) FFI (Foreign Function Interface)"

fnord123 | 4 years ago

At first I thought it is a Postgresql driver for Go that has the same name

https://github.com/jackc/pgx

3ace | 4 years ago

It would be awesome if someone with some Rust knowledge could make an extension to create a compressed string type like Antirez Smaz [1].

I know Postgres compresses TEXT types by default, but only when the data exceeds TOAST_TUPLE_THRESHOLD (default 2 kB)

Some examples on the project page [1] show reduction of size between 10% and more than 50% for some small strings (less than 100 bytes).

[1] https://github.com/antirez/smaz

giovannibonetti | 4 years ago
[deleted]
| 4 years ago
[deleted]
| 4 years ago

Is there any way to make custom types using pgx?

tcyrus | 4 years ago

Typo in the title: oc->of

ficklepickle | 4 years ago

Are there still other languages apart from Rust?

Is any of the daily rewrite-x-in-rust submissions used in production?

C is actually used in production ...

YalsXHAN | 4 years ago