'Quarkus - Shared Database, Shared Schema Multitenancy with Postgres Row Level Security(RLS) and session parameter

I would like to implement Shared Database, Shared Schema Multitenancy using Postgres Row Level Security and Quarkus

Following this AWS article one approach is to set the tenant in a parameter('app.current_tenant') at the session level and then use it in the RLS policy

CREATE POLICY tenant_isolation_policy ON tenant
USING (tenant_id = current_setting('app.current_tenant')::UUID);

There is a nice blog post about how to do that one in Spring

public class TenantAwareHikariDataSource extends HikariDataSource {

    @Override
    public Connection getConnection() throws SQLException {
        Connection connection = super.getConnection();

        try (Statement sql = connection.createStatement()) {
            sql.execute("SET app.current_tenant = '" + ThreadLocalStorage.getTenantName() + "'");
        }

        return connection;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        Connection connection = super.getConnection(username, password);

        try (Statement sql = connection.createStatement()) {
            sql.execute("SET app.current_tenant = '" + ThreadLocalStorage.getTenantName() + "'");
        }

        return connection;
    }

}

Likewise, I would like to keep the solution to the Datasource Layer and set the tenant information when a connection gets acquired, since I do not plan to use Hibernate. I guess setting this at that layer would work for Hibernate as well.

So is it possible to create a TenantAwareDatasource like the TenantAwareHikariDataSource implemented in the previous article in Quarkus ?

Just for reference, I had a look at the Quarkus issues and found one related topic, but this one is about different schemas

Multitenant datasources #11861



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source