MagicStack / MagicStack/asyncpg

Add support for PGOPTIONS environment variable

Open
#1,070 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.1k
Forks
468
PR merge metrics
No merged PRs in 30d

Description

asyncpg supports an explicit server_options=... dict connection method parameter, but does not seem to populate its value from the PGOPTIONS environment variable. This is a feature request for it to do so.

Specifically, `asyncpg/connect_utils.py` reads user, host, port, database, password, and SSL information with calls like `os.getenv('PGUSER')`, `os.environ.get('PGHOST')`, etc. There is no corresponding call for reading PGOPTIONS.

I would like to set the environment variable `PGOPTIONS='-c default_transaction_read_only=on -c statement_timeout=120s'` and have asyncpg interpret its values as server_settings the way libpq does. For instance, the below works for psql:

```
PGOPTIONS='-c statement_timeout=1s' psql -c "SELECT pg_sleep(2)"
ERROR: canceling statement due to statement timeout
```

but a simple asyncpg script running the same ignores the statement_timeout since it never reads the value of PGOPTIONS.

```
$ PGOPTIONS='-c statement_timeout=1s' ./test_asyncpg_timeout.py
SELECT 1
```

https://www.postgresql.org/docs/current/libpq-envars.html

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in asyncpg/connect_utils.py, where the existing PG environment variables are read, and compare the requested behavior with the PostgreSQL libpq-envars documentation. Run the provided test_asyncpg_timeout.py reproduction with PGOPTIONS set; done means PGOPTIONS values are interpreted as server settings and the statement_timeout example behaves like psql.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.