Skip to content
Latchkey

How to Benchmark Database Queries in GitHub Actions

A Postgres service container plus pgbench gives a repeatable query benchmark with latency and TPS you can trend across commits.

Start Postgres as a service container, load a schema, then run pgbench with a custom script. It reports average latency and transactions per second for the workload.

Steps

  • Add a postgres service container with a health check.
  • Initialize a schema, then run pgbench with -f query.sql.
  • Read the latency and TPS lines from the pgbench output.

Workflow

.github/workflows/ci.yml
jobs:
  db-bench:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_PASSWORD: bench
        ports: ['5432:5432']
        options: >-
          --health-cmd "pg_isready" --health-interval 10s --health-retries 5
    env:
      PGPASSWORD: bench
    steps:
      - uses: actions/checkout@v4
      - run: psql -h localhost -U postgres -f schema.sql
      - run: pgbench -h localhost -U postgres -c 10 -T 30 -f query.sql postgres

Gotchas

  • pgbench is part of the postgresql-client package; install it if the runner lacks it.
  • A cold cache skews the first run; add a warmup or use --no-vacuum consistently.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →