Skip to content

Tutorial#

The checkout service is timing out while waiting for a connection to the orders database. This tutorial shows how to see which applications are holding connections and how to monitor for idle transactions.

Install#

$ curl -LO https://github.com/bolajiwahab/pgcue/releases/download/v0.1.0/pgcue_0.1.0_linux_amd64.tar.gz
$ curl -LO https://github.com/bolajiwahab/pgcue/releases/download/v0.1.0/checksums.txt
$ sha256sum --check --ignore-missing checksums.txt
$ tar -xzf pgcue_0.1.0_linux_amd64.tar.gz
$ sudo install pgcue /usr/local/bin/

On ARM, replace amd64 with arm64.

$ curl -LO https://github.com/bolajiwahab/pgcue/releases/download/v0.1.0/pgcue_0.1.0_darwin_arm64.tar.gz
$ curl -LO https://github.com/bolajiwahab/pgcue/releases/download/v0.1.0/checksums.txt
$ shasum -a 256 --check --ignore-missing checksums.txt
$ tar -xzf pgcue_0.1.0_darwin_arm64.tar.gz
$ sudo install pgcue /usr/local/bin/

On Intel Macs, replace arm64 with amd64.

In PowerShell:

$base = "https://github.com/bolajiwahab/pgcue/releases/download/v0.1.0"
Invoke-WebRequest "$base/pgcue_0.1.0_windows_amd64.zip" -OutFile pgcue.zip
Invoke-WebRequest "$base/checksums.txt" -OutFile checksums.txt
(Get-FileHash pgcue.zip).Hash; Select-String windows_amd64 checksums.txt
Expand-Archive pgcue.zip -DestinationPath "$env:LOCALAPPDATA\pgcue"

The two hashes must match. Add %LOCALAPPDATA%\pgcue to your PATH. On ARM, replace amd64 with arm64.

Pass connection settings as environment variables. --env PGPASSWORD copies the password from your shell, so it doesn't appear in the command:

$ docker run --rm -it \
    --env PGHOST=db.internal --env PGDATABASE=orders --env PGUSER=monitor --env PGPASSWORD \
    ghcr.io/bolajiwahab/pgcue:0.1.0 doctor

Or mount your service and password files:

$ docker run --rm -it \
    --volume "$HOME/.pg_service.conf:/pgcue/pg_service.conf:ro" \
    --volume "$HOME/.pgpass:/pgcue/pgpass:ro" \
    --env PGSERVICEFILE=/pgcue/pg_service.conf --env PGPASSFILE=/pgcue/pgpass \
    --env PGSERVICE=orders-production \
    ghcr.io/bolajiwahab/pgcue:0.1.0 doctor

For a server on your own machine, use PGHOST=host.docker.internal, adding --add-host=host.docker.internal:host-gateway on Linux. To run the rest of this tutorial's commands unchanged, define an alias with your options:

$ alias pgcue='docker run --rm -it --env PGHOST --env PGDATABASE --env PGUSER --env PGPASSWORD ghcr.io/bolajiwahab/pgcue:0.1.0'

Drop -t when you pipe pgcue's output to another command.

$ go install github.com/bolajiwahab/pgcue/cmd/pgcue@latest

This needs Go 1.27 or later.

To enable shell completion, write the script for your shell to a location it loads on startup:

$ pgcue completion bash > ~/.local/share/bash-completion/completions/pgcue
$ pgcue completion zsh > "${fpath[1]}/_pgcue"
$ pgcue completion fish > ~/.config/fish/completions/pgcue.fish

Connect#

pgcue uses the same connection settings as psql. Point it at the server and check what it can see:

$ export PGHOST=db.internal PGDATABASE=orders PGUSER=monitor
$ pgcue doctor
PostgreSQL readiness
  ✓ Connected to db.internal:5432/orders
  ✓ Server role: primary
  ✓ PostgreSQL 18 is supported
  ✓ Connected as monitor to database orders
  ✓ Session is read-only
  ✓ Role can read PostgreSQL monitoring views

Optional extensions
  ! pg_stat_statements is not installed; unavailable: statements.top

The missing extension affects statements.top. The connection and transaction checks can still run. See Connecting for role and extension requirements.

Investigate#

connection-exhaustion walks through three checks to see how connections are being used:

$ pgcue investigate connection-exhaustion
Connection exhaustion

Step 1 of 3: Connections by application and state
Check: activity.connections

See how current connections are distributed by application, user, and state.
Run this check? [r]un, [d]escribe, [s]kip, [b]ack, [q]uit (r):

Press Enter to run each step and accept its default parameters. Look at these results in order:

  1. activity.connections groups sessions by database, user, application, client address, and state. A large group of idle in transaction sessions for checkout-worker points to connections held by open transactions.
  2. activity.idle_transactions lists individual sessions that are idle inside a transaction. The default threshold is five minutes since the transaction started.
  3. activity.long_transactions lists transactions that have been open for at least five minutes by default, including transactions that are still active.

Inspect idle transactions#

If you see idle transactions in the first step but the second step returns no rows, try a lower threshold:

$ pgcue run activity.idle_transactions --minimum-duration 30s

Look for repeated application_name and query values. If several sessions from the same worker stay idle with an open transaction, check whether the worker commits or rolls back on every path before returning the connection to its pool. The query column shows the most recently observed statement.

Watch the idle transaction list as the application returns connections:

$ pgcue run activity.idle_transactions --minimum-duration 30s --watch 10s

Catch it next time#

A scheduler can run the check and alert when it finds idle transactions open for at least five minutes. This example uses jq to distinguish a failed check from an empty result:

if ! result=$(pgcue run activity.idle_transactions --minimum-duration 5m --output json) ||
   ! printf '%s\n' "$result" | jq -e '.status == "succeeded" and (.rows | type == "array")' > /dev/null 2>&1; then
  echo "UNKNOWN: the check did not run: $(printf '%s\n' "$result" | jq -r '.error // "no output"' 2>/dev/null)"
  exit 3
fi
idle=$(printf '%s\n' "$result" | jq '.rows | length')
if [ "$idle" -gt 0 ]; then
  echo "CRITICAL: $idle sessions are idle in transactions open for at least five minutes"
  exit 2
fi
echo "OK: no idle transactions over the threshold"

The exit codes follow the Nagios convention, which most monitoring systems accept.

Next#

  • Run pgcue with no command to browse topics, checks and investigations from a menu.
  • Running checks: parameters, JSON output and scripting.
  • Running investigations: step by step or unattended.
  • Topics: where to look for which problem.