Skip to content

Commit

Permalink
Make non-updating queries use readonly unless there was a prior mut…
Browse files Browse the repository at this point in the history
…ation (#963)
  • Loading branch information
yrashk authored Dec 21, 2022
1 parent e503932 commit e6135a9
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 70 deletions.
2 changes: 1 addition & 1 deletion pgx-examples/schemas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ mod tests {

#[pg_test]
fn test_my_some_schema_type() {
Spi::connect(|c| {
Spi::connect(|mut c| {
// "MySomeSchemaType" is in 'some_schema', so it needs to be discoverable
c.update("SET search_path TO some_schema,public", None, None);
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions pgx-tests/src/tests/bgworker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub extern "C" fn bgworker(arg: pg_sys::Datum) {
if arg > 0 {
BackgroundWorker::transaction(|| {
Spi::run("CREATE TABLE tests.bgworker_test (v INTEGER);");
Spi::connect(|client| {
Spi::connect(|mut client| {
client.update(
"INSERT INTO tests.bgworker_test VALUES ($1);",
None,
Expand Down Expand Up @@ -71,7 +71,7 @@ pub extern "C" fn bgworker_return_value(arg: pg_sys::Datum) {
};
while BackgroundWorker::wait_latch(Some(Duration::from_millis(100))) {}
BackgroundWorker::transaction(|| {
Spi::connect(|c| {
Spi::connect(|mut c| {
c.update(
"INSERT INTO tests.bgworker_test_return VALUES ($1)",
None,
Expand Down
28 changes: 22 additions & 6 deletions pgx-tests/src/tests/spi_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod tests {

#[pg_test]
fn test_inserting_null() -> Result<(), pgx::spi::Error> {
Spi::connect(|client| {
Spi::connect(|mut client| {
client.update("CREATE TABLE tests.null_test (id uuid)", None, None);
});
assert_eq!(
Expand All @@ -202,7 +202,7 @@ mod tests {

#[pg_test]
fn test_cursor() {
Spi::connect(|client| {
Spi::connect(|mut client| {
client.update("CREATE TABLE tests.cursor_table (id int)", None, None);
client.update(
"INSERT INTO tests.cursor_table (id) \
Expand All @@ -224,7 +224,7 @@ mod tests {

#[pg_test]
fn test_cursor_prepared_statement() -> Result<(), pgx::spi::Error> {
Spi::connect(|client| {
Spi::connect(|mut client| {
client.update("CREATE TABLE tests.cursor_table (id int)", None, None);
client.update(
"INSERT INTO tests.cursor_table (id) \
Expand All @@ -248,7 +248,7 @@ mod tests {

#[pg_test]
fn test_cursor_by_name() -> Result<(), pgx::spi::Error> {
let cursor_name = Spi::connect(|client| {
let cursor_name = Spi::connect(|mut client| {
client.update("CREATE TABLE tests.cursor_table (id int)", None, None);
client.update(
"INSERT INTO tests.cursor_table (id) \
Expand Down Expand Up @@ -308,7 +308,7 @@ mod tests {
assert_eq!(res.column_name(2).unwrap(), "b");
});

Spi::connect(|client| {
Spi::connect(|mut client| {
let res = client.update("SET TIME ZONE 'PST8PDT'", None, None);

assert_eq!(0, res.columns());
Expand All @@ -324,7 +324,7 @@ mod tests {
#[pg_test]
fn test_spi_non_mut() -> Result<(), pgx::spi::Error> {
// Ensures update and cursor APIs do not need mutable reference to SpiClient
Spi::connect(|client| {
Spi::connect(|mut client| {
client.update("SELECT 1", None, None);
let cursor = client.open_cursor("SELECT 1", None)?.detach_into_name();
client.find_cursor(&cursor).map(|_| ())
Expand Down Expand Up @@ -413,4 +413,20 @@ mod tests {
fn test_option() {
assert!(Spi::get_one::<i32>("SELECT NULL::integer").unwrap().is_none());
}

#[pg_test(error = "CREATE TABLE is not allowed in a non-volatile function")]
fn test_readwrite_in_readonly() {
// This is supposed to run in read-only
Spi::connect(|client| client.select("CREATE TABLE a ()", None, None));
}

#[pg_test]
fn test_readwrite_in_select_readwrite() {
Spi::connect(|mut client| {
// This is supposed to switch connection to read-write and run it there
client.update("CREATE TABLE a (id INT)", None, None);
// This is supposed to run in read-write
client.select("INSERT INTO a VALUES (1)", None, None);
});
}
}
4 changes: 2 additions & 2 deletions pgx-tests/src/tests/srf_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod tests {

#[pg_test]
fn test_srf_setof_datum_detoasting_with_borrow() {
let cnt = Spi::connect(|client| {
let cnt = Spi::connect(|mut client| {
// build up a table with one large column that Postgres will be forced to TOAST
client.update("CREATE TABLE test_srf_datum_detoasting AS SELECT array_to_string(array_agg(g),' ') s FROM (SELECT 'a' g FROM generate_series(1, 1000000)) x;", None, None);

Expand All @@ -195,7 +195,7 @@ mod tests {

#[pg_test]
fn test_srf_table_datum_detoasting_with_borrow() {
let cnt = Spi::connect(|client| {
let cnt = Spi::connect(|mut client| {
// build up a table with one large column that Postgres will be forced to TOAST
client.update("CREATE TABLE test_srf_datum_detoasting AS SELECT array_to_string(array_agg(g),' ') s FROM (SELECT 'a' g FROM generate_series(1, 1000000)) x;", None, None);

Expand Down
2 changes: 1 addition & 1 deletion pgx-tests/src/tests/struct_type_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ mod tests {

#[pg_test]
fn test_complex_storage_and_retrieval() -> Result<(), pgx::spi::Error> {
let complex = Spi::connect(|client| {
let complex = Spi::connect(|mut client| {
client.update(
"CREATE TABLE complex_test AS SELECT s as id, (s || '.0, 2.0' || s)::complex as value FROM generate_series(1, 1000) s;\
SELECT value FROM complex_test ORDER BY id;", None, None).first().get_one::<PgBox<Complex>>()
Expand Down
Loading

0 comments on commit e6135a9

Please sign in to comment.