Skip to content

Commit 0db3436

Browse files
authored
Merge pull request prisma#6 from prisma/perf/simplify-type-info
perf: simplify typeinfo queries
2 parents c62b992 + 1b645ba commit 0db3436

File tree

4 files changed

+16
-34
lines changed

4 files changed

+16
-34
lines changed

postgres-types/src/lib.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -313,23 +313,14 @@ impl fmt::Debug for Type {
313313

314314
impl fmt::Display for Type {
315315
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
316-
match self.schema() {
317-
"public" | "pg_catalog" => {}
318-
schema => write!(fmt, "{}.", schema)?,
319-
}
320316
fmt.write_str(self.name())
321317
}
322318
}
323319

324320
impl Type {
325321
/// Creates a new `Type`.
326-
pub fn new(name: String, oid: Oid, kind: Kind, schema: String) -> Type {
327-
Type(Inner::Other(Arc::new(Other {
328-
name,
329-
oid,
330-
kind,
331-
schema,
332-
})))
322+
pub fn new(name: String, oid: Oid, kind: Kind) -> Type {
323+
Type(Inner::Other(Arc::new(Other { name, oid, kind })))
333324
}
334325

335326
/// Returns the `Type` corresponding to the provided `Oid` if it
@@ -348,14 +339,6 @@ impl Type {
348339
self.0.kind()
349340
}
350341

351-
/// Returns the schema of this type.
352-
pub fn schema(&self) -> &str {
353-
match self.0 {
354-
Inner::Other(ref u) => &u.schema,
355-
_ => "pg_catalog",
356-
}
357-
}
358-
359342
/// Returns the name of this type.
360343
pub fn name(&self) -> &str {
361344
self.0.name()

postgres-types/src/type_gen.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub struct Other {
88
pub name: String,
99
pub oid: Oid,
1010
pub kind: Kind,
11-
pub schema: String,
1211
}
1312

1413
#[derive(PartialEq, Eq, Clone, Debug, Hash)]

tokio-postgres/src/error/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ enum Kind {
357357
#[cfg(feature = "runtime")]
358358
Connect,
359359
Timeout,
360+
UnsupportedType,
360361
}
361362

362363
struct ErrorInner {
@@ -399,6 +400,7 @@ impl fmt::Display for Error {
399400
#[cfg(feature = "runtime")]
400401
Kind::Connect => fmt.write_str("error connecting to server")?,
401402
Kind::Timeout => fmt.write_str("timeout waiting for server")?,
403+
Kind::UnsupportedType => fmt.write_str("unsupported type")?,
402404
};
403405
if let Some(ref cause) = self.0.cause {
404406
write!(fmt, ": {}", cause)?;
@@ -450,6 +452,10 @@ impl Error {
450452
Error::new(Kind::UnexpectedMessage, None)
451453
}
452454

455+
pub(crate) fn unsupported_type() -> Error {
456+
Error::new(Kind::UnsupportedType, None)
457+
}
458+
453459
#[allow(clippy::needless_pass_by_value)]
454460
pub(crate) fn db(error: ErrorResponseBody) -> Error {
455461
match DbError::parse(&mut error.fields()) {

tokio-postgres/src/prepare.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@ use std::sync::atomic::{AtomicUsize, Ordering};
1717
use std::sync::Arc;
1818

1919
const TYPEINFO_QUERY: &str = "\
20-
SELECT t.typname, t.typtype, t.typelem, r.rngsubtype, t.typbasetype, n.nspname, t.typrelid
20+
SELECT t.typname, t.typtype, t.typelem, t.typbasetype, t.typrelid
2121
FROM pg_catalog.pg_type t
22-
LEFT OUTER JOIN pg_catalog.pg_range r ON r.rngtypid = t.oid
23-
INNER JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid
2422
WHERE t.oid = $1
2523
";
2624

2725
// Range types weren't added until Postgres 9.2, so pg_range may not exist
2826
const TYPEINFO_FALLBACK_QUERY: &str = "\
29-
SELECT t.typname, t.typtype, t.typelem, NULL::OID, t.typbasetype, n.nspname, t.typrelid
27+
SELECT t.typname, t.typtype, t.typelem, t.typbasetype, t.typrelid
3028
FROM pg_catalog.pg_type t
31-
INNER JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid
3229
WHERE t.oid = $1
3330
";
3431

@@ -153,10 +150,8 @@ pub(crate) async fn get_type(client: &Arc<InnerClient>, oid: Oid) -> Result<Type
153150
let name: String = row.try_get(0)?;
154151
let type_: i8 = row.try_get(1)?;
155152
let elem_oid: Oid = row.try_get(2)?;
156-
let rngsubtype: Option<Oid> = row.try_get(3)?;
157-
let basetype: Oid = row.try_get(4)?;
158-
let schema: String = row.try_get(5)?;
159-
let relid: Oid = row.try_get(6)?;
153+
let basetype: Oid = row.try_get(3)?;
154+
let relid: Oid = row.try_get(4)?;
160155

161156
let kind = if type_ == b'e' as i8 {
162157
// Note: Quaint is not using the variants information at any time.
@@ -173,14 +168,13 @@ pub(crate) async fn get_type(client: &Arc<InnerClient>, oid: Oid) -> Result<Type
173168
} else if relid != 0 {
174169
let fields = get_composite_fields(client, relid).await?;
175170
Kind::Composite(fields)
176-
} else if let Some(rngsubtype) = rngsubtype {
177-
let type_ = get_type_rec(client, rngsubtype).await?;
178-
Kind::Range(type_)
179-
} else {
171+
} else if type_ == b'b' as i8 {
180172
Kind::Simple
173+
} else {
174+
return Err(Error::unsupported_type());
181175
};
182176

183-
let type_ = Type::new(name, oid, kind, schema);
177+
let type_ = Type::new(name, oid, kind);
184178
client.set_type(oid, &type_);
185179

186180
Ok(type_)

0 commit comments

Comments
 (0)