Skip to content

feat: serve several pools from one image with POOLS - #2

Merged
jclaveau merged 6 commits into
masterfrom
feat/multiple-pools
Aug 14, 2026
Merged

jclaveau merged 6 commits into
masterfrom
feat/multiple-pools

Conversation

@jclaveau

@jclaveau jclaveau commented Aug 13, 2026 •

Copy link
Copy Markdown
Owner

Builds on #3 (the three misplaced database-line parameters, edoburu#132) and on #1, whose expectations are what show the new code is inert until asked for.

What I am after: one pooler serving the same database through several pools of different sizes, so a paid tier and a free tier cannot starve each other, nor the connections the app needs to authenticate anyone at all.

  • PgBouncer does this natively — a pool per [databases] entry, chosen by the database name a client connects to.
  • This image could not express it: one entry, one pool.

POOLS

DATABASE_URL=postgres://user:pass@postgres-host/database
POOLS=base,paid,free
POOL_BASE_POOL_SIZE=20
POOL_PAID_POOL_SIZE=34
POOL_FREE_POOL_SIZE=10
[databases]
base = auth_user=user dbname=database host=postgres-host pool_size=20 port=5432
paid = auth_user=user dbname=database host=postgres-host pool_size=34 port=5432
free = auth_user=user dbname=database host=postgres-host pool_size=10 port=5432
  • Each pool inherits host, port, dbname and auth_user from the existing variables, overriding through POOL_<NAME>_<SETTING>.
  • Any connect string parameter works as a setting, host included, so a pool can point at a replica or another server entirely.
    • No allowlist: pgbouncer already refuses the file with a precise error for a parameter it does not know.
    • A list here would be upstream's to keep in sync, for no gain.
  • Settings are emitted sorted; a value holding a space is quoted, and a quote inside it doubled.
  • The README gains a Multiple pools and users section covering both variables, the two identity axes and the migration off DATABASE_URLS.

USERS

POOLS describes topology, so credentials needed their own home:

USERS=app,reporting
USER_APP_PASSWORD=…
USER_REPORTING_PASSWORD=…
  • One userlist.txt entry per name.
  • A password already stored as a verifier (md5…, SCRAM-SHA-256$…, copied out of pg_authid) is written through untouched rather than hashed again.
  • A username can be spelled in ways an environment variable cannot, so the name is a label:
    • USER_METRICS_NAME=metrics.exporter carries the real one.
    • Unset means the label is the username.
  • Not needed when AUTH_USER and an auth_query are in play — pgbouncer then reads passwords from the server, and one credential covers everyone.

AUTH_USER reaches the entries

Same fallback as edoburu#135 (for edoburu#134), extended to pools: auth_user takes DB_USER, then AUTH_USER, then postgres.

  • AUTH_USER was documented as the way to have pgbouncer look passwords up on the server, and the database entry asked as postgres regardless.
  • A pool keeps its own POOL_<NAME>_AUTH_USER where it names one.
  • Inert wherever DB_USER is set, which is why no existing expectation moved — all-knobs sets both and renders exactly as before.

Passwords reach the file, never the logs

A pool forcing a server identity needs password= on its database line, in the clear. The entrypoint echoes that file at startup, which would have put it in docker logs, the platform's log drain and every CI job that boots the image.

paid = auth_user=appuser dbname=appdb host=… password=*** pool_size=34 port=6543 user=viewer
  • Only the echoed copy is masked, quoted values and doubled quotes included; the file pgbouncer reads is untouched.
  • This is why the comment above the userlist writer says it hashes "to avoid printing it during startup" — the new field would have walked around that intent.
  • live.sh greps the container's own log for the password and fails on finding it.

What is refused, and what is only warned about

