added code for setting and getting session variable in postgres #673
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is raised as an issue at go-gorm/gorm#6753 and this is the underlying code example
Explain your user case and expected results
We are implementing session variables in postgresql to take advantage of row level security (RLS) in postgresql.
For this purpose, we need
Both requests can be concurrent.
So we require that after opening the connection using
gorm.Open()
, it should be possible to obtain two separate connections from the underlying connection pool and both should work independent of each other such that one has session variable value set to TENANT1 and the other has session variable set to TENANT2However, we note by running
that the output is
As can be seen,
FATA-050
andFATA-060
have the value set toTENANT1
However, the moment we set the value for the second session to
TENANT2
, the value is updated for both the first session as well as the second session, which means they are both one and the same.So the question is? How do we obtain two distinct sessions / connections (or whatever is the terminology gorm uses) that can perform operations in parallel?
A very simple solution is to just create a new database connection using the dsn and
gorm.Open()
every time, but then there is no connection pooling - is our understanding.The other solutions seems to be to create a new connection whenever a new tenant is encountered and return the correct *gorm.DB based on the tenant Id. This means that the number of connection pools will be equal to the number of tenants - that's a better solution than the first one but we are still not able to use connections from the same pool for multiple tenant requests.
So how do we do this in an efficient manner?
This is raised as an issue at go-gorm/gorm#6753 and this is the underlying code example