Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pgrx-examples/hooks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use pgrx::prelude::*;

::pgrx::pg_module_magic!(name, version);

fn knock_knock() {
todo!()
}

fn delete_must_have_a_where(query: PgBox<pg_sys::Query>) {
if query.commandType != pg_sys::CmdType::CMD_DELETE {
return ();
Expand All @@ -33,6 +37,25 @@ fn only_superusers_can_truncate(pstmt: PgBox<pg_sys::PlannedStmt>) {
}

unsafe fn register_hooks() {

//
// Client Authentication hook
//
static mut PREV_CLIENTAUTHENTICATION_HOOK: pg_sys::libpq::ClientAuthentication_hook_type = None;
PREV_CLIENTAUTHENTICATION_HOOK = pg_sys::libpq::ClientAuthentication_hook;
pg_sys::libpq::ClientAuthentication_hook = Some(client_authentication_hook);

#[pg_guard]
unsafe extern "C-unwind" fn client_authentication_hook(
port: *mut pg_sys::libpq::be::Port,
status: i32,
) {
knock_knock();
if let Some(prev_hook) = PREV_CLIENTAUTHENTICATION_HOOK {
pg_guard_ffi_boundary(|| prev_hook(port, status));
}
}

//
// Post Parse Analyze hook
//
Expand Down
10 changes: 10 additions & 0 deletions pgrx-pg-sys/src/libpq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,13 @@ pub mod be {
raw_buf_remaining: isize,
}
}

// The ClientAuthentication Hook cannot be implemented simply with bindgen
/// Hook type to get control in ClientAuthentication()
pub type ClientAuthentication_hook_type =
Option<unsafe extern "C-unwind" fn(port: *mut be::Port, status: i32)>;

#[pgrx_macros::pg_guard]
unsafe extern "C-unwind" {
pub static mut ClientAuthentication_hook: ClientAuthentication_hook_type;
}
Loading