Everything below now fails before a single file is written, rather than being misread in silence:

  • a name that is not usable — letters, digits and underscores only, unique, and none the prefix of another
    • POOL_BASE_REPORTING_POOL_SIZE would belong to both base and base_reporting, and names differing only in case collide the same way
    • pgbouncer too: it is the admin console's own database, and pgbouncer refuses the whole file over it
  • a setting naming nothing in the list — POOL_BSAE_POOL_SIZE beside POOLS=base rendered a pool of plain defaults and said nothing
    • POOL_MODE and POOL_SIZE are exempt — the pool mode of the process, and the default size of every entry — rather than settings of a pool named MODE or SIZE
    • a pool's settings need no allowlist, pgbouncer reporting the parameters it knows not; a USERS name's do, NAME and PASSWORD being read here and nowhere else
  • an empty override, which is a reference resolving to nothing far more often than a request to blank an inherited value
  • a USERS name with no password, every name checked before the first one is written
    • userlist.txt is usually a mounted volume and the writer skips names already in it, so a half-written file outlived the startup that made it
  • a name in USERS that DB_USER already spells, whose credential is written first — the listed password went in nowhere and said so nowhere
    • the label is not what collides: USER_<LABEL>_NAME is resolved before the two are compared
  • a pool with no host, neither its own nor a DB_HOST to inherit
  • a pool carrying POOL_<NAME>_PASSWORD and no POOL_<NAME>_USER to present it as — pgbouncer builds a forced credential only where a user carries one, so that password sat in the file in the clear and authenticated nothing
  • POOLS beside a mounted pgbouncer.ini, which is served as it stands and left those pools rendered nowhere

One behaviour change to call out: a stray POOL_* or USER_* variable now fails startup even with POOLS and USERS unset. Upstream ships only POOL_MODE and POOL_SIZE, both exempt, so nothing documented breaks — but an unrelated variable sharing the prefix used to be ignored and no longer is.

One case only warns: a pool forcing a user it holds no password for. A mounted userlist.txt may still carry that password, so it cannot be an error.

DATABASE_URLS is refused beside either

  • DATABASE_URLS says who may connect and what they connect to, in one value.
  • POOLS and USERS split those apart.
  • Supporting both left DATABASE_URLS meaning different things depending on whether POOLS happened to be set — worse than either behaviour alone.
DATABASE_URLS cannot be combined with POOLS or USERS: name the entries in POOLS and the credentials in USERS
  • DATABASE_URL (singular) is untouched, and still provides the defaults a pool does not override.
  • Nothing existing breaks: POOLS and USERS are new, so no configuration in the wild combines them.

Migrating off DATABASE_URLS

DATABASE_URLS gives you replacement
one entry per URL, named by its path POOLS + POOL_<NAME>_DBNAME
host and port per URL POOL_<NAME>_HOST / POOL_<NAME>_PORT
that URL's user as the entry's auth_user POOL_<NAME>_AUTH_USER
that URL's user and password in userlist.txt USERS + USER_<NAME>_PASSWORD
  • No capability is lost, which is what makes refusing the combination safe rather than restrictive.
  • The split separates two things one value used to do at once:
    • who may connect to the pooler — USERS, landing in userlist.txt
    • who the pooler connects to Postgres as — POOL_<NAME>_USER, landing on the database line as user=/password=
  • tests/pools-own-users renders both, and its userlist.txt holds only the connection's own credential, not the pools' forced ones.
  • It also buys what the URL form never had: pool_size, pool_mode, several pools onto one database, pre-hashed verifiers, usernames an environment variable cannot spell.

One bug fixed on the way

  • DATABASE_URLS entries inherited each other's fields, being parsed in a single subshell.
    • A URL omitting a port, user, password or name silently reused the previous one's.
    • A port-less third URL came out carrying the second's port.
  • parse_url now resets to what the environment provided, leaving single-DATABASE_URL behaviour exactly as it was.

Testing

Every case renders and is compared against expected/; those that can also boot the container, since pgbouncer parses its config only at startup and a text diff cannot tell a valid file from an unchanged one.

live.sh asserts what a session sees, not what the file claims:

  • three pools serving one database, SHOW DATABASES reporting 20/34/10
    • DEFAULT_POOL_SIZE is set to 7 there so those numbers mean something: pgbouncer's own default is 20, which a pool sized 20 could not be told apart from
  • a pool's timezone and client_encoding read back through it, with an untouched pool as the witness
  • pool_size capping for real: a pool of one holds a transaction while a second client queues
    • remove the pool_size and that reads cl_waiting=0, so it cannot pass vacuously
  • a USERS credential authenticating: the test creates the role server-side and checks select current_user

The auth_user fallback has four cases — with AUTH_USER alone, beside DB_USER, across pools and beside USERS. Reverting the fallback reddens exactly the three that name no DB_USER, and leaves the fourth green, which is what makes it the witness.

It also reads the container's log, for the two things no rendered file can show:

  • the echoed config carries password=*** and nowhere the password itself
  • a pool forcing a user it holds no password for is warned about at boot
  • both were mutation-checked: unmasking the echo, then deleting the warning, each turns the suite red on its own message

