Two connections
The direct connection reuses
API_PG_DB_USERNAME, API_PG_DB_PASSWORD and
API_PG_DB_DATABASE, so usually only the host changes. When it is not set,
Kodus uses the pooled connection for everything, as it does without a pooler.
Why the direct connection matters in transaction mode:
- Live events. LISTEN needs a session that stays open. Behind a transaction-mode pooler it connects and then never receives anything. Review status in the UI and rule sync after a merge then arrive through polling, up to 10 seconds late.
- Migrations. Some migrations build indexes with
CREATE INDEX CONCURRENTLYafter changing the statement timeout of their session. In transaction mode the two statements can land on different server sessions.
Configure PgBouncer
Configure Kodus
Set these on api, worker, worker-analytics, webhooks and on whatever runs the migrations (RUN_MIGRATIONS=true):
Turn it on with the installer
Thekodus-installer wires the pooler for each way of running Kodus, and
fills in the settings above for you.
Setting the direct connection also moves Kodus’s coordination locks from
session locks to rows in the database. Pods still running without it do not
see the new locks, so turn the pooler on with the old pods stopped: run
docker compose down first, or scale the api, worker and webhooks
Deployments to 0 before the helm upgrade.
Docker Compose
Thepgbouncer profile starts PgBouncer 1.26 in transaction mode in front of
the bundled PostgreSQL, with track_extra_parameters already set. Point Kodus
at it in .env, keeping PostgreSQL as the direct connection:
Helm, with the CloudNativePG operator
Withpostgres.mode: operator, postgres.operator.pooler.enabled: true adds a
CloudNativePG Pooler in front of <release>-postgres-rw. The app connects to
<release>-postgres-pooler-rw; migrations and LISTEN stay on
<release>-postgres-rw. The chart always adds track_extra_parameters, on top
of any parameters you set.
poolMode takes transaction (default) or session. Size
max_client_conn for every api, worker and webhooks replica at
autoscaling.maxReplicas.
Helm, with your own pooler
Withpostgres.mode: external, point host/port at the pooler and
directHost/directPort at PostgreSQL. When a connection string is needed for
the direct connection, use directUrlKey (next section).
postgres.mode: bundled has no pooler. Use operator or external to run
Kodus behind one.
Your own PgBouncer or managed pooler
If PostgreSQL and its pooler are already running on your side, point Kodus at them, with Docker Compose or the Helm chart alike. Check your setup against this table:
Only the pooler is reachable. PgBouncer can run one database entry in
session mode next to the transaction-mode one. Kodus uses it for migrations
and for one long-lived LISTEN connection per process, so a small pool is
enough:
API_DATABASE_DISABLE_SSL applies to every connection, so when the
pooler speaks plain TCP and PostgreSQL requires TLS (or the other way round),
keep the pooled settings as they are and put sslmode in
DATABASE_DIRECT_URL; the URL’s sslmode wins for that connection. The
PostgreSQL driver in Kodus reads sslmode=require and sslmode=prefer as
verify-full, so the server certificate must validate (add your CA with
NODE_EXTRA_CA_CERTS). Use sslmode=no-verify to encrypt without checking
the certificate, and sslmode=disable for plain TCP.
Helm chart. In external mode, put the direct connection string in the
Secret named by postgres.external.existingSecret and set
postgres.external.directUrlKey to its key. Every Kodus pod and the
migration Job receive it as DATABASE_DIRECT_URL.
Highly available PostgreSQL
Kodus connects to one host per connection; it does not take a list of hosts ortarget_session_attrs. Point both connections at endpoints that always
lead to the current primary: a virtual IP, HAProxy with Patroni health
checks, the -rw service of CloudNativePG, or a PgBouncer that follows the
primary. Kodus writes from every component, so read replicas are not used.
During a failover, queries in flight fail and the pools reconnect on the
next request. Behind a pooler, coordination locks are rows in the database,
so they survive a failover with the data; without one they are session locks,
freed when the sessions to the old primary drop.
Check the setup
Run the doctor from thekodus-installer directory: ./scripts/doctor.sh,
or ./scripts/doctor-k8s.sh -n <namespace> on Kubernetes (see
Troubleshooting). Two lines
cover this page:
database.locks: Kodus can take its coordination locks. A failure here usually means the migrations have not run.database.listen: live events between api and worker arrive. A warning here means the LISTEN connection goes through a transaction-mode pooler; set the direct connection.