1 unstable release
Uses new Rust 2024
| 0.1.0 | Nov 4, 2025 |
|---|
#2119 in Database interfaces
Used in 2 crates
25KB
Diesel PGRX
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 serializationsqlite(not default): Enables SQLite backend support using CBOR-encoded BINARY typepgrx: 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.
Dependencies
~6–19MB
~351K SLoC