pgcentralfoundation / pgrx

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

RelationIdCache is NULL when call table_open() in background worker #1728

Open xingtanzjr opened 1 month ago

xingtanzjr commented 1 month ago

Hi there,

I am trying to open a table by Oid in a background worker. But it encountered segmentation fault when trying to open the table.

Here is my code snip

#[pg_guard]
#[no_mangle]
unsafe extern "C" fn parallel_worker_build_index(arg: pg_sys::Datum) {

    // convert Datum to dsm_handle
    let dsm_handle = arg.value() as u32;

    let dsm_segment = pg_sys::dsm_attach(dsm_handle);
    if dsm_segment.is_null() {
        log!("dsm_segment is null");
    }

    let build_state = pg_sys::dsm_segment_address(dsm_segment) as *mut BuildState;

    let heapreloid = (*build_state).heapreloid;
    let indexreloid = (*build_state).indexreloid;

    pg_sys::StartTransactionCommand();
    let heaprel = pg_sys::table_open(heapreloid, pg_sys::AccessShareLock as i32);
    log!("parallel_worker heaprel opened");

I tried to debug it and found that the error occurs here: image

It seems that the RelationIdCache is NULL image

Is someone met the similar issue before ? thanks for your input!

workingjubilee commented 1 month ago

Are you testing the code against a pgrx-built database? One with assertions enabled? That should have caught this behavior.

xingtanzjr commented 1 month ago

@workingjubilee Yes. I install this extension using cargo pgrx install and start the postgres like this

Cargo pgrx start pg15
Cargo pgrx connect pg15