Skip to content

Commit

Permalink
Support pgrx in cbdb(fix compile errors)
Browse files Browse the repository at this point in the history
pgrx(https://github.com/pgcentralfoundation/pgrx) is a framework for
developing PostgreSQL extensions in Rust.
it supports Postgres 12 through Postgres 17.

Cloudberry`s pg version is 14.4, so it is possible to enable
pgrx.

By doing this, we can build new extensions in Cloudberry with rust, and
import other extensions in rust(like iceberg-rust, and other datalake
components) to enrich the Cloudberry ecosystem.

Thie commit mainly fix the compile errors for cbdb, including:
1. add cbdb feature
2. ProcessInterrupts needs two arguments.
3. BackgroundWoker add missing bgw_start_rule field.
4. Pg_magic_struct version and product field.
5. MemoryContextDeleteImpl replace MemoryContextDelete
  • Loading branch information
roseduan authored and my-ship-it committed Nov 27, 2024
1 parent a0ecc8f commit b66581a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions pgrx-pg-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pg15 = []
pg16 = []
pg17 = []
cshim = []
cbdb = []

[package.metadata.docs.rs]
features = ["pg14", "cshim"]
Expand Down
17 changes: 17 additions & 0 deletions pgrx-pg-sys/src/submodules/elog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ pub fn interrupt_pending() -> bool {

/// If an interrupt is pending (perhaps a user-initiated "cancel query" message to this backend),
/// this will safely abort the current transaction
#[cfg(not(feature = "cbdb"))]
#[macro_export]
macro_rules! check_for_interrupts {
() => {
Expand All @@ -437,3 +438,19 @@ macro_rules! check_for_interrupts {
}
};
}

#[cfg(feature = "cbdb")]
#[macro_export]
macro_rules! check_for_interrupts {
() => {
#[allow(unused_unsafe)]
unsafe {
if $crate::InterruptPending != 0 {
$crate::ProcessInterrupts(
crate::memcxt::PgMemoryContexts::CurrentMemoryContext.pstrdup(file!()),
line!() as i32,
);
}
}
};
}
1 change: 1 addition & 0 deletions pgrx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pg17 = ["pgrx-pg-sys/pg17"]
no-schema-generation = ["pgrx-macros/no-schema-generation", "pgrx-sql-entity-graph/no-schema-generation"]
unsafe-postgres = [] # when trying to compile against something that looks like Postgres but claims to be different
nightly = [] # For features and functionality which require nightly Rust - for example, std::mem::allocator.
cbdb = [] # For features and functionality which require the Cloudberry.

[package.metadata.docs.rs]
features = ["pg14", "cshim"]
Expand Down
12 changes: 12 additions & 0 deletions pgrx/src/bgworkers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ pub struct BackgroundWorkerBuilder {
bgw_main_arg: pg_sys::Datum,
bgw_extra: String,
bgw_notify_pid: pg_sys::pid_t,
#[cfg(feature = "cbdb")]
bgw_start_rule: pg_sys::bgworker_start_rule,
shared_memory_startup_fn: Option<unsafe extern "C" fn()>,
}

Expand All @@ -456,6 +458,8 @@ impl BackgroundWorkerBuilder {
bgw_main_arg: pg_sys::Datum::from(0),
bgw_extra: "".to_string(),
bgw_notify_pid: 0,
#[cfg(feature = "cbdb")]
bgw_start_rule: None,
shared_memory_startup_fn: None,
}
}
Expand Down Expand Up @@ -586,6 +590,12 @@ impl BackgroundWorkerBuilder {
self
}

#[cfg(feature = "cbdb")]
pub fn set_start_rule(mut self, input: pg_sys::bgworker_start_rule) -> Self {
self.bgw_start_rule = input;
self
}

/// Once properly configured, call `load()` to get the BackgroundWorker registered and
/// started at the proper time by Postgres.
pub fn load(self) {
Expand Down Expand Up @@ -656,6 +666,8 @@ impl<'a> From<&'a BackgroundWorkerBuilder> for pg_sys::BackgroundWorker {
bgw_main_arg: builder.bgw_main_arg,
bgw_extra: RpgffiChar128::from(&builder.bgw_extra[..]).0,
bgw_notify_pid: builder.bgw_notify_pid,
#[cfg(feature = "cbdb")]
bgw_start_rule: builder.bgw_start_rule,
};

bgw
Expand Down
29 changes: 29 additions & 0 deletions pgrx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ macro_rules! pg_module_magic {
/// [Benjamin Fry]: https://github.com/bluejekyll/pg-extend-rs
/// [Daniel Fagnan]: https://github.com/thehydroimpulse/postgres-extension.rs
/// [Dynamic Loading]: https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-C-DYNLOAD
#[cfg(not(feature = "cbdb"))]
#[macro_export]
macro_rules! pg_magic_func {
() => {
Expand Down Expand Up @@ -268,6 +269,34 @@ macro_rules! pg_magic_func {
};
}

#[cfg(feature = "cbdb")]
#[macro_export]
macro_rules! pg_magic_func {
() => {
#[no_mangle]
#[allow(non_snake_case, unexpected_cfgs)]
#[doc(hidden)]
pub extern "C" fn Pg_magic_func() -> &'static ::pgrx::pg_sys::Pg_magic_struct {
static MY_MAGIC: ::pgrx::pg_sys::Pg_magic_struct = ::pgrx::pg_sys::Pg_magic_struct {
len: ::core::mem::size_of::<::pgrx::pg_sys::Pg_magic_struct>() as i32,
version: ::pgrx::pg_sys::GP_VERSION_NUM as i32 / 100,
funcmaxargs: ::pgrx::pg_sys::FUNC_MAX_ARGS as i32,
indexmaxkeys: ::pgrx::pg_sys::INDEX_MAX_KEYS as i32,
namedatalen: ::pgrx::pg_sys::NAMEDATALEN as i32,
float8byval: cfg!(target_pointer_width = "64") as i32,
product: ::pgrx::pg_sys::Pg_magic_product_code_PgMagicProductCloudberry as i32,
};

// since Postgres calls this first, register our panic handler now
// so we don't unwind into C / Postgres
::pgrx::pg_sys::panic::register_pg_guard_panic_hook();

// return the magic
&MY_MAGIC
}
};
}

pub(crate) static UTF8DATABASE: Lazy<Utf8Compat> = Lazy::new(|| {
use pg_sys::pg_enc::*;
let encoding_int = unsafe { pg_sys::GetDatabaseEncoding() };
Expand Down
16 changes: 16 additions & 0 deletions pgrx/src/memcxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,15 @@ impl Drop for OwnedMemoryContext {
if ptr::eq(pg_sys::CurrentMemoryContext, self.owned) {
pg_sys::CurrentMemoryContext = self.previous;
}
#[cfg(not(feature = "cbdb"))]
pg_sys::MemoryContextDelete(self.owned);
#[cfg(feature = "cbdb")]
pg_sys::MemoryContextDeleteImpl(
self.owned,
PgMemoryContexts::CurrentMemoryContext.pstrdup(file!()),
std::ptr::null(), // field `func` is not used in Cloudberry
line!() as i32,
);
}
}
}
Expand Down Expand Up @@ -394,7 +402,15 @@ impl PgMemoryContexts {
let result = PgMemoryContexts::exec_in_context(context, f);

unsafe {
#[cfg(not(feature = "cbdb"))]
pg_sys::MemoryContextDelete(context);
#[cfg(feature = "cbdb")]
pg_sys::MemoryContextDeleteImpl(
context,
PgMemoryContexts::CurrentMemoryContext.pstrdup(file!()),
std::ptr::null(), // field `func` is not used in Cloudberry
line!() as i32,
);
}

result
Expand Down

0 comments on commit b66581a

Please sign in to comment.