diesel_pgrx

Crates.iodiesel_pgrx
lib.rsdiesel_pgrx
version0.1.0
created_at2025-11-04 09:51:17.354727+00
updated_at2025-11-04 09:51:17.354727+00
descriptionDiesel ORM integration for PGRX PostgreSQL extensions using binary protocol
homepage
repositoryhttps://github.com/earth-metabolome-initiative/emi-monorepo
max_upload_size
id1916054
size83,678
Luca Cappelletti (LucaCappelletti94)

documentation

README

Diesel PGRX

Clippy Test

Integration layer enabling Diesel ORM to work with custom Rust types defined in PGRX PostgreSQL extensions. The crate provides a derive macro that implements Diesel's serialization traits (ToSql and FromSql) using PGRX's binary protocol, which internally uses CBOR serialization via serde_cbor. This allows types decorated with PGRX's PostgresType and #[pg_binary_protocol] to be seamlessly queried through Diesel. Additional backend support includes SQLite using the same CBOR serialization format.

Usage

Add this to your Cargo.toml:

[dependencies]
diesel_pgrx = "0.1"
diesel = { version = "2.2", features = ["postgres"] }
serde = { version = "1.0", features = ["derive"] }

After building your PGRX extension, install it in PostgreSQL:

CREATE EXTENSION your_extension;

Derive DieselPGRX for types that implement serde::Serialize and serde::Deserialize. The type must also use PGRX's PostgresType and pg_binary_protocol attributes:

#[derive(
    Debug,
    serde::Serialize,
    serde::Deserialize,
    diesel_pgrx::DieselPGRX,
    diesel::FromSqlRow,
    diesel::AsExpression
)]
#[cfg_attr(feature = "pgrx", derive(pgrx::PostgresType))]
#[cfg_attr(feature = "pgrx", pg_binary_protocol)]
#[diesel(sql_type = diesel_impls::MyCustomType)]
pub struct MyCustomType {
    pub field: i32,
}

Additional PGRX Derives

PGRX provides optional derives for comparison and hashing operations. When using these, implement the corresponding Rust traits:

#[derive(serde::Serialize, serde::Deserialize, diesel_pgrx::DieselPGRX)]
#[cfg_attr(
    feature = "pgrx",
    derive(pgrx::PostgresType, pgrx::PostgresEq, pgrx::PostgresOrd, pgrx::PostgresHash)
)]
#[cfg_attr(feature = "pgrx", pg_binary_protocol)]
#[derive(
    Debug,
    Copy,
    Clone,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    diesel::FromSqlRow,
    diesel::AsExpression,
)]
#[diesel(sql_type = diesel_impls::MyCustomType)]
pub struct MyCustomType {
    pub field: i32,
}

Feature Flags

  • postgres (not default): Enables PostgreSQL backend support with binary protocol serialization
  • sqlite (not default): Enables SQLite backend support using CBOR-encoded BINARY type
  • pgrx: Used for documentation tests only

Note on serde_cbor

The serde_cbor crate is archived but remains the CBOR implementation used by PGRX. No known issues exist, and it will be used until PGRX adopts an alternative.

Commit count: 0

cargo fmt