From 7586165737243312ae512cf5e6a711673a08b34a Mon Sep 17 00:00:00 2001 From: Rich Turner <7072278+richturner@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:57:26 +0000 Subject: [PATCH] Add POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT to terminate idle in transaction sessions Sessions left idle in a transaction prevent autovacuum removing dead rows, which bloats frequently updated tables such as the asset table. Default the timeout to 10min; set to 0 to disable. --- Dockerfile | 1 + README.md | 3 +++ or-entrypoint.sh | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/Dockerfile b/Dockerfile index 3017179..a82a393 100644 --- a/Dockerfile +++ b/Dockerfile @@ -109,6 +109,7 @@ ENV PGROOT=/var/lib/postgresql \ OR_REINDEX_COUNTER=${OR_REINDEX_COUNTER} \ OR_DISABLE_REINDEX=${OR_DISABLE_REINDEX:-false} \ POSTGRES_MAX_CONNECTIONS=${POSTGRES_MAX_CONNECTIONS:-50} \ + POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT=${POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT:-10min} \ OR_DISABLE_AUTO_UPGRADE=${OR_DISABLE_AUTO_UPGRADE:-false} WORKDIR /var/lib/postgresql diff --git a/README.md b/README.md index aa074d9..90c88b1 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ POSTGIS and TimescaleDB (inc. toolkit for hyperfunctions) image built for amd64 - OR specific ENV variables and a healthcheck added - Easy configuration of `max_connections` using `POSTGRES_MAX_CONNECTIONS` environment variable (set to `-1` to disable this setting) +- Sessions left idle in a transaction for more than 10 minutes are terminated, as they prevent autovacuum removing dead rows + and cause table bloat; configure using `POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT` environment variable with a + PostgreSQL time value e.g. `30min` (set to `0` to disable this setting) - PGDATA path set to match old Alpine image (for ease of DB migration) - POSTGRES user UID and GID changed to match old Alpine image (for ease of DB migration) - Auto upgrade of database with PG major version changes from previous PG major version; can be disabled using diff --git a/or-entrypoint.sh b/or-entrypoint.sh index a592918..26d266f 100755 --- a/or-entrypoint.sh +++ b/or-entrypoint.sh @@ -31,6 +31,12 @@ if [ $POSTGRES_MAX_CONNECTIONS -gt 0 ]; then set -- "$@" -c max_connections=${POSTGRES_MAX_CONNECTIONS} fi +# Terminate sessions left idle in a transaction for longer than this, as they prevent autovacuum removing dead rows +# (set to 0 to disable this setting) +if [ -n "$POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT" ] && [ "$POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT" != "0" ]; then + set -- "$@" -c idle_in_transaction_session_timeout=${POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT} +fi + wait_for_ready() { # Wait for connectivity first until pg_isready -q; do