Every refusal above has its case, and coverage.sh fails when the entrypoint reads a variable no case sets — 92 of 92 today.

Decisions

  • DB_USER keeps all three of its upstream meanings: a userlist.txt credential, the auth_user= of every entry, and whatever the parsed DATABASE_URL carries.
    • Neither renamed nor aliased — it is your documented interface, and AUTH_USER already names the process-wide setting.
    • USERS and POOL_<NAME>_AUTH_USER carry those meanings apart, so nothing new has to lean on it.
  • Three points raised in review are settled in the last two commits: a variable naming no listed pool is refused, run.sh rewrites the failing line number to line N so missing-db-host stops churning, and the AUTH_USER fallback landed here rather than waiting on the upstream PR.
  • A pool forcing a user with no password stays a warning while a password with no user is refused: a mounted userlist.txt can supply the first, nothing can rescue the second.
  • The last round moved every remaining refusal above the render. DATABASE_URL is parsed once, before the checks that read the host and user it carries, and the two later re-parses are gone.
    • Six new cases came with it, each mutation-checked: removing the assertion it pins turns that case red on its own message, and pools-with-database-url is what fails if the parse moves back down.
    • set -f keeps a name in POOLS/USERS out of the shell's globbing — the lists are word-split unquoted, and POOLS=* used to come back naming a file in the container's root.

Out of scope: no change to how userlist.txt is written for the single-connection path, and no per-user [users] section — tiering by database entry covers the need, and the user axis is a different feature.

Assisted-by: Claude:claude-opus-5[1m]

@jclaveau
jclaveau changed the base branch from feat/config-regression-tests to master August 13, 2026 15:13
They are parsed in one subshell, so a URL omitting a port, user, password or name silently reused the previous one's.

Assisted-by: Claude:claude-opus-5[1m]
@jclaveau
jclaveau force-pushed the feat/multiple-pools branch from 2b0d951 to 5a5a10f Compare August 13, 2026 15:18
@jclaveau
jclaveau changed the base branch from master to fix/database-line-parameters August 13, 2026 15:18
@jclaveau
jclaveau changed the base branch from fix/database-line-parameters to master August 13, 2026 15:39
@jclaveau
jclaveau force-pushed the feat/multiple-pools branch 5 times, most recently from 75d32b5 to fc14eec Compare August 13, 2026 22:41
Each name in POOLS becomes a [databases] entry inheriting the base settings, so one database can be served by pools of different sizes.

Assisted-by: Claude:claude-opus-5[1m]
@jclaveau
jclaveau force-pushed the feat/multiple-pools branch from fc14eec to 086277c Compare August 14, 2026 09:40
Comment thread entrypoint.sh
ENV_DB_PASSWORD="${DB_PASSWORD:-}"
ENV_DB_HOST="${DB_HOST:-}"
ENV_DB_PORT="${DB_PORT:-}"
ENV_DB_NAME="${DB_NAME:-}"

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like smell, why no renaming the vars in the parse_url() function?

@jclaveau
jclaveau force-pushed the feat/multiple-pools branch 3 times, most recently from 8acbc93 to 590c545 Compare August 14, 2026 10:55

@jclaveau jclaveau left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaa

@jclaveau jclaveau left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zz

@jclaveau
jclaveau force-pushed the feat/multiple-pools branch 5 times, most recently from ef9a94d to 89e3df6 Compare August 14, 2026 12:41
DATABASE_URLS spells topology and credentials in one value; POOLS and USERS split them, and a variable that means different things depending on its neighbours is worse than either behaviour alone.

Assisted-by: Claude:claude-opus-5[1m]
@jclaveau
jclaveau force-pushed the feat/multiple-pools branch from 89e3df6 to 9426258 Compare August 14, 2026 12:48
Every refusal now lands before a file is written, and each one carries the case that fails without it.

Assisted-by: Claude:claude-opus-5[1m]
Same fallback as edoburu#135, applied to pool entries too, and inert wherever DB_USER is set.

Assisted-by: Claude:claude-opus-5[1m]
A pool with no host failed mid-render, leaving a pgbouncer.ini that turned the next start into a complaint about a config nobody mounted. The rest of this round closes what the entrypoint was still taking in silence.

Assisted-by: Claude:claude-opus-5[1m]
@jclaveau
jclaveau merged commit a822675 into master Aug 14, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant