Replies: 3 comments 2 replies
-
An even superior solution would be to replicate what https://github.com/jackc/pgx does: the prepared statement cache is an LRU cache with a configurable size ( |
Beta Was this translation helpful? Give feedback.
-
Thanks for opening this discussion. For future reference, I'm able to recreate that problem by using the following code: #[test]
fn statement_cache() {
let conn = &mut crate::test_helpers::pg_connection();
table! {
users {
id -> Integer,
name -> Text,
}
}
diesel::sql_query("CREATE TABLE users(id SERIAL PRIMARY KEY, name VARCHAR(20) NOT NULL)")
.execute(conn)
.unwrap();
// execute it one to cache the statement
let r = users::table.load::<(i32, String)>(conn).unwrap();
assert!(r.is_empty());
// change the type
diesel::sql_query("ALTER TABLE users ALTER COLUMN name TYPE TEXT")
.execute(conn)
.unwrap();
// execute it again to break the cache
let r = users::table.load::<(i32, String)>(conn).unwrap();
assert!(r.is_empty());
} The second query then fails with Now with that written, first let me elaborate additional ways to workaround this specific issue in your application:
That said these are workarounds that would be applied now, without patching diesel. I think the general issue is something that we might want to resolve at some point, ideally without affecting performance for those that want to use the prepared statement cache. I'm not sure if that's possible by changing it to a LRU cache as we would then need to somehow clean the cache at some point, which is just additional work. |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot @weiznich for your time looking into this issue, writing this code snippet to reproduce it, and contributing to the discussion. About the workarounds:
We use
Thanks for pointing this solution and providing a link to the relevant documentation. I will look into this possibility, as this could be a better workaround than the patch we use at the moment.
Yes, this would solve just this problem, without any other downsides. We'll look into this solution. |
Beta Was this translation helpful? Give feedback.
-
I'm encountering an issue while doing schema changes on Postgres:
Some examples of cases when this can happen:
vachar(30)
tovarchar
)It is currently complicated to perform these schema changes without breaking the connections. My understanding is that each connection holds a prepared statement cache. When some schema changes modify the signature of the prepared statements, Postgres refuses to execute previously cached prepared statements.
I've evaluated a few options:
Are there other options than these?
If this is in line with the vision of the maintainers, we'd like to contribute, by making the prepared statement cache optional (enabled by default).
Beta Was this translation helpful? Give feedback.
All reactions