sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.5k stars 443 forks source link

Queries against a schema are send to another #896

Closed mamcx closed 2 years ago

mamcx commented 2 years ago

After I move to async, I get sometimes the behavior of certain data sent to another schema, and that is terrible because this is for a multi-tenant setup.

I run all inside actix, and execute in each request:

SET search_path = NAME_SCHEMA, public, pg_catalog

This works most of the time but is unclear to me how or when this fails. It could have been related to the error "current transaction is aborted, commands ignored until end of transaction block" but I'm unsure.

I wonder if this could be related to the use of connection pools:

    #[tracing::instrument(name = "PG OPEN")]
    pub async fn open(conn: PgCon) -> Result<Self, Error> {
        //dbg!(&conn);

        let conn2 = conn.clone();
        let mut cfg = Config::new();
        cfg.dbname = Some(conn.db);
        cfg.user = Some(conn.user);
        cfg.host = Some(conn.host);
        cfg.password = conn.pwd;
        cfg.port = Some(conn.port);
        cfg.application_name = conn.app;

        cfg.manager = Some(ManagerConfig {
            recycling_method: RecyclingMethod::Fast,
        });

        let pool = cfg.create_pool(Some(Runtime::Tokio1), NoTls)?;

        Ok(Self { pool, conn: conn2 })
    }

So:

How to be certain the schema scoping is valid in each request?

sfackler commented 2 years ago

None of the types in that code are defined by this library. If you have a question about a connection pool, you should probably ask that on the repository for that library.

mamcx commented 2 years ago

Ok, I post this in deadpool, thanks.

bikeshedder commented 2 years ago

I wonder if this could be caused by the way tokio-postgres implements pipelining. The only way I can imagine this happening is a connection still executing queries while it's being reused by another worker. I tried coming up with an explanation here: