pgcentralfoundation / pgrx

Build Postgres Extensions with Rust!
Other
3.42k stars 223 forks source link

Question: how to create DSM using pgrx and launch parallel worker #1721

Open xingtanzjr opened 1 month ago

xingtanzjr commented 1 month ago

Hi all,

I am new to pgrx and cannot find some material regarding how to create DSM and launch parallel worker by pgrx.

My scenario is: I am trying to implement a index extension and is implementing ambuild(). When doing that, I'd like to make the build stage in a parallel way. Thus, I need to create the DSM and share it in several parallel workers. But I didn't find corresponding doc or example about how to do that.

I noticed some example in some website such as:

let dsm_handle = Spi::get_current_subtransaction_dsm_handle().unwrap();
let dsm = Spi::attach_dynamic_shared_memory(dsm_handle).unwrap();

let shared_data = Arc::new(Mutex::new(0)); 
dsm.store("shared_data", shared_data);

But it seems that there is no such method in pgrx 0.11.4.

The other way is use pg_sys to leverage all internal method defined by C. But it seems very complicated in that way.

Could you please help to share an example ? Appreciate your any input ! Thank you!

eeeebbbbrrrr commented 1 month ago

Hi!

We currently do not have safe wrappers around the DSM internals. Your intuition is correct that you'll need to use pg_sys directly. It may also be that we don't include all the necessary headers, so you may need to add those to pgrx as well -- we'll happily accept and merge PRs for that! :)

xingtanzjr commented 1 month ago

Thank you @eeeebbbbrrrr , I will do some investigation and try to make a PR when necessary. Thanks for your quick response again

xingtanzjr commented 1 month ago

By the way, is the example I mentioned just made up online, or did it actually exist?