pgcentralfoundation / pgrx

Build Postgres Extensions with Rust!
Other
3.7k stars 249 forks source link

how to test GucContext::SuBackend #1906

Open mrl5 opened 1 month ago

mrl5 commented 1 month ago

hello, let's assume that I have this GUC initialization logic:

use pgrx::*;
use std::ffi::CStr;

pub static MY_FOO_RUNTIME_PARAM: &str = "my.foo";
pub static MY_FOO: GucSetting<Option<&'static CStr>> = GucSetting::<Option<&'static CStr>>::new(None);

pub fn init() {
    GucRegistry::define_string_guc(
        MY_FOO_RUNTIME_PARAM,
        "My Foo",
        "That can be FooBar",
        &MY_FOO,
        GucContext::SuBackend,
        GucFlags::NOT_WHILE_SEC_REST
    );
}
  1. How can I properly set it for unit tests? I've tried with some util function that works for GucContext::Suset:
    fn set_my_foo_in_guc(value: String) {
    Spi::run(format!("SET {} = '{}'", MY_FOO_RUNTIME_PARAM, value).as_str()).unwrap();
    }

    but - as expected - I'm getting

    Client Error:
    parameter "my.foo" cannot be set after connection start
    postgres location: guc.c
  2. For manual tests, how can set this GUC param in cargo pgrx run pg16?
workingjubilee commented 1 month ago

Uhh, I believe that this is impossible to unit test with pg_test as it currently is because pg_test runs tests in transactions, which means it can only test things that work if you run the test against the same backend in two different transactions. It has been a while since I looked at the test code so I may be misremembering slightly.

workingjubilee commented 1 month ago

It seems plausible that pgrx-tests could be taught how to run tests in connections and thus, if I understand correctly, test this by opening multiple connections, right? But this would require Effort, as it would then have to learn not only the connection management... it might even know that already... but it would also need to be taught how to set the configuration on initial connection for the test.

workingjubilee commented 1 month ago

As for manual testing, I suppose you can simply set it in the configuration file? That's what I usually have done.

mrl5 commented 1 month ago

with manual test I was able to make it running with:

export PGOPTIONS="-c my.foo=bar"
cargo pgrx run pg16
SHOW my.foo;
 my.foo 
--------
 bar
(1 row)

with unit tests

export PGOPTIONS="-c my.foo=bar"
cargo test

it seems to be missing but I'm still not 100% sure

workingjubilee commented 1 month ago

I don't believe we pass PGOPTIONS on to the postgres process.

workingjubilee commented 1 month ago

Actually it seems it's not on the list of env vars we strip out. Weird. Should work, then.

workingjubilee commented 1 month ago

this is where we start up postgres: https://github.com/pgcentralfoundation/pgrx/blob/c9e218bfc4503f279bd342949ff2fbf65f9f6451/pgrx-tests/src/framework.rs#L552-L571