Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: simplify typeinfo queries #6

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions postgres-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,14 @@ impl fmt::Debug for Type {

impl fmt::Display for Type {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.schema() {
"public" | "pg_catalog" => {}
schema => write!(fmt, "{}.", schema)?,
}
fmt.write_str(self.name())
}
}

impl Type {
/// Creates a new `Type`.
pub fn new(name: String, oid: Oid, kind: Kind, schema: String) -> Type {
Type(Inner::Other(Arc::new(Other {
name,
oid,
kind,
schema,
})))
pub fn new(name: String, oid: Oid, kind: Kind) -> Type {
Type(Inner::Other(Arc::new(Other { name, oid, kind })))
}

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

/// Returns the schema of this type.
pub fn schema(&self) -> &str {
match self.0 {
Inner::Other(ref u) => &u.schema,
_ => "pg_catalog",
}
}

/// Returns the name of this type.
pub fn name(&self) -> &str {
self.0.name()
Expand Down
1 change: 0 additions & 1 deletion postgres-types/src/type_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub struct Other {
pub name: String,
pub oid: Oid,
pub kind: Kind,
pub schema: String,
}

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
Expand Down
6 changes: 6 additions & 0 deletions tokio-postgres/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ enum Kind {
#[cfg(feature = "runtime")]
Connect,
Timeout,
UnsupportedType,
}

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

pub(crate) fn unsupported_type() -> Error {
Error::new(Kind::UnsupportedType, None)
}

#[allow(clippy::needless_pass_by_value)]
pub(crate) fn db(error: ErrorResponseBody) -> Error {
match DbError::parse(&mut error.fields()) {
Expand Down
22 changes: 8 additions & 14 deletions tokio-postgres/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

const TYPEINFO_QUERY: &str = "\
SELECT t.typname, t.typtype, t.typelem, r.rngsubtype, t.typbasetype, n.nspname, t.typrelid
SELECT t.typname, t.typtype, t.typelem, t.typbasetype, t.typrelid
FROM pg_catalog.pg_type t
LEFT OUTER JOIN pg_catalog.pg_range r ON r.rngtypid = t.oid
INNER JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid
WHERE t.oid = $1
";

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

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

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

let type_ = Type::new(name, oid, kind, schema);
let type_ = Type::new(name, oid, kind);
client.set_type(oid, &type_);

Ok(type_)
Expand Down