Skip to content

Commit 69f9ff9

Browse files
authored
Postgres: force generic plan for better nullability inference. (#3541)
* Postgres: force generic plan when describing statement with args * cargo fmt * Postgres: set plan_cache_mode in transaction * Postgres: only set force_generic_plan for versions that support it * Postgres: reduce database calls in query macro's * Postgres: set generic execution plan only in query macro
1 parent 2fd3dab commit 69f9ff9

File tree

1 file changed

+21
-1
lines changed
  • sqlx-macros-core/src/database

1 file changed

+21
-1
lines changed

sqlx-macros-core/src/database/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,27 @@ impl<DB: DatabaseExt> CachingDescribeBlocking<DB> {
5252
let conn = match cache.entry(database_url.to_string()) {
5353
hash_map::Entry::Occupied(hit) => hit.into_mut(),
5454
hash_map::Entry::Vacant(miss) => {
55-
miss.insert(DB::Connection::connect(database_url).await?)
55+
let conn = miss.insert(DB::Connection::connect(database_url).await?);
56+
57+
#[cfg(feature = "postgres")]
58+
if DB::NAME == sqlx_postgres::Postgres::NAME {
59+
conn.execute(
60+
"
61+
DO $$
62+
BEGIN
63+
IF EXISTS (
64+
SELECT 1
65+
FROM pg_settings
66+
WHERE name = 'plan_cache_mode'
67+
) THEN
68+
SET SESSION plan_cache_mode = 'force_generic_plan';
69+
END IF;
70+
END $$;
71+
",
72+
)
73+
.await?;
74+
}
75+
conn
5676
}
5777
};
5878

0 commit comments

Comments
 (0)