You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT
enumlabel
FROMpg_catalog.pg_enumWHERE
enumtypid = $1ORDER BY
enumsortorder
These type validation queries significantly impact performance, especially with high DB RTTs:
Single query: ~8ms
With type checks: ~24ms (3x slower)
This runtime type checking creates unnecessary overhead in production systems where types are stable and tracked in code. I understand that this is required in many cases and is a good base setup, but allowing for disabling this functionality would greatly improve performance in many cases.
Proposed Solution
Add a configuration option to disable type checking queries, implemented either:
At build time through the query_as! macro system
At runtime via Pool settings (similar to test_before_acquire), e.g., validate_types_before_execution: bool
Attempted Workarounds
query_as_unchecked!()
Direct query!() with manual struct construction
Neither approach resolved the performance issue.
asyncfnget_user_by_id<'a>(&self,tx:&mutBox<dynTransactionContext<'a>>,user_id:UserId,) -> RepositoryErrorResult<Option<UserRow>>{let user = sqlx::query_as!(UserRow,r#" SELECT id as "id: UserId", name, family_name, email, email_verified, phone as "phone: SystemPhoneNumber", phone_verified, password_hash, locale, jurisdiction_code as "jurisdiction_code: SystemCountry", birthdate, gender as "gender: SystemGender", time_zone, blocked_until, excluded_until, created_at, updated_at, deleted_at FROM users WHERE id = $1 AND deleted_at IS NULL "#,
user_id.inner()).fetch_optional(&mut*tx.conn().await?).await?;Ok(user)}
The text was updated successfully, but these errors were encountered:
Could you please provide a complete, runnable, minimal example instead of the current version in the future?
You are making reproducing your issue more difficult than it needs to be.
@dnlsndr these queries are only executed if the types are not in-cache. They are not executed every time.
The caches are per-connection so they do need to be looked up on every new connection, but once the caches are warm and the pool is populated you shouldn't see this happening often.
We could possibly share the type cache through the ConnectOptions, however.
Problem Description
When executing queries with custom types (enums, etc.), SQLx performs two preliminary SQL queries before the actual query:
These type validation queries significantly impact performance, especially with high DB RTTs:
This runtime type checking creates unnecessary overhead in production systems where types are stable and tracked in code. I understand that this is required in many cases and is a good base setup, but allowing for disabling this functionality would greatly improve performance in many cases.
Proposed Solution
Add a configuration option to disable type checking queries, implemented either:
query_as!
macro systemtest_before_acquire
), e.g.,validate_types_before_execution: bool
Attempted Workarounds
query_as_unchecked!()
query!()
with manual struct constructionNeither approach resolved the performance issue.
Environment
Code Context
Type Definitions
Note
SystemPhoneNumber and SystemCountry are just wrapper types around Strings
Database Schema
Query Implementation
The text was updated successfully, but these errors were encountered: