Your Question

Please see the details at https://github.com/go-gorm/playground/pull/673#issue-2045891447 This is to implement row level security (RLS) in postgresql, ie each session / connection (or whatever terminology gorm uses) will have a separate session variable value for tenantId but modifying it in one should not impact the value in the others.

The underlying code example is https://github.com/go-gorm/playground/pull/673/files#diff-ac94c91f8ea84762d55b6932ef1466a2165984591d3e646360c5bb31b2130900R115-R144

The document you expected this should be explained

https://gorm.io/docs/connecting_to_the_database.html#PostgreSQL

Expected answer

An example of how gorm connection pooling can be used to acquire separate connections / sessions which can run in parallel and can have different values of session variables.

Comment From: a631807682

Is this what you need? https://gorm.io/docs/sql_builder.html#Connection

Comment From: alkuma

ok I figured out the following now

  1. changing a session variable in one session changes it in all the other sessions created out of the same connection. It looks like the gorm Session is not the same as a database session, but something new with the gorm layer.
  2. I can set the session variable directly in the out of of gorm.Open(), there is no need to .Session() it and then create the session variable.

So for now I am doing the following:

  1. create a new gorm.Open() connection for every time I need to set a new session variable
  2. reuse the same connection for unique value of the session variable.

This satisfies the functionality we are trying to achieve via RLS in postgresql while still using gorm.