pgbouncer (1)
NAME
pgbouncer - Lightweight connection pooler for PostgreSQL.SYNOPSIS
pgbouncer [-d][-R][-v][-u user] <pgbouncer.ini> pgbouncer -V|-h
On Windows computers, the options are:
pgbouncer.exe [-v][-u user] <pgbouncer.ini> pgbouncer.exe -V|-h
Additional options for setting up a Windows service:
pgbouncer.exe -regservice <pgbouncer.ini> pgbouncer.exe -unregservice <pgbouncer.ini>
DESCRIPTION
pgbouncer is a PostgreSQL connection pooler. Any target application can be connected to pgbouncer as if it were a PostgreSQL server, and pgbouncer will create a connection to the actual server, or it will reuse one of its existing connections.
The aim of pgbouncer is to lower the performance impact of opening new connections to PostgreSQL.
In order not to compromise transaction semantics for connection pooling, pgbouncer supports several types of pooling when rotating connections:
Session pooling
- Most polite method. When client connects, a server connection will be assigned to it for the whole duration the client stays connected. When the client disconnects, the server connection will be put back into the pool. This is the default method.
Transaction pooling
- A server connection is assigned to client only during a transaction. When PgBouncer notices that transaction is over, the server connection will be put back into the pool.
Statement pooling
- Most aggressive method. The server connection will be put back into pool immediately after a query completes. Multi-statement transactions are disallowed in this mode as they would break.
The administration interface of pgbouncer consists of some new SHOW commands available when connected to a special virtual database pgbouncer.
QUICK-START
Basic setup and usage as following.
-
1.
Create a pgbouncer.ini file. Details in
pgbouncer(5). Simple example:
-
[databases] template1 = host=127.0.0.1 port=5432 dbname=template1
-
[pgbouncer] listen_port = 6543 listen_addr = 127.0.0.1 auth_type = md5 auth_file = users.txt logfile = pgbouncer.log pidfile = pgbouncer.pid admin_users = someuser
-
-
2.
Create a users.txt file:
-
"someuser" "same_password_as_in_server"
-
-
3.
Launch
pgbouncer:
-
$ pgbouncer -d pgbouncer.ini
-
-
4.
Have your application (or the
psql
client) connect to
pgbouncer
instead of directly to PostgreSQL server.
-
$ psql -p 6543 -U someuser template1
-
-
5.
Manage
pgbouncer
by connecting to the special administration database
pgbouncer
and issuing
show help;
to begin:
-
$ psql -p 6543 -U someuser pgbouncer pgbouncer=# show help; NOTICE: Console usage DETAIL: SHOW [HELP|CONFIG|DATABASES|FDS|POOLS|CLIENTS|SERVERS|SOCKETS|LISTS|VERSION] SET key = arg RELOAD PAUSE SUSPEND RESUME SHUTDOWN
-
-
6.
If you made changes to the pgbouncer.ini file, you can reload it with:
-
pgbouncer=# RELOAD;
-
COMMAND LINE SWITCHES
-d
- Run in background. Without it the process will run in foreground. Note: Does not work on Windows, pgbouncer need to run as service there.
-R
- Do an online restart. That means connecting to the running process, loading the open sockets from it, and then using them. If there is no active process, boot normally. Note: Works only if OS supports Unix sockets and the unix_socket_dir is not disabled in config. Does not work on Windows machines.
-u user
- Switch to the given user on startup.
-v
- Increase verbosity. Can be used multiple times.
-q
- Be quiet - do not log to stdout. Note this does not affect logging verbosity, only that stdout is not to be used. For use in init.d scripts.
-V
- Show version.
-h
- Show short help.
-regservice
- Win32: Register pgbouncer to run as Windows service. The service_name config parameter value is used as name to register under.
-unregservice
- Win32: Unregister Windows service.
ADMIN CONSOLE
The console is available by connecting as normal to the database pgbouncer
-
$ psql -p 6543 pgbouncer
Only users listed in configuration parameters admin_users or stats_users are allowed to login to the console. (Except when auth_mode=any, then any user is allowed in as an admin.)
Additionally, the username pgbouncer is allowed to log in without password, if the login comes via Unix socket and the client has same Unix user uid as the running process.
SHOW COMMANDS
The SHOW commands output information. Each command is described below.
Shows statistics.
database
total_requests
total_received
total_sent
total_query_time
avg_req
avg_recv
avg_sent
avg_query
type
user
database
state
addr
port
local_addr
local_port
connect_time
request_time
ptr
link
type
user
database
state
addr
port
local_addr
local_port
connect_time
request_time
ptr
link
A new pool entry is made for each couple of (database, user).
database
user
cl_active
cl_waiting
sv_active
sv_idle
sv_used
sv_tested
sv_login
maxwait
Show following internal information, in columns (not rows):
databases
users
pools
free_clients
used_clients
login_clients
free_servers
used_servers
Shows one line per user, under the name column name.
name
host
port
database
force_user
pool_size
Shows list of fds in use. When the connected user has username "pgbouncer", connects through Unix socket and has same UID as running process, the actual fds are passed over the connection. This mechanism is used to do an online restart. Note: This does not work on Windows machines.
fd
task
user
database
addr
port
cancel
link
Show the current configuration settings, one per row, with following columns:
key
value
changeable
Show hostnames in DNS cache.
hostname
ttl
addrs
Show DNS zones in cache.
zonename
serial
count
SHOW STATS;
SHOW DNS_HOSTS
SHOW DNS_ZONES
PROCESS CONTROLLING COMMANDS
PgBouncer tries to disconnect from all servers, first waiting for all queries to complete. The command will not return before all queries are finished. To be used at the time of database restart.
If database name is given, only that database will be paused.
Immediately drop all client and server connections on given database.
All socket buffers are flushed and PgBouncer stops listening for data on them. The command will not return before all buffers are empty. To be used at the time of PgBouncer online reboot.
Resume work from previous PAUSE or SUSPEND command.
The PgBouncer process will exit.
The PgBouncer process will reload its configuration file and update changeable settings.
PAUSE [db];
SIGNALS
SIGHUP
- Reload config. Same as issuing command RELOAD; on console.
SIGINT
- Safe shutdown. Same as issuing PAUSE; and SHUTDOWN; on console.
SIGTERM
- Immediate shutdown. Same as issuing SHUTDOWN; on console.
LIBEVENT SETTINGS
From libevent docs:
-
It is possible to disable support for epoll, kqueue, devpoll, poll or select by setting the environment variable EVENT_NOEPOLL, EVENT_NOKQUEUE, EVENT_NODEVPOLL, EVENT_NOPOLL or EVENT_NOSELECT, respectively.
-
By setting the environment variable EVENT_SHOW_METHOD, libevent displays the kernel notification method that it uses.
SEE ALSO
pgbouncer(5) - manpage of configuration settings descriptions.
m[blue]http://wiki.postgresql.org/wiki/PgBouncerm[]