pgcentralfoundation / pgrx

Build Postgres Extensions with Rust!
Other
3.56k stars 239 forks source link

Question: Hooks and executor_start #1057

Open andresrsanchez opened 1 year ago

andresrsanchez commented 1 year ago

Hi! I'm trying to replicate a common pattern in some Postgres extensions, for example something like:

static void
pgss_ExecutorStart(QueryDesc *queryDesc, int eflags)
{
    if (prev_ExecutorStart)
        prev_ExecutorStart(queryDesc, eflags);
    else
        standard_ExecutorStart(queryDesc, eflags);
// use queryDesc

I tried using prev_hook and standard_ExecutorStart but i don't understand fully the pgx flow of hooks, also I always end with errors with the borrow checker because of this:

fn executor_start(
                &mut self,
                query_desc: PgBox<QueryDesc>,
                eflags: i32,
                prev_hook: fn(PgBox<QueryDesc>, i32) -> HookResult<()>,
            ) -> HookResult<()> {
                prev_hook(query_desc, eflags)
                if query_desc.totaltime.is_null() { // value borrowed here after move blabla
.......
            }

If this is the correct place to ask, any help would be appreciate. Thanks!

ccleve commented 1 year ago

This is the best documentation on hooks that I've seen:

https://github.com/taminomara/psql-hooks

There is some decent sample code in C that you might be able to adapt.