Engine: asyncpg
Postgres version: 14
Hi! I need to set global variables when I am creating pool.
I use set_config
in init
function for each connection:
async def init(con):
configs = [
f"SELECT set_config('element_type.boolean', '{ElementsTypes.Boolean.value}', False)",
f"SELECT set_config('entity_types.element', '{EntityTypes.Elements.value}', False)",
...
]
for config in configs:
await con.execute(config)
db_pool = await asyncpg.create_pool(
database=os.getenv("POSTGRES_DBNAME" if not is_test_db else "POSTGRES_DBNAME_TEST"),
user=os.getenv("POSTGRES_USER"),
password=os.getenv("POSTGRES_PASSWORD"),
host=host,
init=init,
loop=loop
)
And when I try to get some variable inside procedure or query, I get empty string:
RAISE LOG 'ELEMENT TYPE BOOLEAN %', current_setting('element_type.boolean');
-- OUTPUT: 'ELEMENT TYPE BOOLEAN '
RAISE LOG 'ELEMENT TYPE VAR TYPE %', pg_typeof(current_setting('element_type.boolean'));
-- OUTPUT: 'ELEMENT TYPE BOOLEAN VAR TYPE text'
UPD: I tried to set and get variable in python console and I get empty string again
>>>loop.run_until_complete(pool.fetchval("SELECT set_config('myapp.group_types_element', '1', False)"))
'1'
>>>loop.run_until_complete(pool.fetchval("SELECT current_setting('myapp.group_types_element')"))
''