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

Postgres: enums with the same name from different schemas clash silently #2114

Open
twuebi opened this issue Sep 23, 2022 · 0 comments
Open
Labels

Comments

@twuebi
Copy link

twuebi commented Sep 23, 2022

Bug Description

Defining two enums with the same name under separate schemas in postgres produces runtime crashes upon inserts.

With schemas a and b and enum SOME_ENUM, the error is either "column \"that_enum\" is of type some_enum but expression is of type a.some_enum" or "column \"that_enum\" is of type some_enum but expression is of type b.some_enum" depending on the ordering of the rows returned here in fetch_type_id_by_name.

Minimal Reproduction

A repro repo can be found here.

Info

  • SQLx version: 0.6.2
  • SQLx features enabled: "postgres", "runtime-tokio-rustls"
  • Database server and version: Posgres 13.5
  • Operating system: Ubuntu 22.04
  • rustc --version: rustc 1.64.0 (a55dd71d5 2022-09-19)

Attempted solution

By joining pg_catalog.pg_type and pg_catalog.pg_namespace and filtering on current_schema(), it's possible to guide the selection at runtime via search_path. E.g. by changing the query in fetch_type_id_by_name to:

SELECT ty.oid 
FROM pg_catalog.pg_type ty
JOIN pg_catalog.pg_namespace ns 
ON ns.oid = ty.typnamespace 
WHERE typname ILIKE $1 
AND ns.nspname = current_schema()

This seems brittle since current_schema() picks the first item on search_path which, to my understanding, means that types defined on public would not be found. Extending the query to accommodate the full search path would reintroduce the possibility of multiple returned matches, hence a way forward may be to pick the type based on precedence on the search_path.

Related issues

#1171
#1576

@twuebi twuebi added the bug label Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant