postgres

package
v0.7.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package postgres implements the "postgres" protocol.Protocol driver, for load testing a PostgreSQL server (or a database that speaks its wire protocol, such as CockroachDB, YugabyteDB or Aurora). It uses the pgx client, which is pure Go.

Each call to Do runs one SQL text, read from -body, and reads every row it returns. The connections are kept in a pool that all users share, as an application's connection pool is. Waiting for a free connection counts as part of the measured time, so give -opt pool at least as many connections as users when you want to measure the server and not the queue.

Options (set with -opt key=value):

username          The database user. It can also be given in the URL.
password_env      The name of an environment variable that holds the
                  password. It needs a user. The password is never put on
                  the command line, and it is refused in the URL.
database          The database. It can also be given as the path of the
                  URL. The default is the user name.
sslmode           disable, prefer (default), require, or verify-full.
                  With -insecure, verify-full does not check the
                  certificate.
application_name  The name the server shows for these connections
                  (default "vegaload").
pool              The most connections the run opens (default 10).
allow_writes      true to let the SQL change data. The default is
                  read-only: every transaction is read-only, so a load
                  test cannot change data by mistake. A statement that
                  writes then fails with SQLSTATE 25006. This is a
                  safety net, not a lock: the SQL can still ask for a
                  read-write transaction itself (begin read write), or
                  turn the setting off and write in the same text (set
                  default_transaction_read_only = off; ...). Each call
                  puts the setting back before its SQL runs, so one call
                  cannot leave writes on for the next.
query_mode        simple (default) or extended. simple sends the SQL
                  as one text, so it can hold several statements such as
                  "begin; update ... where id = $1; select ...; commit",
                  with or without args. VegaLoad puts the args into the
                  text as quoted values. extended prepares the statement
                  once for each connection and reuses it, as most
                  applications do. It runs one statement.
args              A JSON array of values for $1, $2 and so on, such as
                  '[42, "abc"]'.
min_rows          The result must have at least this many rows.
expect            Some value in the result must contain this text. Only
                  the first max_rows rows are checked.
max_rows          How many rows a scenario script gets back (default
                  1000). Every row is read and counted either way.

The target is postgres://host[:port][/database] (or postgresql://), with port 5432 by default. The URL may end with ?sslmode=... and ?application_name=..., as in psql. Any other setting after the "?" is refused.

Index

Constants

This section is empty.

Variables

View Source
var Options = []string{
	"username", "password_env", "database", "sslmode", "application_name",
	"pool", "allow_writes", "query_mode", "args", "min_rows", "expect", "max_rows",
}

Options are the -opt keys this driver accepts. The command's protocol table uses the same list, so the two cannot differ.

Functions

This section is empty.

Types

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver is a PostgreSQL protocol.Protocol.

func New

func New(target protocol.Target, timeout time.Duration) (*Driver, error)

New returns a ready-to-use Driver for target. target.URL is postgres://host[:port][/database]. The SQL is target.Body.

New returns an error only for a configuration problem, never for the server being unreachable, which Do reports per call.

func NewConn

func NewConn(target protocol.Target, timeout time.Duration, password *string) (*Driver, error)

NewConn is New for a scenario script. It makes only the connection pool: the server, the login and the connection options. It needs no SQL. The job of each call is then set with Call. password is the password, which the script already holds, so password_env is not used with it.

func (*Driver) Call

func (d *Driver) Call(opts map[string]string, body []byte, timeout time.Duration) (*Driver, error)

Call returns a Driver for one call of a script: the same pool as d, with the job that opts and body describe. opts must hold the same connection options d was made with. The result shares d's pool, so closing it is d's job, not the caller's.

func (*Driver) Close

func (d *Driver) Close() error

Close implements protocol.Protocol. It closes the connection pool.

func (*Driver) Do

func (d *Driver) Do(parent context.Context) (protocol.Result, error)

Do implements protocol.Protocol.

func (*Driver) Name

func (d *Driver) Name() string

Name implements protocol.Protocol.

func (*Driver) Run

func (d *Driver) Run(parent context.Context) (protocol.Result, Reply)

Run is Do, and it also returns the rows of the call. A load test does not need them. A scenario script does.

type Reply

type Reply struct {
	// Columns are the column names of the result.
	Columns []string
	// Rows are the first max_rows rows, each as plain values in column order.
	Rows [][]any
	// RowCount is how many rows the result had, including rows past max_rows.
	RowCount int
	// RowsAffected adds up the row counts of the command tags of all the
	// statements: INSERT, UPDATE, DELETE and SELECT.
	RowsAffected int64
	// CommandTag is the tag of the last statement, such as "SELECT 3".
	CommandTag string
}

Reply is what a call read, besides the result.

Directories

Path Synopsis
Package postgrestest is a small fake PostgreSQL server for tests, in this package and in others.
Package postgrestest is a small fake PostgreSQL server for tests, in this package and in others.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL