Execute a SQL statement to set a PostgreSQL parameter setting in each "database session"

31 Views Asked by At

I have created a new Symfony 7.x project with symfony new .... I have installed the symfony/orm-pack. Doctrine, Dbal the ORM etc. are working.

I am using PostgreSQL.

What I want to achieve now is, to set a config variable on every database session in Postgres.

https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-SET

So every database operation will have this setting before any other query is executed.

For illustration, this is a typical log entry from the system I am migration this from:

2024-03-07 14:10:36.937 UTC [1600] LOG:  execute <unnamed>: SELECT set_config($1, $2, false)
2024-03-07 14:10:36.937 UTC [1600] DETAIL:  parameters: $1 = 'customer.user_id', $2 = '1'

2024-03-07 14:10:36.960 UTC [1600] LOG:  execute <unnamed>: SELECT * ...

I evaluated event listeners for this, but it seems they do not cover my use case.

How can I accomplish this in an idiomatic "Symfony way"?

0

There are 0 best solutions below