diesel_async: Failed to find a type oid for ENUM_TYPE

Versions

  • Rust: 1.72
  • Diesel: 2.1
  • Diesel_async: 0.4.1
  • Database: postgresql
  • Operating System linux

Feature Flags

Problem Description

After I upgrade the code from diesel-async 0.3.x to 0.4, I faced this error: Failed to find a type oid for ENUM_TYPE. After I downgrade, the error disappeared. The only change in the code base was diesel-async version.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 31 (15 by maintainers)

Commits related to this issue

Most upvoted comments

No way… Thanks @weiznich and @dullbananas. You are great ❤️

As a temporary workaround it should be possible to just have a query at connection setup that does use that enum type and doesn’t use .eq_any(). That will prepopulate the cache and therefore workaround the cache miss in later queries. That’s not a real solution but it should unblock any user that is affected by this bug.

Another question, somewhere in the code comments, you mentioned: We apply this workaround to prevent requiring all the diesel serialization code to be async. What’s the problem, to run it in async?

The problem would be that the Pg::metadata function would need to return a future if we would execute an async query inside that function. That in turn would need ToSql::to_sql would need to return a future as well, which in turn would be either:

  • A breaking change to diesel itself (and problematic there, because what would we do with futures there?)
  • Disallow reusing the serialization code from diesel, which in turn would make diesel_async incompatible with the existing diesel ecosystem.

What will be the problem to not prevent a Send bound on the query generic type?

That likely would break the AsyncConnectionWrapper , as we don’t have a +Send bound on the LoadConnection::load impl in diesel itself, as opposed to what we had on the AsyncConnection::load impl in diesel_async 0.3. Otherwise it would be “simple” to just perform the lookup queries while serializing the results, but that’s not possible, because as soon as we .await and still hold any reference to the query we would need that Send bound there.

We generally use the binary protocol for all bind values because that decouples the values from the query, which in turn allows us not to care about possible injections. It also gives us better performance as the binary representation is usually smaller.

@weiznich I’m sorry if I hurt your feelings, I didn’t mean to. I was thinking about doing this:

let type_id: TypeId = diesel::sql_query(
	r#"SELECT "pg_type"."oid" AS oid, "pg_type"."typarray" AS array_oid FROM "pg_type" WHERE typname = 'additional_field';"#,
)
.load(&mut conn).await.unwrap().pop().unwrap();
ADDITIONAL_TYPE_OID.store(type_id.oid, Ordering::Release);
ADDITIONAL_TYPE_ARRAY_OID.store(type_id.array_oid, Ordering::Release);
...
#[derive(QueryId)]
pub struct AdditionalFieldType;

impl diesel::sql_types::SqlType for AdditionalFieldType {
	type IsNull = diesel::sql_types::is_nullable::NotNull;
}
impl diesel::sql_types::SingleValue for AdditionalFieldType {}

impl diesel::sql_types::HasSqlType<AdditionalFieldType> for diesel::pg::Pg {
	fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
		PgTypeMetadata::new(ADDITIONAL_TYPE_OID.load(Ordering::Acquire), ADDITIONAL_TYPE_ARRAY_OID.load(Ordering::Acquire))
	}
}

This works nicely and I can refresh the oid whenever I need to

@omid This would be awesome and I’ll pass this message along. In the in between, I might be able to help with some code myself, if that’s useful for him or you. (:

@lsunsi please ask your company to sponsor @weiznich to push this forward 🙏🏼 and/or support him yourself

It’s on my list to address this at some point, but I cannot really make promises about the timeline. The problem is not that simple to solve is outlined in this comment. Essentially it boils down to the fact that we don’t want/cannot have a Send bound on the query for reasons but that makes it harder to evaluate futures borrowing that type. In the end it’s likely solvable by making the returned future conditional send based on the type parameters.

Thanks for providing this example. I can reproduce the issue with that.